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"
33 #include "utils_debug.h"
34 #include "utils_mount.h"
38 # include <sys/param.h>
41 # include <sys/ucred.h>
44 # include <sys/mount.h>
46 #endif /* HAVE_GETMNTINFO */
52 # include <sys/mnttab.h>
59 #ifdef COLLECTD_MNTTAB
60 # undef COLLECTD_MNTTAB
63 #if defined(_PATH_MOUNTED) /* glibc */
64 # define COLLECTD_MNTTAB _PATH_MOUNTED
65 #elif defined(MNTTAB) /* Solaris */
66 # define COLLECTD_MNTTAB MNTTAB
67 #elif defined(MNT_MNTTAB)
68 # define COLLECTD_MNTTAB MNT_MNTTAB
69 #elif defined(MNTTABNAME)
70 # define COLLECTD_MNTTAB MNTTABNAME
72 # define COLLECTD_MNTTAB KMTAB
74 # define COLLECTD_MNTTAB "/etc/mnttab"
77 /* *** *** *** ********************************************* *** *** *** */
78 /* *** *** *** *** *** *** private functions *** *** *** *** *** *** */
79 /* *** *** *** ********************************************* *** *** *** */
81 /* stolen from quota-3.13 (quota-tools) */
83 #define PROC_PARTITIONS "/proc/partitions"
84 #define DEVLABELDIR "/dev"
88 static struct uuidCache_s {
89 struct uuidCache_s *next;
95 #define EXT2_SUPER_MAGIC 0xEF53
96 struct ext2_super_block {
97 unsigned char s_dummy1[56];
98 unsigned char s_magic[2];
99 unsigned char s_dummy2[46];
100 unsigned char s_uuid[16];
101 char s_volume_name[16];
103 #define ext2magic(s) ((unsigned int)s.s_magic[0] \
104 + (((unsigned int)s.s_magic[1]) << 8))
107 struct xfs_super_block {
108 unsigned char s_magic[4];
109 unsigned char s_dummy[28];
110 unsigned char s_uuid[16];
111 unsigned char s_dummy2[60];
114 #endif /* HAVE_XFS_XQM_H */
116 #define REISER_SUPER_MAGIC "ReIsEr2Fs"
117 struct reiserfs_super_block {
118 unsigned char s_dummy1[52];
119 unsigned char s_magic[10];
120 unsigned char s_dummy2[22];
121 unsigned char s_uuid[16];
122 char s_volume_name[16];
125 /* for now, only ext2 and xfs are supported */
127 get_label_uuid(const char *device, char **label, char *uuid)
129 /* start with ext2 and xfs tests, taken from mount_guess_fstype */
130 /* should merge these later */
133 struct ext2_super_block e2sb;
135 struct xfs_super_block xfsb;
137 struct reiserfs_super_block reisersb;
139 fd = open(device, O_RDONLY);
144 if(lseek(fd, 1024, SEEK_SET) == 1024
145 && read(fd, (char *)&e2sb, sizeof(e2sb)) == sizeof(e2sb)
146 && ext2magic(e2sb) == EXT2_SUPER_MAGIC) {
147 memcpy(uuid, e2sb.s_uuid, sizeof(e2sb.s_uuid));
148 namesize = sizeof(e2sb.s_volume_name);
149 *label = smalloc(namesize + 1);
150 sstrncpy(*label, e2sb.s_volume_name, namesize);
153 } else if(lseek(fd, 0, SEEK_SET) == 0
154 && read(fd, (char *)&xfsb, sizeof(xfsb)) == sizeof(xfsb)
155 && (strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC_STR, 4) == 0 ||
156 strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC2_STR, 4) == 0)) {
157 memcpy(uuid, xfsb.s_uuid, sizeof(xfsb.s_uuid));
158 namesize = sizeof(xfsb.s_fsname);
159 *label = smalloc(namesize + 1);
160 sstrncpy(*label, xfsb.s_fsname, namesize);
162 #endif /* HAVE_XFS_XQM_H */
163 } else if(lseek(fd, 65536, SEEK_SET) == 65536
164 && read(fd, (char *)&reisersb, sizeof(reisersb)) == sizeof(reisersb)
165 && !strncmp((char *)&reisersb.s_magic, REISER_SUPER_MAGIC, 9)) {
166 memcpy(uuid, reisersb.s_uuid, sizeof(reisersb.s_uuid));
167 namesize = sizeof(reisersb.s_volume_name);
168 *label = smalloc(namesize + 1);
169 sstrncpy(*label, reisersb.s_volume_name, namesize);
177 uuidcache_addentry(char *device, char *label, char *uuid)
179 struct uuidCache_s *last;
182 last = uuidCache = smalloc(sizeof(*uuidCache));
184 for(last = uuidCache; last->next; last = last->next);
185 last->next = smalloc(sizeof(*uuidCache));
189 last->device = device;
191 memcpy(last->uuid, uuid, sizeof(last->uuid));
200 static char ptname[100];
202 char uuid[16], *label = NULL;
211 procpt = fopen(PROC_PARTITIONS, "r");
216 for(firstPass = 1; firstPass >= 0; firstPass--) {
217 fseek(procpt, 0, SEEK_SET);
218 while(fgets(line, sizeof(line), procpt)) {
219 if(sscanf(line, " %d %d %d %[^\n ]",
220 &ma, &mi, &sz, ptname) != 4)
225 /* skip extended partitions (heuristic: size 1) */
230 /* look only at md devices on first pass */
231 handleOnFirst = !strncmp(ptname, "md", 2);
232 if(firstPass != handleOnFirst) {
236 /* skip entire disk (minor 0, 64, ... on ide;
238 /* heuristic: partition name ends in a digit */
240 for(s = ptname; *s; s++);
242 if(isdigit((int)s[-1])) {
244 * Note: this is a heuristic only - there is no reason
245 * why these devices should live in /dev.
246 * Perhaps this directory should be specifiable by option.
247 * One might for example have /devlabel with links to /dev
248 * for the devices that may be accessed in this way.
249 * (This is useful, if the cdrom on /dev/hdc must not
252 snprintf(device, sizeof(device), "%s/%s",
253 DEVLABELDIR, ptname);
254 if(!get_label_uuid(device, &label, uuid)) {
255 uuidcache_addentry(sstrdup(device),
267 if(isdigit((int)c)) {
269 } else if(islower((int)c)) {
270 return (c - 'a' + 10);
272 return (c - 'A' + 10);
277 get_spec_by_x(int n, const char *t)
279 struct uuidCache_s *uc;
287 if(!memcmp(t, uc->uuid, sizeof(uc->uuid))) {
288 return sstrdup(uc->device);
292 if(!strcmp(t, uc->label)) {
293 return sstrdup(uc->device);
303 get_spec_by_uuid(const char *s)
309 || s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-') {
313 for(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 DBG("Found an invalid UUID: %s", s);
330 static char *get_spec_by_volume_label(const char *s)
332 return get_spec_by_x (VOL, s);
335 static char *get_device_name(const char *optstr)
343 else if (strncmp (optstr, "UUID=", 5) == 0)
345 DBG ("TODO: check UUID= code!");
346 rc = get_spec_by_uuid (optstr + 5);
348 else if (strncmp (optstr, "LABEL=", 6) == 0)
350 DBG ("TODO: check LABEL= code!");
351 rc = get_spec_by_volume_label (optstr + 6);
355 rc = sstrdup (optstr);
360 DBG ("Error checking device name: optstr = %s", optstr);
365 /* What weird OS is this..? I can't find any info with google :/ -octo */
366 #if HAVE_LISTMNTENT && 0
367 static cu_mount_t *cu_mount_listmntent (void)
369 cu_mount_t *last = *list;
373 struct tabmntent *mntlist;
374 if(listmntent(&mntlist, COLLECTD_MNTTAB, NULL, NULL) < 0) {
375 DBG("calling listmntent() failed: %s", strerror(errno));
378 for(p = mntlist; p; p = p->next) {
379 char *loop = NULL, *device = NULL;
382 loop = cu_mount_getoptionvalue(mnt->mnt_opts, "loop=");
383 if(loop == NULL) { /* no loop= mount */
384 device = get_device_name(mnt->mnt_fsname);
386 DBG("can't get devicename for fs (%s) %s (%s)"
387 ": ignored", mnt->mnt_type,
388 mnt->mnt_dir, mnt->mnt_fsname);
395 *list = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
398 while(last->next != NULL) { /* is last really last? */
401 last->next = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
404 last->dir = sstrdup(mnt->mnt_dir);
405 last->spec_device = sstrdup(mnt->mnt_fsname);
406 last->device = device;
407 last->type = sstrdup(mnt->mnt_type);
408 last->options = sstrdup(mnt->mnt_opts);
410 } /* for(p = mntlist; p; p = p->next) */
413 } /* cu_mount_t *cu_mount_listmntent(void) */
414 /* #endif HAVE_LISTMNTENT */
416 /* 4.4BSD and Mac OS X */
418 static cu_mount_t *cu_mount_getfsstat (void)
426 cu_mount_t *first = NULL;
427 cu_mount_t *last = NULL;
428 cu_mount_t *new = NULL;
430 /* Get the number of mounted file systems */
431 if ((bufsize = getfsstat (NULL, 0, MNT_NOWAIT)) < 1)
433 DBG ("getfsstat failed: %s", strerror (errno));
437 if ((buf = (struct statfs *) malloc (bufsize * sizeof (struct statfs)))
440 memset (buf, '\0', bufsize * sizeof (struct statfs));
442 /* The bufsize needs to be passed in bytes. Really. This is not in the
444 if ((num = getfsstat (buf, bufsize * sizeof (struct statfs), MNT_NOWAIT)) < 1)
446 DBG ("getfsstat failed: %s", strerror (errno));
451 for (i = 0; i < num; i++)
453 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
455 memset (new, '\0', sizeof (cu_mount_t));
457 /* Copy values from `struct mnttab' */
458 new->dir = sstrdup (buf[i].f_mntonname);
459 new->spec_device = sstrdup (buf[i].f_mntfromname);
460 new->type = sstrdup (buf[i].f_fstypename);
462 new->device = get_device_name (new->options);
482 /* #endif HAVE_GETFSSTAT */
484 /* Solaris (SunOS 10): int getmntent(FILE *fp, struct mnttab *mp); */
485 #elif HAVE_GEN_GETMNTENT
486 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 DBG ("(void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
497 if ((fp = fopen (COLLECTD_MNTTAB, "r")) == NULL)
499 syslog (LOG_ERR, "fopen (%s): %s", COLLECTD_MNTTAB, strerror (errno));
503 while (getmntent (fp, &mt) == 0)
505 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
507 memset (new, '\0', sizeof (cu_mount_t));
509 /* Copy values from `struct mnttab' */
510 new->dir = sstrdup (mt.mnt_mountp);
511 new->spec_device = sstrdup (mt.mnt_special);
512 new->type = sstrdup (mt.mnt_fstype);
513 new->options = sstrdup (mt.mnt_mntopts);
514 new->device = get_device_name (new->options);
533 } /* static cu_mount_t *cu_mount_gen_getmntent (void) */
534 /* #endif HAVE_GEN_GETMNTENT */
536 #elif HAVE_SEQ_GETMNTENT
537 #warn "This version of `getmntent' hat not yet been implemented!"
538 /* #endif HAVE_SEQ_GETMNTENT */
540 #elif HAVE_SUN_GETMNTENT
541 #warn "This version of `getmntent' hat not yet been implemented!"
542 /* #endif HAVE_SUN_GETMNTENT */
545 static cu_mount_t *cu_mount_getmntent (void)
550 cu_mount_t *first = NULL;
551 cu_mount_t *last = NULL;
552 cu_mount_t *new = NULL;
554 DBG ("(void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
556 if ((fp = setmntent (COLLECTD_MNTTAB, "r")) == NULL)
558 syslog (LOG_ERR, "setmntent (%s): %s", COLLECTD_MNTTAB, strerror (errno));
562 while ((me = getmntent (fp)) != NULL)
564 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
566 memset (new, '\0', sizeof (cu_mount_t));
568 /* Copy values from `struct mntent *' */
569 new->dir = sstrdup (me->mnt_dir);
570 new->spec_device = sstrdup (me->mnt_fsname);
571 new->type = sstrdup (me->mnt_type);
572 new->options = sstrdup (me->mnt_opts);
573 new->device = get_device_name (new->options);
576 DBG ("new = {dir = %s, spec_device = %s, type = %s, options = %s, device = %s}",
577 new->dir, new->spec_device, new->type, new->options, new->device);
594 DBG ("return (0x%p)", (void *) first);
598 #endif /* HAVE_GETMNTENT */
600 /* *** *** *** ******************************************** *** *** *** */
601 /* *** *** *** *** *** *** public functions *** *** *** *** *** *** */
602 /* *** *** *** ******************************************** *** *** *** */
604 cu_mount_t *cu_mount_getlist(cu_mount_t **list)
607 cu_mount_t *first = NULL;
608 cu_mount_t *last = NULL;
617 while (last->next != NULL)
621 #if HAVE_LISTMNTENT && 0
622 new = cu_mount_listmntent ();
624 new = cu_mount_getfsstat ();
625 #elif HAVE_GEN_GETMNTENT
626 new = cu_mount_gen_getmntent ();
628 new = cu_mount_getmntent ();
644 while ((last != NULL) && (last->next != NULL))
648 } /* cu_mount_t *cu_mount_getlist(cu_mount_t **list) */
650 void cu_mount_freelist (cu_mount_t *list)
655 DBG ("(list = 0x%p)", (void *) list);
657 for (this = list; this != NULL; this = next)
662 sfree (this->spec_device);
663 sfree (this->device);
665 sfree (this->options);
668 } /* void cu_mount_freelist(cu_mount_t *list) */
671 cu_mount_checkoption(char *line, char *keyword, int full)
674 int l = strlen(keyword);
677 if(line == NULL || keyword == NULL) {
684 line2 = sstrdup(line);
694 p2 = strchr(line, ',');
696 if(strncmp(line2+(p1-line)+1, keyword, l+full) == 0) {
702 p2 = strchr(p1+1, ',');
708 } /* char *cu_mount_checkoption(char *line, char *keyword, int full) */
711 cu_mount_getoptionvalue(char *line, char *keyword)
715 r = cu_mount_checkoption(line, keyword, 0);
718 r += strlen(keyword);
730 m = (char *)smalloc(p-r+1);
731 sstrncpy(m, r, p-r+1);
736 } /* char *cu_mount_getoptionvalue(char *line, char *keyword) */
741 cu_mount_type(const char *type)
743 if(strcmp(type, "ext3") == 0) return CUMT_EXT3;
744 if(strcmp(type, "ext2") == 0) return CUMT_EXT2;
745 if(strcmp(type, "ufs") == 0) return CUMT_UFS;
746 if(strcmp(type, "vxfs") == 0) return CUMT_VXFS;
747 if(strcmp(type, "zfs") == 0) return CUMT_ZFS;
749 } /* int cu_mount_type(const char *type) */