e972938e17c5bc459f2f7bab63fddc3ad6e5534c
[monky] / src / openbsd.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) 2007 Toni Spets
10  * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
11  *      (see AUTHORS)
12  * All rights reserved.
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  *
26  * $Id$ */
27
28 #include <sys/dkstat.h>
29 #include <sys/param.h>
30 #include <sys/resource.h>
31 #include <sys/socket.h>
32 #include <sys/sysctl.h>
33 #include <sys/time.h>
34 #include <sys/types.h>
35 #include <sys/vmmeter.h>
36 #include <sys/user.h>
37 #include <sys/ioctl.h>
38 #include <sys/sensors.h>
39 #include <sys/malloc.h>
40 #include <sys/swap.h>
41 #include <kvm.h>
42
43 #include <net/if.h>
44 #include <net/if_media.h>
45 #include <netinet/in.h>
46
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <ifaddrs.h>
51 #include <limits.h>
52 #include <unistd.h>
53 #include <machine/apmvar.h>
54
55 #include <net80211/ieee80211.h>
56 #include <net80211/ieee80211_ioctl.h>
57
58 #include "conky.h"
59
60 #define MAXSHOWDEVS             16
61
62 #define LOG1024                 10
63 #define pagetok(size) ((size) << pageshift)
64
65 inline void proc_find_top(struct process **cpu, struct process **mem);
66
67 static short cpu_setup = 0;
68 static kvm_t *kd = 0;
69
70 struct ifmibdata *data = NULL;
71 size_t len = 0;
72
73 int init_kvm = 0;
74 int init_sensors = 0;
75
76 static int kvm_init()
77 {
78         if (init_kvm) {
79                 return 1;
80         }
81
82         kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL);
83         if (kd == NULL) {
84                 ERR("error opening kvm");
85         } else {
86                 init_kvm = 1;
87         }
88
89         return 1;
90 }
91
92 /* note: swapmode taken from 'top' source */
93 /* swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
94  * to be based on the new swapctl(2) system call. */
95 static int swapmode(int *used, int *total)
96 {
97         struct swapent *swdev;
98         int nswap, rnswap, i;
99
100         nswap = swapctl(SWAP_NSWAP, 0, 0);
101         if (nswap == 0) {
102                 return 0;
103         }
104
105         swdev = malloc(nswap * sizeof(*swdev));
106         if (swdev == NULL) {
107                 return 0;
108         }
109
110         rnswap = swapctl(SWAP_STATS, swdev, nswap);
111         if (rnswap == -1) {
112                 return 0;
113         }
114
115         /* if rnswap != nswap, then what? */
116
117         /* Total things up */
118         *total = *used = 0;
119         for (i = 0; i < nswap; i++) {
120                 if (swdev[i].se_flags & SWF_ENABLE) {
121                         *used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
122                         *total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
123                 }
124         }
125         free(swdev);
126         return 1;
127 }
128
129 int check_mount(char *s)
130 {
131         /* stub */
132         return 0;
133 }
134
135 void update_uptime()
136 {
137         int mib[2] = { CTL_KERN, KERN_BOOTTIME };
138         struct timeval boottime;
139         time_t now;
140         size_t size = sizeof(boottime);
141
142         if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
143                         && (boottime.tv_sec != 0)) {
144                 time(&now);
145                 info.uptime = now - boottime.tv_sec;
146         } else {
147                 ERR("Could not get uptime");
148                 info.uptime = 0;
149         }
150 }
151
152 void update_meminfo()
153 {
154         static int mib[2] = { CTL_VM, VM_METER };
155         struct vmtotal vmtotal;
156         size_t size;
157         int pagesize, pageshift, swap_avail, swap_used;
158
159         pagesize = getpagesize();
160         pageshift = 0;
161         while (pagesize > 1) {
162                 pageshift++;
163                 pagesize >>= 1;
164         }
165
166         /* we only need the amount of log(2)1024 for our conversion */
167         pageshift -= LOG1024;
168
169         /* get total -- systemwide main memory usage structure */
170         size = sizeof(vmtotal);
171         if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0) {
172                 warn("sysctl failed");
173                 bzero(&vmtotal, sizeof(vmtotal));
174         }
175
176         info.memmax = pagetok(vmtotal.t_rm) + pagetok(vmtotal.t_free);
177         info.mem = pagetok(vmtotal.t_rm);
178
179         if ((swapmode(&swap_used, &swap_avail)) >= 0) {
180                 info.swapmax = swap_avail;
181                 info.swap = swap_used;
182         } else {
183                 info.swapmax = 0;
184                 info.swap = 0;
185         }
186 }
187
188 void update_net_stats()
189 {
190         struct net_stat *ns;
191         double delta;
192         long long r, t, last_recv, last_trans;
193         struct ifaddrs *ifap, *ifa;
194         struct if_data *ifd;
195
196         /* get delta */
197         delta = current_update_time - last_update_time;
198         if (delta <= 0.0001) {
199                 return;
200         }
201
202         if (getifaddrs(&ifap) < 0) {
203                 return;
204         }
205
206         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
207                 ns = get_net_stat((const char *) ifa->ifa_name);
208
209                 if (ifa->ifa_flags & IFF_UP) {
210                         struct ifaddrs *iftmp;
211
212                         ns->up = 1;
213                         last_recv = ns->recv;
214                         last_trans = ns->trans;
215
216                         if (ifa->ifa_addr->sa_family != AF_LINK) {
217                                 continue;
218                         }
219
220                         for (iftmp = ifa->ifa_next;
221                                         iftmp != NULL && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
222                                         iftmp = iftmp->ifa_next) {
223                                 if (iftmp->ifa_addr->sa_family == AF_INET) {
224                                         memcpy(&(ns->addr), iftmp->ifa_addr,
225                                                 iftmp->ifa_addr->sa_len);
226                                 }
227                         }
228
229                         ifd = (struct if_data *) ifa->ifa_data;
230                         r = ifd->ifi_ibytes;
231                         t = ifd->ifi_obytes;
232
233                         if (r < ns->last_read_recv) {
234                                 ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r;
235                         } else {
236                                 ns->recv += (r - ns->last_read_recv);
237                         }
238
239                         ns->last_read_recv = r;
240
241                         if (t < ns->last_read_trans) {
242                                 ns->trans += (long long) 4294967295U - ns->last_read_trans + t;
243                         } else {
244                                 ns->trans += (t - ns->last_read_trans);
245                         }
246
247                         ns->last_read_trans = t;
248
249                         /* calculate speeds */
250                         ns->recv_speed = (ns->recv - last_recv) / delta;
251                         ns->trans_speed = (ns->trans - last_trans) / delta;
252                 } else {
253                         ns->up = 0;
254                 }
255         }
256
257         freeifaddrs(ifap);
258 }
259
260 void update_total_processes()
261 {
262         int n_processes;
263
264         kvm_init();
265         kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
266
267         info.procs = n_processes;
268 }
269
270 void update_running_processes()
271 {
272         struct kinfo_proc2 *p;
273         int n_processes;
274         int i, cnt = 0;
275
276         kvm_init();
277         int max_size = sizeof(struct kinfo_proc2);
278
279         p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
280         for (i = 0; i < n_processes; i++) {
281                 if (p[i].p_stat == SRUN) {
282                         cnt++;
283                 }
284         }
285
286         info.run_procs = cnt;
287 }
288
289 /* new SMP code can be enabled by commenting the following line */
290 #define OLDCPU
291
292 #ifdef OLDCPU
293 struct cpu_load_struct {
294         unsigned long load[5];
295 };
296
297 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
298 long cpu_used, oldtotal, oldused;
299 #else
300 #include <assert.h>
301 int64_t *fresh = NULL;
302
303 /* XXX is 8 enough? - What's the constant for MAXCPU? */
304 /* allocate this with malloc would be better */
305 int64_t oldtotal[8], oldused[8];
306 #endif
307
308 void get_cpu_count()
309 {
310         int cpu_count = 1;      /* default to 1 cpu */
311 #ifndef OLDCPU
312         int mib[2] = { CTL_HW, HW_NCPU };
313         size_t len = sizeof(cpu_count);
314
315         if (sysctl(mib, 2, &cpu_count, &len, NULL, 0) != 0) {
316                 ERR("error getting cpu count, defaulting to 1");
317         }
318 #endif
319         info.cpu_count = cpu_count;
320
321         info.cpu_usage = malloc(info.cpu_count * sizeof(float));
322         if (info.cpu_usage == NULL) {
323                 CRIT_ERR("malloc");
324         }
325
326 #ifndef OLDCPU
327         assert(fresh == NULL);  /* XXX Is this leaking memory? */
328         /* XXX Where shall I free this? */
329         if (NULL == (fresh = calloc(cpu_count, sizeof(int64_t) * CPUSTATES))) {
330                 CRIT_ERR("calloc");
331         }
332 #endif
333 }
334
335 void update_cpu_usage()
336 {
337 #ifdef OLDCPU
338         int mib[2] = { CTL_KERN, KERN_CPTIME };
339         long used, total;
340         long cp_time[CPUSTATES];
341         size_t len = sizeof(cp_time);
342 #else
343         size_t size;
344         unsigned int i;
345 #endif
346
347         /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
348         if ((cpu_setup == 0) || (!info.cpu_usage)) {
349                 get_cpu_count();
350                 cpu_setup = 1;
351         }
352
353 #ifdef OLDCPU
354         if (sysctl(mib, 2, &cp_time, &len, NULL, 0) < 0) {
355                 ERR("Cannot get kern.cp_time");
356         }
357
358         fresh.load[0] = cp_time[CP_USER];
359         fresh.load[1] = cp_time[CP_NICE];
360         fresh.load[2] = cp_time[CP_SYS];
361         fresh.load[3] = cp_time[CP_IDLE];
362         fresh.load[4] = cp_time[CP_IDLE];
363
364         used = fresh.load[0] + fresh.load[1] + fresh.load[2];
365         total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
366
367         if ((total - oldtotal) != 0) {
368                 info.cpu_usage[0] = ((double) (used - oldused)) /
369                         (double) (total - oldtotal);
370         } else {
371                 info.cpu_usage[0] = 0;
372         }
373
374         oldused = used;
375         oldtotal = total;
376 #else
377         if (info.cpu_count > 1) {
378                 size = CPUSTATES * sizeof(int64_t);
379                 for (i = 0; i < info.cpu_count; i++) {
380                         int cp_time_mib[] = { CTL_KERN, KERN_CPTIME2, i };
381                         if (sysctl(cp_time_mib, 3, &(fresh[i * CPUSTATES]), &size, NULL, 0)
382                                         < 0) {
383                                 ERR("sysctl kern.cp_time2 failed");
384                         }
385                 }
386         } else {
387                 int cp_time_mib[] = { CTL_KERN, KERN_CPTIME };
388                 long cp_time_tmp[CPUSTATES];
389
390                 size = sizeof(cp_time_tmp);
391                 if (sysctl(cp_time_mib, 2, cp_time_tmp, &size, NULL, 0) < 0) {
392                         ERR("sysctl kern.cp_time failed");
393                 }
394
395                 for (i = 0; i < CPUSTATES; i++) {
396                         fresh[i] = (int64_t) cp_time_tmp[i];
397                 }
398         }
399
400         /* XXX Do sg with this int64_t => long => double ? float hell. */
401         for (i = 0; i < info.cpu_count; i++) {
402                 int64_t used, total;
403                 int at = i * CPUSTATES;
404
405                 used = fresh[at + CP_USER] + fresh[at + CP_NICE] + fresh[at + CP_SYS];
406                 total = used + fresh[at + CP_IDLE];
407
408                 if ((total - oldtotal[i]) != 0) {
409                         info.cpu_usage[i] = ((double) (used - oldused[i])) /
410                                 (double) (total - oldtotal[i]);
411                 } else {
412                         info.cpu_usage[i] = 0;
413                 }
414
415                 oldused[i] = used;
416                 oldtotal[i] = total;
417         }
418 #endif
419 }
420
421 void update_load_average()
422 {
423         double v[3];
424
425         getloadavg(v, 3);
426
427         info.loadavg[0] = (float) v[0];
428         info.loadavg[1] = (float) v[1];
429         info.loadavg[2] = (float) v[2];
430 }
431
432 /* read sensors from sysctl */
433 void update_obsd_sensors()
434 {
435         int sensor_cnt, dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
436         struct sensor sensor;
437         struct sensordev sensordev;
438         size_t slen, sdlen;
439         enum sensor_type type;
440
441         slen = sizeof(sensor);
442         sdlen = sizeof(sensordev);
443
444         sensor_cnt = 0;
445
446         dev = obsd_sensors.device;      // FIXME: read more than one device
447
448         /* for (dev = 0; dev < MAXSENSORDEVICES; dev++) { */
449                 mib[2] = dev;
450                 if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
451                         if (errno != ENOENT) {
452                                 warn("sysctl");
453                         }
454                         return;
455                         // continue;
456                 }
457                 for (type = 0; type < SENSOR_MAX_TYPES; type++) {
458                         mib[3] = type;
459                         for (numt = 0; numt < sensordev.maxnumt[type]; numt++) {
460                                 mib[4] = numt;
461                                 if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
462                                         if (errno != ENOENT) {
463                                                 warn("sysctl");
464                                         }
465                                         continue;
466                                 }
467                                 if (sensor.flags & SENSOR_FINVALID) {
468                                         continue;
469                                 }
470
471                                 switch (type) {
472                                         case SENSOR_TEMP:
473                                                 obsd_sensors.temp[dev][sensor.numt] =
474                                                         (sensor.value - 273150000) / 1000000.0;
475                                                 break;
476                                         case SENSOR_FANRPM:
477                                                 obsd_sensors.fan[dev][sensor.numt] = sensor.value;
478                                                 break;
479                                         case SENSOR_VOLTS_DC:
480                                                 obsd_sensors.volt[dev][sensor.numt] =
481                                                         sensor.value / 1000000.0;
482                                                 break;
483                                         default:
484                                                 break;
485                                 }
486
487                                 sensor_cnt++;
488                         }
489                 }
490         /* } */
491
492         init_sensors = 1;
493 }
494
495 /* chipset vendor */
496 void get_obsd_vendor(char *buf, size_t client_buffer_size)
497 {
498         int mib[2];
499
500         mib[0] = CTL_HW;
501         mib[1] = HW_VENDOR;
502         char vendor[64];
503         size_t size = sizeof(vendor);
504
505         if (sysctl(mib, 2, vendor, &size, NULL, 0) == -1) {
506                 ERR("error reading vendor");
507                 snprintf(buf, client_buffer_size, "unknown");
508         } else {
509                 snprintf(buf, client_buffer_size, "%s", vendor);
510         }
511 }
512
513 /* chipset name */
514 void get_obsd_product(char *buf, size_t client_buffer_size)
515 {
516         int mib[2];
517
518         mib[0] = CTL_HW;
519         mib[1] = HW_PRODUCT;
520         char product[64];
521         size_t size = sizeof(product);
522
523         if (sysctl(mib, 2, product, &size, NULL, 0) == -1) {
524                 ERR("error reading product");
525                 snprintf(buf, client_buffer_size, "unknown");
526         } else {
527                 snprintf(buf, client_buffer_size, "%s", product);
528         }
529 }
530
531 /* rdtsc() and get_freq_dynamic() copied from linux.c */
532
533 #if  defined(__i386) || defined(__x86_64)
534 __inline__ unsigned long long int rdtsc()
535 {
536         unsigned long long int x;
537
538         __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
539         return x;
540 }
541 #endif
542
543 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
544 void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
545                 const char *p_format, int divisor)
546 {
547 #if  defined(__i386) || defined(__x86_64)
548         struct timezone tz;
549         struct timeval tvstart, tvstop;
550         unsigned long long cycles[2];   /* gotta be 64 bit */
551         unsigned int microseconds;      /* total time taken */
552
553         memset(&tz, 0, sizeof(tz));
554
555         /* get this function in cached memory */
556         gettimeofday(&tvstart, &tz);
557         cycles[0] = rdtsc();
558         gettimeofday(&tvstart, &tz);
559
560         /* we don't trust that this is any specific length of time */
561         usleep(100);
562         cycles[1] = rdtsc();
563         gettimeofday(&tvstop, &tz);
564         microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
565                 (tvstop.tv_usec - tvstart.tv_usec);
566
567         snprintf(p_client_buffer, client_buffer_size, p_format,
568                 (float) ((cycles[1] - cycles[0]) / microseconds) / divisor);
569 #else
570         get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
571 #endif
572 }
573
574 /* void */
575 char get_freq(char *p_client_buffer, size_t client_buffer_size,
576                 const char *p_format, int divisor, unsigned int cpu)
577 {
578         int freq = cpu;
579         int mib[2] = { CTL_HW, HW_CPUSPEED };
580
581         if (!p_client_buffer || client_buffer_size <= 0 || !p_format
582                         || divisor <= 0) {
583                 return 0;
584         }
585
586         size_t size = sizeof(freq);
587
588         if (sysctl(mib, 2, &freq, &size, NULL, 0) == 0) {
589                 snprintf(p_client_buffer, client_buffer_size, p_format,
590                         (float) freq / divisor);
591         } else {
592                 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
593         }
594
595         return 1;
596 }
597
598 void update_top()
599 {
600         proc_find_top(info.cpu, info.memu);
601 }
602
603 #if 0
604 /* deprecated, will rewrite this soon in update_net_stats() -hifi */
605 void update_wifi_stats()
606 {
607         struct net_stat *ns;
608         struct ifaddrs *ifap, *ifa;
609         struct ifmediareq ifmr;
610         struct ieee80211_nodereq nr;
611         struct ieee80211_bssid bssid;
612         int s, ibssid;
613
614         /* Get iface table */
615         if (getifaddrs(&ifap) < 0) {
616                 return;
617         }
618
619         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
620                 ns = get_net_stat((const char *) ifa->ifa_name);
621
622                 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
623
624                 /* Get media type */
625                 bzero(&ifmr, sizeof(ifmr));
626                 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
627                 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
628                         close(s);
629                         return;
630                 }
631
632                 /* We can monitor only wireless interfaces
633                  * which are not in hostap mode */
634                 if ((ifmr.ifm_active & IFM_IEEE80211)
635                                 && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
636                         /* Get wi status */
637
638                         memset(&bssid, 0, sizeof(bssid));
639                         strlcpy(bssid.i_name, ifa->ifa_name, sizeof(bssid.i_name));
640                         ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
641
642                         bzero(&nr, sizeof(nr));
643                         bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
644                         strlcpy(nr.nr_ifname, ifa->ifa_name, sizeof(nr.nr_ifname));
645
646                         if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
647                                 ns->linkstatus = nr.nr_rssi;
648                         }
649                 }
650 cleanup:
651                 close(s);
652         }
653 }
654 #endif
655
656 void update_diskio()
657 {
658         return; /* XXX: implement? hifi: not sure how */
659 }
660
661 /* While topless is obviously better, top is also not bad. */
662
663 int comparecpu(const void *a, const void *b)
664 {
665         if (((struct process *) a)->amount > ((struct process *) b)->amount) {
666                 return -1;
667         }
668
669         if (((struct process *) a)->amount < ((struct process *) b)->amount) {
670                 return 1;
671         }
672
673         return 0;
674 }
675
676 int comparemem(const void *a, const void *b)
677 {
678         if (((struct process *) a)->totalmem > ((struct process *) b)->totalmem) {
679                 return -1;
680         }
681
682         if (((struct process *) a)->totalmem < ((struct process *) b)->totalmem) {
683                 return 1;
684         }
685
686         return 0;
687 }
688
689 inline void proc_find_top(struct process **cpu, struct process **mem)
690 {
691         struct kinfo_proc2 *p;
692         int n_processes;
693         int i, j = 0;
694         struct process *processes;
695         int mib[2];
696
697         int total_pages;
698         int pagesize = getpagesize();
699
700         /* we get total pages count again to be sure it is up to date */
701         mib[0] = CTL_HW;
702         mib[1] = HW_USERMEM;
703         size_t size = sizeof(total_pages);
704
705         if (sysctl(mib, 2, &total_pages, &size, NULL, 0) == -1) {
706                 ERR("error reading nmempages");
707         }
708
709         int max_size = sizeof(struct kinfo_proc2);
710
711         p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
712         processes = malloc(n_processes * sizeof(struct process));
713
714         for (i = 0; i < n_processes; i++) {
715                 if (!((p[i].p_flag & P_SYSTEM)) && p[i].p_comm != NULL) {
716                         processes[j].pid = p[i].p_pid;
717                         processes[j].name = strndup(p[i].p_comm, text_buffer_size);
718                         processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE;
719                         processes[j].totalmem = (float) (p[i].p_vm_rssize * pagesize /
720                                 (float) total_pages) * 100.0;
721                         j++;
722                 }
723         }
724
725         qsort(processes, j - 1, sizeof(struct process), comparemem);
726         for (i = 0; i < 10; i++) {
727                 struct process *tmp, *ttmp;
728
729                 tmp = malloc(sizeof(struct process));
730                 tmp->pid = processes[i].pid;
731                 tmp->amount = processes[i].amount;
732                 tmp->totalmem = processes[i].totalmem;
733                 tmp->name = strndup(processes[i].name, text_buffer_size);
734
735                 ttmp = mem[i];
736                 mem[i] = tmp;
737                 if (ttmp != NULL) {
738                         free(ttmp->name);
739                         free(ttmp);
740                 }
741         }
742
743         qsort(processes, j - 1, sizeof(struct process), comparecpu);
744         for (i = 0; i < 10; i++) {
745                 struct process *tmp, *ttmp;
746
747                 tmp = malloc(sizeof(struct process));
748                 tmp->pid = processes[i].pid;
749                 tmp->amount = processes[i].amount;
750                 tmp->totalmem = processes[i].totalmem;
751                 tmp->name = strndup(processes[i].name, text_buffer_size);
752
753                 ttmp = cpu[i];
754                 cpu[i] = tmp;
755                 if (ttmp != NULL) {
756                         free(ttmp->name);
757                         free(ttmp);
758                 }
759         }
760
761         for (i = 0; i < j; i++) {
762                 free(processes[i].name);
763         }
764         free(processes);
765 }
766
767 #if     defined(i386) || defined(__i386__)
768 #define APMDEV          "/dev/apm"
769 #define APM_UNKNOWN     255
770
771 int apm_getinfo(int fd, apm_info_t aip)
772 {
773         if (ioctl(fd, APM_IOC_GETPOWER, aip) == -1) {
774                 return -1;
775         }
776
777         return 0;
778 }
779
780 char *get_apm_adapter()
781 {
782         int fd;
783         struct apm_power_info info;
784         char *out;
785
786         out = (char *) calloc(16, sizeof(char));
787
788         fd = open(APMDEV, O_RDONLY);
789         if (fd < 0) {
790                 strncpy(out, "ERR", 16);
791                 return out;
792         }
793
794         if (apm_getinfo(fd, &info) != 0) {
795                 close(fd);
796                 strncpy(out, "ERR", 16);
797                 return out;
798         }
799         close(fd);
800
801         switch (info.ac_state) {
802                 case APM_AC_OFF:
803                         strncpy(out, "off-line", 16);
804                         return out;
805                         break;
806                 case APM_AC_ON:
807                         if (info.battery_state == APM_BATT_CHARGING) {
808                                 strncpy(out, "charging", 16);
809                                 return out;
810                         } else {
811                                 strncpy(out, "on-line", 16);
812                                 return out;
813                         }
814                         break;
815                 default:
816                         strncpy(out, "unknown", 16);
817                         return out;
818                         break;
819         }
820 }
821
822 char *get_apm_battery_life()
823 {
824         int fd;
825         u_int batt_life;
826         struct apm_power_info info;
827         char *out;
828
829         out = (char *) calloc(16, sizeof(char));
830
831         fd = open(APMDEV, O_RDONLY);
832         if (fd < 0) {
833                 strncpy(out, "ERR", 16);
834                 return out;
835         }
836
837         if (apm_getinfo(fd, &info) != 0) {
838                 close(fd);
839                 strncpy(out, "ERR", 16);
840                 return out;
841         }
842         close(fd);
843
844         batt_life = info.battery_life;
845         if (batt_life <= 100) {
846                 snprintf(out, 16, "%d%%", batt_life);
847                 return out;
848         } else {
849                 strncpy(out, "ERR", 16);
850         }
851
852         return out;
853 }
854
855 char *get_apm_battery_time()
856 {
857         int fd;
858         int batt_time;
859         int h, m;
860         struct apm_power_info info;
861         char *out;
862
863         out = (char *) calloc(16, sizeof(char));
864
865         fd = open(APMDEV, O_RDONLY);
866         if (fd < 0) {
867                 strncpy(out, "ERR", 16);
868                 return out;
869         }
870
871         if (apm_getinfo(fd, &info) != 0) {
872                 close(fd);
873                 strncpy(out, "ERR", 16);
874                 return out;
875         }
876         close(fd);
877
878         batt_time = info.minutes_left;
879
880         if (batt_time == -1) {
881                 strncpy(out, "unknown", 16);
882         } else {
883                 h = batt_time / 60;
884                 m = batt_time % 60;
885                 snprintf(out, 16, "%2d:%02d", h, m);
886         }
887
888         return out;
889 }
890
891 #endif
892
893 /* empty stubs so conky links */
894 void prepare_update()
895 {
896 }
897
898 void update_entropy(void)
899 {
900 }
901
902 void free_all_processes(void)
903 {
904 }