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_GETFSSTAT */
49 # include <sys/types.h>
51 # if HAVE_SYS_STATVFS_H
52 # include <sys/statvfs.h>
54 #endif /* HAVE_GETVFSSTAT */
60 # include <sys/mnttab.h>
67 #ifdef COLLECTD_MNTTAB
68 # undef COLLECTD_MNTTAB
71 #if defined(_PATH_MOUNTED) /* glibc */
72 # define COLLECTD_MNTTAB _PATH_MOUNTED
73 #elif defined(MNTTAB) /* Solaris */
74 # define COLLECTD_MNTTAB MNTTAB
75 #elif defined(MNT_MNTTAB)
76 # define COLLECTD_MNTTAB MNT_MNTTAB
77 #elif defined(MNTTABNAME)
78 # define COLLECTD_MNTTAB MNTTABNAME
80 # define COLLECTD_MNTTAB KMTAB
82 # define COLLECTD_MNTTAB "/etc/mnttab"
85 /* *** *** *** ********************************************* *** *** *** */
86 /* *** *** *** *** *** *** private functions *** *** *** *** *** *** */
87 /* *** *** *** ********************************************* *** *** *** */
89 /* stolen from quota-3.13 (quota-tools) */
91 #define PROC_PARTITIONS "/proc/partitions"
92 #define DEVLABELDIR "/dev"
96 static struct uuidCache_s {
97 struct uuidCache_s *next;
103 #define EXT2_SUPER_MAGIC 0xEF53
104 struct ext2_super_block {
105 unsigned char s_dummy1[56];
106 unsigned char s_magic[2];
107 unsigned char s_dummy2[46];
108 unsigned char s_uuid[16];
109 char s_volume_name[16];
111 #define ext2magic(s) ((unsigned int)s.s_magic[0] \
112 + (((unsigned int)s.s_magic[1]) << 8))
115 struct xfs_super_block {
116 unsigned char s_magic[4];
117 unsigned char s_dummy[28];
118 unsigned char s_uuid[16];
119 unsigned char s_dummy2[60];
122 #endif /* HAVE_XFS_XQM_H */
124 #define REISER_SUPER_MAGIC "ReIsEr2Fs"
125 struct reiserfs_super_block {
126 unsigned char s_dummy1[52];
127 unsigned char s_magic[10];
128 unsigned char s_dummy2[22];
129 unsigned char s_uuid[16];
130 char s_volume_name[16];
133 /* for now, only ext2 and xfs are supported */
135 get_label_uuid(const char *device, char **label, char *uuid)
137 /* start with ext2 and xfs tests, taken from mount_guess_fstype */
138 /* should merge these later */
141 struct ext2_super_block e2sb;
143 struct xfs_super_block xfsb;
145 struct reiserfs_super_block reisersb;
147 fd = open(device, O_RDONLY);
152 if(lseek(fd, 1024, SEEK_SET) == 1024
153 && read(fd, (char *)&e2sb, sizeof(e2sb)) == sizeof(e2sb)
154 && ext2magic(e2sb) == EXT2_SUPER_MAGIC) {
155 memcpy(uuid, e2sb.s_uuid, sizeof(e2sb.s_uuid));
156 namesize = sizeof(e2sb.s_volume_name);
157 *label = smalloc(namesize + 1);
158 sstrncpy(*label, e2sb.s_volume_name, namesize);
161 } else if(lseek(fd, 0, SEEK_SET) == 0
162 && read(fd, (char *)&xfsb, sizeof(xfsb)) == sizeof(xfsb)
163 && (strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC_STR, 4) == 0 ||
164 strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC2_STR, 4) == 0)) {
165 memcpy(uuid, xfsb.s_uuid, sizeof(xfsb.s_uuid));
166 namesize = sizeof(xfsb.s_fsname);
167 *label = smalloc(namesize + 1);
168 sstrncpy(*label, xfsb.s_fsname, namesize);
170 #endif /* HAVE_XFS_XQM_H */
171 } else if(lseek(fd, 65536, SEEK_SET) == 65536
172 && read(fd, (char *)&reisersb, sizeof(reisersb)) == sizeof(reisersb)
173 && !strncmp((char *)&reisersb.s_magic, REISER_SUPER_MAGIC, 9)) {
174 memcpy(uuid, reisersb.s_uuid, sizeof(reisersb.s_uuid));
175 namesize = sizeof(reisersb.s_volume_name);
176 *label = smalloc(namesize + 1);
177 sstrncpy(*label, reisersb.s_volume_name, namesize);
185 uuidcache_addentry(char *device, char *label, char *uuid)
187 struct uuidCache_s *last;
190 last = uuidCache = smalloc(sizeof(*uuidCache));
192 for(last = uuidCache; last->next; last = last->next);
193 last->next = smalloc(sizeof(*uuidCache));
197 last->device = device;
199 memcpy(last->uuid, uuid, sizeof(last->uuid));
208 static char ptname[100];
210 char uuid[16], *label = NULL;
219 procpt = fopen(PROC_PARTITIONS, "r");
224 for(firstPass = 1; firstPass >= 0; firstPass--) {
225 fseek(procpt, 0, SEEK_SET);
226 while(fgets(line, sizeof(line), procpt)) {
227 if(sscanf(line, " %d %d %d %[^\n ]",
228 &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++);
250 if(isdigit((int)s[-1])) {
252 * Note: this is a heuristic only - there is no reason
253 * why these devices should live in /dev.
254 * Perhaps this directory should be specifiable by option.
255 * One might for example have /devlabel with links to /dev
256 * for the devices that may be accessed in this way.
257 * (This is useful, if the cdrom on /dev/hdc must not
260 snprintf(device, sizeof(device), "%s/%s",
261 DEVLABELDIR, ptname);
262 if(!get_label_uuid(device, &label, uuid)) {
263 uuidcache_addentry(sstrdup(device),
275 if(isdigit((int)c)) {
277 } else if(islower((int)c)) {
278 return (c - 'a' + 10);
280 return (c - 'A' + 10);
285 get_spec_by_x(int n, const char *t)
287 struct uuidCache_s *uc;
295 if(!memcmp(t, uc->uuid, sizeof(uc->uuid))) {
296 return sstrdup(uc->device);
300 if(!strcmp(t, uc->label)) {
301 return sstrdup(uc->device);
311 get_spec_by_uuid(const char *s)
317 || s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-') {
321 for(i=0; i<16; i++) {
325 if(!isxdigit((int)s[0]) || !isxdigit((int)s[1])) {
328 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
331 return get_spec_by_x(UUID, uuid);
334 DBG("Found an invalid UUID: %s", s);
338 static char *get_spec_by_volume_label(const char *s)
340 return get_spec_by_x (VOL, s);
343 static char *get_device_name(const char *optstr)
351 else if (strncmp (optstr, "UUID=", 5) == 0)
353 DBG ("TODO: check UUID= code!");
354 rc = get_spec_by_uuid (optstr + 5);
356 else if (strncmp (optstr, "LABEL=", 6) == 0)
358 DBG ("TODO: check LABEL= code!");
359 rc = get_spec_by_volume_label (optstr + 6);
363 rc = sstrdup (optstr);
368 DBG ("Error checking device name: optstr = %s", optstr);
373 /* What weird OS is this..? I can't find any info with google :/ -octo */
374 #if HAVE_LISTMNTENT && 0
375 static cu_mount_t *cu_mount_listmntent (void)
377 cu_mount_t *last = *list;
381 struct tabmntent *mntlist;
382 if(listmntent(&mntlist, COLLECTD_MNTTAB, NULL, NULL) < 0) {
383 DBG("calling listmntent() failed: %s", strerror(errno));
386 for(p = mntlist; p; p = p->next) {
387 char *loop = NULL, *device = NULL;
390 loop = cu_mount_getoptionvalue(mnt->mnt_opts, "loop=");
391 if(loop == NULL) { /* no loop= mount */
392 device = get_device_name(mnt->mnt_fsname);
394 DBG("can't get devicename for fs (%s) %s (%s)"
395 ": ignored", mnt->mnt_type,
396 mnt->mnt_dir, mnt->mnt_fsname);
403 *list = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
406 while(last->next != NULL) { /* is last really last? */
409 last->next = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
412 last->dir = sstrdup(mnt->mnt_dir);
413 last->spec_device = sstrdup(mnt->mnt_fsname);
414 last->device = device;
415 last->type = sstrdup(mnt->mnt_type);
416 last->options = sstrdup(mnt->mnt_opts);
418 } /* for(p = mntlist; p; p = p->next) */
421 } /* cu_mount_t *cu_mount_listmntent(void) */
422 /* #endif HAVE_LISTMNTENT */
424 /* 4.4BSD and Mac OS X (getfsstat) or NetBSD (getvfsstat) */
425 #elif HAVE_GETFSSTAT || HAVE_GETVFSSTAT
426 static cu_mount_t *cu_mount_getfsstat (void)
429 # define STRUCT_STATFS struct statfs
430 # define CMD_STATFS getfsstat
431 # define FLAGS_STATFS MNT_NOWAIT
432 /* #endif HAVE_GETFSSTAT */
433 #elif HAVE_GETVFSSTAT
434 # define STRUCT_STATFS struct statvfs
435 # define CMD_STATFS getvfsstat
436 # define FLAGS_STATFS ST_NOWAIT
437 #endif /* HAVE_GETVFSSTAT */
445 cu_mount_t *first = NULL;
446 cu_mount_t *last = NULL;
447 cu_mount_t *new = NULL;
449 /* Get the number of mounted file systems */
450 if ((bufsize = CMD_STATFS (NULL, 0, FLAGS_STATFS)) < 1)
452 DBG (CMD_STATFS" failed: %s", strerror (errno));
456 if ((buf = (STRUCT_STATFS *) malloc (bufsize * sizeof (STRUCT_STATFS)))
459 memset (buf, '\0', bufsize * sizeof (STRUCT_STATFS));
461 /* The bufsize needs to be passed in bytes. Really. This is not in the
463 if ((num = CMD_STATFS (buf, bufsize * sizeof (STRUCT_STATFS), FLAGS_STATFS)) < 1)
465 DBG (CMD_STATFS" failed: %s", strerror (errno));
470 for (i = 0; i < num; i++)
472 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
474 memset (new, '\0', sizeof (cu_mount_t));
476 /* Copy values from `struct mnttab' */
477 new->dir = sstrdup (buf[i].f_mntonname);
478 new->spec_device = sstrdup (buf[i].f_mntfromname);
479 new->type = sstrdup (buf[i].f_fstypename);
481 new->device = get_device_name (new->options);
501 /* #endif HAVE_GETFSSTAT */
503 /* Solaris (SunOS 10): int getmntent(FILE *fp, struct mnttab *mp); */
504 #elif HAVE_GEN_GETMNTENT
505 static cu_mount_t *cu_mount_gen_getmntent (void)
510 cu_mount_t *first = NULL;
511 cu_mount_t *last = NULL;
512 cu_mount_t *new = NULL;
514 DBG ("(void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
516 if ((fp = fopen (COLLECTD_MNTTAB, "r")) == NULL)
518 syslog (LOG_ERR, "fopen (%s): %s", COLLECTD_MNTTAB, strerror (errno));
522 while (getmntent (fp, &mt) == 0)
524 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
526 memset (new, '\0', sizeof (cu_mount_t));
528 /* Copy values from `struct mnttab' */
529 new->dir = sstrdup (mt.mnt_mountp);
530 new->spec_device = sstrdup (mt.mnt_special);
531 new->type = sstrdup (mt.mnt_fstype);
532 new->options = sstrdup (mt.mnt_mntopts);
533 new->device = get_device_name (new->options);
552 } /* static cu_mount_t *cu_mount_gen_getmntent (void) */
553 /* #endif HAVE_GEN_GETMNTENT */
555 #elif HAVE_SEQ_GETMNTENT
556 #warn "This version of `getmntent' hat not yet been implemented!"
557 /* #endif HAVE_SEQ_GETMNTENT */
559 #elif HAVE_SUN_GETMNTENT
560 #warn "This version of `getmntent' hat not yet been implemented!"
561 /* #endif HAVE_SUN_GETMNTENT */
564 static cu_mount_t *cu_mount_getmntent (void)
569 cu_mount_t *first = NULL;
570 cu_mount_t *last = NULL;
571 cu_mount_t *new = NULL;
573 DBG ("(void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
575 if ((fp = setmntent (COLLECTD_MNTTAB, "r")) == NULL)
577 syslog (LOG_ERR, "setmntent (%s): %s", COLLECTD_MNTTAB, strerror (errno));
581 while ((me = getmntent (fp)) != NULL)
583 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
585 memset (new, '\0', sizeof (cu_mount_t));
587 /* Copy values from `struct mntent *' */
588 new->dir = sstrdup (me->mnt_dir);
589 new->spec_device = sstrdup (me->mnt_fsname);
590 new->type = sstrdup (me->mnt_type);
591 new->options = sstrdup (me->mnt_opts);
592 new->device = get_device_name (new->options);
595 DBG ("new = {dir = %s, spec_device = %s, type = %s, options = %s, device = %s}",
596 new->dir, new->spec_device, new->type, new->options, new->device);
613 DBG ("return (0x%p)", (void *) first);
617 #endif /* HAVE_GETMNTENT */
619 /* *** *** *** ******************************************** *** *** *** */
620 /* *** *** *** *** *** *** public functions *** *** *** *** *** *** */
621 /* *** *** *** ******************************************** *** *** *** */
623 cu_mount_t *cu_mount_getlist(cu_mount_t **list)
626 cu_mount_t *first = NULL;
627 cu_mount_t *last = NULL;
636 while (last->next != NULL)
640 #if HAVE_LISTMNTENT && 0
641 new = cu_mount_listmntent ();
643 new = cu_mount_getfsstat ();
644 #elif HAVE_GEN_GETMNTENT
645 new = cu_mount_gen_getmntent ();
647 new = cu_mount_getmntent ();
663 while ((last != NULL) && (last->next != NULL))
667 } /* cu_mount_t *cu_mount_getlist(cu_mount_t **list) */
669 void cu_mount_freelist (cu_mount_t *list)
674 DBG ("(list = 0x%p)", (void *) list);
676 for (this = list; this != NULL; this = next)
681 sfree (this->spec_device);
682 sfree (this->device);
684 sfree (this->options);
687 } /* void cu_mount_freelist(cu_mount_t *list) */
690 cu_mount_checkoption(char *line, char *keyword, int full)
693 int l = strlen(keyword);
696 if(line == NULL || keyword == NULL) {
703 line2 = sstrdup(line);
713 p2 = strchr(line, ',');
715 if(strncmp(line2+(p1-line)+1, keyword, l+full) == 0) {
721 p2 = strchr(p1+1, ',');
727 } /* char *cu_mount_checkoption(char *line, char *keyword, int full) */
730 cu_mount_getoptionvalue(char *line, char *keyword)
734 r = cu_mount_checkoption(line, keyword, 0);
737 r += strlen(keyword);
749 m = (char *)smalloc(p-r+1);
750 sstrncpy(m, r, p-r+1);
755 } /* char *cu_mount_getoptionvalue(char *line, char *keyword) */
760 cu_mount_type(const char *type)
762 if(strcmp(type, "ext3") == 0) return CUMT_EXT3;
763 if(strcmp(type, "ext2") == 0) return CUMT_EXT2;
764 if(strcmp(type, "ufs") == 0) return CUMT_UFS;
765 if(strcmp(type, "vxfs") == 0) return CUMT_VXFS;
766 if(strcmp(type, "zfs") == 0) return CUMT_ZFS;
768 } /* int cu_mount_type(const char *type) */