1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
3 * Conky, a system monitor, based on torsmo
5 * Any original torsmo code is licensed under the BSD license
7 * All code written since the fork of torsmo is licensed under the GPL
9 * Please see COPYING for details
11 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14 * All rights reserved.
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.
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/>.
28 * vim: ts=4 sw=4 noet ai cindent syntax=c
39 #include <sys/ioctl.h>
41 #include <netinet/in.h>
47 /* check for OS and include appropriate headers */
48 #if defined(__linux__)
50 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
52 #elif defined(__OpenBSD__)
56 /* folds a string over top of itself, like so:
58 * if start is "blah", and you call it with count = 1, the result will be "lah"
60 void strfold(char *start, int count)
63 for (curplace = start + count; *curplace != 0; curplace++) {
64 *(curplace - count) = *curplace;
66 *(curplace - count) = 0;
70 // use our own strndup() if it's not available
71 char *strndup(const char *s, size_t n)
74 char *ret = malloc(n + 1);
82 #endif /* HAVE_STRNDUP */
84 void update_uname(void)
94 return tv.tv_sec + (tv.tv_usec / 1000000.0);
97 /* Converts '~/...' paths to '/home/blah/...' assumes that 'dest' is at least
98 * DEFAULT_TEXT_BUFFER_SIZE. It's similar to variable_substitute, except only
99 * cheques for $HOME and ~/ in path */
100 void to_real_path(char *dest, const char *source)
102 char tmp[DEFAULT_TEXT_BUFFER_SIZE];
103 if (sscanf(source, "~/%s", tmp) || sscanf(source, "$HOME/%s", tmp)) {
104 char *homedir = getenv("HOME");
106 snprintf(dest, DEFAULT_TEXT_BUFFER_SIZE, "%s/%s", homedir, tmp);
108 NORM_ERR("$HOME environment variable doesn't exist");
109 strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
111 } else if (dest != source) { //see changelog 2009-06-29 if you doubt that this check is necessary
112 strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
116 int open_fifo(const char *file, int *reported)
118 char path[DEFAULT_TEXT_BUFFER_SIZE];
121 to_real_path(path, file);
122 fd = open(file, O_RDONLY | O_NONBLOCK);
125 if (!reported || *reported == 0) {
126 NORM_ERR("can't open %s: %s", file, strerror(errno));
137 FILE *open_file(const char *file, int *reported)
139 char path[DEFAULT_TEXT_BUFFER_SIZE];
142 to_real_path(path, file);
143 fp = fopen(file, "r");
146 if (!reported || *reported == 0) {
147 NORM_ERR("can't open %s: %s", file, strerror(errno));
158 void variable_substitute(const char *s, char *dest, unsigned int n)
160 while (*s && n > 1) {
168 /* variable is either $foo or ${foo} */
172 while (*s && *s != '}') {
177 while (*s && (isalnum((int) *s) || *s == '_')) {
182 /* copy variable to buffer and look it up */
183 len = (s - a > 255) ? 255 : (s - a);
184 strncpy(buf, a, len);
194 /* add var to dest */
199 strncpy(dest, var, len);
214 /* network interface stuff */
216 static struct net_stat netstats[16];
218 struct net_stat *get_net_stat(const char *dev, void *free_at_crash1, void *free_at_crash2)
226 /* find interface stat */
227 for (i = 0; i < 16; i++) {
228 if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
233 /* wasn't found? add it */
234 for (i = 0; i < 16; i++) {
235 if (netstats[i].dev == 0) {
236 netstats[i].dev = strndup(dev, text_buffer_size);
241 CRIT_ERR(free_at_crash1, free_at_crash2, "too many interfaces used (limit is 16)");
245 void clear_net_stats(void)
248 for (i = 0; i < 16; i++) {
249 if (netstats[i].dev) {
250 free(netstats[i].dev);
253 memset(netstats, 0, sizeof(netstats));
256 /* We should check if this is ok with OpenBSD and NetBSD as well. */
257 int interface_up(const char *dev)
262 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
263 CRIT_ERR(NULL, NULL, "could not create sockfd");
266 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
267 if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
268 /* if device does not exist, treat like not up */
269 if (errno != ENODEV && errno != ENXIO)
270 perror("SIOCGIFFLAGS");
274 if (!(ifr.ifr_flags & IFF_UP)) /* iface is not up */
276 if (ifup_strictness == IFUP_UP)
279 if (!(ifr.ifr_flags & IFF_RUNNING))
281 if (ifup_strictness == IFUP_LINK)
284 if (ioctl(fd, SIOCGIFADDR, &ifr)) {
285 perror("SIOCGIFADDR");
288 if (((struct sockaddr_in *)&(ifr.ifr_ifru.ifru_addr))->sin_addr.s_addr)
299 void free_dns_data(void)
302 struct dns_data *data = &info.nameserver_info;
303 for (i = 0; i < data->nscount; i++)
304 free(data->ns_list[i]);
307 memset(data, 0, sizeof(struct dns_data));
310 //static double last_dns_update;
312 void update_dns_data(void)
316 struct dns_data *data = &info.nameserver_info;
318 /* maybe updating too often causes higher load because of /etc lying on a real FS
319 if (current_update_time - last_dns_update < 10.0)
322 last_dns_update = current_update_time;
327 if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
330 if (fgets(line, 255, fp) == NULL) {
333 if (!strncmp(line, "nameserver ", 11)) {
334 line[strlen(line) - 1] = '\0'; // remove trailing newline
336 data->ns_list = realloc(data->ns_list, data->nscount * sizeof(char *));
337 data->ns_list[data->nscount - 1] = strndup(line + 11, text_buffer_size);
343 void format_seconds(char *buf, unsigned int n, long seconds)
348 days = seconds / 86400;
350 hours = seconds / 3600;
352 minutes = seconds / 60;
356 snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
358 snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
362 void format_seconds_short(char *buf, unsigned int n, long seconds)
367 days = seconds / 86400;
369 hours = seconds / 3600;
371 minutes = seconds / 60;
375 snprintf(buf, n, "%ldd %dh", days, hours);
376 } else if (hours > 0) {
377 snprintf(buf, n, "%dh %dm", hours, minutes);
379 snprintf(buf, n, "%dm %lds", minutes, seconds);
383 static struct update_cb {
384 struct update_cb *next;
390 void add_update_callback(void (*func)(void))
392 struct update_cb *uc = &update_cb_head;
395 if (uc->next->func == func)
399 uc->next = malloc(sizeof(struct update_cb));
400 memset(uc->next, 0, sizeof(struct update_cb));
401 uc->next->func = func;
404 static void __free_update_callbacks(struct update_cb *uc)
407 __free_update_callbacks(uc->next);
411 void free_update_callbacks(void)
413 if (update_cb_head.next)
414 __free_update_callbacks(update_cb_head.next);
415 update_cb_head.next = NULL;
418 unsigned long long need_mask;
421 #define NEED(a) ((need_mask & (1ULL << a)) && ((info.mask & (1ULL << a)) == 0))
423 void update_stuff(void)
426 static double last_meminfo_update;
427 struct update_cb *uc;
432 need_mask |= 1 << INFO_BUFFERS;
435 /* clear speeds and up status in case device was removed and doesn't get
439 #pragma omp parallel for schedule(dynamic,10)
440 #endif /* HAVE_OPENMP */
441 for (i = 0; i < 16; i++) {
442 if (netstats[i].dev) {
444 netstats[i].recv_speed = 0.0;
445 netstats[i].trans_speed = 0.0;
451 for (uc = update_cb_head.next; uc; uc = uc->next)
454 if ((NEED(INFO_MEM) || NEED(INFO_BUFFERS) || NEED(INFO_TOP))
455 && current_update_time - last_meminfo_update > 6.9) {
458 info.mem -= info.bufmem;
459 info.memeasyfree += info.bufmem;
461 last_meminfo_update = current_update_time;
465 if (NEED(INFO_X11) && x_initialised == YES) {
471 /* Ohkie to return negative values for temperatures */
472 int round_to_int_temp(float f)
475 return (int) (f + 0.5);
477 return (int) (f - 0.5);
480 /* Don't return negative values for cpugraph, bar, gauge, percentage.
481 * Causes unreasonable numbers to show */
482 unsigned int round_to_int(float f)
485 return (int) (f + 0.5);