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>
30 #define XFS_SUPER_MAGIC_STR "XFSB"
31 #define XFS_SUPER_MAGIC2_STR "BSFX"
35 #include "utils_mount.h"
39 # include <sys/types.h>
41 # if HAVE_SYS_STATVFS_H
42 # include <sys/statvfs.h>
44 /* #endif HAVE_GETVFSSTAT */
48 # include <sys/param.h>
51 # include <sys/ucred.h>
54 # include <sys/mount.h>
56 #endif /* HAVE_GETFSSTAT */
62 # include <sys/mnttab.h>
69 #ifdef COLLECTD_MNTTAB
70 # undef COLLECTD_MNTTAB
73 #if defined(_PATH_MOUNTED) /* glibc */
74 # define COLLECTD_MNTTAB _PATH_MOUNTED
75 #elif defined(MNTTAB) /* Solaris */
76 # define COLLECTD_MNTTAB MNTTAB
77 #elif defined(MNT_MNTTAB)
78 # define COLLECTD_MNTTAB MNT_MNTTAB
79 #elif defined(MNTTABNAME)
80 # define COLLECTD_MNTTAB MNTTABNAME
82 # define COLLECTD_MNTTAB KMTAB
84 # define COLLECTD_MNTTAB "/etc/mnttab"
87 /* *** *** *** ********************************************* *** *** *** */
88 /* *** *** *** *** *** *** private functions *** *** *** *** *** *** */
89 /* *** *** *** ********************************************* *** *** *** */
91 /* stolen from quota-3.13 (quota-tools) */
93 #define PROC_PARTITIONS "/proc/partitions"
94 #define DEVLABELDIR "/dev"
98 static struct uuidCache_s {
99 struct uuidCache_s *next;
105 #define EXT2_SUPER_MAGIC 0xEF53
106 struct ext2_super_block {
107 unsigned char s_dummy1[56];
108 unsigned char s_magic[2];
109 unsigned char s_dummy2[46];
110 unsigned char s_uuid[16];
111 char s_volume_name[16];
113 #define ext2magic(s) ((unsigned int)s.s_magic[0] \
114 + (((unsigned int)s.s_magic[1]) << 8))
117 struct xfs_super_block {
118 unsigned char s_magic[4];
119 unsigned char s_dummy[28];
120 unsigned char s_uuid[16];
121 unsigned char s_dummy2[60];
124 #endif /* HAVE_XFS_XQM_H */
126 #define REISER_SUPER_MAGIC "ReIsEr2Fs"
127 struct reiserfs_super_block {
128 unsigned char s_dummy1[52];
129 unsigned char s_magic[10];
130 unsigned char s_dummy2[22];
131 unsigned char s_uuid[16];
132 char s_volume_name[16];
135 /* for now, only ext2 and xfs are supported */
137 get_label_uuid(const char *device, char **label, char *uuid)
139 /* start with ext2 and xfs tests, taken from mount_guess_fstype */
140 /* should merge these later */
143 struct ext2_super_block e2sb;
145 struct xfs_super_block xfsb;
147 struct reiserfs_super_block reisersb;
149 fd = open(device, O_RDONLY);
154 if(lseek(fd, 1024, SEEK_SET) == 1024
155 && read(fd, (char *)&e2sb, sizeof(e2sb)) == sizeof(e2sb)
156 && ext2magic(e2sb) == EXT2_SUPER_MAGIC) {
157 memcpy(uuid, e2sb.s_uuid, sizeof(e2sb.s_uuid));
158 namesize = sizeof(e2sb.s_volume_name);
159 *label = smalloc(namesize + 1);
160 sstrncpy(*label, e2sb.s_volume_name, namesize);
163 } else if(lseek(fd, 0, SEEK_SET) == 0
164 && read(fd, (char *)&xfsb, sizeof(xfsb)) == sizeof(xfsb)
165 && (strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC_STR, 4) == 0 ||
166 strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC2_STR, 4) == 0)) {
167 memcpy(uuid, xfsb.s_uuid, sizeof(xfsb.s_uuid));
168 namesize = sizeof(xfsb.s_fsname);
169 *label = smalloc(namesize + 1);
170 sstrncpy(*label, xfsb.s_fsname, namesize);
172 #endif /* HAVE_XFS_XQM_H */
173 } else if(lseek(fd, 65536, SEEK_SET) == 65536
174 && read(fd, (char *)&reisersb, sizeof(reisersb)) == sizeof(reisersb)
175 && !strncmp((char *)&reisersb.s_magic, REISER_SUPER_MAGIC, 9)) {
176 memcpy(uuid, reisersb.s_uuid, sizeof(reisersb.s_uuid));
177 namesize = sizeof(reisersb.s_volume_name);
178 *label = smalloc(namesize + 1);
179 sstrncpy(*label, reisersb.s_volume_name, namesize);
187 uuidcache_addentry(char *device, char *label, char *uuid)
189 struct uuidCache_s *last;
192 last = uuidCache = smalloc(sizeof(*uuidCache));
194 for(last = uuidCache; last->next; last = last->next);
195 last->next = smalloc(sizeof(*uuidCache));
199 last->device = device;
201 memcpy(last->uuid, uuid, sizeof(last->uuid));
210 static char ptname[100];
212 char uuid[16], *label = NULL;
221 procpt = fopen(PROC_PARTITIONS, "r");
226 for(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 ]",
230 &ma, &mi, &sz, ptname) != 4)
235 /* skip extended partitions (heuristic: size 1) */
240 /* look only at md devices on first pass */
241 handleOnFirst = !strncmp(ptname, "md", 2);
242 if(firstPass != handleOnFirst) {
246 /* skip entire disk (minor 0, 64, ... on ide;
248 /* heuristic: partition name ends in a digit */
250 for(s = ptname; *s; s++);
252 if(isdigit((int)s[-1])) {
254 * Note: this is a heuristic only - there is no reason
255 * why these devices should live in /dev.
256 * Perhaps this directory should be specifiable by option.
257 * One might for example have /devlabel with links to /dev
258 * for the devices that may be accessed in this way.
259 * (This is useful, if the cdrom on /dev/hdc must not
262 ssnprintf(device, sizeof(device), "%s/%s",
263 DEVLABELDIR, ptname);
264 if(!get_label_uuid(device, &label, uuid)) {
265 uuidcache_addentry(sstrdup(device),
277 if(isdigit((int)c)) {
279 } else if(islower((int)c)) {
280 return (c - 'a' + 10);
282 return (c - 'A' + 10);
287 get_spec_by_x(int n, const char *t)
289 struct uuidCache_s *uc;
297 if(!memcmp(t, uc->uuid, sizeof(uc->uuid))) {
298 return sstrdup(uc->device);
302 if(!strcmp(t, uc->label)) {
303 return sstrdup(uc->device);
313 get_spec_by_uuid(const char *s)
319 || s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-') {
323 for(i=0; i<16; i++) {
327 if(!isxdigit((int)s[0]) || !isxdigit((int)s[1])) {
330 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
333 return get_spec_by_x(UUID, uuid);
336 DEBUG("utils_mount: Found an invalid UUID: %s", s);
340 static char *get_spec_by_volume_label(const char *s)
342 return get_spec_by_x (VOL, s);
345 static char *get_device_name(const char *optstr)
353 else if (strncmp (optstr, "UUID=", 5) == 0)
355 DEBUG ("utils_mount: TODO: check UUID= code!");
356 rc = get_spec_by_uuid (optstr + 5);
358 else if (strncmp (optstr, "LABEL=", 6) == 0)
360 DEBUG ("utils_mount: TODO: check LABEL= code!");
361 rc = get_spec_by_volume_label (optstr + 6);
365 rc = sstrdup (optstr);
370 DEBUG ("utils_mount: Error checking device name: optstr = %s", optstr);
375 /* What weird OS is this..? I can't find any info with google :/ -octo */
376 #if HAVE_LISTMNTENT && 0
377 static cu_mount_t *cu_mount_listmntent (void)
379 cu_mount_t *last = *list;
383 struct tabmntent *mntlist;
384 if(listmntent(&mntlist, COLLECTD_MNTTAB, NULL, NULL) < 0) {
387 DEBUG("utils_mount: calling listmntent() failed: %s",
388 sstrerror (errno, errbuf, sizeof (errbuf)));
389 #endif /* COLLECT_DEBUG */
392 for(p = mntlist; p; p = p->next) {
393 char *loop = NULL, *device = NULL;
396 loop = cu_mount_getoptionvalue(mnt->mnt_opts, "loop=");
397 if(loop == NULL) { /* no loop= mount */
398 device = get_device_name(mnt->mnt_fsname);
400 DEBUG("utils_mount: can't get devicename for fs (%s) %s (%s)"
401 ": ignored", mnt->mnt_type,
402 mnt->mnt_dir, mnt->mnt_fsname);
409 *list = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
412 while(last->next != NULL) { /* is last really last? */
415 last->next = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
418 last->dir = sstrdup(mnt->mnt_dir);
419 last->spec_device = sstrdup(mnt->mnt_fsname);
420 last->device = device;
421 last->type = sstrdup(mnt->mnt_type);
422 last->options = sstrdup(mnt->mnt_opts);
424 } /* for(p = mntlist; p; p = p->next) */
427 } /* cu_mount_t *cu_mount_listmntent(void) */
428 /* #endif HAVE_LISTMNTENT */
430 /* 4.4BSD and Mac OS X (getfsstat) or NetBSD (getvfsstat) */
431 #elif HAVE_GETVFSSTAT || HAVE_GETFSSTAT
432 static cu_mount_t *cu_mount_getfsstat (void)
435 # define STRUCT_STATFS struct statfs
436 # define CMD_STATFS getfsstat
437 # define FLAGS_STATFS MNT_NOWAIT
438 /* #endif HAVE_GETFSSTAT */
439 #elif HAVE_GETVFSSTAT
440 # define STRUCT_STATFS struct statvfs
441 # define CMD_STATFS getvfsstat
442 # define FLAGS_STATFS ST_NOWAIT
443 #endif /* HAVE_GETVFSSTAT */
451 cu_mount_t *first = NULL;
452 cu_mount_t *last = NULL;
453 cu_mount_t *new = NULL;
455 /* Get the number of mounted file systems */
456 if ((bufsize = CMD_STATFS (NULL, 0, FLAGS_STATFS)) < 1)
460 DEBUG ("utils_mount: getv?fsstat failed: %s",
461 sstrerror (errno, errbuf, sizeof (errbuf)));
462 #endif /* COLLECT_DEBUG */
466 if ((buf = (STRUCT_STATFS *) malloc (bufsize * sizeof (STRUCT_STATFS)))
469 memset (buf, '\0', bufsize * sizeof (STRUCT_STATFS));
471 /* The bufsize needs to be passed in bytes. Really. This is not in the
473 if ((num = CMD_STATFS (buf, bufsize * sizeof (STRUCT_STATFS), FLAGS_STATFS)) < 1)
477 DEBUG ("utils_mount: getv?fsstat failed: %s",
478 sstrerror (errno, errbuf, sizeof (errbuf)));
479 #endif /* COLLECT_DEBUG */
484 for (i = 0; i < num; i++)
486 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
488 memset (new, '\0', sizeof (cu_mount_t));
490 /* Copy values from `struct mnttab' */
491 new->dir = sstrdup (buf[i].f_mntonname);
492 new->spec_device = sstrdup (buf[i].f_mntfromname);
493 new->type = sstrdup (buf[i].f_fstypename);
495 new->device = get_device_name (new->options);
515 /* #endif HAVE_GETVFSSTAT || HAVE_GETFSSTAT */
517 /* Solaris (SunOS 10): int getmntent(FILE *fp, struct mnttab *mp); */
518 #elif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT
519 static cu_mount_t *cu_mount_gen_getmntent (void)
524 cu_mount_t *first = NULL;
525 cu_mount_t *last = NULL;
526 cu_mount_t *new = NULL;
528 DEBUG ("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
530 if ((fp = fopen (COLLECTD_MNTTAB, "r")) == NULL)
533 ERROR ("fopen (%s): %s", COLLECTD_MNTTAB,
534 sstrerror (errno, errbuf, sizeof (errbuf)));
538 while (getmntent (fp, &mt) == 0)
540 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
542 memset (new, '\0', sizeof (cu_mount_t));
544 /* Copy values from `struct mnttab' */
545 new->dir = sstrdup (mt.mnt_mountp);
546 new->spec_device = sstrdup (mt.mnt_special);
547 new->type = sstrdup (mt.mnt_fstype);
548 new->options = sstrdup (mt.mnt_mntopts);
549 new->device = get_device_name (new->options);
568 } /* static cu_mount_t *cu_mount_gen_getmntent (void) */
569 /* #endif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT */
571 #elif HAVE_SEQ_GETMNTENT
572 #warn "This version of `getmntent' hat not yet been implemented!"
573 /* #endif HAVE_SEQ_GETMNTENT */
575 #elif HAVE_GETMNTENT_R
576 static cu_mount_t *cu_mount_getmntent (void)
582 cu_mount_t *first = NULL;
583 cu_mount_t *last = NULL;
584 cu_mount_t *new = NULL;
586 DEBUG ("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
588 if ((fp = setmntent (COLLECTD_MNTTAB, "r")) == NULL)
591 ERROR ("setmntent (%s): %s", COLLECTD_MNTTAB,
592 sstrerror (errno, errbuf, sizeof (errbuf)));
596 while (getmntent_r (fp, &me, mntbuf, sizeof (mntbuf) ))
598 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
600 memset (new, '\0', sizeof (cu_mount_t));
602 /* Copy values from `struct mntent *' */
603 new->dir = sstrdup (me.mnt_dir);
604 new->spec_device = sstrdup (me.mnt_fsname);
605 new->type = sstrdup (me.mnt_type);
606 new->options = sstrdup (me.mnt_opts);
607 new->device = get_device_name (new->options);
610 DEBUG ("utils_mount: new = {dir = %s, spec_device = %s, type = %s, options = %s, device = %s}",
611 new->dir, new->spec_device, new->type, new->options, new->device);
628 DEBUG ("utils_mount: return (0x%p)", (void *) first);
631 } /* HAVE_GETMNTENT_R */
633 #elif HAVE_ONE_GETMNTENT
634 static cu_mount_t *cu_mount_getmntent (void)
639 cu_mount_t *first = NULL;
640 cu_mount_t *last = NULL;
641 cu_mount_t *new = NULL;
643 DEBUG ("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
645 if ((fp = setmntent (COLLECTD_MNTTAB, "r")) == NULL)
648 ERROR ("setmntent (%s): %s", COLLECTD_MNTTAB,
649 sstrerror (errno, errbuf, sizeof (errbuf)));
653 while ((me = getmntent (fp)) != NULL)
655 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
657 memset (new, '\0', sizeof (cu_mount_t));
659 /* Copy values from `struct mntent *' */
660 new->dir = sstrdup (me->mnt_dir);
661 new->spec_device = sstrdup (me->mnt_fsname);
662 new->type = sstrdup (me->mnt_type);
663 new->options = sstrdup (me->mnt_opts);
664 new->device = get_device_name (new->options);
667 DEBUG ("utils_mount: new = {dir = %s, spec_device = %s, type = %s, options = %s, device = %s}",
668 new->dir, new->spec_device, new->type, new->options, new->device);
685 DEBUG ("utils_mount: return (0x%p)", (void *) first);
689 #endif /* HAVE_ONE_GETMNTENT */
691 /* *** *** *** ******************************************** *** *** *** */
692 /* *** *** *** *** *** *** public functions *** *** *** *** *** *** */
693 /* *** *** *** ******************************************** *** *** *** */
695 cu_mount_t *cu_mount_getlist(cu_mount_t **list)
698 cu_mount_t *first = NULL;
699 cu_mount_t *last = NULL;
708 while (last->next != NULL)
712 #if HAVE_LISTMNTENT && 0
713 new = cu_mount_listmntent ();
714 #elif HAVE_GETVFSSTAT || HAVE_GETFSSTAT
715 new = cu_mount_getfsstat ();
716 #elif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT
717 new = cu_mount_gen_getmntent ();
718 #elif HAVE_SEQ_GETMNTENT
719 # error "This version of `getmntent' hat not yet been implemented!"
720 #elif HAVE_ONE_GETMNTENT
721 new = cu_mount_getmntent ();
723 # error "Could not determine how to find mountpoints."
737 while ((last != NULL) && (last->next != NULL))
741 } /* cu_mount_t *cu_mount_getlist(cu_mount_t **list) */
743 void cu_mount_freelist (cu_mount_t *list)
748 for (this = list; this != NULL; this = next)
753 sfree (this->spec_device);
754 sfree (this->device);
756 sfree (this->options);
759 } /* void cu_mount_freelist(cu_mount_t *list) */
762 cu_mount_checkoption(char *line, char *keyword, int full)
764 char *line2, *l2, *p1, *p2;
767 if(line == NULL || keyword == NULL) {
775 line2 = sstrdup(line);
786 p2 = strchr(line, ',');
788 if(strncmp(line2+(p1-line)+1, keyword, l+full) == 0) {
794 p2 = strchr(p1+1, ',');
800 } /* char *cu_mount_checkoption(char *line, char *keyword, int full) */
803 cu_mount_getoptionvalue(char *line, char *keyword)
807 r = cu_mount_checkoption(line, keyword, 0);
810 r += strlen(keyword);
819 m = (char *)smalloc(p-r+1);
820 sstrncpy(m, r, p-r+1);
825 } /* char *cu_mount_getoptionvalue(char *line, char *keyword) */
828 cu_mount_type(const char *type)
830 if(strcmp(type, "ext3") == 0) return CUMT_EXT3;
831 if(strcmp(type, "ext2") == 0) return CUMT_EXT2;
832 if(strcmp(type, "ufs") == 0) return CUMT_UFS;
833 if(strcmp(type, "vxfs") == 0) return CUMT_VXFS;
834 if(strcmp(type, "zfs") == 0) return CUMT_ZFS;
836 } /* int cu_mount_type(const char *type) */