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 /* OS specific prototypes to be implemented by linux.c & Co. */
57 void update_entropy(void);
59 /* folds a string over top of itself, like so:
61 * if start is "blah", and you call it with count = 1, the result will be "lah"
63 void strfold(char *start, int count)
66 for (curplace = start + count; *curplace != 0; curplace++) {
67 *(curplace - count) = *curplace;
69 *(curplace - count) = 0;
73 // use our own strndup() if it's not available
74 char *strndup(const char *s, size_t n)
77 char *ret = malloc(n + 1);
85 #endif /* HAVE_STRNDUP */
87 void update_uname(void)
97 return tv.tv_sec + (tv.tv_usec / 1000000.0);
100 /* Converts '~/...' paths to '/home/blah/...' assumes that 'dest' is at least
101 * DEFAULT_TEXT_BUFFER_SIZE. It's similar to variable_substitute, except only
102 * cheques for $HOME and ~/ in path */
103 void to_real_path(char *dest, const char *source)
105 char tmp[DEFAULT_TEXT_BUFFER_SIZE];
106 if (sscanf(source, "~/%s", tmp) || sscanf(source, "$HOME/%s", tmp)) {
107 char *homedir = getenv("HOME");
109 snprintf(dest, DEFAULT_TEXT_BUFFER_SIZE, "%s/%s", homedir, tmp);
111 ERR("$HOME environment variable doesn't exist");
112 strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
114 } else if (dest != source) { //see changelog 2009-06-29 if you doubt that this check is necessary
115 strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
119 int open_fifo(const char *file, int *reported)
121 char path[DEFAULT_TEXT_BUFFER_SIZE];
124 to_real_path(path, file);
125 fd = open(file, O_RDONLY | O_NONBLOCK);
128 if (!reported || *reported == 0) {
129 ERR("can't open %s: %s", file, strerror(errno));
140 FILE *open_file(const char *file, int *reported)
142 char path[DEFAULT_TEXT_BUFFER_SIZE];
145 to_real_path(path, file);
146 fp = fopen(file, "r");
149 if (!reported || *reported == 0) {
150 ERR("can't open %s: %s", file, strerror(errno));
161 void variable_substitute(const char *s, char *dest, unsigned int n)
163 while (*s && n > 1) {
171 /* variable is either $foo or ${foo} */
175 while (*s && *s != '}') {
180 while (*s && (isalnum((int) *s) || *s == '_')) {
185 /* copy variable to buffer and look it up */
186 len = (s - a > 255) ? 255 : (s - a);
187 strncpy(buf, a, len);
197 /* add var to dest */
202 strncpy(dest, var, len);
217 /* network interface stuff */
219 static struct net_stat netstats[16];
221 struct net_stat *get_net_stat(const char *dev, void *free_at_crash1, void *free_at_crash2)
229 /* find interface stat */
230 for (i = 0; i < 16; i++) {
231 if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
236 /* wasn't found? add it */
237 for (i = 0; i < 16; i++) {
238 if (netstats[i].dev == 0) {
239 netstats[i].dev = strndup(dev, text_buffer_size);
244 CRIT_ERR(free_at_crash1, free_at_crash2, "too many interfaces used (limit is 16)");
248 void clear_net_stats(void)
251 for (i = 0; i < 16; i++) {
252 if (netstats[i].dev) {
253 free(netstats[i].dev);
256 memset(netstats, 0, sizeof(netstats));
259 /* We should check if this is ok with OpenBSD and NetBSD as well. */
260 int interface_up(const char *dev)
265 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
266 CRIT_ERR(NULL, NULL, "could not create sockfd");
269 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
270 if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
271 /* if device does not exist, treat like not up */
272 if (errno != ENODEV && errno != ENXIO)
273 perror("SIOCGIFFLAGS");
277 if (!(ifr.ifr_flags & IFF_UP)) /* iface is not up */
279 if (ifup_strictness == IFUP_UP)
282 if (!(ifr.ifr_flags & IFF_RUNNING))
284 if (ifup_strictness == IFUP_LINK)
287 if (ioctl(fd, SIOCGIFADDR, &ifr)) {
288 perror("SIOCGIFADDR");
291 if (((struct sockaddr_in *)&(ifr.ifr_ifru.ifru_addr))->sin_addr.s_addr)
302 void free_dns_data(void)
305 struct dns_data *data = &info.nameserver_info;
306 for (i = 0; i < data->nscount; i++)
307 free(data->ns_list[i]);
310 memset(data, 0, sizeof(struct dns_data));
313 //static double last_dns_update;
315 static void update_dns_data(void)
319 struct dns_data *data = &info.nameserver_info;
321 /* maybe updating too often causes higher load because of /etc lying on a real FS
322 if (current_update_time - last_dns_update < 10.0)
325 last_dns_update = current_update_time;
330 if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
333 if (fgets(line, 255, fp) == NULL) {
336 if (!strncmp(line, "nameserver ", 11)) {
337 line[strlen(line) - 1] = '\0'; // remove trailing newline
339 data->ns_list = realloc(data->ns_list, data->nscount * sizeof(char *));
340 data->ns_list[data->nscount - 1] = strndup(line + 11, text_buffer_size);
346 void format_seconds(char *buf, unsigned int n, long seconds)
351 days = seconds / 86400;
353 hours = seconds / 3600;
355 minutes = seconds / 60;
359 snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
361 snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
365 void format_seconds_short(char *buf, unsigned int n, long seconds)
370 days = seconds / 86400;
372 hours = seconds / 3600;
374 minutes = seconds / 60;
378 snprintf(buf, n, "%ldd %dh", days, hours);
379 } else if (hours > 0) {
380 snprintf(buf, n, "%dh %dm", hours, minutes);
382 snprintf(buf, n, "%dm %lds", minutes, seconds);
386 static double last_meminfo_update;
387 static double last_fs_update;
389 unsigned long long need_mask;
392 #define NEED(a) ((need_mask & (1ULL << a)) && ((info.mask & (1ULL << a)) == 0))
394 void update_stuff(void)
401 need_mask |= 1 << INFO_BUFFERS;
404 /* clear speeds and up status in case device was removed and doesn't get
408 #pragma omp parallel for schedule(dynamic,10)
409 #endif /* HAVE_OPENMP */
410 for (i = 0; i < 16; i++) {
411 if (netstats[i].dev) {
413 netstats[i].recv_speed = 0.0;
414 netstats[i].trans_speed = 0.0;
420 if (NEED(INFO_UPTIME)) {
424 if (NEED(INFO_PROCS)) {
425 update_total_processes();
428 if (NEED(INFO_RUN_PROCS)) {
429 update_running_processes();
432 if (NEED(INFO_CPU)) {
436 if (NEED(INFO_NET)) {
440 if (NEED(INFO_DISKIO)) {
444 #if defined(__linux__)
445 if (NEED(INFO_I8K)) {
448 #endif /* __linux__ */
451 if (NEED(INFO_MPD)) {
457 if (NEED(INFO_MOC)) {
458 run_moc_thread(info.music_player_interval * 100000);
463 if (NEED(INFO_XMMS2)) {
469 if (NEED(INFO_AUDACIOUS)) {
475 if (NEED(INFO_BMPX)) {
480 if (NEED(INFO_LOADAVG)) {
481 update_load_average();
484 if ((NEED(INFO_MEM) || NEED(INFO_BUFFERS) || NEED(INFO_TOP))
485 && current_update_time - last_meminfo_update > 6.9) {
488 info.mem -= info.bufmem;
489 info.memeasyfree += info.bufmem;
491 last_meminfo_update = current_update_time;
495 if (NEED(INFO_X11) && x_initialised == YES) {
500 if (NEED(INFO_TOP)) {
504 /* update_fs_stat() won't do anything if there aren't fs -things */
505 if (NEED(INFO_FS) && current_update_time - last_fs_update > 12.9) {
507 last_fs_update = current_update_time;
509 #ifdef TCP_PORT_MONITOR
510 if (NEED(INFO_TCP_PORT_MONITOR)) {
511 tcp_portmon_update();
514 if (NEED(INFO_ENTROPY)) {
517 #if defined(__linux__)
518 if (NEED(INFO_USERS)) {
522 update_gateway_info();
524 #endif /* __linux__ */
525 if (NEED(INFO_DNS)) {
529 if (NEED(INFO_APCUPSD)) {
535 /* Ohkie to return negative values for temperatures */
536 int round_to_int_temp(float f)
539 return (int) (f + 0.5);
541 return (int) (f - 0.5);
544 /* Don't return negative values for cpugraph, bar, gauge, percentage.
545 * Causes unreasonable numbers to show */
546 unsigned int round_to_int(float f)
549 return (int) (f + 0.5);