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
16 * Licence along with this program; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
21 * Niki W. Waibel <niki.waibel@gmx.net>
29 #define XFS_SUPER_MAGIC_STR "XFSB"
30 #define XFS_SUPER_MAGIC2_STR "BSFX"
34 #include "utils_mount.h"
38 # include <sys/types.h>
40 # if HAVE_SYS_STATVFS_H
41 # include <sys/statvfs.h>
43 /* #endif HAVE_GETVFSSTAT */
47 # include <sys/param.h>
50 # include <sys/ucred.h>
53 # include <sys/mount.h>
55 #endif /* HAVE_GETFSSTAT */
61 # include <sys/mnttab.h>
68 #ifdef COLLECTD_MNTTAB
69 # undef COLLECTD_MNTTAB
72 #if defined(_PATH_MOUNTED) /* glibc */
73 # define COLLECTD_MNTTAB _PATH_MOUNTED
74 #elif defined(MNTTAB) /* Solaris */
75 # define COLLECTD_MNTTAB MNTTAB
76 #elif defined(MNT_MNTTAB)
77 # define COLLECTD_MNTTAB MNT_MNTTAB
78 #elif defined(MNTTABNAME)
79 # define COLLECTD_MNTTAB MNTTABNAME
81 # define COLLECTD_MNTTAB KMTAB
83 # define COLLECTD_MNTTAB "/etc/mnttab"
86 /* *** *** *** ********************************************* *** *** *** */
87 /* *** *** *** *** *** *** private functions *** *** *** *** *** *** */
88 /* *** *** *** ********************************************* *** *** *** */
90 /* stolen from quota-3.13 (quota-tools) */
92 #define PROC_PARTITIONS "/proc/partitions"
93 #define DEVLABELDIR "/dev"
97 static struct uuidCache_s {
98 struct uuidCache_s *next;
104 #define EXT2_SUPER_MAGIC 0xEF53
105 struct ext2_super_block {
106 unsigned char s_dummy1[56];
107 unsigned char s_magic[2];
108 unsigned char s_dummy2[46];
109 unsigned char s_uuid[16];
110 char s_volume_name[16];
112 #define ext2magic(s) ((unsigned int)s.s_magic[0] \
113 + (((unsigned int)s.s_magic[1]) << 8))
116 struct xfs_super_block {
117 unsigned char s_magic[4];
118 unsigned char s_dummy[28];
119 unsigned char s_uuid[16];
120 unsigned char s_dummy2[60];
123 #endif /* HAVE_XFS_XQM_H */
125 #define REISER_SUPER_MAGIC "ReIsEr2Fs"
126 struct reiserfs_super_block {
127 unsigned char s_dummy1[52];
128 unsigned char s_magic[10];
129 unsigned char s_dummy2[22];
130 unsigned char s_uuid[16];
131 char s_volume_name[16];
134 /* for now, only ext2 and xfs are supported */
136 get_label_uuid(const char *device, char **label, char *uuid)
138 /* start with ext2 and xfs tests, taken from mount_guess_fstype */
139 /* should merge these later */
142 struct ext2_super_block e2sb;
144 struct xfs_super_block xfsb;
146 struct reiserfs_super_block reisersb;
148 fd = open(device, O_RDONLY);
153 if(lseek(fd, 1024, SEEK_SET) == 1024
154 && read(fd, (char *)&e2sb, sizeof(e2sb)) == sizeof(e2sb)
155 && ext2magic(e2sb) == EXT2_SUPER_MAGIC) {
156 memcpy(uuid, e2sb.s_uuid, sizeof(e2sb.s_uuid));
157 namesize = sizeof(e2sb.s_volume_name);
158 *label = smalloc(namesize + 1);
159 sstrncpy(*label, e2sb.s_volume_name, namesize);
162 } else if(lseek(fd, 0, SEEK_SET) == 0
163 && read(fd, (char *)&xfsb, sizeof(xfsb)) == sizeof(xfsb)
164 && (strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC_STR, 4) == 0 ||
165 strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC2_STR, 4) == 0)) {
166 memcpy(uuid, xfsb.s_uuid, sizeof(xfsb.s_uuid));
167 namesize = sizeof(xfsb.s_fsname);
168 *label = smalloc(namesize + 1);
169 sstrncpy(*label, xfsb.s_fsname, namesize);
171 #endif /* HAVE_XFS_XQM_H */
172 } else if(lseek(fd, 65536, SEEK_SET) == 65536
173 && read(fd, (char *)&reisersb, sizeof(reisersb)) == sizeof(reisersb)
174 && !strncmp((char *)&reisersb.s_magic, REISER_SUPER_MAGIC, 9)) {
175 memcpy(uuid, reisersb.s_uuid, sizeof(reisersb.s_uuid));
176 namesize = sizeof(reisersb.s_volume_name);
177 *label = smalloc(namesize + 1);
178 sstrncpy(*label, reisersb.s_volume_name, namesize);
186 uuidcache_addentry(char *device, char *label, char *uuid)
188 struct uuidCache_s *last;
191 last = uuidCache = smalloc(sizeof(*uuidCache));
193 for(last = uuidCache; last->next; last = last->next);
194 last->next = smalloc(sizeof(*uuidCache));
198 last->device = device;
200 memcpy(last->uuid, uuid, sizeof(last->uuid));
209 static char ptname[100];
211 char uuid[16], *label = NULL;
220 procpt = fopen(PROC_PARTITIONS, "r");
225 for(firstPass = 1; firstPass >= 0; firstPass--) {
226 fseek(procpt, 0, SEEK_SET);
227 while(fgets(line, sizeof(line), procpt)) {
228 if(sscanf(line, " %d %d %d %[^\n ]",
229 &ma, &mi, &sz, ptname) != 4)
234 /* skip extended partitions (heuristic: size 1) */
239 /* look only at md devices on first pass */
240 handleOnFirst = !strncmp(ptname, "md", 2);
241 if(firstPass != handleOnFirst) {
245 /* skip entire disk (minor 0, 64, ... on ide;
247 /* heuristic: partition name ends in a digit */
249 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",
262 DEVLABELDIR, ptname);
263 if(!get_label_uuid(device, &label, uuid)) {
264 uuidcache_addentry(sstrdup(device),
276 if(isdigit((int)c)) {
278 } else if(islower((int)c)) {
279 return (c - 'a' + 10);
281 return (c - 'A' + 10);
286 get_spec_by_x(int n, const char *t)
288 struct uuidCache_s *uc;
296 if(!memcmp(t, uc->uuid, sizeof(uc->uuid))) {
297 return sstrdup(uc->device);
301 if(!strcmp(t, uc->label)) {
302 return sstrdup(uc->device);
312 get_spec_by_uuid(const char *s)
318 || s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-') {
322 for(i=0; i<16; i++) {
326 if(!isxdigit((int)s[0]) || !isxdigit((int)s[1])) {
329 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
332 return get_spec_by_x(UUID, uuid);
335 DEBUG("Found an invalid UUID: %s", s);
339 static char *get_spec_by_volume_label(const char *s)
341 return get_spec_by_x (VOL, s);
344 static char *get_device_name(const char *optstr)
352 else if (strncmp (optstr, "UUID=", 5) == 0)
354 DEBUG ("TODO: check UUID= code!");
355 rc = get_spec_by_uuid (optstr + 5);
357 else if (strncmp (optstr, "LABEL=", 6) == 0)
359 DEBUG ("TODO: check LABEL= code!");
360 rc = get_spec_by_volume_label (optstr + 6);
364 rc = sstrdup (optstr);
369 DEBUG ("Error checking device name: optstr = %s", optstr);
374 /* What weird OS is this..? I can't find any info with google :/ -octo */
375 #if HAVE_LISTMNTENT && 0
376 static cu_mount_t *cu_mount_listmntent (void)
378 cu_mount_t *last = *list;
382 struct tabmntent *mntlist;
383 if(listmntent(&mntlist, COLLECTD_MNTTAB, NULL, NULL) < 0) {
385 DEBUG("calling listmntent() failed: %s",
386 sstrerror (errno, errbuf, sizeof (errbuf)));
389 for(p = mntlist; p; p = p->next) {
390 char *loop = NULL, *device = NULL;
393 loop = cu_mount_getoptionvalue(mnt->mnt_opts, "loop=");
394 if(loop == NULL) { /* no loop= mount */
395 device = get_device_name(mnt->mnt_fsname);
397 DEBUG("can't get devicename for fs (%s) %s (%s)"
398 ": ignored", mnt->mnt_type,
399 mnt->mnt_dir, mnt->mnt_fsname);
406 *list = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
409 while(last->next != NULL) { /* is last really last? */
412 last->next = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
415 last->dir = sstrdup(mnt->mnt_dir);
416 last->spec_device = sstrdup(mnt->mnt_fsname);
417 last->device = device;
418 last->type = sstrdup(mnt->mnt_type);
419 last->options = sstrdup(mnt->mnt_opts);
421 } /* for(p = mntlist; p; p = p->next) */
424 } /* cu_mount_t *cu_mount_listmntent(void) */
425 /* #endif HAVE_LISTMNTENT */
427 /* 4.4BSD and Mac OS X (getfsstat) or NetBSD (getvfsstat) */
428 #elif HAVE_GETVFSSTAT || HAVE_GETFSSTAT
429 static cu_mount_t *cu_mount_getfsstat (void)
432 # define STRUCT_STATFS struct statvfs
433 # define CMD_STATFS getvfsstat
434 # define FLAGS_STATFS ST_NOWAIT
435 /* #endif HAVE_GETVFSSTAT */
437 # define STRUCT_STATFS struct statfs
438 # define CMD_STATFS getfsstat
439 # define FLAGS_STATFS MNT_NOWAIT
440 #endif /* HAVE_GETFSSTAT */
448 cu_mount_t *first = NULL;
449 cu_mount_t *last = NULL;
450 cu_mount_t *new = NULL;
452 /* Get the number of mounted file systems */
453 if ((bufsize = CMD_STATFS (NULL, 0, FLAGS_STATFS)) < 1)
456 DEBUG ("getv?fsstat failed: %s",
457 sstrerror (errno, errbuf, sizeof (errbuf)));
461 if ((buf = (STRUCT_STATFS *) malloc (bufsize * sizeof (STRUCT_STATFS)))
464 memset (buf, '\0', bufsize * sizeof (STRUCT_STATFS));
466 /* The bufsize needs to be passed in bytes. Really. This is not in the
468 if ((num = CMD_STATFS (buf, bufsize * sizeof (STRUCT_STATFS), FLAGS_STATFS)) < 1)
471 DEBUG ("getv?fsstat failed: %s",
472 sstrerror (errno, errbuf, sizeof (errbuf)));
477 for (i = 0; i < num; i++)
479 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
481 memset (new, '\0', sizeof (cu_mount_t));
483 /* Copy values from `struct mnttab' */
484 new->dir = sstrdup (buf[i].f_mntonname);
485 new->spec_device = sstrdup (buf[i].f_mntfromname);
486 new->type = sstrdup (buf[i].f_fstypename);
488 new->device = get_device_name (new->options);
508 /* #endif HAVE_GETVFSSTAT || HAVE_GETFSSTAT */
510 /* Solaris (SunOS 10): int getmntent(FILE *fp, struct mnttab *mp); */
511 #elif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT
512 static cu_mount_t *cu_mount_gen_getmntent (void)
517 cu_mount_t *first = NULL;
518 cu_mount_t *last = NULL;
519 cu_mount_t *new = NULL;
521 DEBUG ("(void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
523 if ((fp = fopen (COLLECTD_MNTTAB, "r")) == NULL)
526 ERROR ("fopen (%s): %s", COLLECTD_MNTTAB,
527 sstrerror (errno, errbuf, sizeof (errbuf)));
531 while (getmntent (fp, &mt) == 0)
533 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
535 memset (new, '\0', sizeof (cu_mount_t));
537 /* Copy values from `struct mnttab' */
538 new->dir = sstrdup (mt.mnt_mountp);
539 new->spec_device = sstrdup (mt.mnt_special);
540 new->type = sstrdup (mt.mnt_fstype);
541 new->options = sstrdup (mt.mnt_mntopts);
542 new->device = get_device_name (new->options);
561 } /* static cu_mount_t *cu_mount_gen_getmntent (void) */
562 /* #endif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT */
564 #elif HAVE_SEQ_GETMNTENT
565 #warn "This version of `getmntent' hat not yet been implemented!"
566 /* #endif HAVE_SEQ_GETMNTENT */
568 #elif HAVE_ONE_GETMNTENT
569 static cu_mount_t *cu_mount_getmntent (void)
574 cu_mount_t *first = NULL;
575 cu_mount_t *last = NULL;
576 cu_mount_t *new = NULL;
578 DEBUG ("(void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
580 if ((fp = setmntent (COLLECTD_MNTTAB, "r")) == NULL)
583 ERROR ("setmntent (%s): %s", COLLECTD_MNTTAB,
584 sstrerror (errno, errbuf, sizeof (errbuf)));
588 while ((me = getmntent (fp)) != NULL)
590 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
592 memset (new, '\0', sizeof (cu_mount_t));
594 /* Copy values from `struct mntent *' */
595 new->dir = sstrdup (me->mnt_dir);
596 new->spec_device = sstrdup (me->mnt_fsname);
597 new->type = sstrdup (me->mnt_type);
598 new->options = sstrdup (me->mnt_opts);
599 new->device = get_device_name (new->options);
602 DEBUG ("new = {dir = %s, spec_device = %s, type = %s, options = %s, device = %s}",
603 new->dir, new->spec_device, new->type, new->options, new->device);
620 DEBUG ("return (0x%p)", (void *) first);
624 #endif /* HAVE_ONE_GETMNTENT */
626 /* *** *** *** ******************************************** *** *** *** */
627 /* *** *** *** *** *** *** public functions *** *** *** *** *** *** */
628 /* *** *** *** ******************************************** *** *** *** */
630 cu_mount_t *cu_mount_getlist(cu_mount_t **list)
633 cu_mount_t *first = NULL;
634 cu_mount_t *last = NULL;
643 while (last->next != NULL)
647 #if HAVE_LISTMNTENT && 0
648 new = cu_mount_listmntent ();
649 #elif HAVE_GETVFSSTAT || HAVE_GETFSSTAT
650 new = cu_mount_getfsstat ();
651 #elif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT
652 new = cu_mount_gen_getmntent ();
653 #elif HAVE_SEQ_GETMNTENT
654 # warn "This version of `getmntent' hat not yet been implemented!"
655 #elif HAVE_ONE_GETMNTENT
656 new = cu_mount_getmntent ();
672 while ((last != NULL) && (last->next != NULL))
676 } /* cu_mount_t *cu_mount_getlist(cu_mount_t **list) */
678 void cu_mount_freelist (cu_mount_t *list)
683 DEBUG ("(list = 0x%p)", (void *) list);
685 for (this = list; this != NULL; this = next)
690 sfree (this->spec_device);
691 sfree (this->device);
693 sfree (this->options);
696 } /* void cu_mount_freelist(cu_mount_t *list) */
699 cu_mount_checkoption(char *line, char *keyword, int full)
702 int l = strlen(keyword);
705 if(line == NULL || keyword == NULL) {
712 line2 = sstrdup(line);
722 p2 = strchr(line, ',');
724 if(strncmp(line2+(p1-line)+1, keyword, l+full) == 0) {
730 p2 = strchr(p1+1, ',');
736 } /* char *cu_mount_checkoption(char *line, char *keyword, int full) */
739 cu_mount_getoptionvalue(char *line, char *keyword)
743 r = cu_mount_checkoption(line, keyword, 0);
746 r += strlen(keyword);
758 m = (char *)smalloc(p-r+1);
759 sstrncpy(m, r, p-r+1);
764 } /* char *cu_mount_getoptionvalue(char *line, char *keyword) */
769 cu_mount_type(const char *type)
771 if(strcmp(type, "ext3") == 0) return CUMT_EXT3;
772 if(strcmp(type, "ext2") == 0) return CUMT_EXT2;
773 if(strcmp(type, "ufs") == 0) return CUMT_UFS;
774 if(strcmp(type, "vxfs") == 0) return CUMT_VXFS;
775 if(strcmp(type, "zfs") == 0) return CUMT_ZFS;
777 } /* int cu_mount_type(const char *type) */