Bugfix: ac-adapter is sometimes called ADP1 in sysfs instead of AC
[monky] / src / linux.c
index 1180bee..3185217 100644 (file)
@@ -11,7 +11,7 @@
  *
  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
  * Copyright (c) 2007 Toni Spets
- * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
+ * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
  *     (see AUTHORS)
  * All rights reserved.
  *
@@ -63,6 +63,7 @@
 #endif
 #include <linux/route.h>
 #include <math.h>
+#include <pthread.h>
 
 /* The following ifdefs were adapted from gkrellm */
 #include <linux/major.h>
@@ -464,12 +465,6 @@ void update_net_stats(void)
                        curtmp1 = curtmp1 + ns->net_rec[i];
                        curtmp2 = curtmp2 + ns->net_trans[i];
                }
-               if (curtmp1 == 0) {
-                       curtmp1 = 1;
-               }
-               if (curtmp2 == 0) {
-                       curtmp2 = 1;
-               }
                ns->recv_speed = curtmp1 / (double) info.net_avg_samples;
                ns->trans_speed = curtmp2 / (double) info.net_avg_samples;
                if (info.net_avg_samples > 1) {
@@ -678,14 +673,20 @@ void update_stat(void)
        const char *stat_template = NULL;
        unsigned int malloc_cpu_size = 0;
        extern void* global_cpu;
+
+       static pthread_mutex_t last_stat_update_mutex = PTHREAD_MUTEX_INITIALIZER;
        static double last_stat_update = 0.0;
 
        /* since we use wrappers for this function, the update machinery
         * can't eliminate double invocations of this function. Check for
         * them here, otherwise cpu_usage counters are freaking out. */
-       if (last_stat_update == current_update_time)
+       pthread_mutex_lock(&last_stat_update_mutex);
+       if (last_stat_update == current_update_time) {
+               pthread_mutex_unlock(&last_stat_update_mutex);
                return;
+       }
        last_stat_update = current_update_time;
+       pthread_mutex_unlock(&last_stat_update_mutex);
 
        /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
        if (!cpu_setup || !info.cpu_usage) {
@@ -819,40 +820,6 @@ void update_load_average(void)
        }
 }
 
-#define PROC_I8K "/proc/i8k"
-#define I8K_DELIM " "
-static char *i8k_procbuf = NULL;
-void update_i8k(void)
-{
-       FILE *fp;
-
-       if (!i8k_procbuf) {
-               i8k_procbuf = (char *) malloc(128 * sizeof(char));
-       }
-       if ((fp = fopen(PROC_I8K, "r")) == NULL) {
-               CRIT_ERR(NULL, NULL, "/proc/i8k doesn't exist! use insmod to make sure the kernel "
-                       "driver is loaded...");
-       }
-
-       memset(&i8k_procbuf[0], 0, 128);
-       if (fread(&i8k_procbuf[0], sizeof(char), 128, fp) == 0) {
-               NORM_ERR("something wrong with /proc/i8k...");
-       }
-
-       fclose(fp);
-
-       i8k.version = strtok(&i8k_procbuf[0], I8K_DELIM);
-       i8k.bios = strtok(NULL, I8K_DELIM);
-       i8k.serial = strtok(NULL, I8K_DELIM);
-       i8k.cpu_temp = strtok(NULL, I8K_DELIM);
-       i8k.left_fan_status = strtok(NULL, I8K_DELIM);
-       i8k.right_fan_status = strtok(NULL, I8K_DELIM);
-       i8k.left_fan_rpm = strtok(NULL, I8K_DELIM);
-       i8k.right_fan_rpm = strtok(NULL, I8K_DELIM);
-       i8k.ac_status = strtok(NULL, I8K_DELIM);
-       i8k.buttons_status = strtok(NULL, I8K_DELIM);
-}
-
 /***********************************************************/
 /***********************************************************/
 /***********************************************************/
@@ -1235,7 +1202,7 @@ char get_freq(char *p_client_buffer, size_t client_buffer_size,
  * Peter Tarjan (ptarjan@citromail.hu) */
 
 /* return cpu voltage in mV (use divisor=1) or V (use divisor=1000) */
-char get_voltage(char *p_client_buffer, size_t client_buffer_size,
+static char get_voltage(char *p_client_buffer, size_t client_buffer_size,
                const char *p_format, int divisor, unsigned int cpu)
 {
        FILE *f;
@@ -1304,6 +1271,22 @@ char get_voltage(char *p_client_buffer, size_t client_buffer_size,
        return 1;
 }
 
+void print_voltage_mv(struct text_object *obj, char *p, int p_max_size)
+{
+       static int ok = 1;
+       if (ok) {
+               ok = get_voltage(p, p_max_size, "%.0f", 1, obj->data.i);
+       }
+}
+
+void print_voltage_v(struct text_object *obj, char *p, int p_max_size)
+{
+       static int ok = 1;
+       if (ok) {
+               ok = get_voltage(p, p_max_size, "%'.3f", 1000, obj->data.i);
+       }
+}
+
 #define ACPI_FAN_DIR "/proc/acpi/fan/"
 
 void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
@@ -1338,7 +1321,7 @@ void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
        snprintf(p_client_buffer, client_buffer_size, "%s", buf);
 }
 
-#define SYSFS_AC_ADAPTER_DIR "/sys/class/power_supply/AC"
+#define SYSFS_AC_ADAPTER_DIR "/sys/class/power_supply"
 #define ACPI_AC_ADAPTER_DIR "/proc/acpi/ac_adapter/"
 /* Linux 2.6.25 onwards ac adapter info is in
    /sys/class/power_supply/AC/
@@ -1350,22 +1333,33 @@ void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
      POWER_SUPPLY_NAME=AC
      POWER_SUPPLY_TYPE=Mains
      POWER_SUPPLY_ONLINE=1
+
+   Update: it seems the folder name is hardware-dependent. We add an aditional adapter
+   argument, specifying the folder name.
+
+   Update: on some systems it's /sys/class/power_supply/ADP1 instead of /sys/class/power_supply/AC
 */
 
-void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
+void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size, const char *adapter)
 {
        static int rep = 0;
 
        char buf[256];
        char buf2[256];
+       struct stat sb;
        FILE *fp;
 
        if (!p_client_buffer || client_buffer_size <= 0) {
                return;
        }
 
-       snprintf(buf2, sizeof(buf2), "%s/uevent", SYSFS_AC_ADAPTER_DIR);
-       fp = open_file(buf2, &rep);
+       if(adapter)
+               snprintf(buf2, sizeof(buf2), "%s/%s/uevent", SYSFS_AC_ADAPTER_DIR, adapter);
+       else{
+               snprintf(buf2, sizeof(buf2), "%s/AC/uevent", SYSFS_AC_ADAPTER_DIR);
+               if(stat(buf2, &sb) == -1) snprintf(buf2, sizeof(buf2), "%s/ADP1/uevent", SYSFS_AC_ADAPTER_DIR);
+       }
+       if(stat(buf2, &sb) == 0) fp = open_file(buf2, &rep); else fp = 0;
        if (fp) {
                /* sysfs processing */
                while (!feof(fp)) {
@@ -2260,6 +2254,49 @@ const char *get_disk_protect_queue(const char *disk)
        return (state > 0) ? "frozen" : "free  ";
 }
 
+typedef struct DEV_LIST_TYPE
+{
+       char *dev_name;
+       int memoized;
+       struct DEV_LIST_TYPE *next;
+
+} DEV_LIST, *DEV_LIST_PTR;
+
+/* Same as sf #2942117 but memoized using a linked list */
+int is_disk(char *dev)
+{
+       char syspath[PATH_MAX];
+       char *slash;
+       static DEV_LIST_PTR dev_head = NULL;
+       DEV_LIST_PTR dev_cur, dev_last;
+
+       dev_cur = dev_head;
+
+       while (dev_cur) {
+               if (strcmp(dev_cur->dev_name, dev) == 0)
+                       return dev_cur->memoized;
+               dev_last = dev_cur;
+               dev_cur  = dev_cur->next;
+       }
+
+       dev_cur = (DEV_LIST_PTR)malloc(sizeof(DEV_LIST));
+       dev_cur->dev_name = (char *)malloc((strlen(dev)+1)*sizeof(char));
+       strcpy(dev_cur->dev_name,dev);
+       dev_cur->next = NULL;
+
+       while ((slash = strchr(dev, '/')))
+               *slash = '!';
+       snprintf(syspath, sizeof(syspath), "/sys/block/%s", dev);
+       dev_cur->memoized = !(access(syspath, F_OK));
+
+       if (dev_head)
+               dev_last->next = dev_cur;
+       else
+               dev_head = dev_cur;
+
+       return dev_cur->memoized;
+}
+
 void update_diskio(void)
 {
        FILE *fp;
@@ -2290,8 +2327,11 @@ void update_diskio(void)
                 * XXX: ignore devices which are part of a SW RAID (MD_MAJOR) */
                if (col_count == 5 && major != LVM_BLK_MAJOR && major != NBD_MAJOR
                                && major != RAMDISK_MAJOR && major != LOOP_MAJOR) {
-                       total_reads += reads;
-                       total_writes += writes;
+                       /* check needed for kernel >= 2.6.31, see sf #2942117 */
+                       if (is_disk(devbuf)) {
+                               total_reads += reads;
+                               total_writes += writes;
+                       }
                } else {
                        col_count = sscanf(buf, "%u %u %s %*u %u %*u %u",
                                &major, &minor, devbuf, &reads, &writes);