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 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 NORM_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 NORM_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 NORM_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 void format_seconds(char *buf, unsigned int n, long seconds)
222 if (times_in_seconds()) {
223 snprintf(buf, n, "%ld", seconds);
227 days = seconds / 86400;
229 hours = seconds / 3600;
231 minutes = seconds / 60;
235 snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
237 snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
241 void format_seconds_short(char *buf, unsigned int n, long seconds)
246 if (times_in_seconds()) {
247 snprintf(buf, n, "%ld", seconds);
251 days = seconds / 86400;
253 hours = seconds / 3600;
255 minutes = seconds / 60;
259 snprintf(buf, n, "%ldd %dh", days, hours);
260 } else if (hours > 0) {
261 snprintf(buf, n, "%dh %dm", hours, minutes);
263 snprintf(buf, n, "%dm %lds", minutes, seconds);
267 /* Linked list containing the functions to call upon each update interval.
268 * Populated while initialising text objects in construct_text_object(). */
269 static struct update_cb {
270 struct update_cb *next;
273 sem_t start_wait, end_wait;
275 /* set to 1 when starting the thread
276 * set to 0 to request thread termination */
277 volatile char running;
283 static void *run_update_callback(void *) __attribute__((noreturn));
285 static int threading_started = 0;
287 /* Register an update callback. Don't allow duplicates, to minimise side
288 * effects and overhead. */
289 void add_update_callback(void (*func)(void))
291 struct update_cb *uc = &update_cb_head;
297 if (uc->next->func == func)
302 uc->next = malloc(sizeof(struct update_cb));
305 memset(uc, 0, sizeof(struct update_cb));
307 sem_init(&uc->start_wait, 0, 0);
308 sem_init(&uc->end_wait, 0, 0);
310 if (threading_started) {
313 pthread_create(&uc->thread, NULL, &run_update_callback, uc);
318 /* Free the list element uc and all decendants recursively. */
319 static void __free_update_callbacks(struct update_cb *uc)
322 __free_update_callbacks(uc->next);
325 /* send cancellation request, then trigger and join the thread */
327 sem_post(&uc->start_wait);
329 if (pthread_join(uc->thread, NULL)) {
330 NORM_ERR("Error destroying thread");
333 /* finally destroy the semaphores */
334 sem_destroy(&uc->start_wait);
335 sem_destroy(&uc->end_wait);
340 /* Free the whole list of update callbacks. */
341 void free_update_callbacks(void)
343 if (update_cb_head.next)
344 __free_update_callbacks(update_cb_head.next);
345 update_cb_head.next = NULL;
348 /* We cannot start threads before we forked to background, because the threads
349 * would remain in the wrong process. Because of that, add_update_callback()
350 * doesn't create threads before start_update_threading() is called.
351 * start_update_threading() starts all threads previously registered, and sets a
352 * flag so that future threads are automagically started by
353 * add_update_callback().
354 * This text is almost longer than the actual code.
356 void start_update_threading(void)
358 struct update_cb *uc;
360 threading_started = 1;
362 for (uc = update_cb_head.next; uc; uc = uc->next) {
365 pthread_create(&uc->thread, NULL, &run_update_callback, uc);
370 static void *run_update_callback(void *data)
372 struct update_cb *ucb = data;
374 if (!ucb || !ucb->func) pthread_exit(NULL);
377 if (sem_wait(&ucb->start_wait)) pthread_exit(NULL);
378 if (ucb->running == 0) pthread_exit(NULL);
380 if (sem_post(&ucb->end_wait)) pthread_exit(NULL);
386 void update_stuff(void)
389 struct update_cb *uc;
391 /* clear speeds and up status in case device was removed and doesn't get
395 #pragma omp parallel for schedule(dynamic,10)
396 #endif /* HAVE_OPENMP */
397 for (i = 0; i < MAX_NET_INTERFACES; i++) {
398 if (netstats[i].dev) {
400 netstats[i].recv_speed = 0.0;
401 netstats[i].trans_speed = 0.0;
407 for (uc = update_cb_head.next; uc; uc = uc->next) {
408 if (sem_post(&uc->start_wait)) {
409 NORM_ERR("Semaphore error");
412 /* need to synchronise here, otherwise locking is needed (as data
413 * would be printed with some update callbacks still running) */
414 for (uc = update_cb_head.next; uc; uc = uc->next)
415 sem_wait(&uc->end_wait);
417 /* XXX: move the following into the update_meminfo() functions? */
419 info.mem -= info.bufmem;
420 info.memeasyfree += info.bufmem;
424 /* Ohkie to return negative values for temperatures */
425 int round_to_int_temp(float f)
428 return (int) (f + 0.5);
430 return (int) (f - 0.5);
433 /* Don't return negative values for cpugraph, bar, gauge, percentage.
434 * Causes unreasonable numbers to show */
435 unsigned int round_to_int(float f)
438 return (int) (f + 0.5);
444 void scan_loadavg_arg(struct text_object *obj, const char *arg)
447 if (arg && !arg[1] && isdigit(arg[0])) {
448 obj->data.i = atoi(arg);
449 if (obj->data.i > 3 || obj->data.i < 1) {
450 NORM_ERR("loadavg arg needs to be in range (1,3)");
454 /* convert to array index (or the default (-1)) */
458 void print_loadavg(struct text_object *obj, char *p, int p_max_size)
460 float *v = info.loadavg;
462 if (obj->data.i < 0) {
463 snprintf(p, p_max_size, "%.2f %.2f %.2f", v[0], v[1], v[2]);
465 snprintf(p, p_max_size, "%.2f", v[obj->data.i]);
470 void scan_loadgraph_arg(struct text_object *obj, const char *arg)
474 buf = scan_graph(obj, arg, 0);
479 void print_loadgraph(struct text_object *obj, char *p, int p_max_size)
481 new_graph(obj, p, p_max_size, info.loadavg[0]);