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"
37 #include "common.h" /* sstrncpy() et alii */
38 #include "plugin.h" /* ERROR() macro */
43 # include <sys/types.h>
45 # if HAVE_SYS_STATVFS_H
46 # include <sys/statvfs.h>
48 /* #endif HAVE_GETVFSSTAT */
52 # include <sys/param.h>
55 # include <sys/ucred.h>
58 # include <sys/mount.h>
60 #endif /* HAVE_GETFSSTAT */
66 # include <sys/mnttab.h>
73 #ifdef COLLECTD_MNTTAB
74 # undef COLLECTD_MNTTAB
77 #if defined(_PATH_MOUNTED) /* glibc */
78 # define COLLECTD_MNTTAB _PATH_MOUNTED
79 #elif defined(MNTTAB) /* Solaris */
80 # define COLLECTD_MNTTAB MNTTAB
81 #elif defined(MNT_MNTTAB)
82 # define COLLECTD_MNTTAB MNT_MNTTAB
83 #elif defined(MNTTABNAME)
84 # define COLLECTD_MNTTAB MNTTABNAME
86 # define COLLECTD_MNTTAB KMTAB
88 # define COLLECTD_MNTTAB "/etc/mnttab"
91 /* *** *** *** ********************************************* *** *** *** */
92 /* *** *** *** *** *** *** private functions *** *** *** *** *** *** */
93 /* *** *** *** ********************************************* *** *** *** */
95 /* stolen from quota-3.13 (quota-tools) */
97 #define PROC_PARTITIONS "/proc/partitions"
98 #define DEVLABELDIR "/dev"
102 static struct uuidCache_s {
103 struct uuidCache_s *next;
109 #define EXT2_SUPER_MAGIC 0xEF53
110 struct ext2_super_block {
111 unsigned char s_dummy1[56];
112 unsigned char s_magic[2];
113 unsigned char s_dummy2[46];
114 unsigned char s_uuid[16];
115 char s_volume_name[16];
117 #define ext2magic(s) ((unsigned int)s.s_magic[0] \
118 + (((unsigned int)s.s_magic[1]) << 8))
121 struct xfs_super_block {
122 unsigned char s_magic[4];
123 unsigned char s_dummy[28];
124 unsigned char s_uuid[16];
125 unsigned char s_dummy2[60];
128 #endif /* HAVE_XFS_XQM_H */
130 #define REISER_SUPER_MAGIC "ReIsEr2Fs"
131 struct reiserfs_super_block {
132 unsigned char s_dummy1[52];
133 unsigned char s_magic[10];
134 unsigned char s_dummy2[22];
135 unsigned char s_uuid[16];
136 char s_volume_name[16];
139 /* for now, only ext2 and xfs are supported */
141 get_label_uuid(const char *device, char **label, char *uuid)
143 /* start with ext2 and xfs tests, taken from mount_guess_fstype */
144 /* should merge these later */
147 struct ext2_super_block e2sb;
149 struct xfs_super_block xfsb;
151 struct reiserfs_super_block reisersb;
153 fd = open(device, O_RDONLY);
158 if(lseek(fd, 1024, SEEK_SET) == 1024
159 && read(fd, (char *)&e2sb, sizeof(e2sb)) == sizeof(e2sb)
160 && ext2magic(e2sb) == EXT2_SUPER_MAGIC) {
161 memcpy(uuid, e2sb.s_uuid, sizeof(e2sb.s_uuid));
162 namesize = sizeof(e2sb.s_volume_name);
163 *label = smalloc(namesize + 1);
164 sstrncpy(*label, e2sb.s_volume_name, namesize);
167 } else if(lseek(fd, 0, SEEK_SET) == 0
168 && read(fd, (char *)&xfsb, sizeof(xfsb)) == sizeof(xfsb)
169 && (strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC_STR, 4) == 0 ||
170 strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC2_STR, 4) == 0)) {
171 memcpy(uuid, xfsb.s_uuid, sizeof(xfsb.s_uuid));
172 namesize = sizeof(xfsb.s_fsname);
173 *label = smalloc(namesize + 1);
174 sstrncpy(*label, xfsb.s_fsname, namesize);
176 #endif /* HAVE_XFS_XQM_H */
177 } else if(lseek(fd, 65536, SEEK_SET) == 65536
178 && read(fd, (char *)&reisersb, sizeof(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);
191 uuidcache_addentry(char *device, char *label, char *uuid)
193 struct uuidCache_s *last;
196 last = uuidCache = smalloc(sizeof(*uuidCache));
198 for(last = uuidCache; last->next; last = last->next);
199 last->next = smalloc(sizeof(*uuidCache));
203 last->device = device;
205 memcpy(last->uuid, uuid, sizeof(last->uuid));
214 static char ptname[100];
216 char uuid[16], *label = NULL;
225 procpt = fopen(PROC_PARTITIONS, "r");
230 for(firstPass = 1; firstPass >= 0; firstPass--) {
231 fseek(procpt, 0, SEEK_SET);
232 while(fgets(line, sizeof(line), procpt)) {
233 if(sscanf(line, " %d %d %d %[^\n ]",
234 &ma, &mi, &sz, ptname) != 4)
239 /* skip extended partitions (heuristic: size 1) */
244 /* look only at md devices on first pass */
245 handleOnFirst = !strncmp(ptname, "md", 2);
246 if(firstPass != handleOnFirst) {
250 /* skip entire disk (minor 0, 64, ... on ide;
252 /* heuristic: partition name ends in a digit */
254 for(s = ptname; *s; s++);
256 if(isdigit((int)s[-1])) {
258 * Note: this is a heuristic only - there is no reason
259 * why these devices should live in /dev.
260 * Perhaps this directory should be specifiable by option.
261 * One might for example have /devlabel with links to /dev
262 * for the devices that may be accessed in this way.
263 * (This is useful, if the cdrom on /dev/hdc must not
266 ssnprintf(device, sizeof(device), "%s/%s",
267 DEVLABELDIR, ptname);
268 if(!get_label_uuid(device, &label, uuid)) {
269 uuidcache_addentry(sstrdup(device),
281 if(isdigit((int)c)) {
283 } else if(islower((int)c)) {
284 return (c - 'a' + 10);
286 return (c - 'A' + 10);
291 get_spec_by_x(int n, const char *t)
293 struct uuidCache_s *uc;
301 if(!memcmp(t, uc->uuid, sizeof(uc->uuid))) {
302 return sstrdup(uc->device);
306 if(!strcmp(t, uc->label)) {
307 return sstrdup(uc->device);
317 get_spec_by_uuid(const char *s)
323 || s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-') {
327 for(i=0; i<16; i++) {
331 if(!isxdigit((int)s[0]) || !isxdigit((int)s[1])) {
334 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
337 return get_spec_by_x(UUID, uuid);
340 DEBUG("utils_mount: Found an invalid UUID: %s", s);
344 static char *get_spec_by_volume_label(const char *s)
346 return get_spec_by_x (VOL, s);
349 static char *get_device_name(const char *optstr)
357 else if (strncmp (optstr, "UUID=", 5) == 0)
359 DEBUG ("utils_mount: TODO: check UUID= code!");
360 rc = get_spec_by_uuid (optstr + 5);
362 else if (strncmp (optstr, "LABEL=", 6) == 0)
364 DEBUG ("utils_mount: TODO: check LABEL= code!");
365 rc = get_spec_by_volume_label (optstr + 6);
369 rc = sstrdup (optstr);
374 DEBUG ("utils_mount: Error checking device name: optstr = %s", optstr);
379 /* What weird OS is this..? I can't find any info with google :/ -octo */
380 #if HAVE_LISTMNTENT && 0
381 static cu_mount_t *cu_mount_listmntent (void)
383 cu_mount_t *last = *list;
387 struct tabmntent *mntlist;
388 if(listmntent(&mntlist, COLLECTD_MNTTAB, NULL, NULL) < 0) {
391 DEBUG("utils_mount: calling listmntent() failed: %s",
392 sstrerror (errno, errbuf, sizeof (errbuf)));
393 #endif /* COLLECT_DEBUG */
396 for(p = mntlist; p; p = p->next) {
397 char *loop = NULL, *device = NULL;
400 loop = cu_mount_getoptionvalue(mnt->mnt_opts, "loop=");
401 if(loop == NULL) { /* no loop= mount */
402 device = get_device_name(mnt->mnt_fsname);
404 DEBUG("utils_mount: can't get devicename for fs (%s) %s (%s)"
405 ": ignored", mnt->mnt_type,
406 mnt->mnt_dir, mnt->mnt_fsname);
413 *list = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
416 while(last->next != NULL) { /* is last really last? */
419 last->next = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
422 last->dir = sstrdup(mnt->mnt_dir);
423 last->spec_device = sstrdup(mnt->mnt_fsname);
424 last->device = device;
425 last->type = sstrdup(mnt->mnt_type);
426 last->options = sstrdup(mnt->mnt_opts);
428 } /* for(p = mntlist; p; p = p->next) */
431 } /* cu_mount_t *cu_mount_listmntent(void) */
432 /* #endif HAVE_LISTMNTENT */
434 /* 4.4BSD and Mac OS X (getfsstat) or NetBSD (getvfsstat) */
435 #elif HAVE_GETVFSSTAT || HAVE_GETFSSTAT
436 static cu_mount_t *cu_mount_getfsstat (void)
439 # define STRUCT_STATFS struct statfs
440 # define CMD_STATFS getfsstat
441 # define FLAGS_STATFS MNT_NOWAIT
442 /* #endif HAVE_GETFSSTAT */
443 #elif HAVE_GETVFSSTAT
444 # define STRUCT_STATFS struct statvfs
445 # define CMD_STATFS getvfsstat
446 # define FLAGS_STATFS ST_NOWAIT
447 #endif /* HAVE_GETVFSSTAT */
455 cu_mount_t *first = NULL;
456 cu_mount_t *last = NULL;
457 cu_mount_t *new = NULL;
459 /* Get the number of mounted file systems */
460 if ((bufsize = CMD_STATFS (NULL, 0, FLAGS_STATFS)) < 1)
464 DEBUG ("utils_mount: getv?fsstat failed: %s",
465 sstrerror (errno, errbuf, sizeof (errbuf)));
466 #endif /* COLLECT_DEBUG */
470 if ((buf = malloc (bufsize * sizeof (*buf))) == NULL)
472 memset (buf, '\0', bufsize * sizeof (STRUCT_STATFS));
474 /* The bufsize needs to be passed in bytes. Really. This is not in the
476 if ((num = CMD_STATFS (buf, bufsize * sizeof (STRUCT_STATFS), FLAGS_STATFS)) < 1)
480 DEBUG ("utils_mount: getv?fsstat failed: %s",
481 sstrerror (errno, errbuf, sizeof (errbuf)));
482 #endif /* COLLECT_DEBUG */
487 for (i = 0; i < num; i++)
489 if ((new = malloc (sizeof (*new))) == NULL)
491 memset (new, '\0', sizeof (cu_mount_t));
493 /* Copy values from `struct mnttab' */
494 new->dir = sstrdup (buf[i].f_mntonname);
495 new->spec_device = sstrdup (buf[i].f_mntfromname);
496 new->type = sstrdup (buf[i].f_fstypename);
498 new->device = get_device_name (new->options);
518 /* #endif HAVE_GETVFSSTAT || HAVE_GETFSSTAT */
520 /* Solaris (SunOS 10): int getmntent(FILE *fp, struct mnttab *mp); */
521 #elif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT
522 static cu_mount_t *cu_mount_gen_getmntent (void)
527 cu_mount_t *first = NULL;
528 cu_mount_t *last = NULL;
529 cu_mount_t *new = NULL;
531 DEBUG ("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
533 if ((fp = fopen (COLLECTD_MNTTAB, "r")) == NULL)
536 ERROR ("fopen (%s): %s", COLLECTD_MNTTAB,
537 sstrerror (errno, errbuf, sizeof (errbuf)));
541 while (getmntent (fp, &mt) == 0)
543 if ((new = malloc (sizeof (*new))) == NULL)
545 memset (new, '\0', sizeof (cu_mount_t));
547 /* Copy values from `struct mnttab' */
548 new->dir = sstrdup (mt.mnt_mountp);
549 new->spec_device = sstrdup (mt.mnt_special);
550 new->type = sstrdup (mt.mnt_fstype);
551 new->options = sstrdup (mt.mnt_mntopts);
552 new->device = get_device_name (new->options);
571 } /* static cu_mount_t *cu_mount_gen_getmntent (void) */
572 /* #endif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT */
574 #elif HAVE_SEQ_GETMNTENT
575 #warn "This version of `getmntent' hat not yet been implemented!"
576 /* #endif HAVE_SEQ_GETMNTENT */
578 #elif HAVE_GETMNTENT_R
579 static cu_mount_t *cu_mount_getmntent (void)
585 cu_mount_t *first = NULL;
586 cu_mount_t *last = NULL;
587 cu_mount_t *new = NULL;
589 DEBUG ("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
591 if ((fp = setmntent (COLLECTD_MNTTAB, "r")) == NULL)
594 ERROR ("setmntent (%s): %s", COLLECTD_MNTTAB,
595 sstrerror (errno, errbuf, sizeof (errbuf)));
599 while (getmntent_r (fp, &me, mntbuf, sizeof (mntbuf) ))
601 if ((new = malloc (sizeof (*new))) == NULL)
603 memset (new, '\0', sizeof (cu_mount_t));
605 /* Copy values from `struct mntent *' */
606 new->dir = sstrdup (me.mnt_dir);
607 new->spec_device = sstrdup (me.mnt_fsname);
608 new->type = sstrdup (me.mnt_type);
609 new->options = sstrdup (me.mnt_opts);
610 new->device = get_device_name (new->options);
613 DEBUG ("utils_mount: new = {dir = %s, spec_device = %s, type = %s, options = %s, device = %s}",
614 new->dir, new->spec_device, new->type, new->options, new->device);
631 DEBUG ("utils_mount: return (0x%p)", (void *) first);
634 } /* HAVE_GETMNTENT_R */
636 #elif HAVE_ONE_GETMNTENT
637 static cu_mount_t *cu_mount_getmntent (void)
642 cu_mount_t *first = NULL;
643 cu_mount_t *last = NULL;
644 cu_mount_t *new = NULL;
646 DEBUG ("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
648 if ((fp = setmntent (COLLECTD_MNTTAB, "r")) == NULL)
651 ERROR ("setmntent (%s): %s", COLLECTD_MNTTAB,
652 sstrerror (errno, errbuf, sizeof (errbuf)));
656 while ((me = getmntent (fp)) != NULL)
658 if ((new = malloc (sizeof (*new))) == NULL)
660 memset (new, '\0', sizeof (cu_mount_t));
662 /* Copy values from `struct mntent *' */
663 new->dir = sstrdup (me->mnt_dir);
664 new->spec_device = sstrdup (me->mnt_fsname);
665 new->type = sstrdup (me->mnt_type);
666 new->options = sstrdup (me->mnt_opts);
667 new->device = get_device_name (new->options);
670 DEBUG ("utils_mount: new = {dir = %s, spec_device = %s, type = %s, options = %s, device = %s}",
671 new->dir, new->spec_device, new->type, new->options, new->device);
688 DEBUG ("utils_mount: return (0x%p)", (void *) first);
692 #endif /* HAVE_ONE_GETMNTENT */
694 /* *** *** *** ******************************************** *** *** *** */
695 /* *** *** *** *** *** *** public functions *** *** *** *** *** *** */
696 /* *** *** *** ******************************************** *** *** *** */
698 cu_mount_t *cu_mount_getlist(cu_mount_t **list)
701 cu_mount_t *first = NULL;
702 cu_mount_t *last = NULL;
711 while (last->next != NULL)
715 #if HAVE_LISTMNTENT && 0
716 new = cu_mount_listmntent ();
717 #elif HAVE_GETVFSSTAT || HAVE_GETFSSTAT
718 new = cu_mount_getfsstat ();
719 #elif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT
720 new = cu_mount_gen_getmntent ();
721 #elif HAVE_SEQ_GETMNTENT
722 # error "This version of `getmntent' hat not yet been implemented!"
723 #elif HAVE_ONE_GETMNTENT
724 new = cu_mount_getmntent ();
726 # error "Could not determine how to find mountpoints."
740 while ((last != NULL) && (last->next != NULL))
744 } /* cu_mount_t *cu_mount_getlist(cu_mount_t **list) */
746 void cu_mount_freelist (cu_mount_t *list)
751 for (this = list; this != NULL; this = next)
756 sfree (this->spec_device);
757 sfree (this->device);
759 sfree (this->options);
762 } /* void cu_mount_freelist(cu_mount_t *list) */
765 cu_mount_checkoption(char *line, const char *keyword, int full)
767 char *line2, *l2, *p1, *p2;
770 if(line == NULL || keyword == NULL) {
778 line2 = sstrdup(line);
789 p2 = strchr(line, ',');
791 if(strncmp(line2+(p1-line)+1, keyword, l+full) == 0) {
797 p2 = strchr(p1+1, ',');
803 } /* char *cu_mount_checkoption(char *line, char *keyword, int full) */
806 cu_mount_getoptionvalue(char *line, const char *keyword)
810 r = cu_mount_checkoption(line, keyword, 0);
813 r += strlen(keyword);
823 sstrncpy(m, r, p-r+1);
828 } /* char *cu_mount_getoptionvalue(char *line, const char *keyword) */
831 cu_mount_type(const char *type)
833 if(strcmp(type, "ext3") == 0) return CUMT_EXT3;
834 if(strcmp(type, "ext2") == 0) return CUMT_EXT2;
835 if(strcmp(type, "ufs") == 0) return CUMT_UFS;
836 if(strcmp(type, "vxfs") == 0) return CUMT_VXFS;
837 if(strcmp(type, "zfs") == 0) return CUMT_ZFS;
839 } /* int cu_mount_type(const char *type) */