here comes the big header include rewrite
[monky] / src / common.c
index 7c01957..aff0bcd 100644 (file)
@@ -1,31 +1,75 @@
-/*
- * Conky, a system monitor, based on torsmo
+/* Conky, a system monitor, based on torsmo
  *
- * This program is licensed under BSD license, read COPYING
+ * 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-2008 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/>.
  *
- *  $Id$
  */
 
+#include "config.h"
 #include "conky.h"
-#include "remoted.h"
-#include "remotec.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include "fs.h"
+#include "logging.h"
 #include <ctype.h>
 #include <errno.h>
 #include <sys/time.h>
+#include <pthread.h>
 
-struct information info;
+/* 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
+
+/* OS specific prototypes to be implemented by linux.c & Co. */
+void update_entropy(void);
 
-void update_uname()
+#ifndef HAVE_STRNDUP
+// use our own strndup() if it's not available
+char *strndup(const char *s, size_t n)
+{
+       if (strlen(s) > n) {
+               char *ret = malloc(n + 1);
+               strncpy(ret, s, n);
+               ret[n] = 0;
+               return ret;
+       } else {
+               return strdup(s);
+       }
+}
+#endif /* HAVE_STRNDUP */
+
+void update_uname(void)
 {
        uname(&info.uname_s);
 }
 
-double get_time()
+double get_time(void)
 {
        struct timeval tv;
+
        gettimeofday(&tv, 0);
        return tv.tv_sec + (tv.tv_usec / 1000000.0);
 }
@@ -33,11 +77,13 @@ double get_time()
 FILE *open_file(const char *file, int *reported)
 {
        FILE *fp = fopen(file, "r");
+
        if (!fp) {
                if (!reported || *reported == 0) {
                        ERR("can't open %s: %s", file, strerror(errno));
-                       if (reported)
+                       if (reported) {
                                *reported = 1;
+                       }
                }
                return 0;
        }
@@ -59,13 +105,14 @@ void variable_substitute(const char *s, char *dest, unsigned int n)
                                if (*s == '{') {
                                        s++;
                                        a = s;
-                                       while (*s && *s != '}')
+                                       while (*s && *s != '}') {
                                                s++;
+                                       }
                                } else {
                                        a = s;
-                                       while (*s && (isalnum((int) *s)
-                                                     || *s == '_'))
+                                       while (*s && (isalnum((int) *s) || *s == '_')) {
                                                s++;
+                                       }
                                }
 
                                /* copy variable to buffer and look it up */
@@ -73,16 +120,18 @@ void variable_substitute(const char *s, char *dest, unsigned int n)
                                strncpy(buf, a, len);
                                buf[len] = '\0';
 
-                               if (*s == '}')
+                               if (*s == '}') {
                                        s++;
+                               }
 
                                var = getenv(buf);
 
                                if (var) {
                                        /* add var to dest */
                                        len = strlen(var);
-                                       if (len >= n)
+                                       if (len >= n) {
                                                len = n - 1;
+                                       }
                                        strncpy(dest, var, len);
                                        dest += len;
                                        n -= len;
@@ -106,20 +155,22 @@ struct net_stat *get_net_stat(const char *dev)
 {
        unsigned int i;
 
-       if (!dev)
+       if (!dev) {
                return 0;
+       }
 
        /* find interface stat */
        for (i = 0; i < 16; i++) {
-               if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0)
+               if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
                        return &netstats[i];
+               }
        }
 
        /* wasn't found? add it */
        if (i == 16) {
                for (i = 0; i < 16; i++) {
                        if (netstats[i].dev == 0) {
-                               netstats[i].dev = strdup(dev);
+                               netstats[i].dev = strndup(dev, text_buffer_size);
                                return &netstats[i];
                        }
                }
@@ -129,42 +180,112 @@ struct net_stat *get_net_stat(const char *dev)
        return 0;
 }
 
-void format_seconds(char *buf, unsigned int n, long t)
+void clear_net_stats(void)
 {
-       if (t >= 24 * 60 * 60)  /* hours necessary when there are days? */
-               snprintf(buf, n, "%ldd %ldh %ldm", t / 60 / 60 / 24,
-                        (t / 60 / 60) % 24, (t / 60) % 60);
-       else if (t >= 60 * 60)
-               snprintf(buf, n, "%ldh %ldm", (t / 60 / 60) % 24,
-                        (t / 60) % 60);
-       else
-               snprintf(buf, n, "%ldm %lds", t / 60, t % 60);
+       memset(netstats, 0, sizeof(netstats));
+}
+
+void free_dns_data(void)
+{
+       int i;
+       struct dns_data *data = &info.nameserver_info;
+       for (i = 0; i < data->nscount; i++)
+               free(data->ns_list[i]);
+       if (data->ns_list)
+               free(data->ns_list);
+       memset(data, 0, sizeof(struct dns_data));
 }
 
-void format_seconds_short(char *buf, unsigned int n, long t)
+//static double last_dns_update;
+
+static void update_dns_data(void)
 {
-       if (t >= 24 * 60 * 60)
-               snprintf(buf, n, "%ldd %ldh", t / 60 / 60 / 24,
-                        (t / 60 / 60) % 24);
-       else if (t >= 60 * 60)
-               snprintf(buf, n, "%ldh %ldm", (t / 60 / 60) % 24,
-                        (t / 60) % 60);
+       FILE *fp;
+       char line[256];
+       struct dns_data *data = &info.nameserver_info;
+
+       /* maybe updating too often causes higher load because of /etc lying on a real FS
+       if (current_update_time - last_dns_update < 10.0)
+               return;
        else
-               snprintf(buf, n, "%ldm", t / 60);
+               last_dns_update = current_update_time;
+       */
+
+       free_dns_data();
+
+       if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
+               return;
+       while(!feof(fp)) {
+               if (fgets(line, 255, fp) == NULL) {
+                       break;
+               }
+               if (!strncmp(line, "nameserver ", 11)) {
+                       line[strlen(line) - 1] = '\0';  // remove trailing newline
+                       data->nscount++;
+                       data->ns_list = realloc(data->ns_list, data->nscount * sizeof(char *));
+                       data->ns_list[data->nscount - 1] = strndup(line + 11, text_buffer_size);
+               }
+       }
+       fclose(fp);
+}
+
+void format_seconds(char *buf, unsigned int n, long seconds)
+{
+       long days;
+       int hours, minutes;
+
+       days = seconds / 86400;
+       seconds %= 86400;
+       hours = seconds / 3600;
+       seconds %= 3600;
+       minutes = seconds / 60;
+       seconds %= 60;
+
+       if (days > 0) {
+               snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
+       } else {
+               snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
+       }
+}
+
+void format_seconds_short(char *buf, unsigned int n, long seconds)
+{
+       long days;
+       int hours, minutes;
+
+       days = seconds / 86400;
+       seconds %= 86400;
+       hours = seconds / 3600;
+       seconds %= 3600;
+       minutes = seconds / 60;
+       seconds %= 60;
+
+       if (days > 0) {
+               snprintf(buf, n, "%ldd %dh", days, hours);
+       } else if (hours > 0) {
+               snprintf(buf, n, "%dh %dm", hours, minutes);
+       } else {
+               snprintf(buf, n, "%dm %lds", minutes, seconds);
+       }
 }
 
 static double last_meminfo_update;
 static double last_fs_update;
 
 unsigned long long need_mask;
+int no_buffers;
 
-void update_stuff()
+#define NEED(a) ((need_mask & (1 << a)) && ((info.mask & (1 << a)) == 0))
+
+void update_stuff(void)
 {
        unsigned int i;
+
        info.mask = 0;
 
-       if (no_buffers)
+       if (no_buffers) {
                need_mask |= 1 << INFO_BUFFERS;
+       }
 
        /* clear speeds and up status in case device was removed and doesn't get
         * updated */
@@ -178,78 +299,112 @@ void update_stuff()
        }
 
        prepare_update();
-       /* client(); this is approximately where the client should be called */
-#define NEED(a) ((need_mask & (1 << a)) && ((info.mask & (1 << a)) == 0))
 
-       if (NEED(INFO_UPTIME))
+       if (NEED(INFO_UPTIME)) {
                update_uptime();
+       }
 
-       if (NEED(INFO_PROCS))
+       if (NEED(INFO_PROCS)) {
                update_total_processes();
+       }
 
-       if (NEED(INFO_RUN_PROCS))
+       if (NEED(INFO_RUN_PROCS)) {
                update_running_processes();
+       }
 
-       if (NEED(INFO_CPU))
+       if (NEED(INFO_CPU)) {
                update_cpu_usage();
+       }
 
-       if (NEED(INFO_NET))
+       if (NEED(INFO_NET)) {
                update_net_stats();
+       }
 
-       if (NEED(INFO_DISKIO))
+       if (NEED(INFO_DISKIO)) {
                update_diskio();
-
-       if (NEED(INFO_WIFI))
-               update_wifi_stats();
-
-       if (NEED(INFO_MAIL))
-               update_mail_count();
-
-
+       }
 
 #if defined(__linux__)
-       if (NEED(INFO_I8K))
+       if (NEED(INFO_I8K)) {
                update_i8k();
+       }
 #endif /* __linux__ */
-       
-#ifdef MLDONKEY
-       if (NEED(INFO_MLDONKEY))
-               get_mldonkey_status(&mlconfig, &mlinfo);
-#endif
 
-#ifdef SETI
-       if (NEED(INFO_SETI))
-               update_seti();
+#ifdef MPD
+       if (NEED(INFO_MPD)) {
+               if (!info.mpd.timed_thread) {
+                       init_mpd_stats(&info.mpd);
+                       info.mpd.timed_thread = timed_thread_create(&update_mpd,
+                               (void *) &info.mpd, info.music_player_interval * 1000000);
+                       if (!info.mpd.timed_thread) {
+                               ERR("Failed to create MPD timed thread");
+                       }
+                       timed_thread_register(info.mpd.timed_thread, &info.mpd.timed_thread);
+                       if (timed_thread_run(info.mpd.timed_thread)) {
+                               ERR("Failed to run MPD timed thread");
+                       }
+               }
+       }
 #endif
 
-#ifdef MPD
-       if (NEED(INFO_MPD))
-               update_mpd();
+#ifdef MOC
+  if (NEED(INFO_MOC)) {
+    if (!info.moc.timed_thread) {
+      init_moc(&info.moc);
+      info.moc.timed_thread = timed_thread_create(&update_moc,
+        (void *) &info.moc, info.music_player_interval * 100000);
+      if (!info.moc.timed_thread) {
+        ERR("Failed to create MOC timed thread");
+      }
+      timed_thread_register(info.moc.timed_thread, &info.moc.timed_thread);
+      if (timed_thread_run(info.moc.timed_thread)) {
+        ERR("Failed to run MOC timed thread");
+      }
+    }
+  }
+#endif
+  
+#ifdef XMMS2
+       if (NEED(INFO_XMMS2)) {
+               update_xmms2();
+       }
 #endif
-#if defined(XMMS) || defined(BMP) || defined(AUDACIOUS) || defined(INFOPIPE)
-       if (NEED(INFO_XMMS))
-               update_xmms();
+
+#ifdef AUDACIOUS
+       if (NEED(INFO_AUDACIOUS)) {
+               update_audacious();
+       }
 #endif
+
 #ifdef BMPX
-       if (NEED(INFO_BMPX))
-                update_bmpx();
+       if (NEED(INFO_BMPX)) {
+               update_bmpx();
+       }
 #endif
 
-       if (NEED(INFO_LOADAVG))
+       if (NEED(INFO_LOADAVG)) {
                update_load_average();
+       }
 
-
-       if ((NEED(INFO_MEM) || NEED(INFO_BUFFERS) || NEED(INFO_TOP)) &&
-           current_update_time - last_meminfo_update > 6.9) {
+       if ((NEED(INFO_MEM) || NEED(INFO_BUFFERS) || NEED(INFO_TOP))
+                       && current_update_time - last_meminfo_update > 6.9) {
                update_meminfo();
                if (no_buffers) {
                        info.mem -= info.bufmem;
+                       info.memeasyfree += info.bufmem;
                }
                last_meminfo_update = current_update_time;
        }
+       
+#ifdef X11
+       if (NEED(INFO_X11)) {
+               update_x11info();
+       }
+#endif
 
-       if (NEED(INFO_TOP))
+       if (NEED(INFO_TOP)) {
                update_top();
+       }
 
        /* update_fs_stat() won't do anything if there aren't fs -things */
        if (NEED(INFO_FS) && current_update_time - last_fs_update > 12.9) {
@@ -257,18 +412,31 @@ void update_stuff()
                last_fs_update = current_update_time;
        }
 #ifdef TCP_PORT_MONITOR
-       if (NEED(INFO_TCP_PORT_MONITOR))
-               update_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
+       if (NEED(INFO_TCP_PORT_MONITOR)) {
+               tcp_portmon_update();
+       }
 #endif
+       if (NEED(INFO_ENTROPY)) {
+               update_entropy();
+       }
+#if defined(__linux__)
+       if (NEED(INFO_USERS)) {
+               update_users();
+       }
+       if (NEED(INFO_GW)) {
+               update_gateway_info();
+       }
+#endif /* __linux__ */
+       if (NEED(INFO_DNS)) {
+               update_dns_data();
+       }
 }
 
 int round_to_int(float f)
 {
-    int intval = (int)f;
-    double delta = f - intval;
-    if (!(delta < 0.5)) {
-        ++intval;
-    }
-
-    return intval;
+       if (f >= 0.0) {
+               return (int) (f + 0.5);
+       } else {
+               return (int) (f - 0.5);
+       }
 }