Bugfix: ac-adapter is sometimes called ADP1 in sysfs instead of AC
[monky] / src / linux.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2007 Toni Spets
14  * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
15  *      (see AUTHORS)
16  * All rights reserved.
17  *
18  * This program is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  * You should have received a copy of the GNU General Public License
28  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29  *
30  */
31
32 #include "conky.h"
33 #include "logging.h"
34 #include "common.h"
35 #include "linux.h"
36 #include "net_stat.h"
37 #include "diskio.h"
38 #include "temphelper.h"
39 #include <dirent.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <limits.h>
43 #include <sys/types.h>
44 #include <sys/sysinfo.h>
45 #include <sys/stat.h>
46 #ifndef HAVE_CLOCK_GETTIME
47 #include <sys/time.h>
48 #endif
49 #include <fcntl.h>
50 #include <unistd.h>
51 // #include <assert.h>
52 #include <time.h>
53 #include "top.h"
54
55 #include <sys/ioctl.h>
56 #include <sys/socket.h>
57 #include <netinet/in.h>
58 #include <linux/sockios.h>
59 #include <net/if.h>
60 #include <arpa/inet.h>
61 #ifdef _NET_IF_H
62 #define _LINUX_IF_H
63 #endif
64 #include <linux/route.h>
65 #include <math.h>
66 #include <pthread.h>
67
68 /* The following ifdefs were adapted from gkrellm */
69 #include <linux/major.h>
70
71 #if !defined(MD_MAJOR)
72 #define MD_MAJOR 9
73 #endif
74
75 #if !defined(LVM_BLK_MAJOR)
76 #define LVM_BLK_MAJOR 58
77 #endif
78
79 #if !defined(NBD_MAJOR)
80 #define NBD_MAJOR 43
81 #endif
82
83 #ifdef HAVE_IWLIB
84 #include <iwlib.h>
85 #endif
86
87 struct sysfs {
88         int fd;
89         int arg;
90         char devtype[256];
91         char type[64];
92         float factor, offset;
93 };
94
95 #define SHORTSTAT_TEMPL "%*s %llu %llu %llu"
96 #define LONGSTAT_TEMPL "%*s %llu %llu %llu "
97
98 /* This flag tells the linux routines to use the /proc system where possible,
99  * even if other api's are available, e.g. sysinfo() or getloadavg().
100  * the reason for this is to allow for /proc-based distributed monitoring.
101  * using a flag in this manner creates less confusing code. */
102 static int prefer_proc = 0;
103
104 void prepare_update(void)
105 {
106 }
107
108 void update_uptime(void)
109 {
110 #ifdef HAVE_SYSINFO
111         if (!prefer_proc) {
112                 struct sysinfo s_info;
113
114                 sysinfo(&s_info);
115                 info.uptime = (double) s_info.uptime;
116         } else
117 #endif
118         {
119                 static int rep = 0;
120                 FILE *fp;
121
122                 if (!(fp = open_file("/proc/uptime", &rep))) {
123                         info.uptime = 0.0;
124                         return;
125                 }
126                 fscanf(fp, "%lf", &info.uptime);
127                 fclose(fp);
128         }
129 }
130
131 int check_mount(char *s)
132 {
133         int ret = 0;
134         FILE *mtab = fopen("/etc/mtab", "r");
135
136         if (mtab) {
137                 char buf1[256], buf2[128];
138
139                 while (fgets(buf1, 256, mtab)) {
140                         sscanf(buf1, "%*s %128s", buf2);
141                         if (!strcmp(s, buf2)) {
142                                 ret = 1;
143                                 break;
144                         }
145                 }
146                 fclose(mtab);
147         } else {
148                 NORM_ERR("Could not open mtab");
149         }
150         return ret;
151 }
152
153 /* these things are also in sysinfo except Buffers:
154  * (that's why I'm reading them from proc) */
155
156 void update_meminfo(void)
157 {
158         FILE *meminfo_fp;
159         static int rep = 0;
160
161         /* unsigned int a; */
162         char buf[256];
163
164         info.mem = info.memmax = info.swap = info.swapfree = info.swapmax = info.bufmem =
165                 info.buffers = info.cached = info.memfree = info.memeasyfree = 0;
166
167         if (!(meminfo_fp = open_file("/proc/meminfo", &rep))) {
168                 return;
169         }
170
171         while (!feof(meminfo_fp)) {
172                 if (fgets(buf, 255, meminfo_fp) == NULL) {
173                         break;
174                 }
175
176                 if (strncmp(buf, "MemTotal:", 9) == 0) {
177                         sscanf(buf, "%*s %llu", &info.memmax);
178                 } else if (strncmp(buf, "MemFree:", 8) == 0) {
179                         sscanf(buf, "%*s %llu", &info.memfree);
180                 } else if (strncmp(buf, "SwapTotal:", 10) == 0) {
181                         sscanf(buf, "%*s %llu", &info.swapmax);
182                 } else if (strncmp(buf, "SwapFree:", 9) == 0) {
183                         sscanf(buf, "%*s %llu", &info.swapfree);
184                 } else if (strncmp(buf, "Buffers:", 8) == 0) {
185                         sscanf(buf, "%*s %llu", &info.buffers);
186                 } else if (strncmp(buf, "Cached:", 7) == 0) {
187                         sscanf(buf, "%*s %llu", &info.cached);
188                 }
189         }
190
191         info.mem = info.memmax - info.memfree;
192         info.memeasyfree = info.memfree;
193         info.swap = info.swapmax - info.swapfree;
194
195         info.bufmem = info.cached + info.buffers;
196
197         fclose(meminfo_fp);
198 }
199
200 int get_laptop_mode(void)
201 {
202         FILE *fp;
203         int val = -1;
204
205         if ((fp = fopen("/proc/sys/vm/laptop_mode", "r")) != NULL)
206                 fscanf(fp, "%d\n", &val);
207         fclose(fp);
208         return val;
209 }
210
211 /* my system says:
212  * # cat /sys/block/sda/queue/scheduler
213  * noop [anticipatory] cfq
214  */
215 char *get_ioscheduler(char *disk)
216 {
217         FILE *fp;
218         char buf[128];
219
220         if (!disk)
221                 return strndup("n/a", text_buffer_size);
222
223         snprintf(buf, 127, "/sys/block/%s/queue/scheduler", disk);
224         if ((fp = fopen(buf, "r")) == NULL) {
225                 return strndup("n/a", text_buffer_size);
226         }
227         while (!feof(fp)) {
228                 fscanf(fp, "%127s", buf);
229                 if (buf[0] == '[') {
230                         buf[strlen(buf) - 1] = '\0';
231                         fclose(fp);
232                         return strndup(buf + 1, text_buffer_size);
233                 }
234         }
235         fclose(fp);
236         return strndup("n/a", text_buffer_size);
237 }
238
239 static struct {
240         char *iface;
241         char *ip;
242         int count;
243 } gw_info;
244
245 #define COND_FREE(x) if(x) free(x); x = 0
246 #define SAVE_SET_STRING(x, y) \
247         if (x && strcmp((char *)x, (char *)y)) { \
248                 free(x); \
249                 x = strndup("multiple", text_buffer_size); \
250         } else if (!x) { \
251                 x = strndup(y, text_buffer_size); \
252         }
253
254 void update_gateway_info_failure(const char *reason)
255 {
256         if(reason != NULL) {
257                 perror(reason);
258         }
259         //2 pointers to 1 location causes a crash when we try to free them both
260         gw_info.iface = strndup("failed", text_buffer_size);
261         gw_info.ip = strndup("failed", text_buffer_size);
262 }
263
264
265 /* Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT */
266 #define RT_ENTRY_FORMAT "%63s %lx %lx %x %*d %*d %*d %lx %*d %*d %*d\n"
267
268 void update_gateway_info(void)
269 {
270         FILE *fp;
271         struct in_addr ina;
272         char iface[64];
273         unsigned long dest, gate, mask;
274         unsigned int flags;
275
276         COND_FREE(gw_info.iface);
277         COND_FREE(gw_info.ip);
278         gw_info.count = 0;
279
280         if ((fp = fopen("/proc/net/route", "r")) == NULL) {
281                 update_gateway_info_failure("fopen()");
282                 return;
283         }
284
285         /* skip over the table header line, which is always present */
286         fscanf(fp, "%*[^\n]\n");
287
288         while (!feof(fp)) {
289                 if(fscanf(fp, RT_ENTRY_FORMAT,
290                           iface, &dest, &gate, &flags, &mask) != 5) {
291                         update_gateway_info_failure("fscanf()");
292                         break;
293                 }
294                 if (!(dest || mask) && ((flags & RTF_GATEWAY) || !gate) ) {
295                         gw_info.count++;
296                         SAVE_SET_STRING(gw_info.iface, iface)
297                         ina.s_addr = gate;
298                         SAVE_SET_STRING(gw_info.ip, inet_ntoa(ina))
299                 }
300         }
301         fclose(fp);
302         return;
303 }
304
305 void free_gateway_info(void)
306 {
307         if (gw_info.iface)
308                 free(gw_info.iface);
309         if (gw_info.ip)
310                 free(gw_info.ip);
311         memset(&gw_info, 0, sizeof(gw_info));
312 }
313
314 int gateway_exists(void)
315 {
316         return !!gw_info.count;
317 }
318
319 void print_gateway_iface(char *p, int p_max_size)
320 {
321         snprintf(p, p_max_size, "%s", gw_info.iface);
322 }
323
324 void print_gateway_ip(char *p, int p_max_size)
325 {
326         snprintf(p, p_max_size, "%s", gw_info.ip);
327 }
328
329 void update_net_stats(void)
330 {
331         FILE *net_dev_fp;
332         static int rep = 0;
333         static char first = 1;
334
335         // FIXME: arbitrary size chosen to keep code simple.
336         int i, i2;
337         unsigned int curtmp1, curtmp2;
338         unsigned int k;
339         struct ifconf conf;
340         char buf[256];
341         double delta;
342
343 #ifdef HAVE_IWLIB
344         // wireless info variables
345         int skfd, has_bitrate = 0;
346         struct wireless_info *winfo;
347         struct iwreq wrq;
348 #endif
349
350         /* get delta */
351         delta = current_update_time - last_update_time;
352         if (delta <= 0.0001) {
353                 return;
354         }
355
356         /* open file and ignore first two lines */
357         if (!(net_dev_fp = open_file("/proc/net/dev", &rep))) {
358                 clear_net_stats();
359                 return;
360         }
361
362         fgets(buf, 255, net_dev_fp);    /* garbage */
363         fgets(buf, 255, net_dev_fp);    /* garbage (field names) */
364
365         /* read each interface */
366         for (i2 = 0; i2 < MAX_NET_INTERFACES; i2++) {
367                 struct net_stat *ns;
368                 char *s, *p;
369                 char temp_addr[18];
370                 long long r, t, last_recv, last_trans;
371
372                 if (fgets(buf, 255, net_dev_fp) == NULL) {
373                         break;
374                 }
375                 p = buf;
376                 while (isspace((int) *p)) {
377                         p++;
378                 }
379
380                 s = p;
381
382                 while (*p && *p != ':') {
383                         p++;
384                 }
385                 if (*p == '\0') {
386                         continue;
387                 }
388                 *p = '\0';
389                 p++;
390
391                 ns = get_net_stat(s, NULL, NULL);
392                 ns->up = 1;
393                 memset(&(ns->addr.sa_data), 0, 14);
394
395                 memset(ns->addrs, 0, 17 * MAX_NET_INTERFACES + 1); /* Up to 17 chars per ip, max MAX_NET_INTERFACES interfaces. Nasty memory usage... */
396
397                 last_recv = ns->recv;
398                 last_trans = ns->trans;
399
400                 /* bytes packets errs drop fifo frame compressed multicast|bytes ... */
401                 sscanf(p, "%lld  %*d     %*d  %*d  %*d  %*d   %*d        %*d       %lld",
402                         &r, &t);
403
404                 /* if recv or trans is less than last time, an overflow happened */
405                 if (r < ns->last_read_recv) {
406                         last_recv = 0;
407                 } else {
408                         ns->recv += (r - ns->last_read_recv);
409                 }
410                 ns->last_read_recv = r;
411
412                 if (t < ns->last_read_trans) {
413                         last_trans = 0;
414                 } else {
415                         ns->trans += (t - ns->last_read_trans);
416                 }
417                 ns->last_read_trans = t;
418
419                 /*** ip addr patch ***/
420                 i = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
421
422                 conf.ifc_buf = malloc(sizeof(struct ifreq) * MAX_NET_INTERFACES);
423                 conf.ifc_len = sizeof(struct ifreq) * MAX_NET_INTERFACES;
424                 memset(conf.ifc_buf, 0, conf.ifc_len);
425
426                 ioctl((long) i, SIOCGIFCONF, &conf);
427
428                 for (k = 0; k < conf.ifc_len / sizeof(struct ifreq); k++) {
429                         struct net_stat *ns2;
430
431                         if (!(((struct ifreq *) conf.ifc_buf) + k))
432                                 break;
433
434                         ns2 = get_net_stat(
435                                         ((struct ifreq *) conf.ifc_buf)[k].ifr_ifrn.ifrn_name, NULL, NULL);
436                         ns2->addr = ((struct ifreq *) conf.ifc_buf)[k].ifr_ifru.ifru_addr;
437                         sprintf(temp_addr, "%u.%u.%u.%u, ",
438                                         ns2->addr.sa_data[2] & 255,
439                                         ns2->addr.sa_data[3] & 255,
440                                         ns2->addr.sa_data[4] & 255,
441                                         ns2->addr.sa_data[5] & 255);
442                         if(NULL == strstr(ns2->addrs, temp_addr))
443                                 strncpy(ns2->addrs + strlen(ns2->addrs), temp_addr, 17);
444                 }
445
446                 close((long) i);
447
448                 free(conf.ifc_buf);
449
450                 /*** end ip addr patch ***/
451
452                 if (!first) {
453                         /* calculate speeds */
454                         ns->net_rec[0] = (ns->recv - last_recv) / delta;
455                         ns->net_trans[0] = (ns->trans - last_trans) / delta;
456                 }
457
458                 curtmp1 = 0;
459                 curtmp2 = 0;
460                 // get an average
461 #ifdef HAVE_OPENMP
462 #pragma omp parallel for reduction(+:curtmp1, curtmp2) schedule(dynamic,10)
463 #endif /* HAVE_OPENMP */
464                 for (i = 0; i < info.net_avg_samples; i++) {
465                         curtmp1 = curtmp1 + ns->net_rec[i];
466                         curtmp2 = curtmp2 + ns->net_trans[i];
467                 }
468                 ns->recv_speed = curtmp1 / (double) info.net_avg_samples;
469                 ns->trans_speed = curtmp2 / (double) info.net_avg_samples;
470                 if (info.net_avg_samples > 1) {
471 #ifdef HAVE_OPENMP
472 #pragma omp parallel for schedule(dynamic,10)
473 #endif /* HAVE_OPENMP */
474                         for (i = info.net_avg_samples; i > 1; i--) {
475                                 ns->net_rec[i - 1] = ns->net_rec[i - 2];
476                                 ns->net_trans[i - 1] = ns->net_trans[i - 2];
477                         }
478                 }
479
480 #ifdef HAVE_IWLIB
481                 /* update wireless info */
482                 winfo = malloc(sizeof(struct wireless_info));
483                 memset(winfo, 0, sizeof(struct wireless_info));
484
485                 skfd = iw_sockets_open();
486                 if (iw_get_basic_config(skfd, s, &(winfo->b)) > -1) {
487
488                         // set present winfo variables
489                         if (iw_get_stats(skfd, s, &(winfo->stats),
490                                         &winfo->range, winfo->has_range) >= 0) {
491                                 winfo->has_stats = 1;
492                         }
493                         if (iw_get_range_info(skfd, s, &(winfo->range)) >= 0) {
494                                 winfo->has_range = 1;
495                         }
496                         if (iw_get_ext(skfd, s, SIOCGIWAP, &wrq) >= 0) {
497                                 winfo->has_ap_addr = 1;
498                                 memcpy(&(winfo->ap_addr), &(wrq.u.ap_addr), sizeof(sockaddr));
499                         }
500
501                         // get bitrate
502                         if (iw_get_ext(skfd, s, SIOCGIWRATE, &wrq) >= 0) {
503                                 memcpy(&(winfo->bitrate), &(wrq.u.bitrate), sizeof(iwparam));
504                                 iw_print_bitrate(ns->bitrate, 16, winfo->bitrate.value);
505                                 has_bitrate = 1;
506                         }
507
508                         // get link quality
509                         if (winfo->has_range && winfo->has_stats
510                                         && ((winfo->stats.qual.level != 0)
511                                         || (winfo->stats.qual.updated & IW_QUAL_DBM))) {
512                                 if (!(winfo->stats.qual.updated & IW_QUAL_QUAL_INVALID)) {
513                                         ns->link_qual = winfo->stats.qual.qual;
514                                         ns->link_qual_max = winfo->range.max_qual.qual;
515                                 }
516                         }
517
518                         // get ap mac
519                         if (winfo->has_ap_addr) {
520                                 iw_sawap_ntop(&winfo->ap_addr, ns->ap);
521                         }
522
523                         // get essid
524                         if (winfo->b.has_essid) {
525                                 if (winfo->b.essid_on) {
526                                         snprintf(ns->essid, 32, "%s", winfo->b.essid);
527                                 } else {
528                                         snprintf(ns->essid, 32, "off/any");
529                                 }
530                         }
531
532                         snprintf(ns->mode, 16, "%s", iw_operation_mode[winfo->b.mode]);
533                 }
534                 iw_sockets_close(skfd);
535                 free(winfo);
536 #endif
537         }
538         first = 0;
539
540         fclose(net_dev_fp);
541 }
542
543 int result;
544
545 void update_total_processes(void)
546 {
547         DIR *dir;
548         struct dirent *entry;
549         int ignore1;
550         char ignore2;
551
552         info.procs = 0;
553         if (!(dir = opendir("/proc"))) {
554                 return;
555         }
556         while ((entry = readdir(dir))) {
557                 if (!entry) {
558                         /* Problem reading list of processes */
559                         closedir(dir);
560                         info.procs = 0;
561                         return;
562                 }
563                 if (sscanf(entry->d_name, "%d%c", &ignore1, &ignore2) == 1) {
564                         info.procs++;
565                 }
566         }
567         closedir(dir);
568 }
569
570 void update_threads(void)
571 {
572 #ifdef HAVE_SYSINFO
573         if (!prefer_proc) {
574                 struct sysinfo s_info;
575
576                 sysinfo(&s_info);
577                 info.threads = s_info.procs;
578         } else
579 #endif
580         {
581                 static int rep = 0;
582                 FILE *fp;
583
584                 if (!(fp = open_file("/proc/loadavg", &rep))) {
585                         info.threads = 0;
586                         return;
587                 }
588                 fscanf(fp, "%*f %*f %*f %*d/%hu", &info.threads);
589                 fclose(fp);
590         }
591 }
592
593 #define CPU_SAMPLE_COUNT 15
594 struct cpu_info {
595         unsigned long long cpu_user;
596         unsigned long long cpu_system;
597         unsigned long long cpu_nice;
598         unsigned long long cpu_idle;
599         unsigned long long cpu_iowait;
600         unsigned long long cpu_irq;
601         unsigned long long cpu_softirq;
602         unsigned long long cpu_steal;
603         unsigned long long cpu_total;
604         unsigned long long cpu_active_total;
605         unsigned long long cpu_last_total;
606         unsigned long long cpu_last_active_total;
607         double cpu_val[CPU_SAMPLE_COUNT];
608 };
609 static short cpu_setup = 0;
610
611 /* Determine if this kernel gives us "extended" statistics information in
612  * /proc/stat.
613  * Kernels around 2.5 and earlier only reported user, system, nice, and
614  * idle values in proc stat.
615  * Kernels around 2.6 and greater report these PLUS iowait, irq, softirq,
616  * and steal */
617 void determine_longstat(char *buf)
618 {
619         unsigned long long iowait = 0;
620
621         KFLAG_SETOFF(KFLAG_IS_LONGSTAT);
622         /* scanf will either return -1 or 1 because there is only 1 assignment */
623         if (sscanf(buf, "%*s %*d %*d %*d %*d %llu", &iowait) > 0) {
624                 KFLAG_SETON(KFLAG_IS_LONGSTAT);
625         }
626 }
627
628 void get_cpu_count(void)
629 {
630         FILE *stat_fp;
631         static int rep = 0;
632         char buf[256];
633
634         if (info.cpu_usage) {
635                 return;
636         }
637
638         if (!(stat_fp = open_file("/proc/stat", &rep))) {
639                 return;
640         }
641
642         info.cpu_count = 0;
643
644         while (!feof(stat_fp)) {
645                 if (fgets(buf, 255, stat_fp) == NULL) {
646                         break;
647                 }
648
649                 if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3])) {
650                         if (info.cpu_count == 0) {
651                                 determine_longstat(buf);
652                         }
653                         info.cpu_count++;
654                 }
655         }
656         info.cpu_usage = malloc((info.cpu_count + 1) * sizeof(float));
657
658         fclose(stat_fp);
659 }
660
661 #define TMPL_LONGSTAT "%*s %llu %llu %llu %llu %llu %llu %llu %llu"
662 #define TMPL_SHORTSTAT "%*s %llu %llu %llu %llu"
663
664 void update_stat(void)
665 {
666         FILE *stat_fp;
667         static int rep = 0;
668         static struct cpu_info *cpu = NULL;
669         char buf[256];
670         int i;
671         unsigned int idx;
672         double curtmp;
673         const char *stat_template = NULL;
674         unsigned int malloc_cpu_size = 0;
675         extern void* global_cpu;
676
677         static pthread_mutex_t last_stat_update_mutex = PTHREAD_MUTEX_INITIALIZER;
678         static double last_stat_update = 0.0;
679
680         /* since we use wrappers for this function, the update machinery
681          * can't eliminate double invocations of this function. Check for
682          * them here, otherwise cpu_usage counters are freaking out. */
683         pthread_mutex_lock(&last_stat_update_mutex);
684         if (last_stat_update == current_update_time) {
685                 pthread_mutex_unlock(&last_stat_update_mutex);
686                 return;
687         }
688         last_stat_update = current_update_time;
689         pthread_mutex_unlock(&last_stat_update_mutex);
690
691         /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
692         if (!cpu_setup || !info.cpu_usage) {
693                 get_cpu_count();
694                 cpu_setup = 1;
695         }
696
697         if (!stat_template) {
698                 stat_template =
699                         KFLAG_ISSET(KFLAG_IS_LONGSTAT) ? TMPL_LONGSTAT : TMPL_SHORTSTAT;
700         }
701
702         if (!global_cpu) {
703                 malloc_cpu_size = (info.cpu_count + 1) * sizeof(struct cpu_info);
704                 cpu = malloc(malloc_cpu_size);
705                 memset(cpu, 0, malloc_cpu_size);
706                 global_cpu = cpu;
707         }
708
709         if (!(stat_fp = open_file("/proc/stat", &rep))) {
710                 info.run_threads = 0;
711                 if (info.cpu_usage) {
712                         memset(info.cpu_usage, 0, info.cpu_count * sizeof(float));
713                 }
714                 return;
715         }
716
717         idx = 0;
718         while (!feof(stat_fp)) {
719                 if (fgets(buf, 255, stat_fp) == NULL) {
720                         break;
721                 }
722
723                 if (strncmp(buf, "procs_running ", 14) == 0) {
724                         sscanf(buf, "%*s %hu", &info.run_threads);
725                 } else if (strncmp(buf, "cpu", 3) == 0) {
726                         double delta;
727                         if (isdigit(buf[3])) {
728                                 idx = atoi(&buf[3]) + 1;
729                         } else {
730                                 idx = 0;
731                         }
732                         sscanf(buf, stat_template, &(cpu[idx].cpu_user),
733                                 &(cpu[idx].cpu_nice), &(cpu[idx].cpu_system),
734                                 &(cpu[idx].cpu_idle), &(cpu[idx].cpu_iowait),
735                                 &(cpu[idx].cpu_irq), &(cpu[idx].cpu_softirq),
736                                 &(cpu[idx].cpu_steal));
737
738                         cpu[idx].cpu_total = cpu[idx].cpu_user + cpu[idx].cpu_nice +
739                                 cpu[idx].cpu_system + cpu[idx].cpu_idle +
740                                 cpu[idx].cpu_iowait + cpu[idx].cpu_irq +
741                                 cpu[idx].cpu_softirq + cpu[idx].cpu_steal;
742
743                         cpu[idx].cpu_active_total = cpu[idx].cpu_total -
744                                 (cpu[idx].cpu_idle + cpu[idx].cpu_iowait);
745
746                         delta = current_update_time - last_update_time;
747
748                         if (delta <= 0.001) {
749                                 break;
750                         }
751
752                         cpu[idx].cpu_val[0] = (cpu[idx].cpu_active_total -
753                                 cpu[idx].cpu_last_active_total) /
754                                 (float) (cpu[idx].cpu_total - cpu[idx].cpu_last_total);
755                         curtmp = 0;
756 #ifdef HAVE_OPENMP
757 #pragma omp parallel for reduction(+:curtmp) schedule(dynamic,10)
758 #endif /* HAVE_OPENMP */
759                         for (i = 0; i < info.cpu_avg_samples; i++) {
760                                 curtmp = curtmp + cpu[idx].cpu_val[i];
761                         }
762                         /* TESTING -- I've removed this, because I don't think it is right.
763                          * You shouldn't divide by the cpu count here ...
764                          * removing for testing */
765                         /* if (idx == 0) {
766                                 info.cpu_usage[idx] = curtmp / info.cpu_avg_samples /
767                                         info.cpu_count;
768                         } else {
769                                 info.cpu_usage[idx] = curtmp / info.cpu_avg_samples;
770                         } */
771                         /* TESTING -- this line replaces the prev. "suspect" if/else */
772                         info.cpu_usage[idx] = curtmp / info.cpu_avg_samples;
773
774                         cpu[idx].cpu_last_total = cpu[idx].cpu_total;
775                         cpu[idx].cpu_last_active_total = cpu[idx].cpu_active_total;
776 #ifdef HAVE_OPENMP
777 #pragma omp parallel for schedule(dynamic,10)
778 #endif /* HAVE_OPENMP */
779                         for (i = info.cpu_avg_samples - 1; i > 0; i--) {
780                                 cpu[idx].cpu_val[i] = cpu[idx].cpu_val[i - 1];
781                         }
782                 }
783         }
784         fclose(stat_fp);
785 }
786
787 void update_running_processes(void)
788 {
789         update_stat();
790 }
791
792 void update_cpu_usage(void)
793 {
794         update_stat();
795 }
796
797 void update_load_average(void)
798 {
799 #ifdef HAVE_GETLOADAVG
800         if (!prefer_proc) {
801                 double v[3];
802
803                 getloadavg(v, 3);
804                 info.loadavg[0] = (float) v[0];
805                 info.loadavg[1] = (float) v[1];
806                 info.loadavg[2] = (float) v[2];
807         } else
808 #endif
809         {
810                 static int rep = 0;
811                 FILE *fp;
812
813                 if (!(fp = open_file("/proc/loadavg", &rep))) {
814                         info.loadavg[0] = info.loadavg[1] = info.loadavg[2] = 0.0;
815                         return;
816                 }
817                 fscanf(fp, "%f %f %f", &info.loadavg[0], &info.loadavg[1],
818                         &info.loadavg[2]);
819                 fclose(fp);
820         }
821 }
822
823 /***********************************************************/
824 /***********************************************************/
825 /***********************************************************/
826
827 static int no_dots(const struct dirent *d)
828 {
829         if (d->d_name[0] == '.') {
830                 return 0;
831         }
832         return 1;
833 }
834
835 static int get_first_file_in_a_directory(const char *dir, char *s, int *rep)
836 {
837         struct dirent **namelist;
838         int i, n;
839
840         n = scandir(dir, &namelist, no_dots, alphasort);
841         if (n < 0) {
842                 if (!rep || !*rep) {
843                         NORM_ERR("scandir for %s: %s", dir, strerror(errno));
844                         if (rep) {
845                                 *rep = 1;
846                         }
847                 }
848                 return 0;
849         } else {
850                 if (n == 0) {
851                         return 0;
852                 }
853
854                 strncpy(s, namelist[0]->d_name, 255);
855                 s[255] = '\0';
856
857 #ifdef HAVE_OPENMP
858 #pragma omp parallel for schedule(dynamic,10)
859 #endif /* HAVE_OPENMP */
860                 for (i = 0; i < n; i++) {
861                         free(namelist[i]);
862                 }
863                 free(namelist);
864
865                 return 1;
866         }
867 }
868
869 static int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
870                 int *divisor, char *devtype)
871 {
872         char path[256];
873         char buf[256];
874         int fd;
875         int divfd;
876
877         memset(buf, 0, sizeof(buf));
878
879         /* if device is NULL or *, get first */
880         if (dev == NULL || strcmp(dev, "*") == 0) {
881                 static int rep = 0;
882
883                 if (!get_first_file_in_a_directory(dir, buf, &rep)) {
884                         return -1;
885                 }
886                 dev = buf;
887         }
888
889         if (strcmp(dir, "/sys/class/hwmon/") == 0) {
890                 if (*buf) {
891                         /* buf holds result from get_first_file_in_a_directory() above,
892                          * e.g. "hwmon0" -- append "/device" */
893                         strcat(buf, "/device");
894                 } else {
895                         /* dev holds device number N as a string,
896                          * e.g. "0", -- convert to "hwmon0/device" */
897                         sprintf(buf, "hwmon%s/device", dev);
898                         dev = buf;
899                 }
900         }
901
902         /* change vol to in, tempf to temp */
903         if (strcmp(type, "vol") == 0) {
904                 type = "in";
905         } else if (strcmp(type, "tempf") == 0) {
906                 type = "temp";
907         }
908
909         /* construct path */
910         snprintf(path, 255, "%s%s/%s%d_input", dir, dev, type, n);
911
912         /* first, attempt to open file in /device */
913         fd = open(path, O_RDONLY);
914         if (fd < 0) {
915
916                 /* if it fails, strip the /device from dev and attempt again */
917                 buf[strlen(buf) - 7] = 0;
918                 snprintf(path, 255, "%s%s/%s%d_input", dir, dev, type, n);
919                 fd = open(path, O_RDONLY);
920                 if (fd < 0) {
921                         CRIT_ERR(NULL, NULL, "can't open '%s': %s\nplease check your device or remove this "
922                                          "var from "PACKAGE_NAME, path, strerror(errno));
923                 }
924         }
925
926         strncpy(devtype, path, 255);
927
928         if (strcmp(type, "in") == 0 || strcmp(type, "temp") == 0
929                         || strcmp(type, "tempf") == 0) {
930                 *divisor = 1;
931         } else {
932                 *divisor = 0;
933         }
934         /* fan does not use *_div as a read divisor */
935         if (strcmp("fan", type) == 0) {
936                 return fd;
937         }
938
939         /* test if *_div file exist, open it and use it as divisor */
940         if (strcmp(type, "tempf") == 0) {
941                 snprintf(path, 255, "%s%s/%s%d_div", dir, "one", "two", n);
942         } else {
943                 snprintf(path, 255, "%s%s/%s%d_div", dir, dev, type, n);
944         }
945
946         divfd = open(path, O_RDONLY);
947         if (divfd > 0) {
948                 /* read integer */
949                 char divbuf[64];
950                 int divn;
951
952                 divn = read(divfd, divbuf, 63);
953                 /* should read until n == 0 but I doubt that kernel will give these
954                  * in multiple pieces. :) */
955                 if (divn < 0) {
956                         NORM_ERR("open_sysfs_sensor(): can't read from sysfs");
957                 } else {
958                         divbuf[divn] = '\0';
959                         *divisor = atoi(divbuf);
960                 }
961                 close(divfd);
962         }
963
964         return fd;
965 }
966
967 static double get_sysfs_info(int *fd, int divisor, char *devtype, char *type)
968 {
969         int val = 0;
970
971         if (*fd <= 0) {
972                 return 0;
973         }
974
975         lseek(*fd, 0, SEEK_SET);
976
977         /* read integer */
978         {
979                 char buf[64];
980                 int n;
981                 n = read(*fd, buf, 63);
982                 /* should read until n == 0 but I doubt that kernel will give these
983                  * in multiple pieces. :) */
984                 if (n < 0) {
985                         NORM_ERR("get_sysfs_info(): read from %s failed\n", devtype);
986                 } else {
987                         buf[n] = '\0';
988                         val = atoi(buf);
989                 }
990         }
991
992         close(*fd);
993         /* open file */
994         *fd = open(devtype, O_RDONLY);
995         if (*fd < 0) {
996                 NORM_ERR("can't open '%s': %s", devtype, strerror(errno));
997         }
998
999         /* My dirty hack for computing CPU value
1000          * Filedil, from forums.gentoo.org */
1001         /* if (strstr(devtype, "temp1_input") != NULL) {
1002                 return -15.096 + 1.4893 * (val / 1000.0);
1003         } */
1004
1005         /* divide voltage and temperature by 1000 */
1006         /* or if any other divisor is given, use that */
1007         if (strcmp(type, "tempf") == 0) {
1008                 if (divisor > 1) {
1009                         return ((val / divisor + 40) * 9.0 / 5) - 40;
1010                 } else if (divisor) {
1011                         return ((val / 1000.0 + 40) * 9.0 / 5) - 40;
1012                 } else {
1013                         return ((val + 40) * 9.0 / 5) - 40;
1014                 }
1015         } else {
1016                 if (divisor > 1) {
1017                         return val / divisor;
1018                 } else if (divisor) {
1019                         return val / 1000.0;
1020                 } else {
1021                         return val;
1022                 }
1023         }
1024 }
1025
1026 #define HWMON_RESET() {\
1027                 buf1[0] = 0; \
1028                 factor = 1.0; \
1029                 offset = 0.0; }
1030
1031 static void parse_sysfs_sensor(struct text_object *obj, const char *arg, const char *path, const char *type)
1032 {
1033         char buf1[64], buf2[64];
1034         float factor, offset;
1035         int n, found = 0;
1036         struct sysfs *sf;
1037
1038         if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
1039         if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
1040         if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
1041         if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
1042
1043         if (!found) {
1044                 NORM_ERR("i2c failed to parse arguments");
1045                 obj->type = OBJ_text;
1046                 return;
1047         }
1048         DBGP("parsed %s args: '%s' '%s' %d %f %f\n", type, buf1, buf2, n, factor, offset);
1049         sf = malloc(sizeof(struct sysfs));
1050         memset(sf, 0, sizeof(struct sysfs));
1051         sf->fd = open_sysfs_sensor(path, (*buf1) ? buf1 : 0, buf2, n,
1052                         &sf->arg, sf->devtype);
1053         strncpy(sf->type, buf2, 63);
1054         sf->factor = factor;
1055         sf->offset = offset;
1056         obj->data.opaque = sf;
1057 }
1058
1059 #define PARSER_GENERATOR(name, path)                                \
1060 void parse_##name##_sensor(struct text_object *obj, const char *arg) \
1061 {                                                                   \
1062         parse_sysfs_sensor(obj, arg, path, #name);           \
1063 }
1064
1065 PARSER_GENERATOR(i2c, "/sys/bus/i2c/devices/")
1066 PARSER_GENERATOR(hwmon, "/sys/class/hwmon/")
1067 PARSER_GENERATOR(platform, "/sys/bus/platform/devices/")
1068
1069 void print_sysfs_sensor(struct text_object *obj, char *p, int p_max_size)
1070 {
1071         double r;
1072         struct sysfs *sf = obj->data.opaque;
1073
1074         if (!sf)
1075                 return;
1076
1077         r = get_sysfs_info(&sf->fd, sf->arg,
1078                         sf->devtype, sf->type);
1079
1080         r = r * sf->factor + sf->offset;
1081
1082         if (!strncmp(sf->type, "temp", 4)) {
1083                 temp_print(p, p_max_size, r, TEMP_CELSIUS);
1084         } else if (r >= 100.0 || r == 0) {
1085                 snprintf(p, p_max_size, "%d", (int) r);
1086         } else {
1087                 snprintf(p, p_max_size, "%.1f", r);
1088         }
1089 }
1090
1091 void free_sysfs_sensor(struct text_object *obj)
1092 {
1093         struct sysfs *sf = obj->data.opaque;
1094
1095         if (!sf)
1096                 return;
1097
1098         close(sf->fd);
1099         free(obj->data.opaque);
1100         obj->data.opaque = NULL;
1101 }
1102
1103 #define CPUFREQ_PREFIX "/sys/devices/system/cpu"
1104 #define CPUFREQ_POSTFIX "cpufreq/scaling_cur_freq"
1105
1106 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
1107 char get_freq(char *p_client_buffer, size_t client_buffer_size,
1108                 const char *p_format, int divisor, unsigned int cpu)
1109 {
1110         FILE *f;
1111         static int rep = 0;
1112         char frequency[32];
1113         char s[256];
1114         double freq = 0;
1115
1116         if (!p_client_buffer || client_buffer_size <= 0 || !p_format
1117                         || divisor <= 0) {
1118                 return 0;
1119         }
1120
1121         if (!prefer_proc) {
1122                 char current_freq_file[128];
1123
1124                 snprintf(current_freq_file, 127, "%s/cpu%d/%s", CPUFREQ_PREFIX, cpu - 1,
1125                         CPUFREQ_POSTFIX);
1126                 f = fopen(current_freq_file, "r");
1127                 if (f) {
1128                         /* if there's a cpufreq /sys node, read the current frequency from
1129                          * this node and divide by 1000 to get Mhz. */
1130                         if (fgets(s, sizeof(s), f)) {
1131                                 s[strlen(s) - 1] = '\0';
1132                                 freq = strtod(s, NULL);
1133                         }
1134                         fclose(f);
1135                         snprintf(p_client_buffer, client_buffer_size, p_format,
1136                                 (freq / 1000) / divisor);
1137                         return 1;
1138                 }
1139         }
1140
1141         // open the CPU information file
1142         f = open_file("/proc/cpuinfo", &rep);
1143         if (!f) {
1144                 perror(PACKAGE_NAME": Failed to access '/proc/cpuinfo' at get_freq()");
1145                 return 0;
1146         }
1147
1148         // read the file
1149         while (fgets(s, sizeof(s), f) != NULL) {
1150
1151 #if defined(__i386) || defined(__x86_64)
1152                 // and search for the cpu mhz
1153                 if (strncmp(s, "cpu MHz", 7) == 0 && cpu == 0) {
1154 #else
1155 #if defined(__alpha)
1156                 // different on alpha
1157                 if (strncmp(s, "cycle frequency [Hz]", 20) == 0 && cpu == 0) {
1158 #else
1159                 // this is different on ppc for some reason
1160                 if (strncmp(s, "clock", 5) == 0 && cpu == 0) {
1161 #endif // defined(__alpha)
1162 #endif // defined(__i386) || defined(__x86_64)
1163
1164                         // copy just the number
1165                         strcpy(frequency, strchr(s, ':') + 2);
1166 #if defined(__alpha)
1167                         // strip " est.\n"
1168                         frequency[strlen(frequency) - 6] = '\0';
1169                         // kernel reports in Hz
1170                         freq = strtod(frequency, NULL) / 1000000;
1171 #else
1172                         // strip \n
1173                         frequency[strlen(frequency) - 1] = '\0';
1174                         freq = strtod(frequency, NULL);
1175 #endif
1176                         break;
1177                 }
1178                 if (strncmp(s, "processor", 9) == 0) {
1179                         cpu--;
1180                         continue;
1181                 }
1182         }
1183
1184         fclose(f);
1185         snprintf(p_client_buffer, client_buffer_size, p_format,
1186                 (float) freq / divisor);
1187         return 1;
1188 }
1189
1190 #define CPUFREQ_VOLTAGE "cpufreq/scaling_voltages"
1191
1192 /* /sys/devices/system/cpu/cpu0/cpufreq/scaling_voltages looks something
1193  * like this:
1194 # frequency voltage
1195 1800000 1340
1196 1600000 1292
1197 1400000 1100
1198 1200000 988
1199 1000000 1116
1200 800000 1004
1201 600000 988
1202  * Peter Tarjan (ptarjan@citromail.hu) */
1203
1204 /* return cpu voltage in mV (use divisor=1) or V (use divisor=1000) */
1205 static char get_voltage(char *p_client_buffer, size_t client_buffer_size,
1206                 const char *p_format, int divisor, unsigned int cpu)
1207 {
1208         FILE *f;
1209         char s[256];
1210         int freq = 0;
1211         int voltage = 0;
1212         char current_freq_file[128];
1213         int freq_comp = 0;
1214
1215         /* build the voltage file name */
1216         cpu--;
1217         snprintf(current_freq_file, 127, "%s/cpu%d/%s", CPUFREQ_PREFIX, cpu,
1218                 CPUFREQ_POSTFIX);
1219
1220         if (!p_client_buffer || client_buffer_size <= 0 || !p_format
1221                         || divisor <= 0) {
1222                 return 0;
1223         }
1224
1225         /* read the current cpu frequency from the /sys node */
1226         f = fopen(current_freq_file, "r");
1227         if (f) {
1228                 if (fgets(s, sizeof(s), f)) {
1229                         s[strlen(s) - 1] = '\0';
1230                         freq = strtod(s, NULL);
1231                 }
1232                 fclose(f);
1233         } else {
1234                 fprintf(stderr, PACKAGE_NAME": Failed to access '%s' at ", current_freq_file);
1235                 perror("get_voltage()");
1236                 if (f) {
1237                         fclose(f);
1238                 }
1239                 return 0;
1240         }
1241
1242         snprintf(current_freq_file, 127, "%s/cpu%d/%s", CPUFREQ_PREFIX, cpu,
1243                 CPUFREQ_VOLTAGE);
1244
1245         /* use the current cpu frequency to find the corresponding voltage */
1246         f = fopen(current_freq_file, "r");
1247
1248         if (f) {
1249                 while (!feof(f)) {
1250                         char line[256];
1251
1252                         if (fgets(line, 255, f) == NULL) {
1253                                 break;
1254                         }
1255                         sscanf(line, "%d %d", &freq_comp, &voltage);
1256                         if (freq_comp == freq) {
1257                                 break;
1258                         }
1259                 }
1260                 fclose(f);
1261         } else {
1262                 fprintf(stderr, PACKAGE_NAME": Failed to access '%s' at ", current_freq_file);
1263                 perror("get_voltage()");
1264                 if (f) {
1265                         fclose(f);
1266                 }
1267                 return 0;
1268         }
1269         snprintf(p_client_buffer, client_buffer_size, p_format,
1270                 (float) voltage / divisor);
1271         return 1;
1272 }
1273
1274 void print_voltage_mv(struct text_object *obj, char *p, int p_max_size)
1275 {
1276         static int ok = 1;
1277         if (ok) {
1278                 ok = get_voltage(p, p_max_size, "%.0f", 1, obj->data.i);
1279         }
1280 }
1281
1282 void print_voltage_v(struct text_object *obj, char *p, int p_max_size)
1283 {
1284         static int ok = 1;
1285         if (ok) {
1286                 ok = get_voltage(p, p_max_size, "%'.3f", 1000, obj->data.i);
1287         }
1288 }
1289
1290 #define ACPI_FAN_DIR "/proc/acpi/fan/"
1291
1292 void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
1293 {
1294         static int rep = 0;
1295         char buf[256];
1296         char buf2[256];
1297         FILE *fp;
1298
1299         if (!p_client_buffer || client_buffer_size <= 0) {
1300                 return;
1301         }
1302
1303         /* yeah, slow... :/ */
1304         if (!get_first_file_in_a_directory(ACPI_FAN_DIR, buf, &rep)) {
1305                 snprintf(p_client_buffer, client_buffer_size, "no fans?");
1306                 return;
1307         }
1308
1309         snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_FAN_DIR, buf);
1310
1311         fp = open_file(buf2, &rep);
1312         if (!fp) {
1313                 snprintf(p_client_buffer, client_buffer_size,
1314                         "can't open fan's state file");
1315                 return;
1316         }
1317         memset(buf, 0, sizeof(buf));
1318         fscanf(fp, "%*s %99s", buf);
1319         fclose(fp);
1320
1321         snprintf(p_client_buffer, client_buffer_size, "%s", buf);
1322 }
1323
1324 #define SYSFS_AC_ADAPTER_DIR "/sys/class/power_supply"
1325 #define ACPI_AC_ADAPTER_DIR "/proc/acpi/ac_adapter/"
1326 /* Linux 2.6.25 onwards ac adapter info is in
1327    /sys/class/power_supply/AC/
1328    On my system I get the following.
1329      /sys/class/power_supply/AC/uevent:
1330      PHYSDEVPATH=/devices/LNXSYSTM:00/device:00/PNP0A08:00/device:01/PNP0C09:00/ACPI0003:00
1331      PHYSDEVBUS=acpi
1332      PHYSDEVDRIVER=ac
1333      POWER_SUPPLY_NAME=AC
1334      POWER_SUPPLY_TYPE=Mains
1335      POWER_SUPPLY_ONLINE=1
1336
1337    Update: it seems the folder name is hardware-dependent. We add an aditional adapter
1338    argument, specifying the folder name.
1339
1340    Update: on some systems it's /sys/class/power_supply/ADP1 instead of /sys/class/power_supply/AC
1341 */
1342
1343 void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size, const char *adapter)
1344 {
1345         static int rep = 0;
1346
1347         char buf[256];
1348         char buf2[256];
1349         struct stat sb;
1350         FILE *fp;
1351
1352         if (!p_client_buffer || client_buffer_size <= 0) {
1353                 return;
1354         }
1355
1356         if(adapter)
1357                 snprintf(buf2, sizeof(buf2), "%s/%s/uevent", SYSFS_AC_ADAPTER_DIR, adapter);
1358         else{
1359                 snprintf(buf2, sizeof(buf2), "%s/AC/uevent", SYSFS_AC_ADAPTER_DIR);
1360                 if(stat(buf2, &sb) == -1) snprintf(buf2, sizeof(buf2), "%s/ADP1/uevent", SYSFS_AC_ADAPTER_DIR);
1361         }
1362         if(stat(buf2, &sb) == 0) fp = open_file(buf2, &rep); else fp = 0;
1363         if (fp) {
1364                 /* sysfs processing */
1365                 while (!feof(fp)) {
1366                         if (fgets(buf, sizeof(buf), fp) == NULL)
1367                                 break;
1368
1369                         if (strncmp(buf, "POWER_SUPPLY_ONLINE=", 20) == 0) {
1370                                 int online = 0;
1371                                 sscanf(buf, "POWER_SUPPLY_ONLINE=%d", &online);
1372                                 snprintf(p_client_buffer, client_buffer_size,
1373                                          "%s-line", (online ? "on" : "off"));
1374                                 break;
1375                         }
1376                 }
1377                 fclose(fp);
1378         } else {
1379                 /* yeah, slow... :/ */
1380                 if (!get_first_file_in_a_directory(ACPI_AC_ADAPTER_DIR, buf, &rep)) {
1381                         snprintf(p_client_buffer, client_buffer_size, "no ac_adapters?");
1382                         return;
1383                 }
1384
1385                 snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_AC_ADAPTER_DIR, buf);
1386
1387                 fp = open_file(buf2, &rep);
1388                 if (!fp) {
1389                         snprintf(p_client_buffer, client_buffer_size,
1390                                  "No ac adapter found.... where is it?");
1391                         return;
1392                 }
1393                 memset(buf, 0, sizeof(buf));
1394                 fscanf(fp, "%*s %99s", buf);
1395                 fclose(fp);
1396
1397                 snprintf(p_client_buffer, client_buffer_size, "%s", buf);
1398         }
1399 }
1400
1401 /*
1402 /proc/acpi/thermal_zone/THRM/cooling_mode
1403 cooling mode:            active
1404 /proc/acpi/thermal_zone/THRM/polling_frequency
1405 <polling disabled>
1406 /proc/acpi/thermal_zone/THRM/state
1407 state:                   ok
1408 /proc/acpi/thermal_zone/THRM/temperature
1409 temperature:             45 C
1410 /proc/acpi/thermal_zone/THRM/trip_points
1411 critical (S5):           73 C
1412 passive:                 73 C: tc1=4 tc2=3 tsp=40 devices=0xcdf6e6c0
1413 */
1414
1415 #define ACPI_THERMAL_DIR "/proc/acpi/thermal_zone/"
1416 #define ACPI_THERMAL_FORMAT "/proc/acpi/thermal_zone/%s/temperature"
1417
1418 int open_acpi_temperature(const char *name)
1419 {
1420         char path[256];
1421         char buf[256];
1422         int fd;
1423
1424         if (name == NULL || strcmp(name, "*") == 0) {
1425                 static int rep = 0;
1426
1427                 if (!get_first_file_in_a_directory(ACPI_THERMAL_DIR, buf, &rep)) {
1428                         return -1;
1429                 }
1430                 name = buf;
1431         }
1432
1433         snprintf(path, 255, ACPI_THERMAL_FORMAT, name);
1434
1435         fd = open(path, O_RDONLY);
1436         if (fd < 0) {
1437                 NORM_ERR("can't open '%s': %s", path, strerror(errno));
1438         }
1439
1440         return fd;
1441 }
1442
1443 static double last_acpi_temp;
1444 static double last_acpi_temp_time;
1445
1446 double get_acpi_temperature(int fd)
1447 {
1448         if (fd <= 0) {
1449                 return 0;
1450         }
1451
1452         /* don't update acpi temperature too often */
1453         if (current_update_time - last_acpi_temp_time < 11.32) {
1454                 return last_acpi_temp;
1455         }
1456         last_acpi_temp_time = current_update_time;
1457
1458         /* seek to beginning */
1459         lseek(fd, 0, SEEK_SET);
1460
1461         /* read */
1462         {
1463                 char buf[256];
1464                 int n;
1465
1466                 n = read(fd, buf, 255);
1467                 if (n < 0) {
1468                         NORM_ERR("can't read fd %d: %s", fd, strerror(errno));
1469                 } else {
1470                         buf[n] = '\0';
1471                         sscanf(buf, "temperature: %lf", &last_acpi_temp);
1472                 }
1473         }
1474
1475         return last_acpi_temp;
1476 }
1477
1478 /*
1479 hipo@lepakko hipo $ cat /proc/acpi/battery/BAT1/info
1480 present:                 yes
1481 design capacity:         4400 mAh
1482 last full capacity:      4064 mAh
1483 battery technology:      rechargeable
1484 design voltage:          14800 mV
1485 design capacity warning: 300 mAh
1486 design capacity low:     200 mAh
1487 capacity granularity 1:  32 mAh
1488 capacity granularity 2:  32 mAh
1489 model number:            02KT
1490 serial number:           16922
1491 battery type:            LION
1492 OEM info:                SANYO
1493 */
1494
1495 /*
1496 hipo@lepakko conky $ cat /proc/acpi/battery/BAT1/state
1497 present:                 yes
1498 capacity state:          ok
1499 charging state:          unknown
1500 present rate:            0 mA
1501 remaining capacity:      4064 mAh
1502 present voltage:         16608 mV
1503 */
1504
1505 /*
1506 2213<@jupet�kellari��> jupet@lagi-unstable:~$ cat /proc/apm
1507 2213<@jupet�kellari��> 1.16 1.2 0x03 0x01 0xff 0x10 -1% -1 ?
1508 2213<@jupet�kellari��> (-1 ollee ei akkua kiinni, koska akku on p�yd�ll�)
1509 2214<@jupet�kellari��> jupet@lagi-unstable:~$ cat /proc/apm
1510 2214<@jupet�kellari��> 1.16 1.2 0x03 0x01 0x03 0x09 98% -1 ?
1511
1512 2238<@jupet�kellari��> 1.16 1.2 0x03 0x00 0x00 0x01 100% -1 ? ilman verkkovirtaa
1513 2239<@jupet�kellari��> 1.16 1.2 0x03 0x01 0x00 0x01 99% -1 ? verkkovirralla
1514
1515 2240<@jupet�kellari��> 1.16 1.2 0x03 0x01 0x03 0x09 100% -1 ? verkkovirralla ja monitori p��ll�
1516 2241<@jupet�kellari��> 1.16 1.2 0x03 0x00 0x00 0x01 99% -1 ? monitori p��ll� mutta ilman verkkovirtaa
1517 */
1518
1519 /* Kapil Hari Paranjape <kapil@imsc.res.in>
1520   Linux 2.6.24 onwards battery info is in
1521   /sys/class/power_supply/BAT0/
1522   On my system I get the following.
1523         /sys/class/power_supply/BAT0/uevent:
1524         PHYSDEVPATH=/devices/LNXSYSTM:00/device:00/PNP0A03:00/device:01/PNP0C09:00/PNP0C0A:00
1525         PHYSDEVBUS=acpi
1526         PHYSDEVDRIVER=battery
1527         POWER_SUPPLY_NAME=BAT0
1528         POWER_SUPPLY_TYPE=Battery
1529         POWER_SUPPLY_STATUS=Discharging
1530         POWER_SUPPLY_PRESENT=1
1531         POWER_SUPPLY_TECHNOLOGY=Li-ion
1532         POWER_SUPPLY_VOLTAGE_MIN_DESIGN=10800000
1533         POWER_SUPPLY_VOLTAGE_NOW=10780000
1534         POWER_SUPPLY_CURRENT_NOW=13970000
1535         POWER_SUPPLY_ENERGY_FULL_DESIGN=47510000
1536         POWER_SUPPLY_ENERGY_FULL=27370000
1537         POWER_SUPPLY_ENERGY_NOW=11810000
1538         POWER_SUPPLY_MODEL_NAME=IBM-92P1060
1539         POWER_SUPPLY_MANUFACTURER=Panasonic
1540   On some systems POWER_SUPPLY_ENERGY_* is replaced by POWER_SUPPLY_CHARGE_*
1541 */
1542
1543 #define SYSFS_BATTERY_BASE_PATH "/sys/class/power_supply"
1544 #define ACPI_BATTERY_BASE_PATH "/proc/acpi/battery"
1545 #define APM_PATH "/proc/apm"
1546 #define MAX_BATTERY_COUNT 4
1547
1548 static FILE *sysfs_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
1549 static FILE *acpi_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
1550 static FILE *apm_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
1551
1552 static int batteries_initialized = 0;
1553 static char batteries[MAX_BATTERY_COUNT][32];
1554
1555 static int acpi_last_full[MAX_BATTERY_COUNT];
1556 static int acpi_design_capacity[MAX_BATTERY_COUNT];
1557
1558 /* e.g. "charging 75%" */
1559 static char last_battery_str[MAX_BATTERY_COUNT][64];
1560 /* e.g. "3h 15m" */
1561 static char last_battery_time_str[MAX_BATTERY_COUNT][64];
1562
1563 static double last_battery_time[MAX_BATTERY_COUNT];
1564
1565 static int last_battery_perct[MAX_BATTERY_COUNT];
1566 static double last_battery_perct_time[MAX_BATTERY_COUNT];
1567
1568 void init_batteries(void)
1569 {
1570         int idx;
1571
1572         if (batteries_initialized) {
1573                 return;
1574         }
1575 #ifdef HAVE_OPENMP
1576 #pragma omp parallel for schedule(dynamic,10)
1577 #endif /* HAVE_OPENMP */
1578         for (idx = 0; idx < MAX_BATTERY_COUNT; idx++) {
1579                 batteries[idx][0] = '\0';
1580         }
1581         batteries_initialized = 1;
1582 }
1583
1584 int get_battery_idx(const char *bat)
1585 {
1586         int idx;
1587
1588         for (idx = 0; idx < MAX_BATTERY_COUNT; idx++) {
1589                 if (!strlen(batteries[idx]) || !strcmp(batteries[idx], bat)) {
1590                         break;
1591                 }
1592         }
1593
1594         /* if not found, enter a new entry */
1595         if (!strlen(batteries[idx])) {
1596                 snprintf(batteries[idx], 31, "%s", bat);
1597         }
1598
1599         return idx;
1600 }
1601
1602 void set_return_value(char *buffer, unsigned int n, int item, int idx);
1603
1604 void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item)
1605 {
1606         static int idx, rep = 0, rep1 = 0, rep2 = 0;
1607         char acpi_path[128];
1608         char sysfs_path[128];
1609
1610         snprintf(acpi_path, 127, ACPI_BATTERY_BASE_PATH "/%s/state", bat);
1611         snprintf(sysfs_path, 127, SYSFS_BATTERY_BASE_PATH "/%s/uevent", bat);
1612
1613         init_batteries();
1614
1615         idx = get_battery_idx(bat);
1616
1617         /* don't update battery too often */
1618         if (current_update_time - last_battery_time[idx] < 29.5) {
1619                 set_return_value(buffer, n, item, idx);
1620                 return;
1621         }
1622
1623         last_battery_time[idx] = current_update_time;
1624
1625         memset(last_battery_str[idx], 0, sizeof(last_battery_str[idx]));
1626         memset(last_battery_time_str[idx], 0, sizeof(last_battery_time_str[idx]));
1627
1628         /* first try SYSFS if that fails try ACPI */
1629
1630         if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
1631                 sysfs_bat_fp[idx] = open_file(sysfs_path, &rep);
1632         }
1633
1634         if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
1635                 acpi_bat_fp[idx] = open_file(acpi_path, &rep1);
1636         }
1637
1638         if (sysfs_bat_fp[idx] != NULL) {
1639                 /* SYSFS */
1640                 int present_rate = -1;
1641                 int remaining_capacity = -1;
1642                 char charging_state[64];
1643                 char present[4];
1644
1645                 strcpy(charging_state, "unknown");
1646
1647                 while (!feof(sysfs_bat_fp[idx])) {
1648                         char buf[256];
1649                         if (fgets(buf, 256, sysfs_bat_fp[idx]) == NULL)
1650                                 break;
1651
1652                         /* let's just hope units are ok */
1653                         if (strncmp (buf, "POWER_SUPPLY_PRESENT=1", 22) == 0)
1654                                 strcpy(present, "yes");
1655                         else if (strncmp (buf, "POWER_SUPPLY_PRESENT=0", 22) == 0)
1656                                 strcpy(present, "no");
1657                         else if (strncmp (buf, "POWER_SUPPLY_STATUS=", 20) == 0)
1658                                 sscanf(buf, "POWER_SUPPLY_STATUS=%63s", charging_state);
1659                         /* present_rate is not the same as the
1660                         current flowing now but it is the same value
1661                         which was used in the past. so we continue
1662                         the tradition! */
1663                         else if (strncmp(buf, "POWER_SUPPLY_CURRENT_NOW=", 25) == 0)
1664                                 sscanf(buf, "POWER_SUPPLY_CURRENT_NOW=%d", &present_rate);
1665                         else if (strncmp(buf, "POWER_SUPPLY_ENERGY_NOW=", 24) == 0)
1666                                 sscanf(buf, "POWER_SUPPLY_ENERGY_NOW=%d", &remaining_capacity);
1667                         else if (strncmp(buf, "POWER_SUPPLY_ENERGY_FULL=", 25) == 0)
1668                                 sscanf(buf, "POWER_SUPPLY_ENERGY_FULL=%d", &acpi_last_full[idx]);
1669                         else if (strncmp(buf, "POWER_SUPPLY_CHARGE_NOW=", 24) == 0)
1670                                 sscanf(buf, "POWER_SUPPLY_CHARGE_NOW=%d", &remaining_capacity);
1671                         else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=", 25) == 0)
1672                                 sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_last_full[idx]);
1673                 }
1674
1675                 fclose(sysfs_bat_fp[idx]);
1676                 sysfs_bat_fp[idx] = NULL;
1677
1678                 /* Hellf[i]re notes that remaining capacity can exceed acpi_last_full */
1679                 if (remaining_capacity > acpi_last_full[idx])
1680                         acpi_last_full[idx] = remaining_capacity;  /* normalize to 100% */
1681
1682                 /* not present */
1683                 if (strcmp(present, "No") == 0) {
1684                         strncpy(last_battery_str[idx], "not present", 64);
1685                 }
1686                 /* charging */
1687                 else if (strcmp(charging_state, "Charging") == 0) {
1688                         if (acpi_last_full[idx] != 0 && present_rate > 0) {
1689                                 /* e.g. charging 75% */
1690                                 snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %i%%",
1691                                         (int) (((float) remaining_capacity / acpi_last_full[idx]) * 100 ));
1692                                 /* e.g. 2h 37m */
1693                                 format_seconds(last_battery_time_str[idx], sizeof(last_battery_time_str[idx])-1,
1694                                               (long) (((float)(acpi_last_full[idx] - remaining_capacity) / present_rate) * 3600));
1695                         } else if (acpi_last_full[idx] != 0 && present_rate <= 0) {
1696                                 snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %d%%",
1697                                         (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100));
1698                                 snprintf(last_battery_time_str[idx],
1699                                         sizeof(last_battery_time_str[idx]) - 1, "unknown");
1700                         } else {
1701                                 strncpy(last_battery_str[idx], "charging", sizeof(last_battery_str[idx])-1);
1702                                 snprintf(last_battery_time_str[idx],
1703                                         sizeof(last_battery_time_str[idx]) - 1, "unknown");
1704                         }
1705                 }
1706                 /* discharging */
1707                 else if (strncmp(charging_state, "Discharging", 64) == 0) {
1708                         if (present_rate > 0) {
1709                                 /* e.g. discharging 35% */
1710                                 snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "discharging %i%%",
1711                                         (int) (((float) remaining_capacity / acpi_last_full[idx]) * 100 ));
1712                                 /* e.g. 1h 12m */
1713                                 format_seconds(last_battery_time_str[idx], sizeof(last_battery_time_str[idx])-1,
1714                                               (long) (((float) remaining_capacity / present_rate) * 3600));
1715                         } else if (present_rate == 0) { /* Thanks to Nexox for this one */
1716                                 snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "full");
1717                                 snprintf(last_battery_time_str[idx],
1718                                         sizeof(last_battery_time_str[idx]) - 1, "unknown");
1719                         } else {
1720                                 snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1,
1721                                         "discharging %d%%",
1722                                         (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100));
1723                                 snprintf(last_battery_time_str[idx],
1724                                         sizeof(last_battery_time_str[idx]) - 1, "unknown");
1725                         }
1726                 }
1727                 /* charged */
1728                 /* thanks to Lukas Zapletal <lzap@seznam.cz> */
1729                 else if (strncmp(charging_state, "Charged", 64) == 0 || strncmp(charging_state, "Full", 64) == 0) {
1730                                 /* Below happens with the second battery on my X40,
1731                                  * when the second one is empty and the first one
1732                                  * being charged. */
1733                                 if (remaining_capacity == 0)
1734                                         strcpy(last_battery_str[idx], "empty");
1735                                 else
1736                                         strcpy(last_battery_str[idx], "charged");
1737                 }
1738                 /* unknown, probably full / AC */
1739                 else {
1740                         if (acpi_last_full[idx] != 0
1741                             && remaining_capacity != acpi_last_full[idx])
1742                                 snprintf(last_battery_str[idx], 64, "unknown %d%%",
1743                                         (int) (((float)remaining_capacity / acpi_last_full[idx]) * 100));
1744                         else
1745                                 strncpy(last_battery_str[idx], "AC", 64);
1746                 }
1747         } else if (acpi_bat_fp[idx] != NULL) {
1748                 /* ACPI */
1749                 int present_rate = -1;
1750                 int remaining_capacity = -1;
1751                 char charging_state[64];
1752                 char present[4];
1753
1754                 /* read last full capacity if it's zero */
1755                 if (acpi_last_full[idx] == 0) {
1756                         static int rep3 = 0;
1757                         char path[128];
1758                         FILE *fp;
1759
1760                         snprintf(path, 127, ACPI_BATTERY_BASE_PATH "/%s/info", bat);
1761                         fp = open_file(path, &rep3);
1762                         if (fp != NULL) {
1763                                 while (!feof(fp)) {
1764                                         char b[256];
1765
1766                                         if (fgets(b, 256, fp) == NULL) {
1767                                                 break;
1768                                         }
1769                                         if (sscanf(b, "last full capacity: %d",
1770                                                                 &acpi_last_full[idx]) != 0) {
1771                                                 break;
1772                                         }
1773                                 }
1774
1775                                 fclose(fp);
1776                         }
1777                 }
1778
1779                 fseek(acpi_bat_fp[idx], 0, SEEK_SET);
1780
1781                 strcpy(charging_state, "unknown");
1782
1783                 while (!feof(acpi_bat_fp[idx])) {
1784                         char buf[256];
1785
1786                         if (fgets(buf, 256, acpi_bat_fp[idx]) == NULL) {
1787                                 break;
1788                         }
1789
1790                         /* let's just hope units are ok */
1791                         if (strncmp(buf, "present:", 8) == 0) {
1792                                 sscanf(buf, "present: %4s", present);
1793                         } else if (strncmp(buf, "charging state:", 15) == 0) {
1794                                 sscanf(buf, "charging state: %63s", charging_state);
1795                         } else if (strncmp(buf, "present rate:", 13) == 0) {
1796                                 sscanf(buf, "present rate: %d", &present_rate);
1797                         } else if (strncmp(buf, "remaining capacity:", 19) == 0) {
1798                                 sscanf(buf, "remaining capacity: %d", &remaining_capacity);
1799                         }
1800                 }
1801                 /* Hellf[i]re notes that remaining capacity can exceed acpi_last_full */
1802                 if (remaining_capacity > acpi_last_full[idx]) {
1803                         /* normalize to 100% */
1804                         acpi_last_full[idx] = remaining_capacity;
1805                 }
1806
1807                 /* not present */
1808                 if (strcmp(present, "no") == 0) {
1809                         strncpy(last_battery_str[idx], "not present", 64);
1810                         /* charging */
1811                 } else if (strcmp(charging_state, "charging") == 0) {
1812                         if (acpi_last_full[idx] != 0 && present_rate > 0) {
1813                                 /* e.g. charging 75% */
1814                                 snprintf(last_battery_str[idx],
1815                                                 sizeof(last_battery_str[idx]) - 1, "charging %i%%",
1816                                                 (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
1817                                 /* e.g. 2h 37m */
1818                                 format_seconds(last_battery_time_str[idx],
1819                                                 sizeof(last_battery_time_str[idx]) - 1,
1820                                                 (long) (((acpi_last_full[idx] - remaining_capacity) *
1821                                                                 3600) / present_rate));
1822                         } else if (acpi_last_full[idx] != 0 && present_rate <= 0) {
1823                                 snprintf(last_battery_str[idx],
1824                                                 sizeof(last_battery_str[idx]) - 1, "charging %d%%",
1825                                                 (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
1826                                 snprintf(last_battery_time_str[idx],
1827                                                 sizeof(last_battery_time_str[idx]) - 1, "unknown");
1828                         } else {
1829                                 strncpy(last_battery_str[idx], "charging",
1830                                                 sizeof(last_battery_str[idx]) - 1);
1831                                 snprintf(last_battery_time_str[idx],
1832                                                 sizeof(last_battery_time_str[idx]) - 1, "unknown");
1833                         }
1834                         /* discharging */
1835                 } else if (strncmp(charging_state, "discharging", 64) == 0) {
1836                         if (present_rate > 0) {
1837                                 /* e.g. discharging 35% */
1838                                 snprintf(last_battery_str[idx],
1839                                                 sizeof(last_battery_str[idx]) - 1, "discharging %i%%",
1840                                                 (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
1841                                 /* e.g. 1h 12m */
1842                                 format_seconds(last_battery_time_str[idx],
1843                                                 sizeof(last_battery_time_str[idx]) - 1,
1844                                                 (long) ((remaining_capacity * 3600) / present_rate));
1845                         } else if (present_rate == 0) { /* Thanks to Nexox for this one */
1846                                 snprintf(last_battery_str[idx],
1847                                                 sizeof(last_battery_str[idx]) - 1, "full");
1848                                 snprintf(last_battery_time_str[idx],
1849                                                 sizeof(last_battery_time_str[idx]) - 1, "unknown");
1850                         } else {
1851                                 snprintf(last_battery_str[idx],
1852                                                 sizeof(last_battery_str[idx]) - 1, "discharging %d%%",
1853                                                 (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
1854                                 snprintf(last_battery_time_str[idx],
1855                                                 sizeof(last_battery_time_str[idx]) - 1, "unknown");
1856                         }
1857                         /* charged */
1858                 } else if (strncmp(charging_state, "charged", 64) == 0) {
1859                         /* thanks to Lukas Zapletal <lzap@seznam.cz> */
1860                         /* Below happens with the second battery on my X40,
1861                          * when the second one is empty and the first one being charged. */
1862                         if (remaining_capacity == 0) {
1863                                 strcpy(last_battery_str[idx], "empty");
1864                         } else {
1865                                 strcpy(last_battery_str[idx], "charged");
1866                         }
1867                         /* unknown, probably full / AC */
1868                 } else {
1869                         if (strncmp(charging_state, "Full", 64) == 0) {
1870                                 strncpy(last_battery_str[idx], "full", 64);
1871                         } else if (acpi_last_full[idx] != 0
1872                                         && remaining_capacity != acpi_last_full[idx]) {
1873                                 snprintf(last_battery_str[idx], 64, "unknown %d%%",
1874                                                 (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
1875                         } else {
1876                                 strncpy(last_battery_str[idx], "AC", 64);
1877                         }
1878                 }
1879                 fclose(acpi_bat_fp[idx]);
1880                 acpi_bat_fp[idx] = NULL;
1881         } else {
1882                 /* APM */
1883                 if (apm_bat_fp[idx] == NULL) {
1884                         apm_bat_fp[idx] = open_file(APM_PATH, &rep2);
1885                 }
1886
1887                 if (apm_bat_fp[idx] != NULL) {
1888                         unsigned int ac, status, flag;
1889                         int life;
1890
1891                         fscanf(apm_bat_fp[idx], "%*s %*s %*x %x   %x       %x     %d%%",
1892                                 &ac, &status, &flag, &life);
1893
1894                         if (life == -1) {
1895                                 /* could check now that there is ac */
1896                                 snprintf(last_battery_str[idx], 64, "AC");
1897
1898                         /* could check that status == 3 here? */
1899                         } else if (ac && life != 100) {
1900                                 snprintf(last_battery_str[idx], 64, "charging %d%%", life);
1901                         } else {
1902                                 snprintf(last_battery_str[idx], 64, "%d%%", life);
1903                         }
1904
1905                         /* it seemed to buffer it so file must be closed (or could use
1906                          * syscalls directly but I don't feel like coding it now) */
1907                         fclose(apm_bat_fp[idx]);
1908                         apm_bat_fp[idx] = NULL;
1909                 }
1910         }
1911         set_return_value(buffer, n, item, idx);
1912 }
1913
1914 void set_return_value(char *buffer, unsigned int n, int item, int idx)
1915 {
1916         switch (item) {
1917                 case BATTERY_STATUS:
1918                         snprintf(buffer, n, "%s", last_battery_str[idx]);
1919                         break;
1920                 case BATTERY_TIME:
1921                         snprintf(buffer, n, "%s", last_battery_time_str[idx]);
1922                         break;
1923                 default:
1924                         break;
1925         }
1926 }
1927
1928 void get_battery_short_status(char *buffer, unsigned int n, const char *bat)
1929 {
1930         get_battery_stuff(buffer, n, bat, BATTERY_STATUS);
1931         if (0 == strncmp("charging", buffer, 8)) {
1932                 buffer[0] = 'C';
1933                 memmove(buffer + 1, buffer + 8, n - 8);
1934         } else if (0 == strncmp("discharging", buffer, 11)) {
1935                 buffer[0] = 'D';
1936                 memmove(buffer + 1, buffer + 11, n - 11);
1937         } else if (0 == strncmp("charged", buffer, 7)) {
1938                 buffer[0] = 'F';
1939                 memmove(buffer + 1, buffer + 7, n - 7);
1940         } else if (0 == strncmp("not present", buffer, 11)) {
1941                 buffer[0] = 'N';
1942                 memmove(buffer + 1, buffer + 11, n - 11);
1943         } else if (0 == strncmp("empty", buffer, 5)) {
1944                 buffer[0] = 'E';
1945                 memmove(buffer + 1, buffer + 5, n - 5);
1946         } else if (0 != strncmp("AC", buffer, 2)) {
1947                 buffer[0] = 'U';
1948                 memmove(buffer + 1, buffer + 11, n - 11);
1949         }
1950 }
1951
1952 int get_battery_perct(const char *bat)
1953 {
1954         static int rep = 0;
1955         int idx;
1956         char acpi_path[128];
1957         char sysfs_path[128];
1958         int remaining_capacity = -1;
1959
1960         snprintf(acpi_path, 127, ACPI_BATTERY_BASE_PATH "/%s/state", bat);
1961         snprintf(sysfs_path, 127, SYSFS_BATTERY_BASE_PATH "/%s/uevent", bat);
1962
1963         init_batteries();
1964
1965         idx = get_battery_idx(bat);
1966
1967         /* don't update battery too often */
1968         if (current_update_time - last_battery_perct_time[idx] < 30) {
1969                 return last_battery_perct[idx];
1970         }
1971         last_battery_perct_time[idx] = current_update_time;
1972
1973         /* Only check for SYSFS or ACPI */
1974
1975         if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
1976                 sysfs_bat_fp[idx] = open_file(sysfs_path, &rep);
1977                 rep = 0;
1978         }
1979
1980         if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
1981                 acpi_bat_fp[idx] = open_file(acpi_path, &rep);
1982         }
1983
1984         if (sysfs_bat_fp[idx] != NULL) {
1985                 /* SYSFS */
1986                 while (!feof(sysfs_bat_fp[idx])) {
1987                         char buf[256];
1988                         if (fgets(buf, 256, sysfs_bat_fp[idx]) == NULL)
1989                                 break;
1990
1991                         if (strncmp(buf, "POWER_SUPPLY_CHARGE_NOW=", 24) == 0) {
1992                                 sscanf(buf, "POWER_SUPPLY_CHARGE_NOW=%d", &remaining_capacity);
1993                         } else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=",25) == 0) {
1994                                 sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_design_capacity[idx]);
1995                         } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_NOW=", 24) == 0) {
1996                                 sscanf(buf, "POWER_SUPPLY_ENERGY_NOW=%d", &remaining_capacity);
1997                         } else if (strncmp(buf, "POWER_SUPPLY_ENERGY_FULL=",25) == 0) {
1998                                 sscanf(buf, "POWER_SUPPLY_ENERGY_FULL=%d", &acpi_design_capacity[idx]);
1999                         }
2000                 }
2001
2002                 fclose(sysfs_bat_fp[idx]);
2003                 sysfs_bat_fp[idx] = NULL;
2004
2005         } else if (acpi_bat_fp[idx] != NULL) {
2006                 /* ACPI */
2007                 /* read last full capacity if it's zero */
2008                 if (acpi_design_capacity[idx] == 0) {
2009                         static int rep2;
2010                         char path[128];
2011                         FILE *fp;
2012
2013                         snprintf(path, 127, ACPI_BATTERY_BASE_PATH "/%s/info", bat);
2014                         fp = open_file(path, &rep2);
2015                         if (fp != NULL) {
2016                                 while (!feof(fp)) {
2017                                         char b[256];
2018
2019                                         if (fgets(b, 256, fp) == NULL) {
2020                                                 break;
2021                                         }
2022                                         if (sscanf(b, "last full capacity: %d",
2023                                                                 &acpi_design_capacity[idx]) != 0) {
2024                                                 break;
2025                                         }
2026                                 }
2027                                 fclose(fp);
2028                         }
2029                 }
2030
2031                 fseek(acpi_bat_fp[idx], 0, SEEK_SET);
2032
2033                 while (!feof(acpi_bat_fp[idx])) {
2034                         char buf[256];
2035
2036                         if (fgets(buf, 256, acpi_bat_fp[idx]) == NULL) {
2037                                 break;
2038                         }
2039
2040                         if (buf[0] == 'r') {
2041                                 sscanf(buf, "remaining capacity: %d", &remaining_capacity);
2042                         }
2043                 }
2044         }
2045         if (remaining_capacity < 0) {
2046                 return 0;
2047         }
2048         /* compute the battery percentage */
2049         last_battery_perct[idx] =
2050                 (int) (((float) remaining_capacity / acpi_design_capacity[idx]) * 100);
2051         if (last_battery_perct[idx] > 100) last_battery_perct[idx] = 100;
2052         return last_battery_perct[idx];
2053 }
2054
2055 int get_battery_perct_bar(const char *bar)
2056 {
2057         int idx;
2058
2059         get_battery_perct(bar);
2060         idx = get_battery_idx(bar);
2061         return (int) (last_battery_perct[idx] * 2.56 - 1);
2062 }
2063
2064 /* On Apple powerbook and ibook:
2065 $ cat /proc/pmu/battery_0
2066 flags      : 00000013
2067 charge     : 3623
2068 max_charge : 3720
2069 current    : 388
2070 voltage    : 16787
2071 time rem.  : 900
2072 $ cat /proc/pmu/info
2073 PMU driver version     : 2
2074 PMU firmware version   : 0c
2075 AC Power               : 1
2076 Battery count          : 1
2077 */
2078
2079 /* defines as in <linux/pmu.h> */
2080 #define PMU_BATT_PRESENT                0x00000001
2081 #define PMU_BATT_CHARGING               0x00000002
2082
2083 static FILE *pmu_battery_fp;
2084 static FILE *pmu_info_fp;
2085 static char pb_battery_info[3][32];
2086 static double pb_battery_info_update;
2087
2088 #define PMU_PATH "/proc/pmu"
2089 void get_powerbook_batt_info(char *buffer, size_t n, int i)
2090 {
2091         static int rep = 0;
2092         const char *batt_path = PMU_PATH "/battery_0";
2093         const char *info_path = PMU_PATH "/info";
2094         unsigned int flags;
2095         int charge, max_charge, ac = -1;
2096         long timeval = -1;
2097
2098         /* don't update battery too often */
2099         if (current_update_time - pb_battery_info_update < 29.5) {
2100                 snprintf(buffer, n, "%s", pb_battery_info[i]);
2101                 return;
2102         }
2103         pb_battery_info_update = current_update_time;
2104
2105         if (pmu_battery_fp == NULL) {
2106                 pmu_battery_fp = open_file(batt_path, &rep);
2107                 if (pmu_battery_fp == NULL) {
2108                         return;
2109                 }
2110         }
2111
2112         if (pmu_battery_fp != NULL) {
2113                 rewind(pmu_battery_fp);
2114                 while (!feof(pmu_battery_fp)) {
2115                         char buf[32];
2116
2117                         if (fgets(buf, sizeof(buf), pmu_battery_fp) == NULL) {
2118                                 break;
2119                         }
2120
2121                         if (buf[0] == 'f') {
2122                                 sscanf(buf, "flags      : %8x", &flags);
2123                         } else if (buf[0] == 'c' && buf[1] == 'h') {
2124                                 sscanf(buf, "charge     : %d", &charge);
2125                         } else if (buf[0] == 'm') {
2126                                 sscanf(buf, "max_charge : %d", &max_charge);
2127                         } else if (buf[0] == 't') {
2128                                 sscanf(buf, "time rem.  : %ld", &timeval);
2129                         }
2130                 }
2131         }
2132         if (pmu_info_fp == NULL) {
2133                 pmu_info_fp = open_file(info_path, &rep);
2134                 if (pmu_info_fp == NULL) {
2135                         return;
2136                 }
2137         }
2138
2139         if (pmu_info_fp != NULL) {
2140                 rewind(pmu_info_fp);
2141                 while (!feof(pmu_info_fp)) {
2142                         char buf[32];
2143
2144                         if (fgets(buf, sizeof(buf), pmu_info_fp) == NULL) {
2145                                 break;
2146                         }
2147                         if (buf[0] == 'A') {
2148                                 sscanf(buf, "AC Power               : %d", &ac);
2149                         }
2150                 }
2151         }
2152         /* update status string */
2153         if ((ac && !(flags & PMU_BATT_PRESENT))) {
2154                 strncpy(pb_battery_info[PB_BATT_STATUS], "AC", sizeof(pb_battery_info[PB_BATT_STATUS]));
2155         } else if (ac && (flags & PMU_BATT_PRESENT)
2156                         && !(flags & PMU_BATT_CHARGING)) {
2157                 strncpy(pb_battery_info[PB_BATT_STATUS], "charged", sizeof(pb_battery_info[PB_BATT_STATUS]));
2158         } else if ((flags & PMU_BATT_PRESENT) && (flags & PMU_BATT_CHARGING)) {
2159                 strncpy(pb_battery_info[PB_BATT_STATUS], "charging", sizeof(pb_battery_info[PB_BATT_STATUS]));
2160         } else {
2161                 strncpy(pb_battery_info[PB_BATT_STATUS], "discharging", sizeof(pb_battery_info[PB_BATT_STATUS]));
2162         }
2163
2164         /* update percentage string */
2165         if (timeval == 0 && ac && (flags & PMU_BATT_PRESENT)
2166                         && !(flags & PMU_BATT_CHARGING)) {
2167                 snprintf(pb_battery_info[PB_BATT_PERCENT],
2168                         sizeof(pb_battery_info[PB_BATT_PERCENT]), "100%%");
2169         } else if (timeval == 0) {
2170                 snprintf(pb_battery_info[PB_BATT_PERCENT],
2171                         sizeof(pb_battery_info[PB_BATT_PERCENT]), "unknown");
2172         } else {
2173                 snprintf(pb_battery_info[PB_BATT_PERCENT],
2174                         sizeof(pb_battery_info[PB_BATT_PERCENT]), "%d%%",
2175                         (charge * 100) / max_charge);
2176         }
2177
2178         /* update time string */
2179         if (timeval == 0) {                     /* fully charged or battery not present */
2180                 snprintf(pb_battery_info[PB_BATT_TIME],
2181                         sizeof(pb_battery_info[PB_BATT_TIME]), "unknown");
2182         } else if (timeval < 60 * 60) { /* don't show secs */
2183                 format_seconds_short(pb_battery_info[PB_BATT_TIME],
2184                         sizeof(pb_battery_info[PB_BATT_TIME]), timeval);
2185         } else {
2186                 format_seconds(pb_battery_info[PB_BATT_TIME],
2187                         sizeof(pb_battery_info[PB_BATT_TIME]), timeval);
2188         }
2189
2190         snprintf(buffer, n, "%s", pb_battery_info[i]);
2191 }
2192
2193 void update_top(void)
2194 {
2195         process_find_top(info.cpu, info.memu, info.time
2196 #ifdef IOSTATS
2197                 , info.io
2198 #endif
2199                 );
2200         info.first_process = get_first_process();
2201 }
2202
2203 #define ENTROPY_AVAIL_PATH "/proc/sys/kernel/random/entropy_avail"
2204
2205 int get_entropy_avail(unsigned int *val)
2206 {
2207         static int rep = 0;
2208         FILE *fp;
2209
2210         if (!(fp = open_file(ENTROPY_AVAIL_PATH, &rep)))
2211                 return 1;
2212
2213         if (fscanf(fp, "%u", val) != 1)
2214                 return 1;
2215
2216         fclose(fp);
2217         return 0;
2218 }
2219
2220 #define ENTROPY_POOLSIZE_PATH "/proc/sys/kernel/random/poolsize"
2221
2222 int get_entropy_poolsize(unsigned int *val)
2223 {
2224         static int rep = 0;
2225         FILE *fp;
2226
2227         if (!(fp = open_file(ENTROPY_POOLSIZE_PATH, &rep)))
2228                 return 1;
2229
2230         if (fscanf(fp, "%u", val) != 1)
2231                 return 1;
2232
2233         fclose(fp);
2234         return 0;
2235 }
2236
2237 const char *get_disk_protect_queue(const char *disk)
2238 {
2239         FILE *fp;
2240         char path[128];
2241         int state;
2242
2243         snprintf(path, 127, "/sys/block/%s/device/unload_heads", disk);
2244         if (access(path, F_OK)) {
2245                 snprintf(path, 127, "/sys/block/%s/queue/protect", disk);
2246         }
2247         if ((fp = fopen(path, "r")) == NULL)
2248                 return "n/a   ";
2249         if (fscanf(fp, "%d\n", &state) != 1) {
2250                 fclose(fp);
2251                 return "failed";
2252         }
2253         fclose(fp);
2254         return (state > 0) ? "frozen" : "free  ";
2255 }
2256
2257 typedef struct DEV_LIST_TYPE
2258 {
2259         char *dev_name;
2260         int memoized;
2261         struct DEV_LIST_TYPE *next;
2262
2263 } DEV_LIST, *DEV_LIST_PTR;
2264
2265 /* Same as sf #2942117 but memoized using a linked list */
2266 int is_disk(char *dev)
2267 {
2268         char syspath[PATH_MAX];
2269         char *slash;
2270         static DEV_LIST_PTR dev_head = NULL;
2271         DEV_LIST_PTR dev_cur, dev_last;
2272
2273         dev_cur = dev_head;
2274
2275         while (dev_cur) {
2276                 if (strcmp(dev_cur->dev_name, dev) == 0)
2277                         return dev_cur->memoized;
2278                 dev_last = dev_cur;
2279                 dev_cur  = dev_cur->next;
2280         }
2281
2282         dev_cur = (DEV_LIST_PTR)malloc(sizeof(DEV_LIST));
2283         dev_cur->dev_name = (char *)malloc((strlen(dev)+1)*sizeof(char));
2284         strcpy(dev_cur->dev_name,dev);
2285         dev_cur->next = NULL;
2286
2287         while ((slash = strchr(dev, '/')))
2288                 *slash = '!';
2289         snprintf(syspath, sizeof(syspath), "/sys/block/%s", dev);
2290         dev_cur->memoized = !(access(syspath, F_OK));
2291
2292         if (dev_head)
2293                 dev_last->next = dev_cur;
2294         else
2295                 dev_head = dev_cur;
2296
2297         return dev_cur->memoized;
2298 }
2299
2300 void update_diskio(void)
2301 {
2302         FILE *fp;
2303         static int rep = 0;
2304         char buf[512], devbuf[64];
2305         unsigned int major, minor;
2306         int col_count = 0;
2307         struct diskio_stat *cur;
2308         unsigned int reads, writes;
2309         unsigned int total_reads = 0, total_writes = 0;
2310
2311         stats.current = 0;
2312         stats.current_read = 0;
2313         stats.current_write = 0;
2314
2315         if (!(fp = open_file("/proc/diskstats", &rep))) {
2316                 return;
2317         }
2318
2319         /* read reads and writes from all disks (minor = 0), including cd-roms
2320          * and floppies, and sum them up */
2321         while (fgets(buf, 512, fp)) {
2322                 col_count = sscanf(buf, "%u %u %s %*u %*u %u %*u %*u %*u %u", &major,
2323                         &minor, devbuf, &reads, &writes);
2324                 /* ignore subdevices (they have only 3 matching entries in their line)
2325                  * and virtual devices (LVM, network block devices, RAM disks, Loopback)
2326                  *
2327                  * XXX: ignore devices which are part of a SW RAID (MD_MAJOR) */
2328                 if (col_count == 5 && major != LVM_BLK_MAJOR && major != NBD_MAJOR
2329                                 && major != RAMDISK_MAJOR && major != LOOP_MAJOR) {
2330                         /* check needed for kernel >= 2.6.31, see sf #2942117 */
2331                         if (is_disk(devbuf)) {
2332                                 total_reads += reads;
2333                                 total_writes += writes;
2334                         }
2335                 } else {
2336                         col_count = sscanf(buf, "%u %u %s %*u %u %*u %u",
2337                                 &major, &minor, devbuf, &reads, &writes);
2338                         if (col_count != 5) {
2339                                 continue;
2340                         }
2341                 }
2342                 cur = stats.next;
2343                 while (cur && strcmp(devbuf, cur->dev))
2344                         cur = cur->next;
2345
2346                 if (cur)
2347                         update_diskio_values(cur, reads, writes);
2348         }
2349         update_diskio_values(&stats, total_reads, total_writes);
2350         fclose(fp);
2351 }