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