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/>.
39 #include <sys/ioctl.h>
41 #include <netinet/in.h>
43 #include <semaphore.h>
48 /* check for OS and include appropriate headers */
49 #if defined(__linux__)
51 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
53 #elif defined(__OpenBSD__)
57 /* folds a string over top of itself, like so:
59 * if start is "blah", and you call it with count = 1, the result will be "lah"
61 void strfold(char *start, int count)
64 for (curplace = start + count; *curplace != 0; curplace++) {
65 *(curplace - count) = *curplace;
67 *(curplace - count) = 0;
71 // use our own strndup() if it's not available
72 char *strndup(const char *s, size_t n)
75 char *ret = malloc(n + 1);
83 #endif /* HAVE_STRNDUP */
85 void update_uname(void)
95 return tv.tv_sec + (tv.tv_usec / 1000000.0);
98 /* Converts '~/...' paths to '/home/blah/...' assumes that 'dest' is at least
99 * DEFAULT_TEXT_BUFFER_SIZE. It's similar to variable_substitute, except only
100 * cheques for $HOME and ~/ in path */
101 void to_real_path(char *dest, const char *source)
103 char tmp[DEFAULT_TEXT_BUFFER_SIZE];
104 if (sscanf(source, "~/%s", tmp) || sscanf(source, "$HOME/%s", tmp)) {
105 char *homedir = getenv("HOME");
107 snprintf(dest, DEFAULT_TEXT_BUFFER_SIZE, "%s/%s", homedir, tmp);
109 NORM_ERR("$HOME environment variable doesn't exist");
110 strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
112 } else if (dest != source) { //see changelog 2009-06-29 if you doubt that this check is necessary
113 strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
117 int open_fifo(const char *file, int *reported)
119 char path[DEFAULT_TEXT_BUFFER_SIZE];
122 to_real_path(path, file);
123 fd = open(file, O_RDONLY | O_NONBLOCK);
126 if (!reported || *reported == 0) {
127 NORM_ERR("can't open %s: %s", file, strerror(errno));
138 FILE *open_file(const char *file, int *reported)
140 char path[DEFAULT_TEXT_BUFFER_SIZE];
143 to_real_path(path, file);
144 fp = fopen(file, "r");
147 if (!reported || *reported == 0) {
148 NORM_ERR("can't open %s: %s", file, strerror(errno));
159 void variable_substitute(const char *s, char *dest, unsigned int n)
161 while (*s && n > 1) {
169 /* variable is either $foo or ${foo} */
173 while (*s && *s != '}') {
178 while (*s && (isalnum((int) *s) || *s == '_')) {
183 /* copy variable to buffer and look it up */
184 len = (s - a > 255) ? 255 : (s - a);
185 strncpy(buf, a, len);
195 /* add var to dest */
200 strncpy(dest, var, len);
215 void format_seconds(char *buf, unsigned int n, long seconds)
220 days = seconds / 86400;
222 hours = seconds / 3600;
224 minutes = seconds / 60;
228 snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
230 snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
234 void format_seconds_short(char *buf, unsigned int n, long seconds)
239 days = seconds / 86400;
241 hours = seconds / 3600;
243 minutes = seconds / 60;
247 snprintf(buf, n, "%ldd %dh", days, hours);
248 } else if (hours > 0) {
249 snprintf(buf, n, "%dh %dm", hours, minutes);
251 snprintf(buf, n, "%dm %lds", minutes, seconds);
255 /* Linked list containing the functions to call upon each update interval.
256 * Populated while initialising text objects in construct_text_object(). */
257 static struct update_cb {
258 struct update_cb *next;
261 sem_t start_wait, end_wait;
263 /* set to 1 when starting the thread
264 * set to 0 to request thread termination */
265 volatile char running;
270 static void *run_update_callback(void *);
271 static int threading_started;
273 /* Register an update callback. Don't allow duplicates, to minimise side
274 * effects and overhead. */
275 void add_update_callback(void (*func)(void))
277 struct update_cb *uc = &update_cb_head;
283 if (uc->next->func == func)
288 uc->next = malloc(sizeof(struct update_cb));
291 memset(uc, 0, sizeof(struct update_cb));
293 sem_init(&uc->start_wait, 0, 0);
294 sem_init(&uc->end_wait, 0, 0);
296 if (threading_started) {
298 pthread_create(&uc->thread, NULL, &run_update_callback, uc);
302 /* Free the list element uc and all decendants recursively. */
303 static void __free_update_callbacks(struct update_cb *uc)
306 __free_update_callbacks(uc->next);
309 /* send cancellation request, then trigger and join the thread */
311 sem_post(&uc->start_wait);
312 pthread_join(uc->thread, NULL);
315 /* finally destroy the semaphores */
316 sem_destroy(&uc->start_wait);
317 sem_destroy(&uc->end_wait);
322 /* Free the whole list of update callbacks. */
323 void free_update_callbacks(void)
325 if (update_cb_head.next)
326 __free_update_callbacks(update_cb_head.next);
327 update_cb_head.next = NULL;
330 /* We cannot start threads before we forked to background, because the threads
331 * would remain in the wrong process. Because of that, add_update_callback()
332 * doesn't create threads before start_update_threading() is called.
333 * start_update_threading() starts all threads previously registered, and sets a
334 * flag so that future threads are automagically started by
335 * add_update_callback().
336 * This text is almost longer than the actual code.
338 void start_update_threading(void)
340 struct update_cb *uc;
342 threading_started = 1;
344 for(uc = update_cb_head.next; uc; uc = uc->next) {
346 pthread_create(&uc->thread, NULL, &run_update_callback, uc);
350 static void *run_update_callback(void *data)
352 struct update_cb *ucb = data;
355 sem_wait(&ucb->start_wait);
356 if (ucb->running == 0)
359 sem_post(&ucb->end_wait);
365 void update_stuff(void)
368 struct update_cb *uc;
370 /* clear speeds and up status in case device was removed and doesn't get
374 #pragma omp parallel for schedule(dynamic,10)
375 #endif /* HAVE_OPENMP */
376 for (i = 0; i < 16; i++) {
377 if (netstats[i].dev) {
379 netstats[i].recv_speed = 0.0;
380 netstats[i].trans_speed = 0.0;
386 for (uc = update_cb_head.next; uc; uc = uc->next)
387 sem_post(&uc->start_wait);
388 /* need to synchronise here, otherwise locking is needed (as data
389 * would be printed with some update callbacks still running) */
390 for (uc = update_cb_head.next; uc; uc = uc->next)
391 sem_wait(&uc->end_wait);
393 /* XXX: move the following into the update_meminfo() functions? */
395 info.mem -= info.bufmem;
396 info.memeasyfree += info.bufmem;
400 /* Ohkie to return negative values for temperatures */
401 int round_to_int_temp(float f)
404 return (int) (f + 0.5);
406 return (int) (f - 0.5);
409 /* Don't return negative values for cpugraph, bar, gauge, percentage.
410 * Causes unreasonable numbers to show */
411 unsigned int round_to_int(float f)
414 return (int) (f + 0.5);