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