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