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