Remove freebsd wifi stuff since the API seems to be
[monky] / src / freebsd.c
1 /*
2  * freebsd.c
3  * Contains FreeBSD specific stuff
4  *
5  * $Id$
6  */
7
8 #include <sys/dkstat.h>
9 #include <sys/param.h>
10 #include <sys/resource.h>
11 #include <sys/socket.h>
12 #include <sys/sysctl.h>
13 #include <sys/time.h>
14 #include <sys/types.h>
15 #include <sys/vmmeter.h>
16 #include <sys/user.h>
17 #include <sys/ioctl.h>
18
19 #include <net/if.h>
20 #include <net/if_mib.h>
21 #include <net/if_media.h>
22 #include <net/if_var.h>
23 #include <netinet/in.h>
24
25 #include <devstat.h>
26 #include <fcntl.h>
27 #include <ifaddrs.h>
28 #include <limits.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33
34 #include <dev/wi/if_wavelan_ieee.h>
35
36 #include "conky.h"
37
38 #define GETSYSCTL(name, var)    getsysctl(name, &(var), sizeof (var))
39 #define KELVTOC(x)              ((x - 2732) / 10.0)
40 #define MAXSHOWDEVS             16
41
42 #if 0
43 #define FREEBSD_DEBUG
44 #endif
45
46 inline void proc_find_top(struct process **cpu, struct process **mem);
47
48 u_int64_t diskio_prev = 0;
49 static short cpu_setup = 0;
50 static short diskio_setup = 0;
51
52 static int getsysctl(char *name, void *ptr, size_t len)
53 {
54         size_t nlen = len;
55         if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
56                 return (-1);
57         }
58
59         if (nlen != len) {
60                 return (-1);
61         }
62
63         return (0);
64 }
65
66 struct ifmibdata *data = NULL;
67 size_t len = 0;
68
69 static int swapmode(int *retavail, int *retfree)
70 {
71         int n;
72         int pagesize = getpagesize();
73         struct kvm_swap swapary[1];
74
75         *retavail = 0;
76         *retfree = 0;
77
78 #define CONVERT(v)      ((quad_t)(v) * pagesize / 1024)
79
80         n = kvm_getswapinfo(kd, swapary, 1, 0);
81         if (n < 0 || swapary[0].ksw_total == 0)
82                 return (0);
83
84         *retavail = CONVERT(swapary[0].ksw_total);
85         *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
86
87         n = (int) ((double) swapary[0].ksw_used * 100.0 /
88                 (double) swapary[0].ksw_total);
89
90         return (n);
91 }
92
93 void
94 prepare_update()
95 {
96 }
97
98 void
99 update_uptime()
100 {
101         int mib[2] = { CTL_KERN, KERN_BOOTTIME };
102         struct timeval boottime;
103         time_t now;
104         size_t size = sizeof (boottime);
105
106         if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) &&
107                         (boottime.tv_sec != 0)) {
108                 time(&now);
109                 info.uptime = now - boottime.tv_sec;
110         } else {
111                 fprintf(stderr, "Could not get uptime\n");
112                 info.uptime = 0;
113         }
114 }
115
116 int check_mount(char *s)
117 {
118         struct statfs *mntbuf;
119         int i, mntsize;
120
121         mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
122         for (i = mntsize - 1; i >= 0; i--)
123                 if (strcmp(mntbuf[i].f_mntonname, s) == 0)
124                         return 1;
125
126         return 0;
127 }
128
129 void
130 update_meminfo()
131 {
132         int total_pages, inactive_pages, free_pages;
133         int swap_avail, swap_free;
134
135         int pagesize = getpagesize();
136
137         if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages))
138                 fprintf(stderr,
139                         "Cannot read sysctl \"vm.stats.vm.v_page_count\"");
140
141         if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages))
142                 fprintf(stderr,
143                         "Cannot read sysctl \"vm.stats.vm.v_free_count\"");
144
145         if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages))
146                 fprintf(stderr,
147                         "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"");
148
149         info.memmax = (total_pages * pagesize) >> 10;
150         info.mem =
151             ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
152
153
154         if ((swapmode(&swap_avail, &swap_free)) >= 0) {
155                 info.swapmax = swap_avail;
156                 info.swap = (swap_avail - swap_free);
157         } else {
158                 info.swapmax = 0;
159                 info.swap = 0;
160         }
161 }
162
163 void
164 update_net_stats()
165 {
166         struct net_stat *ns;
167         double delta;
168         long long r, t, last_recv, last_trans;
169         struct ifaddrs *ifap, *ifa;
170         struct if_data *ifd;
171
172
173         /* get delta */
174         delta = current_update_time - last_update_time;
175         if (delta <= 0.0001)
176                 return;
177
178         if (getifaddrs(&ifap) < 0)
179                 return;
180
181         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
182                 ns = get_net_stat((const char *) ifa->ifa_name);
183
184                 if (ifa->ifa_flags & IFF_UP) {
185                         struct ifaddrs *iftmp;
186
187                         ns->up = 1;
188                         ns->linkstatus = 1;
189                         last_recv = ns->recv;
190                         last_trans = ns->trans;
191
192                         if (ifa->ifa_addr->sa_family != AF_LINK)
193                                 continue;
194
195                         for (iftmp = ifa->ifa_next; iftmp != NULL &&
196                                 strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
197                                 iftmp = iftmp->ifa_next)
198                                 if (iftmp->ifa_addr->sa_family == AF_INET)
199                                         memcpy(&(ns->addr), iftmp->ifa_addr,
200                                                 iftmp->ifa_addr->sa_len);
201
202                         ifd = (struct if_data *) ifa->ifa_data;
203                         r = ifd->ifi_ibytes;
204                         t = ifd->ifi_obytes;
205
206                         if (r < ns->last_read_recv)
207                                 ns->recv +=
208                                     ((long long) 4294967295U -
209                                         ns->last_read_recv) + r;
210                         else
211                                 ns->recv += (r - ns->last_read_recv);
212
213                         ns->last_read_recv = r;
214
215                         if (t < ns->last_read_trans)
216                                 ns->trans +=
217                                     ((long long) 4294967295U -
218                                         ns->last_read_trans) + t;
219                         else
220                                 ns->trans += (t - ns->last_read_trans);
221
222                         ns->last_read_trans = t;
223
224                         /* calculate speeds */
225                         ns->recv_speed = (ns->recv - last_recv) / delta;
226                         ns->trans_speed = (ns->trans - last_trans) / delta;
227                 } else {
228                         ns->up = 0;
229                         ns->linkstatus = 0;
230                 }
231         }
232
233         freeifaddrs(ifap);
234 }
235
236 void
237 update_total_processes()
238 {
239         int n_processes;
240
241         kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
242
243         info.procs = n_processes;
244 }
245
246 void
247 update_running_processes()
248 {
249         struct kinfo_proc *p;
250         int n_processes;
251         int i, cnt = 0;
252
253         p = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
254         for (i = 0; i < n_processes; i++) {
255 #if __FreeBSD__ < 5
256                 if (p[i].kp_proc.p_stat == SRUN)
257 #else
258                 if (p[i].ki_stat == SRUN)
259 #endif
260                         cnt++;
261         }
262
263         info.run_procs = cnt;
264 }
265
266 struct cpu_load_struct {
267         unsigned long load[5];
268 };
269
270 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
271 long cpu_used, oldtotal, oldused;
272
273 void
274 get_cpu_count()
275 {
276         /* int cpu_count = 0; */
277
278         /*
279          * XXX
280          * FreeBSD doesn't allow to get per CPU load stats
281          * on SMP machines. It's possible to get a CPU count,
282          * but as we fulfil only info.cpu_usage[0], it's better
283          * to report there's only one CPU. It should fix some bugs
284          * (e.g. cpugraph)
285          */
286 #if 0
287         if (GETSYSCTL("hw.ncpu", cpu_count) == 0)
288                 info.cpu_count = cpu_count;
289 #endif
290         info.cpu_count = 1;
291
292         info.cpu_usage = malloc(info.cpu_count * sizeof (float));
293         if (info.cpu_usage == NULL)
294                 CRIT_ERR("malloc");
295 }
296
297 /* XXX: SMP support */
298 void
299 update_cpu_usage()
300 {
301         long used, total;
302         long cp_time[CPUSTATES];
303         size_t len = sizeof (cp_time);
304
305         /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
306         if ((cpu_setup == 0) || (!info.cpu_usage)) {
307                 get_cpu_count();
308                 cpu_setup = 1;
309         }
310
311         if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
312                 (void) fprintf(stderr, "Cannot get kern.cp_time");
313         }
314
315         fresh.load[0] = cp_time[CP_USER];
316         fresh.load[1] = cp_time[CP_NICE];
317         fresh.load[2] = cp_time[CP_SYS];
318         fresh.load[3] = cp_time[CP_IDLE];
319         fresh.load[4] = cp_time[CP_IDLE];
320
321         used = fresh.load[0] + fresh.load[1] + fresh.load[2];
322         total =
323             fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
324
325         if ((total - oldtotal) != 0) {
326                 info.cpu_usage[0] = ((double) (used - oldused)) /
327                         (double) (total - oldtotal);
328         } else {
329                 info.cpu_usage[0] = 0;
330         }
331
332         oldused = used;
333         oldtotal = total;
334 }
335
336 double
337 get_i2c_info(int *fd, int arg, char *devtype, char *type)
338 {
339         return (0);
340 }
341
342 void
343 update_load_average()
344 {
345         double v[3];
346         getloadavg(v, 3);
347
348         info.loadavg[0] = (float) v[0];
349         info.loadavg[1] = (float) v[1];
350         info.loadavg[2] = (float) v[2];
351 }
352
353 double
354 get_acpi_temperature(int fd)
355 {
356         int temp;
357
358         if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
359                 fprintf(stderr,
360                 "Cannot read sysctl \"hw.acpi.thermal.tz0.temperature\"\n");
361                 return (0.0);
362         }
363
364         return (KELVTOC(temp));
365 }
366
367 void
368 get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
369 {
370         int battime, batcapacity, batstate, ac;
371         char battery_status[64];
372         char battery_time[64];
373
374         if (GETSYSCTL("hw.acpi.battery.time", battime))
375                 (void) fprintf(stderr,
376                         "Cannot read sysctl \"hw.acpi.battery.time\"\n");
377         if (GETSYSCTL("hw.acpi.battery.life", batcapacity))
378                 (void) fprintf(stderr,
379                                            "Cannot read sysctl \"hw.acpi.battery.life\"\n");
380
381         if (GETSYSCTL("hw.acpi.battery.state", batstate))
382                 (void) fprintf(stderr,
383                                            "Cannot read sysctl \"hw.acpi.battery.state\"\n");
384
385         if (GETSYSCTL("hw.acpi.acline", ac))
386                 (void) fprintf(stderr,
387                                            "Cannot read sysctl \"hw.acpi.acline\"\n");
388
389         if (batstate == 1) {
390                 if (battime != -1) {
391                         snprintf (battery_status, sizeof(battery_status)-1,
392                                   "remaining %d%%", batcapacity);
393                         snprintf (battery_time, sizeof(battery_time)-1,
394                                   "%d:%2.2d", battime / 60, battime % 60);
395                         /*
396                         snprintf(buf, n, "remaining %d%% (%d:%2.2d)",
397                                         batcapacity, battime / 60, battime % 60);
398                         */
399                 }
400                 else
401                         /* no time estimate available yet */
402                         snprintf(battery_status, sizeof(battery_status)-1,
403                                  "remaining %d%%", batcapacity);
404                         /*
405                         snprintf(buf, n, "remaining %d%%",
406                                         batcapacity);
407                         */
408                 if (ac == 1)
409                         (void) fprintf(stderr, "Discharging while on AC!\n");
410         } else {
411                 snprintf (battery_status, sizeof(battery_status)-1,
412                           batstate == 2 ? "charging (%d%%)" : "charged (%d%%)", batcapacity);
413                 /*
414                 snprintf(buf, n, batstate == 2 ? "charging (%d%%)" : "charged (%d%%)", batcapacity);
415                 */
416                 if (batstate != 2 && batstate != 0)
417                         (void) fprintf(stderr, "Unknown battery state %d!\n", batstate);
418                 if (ac == 0)
419                         (void) fprintf(stderr, "Charging while not on AC!\n");
420         }
421
422         switch (item) {
423         case BATTERY_STATUS:
424                 {
425                         snprintf(buf, n, "%s", battery_status);
426                         break;
427                 }
428         case BATTERY_TIME:
429                 {
430                         snprintf(buf, n, "%s", battery_time);
431                         break;
432                 }
433         default:
434                         break;
435         }
436         return;
437 }
438
439 int
440 get_battery_perct(const char *bat)
441 {
442         /* not implemented */
443         return (0);
444 }
445
446 int
447 get_battery_perct_bar(const char *bar)
448 {
449         /* not implemented */
450         return (0);
451 }
452
453 int
454 open_i2c_sensor(const char *dev, const char *type, int n, int *div,
455                 char *devtype)
456 {
457         return (0);
458 }
459
460 int
461 open_acpi_temperature(const char *name)
462 {
463         return (0);
464 }
465
466 void
467 get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
468 {
469         int state;
470
471         if (!p_client_buffer || client_buffer_size <= 0)
472                 return;
473
474         if (GETSYSCTL("hw.acpi.acline", state)) {
475                 fprintf(stderr,
476                         "Cannot read sysctl \"hw.acpi.acline\"\n");
477                 return;
478         }
479
480
481         if (state)
482                 strncpy(p_client_buffer, "Running on AC Power",
483                                 client_buffer_size);
484         else
485                 strncpy(p_client_buffer, "Running on battery",
486                                 client_buffer_size);
487
488 }
489
490 void
491 get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
492 {
493         if (!p_client_buffer || client_buffer_size <= 0)
494                 return;
495
496         /* not implemented */
497         memset(p_client_buffer, 0, client_buffer_size);
498 }
499
500 void
501 get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
502 {
503         if (!p_client_buffer || client_buffer_size <= 0)
504                 return;
505
506         /* not implemented */
507         memset(p_client_buffer, 0, client_buffer_size);
508 }
509
510 void
511 get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size)
512 {
513         if (!p_client_buffer || client_buffer_size <= 0)
514                 return;
515
516         /* not implemented */
517         memset(p_client_buffer, 0, client_buffer_size);
518 }
519
520 /* rdtsc() and get_freq_dynamic() copied from linux.c */
521
522 #if  defined(__i386) || defined(__x86_64)
523 __inline__ unsigned long long int
524 rdtsc()
525 {
526         unsigned long long int x;
527         __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
528         return (x);
529 }
530 #endif
531
532 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
533 void
534 get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
535                 char *p_format, int divisor)
536 {
537 #if  defined(__i386) || defined(__x86_64)
538         struct timezone tz;
539         struct timeval tvstart, tvstop;
540         unsigned long long cycles[2];   /* gotta be 64 bit */
541         unsigned int microseconds;      /* total time taken */
542
543         memset(&tz, 0, sizeof (tz));
544
545         /* get this function in cached memory */
546         gettimeofday(&tvstart, &tz);
547         cycles[0] = rdtsc();
548         gettimeofday(&tvstart, &tz);
549
550         /* we don't trust that this is any specific length of time */
551         usleep(100);
552         cycles[1] = rdtsc();
553         gettimeofday(&tvstop, &tz);
554         microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
555                 (tvstop.tv_usec - tvstart.tv_usec);
556
557         snprintf(p_client_buffer, client_buffer_size, p_format,
558                 (float)((cycles[1] - cycles[0]) / microseconds) / divisor);
559 #else
560         get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
561 #endif
562 }
563
564 /*void*/
565 char
566 get_freq(char *p_client_buffer, size_t client_buffer_size,
567                 char *p_format, int divisor, unsigned int cpu)
568 {
569         int freq;
570         char *freq_sysctl;
571
572         freq_sysctl = (char *)calloc(16, sizeof(char));
573         if (freq_sysctl == NULL)
574                 exit(-1);
575
576         snprintf(freq_sysctl, 16, "dev.cpu.%d.freq", (cpu - 1));
577         
578         if (!p_client_buffer || client_buffer_size <= 0 ||
579                         !p_format || divisor <= 0)
580                 return 0;
581
582         if (GETSYSCTL(freq_sysctl, freq) == 0)
583                 snprintf(p_client_buffer, client_buffer_size,
584                                 p_format, (float)freq/divisor);
585         else
586                 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
587
588         free(freq_sysctl);
589         return 1;
590 }
591
592 void
593 update_top()
594 {
595         proc_find_top(info.cpu, info.memu);
596 }
597
598 #if 0
599 void
600 update_wifi_stats()
601 {
602         struct ifreq ifr;               /* interface stats */
603         struct wi_req wireq;
604         struct net_stat * ns;
605         struct ifaddrs *ifap, *ifa;
606         struct ifmediareq ifmr;
607         int s;
608
609         /*
610          * Get iface table
611          */
612         if (getifaddrs(&ifap) < 0)
613                 return;
614
615         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
616                 ns = get_net_stat((const char *) ifa->ifa_name);
617
618                 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
619
620                 /* Get media type */
621                 bzero(&ifmr, sizeof(ifmr));
622                 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
623                 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0)
624                         goto cleanup;
625                 
626                 /*
627                  * We can monitor only wireless interfaces
628                  * which not in hostap mode
629                  */
630                 if ((ifmr.ifm_active & IFM_IEEE80211) &&
631                                 !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
632                         /* Get wi status */
633                         bzero(&ifr, sizeof(ifr));
634                         strlcpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);
635                         wireq.wi_type   = WI_RID_COMMS_QUALITY;
636                         wireq.wi_len    = WI_MAX_DATALEN;
637                         ifr.ifr_data    = (void *) &wireq;
638
639                         if (ioctl(s, SIOCGWAVELAN, (caddr_t) &ifr) < 0) {
640                                 perror("ioctl (getting wi status)");
641                                 exit(1);
642                         }
643
644                         /*
645                          * wi_val[0] = quality
646                          * wi_val[1] = signal
647                          * wi_val[2] = noise
648                          */
649                         ns->linkstatus = (int) wireq.wi_val[1];
650                 }
651 cleanup:
652                 close(s);
653         }
654 }
655 #endif
656
657 void
658 update_diskio()
659 {
660         int devs_count,
661             num_selected,
662             num_selections;
663         struct device_selection *dev_select = NULL;
664         long select_generation;
665         int dn;
666         static struct statinfo  statinfo_cur;
667         u_int64_t diskio_current = 0;
668
669         bzero(&statinfo_cur, sizeof (statinfo_cur));
670         statinfo_cur.dinfo = (struct devinfo *)malloc(sizeof (struct devinfo));
671         bzero(statinfo_cur.dinfo, sizeof (struct devinfo));
672
673         if (devstat_getdevs(NULL, &statinfo_cur) < 0)
674                 return;
675
676         devs_count = statinfo_cur.dinfo->numdevs;
677         if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
678                         &select_generation, statinfo_cur.dinfo->generation,
679                         statinfo_cur.dinfo->devices, devs_count, NULL, 0,
680                         NULL, 0, DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
681                 for (dn = 0; dn < devs_count; ++dn) {
682                         int di;
683                         struct devstat  *dev;
684
685                         di = dev_select[dn].position;
686                         dev = &statinfo_cur.dinfo->devices[di];
687
688                         diskio_current += dev->bytes[DEVSTAT_READ] +
689                                 dev->bytes[DEVSTAT_WRITE];
690                 }
691
692                 free(dev_select);
693         }
694
695         /*
696          * Since we return (diskio_total_current - diskio_total_old), first
697          * frame will be way too high (it will be equal to
698          * diskio_total_current, i.e. all disk I/O since boot). That's why
699          * it is better to return 0 first time;
700          */
701         if (diskio_setup == 0) {
702                 diskio_setup = 1;
703                 diskio_value = 0;
704         } else
705                 diskio_value = (unsigned int)((diskio_current - diskio_prev)/
706                                 1024);
707         diskio_prev = diskio_current;
708
709         free(statinfo_cur.dinfo);
710 }
711
712 /*
713  * While topless is obviously better, top is also not bad.
714  */
715
716 int
717 comparecpu(const void *a, const void *b)
718 {
719         if (((struct process *)a)->amount > ((struct process *)b)->amount)
720                 return (-1);
721
722         if (((struct process *)a)->amount < ((struct process *)b)->amount)
723                 return (1);
724
725         return (0);
726 }
727
728 int
729 comparemem(const void *a, const void *b)
730 {
731         if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem)
732                 return (-1);
733
734         if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem)
735                 return (1);
736
737         return (0);
738 }
739
740 inline void
741 proc_find_top(struct process **cpu, struct process **mem)
742 {
743         struct kinfo_proc *p;
744         int n_processes;
745         int i, j = 0;
746         struct process *processes;
747
748         int total_pages;
749
750         /* we get total pages count again to be sure it is up to date */
751         if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0)
752                 CRIT_ERR("Cannot read sysctl"
753                         "\"vm.stats.vm.v_page_count\"");
754
755         p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
756         processes = malloc(n_processes * sizeof (struct process));
757
758         for (i = 0; i < n_processes; i++) {
759                 if (!((p[i].ki_flag & P_SYSTEM)) &&
760                                 p[i].ki_comm != NULL) {
761                         processes[j].pid = p[i].ki_pid;
762                         processes[j].name =  strdup(p[i].ki_comm);
763                         processes[j].amount = 100.0 *
764                                 p[i].ki_pctcpu / FSCALE;
765                         processes[j].totalmem = (float)(p[i].ki_rssize /
766                                         (float)total_pages) * 100.0;
767                         j++;
768                 }
769         }
770
771         qsort(processes, j - 1, sizeof (struct process), comparemem);
772         for (i = 0; i < 10 && i < n_processes; i++) {
773                 struct process *tmp, *ttmp;
774
775                 tmp = malloc(sizeof (struct process));
776                 tmp->pid = processes[i].pid;
777                 tmp->amount = processes[i].amount;
778                 tmp->totalmem = processes[i].totalmem;
779                 tmp->name = strdup(processes[i].name);
780
781                 ttmp = mem[i];
782                 mem[i] = tmp;
783                 if (ttmp != NULL) {
784                         free(ttmp->name);
785                         free(ttmp);
786                 }
787         }
788
789         qsort(processes, j - 1, sizeof (struct process), comparecpu);
790         for (i = 0; i < 10 && i < n_processes; i++) {
791                 struct process *tmp, *ttmp;
792
793                 tmp = malloc(sizeof (struct process));
794                 tmp->pid = processes[i].pid;
795                 tmp->amount = processes[i].amount;
796                 tmp->totalmem = processes[i].totalmem;
797                 tmp->name = strdup(processes[i].name);
798
799                 ttmp = cpu[i];
800                 cpu[i] = tmp;
801                 if (ttmp != NULL) {
802                         free(ttmp->name);
803                         free(ttmp);
804                 }
805         }
806
807 #if defined(FREEBSD_DEBUG)
808         printf("=====\nmem\n");
809         for (i = 0; i < 10; i++) {
810                 printf("%d: %s(%d) %.2f\n", i, mem[i]->name,
811                                 mem[i]->pid, mem[i]->totalmem);
812         }
813 #endif
814
815         for (i = 0; i < j; free(processes[i++].name));
816         free(processes);
817 }
818
819 #if     defined(i386) || defined(__i386__)
820 #define APMDEV          "/dev/apm"
821 #define APM_UNKNOWN     255
822
823 int
824 apm_getinfo(int fd, apm_info_t aip)
825 {
826         if (ioctl(fd, APMIO_GETINFO, aip) == -1)
827                 return (-1);
828
829         return (0);
830 }
831
832 char
833 *get_apm_adapter()
834 {
835         int fd;
836         struct apm_info info;
837
838         out = (char *)calloc(16, sizeof (char));
839
840         fd = open(APMDEV, O_RDONLY);
841         if (fd < 0) {
842                 strncpy(out, "ERR", 16);
843                 return (out);
844         }
845
846         if (apm_getinfo(fd, &info) != 0) {
847                 close(fd);
848                 strncpy(out, "ERR", 16);
849                 return (out);
850         }
851         close(fd);
852
853         switch (info.ai_acline) {
854                 case 0:
855                         strncpy(out, "off-line", 16);
856                         return (out);
857                         break;
858                 case 1:
859                         if (info.ai_batt_stat == 3) {
860                                 strncpy(out, "charging", 16);
861                                 return (out);
862                         } else {
863                                 strncpy(out, "on-line", 16);
864                                 return (out);
865                         }
866                         break;
867                 default:
868                         strncpy(out, "unknown", 16);
869                         return (out);
870                         break;
871         }
872 }
873
874 char
875 *get_apm_battery_life()
876 {
877         int fd;
878         u_int batt_life;
879         struct apm_info info;
880         char *out;
881
882         out = (char *)calloc(16, sizeof (char));
883
884         fd = open(APMDEV, O_RDONLY);
885         if (fd < 0) {
886                 strncpy(out, "ERR", 16);
887                 return (out);
888         }
889
890         if (apm_getinfo(fd, &info) != 0) {
891                 close(fd);
892                 strncpy(out, "ERR", 16);
893                 return (out);
894         }
895         close(fd);
896
897         batt_life = info.ai_batt_life;
898         if (batt_life == APM_UNKNOWN)
899                 strncpy(out, "unknown", 16);
900         else if (batt_life <= 100) {
901                 snprintf(out, 16, "%d%%", batt_life);
902                 return (out);
903         } else
904                 strncpy(out, "ERR", 16);
905
906         return (out);
907 }
908
909 char
910 *get_apm_battery_time()
911 {
912         int fd;
913         int batt_time;
914         int h, m, s;
915         struct apm_info info;
916         char *out;
917
918         out = (char *)calloc(16, sizeof (char));
919
920         fd = open(APMDEV, O_RDONLY);
921         if (fd < 0) {
922                 strncpy(out, "ERR", 16);
923                 return (out);
924         }
925
926         if (apm_getinfo(fd, &info) != 0) {
927                 close(fd);
928                 strncpy(out, "ERR", 16);
929                 return (out);
930         }
931         close(fd);
932
933         batt_time = info.ai_batt_time;
934
935         if (batt_time == -1)
936                 strncpy(out, "unknown", 16);
937         else {
938                 h = batt_time;
939                 s = h % 60;
940                 h /= 60;
941                 m = h % 60;
942                 h /= 60;
943                 snprintf(out, 16, "%2d:%02d:%02d", h, m, s);
944         }
945
946         return (out);
947 }
948
949 #endif
950
951 void update_entropy (void)
952 {
953      /* mirrorbox: can you do anything equivalent in freebsd? -drphibes. */
954 }
955
956 /* empty stub so conky links */
957 void
958 free_all_processes(void)
959 {
960 }