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