1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
11 * All rights reserved.
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.
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/>.
27 #include <sys/dkstat.h>
28 #include <sys/param.h>
29 #include <sys/resource.h>
30 #include <sys/socket.h>
32 #include <sys/sysctl.h>
34 #include <sys/types.h>
35 #include <sys/vmmeter.h>
39 #include <net/if_mib.h>
40 #include <net/if_media.h>
41 #include <net/if_var.h>
48 #include <dev/wi/if_wavelan_ieee.h>
49 #include <dev/acpica/acpiio.h>
57 #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
58 #define KELVTOC(x) ((x - 2732) / 10.0)
59 #define MAXSHOWDEVS 16
65 inline void proc_find_top(struct process **cpu, struct process **mem);
67 static short cpu_setup = 0;
68 static struct diskio_stat stats = {
74 .last_read = UINT_MAX,
75 .last_write = UINT_MAX,
78 static int getsysctl(char *name, void *ptr, size_t len)
82 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
86 if (nlen != len && errno == ENOMEM) {
93 struct ifmibdata *data = NULL;
96 static int swapmode(unsigned long *retavail, unsigned long *retfree)
99 unsigned long pagesize = getpagesize();
100 struct kvm_swap swapary[1];
105 #define CONVERT(v) ((quad_t)(v) * (pagesize / 1024))
107 n = kvm_getswapinfo(kd, swapary, 1, 0);
108 if (n < 0 || swapary[0].ksw_total == 0) {
112 *retavail = CONVERT(swapary[0].ksw_total);
113 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
115 n = (int) ((double) swapary[0].ksw_used * 100.0 /
116 (double) swapary[0].ksw_total);
121 void prepare_update()
127 int mib[2] = { CTL_KERN, KERN_BOOTTIME };
128 struct timeval boottime;
130 size_t size = sizeof(boottime);
132 if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
133 && (boottime.tv_sec != 0)) {
135 info.uptime = now - boottime.tv_sec;
137 fprintf(stderr, "Could not get uptime\n");
142 int check_mount(char *s)
144 struct statfs *mntbuf;
147 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
148 for (i = mntsize - 1; i >= 0; i--) {
149 if (strcmp(mntbuf[i].f_mntonname, s) == 0) {
157 void update_meminfo()
159 u_int total_pages, inactive_pages, free_pages;
160 unsigned long swap_avail, swap_free;
162 int pagesize = getpagesize();
164 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages)) {
165 fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_page_count\"\n");
168 if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages)) {
169 fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_free_count\"\n");
172 if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages)) {
173 fprintf(stderr, "Cannot read sysctl \"vm.stats.vm.v_inactive_count\"\n");
176 info.memmax = total_pages * (pagesize >> 10);
177 info.mem = (total_pages - free_pages - inactive_pages) * (pagesize >> 10);
178 info.memeasyfree = info.memfree = info.memmax - info.mem;
180 if ((swapmode(&swap_avail, &swap_free)) >= 0) {
181 info.swapmax = swap_avail;
182 info.swap = (swap_avail - swap_free);
189 void update_net_stats()
193 long long r, t, last_recv, last_trans;
194 struct ifaddrs *ifap, *ifa;
198 delta = current_update_time - last_update_time;
199 if (delta <= 0.0001) {
203 if (getifaddrs(&ifap) < 0) {
207 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
208 ns = get_net_stat((const char *) ifa->ifa_name);
210 if (ifa->ifa_flags & IFF_UP) {
211 struct ifaddrs *iftmp;
214 last_recv = ns->recv;
215 last_trans = ns->trans;
217 if (ifa->ifa_addr->sa_family != AF_LINK) {
221 for (iftmp = ifa->ifa_next;
222 iftmp != NULL && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
223 iftmp = iftmp->ifa_next) {
224 if (iftmp->ifa_addr->sa_family == AF_INET) {
225 memcpy(&(ns->addr), iftmp->ifa_addr,
226 iftmp->ifa_addr->sa_len);
230 ifd = (struct if_data *) ifa->ifa_data;
234 if (r < ns->last_read_recv) {
235 ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r;
237 ns->recv += (r - ns->last_read_recv);
240 ns->last_read_recv = r;
242 if (t < ns->last_read_trans) {
243 ns->trans += ((long long) 4294967295U -
244 ns->last_read_trans) + t;
246 ns->trans += (t - ns->last_read_trans);
249 ns->last_read_trans = t;
251 /* calculate speeds */
252 ns->recv_speed = (ns->recv - last_recv) / delta;
253 ns->trans_speed = (ns->trans - last_trans) / delta;
262 void update_total_processes()
266 kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
268 info.procs = n_processes;
271 void update_running_processes()
273 struct kinfo_proc *p;
277 p = kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
278 for (i = 0; i < n_processes; i++) {
279 #if (__FreeBSD__ < 5) && (__FreeBSD_kernel__ < 5)
280 if (p[i].kp_proc.p_stat == SRUN) {
282 if (p[i].ki_stat == SRUN) {
288 info.run_procs = cnt;
291 struct cpu_load_struct {
292 unsigned long load[5];
295 struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
296 long cpu_used, oldtotal, oldused;
300 /* int cpu_count = 0; */
302 /* XXX: FreeBSD doesn't allow to get per CPU load stats on SMP machines.
303 * It's possible to get a CPU count, but as we fulfill only
304 * info.cpu_usage[0], it's better to report there's only one CPU.
305 * It should fix some bugs (e.g. cpugraph) */
307 if (GETSYSCTL("hw.ncpu", cpu_count) == 0) {
308 info.cpu_count = cpu_count;
313 info.cpu_usage = malloc(info.cpu_count * sizeof(float));
314 if (info.cpu_usage == NULL) {
319 /* XXX: SMP support */
320 void update_cpu_usage()
323 long cp_time[CPUSTATES];
324 size_t len = sizeof(cp_time);
326 /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
327 if ((cpu_setup == 0) || (!info.cpu_usage)) {
332 if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
333 fprintf(stderr, "Cannot get kern.cp_time");
336 fresh.load[0] = cp_time[CP_USER];
337 fresh.load[1] = cp_time[CP_NICE];
338 fresh.load[2] = cp_time[CP_SYS];
339 fresh.load[3] = cp_time[CP_IDLE];
340 fresh.load[4] = cp_time[CP_IDLE];
342 used = fresh.load[0] + fresh.load[1] + fresh.load[2];
343 total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
345 if ((total - oldtotal) != 0) {
346 info.cpu_usage[0] = ((double) (used - oldused)) /
347 (double) (total - oldtotal);
349 info.cpu_usage[0] = 0;
356 void update_load_average()
362 info.loadavg[0] = (double) v[0];
363 info.loadavg[1] = (double) v[1];
364 info.loadavg[2] = (double) v[2];
367 double get_acpi_temperature(int fd)
371 if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
373 "Cannot read sysctl \"hw.acpi.thermal.tz0.temperature\"\n");
377 return KELVTOC(temp);
380 static void get_battery_stats(int *battime, int *batcapacity, int *batstate, int *ac) {
381 if (battime && GETSYSCTL("hw.acpi.battery.time", *battime)) {
382 fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.time\"\n");
384 if (batcapacity && GETSYSCTL("hw.acpi.battery.life", *batcapacity)) {
385 fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.life\"\n");
387 if (batstate && GETSYSCTL("hw.acpi.battery.state", *batstate)) {
388 fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.state\"\n");
390 if (ac && GETSYSCTL("hw.acpi.acline", *ac)) {
391 fprintf(stderr, "Cannot read sysctl \"hw.acpi.acline\"\n");
395 void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
397 int battime, batcapacity, batstate, ac;
398 char battery_status[64];
399 char battery_time[64];
401 get_battery_stats(&battime, &batcapacity, &batstate, &ac);
403 if (batstate != 1 && batstate != 2 && batstate != 0 && batstate != 7)
404 fprintf(stderr, "Unknown battery state %d!\n", batstate);
405 else if (batstate != 1 && ac == 0)
406 fprintf(stderr, "Battery charging while not on AC!\n");
407 else if (batstate == 1 && ac == 1)
408 fprintf(stderr, "Battery discharing while on AC!\n");
412 if (batstate == 1 && battime != -1)
413 snprintf(buf, n, "%d:%2.2d", battime / 60, battime % 60);
416 if (batstate == 1) // Discharging
417 snprintf(buf, n, "remaining %d%%", batcapacity);
419 snprintf(buf, n, batstate == 2 ? "charging (%d%%)" :
420 (batstate == 7 ? "absent/on AC" : "charged (%d%%)"),
424 fprintf(stderr, "Unknown requested battery stat %d\n", item);
428 static int check_bat(const char *bat)
430 int batnum, numbatts;
432 if (GETSYSCTL("hw.acpi.battery.units", numbatts)) {
433 fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.units\"\n");
437 fprintf(stderr, "No battery unit detected\n");
440 if (!bat || (batnum = strtol(bat, &endptr, 10)) < 0 ||
441 bat == endptr || batnum > numbatts) {
442 fprintf(stderr, "Wrong battery unit requested\n", bat);
448 int get_battery_perct(const char *bat)
450 union acpi_battery_ioctl_arg battio;
451 int batnum, numbatts, acpifd;
452 int designcap, lastfulcap, batperct;
454 if ((battio.unit = batnum = check_bat(bat)) < 0)
456 if ((acpifd = open("/dev/acpi", O_RDONLY)) < 0) {
457 fprintf(stderr, "Can't open ACPI device\n");
460 if (ioctl(acpifd, ACPIIO_BATT_GET_BIF, &battio) == -1) {
461 fprintf(stderr, "Unable to get info for battery unit %d\n", batnum);
465 designcap = battio.bif.dcap;
466 lastfulcap = battio.bif.lfcap;
467 batperct = (designcap > 0 && lastfulcap > 0) ?
468 (int) (((float) lastfulcap / designcap) * 100) : 0;
469 return batperct > 100 ? 100 : batperct;
472 int get_battery_perct_bar(const char *bar)
474 int batperct = get_battery_perct(bar);
475 return (int)(batperct * 2.56 - 1);
478 int open_acpi_temperature(const char *name)
480 /* Not applicable for FreeBSD. */
484 void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
488 if (!p_client_buffer || client_buffer_size <= 0) {
492 if (GETSYSCTL("hw.acpi.acline", state)) {
493 fprintf(stderr, "Cannot read sysctl \"hw.acpi.acline\"\n");
498 strncpy(p_client_buffer, "Running on AC Power", client_buffer_size);
500 strncpy(p_client_buffer, "Running on battery", client_buffer_size);
504 void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
506 /* not implemented */
507 if (p_client_buffer && client_buffer_size > 0) {
508 memset(p_client_buffer, 0, client_buffer_size);
512 void get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
514 /* not implemented */
515 if (p_client_buffer && client_buffer_size > 0) {
516 memset(p_client_buffer, 0, client_buffer_size);
520 void get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size)
522 /* not implemented */
523 if (p_client_buffer && client_buffer_size > 0) {
524 memset(p_client_buffer, 0, client_buffer_size);
528 /* rdtsc() and get_freq_dynamic() copied from linux.c */
530 #if defined(__i386) || defined(__x86_64)
531 __inline__ unsigned long long int rdtsc()
533 unsigned long long int x;
535 __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
540 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
541 void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
542 const char *p_format, int divisor)
544 #if defined(__i386) || defined(__x86_64)
546 struct timeval tvstart, tvstop;
547 unsigned long long cycles[2]; /* gotta be 64 bit */
548 unsigned int microseconds; /* total time taken */
550 memset(&tz, 0, sizeof(tz));
552 /* get this function in cached memory */
553 gettimeofday(&tvstart, &tz);
555 gettimeofday(&tvstart, &tz);
557 /* we don't trust that this is any specific length of time */
560 gettimeofday(&tvstop, &tz);
561 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
562 (tvstop.tv_usec - tvstart.tv_usec);
564 snprintf(p_client_buffer, client_buffer_size, p_format,
565 (float) ((cycles[1] - cycles[0]) / microseconds) / divisor);
567 get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
572 char get_freq(char *p_client_buffer, size_t client_buffer_size, const char *p_format,
573 int divisor, unsigned int cpu)
578 freq_sysctl = (char *) calloc(16, sizeof(char));
579 if (freq_sysctl == NULL) {
583 snprintf(freq_sysctl, 16, "dev.cpu.%d.freq", (cpu - 1));
585 if (!p_client_buffer || client_buffer_size <= 0 || !p_format
590 if (GETSYSCTL(freq_sysctl, freq) == 0) {
591 snprintf(p_client_buffer, client_buffer_size, p_format,
592 (float) freq / divisor);
594 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
603 proc_find_top(info.cpu, info.memu);
607 void update_wifi_stats()
609 struct ifreq ifr; /* interface stats */
612 struct ifaddrs *ifap, *ifa;
613 struct ifmediareq ifmr;
616 /* Get iface table */
617 if (getifaddrs(&ifap) < 0) {
621 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
622 ns = get_net_stat((const char *) ifa->ifa_name);
624 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
627 bzero(&ifmr, sizeof(ifmr));
628 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
629 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
634 /* We can monitor only wireless interfaces
635 * which are not in hostap mode */
636 if ((ifmr.ifm_active & IFM_IEEE80211)
637 && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
639 bzero(&ifr, sizeof(ifr));
640 strlcpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);
641 wireq.wi_type = WI_RID_COMMS_QUALITY;
642 wireq.wi_len = WI_MAX_DATALEN;
643 ifr.ifr_data = (void *) &wireq;
645 if (ioctl(s, SIOCGWAVELAN, (caddr_t) &ifr) < 0) {
646 perror("ioctl (getting wi status)");
650 /* wi_val[0] = quality
652 * wi_val[2] = noise */
653 ns->linkstatus = (int) wireq.wi_val[1];
663 int devs_count, num_selected, num_selections;
664 struct device_selection *dev_select = NULL;
665 long select_generation;
667 static struct statinfo statinfo_cur;
668 struct diskio_stat *cur;
670 bzero(&statinfo_cur, sizeof(statinfo_cur));
671 statinfo_cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
672 stats.current = stats.current_read = stats.current_write = 0;
674 if (devstat_getdevs(NULL, &statinfo_cur) < 0)
677 devs_count = statinfo_cur.dinfo->numdevs;
678 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
679 &select_generation, statinfo_cur.dinfo->generation,
680 statinfo_cur.dinfo->devices, devs_count, NULL, 0, NULL, 0,
681 DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
682 for (dn = 0; dn < devs_count; ++dn) {
686 di = dev_select[dn].position;
687 dev = &statinfo_cur.dinfo->devices[di];
689 for (cur = stats.next; cur; cur = cur->next) {
690 if (cur->dev && !strcmp(dev_select[dn].device_name, cur->dev)) {
691 cur->current = (dev->bytes[DEVSTAT_READ] +
692 dev->bytes[DEVSTAT_WRITE] - cur->last) / 1024;
693 cur->current_read = (dev->bytes[DEVSTAT_READ] -
694 cur->last_read) / 1024;
695 cur->current_write = (dev->bytes[DEVSTAT_WRITE] -
696 cur->last_write) / 1024;
697 if (dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE] <
701 if (dev->bytes[DEVSTAT_READ] < cur->last_read) {
702 cur->current_read = 0;
703 cur->current = cur->current_write;
705 if (dev->bytes[DEVSTAT_WRITE] < cur->last_write) {
706 cur->current_write = 0;
707 cur->current = cur->current_read;
709 cur->last = dev->bytes[DEVSTAT_READ] +
710 dev->bytes[DEVSTAT_WRITE];
711 cur->last_read = dev->bytes[DEVSTAT_READ];
712 cur->last_write = dev->bytes[DEVSTAT_WRITE];
720 free(statinfo_cur.dinfo);
723 void clear_diskio_stats()
725 struct diskio_stat *cur;
728 stats.next = stats.next->next;
733 struct diskio_stat *prepare_diskio_stat(const char *s)
735 struct diskio_stat *new = 0;
738 char device[text_buffer_size], fbuf[text_buffer_size];
740 /* lookup existing or get new */
741 struct diskio_stat *cur = &stats;
748 if (!strcmp(cur->dev, s))
753 if (!(cur->next = calloc(1, sizeof(struct diskio_stat)))) {
754 ERR("out of memory allocating new disk stats struct");
758 cur->last = cur->last_read = cur->last_write = UINT_MAX;
759 if (strncmp(s, "/dev/", 5) == 0) {
760 // supplied a /dev/device arg, so cut off the /dev part
761 cur->dev = strndup(s + 5, text_buffer_size);
763 cur->dev = strndup(s, text_buffer_size);
766 * check that device actually exists
768 snprintf(device, text_buffer_size, "/dev/%s", new->dev);
770 if (stat(device, &sb)) {
771 ERR("diskio device '%s' does not exist", s);
777 /* While topless is obviously better, top is also not bad. */
779 int comparecpu(const void *a, const void *b)
781 if (((struct process *)a)->amount > ((struct process *)b)->amount) {
783 } else if (((struct process *)a)->amount < ((struct process *)b)->amount) {
790 int comparemem(const void *a, const void *b)
792 if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem) {
794 } else if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem) {
801 inline void proc_find_top(struct process **cpu, struct process **mem)
803 struct kinfo_proc *p;
806 struct process *processes;
810 /* we get total pages count again to be sure it is up to date */
811 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0) {
812 CRIT_ERR("Cannot read sysctl \"vm.stats.vm.v_page_count\"");
815 p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
816 processes = malloc(n_processes * sizeof(struct process));
818 for (i = 0; i < n_processes; i++) {
819 if (!((p[i].ki_flag & P_SYSTEM)) && p[i].ki_comm != NULL) {
820 processes[j].pid = p[i].ki_pid;
821 processes[j].name = strndup(p[i].ki_comm, text_buffer_size);
822 processes[j].amount = 100.0 * p[i].ki_pctcpu / FSCALE;
823 processes[j].totalmem = (float) (p[i].ki_rssize /
824 (float) total_pages) * 100.0;
825 processes[j].vsize = p[i].ki_size;
826 processes[j].rss = (p[i].ki_rssize * getpagesize());
831 qsort(processes, j - 1, sizeof(struct process), comparemem);
832 for (i = 0; i < 10 && i < n_processes; i++) {
833 struct process *tmp, *ttmp;
835 tmp = malloc(sizeof(struct process));
836 tmp->pid = processes[i].pid;
837 tmp->amount = processes[i].amount;
838 tmp->totalmem = processes[i].totalmem;
839 tmp->name = strndup(processes[i].name, text_buffer_size);
840 tmp->rss = processes[i].rss;
841 tmp->vsize = processes[i].vsize;
851 qsort(processes, j - 1, sizeof(struct process), comparecpu);
852 for (i = 0; i < 10 && i < n_processes; i++) {
853 struct process *tmp, *ttmp;
855 tmp = malloc(sizeof(struct process));
856 tmp->pid = processes[i].pid;
857 tmp->amount = processes[i].amount;
858 tmp->totalmem = processes[i].totalmem;
859 tmp->name = strndup(processes[i].name, text_buffer_size);
860 tmp->rss = processes[i].rss;
861 tmp->vsize = processes[i].vsize;
871 #if defined(FREEBSD_DEBUG)
872 printf("=====\nmem\n");
873 for (i = 0; i < 10; i++) {
874 printf("%d: %s(%d) %.2f %ld %ld\n", i, mem[i]->name,
875 mem[i]->pid, mem[i]->totalmem, mem[i]->vsize, mem[i]->rss);
879 for (i = 0; i < j; i++) {
880 free(processes[i].name);
885 #if defined(i386) || defined(__i386__)
886 #define APMDEV "/dev/apm"
887 #define APM_UNKNOWN 255
889 int apm_getinfo(int fd, apm_info_t aip)
891 if (ioctl(fd, APMIO_GETINFO, aip) == -1) {
898 char *get_apm_adapter()
901 struct apm_info info;
904 out = (char *) calloc(16, sizeof(char));
906 fd = open(APMDEV, O_RDONLY);
908 strncpy(out, "ERR", 16);
912 if (apm_getinfo(fd, &info) != 0) {
914 strncpy(out, "ERR", 16);
919 switch (info.ai_acline) {
921 strncpy(out, "off-line", 16);
925 if (info.ai_batt_stat == 3) {
926 strncpy(out, "charging", 16);
929 strncpy(out, "on-line", 16);
934 strncpy(out, "unknown", 16);
940 char *get_apm_battery_life()
944 struct apm_info info;
947 out = (char *) calloc(16, sizeof(char));
949 fd = open(APMDEV, O_RDONLY);
951 strncpy(out, "ERR", 16);
955 if (apm_getinfo(fd, &info) != 0) {
957 strncpy(out, "ERR", 16);
962 batt_life = info.ai_batt_life;
963 if (batt_life == APM_UNKNOWN) {
964 strncpy(out, "unknown", 16);
965 } else if (batt_life <= 100) {
966 snprintf(out, 16, "%d%%", batt_life);
969 strncpy(out, "ERR", 16);
975 char *get_apm_battery_time()
980 struct apm_info info;
983 out = (char *) calloc(16, sizeof(char));
985 fd = open(APMDEV, O_RDONLY);
987 strncpy(out, "ERR", 16);
991 if (apm_getinfo(fd, &info) != 0) {
993 strncpy(out, "ERR", 16);
998 batt_time = info.ai_batt_time;
1000 if (batt_time == -1) {
1001 strncpy(out, "unknown", 16);
1008 snprintf(out, 16, "%2d:%02d:%02d", h, m, s);
1016 void get_battery_short_status(char *buffer, unsigned int n, const char *bat)
1018 get_battery_stuff(buffer, n, bat, BATTERY_STATUS);
1019 if (0 == strncmp("charging", buffer, 8)) {
1021 memmove(buffer + 1, buffer + 8, n - 8);
1022 } else if (0 == strncmp("discharging", buffer, 11)) {
1024 memmove(buffer + 1, buffer + 11, n - 11);
1025 } else if (0 == strncmp("absent/on AC", buffer, 12)) {
1027 memmove(buffer + 1, buffer + 12, n - 12);
1031 void update_entropy(void)
1033 /* Not applicable for FreeBSD as it uses the yarrow prng. */
1036 /* empty stub so conky links */
1037 void free_all_processes(void)