Bugfix: memory and thread-deleting problems
[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-2010 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 "timeinfo.h"
38 #include <ctype.h>
39 #include <errno.h>
40 #include <sys/time.h>
41 #include <sys/ioctl.h>
42 #include <net/if.h>
43 #include <netinet/in.h>
44 #include <pthread.h>
45 #include <semaphore.h>
46 #include <unistd.h>
47 #include "diskio.h"
48 #include <fcntl.h>
49
50 /* check for OS and include appropriate headers */
51 #if defined(__linux__)
52 #include "linux.h"
53 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
54 #include "freebsd.h"
55 #elif defined(__OpenBSD__)
56 #include "openbsd.h"
57 #endif
58
59 /* folds a string over top of itself, like so:
60  *
61  * if start is "blah", and you call it with count = 1, the result will be "lah"
62  */
63 void strfold(char *start, int count)
64 {
65         char *curplace;
66         for (curplace = start + count; *curplace != 0; curplace++) {
67                 *(curplace - count) = *curplace;
68         }
69         *(curplace - count) = 0;
70 }
71
72 #ifndef HAVE_STRNDUP
73 // use our own strndup() if it's not available
74 char *strndup(const char *s, size_t n)
75 {
76         if (strlen(s) > n) {
77                 char *ret = malloc(n + 1);
78                 strncpy(ret, s, n);
79                 ret[n] = 0;
80                 return ret;
81         } else {
82                 return strdup(s);
83         }
84 }
85 #endif /* HAVE_STRNDUP */
86
87 int update_uname(void)
88 {
89         uname(&info.uname_s);
90         return 0;
91 }
92
93 double get_time(void)
94 {
95         struct timeval tv;
96
97         gettimeofday(&tv, 0);
98         return tv.tv_sec + (tv.tv_usec / 1000000.0);
99 }
100
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)
105 {
106         char tmp[DEFAULT_TEXT_BUFFER_SIZE];
107         if (sscanf(source, "~/%s", tmp) || sscanf(source, "$HOME/%s", tmp)) {
108                 char *homedir = getenv("HOME");
109                 if (homedir) {
110                         snprintf(dest, DEFAULT_TEXT_BUFFER_SIZE, "%s/%s", homedir, tmp);
111                 } else {
112                         NORM_ERR("$HOME environment variable doesn't exist");
113                         strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
114                 }
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);
117         }
118 }
119
120 int open_fifo(const char *file, int *reported)
121 {
122         char path[DEFAULT_TEXT_BUFFER_SIZE];
123         int fd = 0;
124
125         to_real_path(path, file);
126         fd = open(file, O_RDONLY | O_NONBLOCK);
127
128         if (fd == -1) {
129                 if (!reported || *reported == 0) {
130                         NORM_ERR("can't open %s: %s", file, strerror(errno));
131                         if (reported) {
132                                 *reported = 1;
133                         }
134                 }
135                 return -1;
136         }
137
138         return fd;
139 }
140
141 FILE *open_file(const char *file, int *reported)
142 {
143         char path[DEFAULT_TEXT_BUFFER_SIZE];
144         FILE *fp = 0;
145
146         to_real_path(path, file);
147         fp = fopen(file, "r");
148
149         if (!fp) {
150                 if (!reported || *reported == 0) {
151                         NORM_ERR("can't open %s: %s", file, strerror(errno));
152                         if (reported) {
153                                 *reported = 1;
154                         }
155                 }
156                 return NULL;
157         }
158
159         return fp;
160 }
161
162 void variable_substitute(const char *s, char *dest, unsigned int n)
163 {
164         while (*s && n > 1) {
165                 if (*s == '$') {
166                         s++;
167                         if (*s != '$') {
168                                 char buf[256];
169                                 const char *a, *var;
170                                 unsigned int len;
171
172                                 /* variable is either $foo or ${foo} */
173                                 if (*s == '{') {
174                                         s++;
175                                         a = s;
176                                         while (*s && *s != '}') {
177                                                 s++;
178                                         }
179                                 } else {
180                                         a = s;
181                                         while (*s && (isalnum((int) *s) || *s == '_')) {
182                                                 s++;
183                                         }
184                                 }
185
186                                 /* copy variable to buffer and look it up */
187                                 len = (s - a > 255) ? 255 : (s - a);
188                                 strncpy(buf, a, len);
189                                 buf[len] = '\0';
190
191                                 if (*s == '}') {
192                                         s++;
193                                 }
194
195                                 var = getenv(buf);
196
197                                 if (var) {
198                                         /* add var to dest */
199                                         len = strlen(var);
200                                         if (len >= n) {
201                                                 len = n - 1;
202                                         }
203                                         strncpy(dest, var, len);
204                                         dest += len;
205                                         n -= len;
206                                 }
207                                 continue;
208                         }
209                 }
210
211                 *dest++ = *s++;
212                 n--;
213         }
214
215         *dest = '\0';
216 }
217
218 void format_seconds(char *buf, unsigned int n, long seconds)
219 {
220         long days;
221         int hours, minutes;
222
223         if (times_in_seconds()) {
224                 snprintf(buf, n, "%ld", seconds);
225                 return;
226         }
227
228         days = seconds / 86400;
229         seconds %= 86400;
230         hours = seconds / 3600;
231         seconds %= 3600;
232         minutes = seconds / 60;
233         seconds %= 60;
234
235         if (days > 0) {
236                 snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
237         } else {
238                 snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
239         }
240 }
241
242 void format_seconds_short(char *buf, unsigned int n, long seconds)
243 {
244         long days;
245         int hours, minutes;
246
247         if (times_in_seconds()) {
248                 snprintf(buf, n, "%ld", seconds);
249                 return;
250         }
251
252         days = seconds / 86400;
253         seconds %= 86400;
254         hours = seconds / 3600;
255         seconds %= 3600;
256         minutes = seconds / 60;
257         seconds %= 60;
258
259         if (days > 0) {
260                 snprintf(buf, n, "%ldd %dh", days, hours);
261         } else if (hours > 0) {
262                 snprintf(buf, n, "%dh %dm", hours, minutes);
263         } else {
264                 snprintf(buf, n, "%dm %lds", minutes, seconds);
265         }
266 }
267
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;
272         int (*func)(void);
273         pthread_t thread;
274         sem_t start_wait, end_wait;
275
276     /* set to 1 when starting the thread
277          * set to 0 to request thread termination */
278         volatile char running;
279 } update_cb_head = {
280         .next = NULL,
281         .func = NULL,
282 };
283
284 static void *run_update_callback(void *) __attribute__((noreturn));
285
286 static int threading_started = 0;
287
288 /* Register an update callback. Don't allow duplicates, to minimise side
289  * effects and overhead. */
290 void add_update_callback(int (*func)(void))
291 {
292         struct update_cb *uc = &update_cb_head;
293
294         if (!func)
295                 return;
296
297         while (uc->next) {
298                 if (uc->next->func == func)
299                         return;
300                 uc = uc->next;
301         }
302
303         uc->next = malloc(sizeof(struct update_cb));
304         uc = uc->next;
305
306         memset(uc, 0, sizeof(struct update_cb));
307         uc->func = func;
308         sem_init(&uc->start_wait, 0, 0);
309         sem_init(&uc->end_wait, 0, 0);
310
311         if (threading_started) {
312                 if (!uc->running) {
313                         uc->running = 1;
314                         pthread_create(&uc->thread, NULL, &run_update_callback, uc);
315                 }
316         }
317 }
318
319 /* Free the list element uc and all decendants recursively. */
320 static void __free_update_callbacks(struct update_cb *uc)
321 {
322         if (uc->next)
323                 __free_update_callbacks(uc->next);
324
325         if (uc->running) {
326                 /* send cancellation request, then trigger and join the thread */
327                 uc->running = 0;
328                 sem_post(&uc->start_wait);
329         }
330         if (pthread_join(uc->thread, NULL)) {
331                 NORM_ERR("Error destroying thread");
332         }
333
334         /* finally destroy the semaphores */
335         sem_destroy(&uc->start_wait);
336         sem_destroy(&uc->end_wait);
337
338         free(uc);
339 }
340
341 /* Free the whole list of update callbacks. */
342 void free_update_callbacks(void)
343 {
344         if (update_cb_head.next)
345                 __free_update_callbacks(update_cb_head.next);
346         update_cb_head.next = NULL;
347 }
348
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.
356  */
357 void start_update_threading(void)
358 {
359         struct update_cb *uc;
360
361         threading_started = 1;
362
363         for (uc = update_cb_head.next; uc; uc = uc->next) {
364                 if (!uc->running) {
365                         uc->running = 1;
366                         pthread_create(&uc->thread, NULL, &run_update_callback, uc);
367                 }
368         }
369 }
370
371 static void *run_update_callback(void *data)
372 {
373         struct update_cb *ucb = data;
374
375         if (!ucb || !ucb->func) pthread_exit(NULL);
376
377         while (1) {
378                 if (sem_wait(&ucb->start_wait)) pthread_exit(NULL);
379                 if (ucb->running == 0) pthread_exit(NULL);
380                 if((*ucb->func)()) {
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);
384                         pthread_exit(NULL);
385                 }
386                 if (sem_post(&ucb->end_wait)) pthread_exit(NULL);
387         }
388 }
389
390 int no_buffers;
391
392 void update_stuff(void)
393 {
394         int i;
395         struct update_cb *uc;
396
397         /* clear speeds and up status in case device was removed and doesn't get
398          * updated */
399
400         #ifdef HAVE_OPENMP
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) {
405                         netstats[i].up = 0;
406                         netstats[i].recv_speed = 0.0;
407                         netstats[i].trans_speed = 0.0;
408                 }
409         }
410
411         prepare_update();
412
413         for (uc = update_cb_head.next; uc; uc = uc->next) {
414                 if (sem_post(&uc->start_wait)) {
415                         NORM_ERR("Semaphore error");
416                 }
417         }
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);
422                 if(uc == uc->next) {
423                         pthread_join(uc->thread, NULL);
424                         free(uc);
425                         exit(EXIT_FAILURE);
426                 }
427         }
428
429         /* XXX: move the following into the update_meminfo() functions? */
430         if (no_buffers) {
431                 info.mem -= info.bufmem;
432                 info.memeasyfree += info.bufmem;
433         }
434 }
435
436 /* Ohkie to return negative values for temperatures */
437 int round_to_int_temp(float f)
438 {
439         if (f >= 0.0) {
440                 return (int) (f + 0.5);
441         } else {
442                 return (int) (f - 0.5);
443         }
444 }
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)
448 {
449         if (f >= 0.0) {
450                 return (int) (f + 0.5);
451         } else {
452                 return 0;
453         }
454 }
455
456 void scan_loadavg_arg(struct text_object *obj, const char *arg)
457 {
458         obj->data.i = 0;
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)");
463                         obj->data.i = 0;
464                 }
465         }
466         /* convert to array index (or the default (-1)) */
467         obj->data.i--;
468 }
469
470 void print_loadavg(struct text_object *obj, char *p, int p_max_size)
471 {
472         float *v = info.loadavg;
473
474         if (obj->data.i < 0) {
475                 snprintf(p, p_max_size, "%.2f %.2f %.2f", v[0], v[1], v[2]);
476         } else {
477                 snprintf(p, p_max_size, "%.2f", v[obj->data.i]);
478         }
479 }
480
481 #ifdef X11
482 void scan_loadgraph_arg(struct text_object *obj, const char *arg)
483 {
484         char *buf = 0;
485
486         buf = scan_graph(obj, arg, 0);
487         if (buf)
488                 free(buf);
489 }
490
491 void print_loadgraph(struct text_object *obj, char *p, int p_max_size)
492 {
493         new_graph(obj, p, p_max_size, info.loadavg[0]);
494 }
495 #endif /* X11 */