Update copyright stuff, fix conky.conf weirdness.
[monky] / src / fs.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
11  *      (see AUTHORS)
12  * All rights reserved.
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  *
26  */
27
28 #include "conky.h"
29 #include "logging.h"
30 #include "fs.h"
31 #include <unistd.h>
32 #include <errno.h>
33 #include <sys/types.h>
34 #include <fcntl.h>
35
36 /* linux */
37 #ifdef HAVE_SYS_STATFS_H
38 #include <sys/statfs.h>
39 #endif
40
41 /* freebsd && netbsd */
42 #ifdef HAVE_SYS_PARAM_H
43 #include <sys/param.h>
44 #endif
45 #ifdef HAVE_SYS_MOUNT_H
46 #include <sys/mount.h>
47 #endif
48
49 #if !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && !defined (__OpenBSD__) && !defined(__FreeBSD__)
50 #include <mntent.h>
51 #endif
52
53 #define MAX_FS_STATS 64
54
55 static struct fs_stat fs_stats_[MAX_FS_STATS];
56 struct fs_stat *fs_stats = fs_stats_;
57
58 static void update_fs_stat(struct fs_stat *fs);
59
60 void get_fs_type(const char *path, char *result);
61
62 void update_fs_stats(void)
63 {
64         unsigned i;
65
66         for (i = 0; i < MAX_FS_STATS; ++i) {
67                 if (fs_stats[i].set) {
68                         update_fs_stat(&fs_stats[i]);
69                 }
70         }
71 }
72
73 void clear_fs_stats(void)
74 {
75         unsigned i;
76         for (i = 0; i < MAX_FS_STATS; ++i) {
77                 memset(&fs_stats[i], 0, sizeof(struct fs_stat));
78         }
79 }
80
81 struct fs_stat *prepare_fs_stat(const char *s)
82 {
83         struct fs_stat *new = 0;
84         unsigned i;
85
86         /* lookup existing or get new */
87         for (i = 0; i < MAX_FS_STATS; ++i) {
88                 if (fs_stats[i].set) {
89                         if (strncmp(fs_stats[i].path, s, DEFAULT_TEXT_BUFFER_SIZE) == 0) {
90                                 return &fs_stats[i];
91                         }
92                 } else {
93                         new = &fs_stats[i];
94                 }
95         }
96         /* new path */
97         if (!new) {
98                 ERR("too many fs stats");
99                 return 0;
100         }
101         strncpy(new->path, s, DEFAULT_TEXT_BUFFER_SIZE);
102         new->set = 1;
103         update_fs_stat(new);
104         return new;
105 }
106
107 static void update_fs_stat(struct fs_stat *fs)
108 {
109         struct statfs s;
110
111         if (statfs(fs->path, &s) == 0) {
112                 fs->size = (long long)s.f_blocks * s.f_bsize;
113                 /* bfree (root) or bavail (non-roots) ? */
114                 fs->avail = (long long)s.f_bavail * s.f_bsize;
115                 fs->free = (long long)s.f_bfree * s.f_bsize;
116                 get_fs_type(fs->path, fs->type);
117         } else {
118                 fs->size = 0;
119                 fs->avail = 0;
120                 fs->free = 0;
121                 strncpy(fs->type, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
122                 ERR("statfs '%s': %s", fs->path, strerror(errno));
123         }
124 }
125
126 void get_fs_type(const char *path, char *result)
127 {
128
129 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) || defined(__FreeBSD__) || defined (__OpenBSD__)
130
131         struct statfs s;
132         if (statfs(path, &s) == 0) {
133                 strncpy(result, s.f_fstypename, DEFAULT_TEXT_BUFFER_SIZE);
134         } else {
135                 ERR("statfs '%s': %s", path, strerror(errno));
136         }
137         return;
138
139 #else                           /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
140
141         struct mntent *me;
142         FILE *mtab = setmntent("/etc/mtab", "r");
143         char *search_path;
144         int match;
145         char *slash;
146
147         if (mtab == NULL) {
148                 ERR("setmntent /etc/mtab: %s", strerror(errno));
149                 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
150                 return;
151         }
152
153         me = getmntent(mtab);
154
155         // find our path in the mtab
156         search_path = strdup(path);
157         do {
158                 while ((match = strcmp(search_path, me->mnt_dir))
159                                 && getmntent(mtab));
160                 if (!match)
161                         break;
162                 fseek(mtab, 0, SEEK_SET);
163                 slash = strrchr(search_path, '/');
164                 if (slash == NULL)
165                         CRIT_ERR("invalid path '%s'", path);
166                 if (strlen(slash) == 1)         /* trailing slash */
167                         *(slash) = '\0';
168                 else if (strlen(slash) > 1)
169                         *(slash + 1) = '\0';
170                 else
171                         CRIT_ERR("found a crack in the matrix!");
172         } while (strlen(search_path) > 0);
173         free(search_path);
174
175         endmntent(mtab);
176
177         if (me && !match) {
178                 strncpy(result, me->mnt_type, DEFAULT_TEXT_BUFFER_SIZE);
179                 return;
180         }
181 #endif                          /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
182
183         strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
184
185 }