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