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) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 * vim: ts=4 sw=4 noet ai cindent syntax=c
37 #include <sys/ioctl.h>
39 #include <netinet/in.h>
45 /* check for OS and include appropriate headers */
46 #if defined(__linux__)
48 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
50 #elif defined(__OpenBSD__)
54 /* OS specific prototypes to be implemented by linux.c & Co. */
55 void update_entropy(void);
57 /* folds a string over top of itself, like so:
59 * if start is "blah", and you call it with count = 1, the result will be "lah"
61 void strfold(char *start, int count)
64 for (curplace = start + count; *curplace != 0; curplace++) {
65 *(curplace - count) = *curplace;
67 *(curplace - count) = 0;
71 // use our own strndup() if it's not available
72 char *strndup(const char *s, size_t n)
75 char *ret = malloc(n + 1);
83 #endif /* HAVE_STRNDUP */
85 void update_uname(void)
95 return tv.tv_sec + (tv.tv_usec / 1000000.0);
98 /* Converts '~/...' paths to '/home/blah/...' assumes that 'dest' is at least
99 * DEFAULT_TEXT_BUFFER_SIZE. It's similar to variable_substitute, except only
100 * cheques for $HOME and ~/ in path */
101 void to_real_path(char *dest, const char *source)
103 char tmp[DEFAULT_TEXT_BUFFER_SIZE];
104 if (sscanf(source, "~/%s", tmp) || sscanf(source, "$HOME/%s", tmp)) {
105 char *homedir = getenv("HOME");
107 snprintf(dest, DEFAULT_TEXT_BUFFER_SIZE, "%s/%s", homedir, tmp);
109 ERR("$HOME environment variable doesn't exist");
110 strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
112 } else if (dest != source) { //see changelog 2009-06-29 if you doubt that this check is necessary
113 strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
117 int open_fifo(const char *file, int *reported)
119 char path[DEFAULT_TEXT_BUFFER_SIZE];
122 to_real_path(path, file);
123 fd = open(file, O_RDONLY | O_NONBLOCK);
126 if (!reported || *reported == 0) {
127 ERR("can't open %s: %s", file, strerror(errno));
138 FILE *open_file(const char *file, int *reported)
140 char path[DEFAULT_TEXT_BUFFER_SIZE];
143 to_real_path(path, file);
144 fp = fopen(file, "r");
147 if (!reported || *reported == 0) {
148 ERR("can't open %s: %s", file, strerror(errno));
159 void variable_substitute(const char *s, char *dest, unsigned int n)
161 while (*s && n > 1) {
169 /* variable is either $foo or ${foo} */
173 while (*s && *s != '}') {
178 while (*s && (isalnum((int) *s) || *s == '_')) {
183 /* copy variable to buffer and look it up */
184 len = (s - a > 255) ? 255 : (s - a);
185 strncpy(buf, a, len);
195 /* add var to dest */
200 strncpy(dest, var, len);
215 /* network interface stuff */
217 static struct net_stat netstats[16];
219 struct net_stat *get_net_stat(const char *dev, void *free_at_crash1, void *free_at_crash2)
227 /* find interface stat */
228 for (i = 0; i < 16; i++) {
229 if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
234 /* wasn't found? add it */
235 for (i = 0; i < 16; i++) {
236 if (netstats[i].dev == 0) {
237 netstats[i].dev = strndup(dev, text_buffer_size);
242 CRIT_ERR(free_at_crash1, free_at_crash2, "too many interfaces used (limit is 16)");
246 void clear_net_stats(void)
249 for (i = 0; i < 16; i++) {
250 if (netstats[i].dev) {
251 free(netstats[i].dev);
254 memset(netstats, 0, sizeof(netstats));
257 /* We should check if this is ok with OpenBSD and NetBSD as well. */
258 int interface_up(const char *dev)
263 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
264 CRIT_ERR(NULL, NULL, "could not create sockfd");
267 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
268 if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
269 /* if device does not exist, treat like not up */
270 if (errno != ENODEV && errno != ENXIO)
271 perror("SIOCGIFFLAGS");
275 if (!(ifr.ifr_flags & IFF_UP)) /* iface is not up */
277 if (ifup_strictness == IFUP_UP)
280 if (!(ifr.ifr_flags & IFF_RUNNING))
282 if (ifup_strictness == IFUP_LINK)
285 if (ioctl(fd, SIOCGIFADDR, &ifr)) {
286 perror("SIOCGIFADDR");
289 if (((struct sockaddr_in *)&(ifr.ifr_ifru.ifru_addr))->sin_addr.s_addr)
300 void free_dns_data(void)
303 struct dns_data *data = &info.nameserver_info;
304 for (i = 0; i < data->nscount; i++)
305 free(data->ns_list[i]);
308 memset(data, 0, sizeof(struct dns_data));
311 //static double last_dns_update;
313 static void update_dns_data(void)
317 struct dns_data *data = &info.nameserver_info;
319 /* maybe updating too often causes higher load because of /etc lying on a real FS
320 if (current_update_time - last_dns_update < 10.0)
323 last_dns_update = current_update_time;
328 if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
331 if (fgets(line, 255, fp) == NULL) {
334 if (!strncmp(line, "nameserver ", 11)) {
335 line[strlen(line) - 1] = '\0'; // remove trailing newline
337 data->ns_list = realloc(data->ns_list, data->nscount * sizeof(char *));
338 data->ns_list[data->nscount - 1] = strndup(line + 11, text_buffer_size);
344 void format_seconds(char *buf, unsigned int n, long seconds)
349 days = seconds / 86400;
351 hours = seconds / 3600;
353 minutes = seconds / 60;
357 snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
359 snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
363 void format_seconds_short(char *buf, unsigned int n, long seconds)
368 days = seconds / 86400;
370 hours = seconds / 3600;
372 minutes = seconds / 60;
376 snprintf(buf, n, "%ldd %dh", days, hours);
377 } else if (hours > 0) {
378 snprintf(buf, n, "%dh %dm", hours, minutes);
380 snprintf(buf, n, "%dm %lds", minutes, seconds);
384 static double last_meminfo_update;
385 static double last_fs_update;
387 unsigned long long need_mask;
390 #define NEED(a) ((need_mask & (1ULL << a)) && ((info.mask & (1ULL << a)) == 0))
392 void update_stuff(void)
399 need_mask |= 1 << INFO_BUFFERS;
402 /* clear speeds and up status in case device was removed and doesn't get
406 #pragma omp parallel for schedule(dynamic,10)
407 #endif /* HAVE_OPENMP */
408 for (i = 0; i < 16; i++) {
409 if (netstats[i].dev) {
411 netstats[i].recv_speed = 0.0;
412 netstats[i].trans_speed = 0.0;
418 if (NEED(INFO_UPTIME)) {
422 if (NEED(INFO_PROCS)) {
423 update_total_processes();
426 if (NEED(INFO_RUN_PROCS)) {
427 update_running_processes();
430 if (NEED(INFO_CPU)) {
434 if (NEED(INFO_NET)) {
438 if (NEED(INFO_DISKIO)) {
442 #if defined(__linux__)
443 if (NEED(INFO_I8K)) {
446 #endif /* __linux__ */
449 if (NEED(INFO_MPD)) {
455 if (NEED(INFO_MOC)) {
456 run_moc_thread(info.music_player_interval * 100000);
461 if (NEED(INFO_XMMS2)) {
467 if (NEED(INFO_AUDACIOUS)) {
473 if (NEED(INFO_BMPX)) {
478 if (NEED(INFO_LOADAVG)) {
479 update_load_average();
482 if ((NEED(INFO_MEM) || NEED(INFO_BUFFERS) || NEED(INFO_TOP))
483 && current_update_time - last_meminfo_update > 6.9) {
486 info.mem -= info.bufmem;
487 info.memeasyfree += info.bufmem;
489 last_meminfo_update = current_update_time;
493 if (NEED(INFO_X11) && x_initialised == YES) {
498 if (NEED(INFO_TOP)) {
502 /* update_fs_stat() won't do anything if there aren't fs -things */
503 if (NEED(INFO_FS) && current_update_time - last_fs_update > 12.9) {
505 last_fs_update = current_update_time;
507 #ifdef TCP_PORT_MONITOR
508 if (NEED(INFO_TCP_PORT_MONITOR)) {
509 tcp_portmon_update();
512 if (NEED(INFO_ENTROPY)) {
515 #if defined(__linux__)
516 if (NEED(INFO_USERS)) {
520 update_gateway_info();
522 #endif /* __linux__ */
523 if (NEED(INFO_DNS)) {
527 if (NEED(INFO_APCUPSD)) {
533 /* Ohkie to return negative values for temperatures */
534 int round_to_int_temp(float f)
537 return (int) (f + 0.5);
539 return (int) (f - 0.5);
542 /* Don't return negative values for cpugraph, bar, gauge, percentage.
543 * Causes unreasonable numbers to show */
544 unsigned int round_to_int(float f)
547 return (int) (f + 0.5);