Debian Codegraph @

agent/mibgroup/hardware/fsys/fsys_mntent.c

raw ◀ ▶
1 Debian patch touch this file
1 #include <net-snmp/net-snmp-config.h>
2 #include <net-snmp/net-snmp-includes.h>
3 #include <net-snmp/agent/net-snmp-agent-includes.h>
4 #include <net-snmp/agent/hardware/fsys.h>
5 #include "hw_fsys.h"
6 #include "hardware/fsys/hw_fsys_private.h"
7
8 #include <stdio.h>
•9 #include <errno.h>
10 #ifdef HAVE_MNTENT_H
11 #include <mntent.h>
12 #endif
13 #ifdef HAVE_SYS_MNTTAB_H
14 #include <sys/mnttab.h>
15 #endif
16 #ifdef HAVE_SYS_VFS_H
17 #include <sys/vfs.h>
18 #endif
19 #ifdef HAVE_SYS_PARAM_H
20 #include <sys/param.h>
21 #endif
22 #ifdef HAVE_SYS_MOUNT_H
23 #include <sys/mount.h>
24 #endif
25 #ifdef HAVE_SYS_STATFS_H
26 #include <sys/statfs.h>
27 #endif
28 #ifdef HAVE_SYS_STATVFS_H
29 #include <sys/statvfs.h>
30 #endif
31 #if defined(HAVE_PCRE_H)
32 #include <pcre.h>
33 #elif defined(HAVE_REGEX_H)
34 #include <sys/types.h>
35 #include <regex.h>
36 #endif
37
38 #ifdef solaris2
39 #define _NETSNMP_GETMNTENT_TWO_ARGS 1
40 #else
41 #undef _NETSNMP_GETMNTENT_TWO_ARGS
42 #endif
43
44 /*
45 * Handle naming differences between getmntent() APIs
46 */
47 #ifdef _NETSNMP_GETMNTENT_TWO_ARGS
48 /* Two-argument form (Solaris) */
49 #define NSFS_MNTENT struct mnttab
50 #define NSFS_PATH mnt_mountp
51 #define NSFS_DEV mnt_special
52 #define NSFS_TYPE mnt_fstype
53
54 #define NSFS_STATFS statvfs
55 #define NSFS_SIZE f_frsize
56
57 #else
58 /* One-argument form (everything else?) */
59 #define NSFS_MNTENT struct mntent
60 #define NSFS_PATH mnt_dir
61 #define NSFS_DEV mnt_fsname
62 #define NSFS_TYPE mnt_type
63
64 #define NSFS_STATFS statfs
65 #define NSFS_SIZE f_bsize
66
67 #endif
68
69 /*
70 * File systems to monitor and that are not covered by any hrFSTypes
71 * enumeration.
72 */
73 static const char *other_fs[] = {
74 "acfs",
75 "btrfs",
76 "cvfs",
77 "f2fs",
78 "fuse.glusterfs",
79 "gfs",
80 "gfs2",
81 "glusterfs",
82 "jfs",
83 "jffs2",
84 "lofs",
85 "mvfs",
86 "nsspool",
87 "nssvol",
88 "nvmfs",
89 "ocfs2",
90 "reiserfs",
91 "simfs",
92 "tmpfs",
93 "ubifs",
94 "virtiofs",
95 "vxfs",
96 "xfs",
97 "zfs",
98 NULL,
99 };
100
101 static int
102 _fsys_remote( char *device, int type )
103 {
104 if (( type == NETSNMP_FS_TYPE_NFS) ||
105 ( type == NETSNMP_FS_TYPE_AFS))
106 return 1;
107 else
108 return 0;
109 }
110
111 static int
112 _fsys_type( char *typename )
113 {
114 const char **fs;
115
116 DEBUGMSGTL(("fsys:type", "Classifying %s\n", typename));
117
118 if ( !typename || *typename=='\0' )
119 return NETSNMP_FS_TYPE_UNKNOWN;
120
121 #include "mnttypes.h"
122
123 else if ( !strcmp(typename, MNTTYPE_FFS) )
124 return NETSNMP_FS_TYPE_BERKELEY;
125 else if ( !strcmp(typename, MNTTYPE_UFS) )
126 return _NETSNMP_FS_TYPE_UFS; /* either N_FS_TYPE_BERKELEY or N_FS_TYPE_SYSV */
127 else if ( !strcmp(typename, MNTTYPE_SYSV) )
128 return NETSNMP_FS_TYPE_SYSV;
129 else if ( !strcmp(typename, MNTTYPE_PC) ||
130 !strcmp(typename, MNTTYPE_MSDOS) )
131 return NETSNMP_FS_TYPE_FAT;
132 else if ( !strcmp(typename, MNTTYPE_HFS) )
133 return NETSNMP_FS_TYPE_HFS;
134 else if ( !strcmp(typename, MNTTYPE_MFS) )
135 return NETSNMP_FS_TYPE_MFS;
136 else if ( !strcmp(typename, MNTTYPE_NTFS) )
137 return NETSNMP_FS_TYPE_NTFS;
138 else if ( !strcmp(typename, MNTTYPE_ISO9660) ||
139 !strcmp(typename, MNTTYPE_CD9660) )
140 return NETSNMP_FS_TYPE_ISO9660;
141 else if ( !strcmp(typename, MNTTYPE_CDFS) )
142 return _NETSNMP_FS_TYPE_CDFS; /* either N_FS_TYPE_ISO9660 or N_FS_TYPE_ROCKRIDGE */
143 else if ( !strcmp(typename, MNTTYPE_HSFS) )
144 return NETSNMP_FS_TYPE_ROCKRIDGE;
145 else if ( !strcmp(typename, MNTTYPE_NFS) ||
146 !strcmp(typename, MNTTYPE_NFS3) ||
147 !strcmp(typename, MNTTYPE_NFS4) ||
148 !strcmp(typename, MNTTYPE_CIFS) || /* i.e. SMB - ?? */
149 !strcmp(typename, MNTTYPE_SMBFS) || /* ?? */
150 /* mvfs (IBM ClearCase) is nfs-like in nature */
151 !strcmp(typename, MNTTYPE_MVFS))
152 return NETSNMP_FS_TYPE_NFS;
153 else if ( !strcmp(typename, MNTTYPE_NCPFS) )
154 return NETSNMP_FS_TYPE_NETWARE;
155 else if ( !strcmp(typename, MNTTYPE_AFS) )
156 return NETSNMP_FS_TYPE_AFS;
157 else if ( !strcmp(typename, MNTTYPE_EXT2) ||
158 !strcmp(typename, MNTTYPE_EXT3) ||
159 !strcmp(typename, MNTTYPE_EXT4) ||
160 !strcmp(typename, MNTTYPE_EXT2FS) ||
161 !strcmp(typename, MNTTYPE_EXT3FS) ||
162 !strcmp(typename, MNTTYPE_EXT4FS) )
163 return NETSNMP_FS_TYPE_EXT2;
164 else if ( !strcmp(typename, MNTTYPE_FAT32) ||
165 !strcmp(typename, MNTTYPE_VFAT) )
166 return NETSNMP_FS_TYPE_FAT32;
167
168 for (fs = other_fs; *fs; fs++)
169 if (strcmp(typename, *fs) == 0)
170 return NETSNMP_FS_TYPE_OTHER;
171
172 /* Detection of AUTOFS.
173 * This file system will be ignored by default
174 */
175 if (!strcmp(typename, MNTTYPE_AUTOFS))
176 return NETSNMP_FS_TYPE_AUTOFS;
177
178
179 /*
180 * All other types are silently skipped
181 */
182 return NETSNMP_FS_TYPE_IGNORE;
183 }
184
185 void
186 netsnmp_fsys_arch_init( void )
187 {
188 return;
189 }
190
191 static int
192 ignore_mount_point(const char *name)
193 {
194 conf_mount_list *m_ptr;
195 #ifdef HAVE_PCRE_H
196 int found_ndx[3];
197 #endif
198
199 if (!ignoremount_list)
200 return FALSE;
201
202 for (m_ptr = ignoremount_list; m_ptr; m_ptr = m_ptr->next) {
203 #if defined(HAVE_PCRE_H)
204 if (m_ptr->regex_ptr) {
205 if (pcre_exec(m_ptr->regex_ptr, NULL, name, strlen(name), 0, 0,
206 found_ndx, 3) >= 0)
207 return TRUE;
208 } else {
209 if (strcmp(name, m_ptr->name) == 0)
210 return TRUE;
211 }
212 #elif defined(HAVE_REGEX_H)
213 if (m_ptr->regex_ptr) {
214 if (regexec(m_ptr->regex_ptr, name, 0, NULL, 0) == 0)
215 return TRUE;
216 } else {
217 if (strcmp(name, m_ptr->name) == 0)
218 return TRUE;
219 }
220 #else
221 if (strcmp(name, m_ptr->name) == 0)
222 return TRUE;
223 #endif
224 }
225
226 return FALSE;
227 }
228
229 void
230 netsnmp_fsys_arch_load( void )
231 {
232 FILE *fp=NULL;
233 #ifdef _NETSNMP_GETMNTENT_TWO_ARGS
234 struct mnttab mtmp;
235 struct mnttab *m = &mtmp;
236 #else
237 struct mntent *m;
238 #endif
239 struct NSFS_STATFS stat_buf;
240 netsnmp_fsys_info *entry;
241 char *tmpbuf = NULL;
242
243 /*
244 * Retrieve information about the currently mounted filesystems...
245 */
246 fp = fopen( ETC_MNTTAB, "r" ); /* OR setmntent()?? */
247 if ( !fp ) {
248 if (asprintf(&tmpbuf, "Cannot open %s", ETC_MNTTAB) >= 0)
249 snmp_log_perror(tmpbuf);
250 free(tmpbuf);
251 return;
252 }
253
254 /*
255 * ... and insert this into the filesystem container.
256 */
257 while
258 #ifdef _NETSNMP_GETMNTENT_TWO_ARGS
259 ((getmntent(fp, m)) == 0 )
260 #else
261 ((m = getmntent(fp)) != NULL )
262 #endif
263 {
264 entry = netsnmp_fsys_by_path( m->NSFS_PATH, NETSNMP_FS_FIND_CREATE );
265 if (!entry) {
266 continue;
267 }
268
269 strlcpy(entry->path, m->NSFS_PATH, sizeof(entry->path));
270 strlcpy(entry->device, m->NSFS_DEV, sizeof(entry->device));
271 entry->type = _fsys_type(m->NSFS_TYPE);
272 if (!(entry->type & _NETSNMP_FS_TYPE_SKIP_BIT))
273 entry->flags |= NETSNMP_FS_FLAG_ACTIVE;
274
275 if ( _fsys_remote( entry->device, entry->type ))
276 entry->flags |= NETSNMP_FS_FLAG_REMOTE;
277 #ifdef HAVE_HASMNTOPT
278 if (hasmntopt( m, NETSNMP_REMOVE_CONST(char *, "ro") ))
279 entry->flags |= NETSNMP_FS_FLAG_RONLY;
280 else
281 entry->flags &= ~NETSNMP_FS_FLAG_RONLY;
282 #endif
283 /*
284 * The root device is presumably bootable.
285 * Other partitions probably aren't!
286 *
287 * XXX - what about /boot ??
288 */
289 if ((entry->path[0] == '/') &&
290 (entry->path[1] == '\0'))
291 entry->flags |= NETSNMP_FS_FLAG_BOOTABLE;
292
293 /*
294 * XXX - identify removeable disks
295 */
296
297 /*
298 * Skip ignored mount points
299 */
300 if (ignore_mount_point(entry->path))
301 continue;
302
303 /*
304 * Optionally skip retrieving statistics for remote mounts
305 */
306 if ( (entry->flags & NETSNMP_FS_FLAG_REMOTE) &&
307 netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
308 NETSNMP_DS_AGENT_SKIPNFSINHOSTRESOURCES))
309 continue;
310
311 /* Skip AUTOFS entries */
312 if (entry->type == NETSNMP_FS_TYPE_AUTOFS)
313 continue;
314
315 #ifdef irix6
316 if ( NSFS_STATFS( entry->path, &stat_buf, sizeof(struct statfs), 0) < 0 )
317 #else
318 if ( NSFS_STATFS( entry->path, &stat_buf ) < 0 )
319 #endif
320 {
321 static char logged = 0;
322
323 if (!logged &&
324 asprintf(&tmpbuf, "Cannot statfs %s", entry->path) >= 0) {
•325 if (errno == EACCES)
326 DEBUGMSGTL(("fsys", "%s\n", tmpbuf));
327 else
328 snmp_log_perror(tmpbuf);
329 free(tmpbuf);
330 logged = 1;
331 }
332 memset(&stat_buf, 0, sizeof(stat_buf));
333 }
334 entry->units = stat_buf.NSFS_SIZE;
335 entry->size = stat_buf.f_blocks;
336 entry->used = (stat_buf.f_blocks - stat_buf.f_bfree);
337 entry->avail = stat_buf.f_bavail;
338 entry->inums_total = stat_buf.f_files;
339 entry->inums_avail = stat_buf.f_ffree;
340 netsnmp_fsys_calculate32(entry);
341 }
342 fclose( fp );
343 }