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