Debian Codegraph @

agent/mibgroup/hardware/fsys/fsys_mntent.c

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