1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2025-01-27 09:08:25 +00:00

entropy: outsource code

This patch ought to be small and simple ...

The reason why it's not is me wanting the entropy data out of struct
information. This means update_entropy() can not be used anymore, as it
uses this globally available object.

The solution I am presenting here is quite messy regarding header
includes. Hopefully this will go away soon as I plan on creating some
sort of "OS library" containing all OS specific routines and defining
macros for easier capability checking in the non-specific code. This on
the other hand means we'll need "wrappers" around OS specific objects,
but that's not as bad as it seems - having non-specific text objects
only will definitely clean up the code, and capabilities can be checked
where they should be.
This commit is contained in:
Phil Sutter 2009-11-12 23:50:17 +01:00
parent e787870eb4
commit 8922603b91
14 changed files with 202 additions and 47 deletions

View File

@ -50,9 +50,9 @@ endif # BUILD_CONFIG_OUTPUT
# source files always needed for compiling
mandatory_sources = colours.c colours.h combine.c combine.h common.c common.h \
conky.c conky.h core.c core.h diskio.c diskio.h exec.c exec.h fs.c \
fs.h logging.h mail.c mail.h mixer.c mixer.h net_stat.c net_stat.h \
template.c template.h timed_thread.c timed_thread.h mboxscan.c \
conky.c conky.h core.c core.h diskio.c diskio.h entropy.c entropy.h \
exec.c exec.h fs.c fs.h logging.h mail.c mail.h mixer.c mixer.h net_stat.c \
net_stat.h template.c template.h timed_thread.c timed_thread.h mboxscan.c \
mboxscan.h read_tcp.c read_tcp.h scroll.c scroll.h specials.c \
specials.h tailhead.c tailhead.h temphelper.c temphelper.h \
text_object.c text_object.h timeinfo.c timeinfo.h algebra.c \

View File

@ -78,6 +78,7 @@
#include "colours.h"
#include "combine.h"
#include "diskio.h"
#include "entropy.h"
#include "exec.h"
#include "proc.h"
#ifdef X11
@ -2119,27 +2120,16 @@ void generate_text_internal(char *p, int p_max_size,
#endif /* HAVE_ICONV */
OBJ(entropy_avail) {
snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
print_entropy_avail(obj, p, p_max_size);
}
OBJ(entropy_perc) {
percent_print(p, p_max_size,
cur->entropy.entropy_avail *
100 / cur->entropy.poolsize);
print_entropy_perc(obj, p, p_max_size);
}
OBJ(entropy_poolsize) {
snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
print_entropy_poolsize(obj, p, p_max_size);
}
OBJ(entropy_bar) {
double entropy_perc;
entropy_perc = (double) cur->entropy.entropy_avail /
(double) cur->entropy.poolsize;
#ifdef X11
if(output_methods & TO_X) {
new_bar(obj, p, (int) (entropy_perc * 255.0f));
} else
#endif /* X11 */
new_bar_in_shell(obj, p, p_max_size, (int) (entropy_perc * 100.0f));
print_entropy_bar(obj, p, p_max_size);
}
#ifdef IBM
OBJ(smapi) {

View File

@ -132,11 +132,6 @@ struct text_object;
* one doesn't know what to choose. Defaults to 256. */
extern unsigned int text_buffer_size;
struct entropy_s {
unsigned int entropy_avail;
unsigned int poolsize;
};
struct usr_info {
char *names;
char *times;
@ -262,7 +257,6 @@ struct information {
#endif
struct process *first_process;
unsigned long looped;
struct entropy_s entropy;
double music_player_interval;
#ifdef X11

View File

@ -36,6 +36,7 @@
#include "colours.h"
#include "combine.h"
#include "diskio.h"
#include "entropy.h"
#include "exec.h"
#include "proc.h"
#ifdef X11
@ -75,9 +76,6 @@
#include "openbsd.h"
#endif
/* OS specific prototypes to be implemented by linux.c & Co. */
void update_entropy(void);
#include <string.h>
#include <ctype.h>

91
src/entropy.c Normal file
View File

@ -0,0 +1,91 @@
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
* vim: ts=4 sw=4 noet ai cindent syntax=c
*
* Conky, a system monitor, based on torsmo
*
* Any original torsmo code is licensed under the BSD license
*
* All code written since the fork of torsmo is licensed under the GPL
*
* Please see COPYING for details
*
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
* Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
* (see AUTHORS)
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include "conky.h"
#include "text_object.h"
/* check for OS and include appropriate headers */
#if defined(__linux__)
#include "linux.h"
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
#include "freebsd.h"
#elif defined(__OpenBSD__)
#include "openbsd.h"
#endif
static struct {
unsigned int avail;
unsigned int poolsize;
} entropy = {
.avail = 0,
.poolsize = 0,
};
void update_entropy(void)
{
get_entropy_avail(&entropy.avail);
get_entropy_poolsize(&entropy.poolsize);
}
void print_entropy_avail(struct text_object *obj, char *p, int p_max_size)
{
(void)obj;
snprintf(p, p_max_size, "%u", entropy.avail);
}
void print_entropy_perc(struct text_object *obj, char *p, int p_max_size)
{
(void)obj;
percent_print(p, p_max_size, entropy.avail *
100 / entropy.poolsize);
}
void print_entropy_poolsize(struct text_object *obj, char *p, int p_max_size)
{
(void)obj;
snprintf(p, p_max_size, "%u", entropy.poolsize);
}
void print_entropy_bar(struct text_object *obj, char *p, int p_max_size)
{
double ratio;
(void)obj;
ratio = (double) entropy.avail /
(double) entropy.poolsize;
#ifdef X11
if(output_methods & TO_X) {
new_bar(obj, p, (int) (ratio * 255.0f));
} else
#endif /* X11 */
new_bar_in_shell(obj, p, p_max_size, (int) (ratio * 100.0f));
}

41
src/entropy.h Normal file
View File

@ -0,0 +1,41 @@
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
* vim: ts=4 sw=4 noet ai cindent syntax=c
*
* Conky, a system monitor, based on torsmo
*
* Any original torsmo code is licensed under the BSD license
*
* All code written since the fork of torsmo is licensed under the GPL
*
* Please see COPYING for details
*
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
* Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
* (see AUTHORS)
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef _ENTROPY_H
#define _ENTROPY_H
void update_entropy(void);
void print_entropy_avail(struct text_object *, char *, int);
void print_entropy_perc(struct text_object *, char *, int);
void print_entropy_poolsize(struct text_object *, char *, int);
void print_entropy_bar(struct text_object *, char *, int);
#endif /* _ENTROPY_H */

View File

@ -960,9 +960,18 @@ void get_battery_short_status(char *buffer, unsigned int n, const char *bat)
}
}
void update_entropy(void)
int get_entropy_avail(unsigned int *val)
{
/* Not applicable for FreeBSD as it uses the yarrow prng. */
(void)val;
return 1;
}
int get_entropy_poolsize(unsigned int *val)
{
/* Not applicable for FreeBSD as it uses the yarrow prng. */
(void)val;
return 1;
}
/* empty stub so conky links */

View File

@ -15,4 +15,7 @@
kvm_t *kd;
int get_entropy_avail(unsigned int *);
int get_entropy_poolsize(unsigned int *);
#endif /*FREEBSD_H_*/

View File

@ -2261,30 +2261,38 @@ void update_top(void)
info.first_process = get_first_process();
}
void update_entropy(void)
#define ENTROPY_AVAIL_PATH "/proc/sys/kernel/random/entropy_avail"
int get_entropy_avail(unsigned int *val)
{
static int rep = 0;
const char *entropy_avail = "/proc/sys/kernel/random/entropy_avail";
const char *entropy_poolsize = "/proc/sys/kernel/random/poolsize";
FILE *fp1, *fp2;
FILE *fp;
info.entropy.entropy_avail = 0;
info.entropy.poolsize = 0;
if (!(fp = open_file(ENTROPY_AVAIL_PATH, &rep)))
return 1;
if ((fp1 = open_file(entropy_avail, &rep)) == NULL) {
return;
}
if (fscanf(fp, "%u", val) != 1)
return 1;
if ((fp2 = open_file(entropy_poolsize, &rep)) == NULL) {
fclose(fp1);
return;
}
fclose(fp);
return 0;
}
fscanf(fp1, "%u", &info.entropy.entropy_avail);
fscanf(fp2, "%u", &info.entropy.poolsize);
#define ENTROPY_POOLSIZE_PATH "/proc/sys/kernel/random/poolsize"
fclose(fp1);
fclose(fp2);
int get_entropy_poolsize(unsigned int *val)
{
static int rep = 0;
FILE *fp;
if (!(fp = open_file(ENTROPY_POOLSIZE_PATH, &rep)))
return 1;
if (fscanf(fp, "%u", val) != 1)
return 1;
fclose(fp);
return 0;
}
const char *get_disk_protect_queue(const char *disk)

View File

@ -40,4 +40,7 @@ void parse_platform_sensor(struct text_object *, const char *);
void print_sysfs_sensor(struct text_object *, char *, int );
void free_sysfs_sensor(struct text_object *);
int get_entropy_avail(unsigned int *);
int get_entropy_poolsize(unsigned int *);
#endif /* _LINUX_H */

View File

@ -353,6 +353,12 @@ void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
memset(p_client_buffer, 0, client_buffer_size);
}
void update_entropy(void)
int get_entropy_avail(unsigned int *val)
{
return 1;
}
int get_entropy_poolsize(unsigned int *val)
{
return 1;
}

View File

@ -30,4 +30,7 @@
#include "conky.h"
#include "common.h"
int get_entropy_avail(unsigned int *);
int get_entropy_poolsize(unsigned int *);
#endif /*NETBSD_H_*/

View File

@ -958,8 +958,14 @@ void prepare_update()
{
}
void update_entropy(void)
int get_entropy_avail(unsigned int *val)
{
return 1;
}
int get_entropy_poolsize(unsigned int *val)
{
return 1;
}
void free_all_processes(void)

View File

@ -20,4 +20,7 @@ void get_obsd_product(char *buf, size_t client_buffer_size);
typedef struct apm_power_info *apm_info_t;
#endif
int get_entropy_avail(unsigned int *);
int get_entropy_poolsize(unsigned int *);
#endif /*OPENBSD_H_*/