Allow method to pass execgraph arguments containing spaces.
[monky] / src / fs.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 "conky.h"
32 #include "logging.h"
33 #include "fs.h"
34 #include "specials.h"
35 #include "text_object.h"
36 #include <ctype.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <sys/types.h>
40 #include <fcntl.h>
41
42 /* linux */
43 #ifdef HAVE_SYS_STATFS_H
44 #include <sys/statfs.h>
45 #endif
46
47 /* freebsd && netbsd */
48 #ifdef HAVE_SYS_PARAM_H
49 #include <sys/param.h>
50 #endif
51 #ifdef HAVE_SYS_MOUNT_H
52 #include <sys/mount.h>
53 #endif
54
55 #if !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && !defined (__OpenBSD__) && !defined(__FreeBSD__)
56 #include <mntent.h>
57 #endif
58
59 #define MAX_FS_STATS 64
60
61 static struct fs_stat fs_stats_[MAX_FS_STATS];
62 struct fs_stat *fs_stats = fs_stats_;
63
64 static void update_fs_stat(struct fs_stat *fs);
65
66 void get_fs_type(const char *path, char *result);
67
68 int update_fs_stats(void)
69 {
70         unsigned i;
71         static double last_fs_update = 0.0;
72
73         if (current_update_time - last_fs_update < 13)
74                 return 0;
75
76         for (i = 0; i < MAX_FS_STATS; ++i) {
77                 if (fs_stats[i].set) {
78                         update_fs_stat(&fs_stats[i]);
79                 }
80         }
81         last_fs_update = current_update_time;
82         return 0;
83 }
84
85 void clear_fs_stats(void)
86 {
87         unsigned i;
88         for (i = 0; i < MAX_FS_STATS; ++i) {
89                 memset(&fs_stats[i], 0, sizeof(struct fs_stat));
90         }
91 }
92
93 struct fs_stat *prepare_fs_stat(const char *s)
94 {
95         struct fs_stat *new = 0;
96         unsigned i;
97
98         /* lookup existing or get new */
99         for (i = 0; i < MAX_FS_STATS; ++i) {
100                 if (fs_stats[i].set) {
101                         if (strncmp(fs_stats[i].path, s, DEFAULT_TEXT_BUFFER_SIZE) == 0) {
102                                 return &fs_stats[i];
103                         }
104                 } else {
105                         new = &fs_stats[i];
106                 }
107         }
108         /* new path */
109         if (!new) {
110                 NORM_ERR("too many fs stats");
111                 return 0;
112         }
113         strncpy(new->path, s, DEFAULT_TEXT_BUFFER_SIZE);
114         new->set = 1;
115         update_fs_stat(new);
116         return new;
117 }
118
119 static void update_fs_stat(struct fs_stat *fs)
120 {
121         struct statfs s;
122
123         if (statfs(fs->path, &s) == 0) {
124                 fs->size = (long long)s.f_blocks * s.f_bsize;
125                 /* bfree (root) or bavail (non-roots) ? */
126                 fs->avail = (long long)s.f_bavail * s.f_bsize;
127                 fs->free = (long long)s.f_bfree * s.f_bsize;
128                 get_fs_type(fs->path, fs->type);
129         } else {
130                 fs->size = 0;
131                 fs->avail = 0;
132                 fs->free = 0;
133                 strncpy(fs->type, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
134                 NORM_ERR("statfs '%s': %s", fs->path, strerror(errno));
135         }
136 }
137
138 void get_fs_type(const char *path, char *result)
139 {
140
141 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) || defined(__FreeBSD__) || defined (__OpenBSD__)
142
143         struct statfs s;
144         if (statfs(path, &s) == 0) {
145                 strncpy(result, s.f_fstypename, DEFAULT_TEXT_BUFFER_SIZE);
146         } else {
147                 NORM_ERR("statfs '%s': %s", path, strerror(errno));
148         }
149         return;
150
151 #else                           /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
152
153         struct mntent *me;
154         FILE *mtab = setmntent("/etc/mtab", "r");
155         char *search_path;
156         int match;
157         char *slash;
158
159         if (mtab == NULL) {
160                 NORM_ERR("setmntent /etc/mtab: %s", strerror(errno));
161                 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
162                 return;
163         }
164
165         me = getmntent(mtab);
166
167         // find our path in the mtab
168         search_path = strdup(path);
169         do {
170                 while ((match = strcmp(search_path, me->mnt_dir))
171                                 && getmntent(mtab));
172                 if (!match)
173                         break;
174                 fseek(mtab, 0, SEEK_SET);
175                 slash = strrchr(search_path, '/');
176                 if (slash == NULL)
177                         CRIT_ERR(NULL, NULL, "invalid path '%s'", path);
178                 if (strlen(slash) == 1)         /* trailing slash */
179                         *(slash) = '\0';
180                 else if (strlen(slash) > 1)
181                         *(slash + 1) = '\0';
182                 else
183                         CRIT_ERR(NULL, NULL, "found a crack in the matrix!");
184         } while (strlen(search_path) > 0);
185         free(search_path);
186
187         endmntent(mtab);
188
189         if (me && !match) {
190                 strncpy(result, me->mnt_type, DEFAULT_TEXT_BUFFER_SIZE);
191                 return;
192         }
193 #endif                          /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
194
195         strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
196
197 }
198
199 void init_fs_bar(struct text_object *obj, const char *arg)
200 {
201         arg = scan_bar(obj, arg);
202         if (arg) {
203                 while (isspace(*arg)) {
204                         arg++;
205                 }
206                 if (*arg == '\0') {
207                         arg = "/";
208                 }
209         } else {
210                 arg = "/";
211         }
212         obj->data.opaque = prepare_fs_stat(arg);
213 }
214
215 static double get_fs_perc(struct fs_stat *fs, int get_free)
216 {
217         double ret = 0.0;
218
219         if(fs && fs->size) {
220                 if(get_free)
221                         ret = fs->avail;
222                 else
223                         ret = fs->size - fs->free;
224                 ret /= fs->size;
225         }
226
227         return ret;
228 }
229
230 void print_fs_bar(struct text_object *obj, int be_free_bar, char *p, int p_max_size)
231 {
232         new_bar(obj, p, p_max_size, (int)(get_fs_perc(obj->data.opaque, be_free_bar) * 255) );
233 }
234
235 void init_fs(struct text_object *obj, const char *arg)
236 {
237         obj->data.opaque = prepare_fs_stat(arg ? arg : "/");
238 }
239
240 void print_fs_perc(struct text_object *obj, int be_free, char *p, int p_max_size)
241 {
242         percent_print(p, p_max_size, (int)(get_fs_perc(obj->data.opaque, be_free) * 100) );
243 }
244
245 #define HUMAN_PRINT_FS_GENERATOR(name, expr)                           \
246 void print_fs_##name(struct text_object *obj, char *p, int p_max_size) \
247 {                                                                      \
248         struct fs_stat *fs = obj->data.opaque;                             \
249         if (fs)                                                            \
250                 human_readable(expr, p, p_max_size);                           \
251 }
252
253 HUMAN_PRINT_FS_GENERATOR(free, fs->avail)
254 HUMAN_PRINT_FS_GENERATOR(size, fs->size)
255 HUMAN_PRINT_FS_GENERATOR(used, fs->size - fs->free)
256
257 void print_fs_type(struct text_object *obj, char *p, int p_max_size)
258 {
259         struct fs_stat *fs = obj->data.opaque;
260
261         if (fs)
262                 snprintf(p, p_max_size, "%s", fs->type);
263 }