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-2009 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/>.
40 #include <sys/ioctl.h>
42 #include <netinet/in.h>
44 #include <semaphore.h>
49 /* check for OS and include appropriate headers */
50 #if defined(__linux__)
52 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
54 #elif defined(__OpenBSD__)
58 /* folds a string over top of itself, like so:
60 * if start is "blah", and you call it with count = 1, the result will be "lah"
62 void strfold(char *start, int count)
65 for (curplace = start + count; *curplace != 0; curplace++) {
66 *(curplace - count) = *curplace;
68 *(curplace - count) = 0;
72 // use our own strndup() if it's not available
73 char *strndup(const char *s, size_t n)
76 char *ret = malloc(n + 1);
84 #endif /* HAVE_STRNDUP */
86 void update_uname(void)
96 return tv.tv_sec + (tv.tv_usec / 1000000.0);
99 /* Converts '~/...' paths to '/home/blah/...' assumes that 'dest' is at least
100 * DEFAULT_TEXT_BUFFER_SIZE. It's similar to variable_substitute, except only
101 * cheques for $HOME and ~/ in path */
102 void to_real_path(char *dest, const char *source)
104 char tmp[DEFAULT_TEXT_BUFFER_SIZE];
105 if (sscanf(source, "~/%s", tmp) || sscanf(source, "$HOME/%s", tmp)) {
106 char *homedir = getenv("HOME");
108 snprintf(dest, DEFAULT_TEXT_BUFFER_SIZE, "%s/%s", homedir, tmp);
110 NORM_ERR("$HOME environment variable doesn't exist");
111 strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
113 } else if (dest != source) { //see changelog 2009-06-29 if you doubt that this check is necessary
114 strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
118 int open_fifo(const char *file, int *reported)
120 char path[DEFAULT_TEXT_BUFFER_SIZE];
123 to_real_path(path, file);
124 fd = open(file, O_RDONLY | O_NONBLOCK);
127 if (!reported || *reported == 0) {
128 NORM_ERR("can't open %s: %s", file, strerror(errno));
139 FILE *open_file(const char *file, int *reported)
141 char path[DEFAULT_TEXT_BUFFER_SIZE];
144 to_real_path(path, file);
145 fp = fopen(file, "r");
148 if (!reported || *reported == 0) {
149 NORM_ERR("can't open %s: %s", file, strerror(errno));
160 void variable_substitute(const char *s, char *dest, unsigned int n)
162 while (*s && n > 1) {
170 /* variable is either $foo or ${foo} */
174 while (*s && *s != '}') {
179 while (*s && (isalnum((int) *s) || *s == '_')) {
184 /* copy variable to buffer and look it up */
185 len = (s - a > 255) ? 255 : (s - a);
186 strncpy(buf, a, len);
196 /* add var to dest */
201 strncpy(dest, var, len);
216 void format_seconds(char *buf, unsigned int n, long seconds)
221 days = seconds / 86400;
223 hours = seconds / 3600;
225 minutes = seconds / 60;
229 snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
231 snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
235 void format_seconds_short(char *buf, unsigned int n, long seconds)
240 days = seconds / 86400;
242 hours = seconds / 3600;
244 minutes = seconds / 60;
248 snprintf(buf, n, "%ldd %dh", days, hours);
249 } else if (hours > 0) {
250 snprintf(buf, n, "%dh %dm", hours, minutes);
252 snprintf(buf, n, "%dm %lds", minutes, seconds);
256 /* Linked list containing the functions to call upon each update interval.
257 * Populated while initialising text objects in construct_text_object(). */
258 static struct update_cb {
259 struct update_cb *next;
262 sem_t start_wait, end_wait;
264 /* set to 1 when starting the thread
265 * set to 0 to request thread termination */
266 volatile char running;
271 static void *run_update_callback(void *);
272 static int threading_started;
274 /* Register an update callback. Don't allow duplicates, to minimise side
275 * effects and overhead. */
276 void add_update_callback(void (*func)(void))
278 struct update_cb *uc = &update_cb_head;
284 if (uc->next->func == func)
289 uc->next = malloc(sizeof(struct update_cb));
292 memset(uc, 0, sizeof(struct update_cb));
294 sem_init(&uc->start_wait, 0, 0);
295 sem_init(&uc->end_wait, 0, 0);
297 if (threading_started) {
299 pthread_create(&uc->thread, NULL, &run_update_callback, uc);
303 /* Free the list element uc and all decendants recursively. */
304 static void __free_update_callbacks(struct update_cb *uc)
307 __free_update_callbacks(uc->next);
310 /* send cancellation request, then trigger and join the thread */
312 sem_post(&uc->start_wait);
313 pthread_join(uc->thread, NULL);
316 /* finally destroy the semaphores */
317 sem_destroy(&uc->start_wait);
318 sem_destroy(&uc->end_wait);
323 /* Free the whole list of update callbacks. */
324 void free_update_callbacks(void)
326 if (update_cb_head.next)
327 __free_update_callbacks(update_cb_head.next);
328 update_cb_head.next = NULL;
331 /* We cannot start threads before we forked to background, because the threads
332 * would remain in the wrong process. Because of that, add_update_callback()
333 * doesn't create threads before start_update_threading() is called.
334 * start_update_threading() starts all threads previously registered, and sets a
335 * flag so that future threads are automagically started by
336 * add_update_callback().
337 * This text is almost longer than the actual code.
339 void start_update_threading(void)
341 struct update_cb *uc;
343 threading_started = 1;
345 for(uc = update_cb_head.next; uc; uc = uc->next) {
347 pthread_create(&uc->thread, NULL, &run_update_callback, uc);
351 static void *run_update_callback(void *data)
353 struct update_cb *ucb = data;
356 sem_wait(&ucb->start_wait);
357 if (ucb->running == 0)
360 sem_post(&ucb->end_wait);
366 void update_stuff(void)
369 struct update_cb *uc;
371 /* clear speeds and up status in case device was removed and doesn't get
375 #pragma omp parallel for schedule(dynamic,10)
376 #endif /* HAVE_OPENMP */
377 for (i = 0; i < 16; i++) {
378 if (netstats[i].dev) {
380 netstats[i].recv_speed = 0.0;
381 netstats[i].trans_speed = 0.0;
387 for (uc = update_cb_head.next; uc; uc = uc->next)
388 sem_post(&uc->start_wait);
389 /* need to synchronise here, otherwise locking is needed (as data
390 * would be printed with some update callbacks still running) */
391 for (uc = update_cb_head.next; uc; uc = uc->next)
392 sem_wait(&uc->end_wait);
394 /* XXX: move the following into the update_meminfo() functions? */
396 info.mem -= info.bufmem;
397 info.memeasyfree += info.bufmem;
401 /* Ohkie to return negative values for temperatures */
402 int round_to_int_temp(float f)
405 return (int) (f + 0.5);
407 return (int) (f - 0.5);
410 /* Don't return negative values for cpugraph, bar, gauge, percentage.
411 * Causes unreasonable numbers to show */
412 unsigned int round_to_int(float f)
415 return (int) (f + 0.5);
421 void scan_loadavg_arg(struct text_object *obj, const char *arg)
424 if (arg && !arg[1] && isdigit(arg[0])) {
425 obj->data.i = atoi(arg);
426 if (obj->data.i > 3 || obj->data.i < 1) {
427 NORM_ERR("loadavg arg needs to be in range (1,3)");
431 /* convert to array index (or the default (-1)) */
435 void print_loadavg(struct text_object *obj, char *p, int p_max_size)
437 float *v = info.loadavg;
439 if (obj->data.i < 0) {
440 snprintf(p, p_max_size, "%.2f %.2f %.2f", v[0], v[1], v[2]);
442 snprintf(p, p_max_size, "%.2f", v[obj->data.i]);
447 void scan_loadgraph_arg(struct text_object *obj, const char *arg)
451 buf = scan_graph(obj, arg);
456 void print_loadgraph(struct text_object *obj, char *p)
458 new_graph(obj, p, info.loadavg[0]);