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-2009 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/>.
36 #include <sys/types.h>
40 #ifdef HAVE_SYS_STATFS_H
41 #include <sys/statfs.h>
44 /* freebsd && netbsd */
45 #ifdef HAVE_SYS_PARAM_H
46 #include <sys/param.h>
48 #ifdef HAVE_SYS_MOUNT_H
49 #include <sys/mount.h>
52 #if !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && !defined (__OpenBSD__) && !defined(__FreeBSD__)
56 #define MAX_FS_STATS 64
58 static struct fs_stat fs_stats_[MAX_FS_STATS];
59 struct fs_stat *fs_stats = fs_stats_;
61 static void update_fs_stat(struct fs_stat *fs);
63 void get_fs_type(const char *path, char *result);
65 void update_fs_stats(void)
68 static double last_fs_update = 0.0;
70 if (current_update_time - last_fs_update < 13)
73 for (i = 0; i < MAX_FS_STATS; ++i) {
74 if (fs_stats[i].set) {
75 update_fs_stat(&fs_stats[i]);
78 last_fs_update = current_update_time;
81 void clear_fs_stats(void)
84 for (i = 0; i < MAX_FS_STATS; ++i) {
85 memset(&fs_stats[i], 0, sizeof(struct fs_stat));
89 struct fs_stat *prepare_fs_stat(const char *s)
91 struct fs_stat *new = 0;
94 /* lookup existing or get new */
95 for (i = 0; i < MAX_FS_STATS; ++i) {
96 if (fs_stats[i].set) {
97 if (strncmp(fs_stats[i].path, s, DEFAULT_TEXT_BUFFER_SIZE) == 0) {
106 NORM_ERR("too many fs stats");
109 strncpy(new->path, s, DEFAULT_TEXT_BUFFER_SIZE);
115 static void update_fs_stat(struct fs_stat *fs)
119 if (statfs(fs->path, &s) == 0) {
120 fs->size = (long long)s.f_blocks * s.f_bsize;
121 /* bfree (root) or bavail (non-roots) ? */
122 fs->avail = (long long)s.f_bavail * s.f_bsize;
123 fs->free = (long long)s.f_bfree * s.f_bsize;
124 get_fs_type(fs->path, fs->type);
129 strncpy(fs->type, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
130 NORM_ERR("statfs '%s': %s", fs->path, strerror(errno));
134 void get_fs_type(const char *path, char *result)
137 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) || defined(__FreeBSD__) || defined (__OpenBSD__)
140 if (statfs(path, &s) == 0) {
141 strncpy(result, s.f_fstypename, DEFAULT_TEXT_BUFFER_SIZE);
143 NORM_ERR("statfs '%s': %s", path, strerror(errno));
147 #else /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
150 FILE *mtab = setmntent("/etc/mtab", "r");
156 NORM_ERR("setmntent /etc/mtab: %s", strerror(errno));
157 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
161 me = getmntent(mtab);
163 // find our path in the mtab
164 search_path = strdup(path);
166 while ((match = strcmp(search_path, me->mnt_dir))
170 fseek(mtab, 0, SEEK_SET);
171 slash = strrchr(search_path, '/');
173 CRIT_ERR(NULL, NULL, "invalid path '%s'", path);
174 if (strlen(slash) == 1) /* trailing slash */
176 else if (strlen(slash) > 1)
179 CRIT_ERR(NULL, NULL, "found a crack in the matrix!");
180 } while (strlen(search_path) > 0);
186 strncpy(result, me->mnt_type, DEFAULT_TEXT_BUFFER_SIZE);
189 #endif /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
191 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);