here comes the big header include rewrite
[monky] / src / linux.c
index 4576c93..57b6f59 100644 (file)
  * 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 "conky.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include "logging.h"
+#include "common.h"
+#include "linux.h"
 #include <dirent.h>
 #include <ctype.h>
 #include <errno.h>
@@ -135,7 +135,7 @@ void update_meminfo(void)
        char buf[256];
 
        info.mem = info.memmax = info.swap = info.swapmax = info.bufmem =
-               info.buffers = info.cached = 0;
+               info.buffers = info.cached = info.memfree = info.memeasyfree = 0;
 
        if (!(meminfo_fp = open_file("/proc/meminfo", &rep))) {
                return;
@@ -149,7 +149,7 @@ void update_meminfo(void)
                if (strncmp(buf, "MemTotal:", 9) == 0) {
                        sscanf(buf, "%*s %llu", &info.memmax);
                } else if (strncmp(buf, "MemFree:", 8) == 0) {
-                       sscanf(buf, "%*s %llu", &info.mem);
+                       sscanf(buf, "%*s %llu", &info.memfree);
                } else if (strncmp(buf, "SwapTotal:", 10) == 0) {
                        sscanf(buf, "%*s %llu", &info.swapmax);
                } else if (strncmp(buf, "SwapFree:", 9) == 0) {
@@ -161,7 +161,8 @@ void update_meminfo(void)
                }
        }
 
-       info.mem = info.memmax - info.mem;
+       info.mem = info.memmax - info.memfree;
+       info.memeasyfree = info.memfree;
        info.swap = info.swapmax - info.swap;
 
        info.bufmem = info.cached + info.buffers;
@@ -261,7 +262,7 @@ END_TRUE:
                x = strndup(y, text_buffer_size); \
        }
 
-void update_gateway_info_failure(char *reason)
+void update_gateway_info_failure(const char *reason)
 {
        if(reason != NULL) {
                perror(reason);
@@ -316,7 +317,7 @@ void update_gateway_info(void)
        return;
 }
 
-inline void update_net_stats(void)
+void update_net_stats(void)
 {
        FILE *net_dev_fp;
        static int rep = 0;
@@ -355,7 +356,7 @@ inline void update_net_stats(void)
        for (i2 = 0; i2 < 16; i2++) {
                struct net_stat *ns;
                char *s, *p;
-               char temp_addr[17];
+               char temp_addr[18];
                long long r, t, last_recv, last_trans;
 
                if (fgets(buf, 255, net_dev_fp) == NULL) {
@@ -382,9 +383,9 @@ inline void update_net_stats(void)
                memset(&(ns->addr.sa_data), 0, 14);
 
                if(NULL == ns->addrs)
-                       ns->addrs = (char*) malloc(17 * 16);
+                       ns->addrs = (char*) malloc(17 * 16 + 1);
                if(NULL != ns->addrs)
-                       memset(ns->addrs, 0, 17 * 16); /* Up to 17 chars per ip, max 16 interfaces. Nasty memory usage... */
+                       memset(ns->addrs, 0, 17 * 16 + 1); /* Up to 17 chars per ip, max 16 interfaces. Nasty memory usage... */
 
                last_recv = ns->recv;
                last_trans = ns->trans;
@@ -677,7 +678,11 @@ inline static void update_stat(void)
                        info.mask |= (1 << INFO_RUN_PROCS);
                } else if (strncmp(buf, "cpu", 3) == 0) {
                        double delta;
-                       idx = isdigit(buf[3]) ? ((int) buf[3]) - 0x2F : 0;
+                       if (isdigit(buf[3])) {
+                               idx = atoi(&buf[3]) + 1;
+                       } else {
+                               idx = 0;
+                       }
                        sscanf(buf, stat_template, &(cpu[idx].cpu_user),
                                &(cpu[idx].cpu_nice), &(cpu[idx].cpu_system),
                                &(cpu[idx].cpu_idle), &(cpu[idx].cpu_iowait),
@@ -891,7 +896,7 @@ int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
        fd = open(path, O_RDONLY);
        if (fd < 0) {
                CRIT_ERR("can't open '%s': %s\nplease check your device or remove this "
-                       "var from Conky", path, strerror(errno));
+                       "var from "PACKAGE_NAME, path, strerror(errno));
        }
 
        if (strcmp(type, "in") == 0 || strcmp(type, "temp") == 0
@@ -1048,69 +1053,6 @@ void get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
        snprintf(p_client_buffer, client_buffer_size, "%s", adt746x_cpu_state);
 }
 
-/* Thanks to "Walt Nelson" <wnelsonjr@comcast.net> */
-
-/***********************************************************************/
-/* This file is part of x86info.
- * (C) 2001 Dave Jones.
- *
- * Licensed under the terms of the GNU GPL License version 2.
- *
- * Estimate CPU MHz routine by Andrea Arcangeli <andrea@suse.de>
- * Small changes by David Sterba <sterd9am@ss1000.ms.mff.cuni.cz> */
-
-#if  defined(__i386) || defined(__x86_64)
-__inline__ unsigned long long int rdtsc(void)
-{
-       unsigned long long int x;
-
-       __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
-       return x;
-}
-#endif
-
-/* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
-void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
-               const char *p_format, int divisor)
-{
-#if  defined(__i386) || defined(__x86_64)
-       struct timezone tz;
-       struct timeval tvstart, tvstop;
-       unsigned long long cycles[2];   /* gotta be 64 bit */
-       unsigned int microseconds;      /* total time taken */
-
-       if (!p_client_buffer || client_buffer_size <= 0 || !p_format
-                       || divisor <= 0) {
-               return;
-       }
-
-       memset(&tz, 0, sizeof(tz));
-
-       /* get this function in cached memory */
-       gettimeofday(&tvstart, &tz);
-       cycles[0] = rdtsc();
-       gettimeofday(&tvstart, &tz);
-
-       /* we don't trust that this is any specific length of time */
-       usleep(100);
-       cycles[1] = rdtsc();
-       gettimeofday(&tvstop, &tz);
-       microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
-               (tvstop.tv_usec - tvstart.tv_usec);
-
-       snprintf(p_client_buffer, client_buffer_size, p_format,
-               (float) ((cycles[1] - cycles[0]) / microseconds) / divisor);
-       return;
-#else
-       /* FIXME: hardwired: get freq for first cpu!
-        * this whole function needs to be rethought and redone for
-        * multi-cpu/multi-core/multi-threaded environments and
-        * arbitrary combinations thereof */
-       get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
-       return;
-#endif
-}
-
 #define CPUFREQ_PREFIX "/sys/devices/system/cpu"
 #define CPUFREQ_POSTFIX "cpufreq/scaling_cur_freq"
 
@@ -1152,7 +1094,7 @@ char get_freq(char *p_client_buffer, size_t client_buffer_size,
        // open the CPU information file
        f = open_file("/proc/cpuinfo", &rep);
        if (!f) {
-               perror("Conky: Failed to access '/proc/cpuinfo' at get_freq()");
+               perror(PACKAGE_NAME": Failed to access '/proc/cpuinfo' at get_freq()");
                return 0;
        }
 
@@ -1242,7 +1184,7 @@ char get_voltage(char *p_client_buffer, size_t client_buffer_size,
                }
                fclose(f);
        } else {
-               fprintf(stderr, "Conky: Failed to access '%s' at ", current_freq_file);
+               fprintf(stderr, PACKAGE_NAME": Failed to access '%s' at ", current_freq_file);
                perror("get_voltage()");
                if (f) {
                        fclose(f);
@@ -1270,7 +1212,7 @@ char get_voltage(char *p_client_buffer, size_t client_buffer_size,
                }
                fclose(f);
        } else {
-               fprintf(stderr, "Conky: Failed to access '%s' at ", current_freq_file);
+               fprintf(stderr, PACKAGE_NAME": Failed to access '%s' at ", current_freq_file);
                perror("get_voltage()");
                if (f) {
                        fclose(f);
@@ -1316,11 +1258,24 @@ 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 ACPI_AC_ADAPTER_DIR "/proc/acpi/ac_adapter/"
+/* Linux 2.6.25 onwards ac adapter info is in
+   /sys/class/power_supply/AC/
+   On my system I get the following.
+     /sys/class/power_supply/AC/uevent:
+     PHYSDEVPATH=/devices/LNXSYSTM:00/device:00/PNP0A08:00/device:01/PNP0C09:00/ACPI0003:00
+     PHYSDEVBUS=acpi
+     PHYSDEVDRIVER=ac
+     POWER_SUPPLY_NAME=AC
+     POWER_SUPPLY_TYPE=Mains
+     POWER_SUPPLY_ONLINE=1
+*/
 
 void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
 {
        static int rep = 0;
+
        char buf[256];
        char buf2[256];
        FILE *fp;
@@ -1329,25 +1284,44 @@ void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
                return;
        }
 
-       /* yeah, slow... :/ */
-       if (!get_first_file_in_a_directory(ACPI_AC_ADAPTER_DIR, buf, &rep)) {
-               snprintf(p_client_buffer, client_buffer_size, "no ac_adapters?");
-               return;
-       }
+       snprintf(buf2, sizeof(buf2), "%s/uevent", SYSFS_AC_ADAPTER_DIR);
+       fp = open_file(buf2, &rep);
+       if (fp) {
+               /* sysfs processing */
+               while (!feof(fp)) {
+                       if (fgets(buf, sizeof(buf), fp) == NULL)
+                               break;
 
-       snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_AC_ADAPTER_DIR, buf);
+                       if (strncmp(buf, "POWER_SUPPLY_ONLINE=", 20) == 0) {
+                               int online = 0;
+                               sscanf(buf, "POWER_SUPPLY_ONLINE=%d", &online);
+                               snprintf(p_client_buffer, client_buffer_size,
+                                        "%s-line", (online ? "on" : "off"));
+                               break;
+                       }
+               }
+               fclose(fp);
+       } else {
+               /* yeah, slow... :/ */
+               if (!get_first_file_in_a_directory(ACPI_AC_ADAPTER_DIR, buf, &rep)) {
+                       snprintf(p_client_buffer, client_buffer_size, "no ac_adapters?");
+                       return;
+               }
 
-       fp = open_file(buf2, &rep);
-       if (!fp) {
-               snprintf(p_client_buffer, client_buffer_size,
-                       "No ac adapter found.... where is it?");
-               return;
-       }
-       memset(buf, 0, sizeof(buf));
-       fscanf(fp, "%*s %99s", buf);
-       fclose(fp);
+               snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_AC_ADAPTER_DIR, buf);
 
-       snprintf(p_client_buffer, client_buffer_size, "%s", buf);
+               fp = open_file(buf2, &rep);
+               if (!fp) {
+                       snprintf(p_client_buffer, client_buffer_size,
+                                "No ac adapter found.... where is it?");
+                       return;
+               }
+               memset(buf, 0, sizeof(buf));
+               fscanf(fp, "%*s %99s", buf);
+               fclose(fp);
+
+               snprintf(p_client_buffer, client_buffer_size, "%s", buf);
+       }
 }
 
 /*
@@ -1552,7 +1526,7 @@ void set_return_value(char *buffer, unsigned int n, int item, int idx);
 
 void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item)
 {
-       static int idx, rep = 0, rep2 = 0;
+       static int idx, rep = 0, rep1 = 0, rep2 = 0;
        char acpi_path[128];
        char sysfs_path[128];
 
@@ -1575,30 +1549,29 @@ void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item)
        memset(last_battery_time_str[idx], 0, sizeof(last_battery_time_str[idx]));
 
        /* first try SYSFS if that fails try ACPI */
+
        if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
                sysfs_bat_fp[idx] = open_file(sysfs_path, &rep);
-               rep = 0;
        }
-  
+
        if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
-               acpi_bat_fp[idx] = open_file(acpi_path, &rep);
+               acpi_bat_fp[idx] = open_file(acpi_path, &rep1);
        }
-  
+
        if (sysfs_bat_fp[idx] != NULL) {
                /* SYSFS */
                int present_rate = -1;
                int remaining_capacity = -1;
                char charging_state[64];
                char present[4];
+
                strcpy(charging_state, "unknown");
+
                while (!feof(sysfs_bat_fp[idx])) {
                        char buf[256];
                        if (fgets(buf, 256, sysfs_bat_fp[idx]) == NULL)
                                break;
+
                        /* let's just hope units are ok */
                        if (strncmp (buf, "POWER_SUPPLY_PRESENT=1", 22) == 0)
                                strcpy(present, "yes");
@@ -1621,14 +1594,14 @@ void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item)
                        else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=", 25) == 0)
                                sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_last_full[idx]);
                }
+
                fclose(sysfs_bat_fp[idx]);
                sysfs_bat_fp[idx] = NULL;
+
                /* Hellf[i]re notes that remaining capacity can exceed acpi_last_full */
                if (remaining_capacity > acpi_last_full[idx])
                        acpi_last_full[idx] = remaining_capacity;  /* normalize to 100% */
+
                /* not present */
                if (strcmp(present, "No") == 0) {
                        strncpy(last_battery_str[idx], "not present", 64);
@@ -1970,6 +1943,7 @@ int get_battery_perct(const char *bat)
        /* compute the battery percentage */
        last_battery_perct[idx] =
                (int) (((float) remaining_capacity / acpi_design_capacity[idx]) * 100);
+       if (last_battery_perct[idx] > 100) last_battery_perct[idx] = 100;
        return last_battery_perct[idx];
 }
 
@@ -2163,7 +2137,7 @@ void get_ibm_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
                }
        } else {
                CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
-                       "ibm* from your Conky config file.", fan, strerror(errno));
+                       "ibm* from your "PACKAGE_NAME" config file.", fan, strerror(errno));
        }
 
        fclose(fp);
@@ -2227,7 +2201,7 @@ void get_ibm_acpi_temps(void)
                }
        } else {
                CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
-                       "ibm* from your Conky config file.", thermal, strerror(errno));
+                       "ibm* from your "PACKAGE_NAME" config file.", thermal, strerror(errno));
        }
 
        fclose(fp);
@@ -2275,7 +2249,7 @@ void get_ibm_acpi_volume(char *p_client_buffer, size_t client_buffer_size)
                }
        } else {
                CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
-                       "ibm* from your Conky config file.", volume, strerror(errno));
+                       "ibm* from your "PACKAGE_NAME" config file.", volume, strerror(errno));
        }
 
        fclose(fp);
@@ -2324,7 +2298,7 @@ void get_ibm_acpi_brightness(char *p_client_buffer, size_t client_buffer_size)
                }
        } else {
                CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
-                       "ibm* from your Conky config file.", filename, strerror(errno));
+                       "ibm* from your "PACKAGE_NAME" config file.", filename, strerror(errno));
        }
 
        fclose(fp);
@@ -2366,7 +2340,10 @@ const char *get_disk_protect_queue(const char *disk)
        char path[128];
        int state;
 
-       snprintf(path, 127, "/sys/block/%s/queue/protect", disk);
+       snprintf(path, 127, "/sys/block/%s/device/unload_heads", disk);
+       if (access(path, F_OK)) {
+               snprintf(path, 127, "/sys/block/%s/queue/protect", disk);
+       }
        if ((fp = fopen(path, "r")) == NULL)
                return "n/a   ";
        if (fscanf(fp, "%d\n", &state) != 1) {
@@ -2374,6 +2351,6 @@ const char *get_disk_protect_queue(const char *disk)
                return "failed";
        }
        fclose(fp);
-       return state ? "frozen" : "free  ";
+       return (state > 0) ? "frozen" : "free  ";
 }