098191956755403d24ebc642b7399a8fb12600a8
[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 void 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;
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 }
83
84 void clear_fs_stats(void)
85 {
86         unsigned i;
87         for (i = 0; i < MAX_FS_STATS; ++i) {
88                 memset(&fs_stats[i], 0, sizeof(struct fs_stat));
89         }
90 }
91
92 struct fs_stat *prepare_fs_stat(const char *s)
93 {
94         struct fs_stat *new = 0;
95         unsigned i;
96
97         /* lookup existing or get new */
98         for (i = 0; i < MAX_FS_STATS; ++i) {
99                 if (fs_stats[i].set) {
100                         if (strncmp(fs_stats[i].path, s, DEFAULT_TEXT_BUFFER_SIZE) == 0) {
101                                 return &fs_stats[i];
102                         }
103                 } else {
104                         new = &fs_stats[i];
105                 }
106         }
107         /* new path */
108         if (!new) {
109                 NORM_ERR("too many fs stats");
110                 return 0;
111         }
112         strncpy(new->path, s, DEFAULT_TEXT_BUFFER_SIZE);
113         new->set = 1;
114         update_fs_stat(new);
115         return new;
116 }
117
118 static void update_fs_stat(struct fs_stat *fs)
119 {
120         struct statfs s;
121
122         if (statfs(fs->path, &s) == 0) {
123                 fs->size = (long long)s.f_blocks * s.f_bsize;
124                 /* bfree (root) or bavail (non-roots) ? */
125                 fs->avail = (long long)s.f_bavail * s.f_bsize;
126                 fs->free = (long long)s.f_bfree * s.f_bsize;
127                 get_fs_type(fs->path, fs->type);
128         } else {
129                 fs->size = 0;
130                 fs->avail = 0;
131                 fs->free = 0;
132                 strncpy(fs->type, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
133                 NORM_ERR("statfs '%s': %s", fs->path, strerror(errno));
134         }
135 }
136
137 void get_fs_type(const char *path, char *result)
138 {
139
140 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) || defined(__FreeBSD__) || defined (__OpenBSD__)
141
142         struct statfs s;
143         if (statfs(path, &s) == 0) {
144                 strncpy(result, s.f_fstypename, DEFAULT_TEXT_BUFFER_SIZE);
145         } else {
146                 NORM_ERR("statfs '%s': %s", path, strerror(errno));
147         }
148         return;
149
150 #else                           /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
151
152         struct mntent *me;
153         FILE *mtab = setmntent("/etc/mtab", "r");
154         char *search_path;
155         int match;
156         char *slash;
157
158         if (mtab == NULL) {
159                 NORM_ERR("setmntent /etc/mtab: %s", strerror(errno));
160                 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
161                 return;
162         }
163
164         me = getmntent(mtab);
165
166         // find our path in the mtab
167         search_path = strdup(path);
168         do {
169                 while ((match = strcmp(search_path, me->mnt_dir))
170                                 && getmntent(mtab));
171                 if (!match)
172                         break;
173                 fseek(mtab, 0, SEEK_SET);
174                 slash = strrchr(search_path, '/');
175                 if (slash == NULL)
176                         CRIT_ERR(NULL, NULL, "invalid path '%s'", path);
177                 if (strlen(slash) == 1)         /* trailing slash */
178                         *(slash) = '\0';
179                 else if (strlen(slash) > 1)
180                         *(slash + 1) = '\0';
181                 else
182                         CRIT_ERR(NULL, NULL, "found a crack in the matrix!");
183         } while (strlen(search_path) > 0);
184         free(search_path);
185
186         endmntent(mtab);
187
188         if (me && !match) {
189                 strncpy(result, me->mnt_type, DEFAULT_TEXT_BUFFER_SIZE);
190                 return;
191         }
192 #endif                          /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
193
194         strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
195
196 }
197
198 void init_fs_bar(struct text_object *obj, const char *arg)
199 {
200         arg = scan_bar(obj, arg);
201         if (arg) {
202                 while (isspace(*arg)) {
203                         arg++;
204                 }
205                 if (*arg == '\0') {
206                         arg = "/";
207                 }
208         } else {
209                 arg = "/";
210         }
211         obj->data.opaque = prepare_fs_stat(arg);
212 }
213
214 void print_fs_bar(struct text_object *obj, int be_free_bar, char *p, int p_max_size)
215 {
216         double val = 1.0;
217         struct fs_stat *fs = obj->data.opaque;
218
219         if (!fs)
220                 return;
221
222         if (fs->size)
223                 val = (double)fs->avail / (double)fs->size;
224
225         if (!be_free_bar)
226                 val = 1.0 - val;
227
228         new_bar(obj, p, p_max_size, (int)(255 * val));
229 }
230
231 void init_fs(struct text_object *obj, const char *arg)
232 {
233         obj->data.opaque = prepare_fs_stat(arg ? arg : "/");
234 }
235
236 void print_fs_perc(struct text_object *obj, int be_free, char *p, int p_max_size)
237 {
238         struct fs_stat *fs = obj->data.opaque;
239         int val = 100;
240
241         if (!fs)
242                 return;
243
244         if (fs->size)
245                 val = fs->avail * 100 / fs->size;
246
247         if (!be_free)
248                 val = 100 - val;
249
250         percent_print(p, p_max_size, val);
251 }
252
253 #define HUMAN_PRINT_FS_GENERATOR(name, expr)                           \
254 void print_fs_##name(struct text_object *obj, char *p, int p_max_size) \
255 {                                                                      \
256         struct fs_stat *fs = obj->data.opaque;                             \
257         if (fs)                                                            \
258                 human_readable(expr, p, p_max_size);                           \
259 }
260
261 HUMAN_PRINT_FS_GENERATOR(free, fs->avail)
262 HUMAN_PRINT_FS_GENERATOR(size, fs->size)
263 HUMAN_PRINT_FS_GENERATOR(used, fs->size - fs->free)
264
265 void print_fs_type(struct text_object *obj, char *p, int p_max_size)
266 {
267         struct fs_stat *fs = obj->data.opaque;
268
269         if (fs)
270                 snprintf(p, p_max_size, "%s", fs->type);
271 }