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 int 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;
85 void clear_fs_stats(void)
88 for (i = 0; i < MAX_FS_STATS; ++i) {
89 memset(&fs_stats[i], 0, sizeof(struct fs_stat));
93 struct fs_stat *prepare_fs_stat(const char *s)
95 struct fs_stat *new = 0;
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) {
110 NORM_ERR("too many fs stats");
113 strncpy(new->path, s, DEFAULT_TEXT_BUFFER_SIZE);
119 static void update_fs_stat(struct fs_stat *fs)
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);
133 strncpy(fs->type, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
134 NORM_ERR("statfs '%s': %s", fs->path, strerror(errno));
138 void get_fs_type(const char *path, char *result)
141 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) || defined(__FreeBSD__) || defined (__OpenBSD__)
144 if (statfs(path, &s) == 0) {
145 strncpy(result, s.f_fstypename, DEFAULT_TEXT_BUFFER_SIZE);
147 NORM_ERR("statfs '%s': %s", path, strerror(errno));
151 #else /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
154 FILE *mtab = setmntent("/etc/mtab", "r");
160 NORM_ERR("setmntent /etc/mtab: %s", strerror(errno));
161 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
165 me = getmntent(mtab);
167 // find our path in the mtab
168 search_path = strdup(path);
170 while ((match = strcmp(search_path, me->mnt_dir))
174 fseek(mtab, 0, SEEK_SET);
175 slash = strrchr(search_path, '/');
177 CRIT_ERR(NULL, NULL, "invalid path '%s'", path);
178 if (strlen(slash) == 1) /* trailing slash */
180 else if (strlen(slash) > 1)
183 CRIT_ERR(NULL, NULL, "found a crack in the matrix!");
184 } while (strlen(search_path) > 0);
190 strncpy(result, me->mnt_type, DEFAULT_TEXT_BUFFER_SIZE);
193 #endif /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
195 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
199 void init_fs_bar(struct text_object *obj, const char *arg)
201 arg = scan_bar(obj, arg);
203 while (isspace(*arg)) {
212 obj->data.opaque = prepare_fs_stat(arg);
215 static double get_fs_perc(struct fs_stat *fs, int get_free)
223 ret = fs->size - fs->free;
230 void print_fs_bar(struct text_object *obj, int be_free_bar, char *p, int p_max_size)
232 new_bar(obj, p, p_max_size, (int)(get_fs_perc(obj->data.opaque, be_free_bar) * 255) );
235 void init_fs(struct text_object *obj, const char *arg)
237 obj->data.opaque = prepare_fs_stat(arg ? arg : "/");
240 void print_fs_perc(struct text_object *obj, int be_free, char *p, int p_max_size)
242 percent_print(p, p_max_size, (int)(get_fs_perc(obj->data.opaque, be_free) * 100) );
245 #define HUMAN_PRINT_FS_GENERATOR(name, expr) \
246 void print_fs_##name(struct text_object *obj, char *p, int p_max_size) \
248 struct fs_stat *fs = obj->data.opaque; \
250 human_readable(expr, p, p_max_size); \
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)
257 void print_fs_type(struct text_object *obj, char *p, int p_max_size)
259 struct fs_stat *fs = obj->data.opaque;
262 snprintf(p, p_max_size, "%s", fs->type);