1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
41 #include <sys/ioctl.h>
43 #include <netinet/in.h>
45 #include <semaphore.h>
50 /* check for OS and include appropriate headers */
51 #if defined(__linux__)
53 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
55 #elif defined(__OpenBSD__)
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 int update_uname(void)
98 return tv.tv_sec + (tv.tv_usec / 1000000.0);
101 /* Converts '~/...' paths to '/home/blah/...' assumes that 'dest' is at least
102 * DEFAULT_TEXT_BUFFER_SIZE. It's similar to variable_substitute, except only
103 * cheques for $HOME and ~/ in path */
104 void to_real_path(char *dest, const char *source)
106 char tmp[DEFAULT_TEXT_BUFFER_SIZE];
107 if (sscanf(source, "~/%s", tmp) || sscanf(source, "$HOME/%s", tmp)) {
108 char *homedir = getenv("HOME");
110 snprintf(dest, DEFAULT_TEXT_BUFFER_SIZE, "%s/%s", homedir, tmp);
112 NORM_ERR("$HOME environment variable doesn't exist");
113 strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
115 } else if (dest != source) { //see changelog 2009-06-29 if you doubt that this check is necessary
116 strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
120 int open_fifo(const char *file, int *reported)
122 char path[DEFAULT_TEXT_BUFFER_SIZE];
125 to_real_path(path, file);
126 fd = open(file, O_RDONLY | O_NONBLOCK);
129 if (!reported || *reported == 0) {
130 NORM_ERR("can't open %s: %s", file, strerror(errno));
141 FILE *open_file(const char *file, int *reported)
143 char path[DEFAULT_TEXT_BUFFER_SIZE];
146 to_real_path(path, file);
147 fp = fopen(file, "r");
150 if (!reported || *reported == 0) {
151 NORM_ERR("can't open %s: %s", file, strerror(errno));
162 void variable_substitute(const char *s, char *dest, unsigned int n)
164 while (*s && n > 1) {
172 /* variable is either $foo or ${foo} */
176 while (*s && *s != '}') {
181 while (*s && (isalnum((int) *s) || *s == '_')) {
186 /* copy variable to buffer and look it up */
187 len = (s - a > 255) ? 255 : (s - a);
188 strncpy(buf, a, len);
198 /* add var to dest */
203 strncpy(dest, var, len);
218 void format_seconds(char *buf, unsigned int n, long seconds)
223 if (times_in_seconds()) {
224 snprintf(buf, n, "%ld", seconds);
228 days = seconds / 86400;
230 hours = seconds / 3600;
232 minutes = seconds / 60;
236 snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
238 snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
242 void format_seconds_short(char *buf, unsigned int n, long seconds)
247 if (times_in_seconds()) {
248 snprintf(buf, n, "%ld", seconds);
252 days = seconds / 86400;
254 hours = seconds / 3600;
256 minutes = seconds / 60;
260 snprintf(buf, n, "%ldd %dh", days, hours);
261 } else if (hours > 0) {
262 snprintf(buf, n, "%dh %dm", hours, minutes);
264 snprintf(buf, n, "%dm %lds", minutes, seconds);
268 /* Linked list containing the functions to call upon each update interval.
269 * Populated while initialising text objects in construct_text_object(). */
270 static struct update_cb {
271 struct update_cb *next;
274 sem_t start_wait, end_wait;
276 /* set to 1 when starting the thread
277 * set to 0 to request thread termination */
278 volatile char running;
284 static void *run_update_callback(void *) __attribute__((noreturn));
286 static int threading_started = 0;
288 /* Register an update callback. Don't allow duplicates, to minimise side
289 * effects and overhead. */
290 void add_update_callback(int (*func)(void))
292 struct update_cb *uc = &update_cb_head;
298 if (uc->next->func == func)
303 uc->next = malloc(sizeof(struct update_cb));
306 memset(uc, 0, sizeof(struct update_cb));
308 sem_init(&uc->start_wait, 0, 0);
309 sem_init(&uc->end_wait, 0, 0);
311 if (threading_started) {
314 pthread_create(&uc->thread, NULL, &run_update_callback, uc);
319 /* Free the list element uc and all decendants recursively. */
320 static void __free_update_callbacks(struct update_cb *uc)
323 __free_update_callbacks(uc->next);
326 /* send cancellation request, then trigger and join the thread */
328 sem_post(&uc->start_wait);
330 if (pthread_join(uc->thread, NULL)) {
331 NORM_ERR("Error destroying thread");
334 /* finally destroy the semaphores */
335 sem_destroy(&uc->start_wait);
336 sem_destroy(&uc->end_wait);
341 /* Free the whole list of update callbacks. */
342 void free_update_callbacks(void)
344 if (update_cb_head.next)
345 __free_update_callbacks(update_cb_head.next);
346 update_cb_head.next = NULL;
349 /* We cannot start threads before we forked to background, because the threads
350 * would remain in the wrong process. Because of that, add_update_callback()
351 * doesn't create threads before start_update_threading() is called.
352 * start_update_threading() starts all threads previously registered, and sets a
353 * flag so that future threads are automagically started by
354 * add_update_callback().
355 * This text is almost longer than the actual code.
357 void start_update_threading(void)
359 struct update_cb *uc;
361 threading_started = 1;
363 for (uc = update_cb_head.next; uc; uc = uc->next) {
366 pthread_create(&uc->thread, NULL, &run_update_callback, uc);
371 static void *run_update_callback(void *data)
373 struct update_cb *ucb = data;
375 if (!ucb || !ucb->func) pthread_exit(NULL);
378 if (sem_wait(&ucb->start_wait)) pthread_exit(NULL);
379 if (ucb->running == 0) pthread_exit(NULL);
381 ucb->next = ucb; //this is normally not be possible, so we use it to show that there was a critical error
382 sem_post(&ucb->end_wait);
383 sem_post(&ucb->end_wait);
386 if (sem_post(&ucb->end_wait)) pthread_exit(NULL);
392 void update_stuff(void)
395 struct update_cb *uc;
397 /* clear speeds and up status in case device was removed and doesn't get
401 #pragma omp parallel for schedule(dynamic,10)
402 #endif /* HAVE_OPENMP */
403 for (i = 0; i < MAX_NET_INTERFACES; i++) {
404 if (netstats[i].dev) {
406 netstats[i].recv_speed = 0.0;
407 netstats[i].trans_speed = 0.0;
413 for (uc = update_cb_head.next; uc; uc = uc->next) {
414 if (sem_post(&uc->start_wait)) {
415 NORM_ERR("Semaphore error");
418 /* need to synchronise here, otherwise locking is needed (as data
419 * would be printed with some update callbacks still running) */
420 for (uc = update_cb_head.next; uc; uc = uc->next) {
421 sem_wait(&uc->end_wait);
423 pthread_join(uc->thread, NULL);
429 /* XXX: move the following into the update_meminfo() functions? */
431 info.mem -= info.bufmem;
432 info.memeasyfree += info.bufmem;
436 /* Ohkie to return negative values for temperatures */
437 int round_to_int_temp(float f)
440 return (int) (f + 0.5);
442 return (int) (f - 0.5);
445 /* Don't return negative values for cpugraph, bar, gauge, percentage.
446 * Causes unreasonable numbers to show */
447 unsigned int round_to_int(float f)
450 return (int) (f + 0.5);
456 void scan_loadavg_arg(struct text_object *obj, const char *arg)
459 if (arg && !arg[1] && isdigit(arg[0])) {
460 obj->data.i = atoi(arg);
461 if (obj->data.i > 3 || obj->data.i < 1) {
462 NORM_ERR("loadavg arg needs to be in range (1,3)");
466 /* convert to array index (or the default (-1)) */
470 void print_loadavg(struct text_object *obj, char *p, int p_max_size)
472 float *v = info.loadavg;
474 if (obj->data.i < 0) {
475 snprintf(p, p_max_size, "%.2f %.2f %.2f", v[0], v[1], v[2]);
477 snprintf(p, p_max_size, "%.2f", v[obj->data.i]);
482 void scan_loadgraph_arg(struct text_object *obj, const char *arg)
486 buf = scan_graph(obj, arg, 0);
491 void print_loadgraph(struct text_object *obj, char *p, int p_max_size)
493 new_graph(obj, p, p_max_size, info.loadavg[0]);