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