1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
3 * Conky, a system monitor, based on torsmo
5 * Any original torsmo code is licensed under the BSD license
7 * All code written since the fork of torsmo is licensed under the GPL
9 * Please see COPYING for details
11 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14 * All rights reserved.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * vim: ts=4 sw=4 noet ai cindent syntax=c
37 #include <sys/types.h>
41 #ifdef HAVE_SYS_STATFS_H
42 #include <sys/statfs.h>
45 /* freebsd && netbsd */
46 #ifdef HAVE_SYS_PARAM_H
47 #include <sys/param.h>
49 #ifdef HAVE_SYS_MOUNT_H
50 #include <sys/mount.h>
53 #if !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && !defined (__OpenBSD__) && !defined(__FreeBSD__)
57 #define MAX_FS_STATS 64
59 static struct fs_stat fs_stats_[MAX_FS_STATS];
60 struct fs_stat *fs_stats = fs_stats_;
62 static void update_fs_stat(struct fs_stat *fs);
64 void get_fs_type(const char *path, char *result);
66 void update_fs_stats(void)
70 for (i = 0; i < MAX_FS_STATS; ++i) {
71 if (fs_stats[i].set) {
72 update_fs_stat(&fs_stats[i]);
77 void clear_fs_stats(void)
80 for (i = 0; i < MAX_FS_STATS; ++i) {
81 memset(&fs_stats[i], 0, sizeof(struct fs_stat));
85 struct fs_stat *prepare_fs_stat(const char *s)
87 struct fs_stat *new = 0;
90 /* lookup existing or get new */
91 for (i = 0; i < MAX_FS_STATS; ++i) {
92 if (fs_stats[i].set) {
93 if (strncmp(fs_stats[i].path, s, DEFAULT_TEXT_BUFFER_SIZE) == 0) {
102 ERR("too many fs stats");
105 strncpy(new->path, s, DEFAULT_TEXT_BUFFER_SIZE);
111 static void update_fs_stat(struct fs_stat *fs)
115 if (statfs(fs->path, &s) == 0) {
116 fs->size = (long long)s.f_blocks * s.f_bsize;
117 /* bfree (root) or bavail (non-roots) ? */
118 fs->avail = (long long)s.f_bavail * s.f_bsize;
119 fs->free = (long long)s.f_bfree * s.f_bsize;
120 get_fs_type(fs->path, fs->type);
125 strncpy(fs->type, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
126 ERR("statfs '%s': %s", fs->path, strerror(errno));
130 void get_fs_type(const char *path, char *result)
133 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) || defined(__FreeBSD__) || defined (__OpenBSD__)
136 if (statfs(path, &s) == 0) {
137 strncpy(result, s.f_fstypename, DEFAULT_TEXT_BUFFER_SIZE);
139 ERR("statfs '%s': %s", path, strerror(errno));
143 #else /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
146 FILE *mtab = setmntent("/etc/mtab", "r");
152 ERR("setmntent /etc/mtab: %s", strerror(errno));
153 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
157 me = getmntent(mtab);
159 // find our path in the mtab
160 search_path = strdup(path);
162 while ((match = strcmp(search_path, me->mnt_dir))
166 fseek(mtab, 0, SEEK_SET);
167 slash = strrchr(search_path, '/');
169 CRIT_ERR(NULL, NULL, "invalid path '%s'", path);
170 if (strlen(slash) == 1) /* trailing slash */
172 else if (strlen(slash) > 1)
175 CRIT_ERR(NULL, NULL, "found a crack in the matrix!");
176 } while (strlen(search_path) > 0);
182 strncpy(result, me->mnt_type, DEFAULT_TEXT_BUFFER_SIZE);
185 #endif /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
187 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);