From e787870eb438bd875ceb408d84aca0444396e132 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Thu, 12 Nov 2009 21:32:59 +0100 Subject: [PATCH 1/2] top: fix segfault for $if_running The "name" field may be zero, which makes strcmp() freak out. --- src/top.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/top.c b/src/top.c index 980cc4c8..45965571 100644 --- a/src/top.c +++ b/src/top.c @@ -131,7 +131,7 @@ struct process *get_process_by_name(const char *name) struct process *p = first_process; while (p) { - if (!strcmp(p->name, name)) + if (p->name && !strcmp(p->name, name)) return p; p = p->next; } From 8922603b9196f3648f11fdbe386d1bcf0675cabf Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Thu, 12 Nov 2009 23:50:17 +0100 Subject: [PATCH 2/2] 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. --- src/Makefile.am | 6 ++-- src/conky.c | 20 +++-------- src/conky.h | 6 ---- src/core.c | 4 +-- src/entropy.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++ src/entropy.h | 41 ++++++++++++++++++++++ src/freebsd.c | 11 +++++- src/freebsd.h | 3 ++ src/linux.c | 42 ++++++++++++++--------- src/linux.h | 3 ++ src/netbsd.c | 8 ++++- src/netbsd.h | 3 ++ src/openbsd.c | 8 ++++- src/openbsd.h | 3 ++ 14 files changed, 202 insertions(+), 47 deletions(-) create mode 100644 src/entropy.c create mode 100644 src/entropy.h diff --git a/src/Makefile.am b/src/Makefile.am index a11fd415..af9f1bde 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 \ diff --git a/src/conky.c b/src/conky.c index 072383fb..c0a4254b 100644 --- a/src/conky.c +++ b/src/conky.c @@ -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) { diff --git a/src/conky.h b/src/conky.h index e192f413..8bca772a 100644 --- a/src/conky.h +++ b/src/conky.h @@ -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 diff --git a/src/core.c b/src/core.c index 3c78937e..489c7ba0 100644 --- a/src/core.c +++ b/src/core.c @@ -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 #include diff --git a/src/entropy.c b/src/entropy.c new file mode 100644 index 00000000..8cf2bc2c --- /dev/null +++ b/src/entropy.c @@ -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 . + * + */ + +#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)); +} diff --git a/src/entropy.h b/src/entropy.h new file mode 100644 index 00000000..5cc90efb --- /dev/null +++ b/src/entropy.h @@ -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 . + * + */ + +#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 */ diff --git a/src/freebsd.c b/src/freebsd.c index 8d13962d..2c29273d 100644 --- a/src/freebsd.c +++ b/src/freebsd.c @@ -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 */ diff --git a/src/freebsd.h b/src/freebsd.h index 54a981b2..063b1317 100644 --- a/src/freebsd.h +++ b/src/freebsd.h @@ -15,4 +15,7 @@ kvm_t *kd; +int get_entropy_avail(unsigned int *); +int get_entropy_poolsize(unsigned int *); + #endif /*FREEBSD_H_*/ diff --git a/src/linux.c b/src/linux.c index 249ada2e..7c02e42d 100644 --- a/src/linux.c +++ b/src/linux.c @@ -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) diff --git a/src/linux.h b/src/linux.h index 323528af..ecd1bfa0 100644 --- a/src/linux.h +++ b/src/linux.h @@ -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 */ diff --git a/src/netbsd.c b/src/netbsd.c index dc4e0f71..948ff59e 100644 --- a/src/netbsd.c +++ b/src/netbsd.c @@ -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; } diff --git a/src/netbsd.h b/src/netbsd.h index 65f303c8..908bf55e 100644 --- a/src/netbsd.h +++ b/src/netbsd.h @@ -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_*/ diff --git a/src/openbsd.c b/src/openbsd.c index e2f6cc20..6ca84535 100644 --- a/src/openbsd.c +++ b/src/openbsd.c @@ -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) diff --git a/src/openbsd.h b/src/openbsd.h index 0f7a3ac1..a60aeade 100644 --- a/src/openbsd.h +++ b/src/openbsd.h @@ -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_*/