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/ioctl.h>
28 #include <sys/dkstat.h>
29 #include <sys/param.h>
30 #include <sys/resource.h>
31 #include <sys/socket.h>
33 #include <sys/sysctl.h>
35 #include <sys/types.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;
399 get_battery_stats(&battime, &batcapacity, &batstate, &ac);
401 if (batstate != 1 && batstate != 2 && batstate != 0 && batstate != 7)
402 fprintf(stderr, "Unknown battery state %d!\n", batstate);
403 else if (batstate != 1 && ac == 0)
404 fprintf(stderr, "Battery charging while not on AC!\n");
405 else if (batstate == 1 && ac == 1)
406 fprintf(stderr, "Battery discharing while on AC!\n");
410 if (batstate == 1 && battime != -1)
411 snprintf(buf, n, "%d:%2.2d", battime / 60, battime % 60);
414 if (batstate == 1) // Discharging
415 snprintf(buf, n, "remaining %d%%", batcapacity);
417 snprintf(buf, n, batstate == 2 ? "charging (%d%%)" :
418 (batstate == 7 ? "absent/on AC" : "charged (%d%%)"),
422 fprintf(stderr, "Unknown requested battery stat %d\n", item);
426 static int check_bat(const char *bat)
428 int batnum, numbatts;
430 if (GETSYSCTL("hw.acpi.battery.units", numbatts)) {
431 fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.units\"\n");
435 fprintf(stderr, "No battery unit detected\n");
438 if (!bat || (batnum = strtol(bat, &endptr, 10)) < 0 ||
439 bat == endptr || batnum > numbatts) {
440 fprintf(stderr, "Wrong battery unit %s requested\n", bat ? bat : "");
446 int get_battery_perct(const char *bat)
448 union acpi_battery_ioctl_arg battio;
450 int designcap, lastfulcap, batperct;
452 if ((battio.unit = batnum = check_bat(bat)) < 0)
454 if ((acpifd = open("/dev/acpi", O_RDONLY)) < 0) {
455 fprintf(stderr, "Can't open ACPI device\n");
458 if (ioctl(acpifd, ACPIIO_BATT_GET_BIF, &battio) == -1) {
459 fprintf(stderr, "Unable to get info for battery unit %d\n", batnum);
463 designcap = battio.bif.dcap;
464 lastfulcap = battio.bif.lfcap;
465 batperct = (designcap > 0 && lastfulcap > 0) ?
466 (int) (((float) lastfulcap / designcap) * 100) : 0;
467 return batperct > 100 ? 100 : batperct;
470 int get_battery_perct_bar(const char *bar)
472 int batperct = get_battery_perct(bar);
473 return (int)(batperct * 2.56 - 1);
476 int open_acpi_temperature(const char *name)
478 /* Not applicable for FreeBSD. */
482 void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
486 if (!p_client_buffer || client_buffer_size <= 0) {
490 if (GETSYSCTL("hw.acpi.acline", state)) {
491 fprintf(stderr, "Cannot read sysctl \"hw.acpi.acline\"\n");
496 strncpy(p_client_buffer, "Running on AC Power", client_buffer_size);
498 strncpy(p_client_buffer, "Running on battery", client_buffer_size);
502 void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
504 /* not implemented */
505 if (p_client_buffer && client_buffer_size > 0) {
506 memset(p_client_buffer, 0, client_buffer_size);
510 void get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
512 /* not implemented */
513 if (p_client_buffer && client_buffer_size > 0) {
514 memset(p_client_buffer, 0, client_buffer_size);
518 void get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size)
520 /* not implemented */
521 if (p_client_buffer && client_buffer_size > 0) {
522 memset(p_client_buffer, 0, client_buffer_size);
526 /* rdtsc() and get_freq_dynamic() copied from linux.c */
528 #if defined(__i386) || defined(__x86_64)
529 __inline__ unsigned long long int rdtsc()
531 unsigned long long int x;
533 __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
538 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
539 void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
540 const char *p_format, int divisor)
542 #if defined(__i386) || defined(__x86_64)
544 struct timeval tvstart, tvstop;
545 unsigned long long cycles[2]; /* gotta be 64 bit */
546 unsigned int microseconds; /* total time taken */
548 memset(&tz, 0, sizeof(tz));
550 /* get this function in cached memory */
551 gettimeofday(&tvstart, &tz);
553 gettimeofday(&tvstart, &tz);
555 /* we don't trust that this is any specific length of time */
558 gettimeofday(&tvstop, &tz);
559 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
560 (tvstop.tv_usec - tvstart.tv_usec);
562 snprintf(p_client_buffer, client_buffer_size, p_format,
563 (float) ((cycles[1] - cycles[0]) / microseconds) / divisor);
565 get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
570 char get_freq(char *p_client_buffer, size_t client_buffer_size, const char *p_format,
571 int divisor, unsigned int cpu)
576 freq_sysctl = (char *) calloc(16, sizeof(char));
577 if (freq_sysctl == NULL) {
581 snprintf(freq_sysctl, 16, "dev.cpu.%d.freq", (cpu - 1));
583 if (!p_client_buffer || client_buffer_size <= 0 || !p_format
588 if (GETSYSCTL(freq_sysctl, freq) == 0) {
589 snprintf(p_client_buffer, client_buffer_size, p_format,
590 (float) freq / divisor);
592 snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
601 proc_find_top(info.cpu, info.memu);
605 void update_wifi_stats()
607 struct ifreq ifr; /* interface stats */
610 struct ifaddrs *ifap, *ifa;
611 struct ifmediareq ifmr;
614 /* Get iface table */
615 if (getifaddrs(&ifap) < 0) {
619 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
620 ns = get_net_stat((const char *) ifa->ifa_name);
622 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
625 bzero(&ifmr, sizeof(ifmr));
626 strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
627 if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
632 /* We can monitor only wireless interfaces
633 * which are not in hostap mode */
634 if ((ifmr.ifm_active & IFM_IEEE80211)
635 && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
637 bzero(&ifr, sizeof(ifr));
638 strlcpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ);
639 wireq.wi_type = WI_RID_COMMS_QUALITY;
640 wireq.wi_len = WI_MAX_DATALEN;
641 ifr.ifr_data = (void *) &wireq;
643 if (ioctl(s, SIOCGWAVELAN, (caddr_t) &ifr) < 0) {
644 perror("ioctl (getting wi status)");
648 /* wi_val[0] = quality
650 * wi_val[2] = noise */
651 ns->linkstatus = (int) wireq.wi_val[1];
661 int devs_count, num_selected, num_selections;
662 struct device_selection *dev_select = NULL;
663 long select_generation;
665 static struct statinfo statinfo_cur;
666 struct diskio_stat *cur;
668 bzero(&statinfo_cur, sizeof(statinfo_cur));
669 statinfo_cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
670 stats.current = stats.current_read = stats.current_write = 0;
672 if (devstat_getdevs(NULL, &statinfo_cur) < 0)
675 devs_count = statinfo_cur.dinfo->numdevs;
676 if (devstat_selectdevs(&dev_select, &num_selected, &num_selections,
677 &select_generation, statinfo_cur.dinfo->generation,
678 statinfo_cur.dinfo->devices, devs_count, NULL, 0, NULL, 0,
679 DS_SELECT_ONLY, MAXSHOWDEVS, 1) >= 0) {
680 for (dn = 0; dn < devs_count; ++dn) {
684 di = dev_select[dn].position;
685 dev = &statinfo_cur.dinfo->devices[di];
687 for (cur = stats.next; cur; cur = cur->next) {
688 if (cur->dev && !strcmp(dev_select[dn].device_name, cur->dev)) {
689 cur->current = (dev->bytes[DEVSTAT_READ] +
690 dev->bytes[DEVSTAT_WRITE] - cur->last) / 1024;
691 cur->current_read = (dev->bytes[DEVSTAT_READ] -
692 cur->last_read) / 1024;
693 cur->current_write = (dev->bytes[DEVSTAT_WRITE] -
694 cur->last_write) / 1024;
695 if (dev->bytes[DEVSTAT_READ] + dev->bytes[DEVSTAT_WRITE] <
699 if (dev->bytes[DEVSTAT_READ] < cur->last_read) {
700 cur->current_read = 0;
701 cur->current = cur->current_write;
703 if (dev->bytes[DEVSTAT_WRITE] < cur->last_write) {
704 cur->current_write = 0;
705 cur->current = cur->current_read;
707 cur->last = dev->bytes[DEVSTAT_READ] +
708 dev->bytes[DEVSTAT_WRITE];
709 cur->last_read = dev->bytes[DEVSTAT_READ];
710 cur->last_write = dev->bytes[DEVSTAT_WRITE];
718 free(statinfo_cur.dinfo);
721 void clear_diskio_stats()
723 struct diskio_stat *cur;
726 stats.next = stats.next->next;
731 struct diskio_stat *prepare_diskio_stat(const char *s)
733 struct diskio_stat *new = 0;
736 char device[text_buffer_size], fbuf[text_buffer_size];
738 /* lookup existing or get new */
739 struct diskio_stat *cur = &stats;
746 if (!strcmp(cur->dev, s))
751 if (!(cur->next = calloc(1, sizeof(struct diskio_stat)))) {
752 ERR("out of memory allocating new disk stats struct");
756 cur->last = cur->last_read = cur->last_write = UINT_MAX;
757 if (strncmp(s, "/dev/", 5) == 0) {
758 // supplied a /dev/device arg, so cut off the /dev part
759 cur->dev = strndup(s + 5, text_buffer_size);
761 cur->dev = strndup(s, text_buffer_size);
764 * check that device actually exists
766 snprintf(device, text_buffer_size, "/dev/%s", new->dev);
768 if (stat(device, &sb)) {
769 ERR("diskio device '%s' does not exist", s);
775 /* While topless is obviously better, top is also not bad. */
777 int comparecpu(const void *a, const void *b)
779 if (((struct process *)a)->amount > ((struct process *)b)->amount) {
781 } else if (((struct process *)a)->amount < ((struct process *)b)->amount) {
788 int comparemem(const void *a, const void *b)
790 if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem) {
792 } else if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem) {
799 inline void proc_find_top(struct process **cpu, struct process **mem)
801 struct kinfo_proc *p;
804 struct process *processes;
808 /* we get total pages count again to be sure it is up to date */
809 if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0) {
810 CRIT_ERR("Cannot read sysctl \"vm.stats.vm.v_page_count\"");
813 p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
814 processes = malloc(n_processes * sizeof(struct process));
816 for (i = 0; i < n_processes; i++) {
817 if (!((p[i].ki_flag & P_SYSTEM)) && p[i].ki_comm != NULL) {
818 processes[j].pid = p[i].ki_pid;
819 processes[j].name = strndup(p[i].ki_comm, text_buffer_size);
820 processes[j].amount = 100.0 * p[i].ki_pctcpu / FSCALE;
821 processes[j].totalmem = (float) (p[i].ki_rssize /
822 (float) total_pages) * 100.0;
823 processes[j].vsize = p[i].ki_size;
824 processes[j].rss = (p[i].ki_rssize * getpagesize());
829 qsort(processes, j - 1, sizeof(struct process), comparemem);
830 for (i = 0; i < 10 && i < n_processes; i++) {
831 struct process *tmp, *ttmp;
833 tmp = malloc(sizeof(struct process));
834 tmp->pid = processes[i].pid;
835 tmp->amount = processes[i].amount;
836 tmp->totalmem = processes[i].totalmem;
837 tmp->name = strndup(processes[i].name, text_buffer_size);
838 tmp->rss = processes[i].rss;
839 tmp->vsize = processes[i].vsize;
849 qsort(processes, j - 1, sizeof(struct process), comparecpu);
850 for (i = 0; i < 10 && i < n_processes; i++) {
851 struct process *tmp, *ttmp;
853 tmp = malloc(sizeof(struct process));
854 tmp->pid = processes[i].pid;
855 tmp->amount = processes[i].amount;
856 tmp->totalmem = processes[i].totalmem;
857 tmp->name = strndup(processes[i].name, text_buffer_size);
858 tmp->rss = processes[i].rss;
859 tmp->vsize = processes[i].vsize;
869 #if defined(FREEBSD_DEBUG)
870 printf("=====\nmem\n");
871 for (i = 0; i < 10; i++) {
872 printf("%d: %s(%d) %.2f %ld %ld\n", i, mem[i]->name,
873 mem[i]->pid, mem[i]->totalmem, mem[i]->vsize, mem[i]->rss);
877 for (i = 0; i < j; i++) {
878 free(processes[i].name);
883 #if defined(i386) || defined(__i386__)
884 #define APMDEV "/dev/apm"
885 #define APM_UNKNOWN 255
887 int apm_getinfo(int fd, apm_info_t aip)
889 if (ioctl(fd, APMIO_GETINFO, aip) == -1) {
896 char *get_apm_adapter()
899 struct apm_info info;
902 out = (char *) calloc(16, sizeof(char));
904 fd = open(APMDEV, O_RDONLY);
906 strncpy(out, "ERR", 16);
910 if (apm_getinfo(fd, &info) != 0) {
912 strncpy(out, "ERR", 16);
917 switch (info.ai_acline) {
919 strncpy(out, "off-line", 16);
923 if (info.ai_batt_stat == 3) {
924 strncpy(out, "charging", 16);
927 strncpy(out, "on-line", 16);
932 strncpy(out, "unknown", 16);
938 char *get_apm_battery_life()
942 struct apm_info info;
945 out = (char *) calloc(16, sizeof(char));
947 fd = open(APMDEV, O_RDONLY);
949 strncpy(out, "ERR", 16);
953 if (apm_getinfo(fd, &info) != 0) {
955 strncpy(out, "ERR", 16);
960 batt_life = info.ai_batt_life;
961 if (batt_life == APM_UNKNOWN) {
962 strncpy(out, "unknown", 16);
963 } else if (batt_life <= 100) {
964 snprintf(out, 16, "%d%%", batt_life);
967 strncpy(out, "ERR", 16);
973 char *get_apm_battery_time()
978 struct apm_info info;
981 out = (char *) calloc(16, sizeof(char));
983 fd = open(APMDEV, O_RDONLY);
985 strncpy(out, "ERR", 16);
989 if (apm_getinfo(fd, &info) != 0) {
991 strncpy(out, "ERR", 16);
996 batt_time = info.ai_batt_time;
998 if (batt_time == -1) {
999 strncpy(out, "unknown", 16);
1006 snprintf(out, 16, "%2d:%02d:%02d", h, m, s);
1014 void get_battery_short_status(char *buffer, unsigned int n, const char *bat)
1016 get_battery_stuff(buffer, n, bat, BATTERY_STATUS);
1017 if (0 == strncmp("charging", buffer, 8)) {
1019 memmove(buffer + 1, buffer + 8, n - 8);
1020 } else if (0 == strncmp("discharging", buffer, 11)) {
1022 memmove(buffer + 1, buffer + 11, n - 11);
1023 } else if (0 == strncmp("absent/on AC", buffer, 12)) {
1025 memmove(buffer + 1, buffer + 12, n - 12);
1029 void update_entropy(void)
1031 /* Not applicable for FreeBSD as it uses the yarrow prng. */
1034 /* empty stub so conky links */
1035 void free_all_processes(void)