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