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/>.
35 #include <sys/ioctl.h>
37 #include <netinet/in.h>
42 /* check for OS and include appropriate headers */
43 #if defined(__linux__)
45 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
47 #elif defined(__OpenBSD__)
51 /* OS specific prototypes to be implemented by linux.c & Co. */
52 void update_entropy(void);
55 // use our own strndup() if it's not available
56 char *strndup(const char *s, size_t n)
59 char *ret = malloc(n + 1);
67 #endif /* HAVE_STRNDUP */
69 void update_uname(void)
79 return tv.tv_sec + (tv.tv_usec / 1000000.0);
82 /* Converts '~/...' paths to '/home/blah/...' assumes that 'dest' is at least
83 * DEFAULT_TEXT_BUFFER_SIZE. It's similar to variable_substitute, except only
84 * cheques for $HOME and ~/ in path */
85 void to_real_path(char *dest, const char *source)
87 char tmp[DEFAULT_TEXT_BUFFER_SIZE];
88 if (sscanf(source, "~/%s", tmp) || sscanf(source, "$HOME/%s", tmp)) {
89 char *homedir = getenv("HOME");
91 snprintf(dest, DEFAULT_TEXT_BUFFER_SIZE, "%s/%s", homedir, tmp);
93 ERR("$HOME environment variable doesn't exist");
94 strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
96 } else if (dest != source) { //see changelog 2009-06-29 if you doubt that this check is necessary
97 strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
101 FILE *open_file(const char *file, int *reported)
103 char path[DEFAULT_TEXT_BUFFER_SIZE];
106 to_real_path(path, file);
107 fp = fopen(file, "r");
110 if (!reported || *reported == 0) {
111 ERR("can't open %s: %s", file, strerror(errno));
122 void variable_substitute(const char *s, char *dest, unsigned int n)
124 while (*s && n > 1) {
132 /* variable is either $foo or ${foo} */
136 while (*s && *s != '}') {
141 while (*s && (isalnum((int) *s) || *s == '_')) {
146 /* copy variable to buffer and look it up */
147 len = (s - a > 255) ? 255 : (s - a);
148 strncpy(buf, a, len);
158 /* add var to dest */
163 strncpy(dest, var, len);
178 /* network interface stuff */
180 static struct net_stat netstats[16];
182 struct net_stat *get_net_stat(const char *dev, void *free_at_crash1, void *free_at_crash2)
190 /* find interface stat */
191 for (i = 0; i < 16; i++) {
192 if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
197 /* wasn't found? add it */
198 for (i = 0; i < 16; i++) {
199 if (netstats[i].dev == 0) {
200 netstats[i].dev = strndup(dev, text_buffer_size);
205 CRIT_ERR(free_at_crash1, free_at_crash2, "too many interfaces used (limit is 16)");
209 void clear_net_stats(void)
212 for (i = 0; i < 16; i++) {
213 if (netstats[i].dev) {
214 free(netstats[i].dev);
217 memset(netstats, 0, sizeof(netstats));
220 /* We should check if this is ok with OpenBSD and NetBSD as well. */
221 int interface_up(const char *dev)
226 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
227 CRIT_ERR(NULL, NULL, "could not create sockfd");
230 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
231 if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
232 /* if device does not exist, treat like not up */
233 if (errno != ENODEV && errno != ENXIO)
234 perror("SIOCGIFFLAGS");
238 if (!(ifr.ifr_flags & IFF_UP)) /* iface is not up */
240 if (ifup_strictness == IFUP_UP)
243 if (!(ifr.ifr_flags & IFF_RUNNING))
245 if (ifup_strictness == IFUP_LINK)
248 if (ioctl(fd, SIOCGIFADDR, &ifr)) {
249 perror("SIOCGIFADDR");
252 if (((struct sockaddr_in *)&(ifr.ifr_ifru.ifru_addr))->sin_addr.s_addr)
263 void free_dns_data(void)
266 struct dns_data *data = &info.nameserver_info;
267 for (i = 0; i < data->nscount; i++)
268 free(data->ns_list[i]);
271 memset(data, 0, sizeof(struct dns_data));
274 //static double last_dns_update;
276 static void update_dns_data(void)
280 struct dns_data *data = &info.nameserver_info;
282 /* maybe updating too often causes higher load because of /etc lying on a real FS
283 if (current_update_time - last_dns_update < 10.0)
286 last_dns_update = current_update_time;
291 if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
294 if (fgets(line, 255, fp) == NULL) {
297 if (!strncmp(line, "nameserver ", 11)) {
298 line[strlen(line) - 1] = '\0'; // remove trailing newline
300 data->ns_list = realloc(data->ns_list, data->nscount * sizeof(char *));
301 data->ns_list[data->nscount - 1] = strndup(line + 11, text_buffer_size);
307 void format_seconds(char *buf, unsigned int n, long seconds)
312 days = seconds / 86400;
314 hours = seconds / 3600;
316 minutes = seconds / 60;
320 snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
322 snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
326 void format_seconds_short(char *buf, unsigned int n, long seconds)
331 days = seconds / 86400;
333 hours = seconds / 3600;
335 minutes = seconds / 60;
339 snprintf(buf, n, "%ldd %dh", days, hours);
340 } else if (hours > 0) {
341 snprintf(buf, n, "%dh %dm", hours, minutes);
343 snprintf(buf, n, "%dm %lds", minutes, seconds);
347 static double last_meminfo_update;
348 static double last_fs_update;
350 unsigned long long need_mask;
353 #define NEED(a) ((need_mask & (1ULL << a)) && ((info.mask & (1ULL << a)) == 0))
355 void update_stuff(void)
362 need_mask |= 1 << INFO_BUFFERS;
365 /* clear speeds and up status in case device was removed and doesn't get
369 #pragma omp parallel for schedule(dynamic,10)
370 #endif /* HAVE_OPENMP */
371 for (i = 0; i < 16; i++) {
372 if (netstats[i].dev) {
374 netstats[i].recv_speed = 0.0;
375 netstats[i].trans_speed = 0.0;
381 if (NEED(INFO_UPTIME)) {
385 if (NEED(INFO_PROCS)) {
386 update_total_processes();
389 if (NEED(INFO_RUN_PROCS)) {
390 update_running_processes();
393 if (NEED(INFO_CPU)) {
397 if (NEED(INFO_NET)) {
401 if (NEED(INFO_DISKIO)) {
405 #if defined(__linux__)
406 if (NEED(INFO_I8K)) {
409 #endif /* __linux__ */
412 if (NEED(INFO_MPD)) {
418 if (NEED(INFO_MOC)) {
419 run_moc_thread(info.music_player_interval * 100000);
424 if (NEED(INFO_XMMS2)) {
430 if (NEED(INFO_AUDACIOUS)) {
436 if (NEED(INFO_BMPX)) {
441 if (NEED(INFO_LOADAVG)) {
442 update_load_average();
445 if ((NEED(INFO_MEM) || NEED(INFO_BUFFERS) || NEED(INFO_TOP))
446 && current_update_time - last_meminfo_update > 6.9) {
449 info.mem -= info.bufmem;
450 info.memeasyfree += info.bufmem;
452 last_meminfo_update = current_update_time;
456 if (NEED(INFO_X11) && x_initialised == YES) {
461 if (NEED(INFO_TOP)) {
465 /* update_fs_stat() won't do anything if there aren't fs -things */
466 if (NEED(INFO_FS) && current_update_time - last_fs_update > 12.9) {
468 last_fs_update = current_update_time;
470 #ifdef TCP_PORT_MONITOR
471 if (NEED(INFO_TCP_PORT_MONITOR)) {
472 tcp_portmon_update();
475 if (NEED(INFO_ENTROPY)) {
478 #if defined(__linux__)
479 if (NEED(INFO_USERS)) {
483 update_gateway_info();
485 #endif /* __linux__ */
486 if (NEED(INFO_DNS)) {
490 if (NEED(INFO_APCUPSD)) {
496 /* Ohkie to return negative values for temperatures */
497 int round_to_int_temp(float f)
500 return (int) (f + 0.5);
502 return (int) (f - 0.5);
505 /* Don't return negative values for cpugraph, bar, gauge, percentage.
506 * Causes unreasonable numbers to show */
507 unsigned int round_to_int(float f)
510 return (int) (f + 0.5);
516 /* utility function used by RSS and Weather stuff for a curl callback.
518 size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
520 size_t realsize = size * nmemb;
521 struct MemoryStruct *mem = (struct MemoryStruct *) data;
523 mem->memory = (char *) realloc(mem->memory, mem->size + realsize + 1);
525 memcpy(&(mem->memory[mem->size]), ptr, realsize);
526 mem->size += realsize;
527 mem->memory[mem->size] = 0;