* Replacing strdup() with strndup() throughout
[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                         goto cleanup;
632                 }
633
634                 /* We can monitor only wireless interfaces
635                  * which are not in hostap mode */
636                 if ((ifmr.ifm_active & IFM_IEEE80211)
637                                 && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
638                         /* Get wi status */
639
640                         memset(&bssid, 0, sizeof(bssid));
641                         strlcpy(bssid.i_name, ifa->ifa_name, sizeof(bssid.i_name));
642                         ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
643
644                         bzero(&nr, sizeof(nr));
645                         bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
646                         strlcpy(nr.nr_ifname, ifa->ifa_name, sizeof(nr.nr_ifname));
647
648                         if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
649                                 ns->linkstatus = nr.nr_rssi;
650                         }
651                 }
652 cleanup:
653                 close(s);
654         }
655 }
656 #endif
657
658 void update_diskio()
659 {
660         return; /* XXX: implement? hifi: not sure how */
661 }
662
663 /* While topless is obviously better, top is also not bad. */
664
665 int comparecpu(const void *a, const void *b)
666 {
667         if (((struct process *) a)->amount > ((struct process *) b)->amount) {
668                 return -1;
669         }
670
671         if (((struct process *) a)->amount < ((struct process *) b)->amount) {
672                 return 1;
673         }
674
675         return 0;
676 }
677
678 int comparemem(const void *a, const void *b)
679 {
680         if (((struct process *) a)->totalmem > ((struct process *) b)->totalmem) {
681                 return -1;
682         }
683
684         if (((struct process *) a)->totalmem < ((struct process *) b)->totalmem) {
685                 return 1;
686         }
687
688         return 0;
689 }
690
691 inline void proc_find_top(struct process **cpu, struct process **mem)
692 {
693         struct kinfo_proc2 *p;
694         int n_processes;
695         int i, j = 0;
696         struct process *processes;
697         int mib[2];
698
699         int total_pages;
700         int pagesize = getpagesize();
701
702         /* we get total pages count again to be sure it is up to date */
703         mib[0] = CTL_HW;
704         mib[1] = HW_USERMEM;
705         size_t size = sizeof(total_pages);
706
707         if (sysctl(mib, 2, &total_pages, &size, NULL, 0) == -1) {
708                 ERR("error reading nmempages");
709         }
710
711         int max_size = sizeof(struct kinfo_proc2);
712
713         p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
714         processes = malloc(n_processes * sizeof(struct process));
715
716         for (i = 0; i < n_processes; i++) {
717                 if (!((p[i].p_flag & P_SYSTEM)) && p[i].p_comm != NULL) {
718                         processes[j].pid = p[i].p_pid;
719                         processes[j].name = strndup(p[i].p_comm, text_buffer_size);
720                         processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE;
721                         processes[j].totalmem = (float) (p[i].p_vm_rssize * pagesize /
722                                 (float) total_pages) * 100.0;
723                         j++;
724                 }
725         }
726
727         qsort(processes, j - 1, sizeof(struct process), comparemem);
728         for (i = 0; i < 10; i++) {
729                 struct process *tmp, *ttmp;
730
731                 tmp = malloc(sizeof(struct process));
732                 tmp->pid = processes[i].pid;
733                 tmp->amount = processes[i].amount;
734                 tmp->totalmem = processes[i].totalmem;
735                 tmp->name = strndup(processes[i].name, text_buffer_size);
736
737                 ttmp = mem[i];
738                 mem[i] = tmp;
739                 if (ttmp != NULL) {
740                         free(ttmp->name);
741                         free(ttmp);
742                 }
743         }
744
745         qsort(processes, j - 1, sizeof(struct process), comparecpu);
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 = cpu[i];
756                 cpu[i] = tmp;
757                 if (ttmp != NULL) {
758                         free(ttmp->name);
759                         free(ttmp);
760                 }
761         }
762
763         for (i = 0; i < j; i++) {
764                 free(processes[i].name);
765         }
766         free(processes);
767 }
768
769 #if     defined(i386) || defined(__i386__)
770 #define APMDEV          "/dev/apm"
771 #define APM_UNKNOWN     255
772
773 int apm_getinfo(int fd, apm_info_t aip)
774 {
775         if (ioctl(fd, APM_IOC_GETPOWER, aip) == -1) {
776                 return -1;
777         }
778
779         return 0;
780 }
781
782 char *get_apm_adapter()
783 {
784         int fd;
785         struct apm_power_info info;
786         char *out;
787
788         out = (char *) calloc(16, sizeof(char));
789
790         fd = open(APMDEV, O_RDONLY);
791         if (fd < 0) {
792                 strncpy(out, "ERR", 16);
793                 return out;
794         }
795
796         if (apm_getinfo(fd, &info) != 0) {
797                 close(fd);
798                 strncpy(out, "ERR", 16);
799                 return out;
800         }
801         close(fd);
802
803         switch (info.ac_state) {
804                 case APM_AC_OFF:
805                         strncpy(out, "off-line", 16);
806                         return out;
807                         break;
808                 case APM_AC_ON:
809                         if (info.battery_state == APM_BATT_CHARGING) {
810                                 strncpy(out, "charging", 16);
811                                 return out;
812                         } else {
813                                 strncpy(out, "on-line", 16);
814                                 return out;
815                         }
816                         break;
817                 default:
818                         strncpy(out, "unknown", 16);
819                         return out;
820                         break;
821         }
822 }
823
824 char *get_apm_battery_life()
825 {
826         int fd;
827         u_int batt_life;
828         struct apm_power_info info;
829         char *out;
830
831         out = (char *) calloc(16, sizeof(char));
832
833         fd = open(APMDEV, O_RDONLY);
834         if (fd < 0) {
835                 strncpy(out, "ERR", 16);
836                 return out;
837         }
838
839         if (apm_getinfo(fd, &info) != 0) {
840                 close(fd);
841                 strncpy(out, "ERR", 16);
842                 return out;
843         }
844         close(fd);
845
846         batt_life = info.battery_life;
847         if (batt_life <= 100) {
848                 snprintf(out, 16, "%d%%", batt_life);
849                 return out;
850         } else {
851                 strncpy(out, "ERR", 16);
852         }
853
854         return out;
855 }
856
857 char *get_apm_battery_time()
858 {
859         int fd;
860         int batt_time;
861         int h, m;
862         struct apm_power_info info;
863         char *out;
864
865         out = (char *) calloc(16, sizeof(char));
866
867         fd = open(APMDEV, O_RDONLY);
868         if (fd < 0) {
869                 strncpy(out, "ERR", 16);
870                 return out;
871         }
872
873         if (apm_getinfo(fd, &info) != 0) {
874                 close(fd);
875                 strncpy(out, "ERR", 16);
876                 return out;
877         }
878         close(fd);
879
880         batt_time = info.minutes_left;
881
882         if (batt_time == -1) {
883                 strncpy(out, "unknown", 16);
884         } else {
885                 h = batt_time / 60;
886                 m = batt_time % 60;
887                 snprintf(out, 16, "%2d:%02d", h, m);
888         }
889
890         return out;
891 }
892
893 #endif
894
895 /* empty stubs so conky links */
896 void prepare_update()
897 {
898 }
899
900 void update_entropy(void)
901 {
902 }
903
904 void free_all_processes(void)
905 {
906 }