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