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