faa361e1361d74c43851200457f885c27fd3dae1
[monky] / src / fs.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  *
3  * Conky, a system monitor, based on torsmo
4  *
5  * Any original torsmo code is licensed under the BSD license
6  *
7  * All code written since the fork of torsmo is licensed under the GPL
8  *
9  * Please see COPYING for details
10  *
11  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
13  *      (see AUTHORS)
14  * All rights reserved.
15  *
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.
20  *
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/>.
27  *
28  * vim: ts=4 sw=4 noet ai cindent syntax=c
29  *
30  */
31
32 #include "conky.h"
33 #include "logging.h"
34 #include "fs.h"
35 #include <unistd.h>
36 #include <errno.h>
37 #include <sys/types.h>
38 #include <fcntl.h>
39
40 /* linux */
41 #ifdef HAVE_SYS_STATFS_H
42 #include <sys/statfs.h>
43 #endif
44
45 /* freebsd && netbsd */
46 #ifdef HAVE_SYS_PARAM_H
47 #include <sys/param.h>
48 #endif
49 #ifdef HAVE_SYS_MOUNT_H
50 #include <sys/mount.h>
51 #endif
52
53 #if !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && !defined (__OpenBSD__) && !defined(__FreeBSD__)
54 #include <mntent.h>
55 #endif
56
57 #define MAX_FS_STATS 64
58
59 static struct fs_stat fs_stats_[MAX_FS_STATS];
60 struct fs_stat *fs_stats = fs_stats_;
61
62 static void update_fs_stat(struct fs_stat *fs);
63
64 void get_fs_type(const char *path, char *result);
65
66 void update_fs_stats(void)
67 {
68         unsigned i;
69         static double last_fs_update = 0.0;
70
71         if (current_update_time - last_fs_update < 13)
72                 return;
73
74         for (i = 0; i < MAX_FS_STATS; ++i) {
75                 if (fs_stats[i].set) {
76                         update_fs_stat(&fs_stats[i]);
77                 }
78         }
79         last_fs_update = current_update_time;
80 }
81
82 void clear_fs_stats(void)
83 {
84         unsigned i;
85         for (i = 0; i < MAX_FS_STATS; ++i) {
86                 memset(&fs_stats[i], 0, sizeof(struct fs_stat));
87         }
88 }
89
90 struct fs_stat *prepare_fs_stat(const char *s)
91 {
92         struct fs_stat *new = 0;
93         unsigned i;
94
95         /* lookup existing or get new */
96         for (i = 0; i < MAX_FS_STATS; ++i) {
97                 if (fs_stats[i].set) {
98                         if (strncmp(fs_stats[i].path, s, DEFAULT_TEXT_BUFFER_SIZE) == 0) {
99                                 return &fs_stats[i];
100                         }
101                 } else {
102                         new = &fs_stats[i];
103                 }
104         }
105         /* new path */
106         if (!new) {
107                 NORM_ERR("too many fs stats");
108                 return 0;
109         }
110         strncpy(new->path, s, DEFAULT_TEXT_BUFFER_SIZE);
111         new->set = 1;
112         update_fs_stat(new);
113         return new;
114 }
115
116 static void update_fs_stat(struct fs_stat *fs)
117 {
118         struct statfs s;
119
120         if (statfs(fs->path, &s) == 0) {
121                 fs->size = (long long)s.f_blocks * s.f_bsize;
122                 /* bfree (root) or bavail (non-roots) ? */
123                 fs->avail = (long long)s.f_bavail * s.f_bsize;
124                 fs->free = (long long)s.f_bfree * s.f_bsize;
125                 get_fs_type(fs->path, fs->type);
126         } else {
127                 fs->size = 0;
128                 fs->avail = 0;
129                 fs->free = 0;
130                 strncpy(fs->type, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
131                 NORM_ERR("statfs '%s': %s", fs->path, strerror(errno));
132         }
133 }
134
135 void get_fs_type(const char *path, char *result)
136 {
137
138 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) || defined(__FreeBSD__) || defined (__OpenBSD__)
139
140         struct statfs s;
141         if (statfs(path, &s) == 0) {
142                 strncpy(result, s.f_fstypename, DEFAULT_TEXT_BUFFER_SIZE);
143         } else {
144                 NORM_ERR("statfs '%s': %s", path, strerror(errno));
145         }
146         return;
147
148 #else                           /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
149
150         struct mntent *me;
151         FILE *mtab = setmntent("/etc/mtab", "r");
152         char *search_path;
153         int match;
154         char *slash;
155
156         if (mtab == NULL) {
157                 NORM_ERR("setmntent /etc/mtab: %s", strerror(errno));
158                 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
159                 return;
160         }
161
162         me = getmntent(mtab);
163
164         // find our path in the mtab
165         search_path = strdup(path);
166         do {
167                 while ((match = strcmp(search_path, me->mnt_dir))
168                                 && getmntent(mtab));
169                 if (!match)
170                         break;
171                 fseek(mtab, 0, SEEK_SET);
172                 slash = strrchr(search_path, '/');
173                 if (slash == NULL)
174                         CRIT_ERR(NULL, NULL, "invalid path '%s'", path);
175                 if (strlen(slash) == 1)         /* trailing slash */
176                         *(slash) = '\0';
177                 else if (strlen(slash) > 1)
178                         *(slash + 1) = '\0';
179                 else
180                         CRIT_ERR(NULL, NULL, "found a crack in the matrix!");
181         } while (strlen(search_path) > 0);
182         free(search_path);
183
184         endmntent(mtab);
185
186         if (me && !match) {
187                 strncpy(result, me->mnt_type, DEFAULT_TEXT_BUFFER_SIZE);
188                 return;
189         }
190 #endif                          /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
191
192         strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
193
194 }