Debian Codegraph @

agent/mibgroup/host/hr_filesys.c

raw
1 /*
2 * Host Resources MIB - File System device group implementation - hr_filesys.c
3 *
4 */
5 /* Portions of this file are subject to the following copyright(s). See
6 * the Net-SNMP's COPYING file for more details and other copyrights
7 * that may apply:
8 */
9 /*
10 * Portions of this file are copyrighted by:
11 * Copyright (C) 2007 Apple, Inc. All rights reserved.
12 * Use is subject to license terms specified in the COPYING file
13 * distributed with the Net-SNMP package.
14 */
15
16 #include <net-snmp/net-snmp-config.h>
17 #include <net-snmp/net-snmp-features.h>
18 #include <net-snmp/net-snmp-includes.h>
19 #include <net-snmp/agent/net-snmp-agent-includes.h>
20 #include <net-snmp/agent/hardware/memory.h>
21 #include "host_res.h"
22 #include "hr_filesys.h"
23 #include "hr_storage.h"
24 #include "hr_disk.h"
25 #include <net-snmp/utilities.h>
26
27 #ifdef HAVE_MNTENT_H
28 #include <mntent.h>
29 #endif
30 #ifdef HAVE_SYS_MNTENT_H
31 #include <sys/mntent.h>
32 #endif
33 #ifdef HAVE_SYS_MNTTAB_H
34 #include <sys/mnttab.h>
35 #endif
36 #ifdef HAVE_SYS_STATVFS_H
37 #include <sys/statvfs.h>
38 #endif
39 #ifdef HAVE_SYS_VFS_H
40 #include <sys/vfs.h>
41 #endif
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 #include <ctype.h>
50 #ifdef HAVE_STRING_H
51 #include <string.h>
52 #endif
53 #ifdef HAVE_STDLIB_H
54 #include <stdlib.h>
55 #endif
56
57 #ifdef HAVE_NBUTIL_H
58 #include <nbutil.h>
59 #endif
60
61 #if defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
62 #include <sys/mntctl.h>
63 #include <sys/vmount.h>
64 #include <sys/statfs.h>
65 #endif
66
67 netsnmp_feature_require(se_find_free_value_in_slist);
68 netsnmp_feature_require(date_n_time);
69 netsnmp_feature_require(ctime_to_timet);
70
71 #ifndef MNTTYPE_AUTOFS
72 #define MNTTYPE_AUTOFS "autofs"
73 #endif
74
75 #if defined(bsdi4) || defined(freebsd3) || defined(freebsd4) || defined(freebsd5) || defined(darwin)
76 #if defined(HAVE_GETFSSTAT) && defined(MFSNAMELEN)
77 #define MOUNT_NFS "nfs"
78 #define MNTTYPE_UFS "ufs"
79 #define BerkelyFS
80 #define MNTTYPE_FFS "ffs"
81 #define MNTTYPE_NFS "nfs"
82 #define MNTTYPE_NFS3 "nfs"
83 #define MNTTYPE_HFS "hfs"
84 #define MNTTYPE_MFS "mfs"
85 #define MNTTYPE_MSDOS "msdos"
86 #define MNTTYPE_LFS "lfs"
87 #define MNTTYPE_FDESC "fdesc"
88 #define MNTTYPE_PORTAL "portal"
89 #define MNTTYPE_NULL "null"
90 #define MNTTYPE_UMAP "umap"
91 #define MNTTYPE_KERNFS "kernfs"
92 #define MNTTYPE_PROCFS "procfs"
93 #define MNTTYPE_AFS "afs"
94 #define MNTTYPE_CD9660 "cd9660"
95 #define MNTTYPE_UNION "union"
96 #define MNTTYPE_ADOSFS "adosfs"
97 #define MNTTYPE_EXT2FS "ext2fs"
98 #define MNTTYPE_CFS "coda"
99 #define MNTTYPE_NTFS "ntfs"
100 #endif
101 #endif /* freebsd3 */
102
103 #define HRFS_MONOTONICALLY_INCREASING
104
105 /*********************
106 *
107 * Kernel & interface information,
108 * and internal forward declarations
109 *
110 *********************/
111
112 #ifdef solaris2
113
114 struct mnttab HRFS_entry_struct;
115 struct mnttab *HRFS_entry = &HRFS_entry_struct;
116 #define HRFS_name mnt_special
117 #define HRFS_mount mnt_mountp
118 #define HRFS_type mnt_fstype
119 #define HRFS_statfs statvfs
120
121 #elif defined(HAVE_STATVFS) && defined(__NetBSD__)
122
123 #if !defined(MFSNAMELEN) && defined(_VFS_NAMELEN)
124 #define MFSNAMELEN _VFS_NAMELEN
125 #endif
126
127 #define getfsstat getvfsstat
128
129 static struct statvfs *fsstats = NULL;
130 struct statvfs *HRFS_entry;
131 static int fscount;
132 #define HRFS_mount f_mntonname
133 #define HRFS_name f_mntfromname
134 #define HRFS_statfs statvfs
135 #define HRFS_type f_fstypename
136 #elif defined(HAVE_GETFSSTAT) && !defined(HAVE_STATFS) && defined(HAVE_STATVFS)
137
138 static struct statfs *fsstats = NULL;
139 struct statfs *HRFS_entry;
140 static int fscount;
141 #define HRFS_mount f_mntonname
142 #define HRFS_name f_mntfromname
143 #define HRFS_statfs statvfs
144 #define HRFS_type f_fstypename
145
146 #elif defined(HAVE_GETFSSTAT)
147 static struct statfs *fsstats = 0;
148 static int fscount;
149 struct statfs *HRFS_entry;
150 #define HRFS_statfs statfs
151 #ifdef MFSNAMELEN
152 #define HRFS_type f_fstypename
153 #else
154 #define HRFS_type f_type
155 #endif
156 #define HRFS_mount f_mntonname
157 #define HRFS_name f_mntfromname
158
159 #elif defined(dynix)
160 struct mntent *HRFS_entry;
161 #define HRFS_name mnt_fsname
162 #define HRFS_mount mnt_dir
163 #define HRFS_type mnt_type
164 #define HRFS_statfs statvfs
165
166 #elif defined(WIN32)
167 struct win_statfs *HRFS_entry;
168 static int fscount;
169 #define HRFS_mount f_driveletter
170 #define HRFS_name f_fsid
171 #define HRFS_statfs win_statfs
172 #define HRFS_type f_type
173
174 #elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
175
176 struct vmount *aixmnt, *aixcurr;
177 struct HRFS_entry {
178 char *HRFS_name;
179 char *HRFS_mount;
180 int HRFS_type;
181 int HRFS_flags;
182 } *HRFS_entry;
183 #define HRFS_statfs statfs
184
185 #else
186 struct mntent *HRFS_entry;
187 #define HRFS_name mnt_fsname
188 #define HRFS_mount mnt_dir
189 #define HRFS_type mnt_type
190 #define HRFS_statfs statfs
191
192 #ifdef linux
193 #define MNTTYPE_CD9660 "iso9660"
194 #define MNTTYPE_EXT2FS "ext2"
195 #define MNTTYPE_EXT3FS "ext3"
196 #define MNTTYPE_SMBFS "smbfs"
197 #define MNTTYPE_MSDOS "msdos"
198 #define MNTTYPE_FAT32 "vfat"
199 #define MNTTYPE_NTFS "ntfs"
200 #define MNTTYPE_NFS4 "nfs4"
201 #endif /* linux */
202
203 #endif
204
205 #define FULL_DUMP 0
206 #define PART_DUMP 1
207
208 static u_char *when_dumped(const char *filesys, int level, size_t *length);
209
210 /*********************
211 *
212 * Initialisation & common implementation functions
213 *
214 *********************/
215
216 #define HRFSYS_INDEX 1
217 #define HRFSYS_MOUNT 2
218 #define HRFSYS_RMOUNT 3
219 #define HRFSYS_TYPE 4
220 #define HRFSYS_ACCESS 5
221 #define HRFSYS_BOOT 6
222 #define HRFSYS_STOREIDX 7
223 #define HRFSYS_FULLDUMP 8
224 #define HRFSYS_PARTDUMP 9
225
226 struct variable4 hrfsys_variables[] = {
227 {HRFSYS_INDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
228 var_hrfilesys, 2, {1, 1}},
229 {HRFSYS_MOUNT, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
230 var_hrfilesys, 2, {1, 2}},
231 {HRFSYS_RMOUNT, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
232 var_hrfilesys, 2, {1, 3}},
233 {HRFSYS_TYPE, ASN_OBJECT_ID, NETSNMP_OLDAPI_RONLY,
234 var_hrfilesys, 2, {1, 4}},
235 {HRFSYS_ACCESS, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
236 var_hrfilesys, 2, {1, 5}},
237 {HRFSYS_BOOT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
238 var_hrfilesys, 2, {1, 6}},
239 {HRFSYS_STOREIDX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
240 var_hrfilesys, 2, {1, 7}},
241 {HRFSYS_FULLDUMP, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
242 var_hrfilesys, 2, {1, 8}},
243 {HRFSYS_PARTDUMP, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
244 var_hrfilesys, 2, {1, 9}},
245 };
246 oid hrfsys_variables_oid[] = { 1, 3, 6, 1, 2, 1, 25, 3, 8 };
247
248 void
249 init_hr_filesys(void)
250 {
251 REGISTER_MIB("host/hr_filesys", hrfsys_variables, variable4,
252 hrfsys_variables_oid);
253 #if defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
254 /* something leaks, make it idiot-safe */
255 aixmnt = NULL;
256 aixcurr = NULL;
257 #endif
258 }
259
260 /*
261 * header_hrfilesys(...
262 * Arguments:
263 * vp IN - pointer to variable entry that points here
264 * name IN/OUT - IN/name requested, OUT/name found
265 * length IN/OUT - length of IN/OUT oid's
266 * exact IN - TRUE if an exact match was requested
267 * var_len OUT - length of variable or 0 if function returned
268 * write_method
269 *
270 */
271
272 int
273 header_hrfilesys(struct variable *vp,
274 oid * name,
275 size_t * length,
276 int exact, size_t * var_len, WriteMethod ** write_method)
277 {
278 #define HRFSYS_ENTRY_NAME_LENGTH 11
279 oid newname[MAX_OID_LEN];
280 int fsys_idx, LowIndex = -1;
281 int result;
282
283 DEBUGMSGTL(("host/hr_filesys", "var_hrfilesys: "));
284 DEBUGMSGOID(("host/hr_filesys", name, *length));
285 DEBUGMSG(("host/hr_filesys", " %d\n", exact));
286
287 memcpy((char *) newname, (char *) vp->name, vp->namelen * sizeof(oid));
288 /*
289 * Find "next" file system entry
290 */
291
292 Init_HR_FileSys();
293 for (;;) {
294 fsys_idx = Get_Next_HR_FileSys();
295 if (fsys_idx == -1)
296 break;
297 newname[HRFSYS_ENTRY_NAME_LENGTH] = fsys_idx;
298 result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);
299 if (exact && (result == 0)) {
300 LowIndex = fsys_idx;
301 break;
302 }
303 if ((!exact && (result < 0)) &&
304 (LowIndex == -1 || fsys_idx < LowIndex)) {
305 LowIndex = fsys_idx;
306 #ifdef HRFS_MONOTONICALLY_INCREASING
307 break;
308 #endif
309 }
310 }
311
312 if (LowIndex == -1) {
313 DEBUGMSGTL(("host/hr_filesys", "... index out of range\n"));
314 return (MATCH_FAILED);
315 }
316
317 memcpy((char *) name, (char *) newname,
318 (vp->namelen + 1) * sizeof(oid));
319 *length = vp->namelen + 1;
320 *write_method = (WriteMethod*)0;
321 *var_len = sizeof(long); /* default to 'long' results */
322
323 DEBUGMSGTL(("host/hr_filesys", "... get filesys stats "));
324 DEBUGMSGOID(("host/hr_filesys", name, *length));
325 DEBUGMSG(("host/hr_filesys", "\n"));
326
327 return LowIndex;
328 }
329
330
331 oid fsys_type_id[] = { 1, 3, 6, 1, 2, 1, 25, 3, 9, 1 }; /* hrFSOther */
332 int fsys_type_len =
333 sizeof(fsys_type_id) / sizeof(fsys_type_id[0]);
334
335 /*********************
336 *
337 * System specific implementation functions
338 *
339 *********************/
340
341
342 u_char *
343 var_hrfilesys(struct variable *vp,
344 oid * name,
345 size_t * length,
346 int exact, size_t * var_len, WriteMethod ** write_method)
347 {
348 int fsys_idx;
349 static char string[1024];
350 char *mnt_type;
351
352 fsys_idx =
353 header_hrfilesys(vp, name, length, exact, var_len, write_method);
354 if (fsys_idx == MATCH_FAILED)
355 return NULL;
356
357
358 switch (vp->magic) {
359 case HRFSYS_INDEX:
360 long_return = fsys_idx;
361 return (u_char *) & long_return;
362 case HRFSYS_MOUNT:
363 strlcpy(string, HRFS_entry->HRFS_mount, sizeof(string));
364 *var_len = strlen(string);
365 return (u_char *) string;
366 case HRFSYS_RMOUNT:
367 if (Check_HR_FileSys_NFS())
368 strlcpy(string, HRFS_entry->HRFS_name, sizeof(string));
369 else
370 string[0] = '\0';
371 *var_len = strlen(string);
372 return (u_char *) string;
373
374 case HRFSYS_TYPE:
375 if (Check_HR_FileSys_NFS())
376 fsys_type_id[fsys_type_len - 1] = 14;
377 else {
378 /*
379 * Not sufficient to identity the file
380 * type precisely, but it's a start.
381 */
382 #if defined(HAVE_GETFSSTAT) && !defined(MFSNAMELEN)
383 switch (HRFS_entry->HRFS_type) {
384 case MOUNT_UFS:
385 fsys_type_id[fsys_type_len - 1] = 3;
386 break;
387 case MOUNT_NFS:
388 fsys_type_id[fsys_type_len - 1] = 14;
389 break;
390 case MOUNT_HFS:
391 fsys_type_id[fsys_type_len - 1] = 7;
392 break;
393 case MOUNT_MFS:
394 fsys_type_id[fsys_type_len - 1] = 8;
395 break;
396 case MOUNT_MSDOS:
397 fsys_type_id[fsys_type_len - 1] = 5;
398 break;
399 case MOUNT_LFS:
400 fsys_type_id[fsys_type_len - 1] = 1;
401 break;
402 case MOUNT_LOFS:
403 fsys_type_id[fsys_type_len - 1] = 1;
404 break;
405 case MOUNT_FDESC:
406 fsys_type_id[fsys_type_len - 1] = 1;
407 break;
408 case MOUNT_PORTAL:
409 fsys_type_id[fsys_type_len - 1] = 1;
410 break;
411 case MOUNT_NULL:
412 fsys_type_id[fsys_type_len - 1] = 1;
413 break;
414 case MOUNT_UMAP:
415 fsys_type_id[fsys_type_len - 1] = 1;
416 break;
417 case MOUNT_KERNFS:
418 fsys_type_id[fsys_type_len - 1] = 1;
419 break;
420 case MOUNT_PROCFS:
421 fsys_type_id[fsys_type_len - 1] = 1;
422 break;
423 case MOUNT_AFS:
424 fsys_type_id[fsys_type_len - 1] = 16;
425 break;
426 case MOUNT_CD9660:
427 fsys_type_id[fsys_type_len - 1] = 12;
428 break;
429 case MOUNT_UNION:
430 fsys_type_id[fsys_type_len - 1] = 1;
431 break;
432 case MOUNT_DEVFS:
433 fsys_type_id[fsys_type_len - 1] = 1;
434 break;
435 #ifdef MOUNT_EXT2FS
436 case MOUNT_EXT2FS:
437 fsys_type_id[fsys_type_len - 1] = 23;
438 break;
439 #endif
440 #ifdef MOUNT_TFS
441 case MOUNT_TFS:
442 fsys_type_id[fsys_type_len - 1] = 15;
443 break;
444 #endif
445 }
446 #elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
447 switch (HRFS_entry->HRFS_type) {
448 case MNT_AIX:
449 case MNT_JFS:
450 fsys_type_id[fsys_type_len - 1] = 3;
451 break;
452 case MNT_CDROM:
453 fsys_type_id[fsys_type_len - 1] = 12;
454 break;
455 #ifdef MNT_NAMEFS
456 case MNT_NAMEFS:
457 #endif
458 #ifdef MNT_PROCFS
459 case MNT_PROCFS:
460 #endif
461 case MNT_SFS:
462 case MNT_CACHEFS:
463 fsys_type_id[fsys_type_len - 1] = 1;
464 break;
465 case MNT_NFS:
466 case MNT_NFS3:
467 case MNT_AUTOFS:
468 fsys_type_id[fsys_type_len - 1] = 14;
469 break;
470 }
471 #else
472 mnt_type = HRFS_entry->HRFS_type;
473 if (mnt_type == NULL)
474 fsys_type_id[fsys_type_len - 1] = 2; /* unknown */
475 #ifdef MNTTYPE_HFS
476 else if (!strcmp(mnt_type, MNTTYPE_HFS))
477 fsys_type_id[fsys_type_len - 1] = 7;
478 #endif
479 #ifdef MNTTYPE_UFS
480 else if (!strcmp(mnt_type, MNTTYPE_UFS))
481 #if (defined(BerkelyFS) && !defined(MNTTYPE_HFS)) || defined(solaris2)
482 fsys_type_id[fsys_type_len - 1] = 3;
483 #else /* SysV */
484 fsys_type_id[fsys_type_len - 1] = 4; /* or 3? XXX */
485 #endif
486 #endif
487 #ifdef MNTTYPE_SYSV
488 else if (!strcmp(mnt_type, MNTTYPE_SYSV))
489 fsys_type_id[fsys_type_len - 1] = 4;
490 #endif
491 #ifdef MNTTYPE_PC
492 else if (!strcmp(mnt_type, MNTTYPE_PC))
493 fsys_type_id[fsys_type_len - 1] = 5;
494 #endif
495 #ifdef MNTTYPE_MSDOS
496 else if (!strcmp(mnt_type, MNTTYPE_MSDOS))
497 fsys_type_id[fsys_type_len - 1] = 5;
498 #endif
499 #ifdef MNTTYPE_FAT32
500 else if (!strcmp(mnt_type, MNTTYPE_FAT32))
501 fsys_type_id[fsys_type_len - 1] = 22;
502 #endif
503 #ifdef MNTTYPE_CDFS
504 else if (!strcmp(mnt_type, MNTTYPE_CDFS))
505 #ifdef RockRidge
506 fsys_type_id[fsys_type_len - 1] = 13;
507 #else /* ISO 9660 */
508 fsys_type_id[fsys_type_len - 1] = 12;
509 #endif
510 #endif
511 #ifdef MNTTYPE_HSFS
512 else if (!strcmp(mnt_type, MNTTYPE_HSFS))
513 fsys_type_id[fsys_type_len - 1] = 13;
514 #endif
515 #ifdef MNTTYPE_ISO9660
516 else if (!strcmp(mnt_type, MNTTYPE_ISO9660))
517 fsys_type_id[fsys_type_len - 1] = 12;
518 #endif
519 #ifdef MNTTYPE_CD9660
520 else if (!strcmp(mnt_type, MNTTYPE_CD9660))
521 fsys_type_id[fsys_type_len - 1] = 12;
522 #endif
523 #ifdef MNTTYPE_SMBFS
524 else if (!strcmp(mnt_type, MNTTYPE_SMBFS))
525 fsys_type_id[fsys_type_len - 1] = 14;
526 #endif
527 #ifdef MNTTYPE_NFS
528 else if (!strcmp(mnt_type, MNTTYPE_NFS))
529 fsys_type_id[fsys_type_len - 1] = 14;
530 #endif
531 #ifdef MNTTYPE_NFS3
532 else if (!strcmp(mnt_type, MNTTYPE_NFS3))
533 fsys_type_id[fsys_type_len - 1] = 14;
534 #endif
535 #ifdef MNTTYPE_NFS4
536 else if (!strcmp(mnt_type, MNTTYPE_NFS4))
537 fsys_type_id[fsys_type_len - 1] = 14;
538 #endif
539 #ifdef MNTTYPE_MFS
540 else if (!strcmp(mnt_type, MNTTYPE_MFS))
541 fsys_type_id[fsys_type_len - 1] = 8;
542 #endif
543 #ifdef MNTTYPE_EXT2FS
544 else if (!strcmp(mnt_type, MNTTYPE_EXT2FS))
545 fsys_type_id[fsys_type_len - 1] = 23;
546 #endif
547 #ifdef MNTTYPE_EXT3FS
548 else if (!strcmp(mnt_type, MNTTYPE_EXT3FS))
549 fsys_type_id[fsys_type_len - 1] = 23;
550 #endif
551 #ifdef MNTTYPE_NTFS
552 else if (!strcmp(mnt_type, MNTTYPE_NTFS))
553 fsys_type_id[fsys_type_len - 1] = 9;
554 #endif
555 else
556 fsys_type_id[fsys_type_len - 1] = 1; /* Other */
557 #endif /* HAVE_GETFSSTAT */
558 }
559
560 *var_len = sizeof(fsys_type_id);
561 return (u_char *) fsys_type_id;
562
563 case HRFSYS_ACCESS:
564 #if defined(HAVE_STATVFS) && defined(__NetBSD__)
565 long_return = HRFS_entry->f_flag & MNT_RDONLY ? 2 : 1;
566 #elif defined(HAVE_GETFSSTAT)
567 #ifdef HAVE_STRUCT_STATFS_F_FLAGS
568 long_return = HRFS_entry->f_flags & MNT_RDONLY ? 2 : 1;
569 #else
570 long_return = HRFS_entry->f_flag & MNT_RDONLY ? 2 : 1;
571 #endif
572 #elif defined(cygwin)
573 long_return = 1;
574 #elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
575 long_return = (HRFS_entry->HRFS_flags & MNT_READONLY) == 0 ? 1 : 2;
576 #else
577 #ifdef HAVE_HASMNTOPT
578 if (hasmntopt(HRFS_entry, "ro") != NULL)
579 long_return = 2; /* Read Only */
580 else
581 #endif
582 long_return = 1; /* Read-Write */
583 #endif
584 return (u_char *) & long_return;
585 case HRFSYS_BOOT:
586 if (HRFS_entry->HRFS_mount[0] == '/' &&
587 HRFS_entry->HRFS_mount[1] == 0)
588 long_return = 1; /* root is probably bootable! */
589 else
590 long_return = 2; /* others probably aren't */
591 return (u_char *) & long_return;
592 case HRFSYS_STOREIDX:
593 long_return = fsys_idx + NETSNMP_MEM_TYPE_MAX;
594 return (u_char *) & long_return;
595 case HRFSYS_FULLDUMP:
596 return when_dumped(HRFS_entry->HRFS_name, FULL_DUMP, var_len);
597 case HRFSYS_PARTDUMP:
598 return when_dumped(HRFS_entry->HRFS_name, PART_DUMP, var_len);
599 default:
600 DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrfilesys\n",
601 vp->magic));
602 }
603 return NULL;
604 }
605
606
607 /*********************
608 *
609 * Internal implementation functions
610 *
611 *********************/
612
613 static int HRFS_index;
614 #ifndef HAVE_GETFSSTAT
615 static FILE *fp;
616 #endif
617
618 void
619 Init_HR_FileSys(void)
620 {
621 #ifdef HAVE_GETFSSTAT
622 #if defined(HAVE_STATVFS) && defined(__NetBSD__)
623 fscount = getvfsstat(NULL, 0, ST_NOWAIT);
624 #else
625 fscount = getfsstat(NULL, 0, MNT_WAIT);
626 #endif
627 if (fsstats)
628 free(fsstats);
629 fsstats = NULL;
630 fsstats = malloc(fscount * sizeof(*fsstats));
631 #if defined(HAVE_STATVFS) && defined(__NetBSD__)
632 getvfsstat(fsstats, fscount * sizeof(*fsstats), ST_NOWAIT);
633 #else
634 getfsstat(fsstats, fscount * sizeof(*fsstats), MNT_WAIT);
635 #endif
636 HRFS_index = 0;
637 #elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
638 int ret;
639 uint size;
640 ret = 0;
641 size = 0;
642 if(aixmnt != NULL) free(aixmnt); /* something leaks, make it idiot-safe */
643 aixmnt = NULL;
644 aixcurr = NULL;
645 HRFS_index = 0;
646 ret = mntctl(MCTL_QUERY, sizeof(uint), &size);
647 if(ret == 0 && size > 0) {
648 aixmnt = malloc(size + sizeof(struct HRFS_entry));
649 if(aixmnt != NULL) {
650 HRFS_entry = (char *) aixmnt + size;
651 ret = mntctl(MCTL_QUERY, size, aixmnt);
652 HRFS_index = 1;
653 if(ret <= 0) {
654 free(aixmnt);
655 aixmnt = NULL;
656 HRFS_entry = NULL;
657 } else aixcurr = aixmnt;
658 }
659 }
660 #else
661 HRFS_index = 1;
662 if (fp != NULL)
663 fclose(fp);
664 fp = fopen(ETC_MNTTAB, "r");
665 if (!fp) {
666 netsnmp_config_error("Can't open mnttab %s\n", ETC_MNTTAB);
667 }
668 #endif
669 }
670
671 static const char *HRFS_ignores[] = {
672 #ifdef MNTTYPE_IGNORE
673 MNTTYPE_IGNORE,
674 #endif
675 #ifdef MNTTYPE_SWAP
676 MNTTYPE_SWAP,
677 #endif
678 #ifdef MNTTYPE_PROC
679 MNTTYPE_PROC,
680 #endif
681 #ifdef MNTTYPE_PROCFS
682 MNTTYPE_PROCFS,
683 #endif
684 MNTTYPE_AUTOFS,
685 #ifdef linux
686 "bdev",
687 "binfmt_misc",
688 "cpuset",
689 "debugfs",
690 "devfs",
691 "devpts",
692 "eventpollfs",
693 "futexfs",
694 "hugetlbfs",
695 "inotifyfs",
696 "mqueue",
697 "nfsd",
698 "pipefs",
699 "proc",
700 "ramfs",
701 "rootfs",
702 "rpc_pipefs",
703 "securityfs",
704 "shm",
705 "sockfs",
706 "sysfs",
707 "usbdevfs",
708 "usbfs",
709 #endif
710 #ifdef solaris2
711 "mntfs",
712 "proc",
713 "fd",
714 #endif
715 NULL
716 };
717
718 int
719 Get_Next_HR_FileSys(void)
720 {
721 next:
722 #ifdef HAVE_GETFSSTAT
723 if (HRFS_index >= fscount)
724 return -1;
725 HRFS_entry = fsstats + HRFS_index;
726 return ++HRFS_index;
727 #elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
728 if(aixcurr == NULL) {
729 if(aixmnt != NULL) free(aixmnt);
730 aixmnt = NULL;
731 HRFS_entry = NULL;
732 return -1;
733 }
734 HRFS_entry->HRFS_name = vmt2dataptr(aixcurr, VMT_OBJECT);
735 HRFS_entry->HRFS_mount = vmt2dataptr(aixcurr, VMT_STUB);
736 HRFS_entry->HRFS_type = aixcurr->vmt_gfstype;
737 HRFS_entry->HRFS_flags = aixcurr->vmt_flags;
738 aixcurr = (char *) aixcurr + aixcurr->vmt_length;
739 if((char *) aixcurr >= (char *) HRFS_entry) aixcurr = NULL;
740 switch(HRFS_entry->HRFS_type) {
741 #ifdef MNT_NAMEFS
742 case MNT_NAMEFS:
743 #endif
744 #ifdef MNT_PROCFS
745 case MNT_PROCFS:
746 #endif
747 case MNT_SFS:
748 goto next;
749 }
750 return HRFS_index++;
751 #else
752 const char **cpp;
753
754 if (fp == NULL)
755 return -1;
756
757 #ifdef solaris2
758 if (getmntent(fp, HRFS_entry) != 0)
759 return -1;
760 #else
761 HRFS_entry = getmntent(fp);
762 if (HRFS_entry == NULL)
763 return -1;
764 #endif /* solaris2 */
765
766 for (cpp = HRFS_ignores; *cpp != NULL; ++cpp)
767 if (!strcmp(HRFS_entry->HRFS_type, *cpp))
768 goto next;
769
770 /*
771 * Try and ensure that index values are persistent
772 * at least within a single run of the agent
773 */
774 HRFS_index = se_find_value_in_slist("filesys", HRFS_entry->HRFS_name );
775 if (HRFS_index == SE_DNE) {
776 HRFS_index = se_find_free_value_in_slist("filesys");
777 if (HRFS_index == SE_DNE) { HRFS_index = 1; }
778 se_add_pair_to_slist( "filesys",
779 strdup( HRFS_entry->HRFS_name ), HRFS_index);
780 }
781
782 return HRFS_index++;
783 #endif /* HAVE_GETFSSTAT */
784 }
785
786 /*
787 * this function checks whether the current file system (info can be found
788 * in HRFS_entry) is a NFS file system
789 * HRFS_entry must be valid prior to calling this function
790 * returns 1 if NFS file system, 0 otherwise
791 */
792 int
793 Check_HR_FileSys_NFS (void)
794 {
795 #if defined(HAVE_GETFSSTAT) && !defined(MFSNAMELEN)
796 if ((HRFS_entry->HRFS_type == MOUNT_NFS) ||
797 (HRFS_entry->HRFS_type == MOUNT_AFS))
798 #elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
799 if(HRFS_entry->HRFS_type == MNT_NFS || HRFS_entry->HRFS_type == MNT_NFS3)
800 #else /* HAVE_GETFSSTAT */
801 if ( HRFS_entry->HRFS_type != NULL && (
802 #if defined(MNTTYPE_NFS)
803 !strcmp( HRFS_entry->HRFS_type, MNTTYPE_NFS) ||
804 #else
805 !strcmp( HRFS_entry->HRFS_type, "nfs") ||
806 #endif
807 #if defined(MNTTYPE_NFS3)
808 !strcmp( HRFS_entry->HRFS_type, MNTTYPE_NFS3) ||
809 #endif
810 #if defined(MNTTYPE_NFS4)
811 !strcmp( HRFS_entry->HRFS_type, MNTTYPE_NFS4) ||
812 #endif
813 #if defined(MNTTYPE_SMBFS)
814 !strcmp( HRFS_entry->HRFS_type, MNTTYPE_SMBFS) ||
815 #endif
816 #if defined(MNTTYPE_LOFS)
817 !strcmp( HRFS_entry->HRFS_type, MNTTYPE_LOFS) ||
818 #endif
819 #if defined(MNTTYPE_AFP)
820 !strcmp( HRFS_entry->HRFS_type, MNTTYPE_AFP) ||
821 #endif
822 !strcmp( HRFS_entry->HRFS_type, "cifs") ||
823 /*
824 * MVFS is Rational ClearCase's view file system
825 * it is similiar to NFS file systems in that it is mounted
826 * locally or remotely from the ClearCase server
827 */
828 !strcmp( HRFS_entry->HRFS_type, "mvfs")))
829 #endif /* HAVE_GETFSSTAT */
830 return 1; /* NFS file system */
831
832 return 0; /* no NFS file system */
833 }
834
835 /* This function checks whether current file system is an AutoFs
836 * HRFS_entry must be valid prior to calling this function
837 * return 1 if AutoFs, 0 otherwise
838 */
839 int
840 Check_HR_FileSys_AutoFs(void)
841 {
842 return HRFS_entry->HRFS_type &&
843 strcmp(HRFS_entry->HRFS_type, MNTTYPE_AUTOFS) == 0;
844 }
845
846 void
847 End_HR_FileSys(void)
848 {
849 #ifdef HAVE_GETFSSTAT
850 if (fsstats)
851 free(fsstats);
852 fsstats = NULL;
853 #elif defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
854 if(aixmnt != NULL) {
855 free(aixmnt);
856 aixmnt = NULL;
857 aixcurr = NULL;
858 HRFS_entry = NULL;
859 }
860 #else
861 if (fp != NULL)
862 fclose(fp);
863 fp = NULL;
864 #endif
865 }
866
867
868 static u_char *
869 when_dumped(const char *filesys, int level, size_t *length)
870 {
871 time_t dumpdate = 0, tmp;
872 FILE *dump_fp;
873 char line[1024];
874 char *cp1, *cp2, *cp3;
875
876 /*
877 * Look for the relevent entries in /etc/dumpdates
878 *
879 * This is complicated by the fact that disks are
880 * mounted using block devices, but dumps are
881 * done via the raw character devices.
882 * Thus the device names in /etc/dumpdates and
883 * /etc/mnttab don't match.
884 * These comparisons are therefore made using the
885 * final portion of the device name only.
886 */
887
888 if (*filesys == '\0') /* No filesystem name? */
889 return date_n_time(NULL, length);
890 cp1 = strrchr(filesys, '/'); /* Find the last element of the current FS */
891
892 if (cp1 == NULL)
893 cp1 = filesys;
894
895 if ((dump_fp = fopen("/etc/dumpdates", "r")) == NULL)
896 return date_n_time(NULL, length);
897
898 while (fgets(line, sizeof(line), dump_fp) != NULL) {
899 cp2 = strchr(line, ' '); /* Start by looking at the device name only */
900 if (cp2 != NULL) {
901 *cp2 = '\0';
902 cp3 = strrchr(line, '/'); /* and find the last element */
903 if (cp3 == NULL)
904 cp3 = line;
905
906 if (strcmp(cp1, cp3) != 0) /* Wrong FS */
907 continue;
908
909 ++cp2;
910 while (isspace(*cp2 & 0xFF))
911 ++cp2; /* Now find the dump level */
912
913 if (level == FULL_DUMP) {
914 if (*(cp2++) != '0')
915 continue; /* Not interested in partial dumps */
916 while (isspace(*cp2 & 0xFF))
917 ++cp2;
918
919 dumpdate = ctime_to_timet(cp2);
920 fclose(dump_fp);
921 return date_n_time(&dumpdate, length);
922 } else { /* Partial Dump */
923 if (*(cp2++) == '0')
924 continue; /* Not interested in full dumps */
925 while (isspace(*cp2 & 0xFF))
926 ++cp2;
927
928 tmp = ctime_to_timet(cp2);
929 if (tmp > dumpdate)
930 dumpdate = tmp; /* Remember the 'latest' partial dump */
931 }
932 }
933 }
934
935 fclose(dump_fp);
936
937 return date_n_time(&dumpdate, length);
938 }
939
940
941 #define RAW_DEVICE_PREFIX "/dev/rdsk"
942 #define COOKED_DEVICE_PREFIX "/dev/dsk"
943
944 char *
945 cook_device(char *dev)
946 {
947 static char cooked_dev[SNMP_MAXPATH+1];
948
949 if (!strncmp(dev, RAW_DEVICE_PREFIX, strlen(RAW_DEVICE_PREFIX))) {
950 strlcpy(cooked_dev, COOKED_DEVICE_PREFIX, sizeof(cooked_dev));
951 strlcat(cooked_dev, dev + strlen(RAW_DEVICE_PREFIX),
952 sizeof(cooked_dev));
953 } else {
954 strlcpy(cooked_dev, dev, sizeof(cooked_dev));
955 }
956
957 return cooked_dev;
958 }
959
960
961 int
962 Get_FSIndex(char *dev)
963 {
964 int iindex;
965
966 Init_HR_FileSys();
967
968 while ((iindex = Get_Next_HR_FileSys()) != -1)
969 if (!strcmp(HRFS_entry->HRFS_name, cook_device(dev))) {
970 End_HR_FileSys();
971 return iindex;
972 }
973
974 End_HR_FileSys();
975 return 0;
976 }
977
978 long
979 Get_FSSize(char *dev)
980 {
981 struct HRFS_statfs statfs_buf;
982
983 Init_HR_FileSys();
984
985 while (Get_Next_HR_FileSys() != -1)
986 if (!strcmp(HRFS_entry->HRFS_name, cook_device(dev))) {
987 End_HR_FileSys();
988
989 if (HRFS_statfs(HRFS_entry->HRFS_mount, &statfs_buf) != -1)
990 /*
991 * with large file systems the following calculation produces
992 * an overflow:
993 * (statfs_buf.f_blocks*statfs_buf.f_bsize)/1024
994 *
995 * assumption: f_bsize is either 512 or a multiple of 1024
996 * in case of 512 (f_blocks/2) is returned
997 * otherwise (f_blocks*(f_bsize/1024)) is returned
998 */
999 #if defined(solaris2) && defined(HAVE_STRUCT_STATVFS_F_FRSIZE)
1000 return (statfs_buf.f_blocks*(statfs_buf.f_frsize/1024));
1001 #else
1002 if (statfs_buf.f_bsize == 512)
1003 return (statfs_buf.f_blocks/2);
1004 else
1005 return (statfs_buf.f_blocks*(statfs_buf.f_bsize/1024));
1006 #endif
1007 else
1008 return -1;
1009 }
1010
1011 End_HR_FileSys();
1012 return 0;
1013 }