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
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
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.
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/>.
35 #include "text_object.h"
39 #include <sys/types.h>
43 #ifdef HAVE_SYS_STATFS_H
44 #include <sys/statfs.h>
47 /* freebsd && netbsd */
48 #ifdef HAVE_SYS_PARAM_H
49 #include <sys/param.h>
51 #ifdef HAVE_SYS_MOUNT_H
52 #include <sys/mount.h>
55 #if !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && !defined (__OpenBSD__) && !defined(__FreeBSD__)
59 #define MAX_FS_STATS 64
61 static struct fs_stat fs_stats_[MAX_FS_STATS];
62 struct fs_stat *fs_stats = fs_stats_;
64 static void update_fs_stat(struct fs_stat *fs);
66 void get_fs_type(const char *path, char *result);
68 void update_fs_stats(void)
71 static double last_fs_update = 0.0;
73 if (current_update_time - last_fs_update < 13)
76 for (i = 0; i < MAX_FS_STATS; ++i) {
77 if (fs_stats[i].set) {
78 update_fs_stat(&fs_stats[i]);
81 last_fs_update = current_update_time;
84 void clear_fs_stats(void)
87 for (i = 0; i < MAX_FS_STATS; ++i) {
88 memset(&fs_stats[i], 0, sizeof(struct fs_stat));
92 struct fs_stat *prepare_fs_stat(const char *s)
94 struct fs_stat *new = 0;
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) {
109 NORM_ERR("too many fs stats");
112 strncpy(new->path, s, DEFAULT_TEXT_BUFFER_SIZE);
118 static void update_fs_stat(struct fs_stat *fs)
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);
132 strncpy(fs->type, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
133 NORM_ERR("statfs '%s': %s", fs->path, strerror(errno));
137 void get_fs_type(const char *path, char *result)
140 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) || defined(__FreeBSD__) || defined (__OpenBSD__)
143 if (statfs(path, &s) == 0) {
144 strncpy(result, s.f_fstypename, DEFAULT_TEXT_BUFFER_SIZE);
146 NORM_ERR("statfs '%s': %s", path, strerror(errno));
150 #else /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
153 FILE *mtab = setmntent("/etc/mtab", "r");
159 NORM_ERR("setmntent /etc/mtab: %s", strerror(errno));
160 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
164 me = getmntent(mtab);
166 // find our path in the mtab
167 search_path = strdup(path);
169 while ((match = strcmp(search_path, me->mnt_dir))
173 fseek(mtab, 0, SEEK_SET);
174 slash = strrchr(search_path, '/');
176 CRIT_ERR(NULL, NULL, "invalid path '%s'", path);
177 if (strlen(slash) == 1) /* trailing slash */
179 else if (strlen(slash) > 1)
182 CRIT_ERR(NULL, NULL, "found a crack in the matrix!");
183 } while (strlen(search_path) > 0);
189 strncpy(result, me->mnt_type, DEFAULT_TEXT_BUFFER_SIZE);
192 #endif /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
194 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
198 void init_fs_bar(struct text_object *obj, const char *arg)
200 arg = scan_bar(obj, arg);
202 while (isspace(*arg)) {
211 obj->data.opaque = prepare_fs_stat(arg);
214 void print_fs_bar(struct text_object *obj, int be_free_bar, char *p, int p_max_size)
217 struct fs_stat *fs = obj->data.opaque;
223 val = (double)fs->avail / (double)fs->size;
228 new_bar(obj, p, p_max_size, (int)(255 * val));
231 void init_fs(struct text_object *obj, const char *arg)
233 obj->data.opaque = prepare_fs_stat(arg ? arg : "/");
236 void print_fs_perc(struct text_object *obj, int be_free, char *p, int p_max_size)
238 struct fs_stat *fs = obj->data.opaque;
245 val = fs->avail * 100 / fs->size;
250 percent_print(p, p_max_size, val);
253 #define HUMAN_PRINT_FS_GENERATOR(name, expr) \
254 void print_fs_##name(struct text_object *obj, char *p, int p_max_size) \
256 struct fs_stat *fs = obj->data.opaque; \
258 human_readable(expr, p, p_max_size); \
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)
265 void print_fs_type(struct text_object *obj, char *p, int p_max_size)
267 struct fs_stat *fs = obj->data.opaque;
270 snprintf(p, p_max_size, "%s", fs->type);