specials: convert graph objects to new style
[monky] / src / common.c
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
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
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.
21  *
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/>.
28  *
29  */
30
31 #include "config.h"
32 #include "conky.h"
33 #include "fs.h"
34 #include "logging.h"
35 #include "net_stat.h"
36 #include "specials.h"
37 #include <ctype.h>
38 #include <errno.h>
39 #include <sys/time.h>
40 #include <sys/ioctl.h>
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <pthread.h>
44 #include <semaphore.h>
45 #include <unistd.h>
46 #include "diskio.h"
47 #include <fcntl.h>
48
49 /* check for OS and include appropriate headers */
50 #if defined(__linux__)
51 #include "linux.h"
52 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
53 #include "freebsd.h"
54 #elif defined(__OpenBSD__)
55 #include "openbsd.h"
56 #endif
57
58 /* folds a string over top of itself, like so:
59  *
60  * if start is "blah", and you call it with count = 1, the result will be "lah"
61  */
62 void strfold(char *start, int count)
63 {
64         char *curplace;
65         for (curplace = start + count; *curplace != 0; curplace++) {
66                 *(curplace - count) = *curplace;
67         }
68         *(curplace - count) = 0;
69 }
70
71 #ifndef HAVE_STRNDUP
72 // use our own strndup() if it's not available
73 char *strndup(const char *s, size_t n)
74 {
75         if (strlen(s) > n) {
76                 char *ret = malloc(n + 1);
77                 strncpy(ret, s, n);
78                 ret[n] = 0;
79                 return ret;
80         } else {
81                 return strdup(s);
82         }
83 }
84 #endif /* HAVE_STRNDUP */
85
86 void update_uname(void)
87 {
88         uname(&info.uname_s);
89 }
90
91 double get_time(void)
92 {
93         struct timeval tv;
94
95         gettimeofday(&tv, 0);
96         return tv.tv_sec + (tv.tv_usec / 1000000.0);
97 }
98
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)
103 {
104         char tmp[DEFAULT_TEXT_BUFFER_SIZE];
105         if (sscanf(source, "~/%s", tmp) || sscanf(source, "$HOME/%s", tmp)) {
106                 char *homedir = getenv("HOME");
107                 if (homedir) {
108                         snprintf(dest, DEFAULT_TEXT_BUFFER_SIZE, "%s/%s", homedir, tmp);
109                 } else {
110                         NORM_ERR("$HOME environment variable doesn't exist");
111                         strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
112                 }
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);
115         }
116 }
117
118 int open_fifo(const char *file, int *reported)
119 {
120         char path[DEFAULT_TEXT_BUFFER_SIZE];
121         int fd = 0;
122
123         to_real_path(path, file);
124         fd = open(file, O_RDONLY | O_NONBLOCK);
125
126         if (fd == -1) {
127                 if (!reported || *reported == 0) {
128                         NORM_ERR("can't open %s: %s", file, strerror(errno));
129                         if (reported) {
130                                 *reported = 1;
131                         }
132                 }
133                 return -1;
134         }
135
136         return fd;
137 }
138
139 FILE *open_file(const char *file, int *reported)
140 {
141         char path[DEFAULT_TEXT_BUFFER_SIZE];
142         FILE *fp = 0;
143
144         to_real_path(path, file);
145         fp = fopen(file, "r");
146
147         if (!fp) {
148                 if (!reported || *reported == 0) {
149                         NORM_ERR("can't open %s: %s", file, strerror(errno));
150                         if (reported) {
151                                 *reported = 1;
152                         }
153                 }
154                 return NULL;
155         }
156
157         return fp;
158 }
159
160 void variable_substitute(const char *s, char *dest, unsigned int n)
161 {
162         while (*s && n > 1) {
163                 if (*s == '$') {
164                         s++;
165                         if (*s != '$') {
166                                 char buf[256];
167                                 const char *a, *var;
168                                 unsigned int len;
169
170                                 /* variable is either $foo or ${foo} */
171                                 if (*s == '{') {
172                                         s++;
173                                         a = s;
174                                         while (*s && *s != '}') {
175                                                 s++;
176                                         }
177                                 } else {
178                                         a = s;
179                                         while (*s && (isalnum((int) *s) || *s == '_')) {
180                                                 s++;
181                                         }
182                                 }
183
184                                 /* copy variable to buffer and look it up */
185                                 len = (s - a > 255) ? 255 : (s - a);
186                                 strncpy(buf, a, len);
187                                 buf[len] = '\0';
188
189                                 if (*s == '}') {
190                                         s++;
191                                 }
192
193                                 var = getenv(buf);
194
195                                 if (var) {
196                                         /* add var to dest */
197                                         len = strlen(var);
198                                         if (len >= n) {
199                                                 len = n - 1;
200                                         }
201                                         strncpy(dest, var, len);
202                                         dest += len;
203                                         n -= len;
204                                 }
205                                 continue;
206                         }
207                 }
208
209                 *dest++ = *s++;
210                 n--;
211         }
212
213         *dest = '\0';
214 }
215
216 void format_seconds(char *buf, unsigned int n, long seconds)
217 {
218         long days;
219         int hours, minutes;
220
221         days = seconds / 86400;
222         seconds %= 86400;
223         hours = seconds / 3600;
224         seconds %= 3600;
225         minutes = seconds / 60;
226         seconds %= 60;
227
228         if (days > 0) {
229                 snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
230         } else {
231                 snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
232         }
233 }
234
235 void format_seconds_short(char *buf, unsigned int n, long seconds)
236 {
237         long days;
238         int hours, minutes;
239
240         days = seconds / 86400;
241         seconds %= 86400;
242         hours = seconds / 3600;
243         seconds %= 3600;
244         minutes = seconds / 60;
245         seconds %= 60;
246
247         if (days > 0) {
248                 snprintf(buf, n, "%ldd %dh", days, hours);
249         } else if (hours > 0) {
250                 snprintf(buf, n, "%dh %dm", hours, minutes);
251         } else {
252                 snprintf(buf, n, "%dm %lds", minutes, seconds);
253         }
254 }
255
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;
260         void (*func)(void);
261         pthread_t thread;
262         sem_t start_wait, end_wait;
263
264     /* set to 1 when starting the thread
265          * set to 0 to request thread termination */
266         volatile char running;
267 } update_cb_head = {
268         .next = NULL,
269 };
270
271 static void *run_update_callback(void *);
272 static int threading_started;
273
274 /* Register an update callback. Don't allow duplicates, to minimise side
275  * effects and overhead. */
276 void add_update_callback(void (*func)(void))
277 {
278         struct update_cb *uc = &update_cb_head;
279
280         if (!func)
281                 return;
282
283         while (uc->next) {
284                 if (uc->next->func == func)
285                         return;
286                 uc = uc->next;
287         }
288
289         uc->next = malloc(sizeof(struct update_cb));
290         uc = uc->next;
291
292         memset(uc, 0, sizeof(struct update_cb));
293         uc->func = func;
294         sem_init(&uc->start_wait, 0, 0);
295         sem_init(&uc->end_wait, 0, 0);
296
297         if (threading_started) {
298                 uc->running = 1;
299                 pthread_create(&uc->thread, NULL, &run_update_callback, uc);
300         }
301 }
302
303 /* Free the list element uc and all decendants recursively. */
304 static void __free_update_callbacks(struct update_cb *uc)
305 {
306         if (uc->next)
307                 __free_update_callbacks(uc->next);
308
309         if (uc->running) {
310                 /* send cancellation request, then trigger and join the thread */
311                 uc->running = 0;
312                 sem_post(&uc->start_wait);
313                 pthread_join(uc->thread, NULL);
314         }
315
316         /* finally destroy the semaphores */
317         sem_destroy(&uc->start_wait);
318         sem_destroy(&uc->end_wait);
319
320         free(uc);
321 }
322
323 /* Free the whole list of update callbacks. */
324 void free_update_callbacks(void)
325 {
326         if (update_cb_head.next)
327                 __free_update_callbacks(update_cb_head.next);
328         update_cb_head.next = NULL;
329 }
330
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.
338  */
339 void start_update_threading(void)
340 {
341         struct update_cb *uc;
342
343         threading_started = 1;
344
345         for(uc = update_cb_head.next; uc; uc = uc->next) {
346                 uc->running = 1;
347                 pthread_create(&uc->thread, NULL, &run_update_callback, uc);
348         }
349 }
350
351 static void *run_update_callback(void *data)
352 {
353         struct update_cb *ucb = data;
354
355         while (1) {
356                 sem_wait(&ucb->start_wait);
357                 if (ucb->running == 0)
358                         return NULL;
359                 (*ucb->func)();
360                 sem_post(&ucb->end_wait);
361         }
362 }
363
364 int no_buffers;
365
366 void update_stuff(void)
367 {
368         int i;
369         struct update_cb *uc;
370
371         /* clear speeds and up status in case device was removed and doesn't get
372          * updated */
373
374         #ifdef HAVE_OPENMP
375         #pragma omp parallel for schedule(dynamic,10)
376         #endif /* HAVE_OPENMP */
377         for (i = 0; i < 16; i++) {
378                 if (netstats[i].dev) {
379                         netstats[i].up = 0;
380                         netstats[i].recv_speed = 0.0;
381                         netstats[i].trans_speed = 0.0;
382                 }
383         }
384
385         prepare_update();
386
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);
393
394         /* XXX: move the following into the update_meminfo() functions? */
395         if (no_buffers) {
396                 info.mem -= info.bufmem;
397                 info.memeasyfree += info.bufmem;
398         }
399 }
400
401 /* Ohkie to return negative values for temperatures */
402 int round_to_int_temp(float f)
403 {
404         if (f >= 0.0) {
405                 return (int) (f + 0.5);
406         } else {
407                 return (int) (f - 0.5);
408         }
409 }
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)
413 {
414         if (f >= 0.0) {
415                 return (int) (f + 0.5);
416         } else {
417                 return 0;
418         }
419 }
420
421 void scan_loadavg_arg(struct text_object *obj, const char *arg)
422 {
423         obj->data.i = 0;
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)");
428                         obj->data.i = 0;
429                 }
430         }
431         /* convert to array index (or the default (-1)) */
432         obj->data.i--;
433 }
434
435 void print_loadavg(struct text_object *obj, char *p, int p_max_size)
436 {
437         float *v = info.loadavg;
438
439         if (obj->data.i < 0) {
440                 snprintf(p, p_max_size, "%.2f %.2f %.2f", v[0], v[1], v[2]);
441         } else {
442                 snprintf(p, p_max_size, "%.2f", v[obj->data.i]);
443         }
444 }
445
446 #ifdef X11
447 void scan_loadgraph_arg(struct text_object *obj, const char *arg)
448 {
449         char *buf = 0;
450
451         buf = scan_graph(obj, arg);
452         if (buf)
453                 free(buf);
454 }
455
456 void print_loadgraph(struct text_object *obj, char *p)
457 {
458         new_graph(obj, p, info.loadavg[0]);
459 }
460 #endif /* X11 */