Split conky.h into several smaller header files
[monky] / src / common.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10  * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
11  *      (see AUTHORS)
12  * All rights reserved.
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  *
26  * $Id$ */
27
28 #include "conky.h"
29 #include <ctype.h>
30 #include <errno.h>
31 #include <sys/time.h>
32 #include <pthread.h>
33
34 #ifndef HAVE_STRNDUP
35 // use our own strndup() if it's not available
36 char *strndup(const char *s, size_t n)
37 {
38         if (strlen(s) + 1 > n) {
39                 char *ret = malloc(n);
40                 strncpy(ret, s, n);
41                 return ret;
42         } else {
43                 return strdup(s);
44         }
45 }
46 #endif /* HAVE_STRNDUP */
47
48 void update_uname(void)
49 {
50         uname(&info.uname_s);
51 }
52
53 double get_time(void)
54 {
55         struct timeval tv;
56
57         gettimeofday(&tv, 0);
58         return tv.tv_sec + (tv.tv_usec / 1000000.0);
59 }
60
61 FILE *open_file(const char *file, int *reported)
62 {
63         FILE *fp = fopen(file, "r");
64
65         if (!fp) {
66                 if (!reported || *reported == 0) {
67                         ERR("can't open %s: %s", file, strerror(errno));
68                         if (reported) {
69                                 *reported = 1;
70                         }
71                 }
72                 return 0;
73         }
74
75         return fp;
76 }
77
78 void variable_substitute(const char *s, char *dest, unsigned int n)
79 {
80         while (*s && n > 1) {
81                 if (*s == '$') {
82                         s++;
83                         if (*s != '$') {
84                                 char buf[256];
85                                 const char *a, *var;
86                                 unsigned int len;
87
88                                 /* variable is either $foo or ${foo} */
89                                 if (*s == '{') {
90                                         s++;
91                                         a = s;
92                                         while (*s && *s != '}') {
93                                                 s++;
94                                         }
95                                 } else {
96                                         a = s;
97                                         while (*s && (isalnum((int) *s) || *s == '_')) {
98                                                 s++;
99                                         }
100                                 }
101
102                                 /* copy variable to buffer and look it up */
103                                 len = (s - a > 255) ? 255 : (s - a);
104                                 strncpy(buf, a, len);
105                                 buf[len] = '\0';
106
107                                 if (*s == '}') {
108                                         s++;
109                                 }
110
111                                 var = getenv(buf);
112
113                                 if (var) {
114                                         /* add var to dest */
115                                         len = strlen(var);
116                                         if (len >= n) {
117                                                 len = n - 1;
118                                         }
119                                         strncpy(dest, var, len);
120                                         dest += len;
121                                         n -= len;
122                                 }
123                                 continue;
124                         }
125                 }
126
127                 *dest++ = *s++;
128                 n--;
129         }
130
131         *dest = '\0';
132 }
133
134 /* network interface stuff */
135
136 static struct net_stat netstats[16];
137
138 struct net_stat *get_net_stat(const char *dev)
139 {
140         unsigned int i;
141
142         if (!dev) {
143                 return 0;
144         }
145
146         /* find interface stat */
147         for (i = 0; i < 16; i++) {
148                 if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
149                         return &netstats[i];
150                 }
151         }
152
153         /* wasn't found? add it */
154         if (i == 16) {
155                 for (i = 0; i < 16; i++) {
156                         if (netstats[i].dev == 0) {
157                                 netstats[i].dev = strndup(dev, text_buffer_size);
158                                 return &netstats[i];
159                         }
160                 }
161         }
162
163         CRIT_ERR("too many interfaces used (limit is 16)");
164         return 0;
165 }
166
167 void clear_net_stats(void)
168 {
169         memset(netstats, 0, sizeof(netstats));
170 }
171
172 void free_dns_data(void)
173 {
174         int i;
175         struct dns_data *data = &info.nameserver_info;
176         for (i = 0; i < data->nscount; i++)
177                 free(data->ns_list[i]);
178         if (data->ns_list)
179                 free(data->ns_list);
180         memset(data, 0, sizeof(struct dns_data));
181 }
182
183 //static double last_dns_update;
184
185 void update_dns_data(void)
186 {
187         FILE *fp;
188         char line[256];
189         struct dns_data *data = &info.nameserver_info;
190
191         /* maybe updating too often causes higher load because of /etc lying on a real FS
192         if (current_update_time - last_dns_update < 10.0)
193                 return;
194         else
195                 last_dns_update = current_update_time;
196         */
197
198         free_dns_data();
199
200         if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
201                 return;
202         while(!feof(fp)) {
203                 if (fgets(line, 255, fp) == NULL) {
204                         break;
205                 }
206                 if (!strncmp(line, "nameserver ", 11)) {
207                         line[strlen(line) - 1] = '\0';  // remove trailing newline
208                         data->nscount++;
209                         data->ns_list = realloc(data->ns_list, data->nscount * sizeof(char *));
210                         data->ns_list[data->nscount - 1] = strndup(line + 11, text_buffer_size);
211                 }
212         }
213         fclose(fp);
214 }
215
216 void format_seconds(char *buf, unsigned int n, long t)
217 {
218         if (t >= 24 * 60 * 60) {        /* hours necessary when there are days? */
219                 snprintf(buf, n, "%ldd %ldh %ldm", t / 60 / 60 / 24, (t / 60 / 60) % 24,
220                         (t / 60) % 60);
221         } else if (t >= 60 * 60) {
222                 snprintf(buf, n, "%ldh %ldm", (t / 60 / 60) % 24, (t / 60) % 60);
223         } else {
224                 snprintf(buf, n, "%ldm %lds", t / 60, t % 60);
225         }
226 }
227
228 void format_seconds_short(char *buf, unsigned int n, long t)
229 {
230         if (t >= 24 * 60 * 60) {
231                 snprintf(buf, n, "%ldd %ldh", t / 60 / 60 / 24, (t / 60 / 60) % 24);
232         } else if (t >= 60 * 60) {
233                 snprintf(buf, n, "%ldh %ldm", (t / 60 / 60) % 24, (t / 60) % 60);
234         } else {
235                 snprintf(buf, n, "%ldm", t / 60);
236         }
237 }
238
239 static double last_meminfo_update;
240 static double last_fs_update;
241
242 unsigned long long need_mask;
243
244 #define NEED(a) ((need_mask & (1 << a)) && ((info.mask & (1 << a)) == 0))
245
246 void update_stuff(void)
247 {
248         unsigned int i;
249
250         info.mask = 0;
251
252         if (no_buffers) {
253                 need_mask |= 1 << INFO_BUFFERS;
254         }
255
256         /* clear speeds and up status in case device was removed and doesn't get
257          * updated */
258
259         for (i = 0; i < 16; i++) {
260                 if (netstats[i].dev) {
261                         netstats[i].up = 0;
262                         netstats[i].recv_speed = 0.0;
263                         netstats[i].trans_speed = 0.0;
264                 }
265         }
266
267         prepare_update();
268
269         if (NEED(INFO_UPTIME)) {
270                 update_uptime();
271         }
272
273         if (NEED(INFO_PROCS)) {
274                 update_total_processes();
275         }
276
277         if (NEED(INFO_RUN_PROCS)) {
278                 update_running_processes();
279         }
280
281         if (NEED(INFO_CPU)) {
282                 update_cpu_usage();
283         }
284
285         if (NEED(INFO_NET)) {
286                 update_net_stats();
287         }
288
289         if (NEED(INFO_DISKIO)) {
290                 update_diskio();
291         }
292
293 #if defined(__linux__)
294         if (NEED(INFO_I8K)) {
295                 update_i8k();
296         }
297 #endif /* __linux__ */
298
299 #ifdef MPD
300         if (NEED(INFO_MPD)) {
301                 if (!info.mpd.timed_thread) {
302                         init_mpd_stats(&info.mpd);
303                         info.mpd.timed_thread = timed_thread_create(&update_mpd,
304                                 (void *) &info.mpd, info.music_player_interval * 1000000);
305                         if (!info.mpd.timed_thread) {
306                                 ERR("Failed to create MPD timed thread");
307                         }
308                         timed_thread_register(info.mpd.timed_thread, &info.mpd.timed_thread);
309                         if (timed_thread_run(info.mpd.timed_thread)) {
310                                 ERR("Failed to run MPD timed thread");
311                         }
312                 }
313         }
314 #endif
315
316 #ifdef XMMS2
317         if (NEED(INFO_XMMS2)) {
318                 update_xmms2();
319         }
320 #endif
321
322 #ifdef AUDACIOUS
323         if (NEED(INFO_AUDACIOUS)) {
324                 update_audacious();
325         }
326 #endif
327
328 #ifdef BMPX
329         if (NEED(INFO_BMPX)) {
330                 update_bmpx();
331         }
332 #endif
333
334         if (NEED(INFO_LOADAVG)) {
335                 update_load_average();
336         }
337
338         if ((NEED(INFO_MEM) || NEED(INFO_BUFFERS) || NEED(INFO_TOP))
339                         && current_update_time - last_meminfo_update > 6.9) {
340                 update_meminfo();
341                 if (no_buffers) {
342                         info.mem -= info.bufmem;
343                 }
344                 last_meminfo_update = current_update_time;
345         }
346
347         if (NEED(INFO_TOP)) {
348                 update_top();
349         }
350
351         /* update_fs_stat() won't do anything if there aren't fs -things */
352         if (NEED(INFO_FS) && current_update_time - last_fs_update > 12.9) {
353                 update_fs_stats();
354                 last_fs_update = current_update_time;
355         }
356 #ifdef TCP_PORT_MONITOR
357         if (NEED(INFO_TCP_PORT_MONITOR)) {
358                 update_tcp_port_monitor_collection(info.p_tcp_port_monitor_collection);
359         }
360 #endif
361         if (NEED(INFO_ENTROPY)) {
362                 update_entropy();
363         }
364         if (NEED(INFO_USERS)) {
365                 update_users();
366         }
367         if (NEED(INFO_GW)) {
368                 update_gateway_info();
369         }
370         if (NEED(INFO_DNS)) {
371                 update_dns_data();
372         }
373 }
374
375 int round_to_int(float f)
376 {
377         if (f >= 0.0) {
378                 return (int) (f + 0.5);
379         } else {
380                 return (int) (f - 0.5);
381         }
382 }