2 * collectd - src/utils_mount.c
3 * Copyright (C) 2005,2006 Niki W. Waibel
5 * This program is free software; you can redistribute it and/
6 * or modify it under the terms of the GNU General Public Li-
7 * cence as published by the Free Software Foundation; either
8 * version 2 of the Licence, or any later version.
10 * This program is distributed in the hope that it will be use-
11 * ful, but WITHOUT ANY WARRANTY; without even the implied war-
12 * ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public Licence for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Niki W. Waibel <niki.waibel@gmx.net>
31 #include "utils_mount.h"
35 #define XFS_SUPER_MAGIC_STR "XFSB"
36 #define XFS_SUPER_MAGIC2_STR "BSFX"
39 #include "common.h" /* sstrncpy() et alii */
40 #include "plugin.h" /* ERROR() macro */
44 #include <sys/types.h>
46 #if HAVE_SYS_STATVFS_H
47 #include <sys/statvfs.h>
49 /* #endif HAVE_GETVFSSTAT */
53 #include <sys/param.h>
56 #include <sys/ucred.h>
59 #include <sys/mount.h>
61 #endif /* HAVE_GETFSSTAT */
67 #include <sys/mnttab.h>
74 #ifdef COLLECTD_MNTTAB
75 #undef COLLECTD_MNTTAB
78 #if defined(_PATH_MOUNTED) /* glibc */
79 #define COLLECTD_MNTTAB _PATH_MOUNTED
80 #elif defined(MNTTAB) /* Solaris */
81 #define COLLECTD_MNTTAB MNTTAB
82 #elif defined(MNT_MNTTAB)
83 #define COLLECTD_MNTTAB MNT_MNTTAB
84 #elif defined(MNTTABNAME)
85 #define COLLECTD_MNTTAB MNTTABNAME
87 #define COLLECTD_MNTTAB KMTAB
89 #define COLLECTD_MNTTAB "/etc/mnttab"
92 /* *** *** *** ********************************************* *** *** *** */
93 /* *** *** *** *** *** *** private functions *** *** *** *** *** *** */
94 /* *** *** *** ********************************************* *** *** *** */
96 /* stolen from quota-3.13 (quota-tools) */
98 #define PROC_PARTITIONS "/proc/partitions"
99 #define DEVLABELDIR "/dev"
103 static struct uuidCache_s {
104 struct uuidCache_s *next;
110 #define EXT2_SUPER_MAGIC 0xEF53
111 struct ext2_super_block {
112 unsigned char s_dummy1[56];
113 unsigned char s_magic[2];
114 unsigned char s_dummy2[46];
115 unsigned char s_uuid[16];
116 char s_volume_name[16];
118 #define ext2magic(s) \
119 ((unsigned int)s.s_magic[0] + (((unsigned int)s.s_magic[1]) << 8))
122 struct xfs_super_block {
123 unsigned char s_magic[4];
124 unsigned char s_dummy[28];
125 unsigned char s_uuid[16];
126 unsigned char s_dummy2[60];
129 #endif /* HAVE_XFS_XQM_H */
131 #define REISER_SUPER_MAGIC "ReIsEr2Fs"
132 struct reiserfs_super_block {
133 unsigned char s_dummy1[52];
134 unsigned char s_magic[10];
135 unsigned char s_dummy2[22];
136 unsigned char s_uuid[16];
137 char s_volume_name[16];
140 /* for now, only ext2 and xfs are supported */
141 static int get_label_uuid(const char *device, char **label, char *uuid) {
142 /* start with ext2 and xfs tests, taken from mount_guess_fstype */
143 /* should merge these later */
146 struct ext2_super_block e2sb;
148 struct xfs_super_block xfsb;
150 struct reiserfs_super_block reisersb;
152 fd = open(device, O_RDONLY);
157 if (lseek(fd, 1024, SEEK_SET) == 1024 &&
158 read(fd, (char *)&e2sb, sizeof(e2sb)) == sizeof(e2sb) &&
159 ext2magic(e2sb) == EXT2_SUPER_MAGIC) {
160 memcpy(uuid, e2sb.s_uuid, sizeof(e2sb.s_uuid));
161 namesize = sizeof(e2sb.s_volume_name);
162 *label = smalloc(namesize + 1);
163 sstrncpy(*label, e2sb.s_volume_name, namesize);
166 } else if (lseek(fd, 0, SEEK_SET) == 0 &&
167 read(fd, (char *)&xfsb, sizeof(xfsb)) == sizeof(xfsb) &&
168 (strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC_STR, 4) == 0 ||
169 strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC2_STR, 4) == 0)) {
170 memcpy(uuid, xfsb.s_uuid, sizeof(xfsb.s_uuid));
171 namesize = sizeof(xfsb.s_fsname);
172 *label = smalloc(namesize + 1);
173 sstrncpy(*label, xfsb.s_fsname, namesize);
175 #endif /* HAVE_XFS_XQM_H */
176 } else if (lseek(fd, 65536, SEEK_SET) == 65536 &&
177 read(fd, (char *)&reisersb, sizeof(reisersb)) ==
179 !strncmp((char *)&reisersb.s_magic, REISER_SUPER_MAGIC, 9)) {
180 memcpy(uuid, reisersb.s_uuid, sizeof(reisersb.s_uuid));
181 namesize = sizeof(reisersb.s_volume_name);
182 *label = smalloc(namesize + 1);
183 sstrncpy(*label, reisersb.s_volume_name, namesize);
190 static void uuidcache_addentry(char *device, char *label, char *uuid) {
191 struct uuidCache_s *last;
194 last = uuidCache = smalloc(sizeof(*uuidCache));
196 for (last = uuidCache; last->next; last = last->next)
198 last->next = smalloc(sizeof(*uuidCache));
202 last->device = device;
204 memcpy(last->uuid, uuid, sizeof(last->uuid));
207 static void uuidcache_init(void) {
211 static char ptname[100];
213 char uuid[16], *label = NULL;
221 procpt = fopen(PROC_PARTITIONS, "r");
222 if (procpt == NULL) {
226 for (int firstPass = 1; firstPass >= 0; firstPass--) {
227 fseek(procpt, 0, SEEK_SET);
228 while (fgets(line, sizeof(line), procpt)) {
229 if (sscanf(line, " %d %d %d %[^\n ]", &ma, &mi, &sz, ptname) != 4) {
233 /* skip extended partitions (heuristic: size 1) */
238 /* look only at md devices on first pass */
239 handleOnFirst = !strncmp(ptname, "md", 2);
240 if (firstPass != handleOnFirst) {
244 /* skip entire disk (minor 0, 64, ... on ide;
246 /* heuristic: partition name ends in a digit */
248 for (s = ptname; *s; s++)
251 if (isdigit((int)s[-1])) {
253 * Note: this is a heuristic only - there is no reason
254 * why these devices should live in /dev.
255 * Perhaps this directory should be specifiable by option.
256 * One might for example have /devlabel with links to /dev
257 * for the devices that may be accessed in this way.
258 * (This is useful, if the cdrom on /dev/hdc must not
261 snprintf(device, sizeof(device), "%s/%s", DEVLABELDIR, ptname);
262 if (!get_label_uuid(device, &label, uuid)) {
263 uuidcache_addentry(sstrdup(device), label, uuid);
271 static unsigned char fromhex(char c) {
272 if (isdigit((int)c)) {
274 } else if (islower((int)c)) {
281 static char *get_spec_by_x(int n, const char *t) {
282 struct uuidCache_s *uc;
290 if (!memcmp(t, uc->uuid, sizeof(uc->uuid))) {
291 return sstrdup(uc->device);
295 if (!strcmp(t, uc->label)) {
296 return sstrdup(uc->device);
305 static char *get_spec_by_uuid(const char *s) {
308 if (strlen(s) != 36 || s[8] != '-' || s[13] != '-' || s[18] != '-' ||
313 for (int i = 0; i < 16; i++) {
317 if (!isxdigit((int)s[0]) || !isxdigit((int)s[1])) {
320 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
323 return get_spec_by_x(UUID, uuid);
326 DEBUG("utils_mount: Found an invalid UUID: %s", s);
330 static char *get_spec_by_volume_label(const char *s) {
331 return get_spec_by_x(VOL, s);
334 static char *get_device_name(const char *optstr) {
337 if (optstr == NULL) {
339 } else if (strncmp(optstr, "UUID=", 5) == 0) {
340 DEBUG("utils_mount: TODO: check UUID= code!");
341 rc = get_spec_by_uuid(optstr + 5);
342 } else if (strncmp(optstr, "LABEL=", 6) == 0) {
343 DEBUG("utils_mount: TODO: check LABEL= code!");
344 rc = get_spec_by_volume_label(optstr + 6);
346 rc = sstrdup(optstr);
350 DEBUG("utils_mount: Error checking device name: optstr = %s", optstr);
355 /* What weird OS is this..? I can't find any info with google :/ -octo */
356 #if HAVE_LISTMNTENT && 0
357 static cu_mount_t *cu_mount_listmntent(void) {
358 cu_mount_t *last = *list;
361 struct tabmntent *mntlist;
362 if (listmntent(&mntlist, COLLECTD_MNTTAB, NULL, NULL) < 0) {
365 DEBUG("utils_mount: calling listmntent() failed: %s",
366 sstrerror(errno, errbuf, sizeof(errbuf)));
367 #endif /* COLLECT_DEBUG */
370 for (struct tabmntent *p = mntlist; p; p = p->next) {
371 char *loop = NULL, *device = NULL;
374 loop = cu_mount_getoptionvalue(mnt->mnt_opts, "loop=");
375 if (loop == NULL) { /* no loop= mount */
376 device = get_device_name(mnt->mnt_fsname);
377 if (device == NULL) {
378 DEBUG("utils_mount: can't get devicename for fs (%s) %s (%s)"
380 mnt->mnt_type, mnt->mnt_dir, mnt->mnt_fsname);
387 *list = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
390 while (last->next != NULL) { /* is last really last? */
393 last->next = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
396 last->dir = sstrdup(mnt->mnt_dir);
397 last->spec_device = sstrdup(mnt->mnt_fsname);
398 last->device = device;
399 last->type = sstrdup(mnt->mnt_type);
400 last->options = sstrdup(mnt->mnt_opts);
402 } /* for(p = mntlist; p; p = p->next) */
405 } /* cu_mount_t *cu_mount_listmntent(void) */
406 /* #endif HAVE_LISTMNTENT */
408 /* 4.4BSD and Mac OS X (getfsstat) or NetBSD (getvfsstat) */
409 #elif HAVE_GETVFSSTAT || HAVE_GETFSSTAT
410 static cu_mount_t *cu_mount_getfsstat(void) {
412 #define STRUCT_STATFS struct statfs
413 #define CMD_STATFS getfsstat
414 #define FLAGS_STATFS MNT_NOWAIT
415 /* #endif HAVE_GETFSSTAT */
416 #elif HAVE_GETVFSSTAT
417 #define STRUCT_STATFS struct statvfs
418 #define CMD_STATFS getvfsstat
419 #define FLAGS_STATFS ST_NOWAIT
420 #endif /* HAVE_GETVFSSTAT */
427 cu_mount_t *first = NULL;
428 cu_mount_t *last = NULL;
429 cu_mount_t *new = NULL;
431 /* Get the number of mounted file systems */
432 if ((bufsize = CMD_STATFS(NULL, 0, FLAGS_STATFS)) < 1) {
435 DEBUG("utils_mount: getv?fsstat failed: %s",
436 sstrerror(errno, errbuf, sizeof(errbuf)));
437 #endif /* COLLECT_DEBUG */
441 if ((buf = calloc(bufsize, sizeof(*buf))) == NULL)
444 /* The bufsize needs to be passed in bytes. Really. This is not in the
446 if ((num = CMD_STATFS(buf, bufsize * sizeof(STRUCT_STATFS), FLAGS_STATFS)) <
450 DEBUG("utils_mount: getv?fsstat failed: %s",
451 sstrerror(errno, errbuf, sizeof(errbuf)));
452 #endif /* COLLECT_DEBUG */
457 for (int i = 0; i < num; i++) {
458 if ((new = calloc(1, sizeof(*new))) == NULL)
461 /* Copy values from `struct mnttab' */
462 new->dir = sstrdup(buf[i].f_mntonname);
463 new->spec_device = sstrdup(buf[i].f_mntfromname);
464 new->type = sstrdup(buf[i].f_fstypename);
466 new->device = get_device_name(new->options);
483 /* #endif HAVE_GETVFSSTAT || HAVE_GETFSSTAT */
485 /* Solaris (SunOS 10): int getmntent(FILE *fp, struct mnttab *mp); */
486 #elif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT
487 static cu_mount_t *cu_mount_gen_getmntent(void) {
491 cu_mount_t *first = NULL;
492 cu_mount_t *last = NULL;
493 cu_mount_t *new = NULL;
495 DEBUG("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
497 if ((fp = fopen(COLLECTD_MNTTAB, "r")) == NULL) {
499 ERROR("fopen (%s): %s", COLLECTD_MNTTAB,
500 sstrerror(errno, errbuf, sizeof(errbuf)));
504 while (getmntent(fp, &mt) == 0) {
505 if ((new = calloc(1, sizeof(*new))) == NULL)
508 /* Copy values from `struct mnttab' */
509 new->dir = sstrdup(mt.mnt_mountp);
510 new->spec_device = sstrdup(mt.mnt_special);
511 new->type = sstrdup(mt.mnt_fstype);
512 new->options = sstrdup(mt.mnt_mntopts);
513 new->device = get_device_name(new->options);
529 } /* static cu_mount_t *cu_mount_gen_getmntent (void) */
530 /* #endif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT */
532 #elif HAVE_SEQ_GETMNTENT
533 #warn "This version of `getmntent' hat not yet been implemented!"
534 /* #endif HAVE_SEQ_GETMNTENT */
536 #elif HAVE_GETMNTENT_R
537 static cu_mount_t *cu_mount_getmntent(void) {
542 cu_mount_t *first = NULL;
543 cu_mount_t *last = NULL;
544 cu_mount_t *new = NULL;
546 DEBUG("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
548 if ((fp = setmntent(COLLECTD_MNTTAB, "r")) == NULL) {
550 ERROR("setmntent (%s): %s", COLLECTD_MNTTAB,
551 sstrerror(errno, errbuf, sizeof(errbuf)));
555 while (getmntent_r(fp, &me, mntbuf, sizeof(mntbuf))) {
556 if ((new = calloc(1, sizeof(*new))) == NULL)
559 /* Copy values from `struct mntent *' */
560 new->dir = sstrdup(me.mnt_dir);
561 new->spec_device = sstrdup(me.mnt_fsname);
562 new->type = sstrdup(me.mnt_type);
563 new->options = sstrdup(me.mnt_opts);
564 new->device = get_device_name(new->options);
567 DEBUG("utils_mount: new = {dir = %s, spec_device = %s, type = %s, options "
568 "= %s, device = %s}",
569 new->dir, new->spec_device, new->type, new->options, new->device);
583 DEBUG("utils_mount: return 0x%p", (void *)first);
586 } /* HAVE_GETMNTENT_R */
588 #elif HAVE_ONE_GETMNTENT
589 static cu_mount_t *cu_mount_getmntent(void) {
593 cu_mount_t *first = NULL;
594 cu_mount_t *last = NULL;
595 cu_mount_t *new = NULL;
597 DEBUG("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
599 if ((fp = setmntent(COLLECTD_MNTTAB, "r")) == NULL) {
601 ERROR("setmntent (%s): %s", COLLECTD_MNTTAB,
602 sstrerror(errno, errbuf, sizeof(errbuf)));
606 while ((me = getmntent(fp)) != NULL) {
607 if ((new = calloc(1, sizeof(*new))) == NULL)
610 /* Copy values from `struct mntent *' */
611 new->dir = sstrdup(me->mnt_dir);
612 new->spec_device = sstrdup(me->mnt_fsname);
613 new->type = sstrdup(me->mnt_type);
614 new->options = sstrdup(me->mnt_opts);
615 new->device = get_device_name(new->options);
618 DEBUG("utils_mount: new = {dir = %s, spec_device = %s, type = %s, options "
619 "= %s, device = %s}",
620 new->dir, new->spec_device, new->type, new->options, new->device);
634 DEBUG("utils_mount: return 0x%p", (void *)first);
638 #endif /* HAVE_ONE_GETMNTENT */
640 /* *** *** *** ******************************************** *** *** *** */
641 /* *** *** *** *** *** *** public functions *** *** *** *** *** *** */
642 /* *** *** *** ******************************************** *** *** *** */
644 cu_mount_t *cu_mount_getlist(cu_mount_t **list) {
646 cu_mount_t *first = NULL;
647 cu_mount_t *last = NULL;
655 while (last->next != NULL)
659 #if HAVE_LISTMNTENT && 0
660 new = cu_mount_listmntent();
661 #elif HAVE_GETVFSSTAT || HAVE_GETFSSTAT
662 new = cu_mount_getfsstat();
663 #elif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT
664 new = cu_mount_gen_getmntent();
665 #elif HAVE_SEQ_GETMNTENT
666 #error "This version of `getmntent' hat not yet been implemented!"
667 #elif HAVE_GETMNTENT_R
668 new = cu_mount_getmntent();
669 #elif HAVE_ONE_GETMNTENT
670 new = cu_mount_getmntent();
672 #error "Could not determine how to find mountpoints."
683 while ((last != NULL) && (last->next != NULL))
687 } /* cu_mount_t *cu_mount_getlist(cu_mount_t **list) */
689 void cu_mount_freelist(cu_mount_t *list) {
692 for (cu_mount_t *this = list; this != NULL; this = next) {
696 sfree(this->spec_device);
699 sfree(this->options);
702 } /* void cu_mount_freelist(cu_mount_t *list) */
704 char *cu_mount_checkoption(char *line, const char *keyword, int full) {
705 char *line2, *l2, *p1, *p2;
708 if (line == NULL || keyword == NULL) {
716 line2 = sstrdup(line);
718 while (*l2 != '\0') {
727 p2 = strchr(line, ',');
729 if (strncmp(line2 + (p1 - line) + 1, keyword, l + full) == 0) {
735 p2 = strchr(p1 + 1, ',');
737 } while (p1 != NULL);
741 } /* char *cu_mount_checkoption(char *line, char *keyword, int full) */
743 char *cu_mount_getoptionvalue(char *line, const char *keyword) {
746 r = cu_mount_checkoption(line, keyword, 0);
749 r += strlen(keyword);
758 m = smalloc(p - r + 1);
759 sstrncpy(m, r, p - r + 1);
764 } /* char *cu_mount_getoptionvalue(char *line, const char *keyword) */
766 int cu_mount_type(const char *type) {
767 if (strcmp(type, "ext3") == 0)
769 if (strcmp(type, "ext2") == 0)
771 if (strcmp(type, "ufs") == 0)
773 if (strcmp(type, "vxfs") == 0)
775 if (strcmp(type, "zfs") == 0)
778 } /* int cu_mount_type(const char *type) */