X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;ds=sidebyside;f=src%2Flinux.c;h=8703c9941f924aa37d306d9fc2695cfbaecca05f;hb=95f03f2a2df0c29ec7ecb7bd2cce2fb6d8293f53;hp=d8dd5d1f4fcee1ce49fe2e17b67e557825b2b183;hpb=0ee3efdc0f71180e58573a8ce6b12b5de81ebe52;p=monky
diff --git a/src/linux.c b/src/linux.c
index d8dd5d1..8703c99 100644
--- a/src/linux.c
+++ b/src/linux.c
@@ -1,4 +1,7 @@
-/* Conky, a system monitor, based on torsmo
+/* -*- 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
*
@@ -8,7 +11,7 @@
*
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
* Copyright (c) 2007 Toni Spets
- * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al.
+ * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
* (see AUTHORS)
* All rights reserved.
*
@@ -24,12 +27,15 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
- * $Id$ */
+ */
#include "conky.h"
-#include
-#include
-#include
+#include "logging.h"
+#include "common.h"
+#include "linux.h"
+#include "net_stat.h"
+#include "diskio.h"
+#include "temphelper.h"
#include
#include
#include
@@ -57,16 +63,40 @@
#endif
#include
#include
+#include
+
+/* The following ifdefs were adapted from gkrellm */
+#include
+
+#if !defined(MD_MAJOR)
+#define MD_MAJOR 9
+#endif
+
+#if !defined(LVM_BLK_MAJOR)
+#define LVM_BLK_MAJOR 58
+#endif
+
+#if !defined(NBD_MAJOR)
+#define NBD_MAJOR 43
+#endif
#ifdef HAVE_IWLIB
#include
#endif
+#include
+
+struct sysfs {
+ int fd;
+ int arg;
+ char devtype[256];
+ char type[64];
+ float factor, offset;
+};
+
#define SHORTSTAT_TEMPL "%*s %llu %llu %llu"
#define LONGSTAT_TEMPL "%*s %llu %llu %llu "
-static int show_nice_processes;
-
/* This flag tells the linux routines to use the /proc system where possible,
* even if other api's are available, e.g. sysinfo() or getloadavg().
* the reason for this is to allow for /proc-based distributed monitoring.
@@ -77,7 +107,7 @@ void prepare_update(void)
{
}
-void update_uptime(void)
+int update_uptime(void)
{
#ifdef HAVE_SYSINFO
if (!prefer_proc) {
@@ -93,12 +123,12 @@ void update_uptime(void)
if (!(fp = open_file("/proc/uptime", &rep))) {
info.uptime = 0.0;
- return;
+ return 0;
}
fscanf(fp, "%lf", &info.uptime);
fclose(fp);
}
- info.mask |= (1 << INFO_UPTIME);
+ return 0;
}
int check_mount(char *s)
@@ -118,7 +148,7 @@ int check_mount(char *s)
}
fclose(mtab);
} else {
- ERR("Could not open mtab");
+ NORM_ERR("Could not open mtab");
}
return ret;
}
@@ -126,7 +156,7 @@ int check_mount(char *s)
/* these things are also in sysinfo except Buffers:
* (that's why I'm reading them from proc) */
-void update_meminfo(void)
+int update_meminfo(void)
{
FILE *meminfo_fp;
static int rep = 0;
@@ -134,11 +164,11 @@ void update_meminfo(void)
/* unsigned int a; */
char buf[256];
- info.mem = info.memmax = info.swap = info.swapmax = info.bufmem =
- info.buffers = info.cached = 0;
+ info.mem = info.memmax = info.swap = info.swapfree = info.swapmax = info.bufmem =
+ info.buffers = info.cached = info.memfree = info.memeasyfree = 0;
if (!(meminfo_fp = open_file("/proc/meminfo", &rep))) {
- return;
+ return 0;
}
while (!feof(meminfo_fp)) {
@@ -149,11 +179,11 @@ 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) {
- sscanf(buf, "%*s %llu", &info.swap);
+ sscanf(buf, "%*s %llu", &info.swapfree);
} else if (strncmp(buf, "Buffers:", 8) == 0) {
sscanf(buf, "%*s %llu", &info.buffers);
} else if (strncmp(buf, "Cached:", 7) == 0) {
@@ -161,14 +191,14 @@ void update_meminfo(void)
}
}
- info.mem = info.memmax - info.mem;
- info.swap = info.swapmax - info.swap;
+ info.mem = info.memmax - info.memfree;
+ info.memeasyfree = info.memfree;
+ info.swap = info.swapmax - info.swapfree;
info.bufmem = info.cached + info.buffers;
- info.mask |= (1 << INFO_MEM) | (1 << INFO_BUFFERS);
-
fclose(meminfo_fp);
+ return 0;
}
int get_laptop_mode(void)
@@ -192,106 +222,119 @@ char *get_ioscheduler(char *disk)
char buf[128];
if (!disk)
- return strdup("n/a");
+ return strndup("n/a", text_buffer_size);
snprintf(buf, 127, "/sys/block/%s/queue/scheduler", disk);
if ((fp = fopen(buf, "r")) == NULL) {
- return strdup("n/a");
+ return strndup("n/a", text_buffer_size);
}
while (!feof(fp)) {
fscanf(fp, "%127s", buf);
if (buf[0] == '[') {
buf[strlen(buf) - 1] = '\0';
fclose(fp);
- return strdup(buf + 1);
+ return strndup(buf + 1, text_buffer_size);
}
}
fclose(fp);
- return strdup("n/a");
+ return strndup("n/a", text_buffer_size);
}
-int interface_up(const char *dev)
-{
- int fd;
- struct ifreq ifr;
-
- if((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
- CRIT_ERR("could not create sockfd");
- return 0;
- }
- strncpy(ifr.ifr_name, dev, IFNAMSIZ);
- if(ioctl(fd, SIOCGIFFLAGS, &ifr)) {
- /* if device does not exist, treat like not up */
- if (errno != ENODEV)
- perror("SIOCGIFFLAGS");
- } else {
- close(fd);
- return (ifr.ifr_flags & IFF_UP);
- }
- close(fd);
- return 0;
-}
+static struct {
+ char *iface;
+ char *ip;
+ int count;
+} gw_info;
#define COND_FREE(x) if(x) free(x); x = 0
#define SAVE_SET_STRING(x, y) \
if (x && strcmp((char *)x, (char *)y)) { \
free(x); \
- x = strdup("multiple"); \
+ x = strndup("multiple", text_buffer_size); \
} else if (!x) { \
- x = strdup(y); \
+ x = strndup(y, text_buffer_size); \
+ }
+
+void update_gateway_info_failure(const char *reason)
+{
+ if(reason != NULL) {
+ perror(reason);
}
+ //2 pointers to 1 location causes a crash when we try to free them both
+ gw_info.iface = strndup("failed", text_buffer_size);
+ gw_info.ip = strndup("failed", text_buffer_size);
+}
+
+
+/* Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT */
+#define RT_ENTRY_FORMAT "%63s %lx %lx %x %*d %*d %*d %lx %*d %*d %*d\n"
-void update_gateway_info(void)
+int update_gateway_info(void)
{
FILE *fp;
struct in_addr ina;
char iface[64];
unsigned long dest, gate, mask;
unsigned int flags;
- short ref, use, metric, mtu, win, irtt;
- struct gateway_info *gw_info = &info.gw_info;
-
- COND_FREE(gw_info->iface);
- COND_FREE(gw_info->ip);
- gw_info->count = 0;
+ COND_FREE(gw_info.iface);
+ COND_FREE(gw_info.ip);
+ gw_info.count = 0;
if ((fp = fopen("/proc/net/route", "r")) == NULL) {
- perror("fopen()");
- goto FAIL;
- }
- if (fscanf(fp, "%*[^\n]\n") == EOF) {
- perror("fscanf()");
- goto CLOSE_FAIL;
+ update_gateway_info_failure("fopen()");
+ return 0;
}
+
+ /* skip over the table header line, which is always present */
+ fscanf(fp, "%*[^\n]\n");
+
while (!feof(fp)) {
- // Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
- if(fscanf(fp, "%63s %lx %lx %x %hd %hd %hd %lx %hd %hd %hd\n",
- iface, &dest, &gate, &flags, &ref, &use,
- &metric, &mask, &mtu, &win, &irtt) != 11) {
- perror("fscanf()");
- goto CLOSE_FAIL;
+ if(fscanf(fp, RT_ENTRY_FORMAT,
+ iface, &dest, &gate, &flags, &mask) != 5) {
+ update_gateway_info_failure("fscanf()");
+ break;
}
- if (flags & RTF_GATEWAY && dest == 0 && mask == 0) {
- gw_info->count++;
- SAVE_SET_STRING(gw_info->iface, iface)
+ if (!(dest || mask) && ((flags & RTF_GATEWAY) || !gate) ) {
+ gw_info.count++;
+ SAVE_SET_STRING(gw_info.iface, iface)
ina.s_addr = gate;
- SAVE_SET_STRING(gw_info->ip, inet_ntoa(ina))
+ SAVE_SET_STRING(gw_info.ip, inet_ntoa(ina))
}
}
fclose(fp);
- return;
-CLOSE_FAIL:
- fclose(fp);
-FAIL:
- info.gw_info.iface = info.gw_info.ip = strdup("failed");
- return;
+ return 0;
+}
+
+void free_gateway_info(void)
+{
+ if (gw_info.iface)
+ free(gw_info.iface);
+ if (gw_info.ip)
+ free(gw_info.ip);
+ memset(&gw_info, 0, sizeof(gw_info));
+}
+
+int gateway_exists(void)
+{
+ return !!gw_info.count;
+}
+
+void print_gateway_iface(char *p, int p_max_size)
+{
+ snprintf(p, p_max_size, "%s", gw_info.iface);
+}
+
+void print_gateway_ip(char *p, int p_max_size)
+{
+ snprintf(p, p_max_size, "%s", gw_info.ip);
}
-inline void update_net_stats(void)
+int update_net_stats(void)
{
FILE *net_dev_fp;
static int rep = 0;
+ static char first = 1;
// FIXME: arbitrary size chosen to keep code simple.
int i, i2;
@@ -311,23 +354,23 @@ inline void update_net_stats(void)
/* get delta */
delta = current_update_time - last_update_time;
if (delta <= 0.0001) {
- return;
+ return 0;
}
/* open file and ignore first two lines */
if (!(net_dev_fp = open_file("/proc/net/dev", &rep))) {
clear_net_stats();
- return;
+ return 0;
}
fgets(buf, 255, net_dev_fp); /* garbage */
fgets(buf, 255, net_dev_fp); /* garbage (field names) */
/* read each interface */
- for (i2 = 0; i2 < 16; i2++) {
+ for (i2 = 0; i2 < MAX_NET_INTERFACES; 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) {
@@ -349,14 +392,11 @@ inline void update_net_stats(void)
*p = '\0';
p++;
- ns = get_net_stat(s);
+ ns = get_net_stat(s, NULL, NULL);
ns->up = 1;
memset(&(ns->addr.sa_data), 0, 14);
- if(NULL == ns->addrs)
- ns->addrs = (char*) malloc(17 * 16);
- 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 * MAX_NET_INTERFACES + 1); /* Up to 17 chars per ip, max MAX_NET_INTERFACES interfaces. Nasty memory usage... */
last_recv = ns->recv;
last_trans = ns->trans;
@@ -383,8 +423,8 @@ inline void update_net_stats(void)
/*** ip addr patch ***/
i = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
- conf.ifc_buf = malloc(sizeof(struct ifreq) * 16);
- conf.ifc_len = sizeof(struct ifreq) * 16;
+ conf.ifc_buf = malloc(sizeof(struct ifreq) * MAX_NET_INTERFACES);
+ conf.ifc_len = sizeof(struct ifreq) * MAX_NET_INTERFACES;
memset(conf.ifc_buf, 0, conf.ifc_len);
ioctl((long) i, SIOCGIFCONF, &conf);
@@ -396,17 +436,15 @@ inline void update_net_stats(void)
break;
ns2 = get_net_stat(
- ((struct ifreq *) conf.ifc_buf)[k].ifr_ifrn.ifrn_name);
+ ((struct ifreq *) conf.ifc_buf)[k].ifr_ifrn.ifrn_name, NULL, NULL);
ns2->addr = ((struct ifreq *) conf.ifc_buf)[k].ifr_ifru.ifru_addr;
- if(NULL != ns2->addrs) {
- sprintf(temp_addr, "%u.%u.%u.%u, ",
+ sprintf(temp_addr, "%u.%u.%u.%u, ",
ns2->addr.sa_data[2] & 255,
ns2->addr.sa_data[3] & 255,
ns2->addr.sa_data[4] & 255,
ns2->addr.sa_data[5] & 255);
- if(NULL == strstr(ns2->addrs, temp_addr))
- strncpy(ns2->addrs + strlen(ns2->addrs), temp_addr, 17);
- }
+ if(NULL == strstr(ns2->addrs, temp_addr))
+ strncpy(ns2->addrs + strlen(ns2->addrs), temp_addr, 17);
}
close((long) i);
@@ -415,25 +453,28 @@ inline void update_net_stats(void)
/*** end ip addr patch ***/
- /* calculate speeds */
- ns->net_rec[0] = (ns->recv - last_recv) / delta;
- ns->net_trans[0] = (ns->trans - last_trans) / delta;
+ if (!first) {
+ /* calculate speeds */
+ ns->net_rec[0] = (ns->recv - last_recv) / delta;
+ ns->net_trans[0] = (ns->trans - last_trans) / delta;
+ }
+
curtmp1 = 0;
curtmp2 = 0;
// get an average
- for (i = 0; (unsigned) i < info.net_avg_samples; i++) {
- curtmp1 += ns->net_rec[i];
- curtmp2 += ns->net_trans[i];
- }
- if (curtmp1 == 0) {
- curtmp1 = 1;
- }
- if (curtmp2 == 0) {
- curtmp2 = 1;
+#ifdef HAVE_OPENMP
+#pragma omp parallel for reduction(+:curtmp1, curtmp2) schedule(dynamic,10)
+#endif /* HAVE_OPENMP */
+ for (i = 0; i < info.net_avg_samples; i++) {
+ curtmp1 = curtmp1 + ns->net_rec[i];
+ curtmp2 = curtmp2 + ns->net_trans[i];
}
ns->recv_speed = curtmp1 / (double) info.net_avg_samples;
ns->trans_speed = curtmp2 / (double) info.net_avg_samples;
if (info.net_avg_samples > 1) {
+#ifdef HAVE_OPENMP
+#pragma omp parallel for schedule(dynamic,10)
+#endif /* HAVE_OPENMP */
for (i = info.net_avg_samples; i > 1; i--) {
ns->net_rec[i - 1] = ns->net_rec[i - 2];
ns->net_trans[i - 1] = ns->net_trans[i - 2];
@@ -498,22 +539,48 @@ inline void update_net_stats(void)
free(winfo);
#endif
}
+ first = 0;
fclose(net_dev_fp);
-
- info.mask |= (1 << INFO_NET);
+ return 0;
}
int result;
-void update_total_processes(void)
+int update_total_processes(void)
+{
+ DIR *dir;
+ struct dirent *entry;
+ int ignore1;
+ char ignore2;
+
+ info.procs = 0;
+ if (!(dir = opendir("/proc"))) {
+ return 0;
+ }
+ while ((entry = readdir(dir))) {
+ if (!entry) {
+ /* Problem reading list of processes */
+ closedir(dir);
+ info.procs = 0;
+ return 0;
+ }
+ if (sscanf(entry->d_name, "%d%c", &ignore1, &ignore2) == 1) {
+ info.procs++;
+ }
+ }
+ closedir(dir);
+ return 0;
+}
+
+int update_threads(void)
{
#ifdef HAVE_SYSINFO
if (!prefer_proc) {
struct sysinfo s_info;
sysinfo(&s_info);
- info.procs = s_info.procs;
+ info.threads = s_info.procs;
} else
#endif
{
@@ -521,13 +588,13 @@ void update_total_processes(void)
FILE *fp;
if (!(fp = open_file("/proc/loadavg", &rep))) {
- info.procs = 0;
- return;
+ info.threads = 0;
+ return 0;
}
- fscanf(fp, "%*f %*f %*f %*d/%hu", &info.procs);
+ fscanf(fp, "%*f %*f %*f %*d/%hu", &info.threads);
fclose(fp);
}
- info.mask |= (1 << INFO_PROCS);
+ return 0;
}
#define CPU_SAMPLE_COUNT 15
@@ -601,17 +668,32 @@ void get_cpu_count(void)
#define TMPL_LONGSTAT "%*s %llu %llu %llu %llu %llu %llu %llu %llu"
#define TMPL_SHORTSTAT "%*s %llu %llu %llu %llu"
-inline static void update_stat(void)
+int update_stat(void)
{
FILE *stat_fp;
static int rep = 0;
static struct cpu_info *cpu = NULL;
char buf[256];
- unsigned int i;
+ int i;
unsigned int idx;
double curtmp;
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. */
+ pthread_mutex_lock(&last_stat_update_mutex);
+ if (last_stat_update == current_update_time) {
+ pthread_mutex_unlock(&last_stat_update_mutex);
+ return 0;
+ }
+ 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) {
@@ -624,18 +706,19 @@ inline static void update_stat(void)
KFLAG_ISSET(KFLAG_IS_LONGSTAT) ? TMPL_LONGSTAT : TMPL_SHORTSTAT;
}
- if (!cpu) {
+ if (!global_cpu) {
malloc_cpu_size = (info.cpu_count + 1) * sizeof(struct cpu_info);
cpu = malloc(malloc_cpu_size);
memset(cpu, 0, malloc_cpu_size);
+ global_cpu = cpu;
}
if (!(stat_fp = open_file("/proc/stat", &rep))) {
- info.run_procs = 0;
+ info.run_threads = 0;
if (info.cpu_usage) {
memset(info.cpu_usage, 0, info.cpu_count * sizeof(float));
}
- return;
+ return 0;
}
idx = 0;
@@ -645,11 +728,14 @@ inline static void update_stat(void)
}
if (strncmp(buf, "procs_running ", 14) == 0) {
- sscanf(buf, "%*s %hu", &info.run_procs);
- info.mask |= (1 << INFO_RUN_PROCS);
+ sscanf(buf, "%*s %hu", &info.run_threads);
} 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),
@@ -663,7 +749,6 @@ inline static void update_stat(void)
cpu[idx].cpu_active_total = cpu[idx].cpu_total -
(cpu[idx].cpu_idle + cpu[idx].cpu_iowait);
- info.mask |= (1 << INFO_CPU);
delta = current_update_time - last_update_time;
@@ -675,8 +760,11 @@ inline static void update_stat(void)
cpu[idx].cpu_last_active_total) /
(float) (cpu[idx].cpu_total - cpu[idx].cpu_last_total);
curtmp = 0;
+#ifdef HAVE_OPENMP
+#pragma omp parallel for reduction(+:curtmp) schedule(dynamic,10)
+#endif /* HAVE_OPENMP */
for (i = 0; i < info.cpu_avg_samples; i++) {
- curtmp += cpu[idx].cpu_val[i];
+ curtmp = curtmp + cpu[idx].cpu_val[i];
}
/* TESTING -- I've removed this, because I don't think it is right.
* You shouldn't divide by the cpu count here ...
@@ -692,25 +780,31 @@ inline static void update_stat(void)
cpu[idx].cpu_last_total = cpu[idx].cpu_total;
cpu[idx].cpu_last_active_total = cpu[idx].cpu_active_total;
+#ifdef HAVE_OPENMP
+#pragma omp parallel for schedule(dynamic,10)
+#endif /* HAVE_OPENMP */
for (i = info.cpu_avg_samples - 1; i > 0; i--) {
cpu[idx].cpu_val[i] = cpu[idx].cpu_val[i - 1];
}
}
}
fclose(stat_fp);
+ return 0;
}
-void update_running_processes(void)
+int update_running_processes(void)
{
update_stat();
+ return 0;
}
-void update_cpu_usage(void)
+int update_cpu_usage(void)
{
update_stat();
+ return 0;
}
-void update_load_average(void)
+int update_load_average(void)
{
#ifdef HAVE_GETLOADAVG
if (!prefer_proc) {
@@ -728,47 +822,13 @@ void update_load_average(void)
if (!(fp = open_file("/proc/loadavg", &rep))) {
info.loadavg[0] = info.loadavg[1] = info.loadavg[2] = 0.0;
- return;
+ return 0;
}
fscanf(fp, "%f %f %f", &info.loadavg[0], &info.loadavg[1],
&info.loadavg[2]);
fclose(fp);
}
- info.mask |= (1 << INFO_LOADAVG);
-}
-
-#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("/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) {
- 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);
+ return 0;
}
/***********************************************************/
@@ -791,7 +851,7 @@ static int get_first_file_in_a_directory(const char *dir, char *s, int *rep)
n = scandir(dir, &namelist, no_dots, alphasort);
if (n < 0) {
if (!rep || !*rep) {
- ERR("scandir for %s: %s", dir, strerror(errno));
+ NORM_ERR("scandir for %s: %s", dir, strerror(errno));
if (rep) {
*rep = 1;
}
@@ -805,6 +865,9 @@ static int get_first_file_in_a_directory(const char *dir, char *s, int *rep)
strncpy(s, namelist[0]->d_name, 255);
s[255] = '\0';
+#ifdef HAVE_OPENMP
+#pragma omp parallel for schedule(dynamic,10)
+#endif /* HAVE_OPENMP */
for (i = 0; i < n; i++) {
free(namelist[i]);
}
@@ -814,7 +877,7 @@ static int get_first_file_in_a_directory(const char *dir, char *s, int *rep)
}
}
-int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
+static int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
int *divisor, char *devtype)
{
char path[256];
@@ -847,25 +910,32 @@ int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
}
}
- /* change vol to in */
+ /* change vol to in, tempf to temp */
if (strcmp(type, "vol") == 0) {
type = "in";
+ } else if (strcmp(type, "tempf") == 0) {
+ type = "temp";
}
- if (strcmp(type, "tempf") == 0) {
- snprintf(path, 255, "%s%s/%s%d_input", dir, dev, "temp", n);
- } else {
- snprintf(path, 255, "%s%s/%s%d_input", dir, dev, type, n);
- }
- strncpy(devtype, path, 255);
+ /* construct path */
+ snprintf(path, 255, "%s%s/%s%d_input", dir, dev, type, n);
- /* open file */
+ /* first, attempt to open file in /device */
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));
+
+ /* if it fails, strip the /device from dev and attempt again */
+ buf[strlen(buf) - 7] = 0;
+ snprintf(path, 255, "%s%s/%s%d_input", dir, dev, type, n);
+ fd = open(path, O_RDONLY);
+ if (fd < 0) {
+ CRIT_ERR(NULL, NULL, "can't open '%s': %s\nplease check your device or remove this "
+ "var from "PACKAGE_NAME, path, strerror(errno));
+ }
}
+ strncpy(devtype, path, 255);
+
if (strcmp(type, "in") == 0 || strcmp(type, "temp") == 0
|| strcmp(type, "tempf") == 0) {
*divisor = 1;
@@ -894,19 +964,18 @@ int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
/* should read until n == 0 but I doubt that kernel will give these
* in multiple pieces. :) */
if (divn < 0) {
- ERR("open_sysfs_sensor(): can't read from sysfs");
+ NORM_ERR("open_sysfs_sensor(): can't read from sysfs");
} else {
divbuf[divn] = '\0';
*divisor = atoi(divbuf);
}
+ close(divfd);
}
- close(divfd);
-
return fd;
}
-double get_sysfs_info(int *fd, int divisor, char *devtype, char *type)
+static double get_sysfs_info(int *fd, int divisor, char *devtype, char *type)
{
int val = 0;
@@ -924,7 +993,7 @@ double get_sysfs_info(int *fd, int divisor, char *devtype, char *type)
/* should read until n == 0 but I doubt that kernel will give these
* in multiple pieces. :) */
if (n < 0) {
- ERR("get_sysfs_info(): read from %s failed\n", devtype);
+ NORM_ERR("get_sysfs_info(): read from %s failed\n", devtype);
} else {
buf[n] = '\0';
val = atoi(buf);
@@ -935,7 +1004,7 @@ double get_sysfs_info(int *fd, int divisor, char *devtype, char *type)
/* open file */
*fd = open(devtype, O_RDONLY);
if (*fd < 0) {
- ERR("can't open '%s': %s", devtype, strerror(errno));
+ NORM_ERR("can't open '%s': %s", devtype, strerror(errno));
}
/* My dirty hack for computing CPU value
@@ -965,122 +1034,81 @@ double get_sysfs_info(int *fd, int divisor, char *devtype, char *type)
}
}
-/* Prior to kernel version 2.6.12, the CPU fan speed was available in
- * ADT746X_FAN_OLD, whereas later kernel versions provide this information in
- * ADT746X_FAN. */
-#define ADT746X_FAN "/sys/devices/temperatures/sensor1_fan_speed"
-#define ADT746X_FAN_OLD "/sys/devices/temperatures/cpu_fan_speed"
+#define HWMON_RESET() {\
+ buf1[0] = 0; \
+ factor = 1.0; \
+ offset = 0.0; }
-void get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size)
+static void parse_sysfs_sensor(struct text_object *obj, const char *arg, const char *path, const char *type)
{
- static int rep = 0;
- char adt746x_fan_state[64];
- FILE *fp;
-
- if (!p_client_buffer || client_buffer_size <= 0) {
+ char buf1[64], buf2[64];
+ float factor, offset;
+ int n, found = 0;
+ struct sysfs *sf;
+
+ if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
+ if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
+ if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
+ if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
+
+ if (!found) {
+ NORM_ERR("i2c failed to parse arguments");
+ obj->type = OBJ_text;
return;
}
+ DBGP("parsed %s args: '%s' '%s' %d %f %f\n", type, buf1, buf2, n, factor, offset);
+ sf = malloc(sizeof(struct sysfs));
+ memset(sf, 0, sizeof(struct sysfs));
+ sf->fd = open_sysfs_sensor(path, (*buf1) ? buf1 : 0, buf2, n,
+ &sf->arg, sf->devtype);
+ strncpy(sf->type, buf2, 63);
+ sf->factor = factor;
+ sf->offset = offset;
+ obj->data.opaque = sf;
+}
- if ((fp = open_file(ADT746X_FAN, &rep)) == NULL
- && (fp = open_file(ADT746X_FAN_OLD, &rep)) == NULL) {
- sprintf(adt746x_fan_state, "adt746x not found");
- } else {
- fgets(adt746x_fan_state, sizeof(adt746x_fan_state), fp);
- adt746x_fan_state[strlen(adt746x_fan_state) - 1] = 0;
- fclose(fp);
- }
-
- snprintf(p_client_buffer, client_buffer_size, "%s", adt746x_fan_state);
+#define PARSER_GENERATOR(name, path) \
+void parse_##name##_sensor(struct text_object *obj, const char *arg) \
+{ \
+ parse_sysfs_sensor(obj, arg, path, #name); \
}
-/* Prior to kernel version 2.6.12, the CPU temperature was found in
- * ADT746X_CPU_OLD, whereas later kernel versions provide this information in
- * ADT746X_CPU. */
-#define ADT746X_CPU "/sys/devices/temperatures/sensor1_temperature"
-#define ADT746X_CPU_OLD "/sys/devices/temperatures/cpu_temperature"
+PARSER_GENERATOR(i2c, "/sys/bus/i2c/devices/")
+PARSER_GENERATOR(hwmon, "/sys/class/hwmon/")
+PARSER_GENERATOR(platform, "/sys/bus/platform/devices/")
-void get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
+void print_sysfs_sensor(struct text_object *obj, char *p, int p_max_size)
{
- static int rep = 0;
- char adt746x_cpu_state[64];
- FILE *fp;
+ double r;
+ struct sysfs *sf = obj->data.opaque;
- if (!p_client_buffer || client_buffer_size <= 0) {
+ if (!sf)
return;
- }
-
- if ((fp = open_file(ADT746X_CPU, &rep)) == NULL
- && (fp = open_file(ADT746X_CPU_OLD, &rep)) == NULL) {
- sprintf(adt746x_cpu_state, "adt746x not found");
- } else {
- fscanf(fp, "%2s", adt746x_cpu_state);
- fclose(fp);
- }
-
- snprintf(p_client_buffer, client_buffer_size, "%s", adt746x_cpu_state);
-}
-
-/* Thanks to "Walt Nelson" */
-/***********************************************************************/
-/* 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
- * Small changes by David Sterba */
+ r = get_sysfs_info(&sf->fd, sf->arg,
+ sf->devtype, sf->type);
-#if defined(__i386) || defined(__x86_64)
-__inline__ unsigned long long int rdtsc(void)
-{
- unsigned long long int x;
+ r = r * sf->factor + sf->offset;
- __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
- return x;
+ if (!strncmp(sf->type, "temp", 4)) {
+ temp_print(p, p_max_size, r, TEMP_CELSIUS);
+ } else if (r >= 100.0 || r == 0) {
+ snprintf(p, p_max_size, "%d", (int) r);
+ } else {
+ snprintf(p, p_max_size, "%.1f", r);
+ }
}
-#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)
+void free_sysfs_sensor(struct text_object *obj)
{
-#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 */
+ struct sysfs *sf = obj->data.opaque;
- if (!p_client_buffer || client_buffer_size <= 0 || !p_format
- || divisor <= 0) {
+ if (!sf)
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
+ close(sf->fd);
+ free(obj->data.opaque);
+ obj->data.opaque = NULL;
}
#define CPUFREQ_PREFIX "/sys/devices/system/cpu"
@@ -1124,7 +1152,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;
}
@@ -1185,7 +1213,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;
@@ -1214,7 +1242,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);
@@ -1242,7 +1270,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);
@@ -1254,6 +1282,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)
@@ -1288,38 +1332,81 @@ 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"
#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
+
+ 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;
}
- /* 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;
+ 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)) {
+ 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);
+ }
}
/*
@@ -1358,7 +1445,7 @@ int open_acpi_temperature(const char *name)
fd = open(path, O_RDONLY);
if (fd < 0) {
- ERR("can't open '%s': %s", path, strerror(errno));
+ NORM_ERR("can't open '%s': %s", path, strerror(errno));
}
return fd;
@@ -1389,7 +1476,7 @@ double get_acpi_temperature(int fd)
n = read(fd, buf, 255);
if (n < 0) {
- ERR("can't read fd %d: %s", fd, strerror(errno));
+ NORM_ERR("can't read fd %d: %s", fd, strerror(errno));
} else {
buf[n] = '\0';
sscanf(buf, "temperature: %lf", &last_acpi_temp);
@@ -1464,20 +1551,49 @@ present voltage: 16608 mV
On some systems POWER_SUPPLY_ENERGY_* is replaced by POWER_SUPPLY_CHARGE_*
*/
+/*on n900 with power kernel: (non power kernel could use "hal-device bme" and do something else)
+/sys/class/power_supply/bq27200-0/uevent
+PHYSDEVPATH=/class/i2c-adapter/i2c-2/2-0055
+PHYSDEVBUS=i2c
+PHYSDEVDRIVER=bq27200-battery
+POWER_SUPPLY_NAME=bq27200-0
+POWER_SUPPLY_TYPE=Battery
+POWER_SUPPLY_PRESENT=1 << this is always 1, it means the battery is inserted
+POWER_SUPPLY_VOLTAGE_NOW=4039 << this keeps updating during charging, both here and in dbus
+POWER_SUPPLY_CURRENT_NOW=1960 << this goes negative when charging!
+POWER_SUPPLY_CAPACITY=98 << supposed to be the %, it keeps updating while charging, unlike the dbus vals that freeze
+POWER_SUPPLY_TEMP=39 << only temperature sensor in n900 :(
+*/
+
#define SYSFS_BATTERY_BASE_PATH "/sys/class/power_supply"
-#define ACPI_BATTERY_BASE_PATH "/proc/acpi/battery"
-#define APM_PATH "/proc/apm"
-#define MAX_BATTERY_COUNT 4
+#define ACPI_BATTERY_BASE_PATH "/proc/acpi/battery" //not for n900
+#define APM_PATH "/proc/apm" //not for n900
+#define MAX_BATTERY_COUNT 4 //more like 1
static FILE *sysfs_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
-static FILE *acpi_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
-static FILE *apm_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
+//static FILE *acpi_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
+//static FILE *apm_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
static int batteries_initialized = 0;
static char batteries[MAX_BATTERY_COUNT][32];
-static int acpi_last_full[MAX_BATTERY_COUNT];
-static int acpi_design_capacity[MAX_BATTERY_COUNT];
+//static int acpi_last_full[MAX_BATTERY_COUNT];
+//static int acpi_design_capacity[MAX_BATTERY_COUNT];
+
+//eg 4100
+static int last_battery_volts[MAX_BATTERY_COUNT];
+
+//eg 78
+static unsigned char last_cell_radio_dbm;
+
+//eg 100
+static unsigned char last_cell_radio_percent;
+
+//eg 'full' 'on' 'off'
+static char last_batt_charge_status[16];
+
+//eg 35
+static int last_battery_temp[MAX_BATTERY_COUNT];
/* e.g. "charging 75%" */
static char last_battery_str[MAX_BATTERY_COUNT][64];
@@ -1496,6 +1612,9 @@ void init_batteries(void)
if (batteries_initialized) {
return;
}
+#ifdef HAVE_OPENMP
+#pragma omp parallel for schedule(dynamic,10)
+#endif /* HAVE_OPENMP */
for (idx = 0; idx < MAX_BATTERY_COUNT; idx++) {
batteries[idx][0] = '\0';
}
@@ -1504,25 +1623,233 @@ void init_batteries(void)
int get_battery_idx(const char *bat)
{
- int idx;
+// int idx;
+//
+// for (idx = 0; idx < MAX_BATTERY_COUNT; idx++) {
+// if (!strlen(batteries[idx]) || !strcmp(batteries[idx], bat)) {
+// break;
+// }
+// }
+//
+// /* if not found, enter a new entry */
+// if (!strlen(batteries[idx])) {
+// snprintf(batteries[idx], 31, "%s", bat);
+// }
- for (idx = 0; idx < MAX_BATTERY_COUNT; idx++) {
- if (!strlen(batteries[idx]) || !strcmp(batteries[idx], bat)) {
- break;
- }
+ return 0;
+}
+
+static int dbus_queue = 0;
+
+void set_dbus_retval(char *buffer, unsigned int n, int item);
+
+DBusConnection *connection;
+DBusError error;
+DBusMessage *message;
+DBusMessageIter iter;
+DBusBusType type;
+int message_type;
+DBusMessage *reply;
+
+void get_dbus_stuff(char *buffer,unsigned int intMax_length, int item)
+{
+ char method[128];
+ char path[128];
+ char dest[128];
+ char *args = "";
+ if (dbus_queue > 10)
+ {
+ fprintf (stderr, "too much dbus queuing\n");
+ exit(1);
+// set_dbus_retval(buffer, intMax_length, item);
+// return;
+ }
+ dbus_queue++;//prevent a queue from forming on these requests...
+//fetch data from dbus, store in here as last_cell_radio_dbm
+//return into buffer
+
+ type = DBUS_BUS_SYSTEM;
+ message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
+// print_reply = TRUE;
+// print_reply_literal = FALSE;
+ int reply_timeout_ms = 5000;
+ dbus_error_init (&error);
+ connection = dbus_bus_get (type, &error);
+ if (connection == NULL)
+ {
+ fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
+ (type == DBUS_BUS_SYSTEM) ? "system" : "session",
+ error.message);
+ dbus_error_free (&error);
+ exit (1);
+ }
+ switch(item){
+ case DBUS_CELL_DBM:
+ snprintf(method,127,"get_signal_strength");
+ snprintf(path,127,"/com/nokia/phone/net");
+ snprintf(dest,127,"com.nokia.phone.net");
+ message = dbus_message_new_method_call (dest,path,"Phone.Net",method);
+ dbus_message_set_auto_start (message, TRUE);
+ break;
+ case DBUS_CELL_PERCENT:
+ snprintf(method,127,"get_signal_strength");
+ snprintf(path,127,"/com/nokia/phone/net");
+ snprintf(dest,127,"com.nokia.phone.net");
+ message = dbus_message_new_method_call (dest,path,"Phone.Net",method);
+ dbus_message_set_auto_start (message, TRUE);
+ break;
+ case DBUS_HAL_BATTERY_CHRG_STATUS:
+ // 'full' 'on' 'off'
+ snprintf(method,127,"GetProperty");
+ args = "maemo.rechargeable.charging_status";
+ snprintf(path,127,"/org/freedesktop/Hal/devices/bme");
+ snprintf(dest,127,"org.freedesktop.Hal");
+ message = dbus_message_new_method_call (dest,path,"org.freedesktop.Hal.Device",method);
+ dbus_message_set_auto_start (message, TRUE);
+ dbus_message_iter_init_append(message, &iter);
+ if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &args))
+ fprintf (stderr, "OOM appending args\n");
+ break;
+ case DBUS_HAL_BATTERY_PERCENT:
+ // '96'
+ snprintf(method,127,"GetProperty");
+ args = "battery.charge_level.percentage";
+ snprintf(path,127,"/org/freedesktop/Hal/devices/bme");
+ snprintf(dest,127,"org.freedesktop.Hal");
+ message = dbus_message_new_method_call (dest,path,"org.freedesktop.Hal.Device",method);
+ dbus_message_set_auto_start (message, TRUE);
+ dbus_message_iter_init_append(message, &iter);
+ if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &args))
+ fprintf (stderr, "OOM appending args\n");
+ break;
+ case DBUS_HAL_BATTERY_VOLTS_CURRENT:
+ // '3600' - '4200'
+ snprintf(method,127,"GetProperty");
+ args = "battery.voltage.current";
+ snprintf(path,127,"/org/freedesktop/Hal/devices/bme");
+ snprintf(dest,127,"org.freedesktop.Hal");
+ message = dbus_message_new_method_call (dest,path,"org.freedesktop.Hal.Device",method);
+ dbus_message_set_auto_start (message, TRUE);
+ dbus_message_iter_init_append(message, &iter);
+ if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &args))
+ fprintf (stderr, "OOM appending args\n");
+ break;
+ default:
+ fprintf (stderr, "invalid item type in get_dbus_stuff");
+ break;
+ }
+ if (message == NULL)
+ {
+ fprintf (stderr, "Couldn't allocate D-Bus message\n");
+ exit (1);
+ }
+ if (!dbus_message_set_destination (message, dest))
+ {
+ fprintf (stderr, "Not enough memory\n");
+ exit (1);
}
- /* if not found, enter a new entry */
- if (!strlen(batteries[idx])) {
- snprintf(batteries[idx], 31, "%s", bat);
+ dbus_error_init (&error);
+ reply = dbus_connection_send_with_reply_and_block (connection, message, reply_timeout_ms, &error);
+ if (dbus_error_is_set (&error))
+ {
+ fprintf (stderr, "Error %s: %s\n",error.name,error.message);
+ //exit (1);//if we set timeout to 30s or something i guess it's okay to exit on "no reply" cuz something is fu*ked;
+ }
+ if (reply)
+ {
+ DBusMessageIter iter;
+ dbus_message_iter_init (reply, &iter);
+ //int type = dbus_message_iter_get_arg_type(&iter);
+ int current_fieldnumber = 0;
+ while (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INVALID)
+ {
+ //fprintf (stderr,"dbus-monitor too dumb to decipher arg type '%c'\n", type);
+ current_fieldnumber++;
+ switch(item){
+ case DBUS_CELL_DBM:
+ if (current_fieldnumber == 2)
+ {
+ unsigned char val;
+ dbus_message_iter_get_basic(&iter, &val);
+ last_cell_radio_dbm = val;
+ }
+ break;
+ case DBUS_CELL_PERCENT:
+ if (current_fieldnumber == 1)
+ {
+ unsigned char val;
+ dbus_message_iter_get_basic(&iter, &val);
+ last_cell_radio_percent = val;
+ }
+ break;
+ case DBUS_HAL_BATTERY_CHRG_STATUS:
+ if (current_fieldnumber == 1)
+ {
+ char *val;
+ dbus_message_iter_get_basic(&iter, &val);
+ snprintf(last_batt_charge_status,16,"%s",val);
+ }
+ break;
+ case DBUS_HAL_BATTERY_PERCENT:
+ if (current_fieldnumber == 1)
+ {
+ dbus_uint32_t val;
+ dbus_message_iter_get_basic(&iter, &val);
+ last_battery_perct[0] = val;
+ }
+ break;
+ case DBUS_HAL_BATTERY_VOLTS_CURRENT:
+ if (current_fieldnumber == 1)
+ {
+ dbus_uint32_t val;
+ dbus_message_iter_get_basic(&iter, &val);
+ last_battery_volts[0] = val;
+ }
+ break;
+ default:
+ fprintf (stderr, "invalid item type in get_dbus_stuff reply loop");
+ break;
+ }
+ dbus_message_iter_next (&iter);
+ }
+ dbus_message_unref (reply);
}
+ set_dbus_retval(buffer, intMax_length, item);
+ dbus_message_unref (message);
+ dbus_connection_unref (connection);
+ dbus_queue = 0;//reset to zero now that complete
+}
- return idx;
+void set_dbus_retval(char *buffer, unsigned int intMax_length, int item)
+{
+ switch (item) {
+ case DBUS_CELL_DBM:
+ snprintf(buffer, intMax_length, "%d", last_cell_radio_dbm);
+ break;
+ case DBUS_CELL_PERCENT:
+ snprintf(buffer, intMax_length, "%d", last_cell_radio_percent);
+ break;
+ case DBUS_HAL_BATTERY_CHRG_STATUS:
+ snprintf(buffer, intMax_length, "%s", last_batt_charge_status);
+ break;
+ case DBUS_HAL_BATTERY_PERCENT:
+ snprintf(buffer, intMax_length, "%i", last_battery_perct[0]);
+ break;
+ case DBUS_HAL_BATTERY_VOLTS_CURRENT:
+ snprintf(buffer, intMax_length, "%i", last_battery_volts[0]);
+ break;
+ default:
+ fprintf (stderr, "invalid item type in set_dbus_retval");
+ break;
+ }
}
+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;
char acpi_path[128];
char sysfs_path[128];
@@ -1535,7 +1862,8 @@ void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item)
/* don't update battery too often */
if (current_update_time - last_battery_time[idx] < 29.5) {
- goto set_return_value;
+ set_return_value(buffer, n, item, idx);
+ return;
}
last_battery_time[idx] = current_update_time;
@@ -1543,288 +1871,83 @@ void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item)
memset(last_battery_str[idx], 0, sizeof(last_battery_str[idx]));
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) {
+ /* first try SYSFS if that fails try DBUS */
+
+ if (sysfs_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);
- }
-
+
if (sysfs_bat_fp[idx] != NULL) {
/* SYSFS */
- int present_rate = -1;
- int remaining_capacity = -1;
- char charging_state[64];
+ int present_rate = -9999; //we will put "current now" into this. negative when charging!
+ int remaining_capacity = -1; //in %
+ char charging_state[64];//can't get this without hal bme
+ int voltage = -1;
+ int temp = 9999;
char present[4];
-
- strcpy(charging_state, "Unknown");
-
+ strcpy(present, "no");
+ strcpy(charging_state, "unknown");
+
while (!feof(sysfs_bat_fp[idx])) {
+ //we are looping through the data here
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");
- else if (strncmp (buf, "POWER_SUPPLY_PRESENT=0", 22) == 0)
- strcpy(present, "No");
- else if (strncmp (buf, "POWER_SUPPLY_STATUS=", 20) == 0)
- sscanf(buf, "POWER_SUPPLY_STATUS=%63s", charging_state);
- /* present_rate is not the same as the
- current flowing now but it is the same value
- which was used in the past. so we continue
- the tradition! */
+ if (strncmp (buf, "POWER_SUPPLY_PRESENT=1", 22) == 0)//does current row match this?
+ strcpy(present, "yes");//n900 always yes
+ else if (strncmp(buf, "POWER_SUPPLY_VOLTAGE_NOW=", 25) == 0)//keep checking possible matches
+ sscanf(buf, "POWER_SUPPLY_VOLTAGE_NOW=%d", &voltage);
else if (strncmp(buf, "POWER_SUPPLY_CURRENT_NOW=", 25) == 0)
sscanf(buf, "POWER_SUPPLY_CURRENT_NOW=%d", &present_rate);
- else if (strncmp(buf, "POWER_SUPPLY_ENERGY_NOW=", 24) == 0)
- sscanf(buf, "POWER_SUPPLY_ENERGY_NOW=%d", &remaining_capacity);
- else if (strncmp(buf, "POWER_SUPPLY_ENERGY_FULL=", 25) == 0)
- sscanf(buf, "POWER_SUPPLY_ENERGY_FULL=%d", &acpi_last_full[idx]);
- else if (strncmp(buf, "POWER_SUPPLY_CHARGE_NOW=", 24) == 0)
- sscanf(buf, "POWER_SUPPLY_CHARGE_NOW=%d", &remaining_capacity);
- else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=", 25) == 0)
- sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_last_full[idx]);
+ else if (strncmp(buf, "POWER_SUPPLY_CAPACITY=", 22) == 0)
+ sscanf(buf, "POWER_SUPPLY_CAPACITY=%d", &remaining_capacity);
+ else if (strncmp(buf, "POWER_SUPPLY_TEMP=", 18) == 0)
+ sscanf(buf, "POWER_SUPPLY_TEMP=%d", &temp);
}
-
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);
- }
+
+ last_battery_volts[idx] = voltage;
+ last_battery_temp[idx] = temp;
+
/* charging */
- else if (strcmp(charging_state, "Charging") == 0) {
- if (acpi_last_full[idx] != 0 && present_rate > 0) {
+ if (present_rate <= 0) {
/* e.g. charging 75% */
- snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %i%%",
- (int) (((float) remaining_capacity / acpi_last_full[idx]) * 100 ));
- /* e.g. 2h 37m */
- format_seconds(last_battery_time_str[idx], sizeof(last_battery_time_str[idx])-1,
- (long) (((float)(acpi_last_full[idx] - remaining_capacity) / present_rate) * 3600));
- } else if (acpi_last_full[idx] != 0 && present_rate <= 0) {
- snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %d%%",
- (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100));
- snprintf(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1, "unknown");
- } else {
- strncpy(last_battery_str[idx], "Charging", sizeof(last_battery_str[idx])-1);
- snprintf(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1, "unknown");
- }
- }
+ snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %i%%", remaining_capacity);
+ snprintf(last_battery_time_str[idx], sizeof(last_battery_time_str[idx]) - 1, "unknown");
+ }
/* discharging */
- else if (strncmp(charging_state, "Discharging", 64) == 0) {
- if (present_rate > 0) {
+ else if (present_rate > 0) {
/* e.g. discharging 35% */
- snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "discharging %i%%",
- (int) (((float) remaining_capacity / acpi_last_full[idx]) * 100 ));
- /* e.g. 1h 12m */
- format_seconds(last_battery_time_str[idx], sizeof(last_battery_time_str[idx])-1,
- (long) (((float)(acpi_last_full[idx] - remaining_capacity) / present_rate) * 3600));
- } else if (present_rate == 0) { /* Thanks to Nexox for this one */
- snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "full");
- snprintf(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1, "unknown");
- } else {
- snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1,
- "discharging %d%%",
- (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100));
- snprintf(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1, "unknown");
- }
+ snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "discharging %i%%", remaining_capacity);
+ snprintf(last_battery_time_str[idx], sizeof(last_battery_time_str[idx]) - 1, "unknown");
}
/* charged */
- /* thanks to Lukas Zapletal */
- else if (strncmp(charging_state, "Charged", 64) == 0) {
- /* Below happens with the second battery on my X40,
- * when the second one is empty and the first one
- * being charged. */
- if (remaining_capacity == 0)
- strcpy(last_battery_str[idx], "empty");
- else
- strcpy(last_battery_str[idx], "charged");
+ if (remaining_capacity == 100)
+ strcpy(last_battery_str[idx], "charged 100%");
+ } else {
+ //if don't have power kernel, use HAL / dbus
+ int remaining_capacity = -1; //in %
+ int temp = -1;
+ remaining_capacity = get_battery_perct(bat);
+ get_dbus_stuff(buffer,n,DBUS_HAL_BATTERY_VOLTS_CURRENT);
+ get_dbus_stuff(buffer,n,DBUS_HAL_BATTERY_CHRG_STATUS);
+ if (strncmp(buffer, "on", 2) == 0) {
+ snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %i%%", remaining_capacity);
}
- /* unknown, probably full / AC */
- else {
- if (acpi_last_full[idx] != 0
- && remaining_capacity != acpi_last_full[idx])
- snprintf(last_battery_str[idx], 64, "unknown %d%%",
- (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100));
- else
- strncpy(last_battery_str[idx], "AC", 64);
+ else if (strncmp(buffer, "off", 3) == 0) {
+ snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "discharging %i%%", remaining_capacity);
}
- } else if (acpi_bat_fp[idx] != NULL) {
- /* ACPI */
- int present_rate = -1;
- int remaining_capacity = -1;
- char charging_state[64];
- char present[4];
-
- /* read last full capacity if it's zero */
- if (acpi_last_full[idx] == 0) {
- static int rep3 = 0;
- char path[128];
- FILE *fp;
-
- snprintf(path, 127, ACPI_BATTERY_BASE_PATH "/%s/info", bat);
- fp = open_file(path, &rep3);
- if (fp != NULL) {
- while (!feof(fp)) {
- char b[256];
-
- if (fgets(b, 256, fp) == NULL) {
- break;
- }
- if (sscanf(b, "last full capacity: %d",
- &acpi_last_full[idx]) != 0) {
- break;
- }
- }
-
- fclose(fp);
- }
- }
-
- fseek(acpi_bat_fp[idx], 0, SEEK_SET);
-
- strcpy(charging_state, "unknown");
-
- while (!feof(acpi_bat_fp[idx])) {
- char buf[256];
-
- if (fgets(buf, 256, acpi_bat_fp[idx]) == NULL) {
- break;
- }
-
- /* let's just hope units are ok */
- if (strncmp(buf, "present:", 8) == 0) {
- sscanf(buf, "present: %4s", present);
- } else if (strncmp(buf, "charging state:", 15) == 0) {
- sscanf(buf, "charging state: %63s", charging_state);
- } else if (strncmp(buf, "present rate:", 13) == 0) {
- sscanf(buf, "present rate: %d", &present_rate);
- } else if (strncmp(buf, "remaining capacity:", 19) == 0) {
- sscanf(buf, "remaining capacity: %d", &remaining_capacity);
- }
- }
- /* Hellf[i]re notes that remaining capacity can exceed acpi_last_full */
- if (remaining_capacity > acpi_last_full[idx]) {
- /* normalize to 100% */
- acpi_last_full[idx] = remaining_capacity;
- }
-
- /* not present */
- if (strcmp(present, "no") == 0) {
- strncpy(last_battery_str[idx], "not present", 64);
- /* charging */
- } else if (strcmp(charging_state, "charging") == 0) {
- if (acpi_last_full[idx] != 0 && present_rate > 0) {
- /* e.g. charging 75% */
- snprintf(last_battery_str[idx],
- sizeof(last_battery_str[idx]) - 1, "charging %i%%",
- (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
- /* e.g. 2h 37m */
- format_seconds(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1,
- (long) (((acpi_last_full[idx] - remaining_capacity) *
- 3600) / present_rate));
- } else if (acpi_last_full[idx] != 0 && present_rate <= 0) {
- snprintf(last_battery_str[idx],
- sizeof(last_battery_str[idx]) - 1, "charging %d%%",
- (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
- snprintf(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1, "unknown");
- } else {
- strncpy(last_battery_str[idx], "charging",
- sizeof(last_battery_str[idx]) - 1);
- snprintf(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1, "unknown");
- }
- /* discharging */
- } else if (strncmp(charging_state, "discharging", 64) == 0) {
- if (present_rate > 0) {
- /* e.g. discharging 35% */
- snprintf(last_battery_str[idx],
- sizeof(last_battery_str[idx]) - 1, "discharging %i%%",
- (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
- /* e.g. 1h 12m */
- format_seconds(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1,
- (long) ((remaining_capacity * 3600) / present_rate));
- } else if (present_rate == 0) { /* Thanks to Nexox for this one */
- snprintf(last_battery_str[idx],
- sizeof(last_battery_str[idx]) - 1, "full");
- snprintf(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1, "unknown");
- } else {
- snprintf(last_battery_str[idx],
- sizeof(last_battery_str[idx]) - 1, "discharging %d%%",
- (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
- snprintf(last_battery_time_str[idx],
- sizeof(last_battery_time_str[idx]) - 1, "unknown");
- }
- /* charged */
- } else if (strncmp(charging_state, "charged", 64) == 0) {
- /* thanks to Lukas Zapletal */
- /* Below happens with the second battery on my X40,
- * when the second one is empty and the first one being charged. */
- if (remaining_capacity == 0) {
- strcpy(last_battery_str[idx], "empty");
- } else {
- strcpy(last_battery_str[idx], "charged");
- }
- /* unknown, probably full / AC */
- } else {
- if (acpi_last_full[idx] != 0
- && remaining_capacity != acpi_last_full[idx]) {
- snprintf(last_battery_str[idx], 64, "unknown %d%%",
- (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
- } else {
- strncpy(last_battery_str[idx], "AC", 64);
- }
- }
- } else {
- /* APM */
- if (apm_bat_fp[idx] == NULL) {
- apm_bat_fp[idx] = open_file(APM_PATH, &rep2);
- }
-
- if (apm_bat_fp[idx] != NULL) {
- unsigned int ac, status, flag;
- int life;
-
- fscanf(apm_bat_fp[idx], "%*s %*s %*x %x %x %x %d%%",
- &ac, &status, &flag, &life);
-
- if (life == -1) {
- /* could check now that there is ac */
- snprintf(last_battery_str[idx], 64, "AC");
-
- /* could check that status == 3 here? */
- } else if (ac && life != 100) {
- snprintf(last_battery_str[idx], 64, "charging %d%%", life);
- } else {
- snprintf(last_battery_str[idx], 64, "%d%%", life);
- }
-
- /* it seemed to buffer it so file must be closed (or could use
- * syscalls directly but I don't feel like coding it now) */
- fclose(apm_bat_fp[idx]);
- apm_bat_fp[idx] = NULL;
- }
+ else if (strncmp(buffer, "full", 4) == 0) {//no, it won't always be 100%. stupid dbus.
+ snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charged %i%%", remaining_capacity);
+ }
+ last_battery_temp[idx] = temp;
}
+ set_return_value(buffer, n, item, idx);
+}
-set_return_value:
+void set_return_value(char *buffer, unsigned int n, int item, int idx)
+{
switch (item) {
case BATTERY_STATUS:
snprintf(buffer, n, "%s", last_battery_str[idx]);
@@ -1832,119 +1955,154 @@ set_return_value:
case BATTERY_TIME:
snprintf(buffer, n, "%s", last_battery_time_str[idx]);
break;
+ case BATTERY_VOLTS:
+ snprintf(buffer, n, "%i", last_battery_volts[idx]); // voltage
+ break;
+ case BATTERY_TEMP:
+ snprintf(buffer, n, "%i", last_battery_temp[idx]); // temperature
+ break;
default:
+ fprintf (stderr, "invalid item type in set_return_value");
break;
}
}
+void get_battery_short_status(char *buffer, unsigned int n, const char *bat)
+{
+ get_battery_stuff(buffer, n, bat, BATTERY_STATUS);
+ if (0 == strncmp("charging", buffer, 8)) {
+ buffer[0] = 'C';
+ memmove(buffer + 1, buffer + 8, n - 8);
+ } else if (0 == strncmp("discharging", buffer, 11)) {
+ buffer[0] = 'D';
+ memmove(buffer + 1, buffer + 11, n - 11);
+ } else if (0 == strncmp("charged", buffer, 7)) {
+ buffer[0] = 'F';
+ memmove(buffer + 1, buffer + 7, n - 7);
+ } else if (0 == strncmp("not present", buffer, 11)) {
+ buffer[0] = 'N';
+ memmove(buffer + 1, buffer + 11, n - 11);
+ } else if (0 == strncmp("empty", buffer, 5)) {
+ buffer[0] = 'E';
+ memmove(buffer + 1, buffer + 5, n - 5);
+ } else if (0 != strncmp("AC", buffer, 2)) {
+ buffer[0] = 'U';
+ memmove(buffer + 1, buffer + 11, n - 11);
+ } else fprintf (stderr, "invalid input buffer in get_battery_short_status");
+}
+
int get_battery_perct(const char *bat)
{
static int rep = 0;
int idx;
- char acpi_path[128];
char sysfs_path[128];
int remaining_capacity = -1;
-
- snprintf(acpi_path, 127, ACPI_BATTERY_BASE_PATH "/%s/state", bat);
snprintf(sysfs_path, 127, SYSFS_BATTERY_BASE_PATH "/%s/uevent", bat);
init_batteries();
-
idx = get_battery_idx(bat);
/* don't update battery too often */
- if (current_update_time - last_battery_perct_time[idx] < 30) {
+ if (current_update_time - last_battery_perct_time[idx] < 29.5) {
return last_battery_perct[idx];
}
last_battery_perct_time[idx] = current_update_time;
- /* Only check for SYSFS or 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);
- }
-
+ /* try SYSFS first */
+ sysfs_bat_fp[idx] = open_file(sysfs_path, &rep);
if (sysfs_bat_fp[idx] != NULL) {
/* SYSFS */
while (!feof(sysfs_bat_fp[idx])) {
char buf[256];
if (fgets(buf, 256, sysfs_bat_fp[idx]) == NULL)
break;
-
- if (strncmp(buf, "POWER_SUPPLY_CHARGE_NOW=", 24) == 0) {
- sscanf(buf, "POWER_SUPPLY_CHARGE_NOW=%d", &remaining_capacity);
- } else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=",25) == 0) {
- sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_design_capacity[idx]);
- } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_NOW=", 24) == 0) {
- sscanf(buf, "POWER_SUPPLY_ENERGY_NOW=%d", &remaining_capacity);
- } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_FULL=",25) == 0) {
- sscanf(buf, "POWER_SUPPLY_ENERGY_FULL=%d", &acpi_design_capacity[idx]);
+ if (strncmp(buf, "POWER_SUPPLY_CAPACITY=", 22) == 0) {
+ sscanf(buf, "POWER_SUPPLY_CAPACITY=%d", &remaining_capacity);
+
+// if (strncmp(buf, "POWER_SUPPLY_CHARGE_NOW=", 24) == 0) {
+// sscanf(buf, "POWER_SUPPLY_CHARGE_NOW=%d", &remaining_capacity);
+// } else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=",25) == 0) {
+// sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_design_capacity[idx]);
+// } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_NOW=", 24) == 0) {
+// sscanf(buf, "POWER_SUPPLY_ENERGY_NOW=%d", &remaining_capacity);
+// } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_FULL=",25) == 0) {
+// sscanf(buf, "POWER_SUPPLY_ENERGY_FULL=%d", &acpi_design_capacity[idx]);
+// }
}
}
-
fclose(sysfs_bat_fp[idx]);
sysfs_bat_fp[idx] = NULL;
+ last_battery_perct[idx] = remaining_capacity;
- } else if (acpi_bat_fp[idx] != NULL) {
- /* ACPI */
- /* read last full capacity if it's zero */
- if (acpi_design_capacity[idx] == 0) {
- static int rep2;
- char path[128];
- FILE *fp;
-
- snprintf(path, 127, ACPI_BATTERY_BASE_PATH "/%s/info", bat);
- fp = open_file(path, &rep2);
- if (fp != NULL) {
- while (!feof(fp)) {
- char b[256];
-
- if (fgets(b, 256, fp) == NULL) {
- break;
- }
- if (sscanf(b, "last full capacity: %d",
- &acpi_design_capacity[idx]) != 0) {
- break;
- }
- }
- fclose(fp);
- }
- }
-
- fseek(acpi_bat_fp[idx], 0, SEEK_SET);
-
- while (!feof(acpi_bat_fp[idx])) {
- char buf[256];
-
- if (fgets(buf, 256, acpi_bat_fp[idx]) == NULL) {
- break;
- }
-
- if (buf[0] == 'r') {
- sscanf(buf, "remaining capacity: %d", &remaining_capacity);
- }
- }
- }
- if (remaining_capacity < 0) {
- return 0;
}
+ else
+ {
+ char *useless;
+ useless = malloc(128 * sizeof(char));
+ get_dbus_stuff(useless,128,DBUS_HAL_BATTERY_PERCENT);
+ //i told you it's useless...
+ //snprintf(last_battery_perct[idx], 127, "%i", useless);
+ //guess what? dbus battery % doesn't update while charging
+// get_dbus_stuff(useless,n,DBUS_HAL_BATTERY_CHRG_STATUS);
+// if (strncmp(useless, "full", 4) == 0)
+// {
+//
+// }
+ }
+// else if (acpi_bat_fp[idx] != NULL) {
+// /* ACPI */
+// /* read last full capacity if it's zero */
+// if (acpi_design_capacity[idx] == 0) {
+// static int rep2;
+// char path[128];
+// FILE *fp;
+//
+// snprintf(path, 127, ACPI_BATTERY_BASE_PATH "/%s/info", bat);
+// fp = open_file(path, &rep2);
+// if (fp != NULL) {
+// while (!feof(fp)) {
+// char b[256];
+//
+// if (fgets(b, 256, fp) == NULL) {
+// break;
+// }
+// if (sscanf(b, "last full capacity: %d",
+// &acpi_design_capacity[idx]) != 0) {
+// break;
+// }
+// }
+// fclose(fp);
+// }
+// }
+//
+// fseek(acpi_bat_fp[idx], 0, SEEK_SET);
+//
+// while (!feof(acpi_bat_fp[idx])) {
+// char buf[256];
+//
+// if (fgets(buf, 256, acpi_bat_fp[idx]) == NULL) {
+// break;
+// }
+//
+// if (buf[0] == 'r') {
+// sscanf(buf, "remaining capacity: %d", &remaining_capacity);
+// }
+// }
+// }
+
/* compute the battery percentage */
- last_battery_perct[idx] =
- (int) (((float) remaining_capacity / acpi_design_capacity[idx]) * 100);
+
+ //(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];
}
-int get_battery_perct_bar(const char *bar)
+int get_battery_perct_bar(const char *bat)
{
int idx;
- get_battery_perct(bar);
- idx = get_battery_idx(bar);
+ get_battery_perct(bat);
+ idx = get_battery_idx(bat);
return (int) (last_battery_perct[idx] * 2.56 - 1);
}
@@ -1991,6 +2149,9 @@ void get_powerbook_batt_info(char *buffer, size_t n, int i)
if (pmu_battery_fp == NULL) {
pmu_battery_fp = open_file(batt_path, &rep);
+ if (pmu_battery_fp == NULL) {
+ return;
+ }
}
if (pmu_battery_fp != NULL) {
@@ -2015,6 +2176,9 @@ void get_powerbook_batt_info(char *buffer, size_t n, int i)
}
if (pmu_info_fp == NULL) {
pmu_info_fp = open_file(info_path, &rep);
+ if (pmu_info_fp == NULL) {
+ return;
+ }
}
if (pmu_info_fp != NULL) {
@@ -2032,19 +2196,24 @@ void get_powerbook_batt_info(char *buffer, size_t n, int i)
}
/* update status string */
if ((ac && !(flags & PMU_BATT_PRESENT))) {
- strcpy(pb_battery_info[PB_BATT_STATUS], "AC");
+ strncpy(pb_battery_info[PB_BATT_STATUS], "AC", sizeof(pb_battery_info[PB_BATT_STATUS]));
} else if (ac && (flags & PMU_BATT_PRESENT)
&& !(flags & PMU_BATT_CHARGING)) {
- strcpy(pb_battery_info[PB_BATT_STATUS], "charged");
+ strncpy(pb_battery_info[PB_BATT_STATUS], "charged", sizeof(pb_battery_info[PB_BATT_STATUS]));
} else if ((flags & PMU_BATT_PRESENT) && (flags & PMU_BATT_CHARGING)) {
- strcpy(pb_battery_info[PB_BATT_STATUS], "charging");
+ strncpy(pb_battery_info[PB_BATT_STATUS], "charging", sizeof(pb_battery_info[PB_BATT_STATUS]));
} else {
- strcpy(pb_battery_info[PB_BATT_STATUS], "discharging");
+ strncpy(pb_battery_info[PB_BATT_STATUS], "discharging", sizeof(pb_battery_info[PB_BATT_STATUS]));
}
/* update percentage string */
- if (timeval == 0) {
- pb_battery_info[PB_BATT_PERCENT][0] = 0;
+ if (timeval == 0 && ac && (flags & PMU_BATT_PRESENT)
+ && !(flags & PMU_BATT_CHARGING)) {
+ snprintf(pb_battery_info[PB_BATT_PERCENT],
+ sizeof(pb_battery_info[PB_BATT_PERCENT]), "100%%");
+ } else if (timeval == 0) {
+ snprintf(pb_battery_info[PB_BATT_PERCENT],
+ sizeof(pb_battery_info[PB_BATT_PERCENT]), "unknown");
} else {
snprintf(pb_battery_info[PB_BATT_PERCENT],
sizeof(pb_battery_info[PB_BATT_PERCENT]), "%d%%",
@@ -2053,7 +2222,8 @@ void get_powerbook_batt_info(char *buffer, size_t n, int i)
/* update time string */
if (timeval == 0) { /* fully charged or battery not present */
- pb_battery_info[PB_BATT_TIME][0] = 0;
+ snprintf(pb_battery_info[PB_BATT_TIME],
+ sizeof(pb_battery_info[PB_BATT_TIME]), "unknown");
} else if (timeval < 60 * 60) { /* don't show secs */
format_seconds_short(pb_battery_info[PB_BATT_TIME],
sizeof(pb_battery_info[PB_BATT_TIME]), timeval);
@@ -2065,399 +2235,164 @@ void get_powerbook_batt_info(char *buffer, size_t n, int i)
snprintf(buffer, n, "%s", pb_battery_info[i]);
}
-void update_top(void)
+int update_top(void)
{
- show_nice_processes = 1;
- process_find_top(info.cpu, info.memu);
+ process_find_top(info.cpu, info.memu, info.time
+#ifdef IOSTATS
+ , info.io
+#endif
+ );
info.first_process = get_first_process();
+ return 0;
}
-/* The following ifdefs were adapted from gkrellm */
-#include
-
-#if !defined(MD_MAJOR)
-#define MD_MAJOR 9
-#endif
-
-#if !defined(LVM_BLK_MAJOR)
-#define LVM_BLK_MAJOR 58
-#endif
-
-#if !defined(NBD_MAJOR)
-#define NBD_MAJOR 43
-#endif
+#define ENTROPY_AVAIL_PATH "/proc/sys/kernel/random/entropy_avail"
-void update_diskio(void)
+int get_entropy_avail(unsigned int *val)
{
- static unsigned int last = UINT_MAX;
- static unsigned int last_read = UINT_MAX;
- static unsigned int last_write = UINT_MAX;
- FILE *fp;
static int rep = 0;
+ FILE *fp;
- char buf[512], devbuf[64];
- int i;
- unsigned int major, minor;
- unsigned int current = 0;
- unsigned int current_read = 0;
- unsigned int current_write = 0;
- unsigned int reads, writes = 0;
- int col_count = 0;
- int tot, tot_read, tot_write;
-
- if (!(fp = open_file("/proc/diskstats", &rep))) {
- diskio_value = 0;
- return;
- }
-
- /* read reads and writes from all disks (minor = 0), including cd-roms
- * and floppies, and sum them up */
- while (!feof(fp)) {
- fgets(buf, 512, fp);
- col_count = sscanf(buf, "%u %u %s %*u %*u %u %*u %*u %*u %u", &major,
- &minor, devbuf, &reads, &writes);
- /* ignore subdevices (they have only 3 matching entries in their line)
- * and virtual devices (LVM, network block devices, RAM disks, Loopback)
- *
- * 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) {
- current += reads + writes;
- current_read += reads;
- current_write += writes;
- } else {
- col_count = sscanf(buf, "%u %u %s %*u %u %*u %u",
- &major, &minor, devbuf, &reads, &writes);
- if (col_count != 5) {
- continue;
- }
- }
- for (i = 0; i < MAX_DISKIO_STATS; i++) {
- if (diskio_stats[i].dev &&
- strncmp(devbuf, diskio_stats[i].dev, text_buffer_size) == 0) {
- diskio_stats[i].current =
- (reads + writes - diskio_stats[i].last) / 2;
- diskio_stats[i].current_read =
- (reads - diskio_stats[i].last_read) / 2;
- diskio_stats[i].current_write =
- (writes - diskio_stats[i].last_write) / 2;
- if (reads + writes < diskio_stats[i].last) {
- diskio_stats[i].current = 0;
- }
- if (reads < diskio_stats[i].last_read) {
- diskio_stats[i].current_read = 0;
- diskio_stats[i].current = diskio_stats[i].current_write;
- }
- if (writes < diskio_stats[i].last_write) {
- diskio_stats[i].current_write = 0;
- diskio_stats[i].current = diskio_stats[i].current_read;
- }
- diskio_stats[i].last = reads + writes;
- diskio_stats[i].last_read = reads;
- diskio_stats[i].last_write = writes;
- }
- }
- }
-
- /* since the values in /proc/diststats are absolute, we have to substract
- * our last reading. The numbers stand for "sectors read", and we therefore
- * have to divide by two to get KB */
- tot = ((double) (current - last) / 2);
- tot_read = ((double) (current_read - last_read) / 2);
- tot_write = ((double) (current_write - last_write) / 2);
-
- if (last_read > current_read) {
- tot_read = 0;
- }
- if (last_write > current_write) {
- tot_write = 0;
- }
-
- if (last > current) {
- /* we hit this either if it's the very first time we run this, or
- * when /proc/diskstats overflows; while 0 is not correct, it's at
- * least not way off */
- tot = 0;
- }
- last = current;
- last_read = current_read;
- last_write = current_write;
+ if (!(fp = open_file(ENTROPY_AVAIL_PATH, &rep)))
+ return 1;
- diskio_value = tot;
- diskio_read_value = tot_read;
- diskio_write_value = tot_write;
+ if (fscanf(fp, "%u", val) != 1)
+ return 1;
fclose(fp);
+ return 0;
}
-/* Here come the IBM ACPI-specific things. For reference, see
- * http://ibm-acpi.sourceforge.net/README
- * If IBM ACPI is installed, /proc/acpi/ibm contains the following files:
-bay
-beep
-bluetooth
-brightness
-cmos
-dock
-driver
-ecdump
-fan
-hotkey
-led
-light
-thermal
-video
-volume
- * The content of these files is described in detail in the aforementioned
- * README - some of them also in the following functions accessing them.
- * Peter Tarjan (ptarjan@citromail.hu) */
+#define ENTROPY_POOLSIZE_PATH "/proc/sys/kernel/random/poolsize"
-#define IBM_ACPI_DIR "/proc/acpi/ibm"
-
-/* get fan speed on IBM/Lenovo laptops running the ibm acpi.
- * /proc/acpi/ibm/fan looks like this (3 lines):
-status: disabled
-speed: 2944
-commands: enable, disable
- * Peter Tarjan (ptarjan@citromail.hu) */
-
-void get_ibm_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
+int get_entropy_poolsize(unsigned int *val)
{
+ static int rep = 0;
FILE *fp;
- unsigned int speed = 0;
- char fan[128];
- if (!p_client_buffer || client_buffer_size <= 0) {
- return;
- }
-
- snprintf(fan, 127, "%s/fan", IBM_ACPI_DIR);
-
- fp = fopen(fan, "r");
- if (fp != NULL) {
- while (!feof(fp)) {
- char line[256];
+ if (!(fp = open_file(ENTROPY_POOLSIZE_PATH, &rep)))
+ return 1;
- if (fgets(line, 255, fp) == NULL) {
- break;
- }
- if (sscanf(line, "speed: %u", &speed)) {
- break;
- }
- }
- } 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));
- }
+ if (fscanf(fp, "%u", val) != 1)
+ return 1;
fclose(fp);
- snprintf(p_client_buffer, client_buffer_size, "%d", speed);
+ return 0;
}
-/* get the measured temperatures from the temperature sensors
- * on IBM/Lenovo laptops running the ibm acpi.
- * There are 8 values in /proc/acpi/ibm/thermal, and according to
- * http://ibm-acpi.sourceforge.net/README
- * these mean the following (at least on an IBM R51...)
- * 0: CPU (also on the T series laptops)
- * 1: Mini PCI Module (?)
- * 2: HDD (?)
- * 3: GPU (also on the T series laptops)
- * 4: Battery (?)
- * 5: N/A
- * 6: Battery (?)
- * 7: N/A
- * I'm not too sure about those with the question mark, but the values I'm
- * reading from *my* thermal file (on a T42p) look realistic for the
- * hdd and the battery.
- * #5 and #7 are always -128.
- * /proc/acpi/ibm/thermal looks like this (1 line):
-temperatures: 41 43 31 46 33 -128 29 -128
- * Peter Tarjan (ptarjan@citromail.hu) */
-
-static double last_ibm_acpi_temp_time;
-void get_ibm_acpi_temps(void)
+const char *get_disk_protect_queue(const char *disk)
{
-
FILE *fp;
- char thermal[128];
+ char path[128];
+ int state;
- /* don't update too often */
- if (current_update_time - last_ibm_acpi_temp_time < 10.00) {
- return;
+ snprintf(path, 127, "/sys/block/%s/device/unload_heads", disk);
+ if (access(path, F_OK)) {
+ snprintf(path, 127, "/sys/block/%s/queue/protect", disk);
}
- last_ibm_acpi_temp_time = current_update_time;
-
- /* if (!p_client_buffer || client_buffer_size <= 0) {
- return;
- } */
-
- snprintf(thermal, 127, "%s/thermal", IBM_ACPI_DIR);
- fp = fopen(thermal, "r");
-
- if (fp != NULL) {
- while (!feof(fp)) {
- char line[256];
-
- if (fgets(line, 255, fp) == NULL) {
- break;
- }
- if (sscanf(line, "temperatures: %d %d %d %d %d %d %d %d",
- &ibm_acpi.temps[0], &ibm_acpi.temps[1], &ibm_acpi.temps[2],
- &ibm_acpi.temps[3], &ibm_acpi.temps[4], &ibm_acpi.temps[5],
- &ibm_acpi.temps[6], &ibm_acpi.temps[7])) {
- break;
- }
- }
- } 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));
+ if ((fp = fopen(path, "r")) == NULL)
+ return "n/a ";
+ if (fscanf(fp, "%d\n", &state) != 1) {
+ fclose(fp);
+ return "failed";
}
-
fclose(fp);
+ return (state > 0) ? "frozen" : "free ";
}
-/* get volume (0-14) on IBM/Lenovo laptops running the ibm acpi.
- * "Volume" here is none of the mixer volumes, but a "master of masters"
- * volume adjusted by the IBM volume keys.
- * /proc/acpi/ibm/fan looks like this (4 lines):
-level: 4
-mute: off
-commands: up, down, mute
-commands: level ( is 0-15)
- * Peter Tarjan (ptarjan@citromail.hu) */
-
-void get_ibm_acpi_volume(char *p_client_buffer, size_t client_buffer_size)
+typedef struct DEV_LIST_TYPE
{
- FILE *fp;
- char volume[128];
- unsigned int vol = -1;
- char mute[3] = "";
+ char *dev_name;
+ int memoized;
+ struct DEV_LIST_TYPE *next;
- if (!p_client_buffer || client_buffer_size <= 0) {
- return;
- }
+} DEV_LIST, *DEV_LIST_PTR;
- snprintf(volume, 127, "%s/volume", IBM_ACPI_DIR);
+/* 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;
- fp = fopen(volume, "r");
- if (fp != NULL) {
- while (!feof(fp)) {
- char line[256];
- unsigned int read_vol = -1;
+ dev_cur = dev_head;
- if (fgets(line, 255, fp) == NULL) {
- break;
- }
- if (sscanf(line, "level: %u", &read_vol)) {
- vol = read_vol;
- continue;
- }
- if (sscanf(line, "mute: %s", mute)) {
- break;
- }
- }
- } 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));
+ while (dev_cur) {
+ if (strcmp(dev_cur->dev_name, dev) == 0)
+ return dev_cur->memoized;
+ dev_last = dev_cur;
+ dev_cur = dev_cur->next;
}
- fclose(fp);
+ 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;
- if (strcmp(mute, "on") == 0) {
- snprintf(p_client_buffer, client_buffer_size, "%s", "mute");
- return;
- } else {
- snprintf(p_client_buffer, client_buffer_size, "%d", vol);
- return;
- }
-}
+ while ((slash = strchr(dev, '/')))
+ *slash = '!';
+ snprintf(syspath, sizeof(syspath), "/sys/block/%s", dev);
+ dev_cur->memoized = !(access(syspath, F_OK));
-/* static FILE *fp = NULL; */
+ if (dev_head)
+ dev_last->next = dev_cur;
+ else
+ dev_head = dev_cur;
-/* get LCD brightness on IBM/Lenovo laptops running the ibm acpi.
- * /proc/acpi/ibm/brightness looks like this (3 lines):
-level: 7
-commands: up, down
-commands: level ( is 0-7)
- * Peter Tarjan (ptarjan@citromail.hu) */
+ return dev_cur->memoized;
+}
-void get_ibm_acpi_brightness(char *p_client_buffer, size_t client_buffer_size)
+int update_diskio(void)
{
FILE *fp;
- unsigned int brightness = 0;
- char filename[128];
-
- if (!p_client_buffer || client_buffer_size <= 0) {
- return;
- }
+ static int rep = 0;
+ char buf[512], devbuf[64];
+ unsigned int major, minor;
+ int col_count = 0;
+ struct diskio_stat *cur;
+ unsigned int reads, writes;
+ unsigned int total_reads = 0, total_writes = 0;
- snprintf(filename, 127, "%s/brightness", IBM_ACPI_DIR);
+ stats.current = 0;
+ stats.current_read = 0;
+ stats.current_write = 0;
- fp = fopen(filename, "r");
- if (fp != NULL) {
- while (!feof(fp)) {
- char line[256];
+ if (!(fp = open_file("/proc/diskstats", &rep))) {
+ return 0;
+ }
- if (fgets(line, 255, fp) == NULL) {
- break;
+ /* read reads and writes from all disks (minor = 0), including cd-roms
+ * and floppies, and sum them up */
+ while (fgets(buf, 512, fp)) {
+ col_count = sscanf(buf, "%u %u %s %*u %*u %u %*u %*u %*u %u", &major,
+ &minor, devbuf, &reads, &writes);
+ /* ignore subdevices (they have only 3 matching entries in their line)
+ * and virtual devices (LVM, network block devices, RAM disks, Loopback)
+ *
+ * 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) {
+ /* check needed for kernel >= 2.6.31, see sf #2942117 */
+ if (is_disk(devbuf)) {
+ total_reads += reads;
+ total_writes += writes;
}
- if (sscanf(line, "level: %u", &brightness)) {
- break;
+ } else {
+ col_count = sscanf(buf, "%u %u %s %*u %u %*u %u",
+ &major, &minor, devbuf, &reads, &writes);
+ if (col_count != 5) {
+ continue;
}
}
- } 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));
- }
-
- fclose(fp);
-
- snprintf(p_client_buffer, client_buffer_size, "%d", brightness);
-}
-
-void update_entropy(void)
-{
- 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;
-
- info.entropy.entropy_avail = 0;
- info.entropy.poolsize = 0;
+ cur = stats.next;
+ while (cur && strcmp(devbuf, cur->dev))
+ cur = cur->next;
- if ((fp1 = open_file(entropy_avail, &rep)) == NULL) {
- return;
- }
-
- if ((fp2 = open_file(entropy_poolsize, &rep)) == NULL) {
- fclose(fp1);
- return;
- }
-
- fscanf(fp1, "%u", &info.entropy.entropy_avail);
- fscanf(fp2, "%u", &info.entropy.poolsize);
-
- fclose(fp1);
- fclose(fp2);
-
- info.mask |= (1 << INFO_ENTROPY);
-}
-
-const char *get_disk_protect_queue(const char *disk)
-{
- FILE *fp;
- char path[128];
- int state;
-
- 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) {
- fclose(fp);
- return "failed";
+ if (cur)
+ update_diskio_values(cur, reads, writes);
}
+ update_diskio_values(&stats, total_reads, total_writes);
fclose(fp);
- return state ? "frozen" : "free ";
+ return 0;
}
-