entropy: outsource code
authorPhil Sutter <phil@nwl.cc>
Thu, 12 Nov 2009 22:50:17 +0000 (23:50 +0100)
committerPhil Sutter <phil@nwl.cc>
Thu, 12 Nov 2009 23:02:16 +0000 (00:02 +0100)
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.

14 files changed:
src/Makefile.am
src/conky.c
src/conky.h
src/core.c
src/entropy.c [new file with mode: 0644]
src/entropy.h [new file with mode: 0644]
src/freebsd.c
src/freebsd.h
src/linux.c
src/linux.h
src/netbsd.c
src/netbsd.h
src/openbsd.c
src/openbsd.h

index a11fd41..af9f1bd 100644 (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 \
index 072383f..c0a4254 100644 (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) {
index e192f41..8bca772 100644 (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
index 3c78937..489c7ba 100644 (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>
 
diff --git a/src/entropy.c b/src/entropy.c
new file mode 100644 (file)
index 0000000..8cf2bc2
--- /dev/null
@@ -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));
+}
diff --git a/src/entropy.h b/src/entropy.h
new file mode 100644 (file)
index 0000000..5cc90ef
--- /dev/null
@@ -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 */
index 8d13962..2c29273 100644 (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 */
index 54a981b..063b131 100644 (file)
@@ -15,4 +15,7 @@
 
 kvm_t *kd;
 
+int get_entropy_avail(unsigned int *);
+int get_entropy_poolsize(unsigned int *);
+
 #endif /*FREEBSD_H_*/
index 249ada2..7c02e42 100644 (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)
index 323528a..ecd1bfa 100644 (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 */
index dc4e0f7..948ff59 100644 (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;
 }
index 65f303c..908bf55 100644 (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_*/
index e2f6cc2..6ca8453 100644 (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)
index 0f7a3ac..a60aead 100644 (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_*/