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>
31 #define XFS_SUPER_MAGIC_STR "XFSB"
32 #define XFS_SUPER_MAGIC2_STR "BSFX"
36 #include "utils_mount.h"
40 # include <sys/types.h>
42 # if HAVE_SYS_STATVFS_H
43 # include <sys/statvfs.h>
45 /* #endif HAVE_GETVFSSTAT */
49 # include <sys/param.h>
52 # include <sys/ucred.h>
55 # include <sys/mount.h>
57 #endif /* HAVE_GETFSSTAT */
63 # include <sys/mnttab.h>
70 #ifdef COLLECTD_MNTTAB
71 # undef COLLECTD_MNTTAB
74 #if defined(_PATH_MOUNTED) /* glibc */
75 # define COLLECTD_MNTTAB _PATH_MOUNTED
76 #elif defined(MNTTAB) /* Solaris */
77 # define COLLECTD_MNTTAB MNTTAB
78 #elif defined(MNT_MNTTAB)
79 # define COLLECTD_MNTTAB MNT_MNTTAB
80 #elif defined(MNTTABNAME)
81 # define COLLECTD_MNTTAB MNTTABNAME
83 # define COLLECTD_MNTTAB KMTAB
85 # define COLLECTD_MNTTAB "/etc/mnttab"
88 /* *** *** *** ********************************************* *** *** *** */
89 /* *** *** *** *** *** *** private functions *** *** *** *** *** *** */
90 /* *** *** *** ********************************************* *** *** *** */
92 /* stolen from quota-3.13 (quota-tools) */
94 #define PROC_PARTITIONS "/proc/partitions"
95 #define DEVLABELDIR "/dev"
99 static struct uuidCache_s {
100 struct uuidCache_s *next;
106 #define EXT2_SUPER_MAGIC 0xEF53
107 struct ext2_super_block {
108 unsigned char s_dummy1[56];
109 unsigned char s_magic[2];
110 unsigned char s_dummy2[46];
111 unsigned char s_uuid[16];
112 char s_volume_name[16];
114 #define ext2magic(s) ((unsigned int)s.s_magic[0] \
115 + (((unsigned int)s.s_magic[1]) << 8))
118 struct xfs_super_block {
119 unsigned char s_magic[4];
120 unsigned char s_dummy[28];
121 unsigned char s_uuid[16];
122 unsigned char s_dummy2[60];
125 #endif /* HAVE_XFS_XQM_H */
127 #define REISER_SUPER_MAGIC "ReIsEr2Fs"
128 struct reiserfs_super_block {
129 unsigned char s_dummy1[52];
130 unsigned char s_magic[10];
131 unsigned char s_dummy2[22];
132 unsigned char s_uuid[16];
133 char s_volume_name[16];
136 /* for now, only ext2 and xfs are supported */
138 get_label_uuid(const char *device, char **label, char *uuid)
140 /* start with ext2 and xfs tests, taken from mount_guess_fstype */
141 /* should merge these later */
144 struct ext2_super_block e2sb;
146 struct xfs_super_block xfsb;
148 struct reiserfs_super_block reisersb;
150 fd = open(device, O_RDONLY);
155 if(lseek(fd, 1024, SEEK_SET) == 1024
156 && read(fd, (char *)&e2sb, sizeof(e2sb)) == sizeof(e2sb)
157 && ext2magic(e2sb) == EXT2_SUPER_MAGIC) {
158 memcpy(uuid, e2sb.s_uuid, sizeof(e2sb.s_uuid));
159 namesize = sizeof(e2sb.s_volume_name);
160 *label = smalloc(namesize + 1);
161 sstrncpy(*label, e2sb.s_volume_name, namesize);
164 } else if(lseek(fd, 0, SEEK_SET) == 0
165 && read(fd, (char *)&xfsb, sizeof(xfsb)) == sizeof(xfsb)
166 && (strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC_STR, 4) == 0 ||
167 strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC2_STR, 4) == 0)) {
168 memcpy(uuid, xfsb.s_uuid, sizeof(xfsb.s_uuid));
169 namesize = sizeof(xfsb.s_fsname);
170 *label = smalloc(namesize + 1);
171 sstrncpy(*label, xfsb.s_fsname, namesize);
173 #endif /* HAVE_XFS_XQM_H */
174 } else if(lseek(fd, 65536, SEEK_SET) == 65536
175 && read(fd, (char *)&reisersb, sizeof(reisersb)) == sizeof(reisersb)
176 && !strncmp((char *)&reisersb.s_magic, REISER_SUPER_MAGIC, 9)) {
177 memcpy(uuid, reisersb.s_uuid, sizeof(reisersb.s_uuid));
178 namesize = sizeof(reisersb.s_volume_name);
179 *label = smalloc(namesize + 1);
180 sstrncpy(*label, reisersb.s_volume_name, namesize);
188 uuidcache_addentry(char *device, char *label, char *uuid)
190 struct uuidCache_s *last;
193 last = uuidCache = smalloc(sizeof(*uuidCache));
195 for(last = uuidCache; last->next; last = last->next);
196 last->next = smalloc(sizeof(*uuidCache));
200 last->device = device;
202 memcpy(last->uuid, uuid, sizeof(last->uuid));
211 static char ptname[100];
213 char uuid[16], *label = NULL;
222 procpt = fopen(PROC_PARTITIONS, "r");
227 for(firstPass = 1; firstPass >= 0; firstPass--) {
228 fseek(procpt, 0, SEEK_SET);
229 while(fgets(line, sizeof(line), procpt)) {
230 if(sscanf(line, " %d %d %d %[^\n ]",
231 &ma, &mi, &sz, ptname) != 4)
236 /* skip extended partitions (heuristic: size 1) */
241 /* look only at md devices on first pass */
242 handleOnFirst = !strncmp(ptname, "md", 2);
243 if(firstPass != handleOnFirst) {
247 /* skip entire disk (minor 0, 64, ... on ide;
249 /* heuristic: partition name ends in a digit */
251 for(s = ptname; *s; s++);
253 if(isdigit((int)s[-1])) {
255 * Note: this is a heuristic only - there is no reason
256 * why these devices should live in /dev.
257 * Perhaps this directory should be specifiable by option.
258 * One might for example have /devlabel with links to /dev
259 * for the devices that may be accessed in this way.
260 * (This is useful, if the cdrom on /dev/hdc must not
263 ssnprintf(device, sizeof(device), "%s/%s",
264 DEVLABELDIR, ptname);
265 if(!get_label_uuid(device, &label, uuid)) {
266 uuidcache_addentry(sstrdup(device),
278 if(isdigit((int)c)) {
280 } else if(islower((int)c)) {
281 return (c - 'a' + 10);
283 return (c - 'A' + 10);
288 get_spec_by_x(int n, const char *t)
290 struct uuidCache_s *uc;
298 if(!memcmp(t, uc->uuid, sizeof(uc->uuid))) {
299 return sstrdup(uc->device);
303 if(!strcmp(t, uc->label)) {
304 return sstrdup(uc->device);
314 get_spec_by_uuid(const char *s)
320 || s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-') {
324 for(i=0; i<16; i++) {
328 if(!isxdigit((int)s[0]) || !isxdigit((int)s[1])) {
331 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
334 return get_spec_by_x(UUID, uuid);
337 DEBUG("utils_mount: Found an invalid UUID: %s", s);
341 static char *get_spec_by_volume_label(const char *s)
343 return get_spec_by_x (VOL, s);
346 static char *get_device_name(const char *optstr)
354 else if (strncmp (optstr, "UUID=", 5) == 0)
356 DEBUG ("utils_mount: TODO: check UUID= code!");
357 rc = get_spec_by_uuid (optstr + 5);
359 else if (strncmp (optstr, "LABEL=", 6) == 0)
361 DEBUG ("utils_mount: TODO: check LABEL= code!");
362 rc = get_spec_by_volume_label (optstr + 6);
366 rc = sstrdup (optstr);
371 DEBUG ("utils_mount: Error checking device name: optstr = %s", optstr);
376 /* What weird OS is this..? I can't find any info with google :/ -octo */
377 #if HAVE_LISTMNTENT && 0
378 static cu_mount_t *cu_mount_listmntent (void)
380 cu_mount_t *last = *list;
384 struct tabmntent *mntlist;
385 if(listmntent(&mntlist, COLLECTD_MNTTAB, NULL, NULL) < 0) {
388 DEBUG("utils_mount: calling listmntent() failed: %s",
389 sstrerror (errno, errbuf, sizeof (errbuf)));
390 #endif /* COLLECT_DEBUG */
393 for(p = mntlist; p; p = p->next) {
394 char *loop = NULL, *device = NULL;
397 loop = cu_mount_getoptionvalue(mnt->mnt_opts, "loop=");
398 if(loop == NULL) { /* no loop= mount */
399 device = get_device_name(mnt->mnt_fsname);
401 DEBUG("utils_mount: can't get devicename for fs (%s) %s (%s)"
402 ": ignored", mnt->mnt_type,
403 mnt->mnt_dir, mnt->mnt_fsname);
410 *list = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
413 while(last->next != NULL) { /* is last really last? */
416 last->next = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
419 last->dir = sstrdup(mnt->mnt_dir);
420 last->spec_device = sstrdup(mnt->mnt_fsname);
421 last->device = device;
422 last->type = sstrdup(mnt->mnt_type);
423 last->options = sstrdup(mnt->mnt_opts);
425 } /* for(p = mntlist; p; p = p->next) */
428 } /* cu_mount_t *cu_mount_listmntent(void) */
429 /* #endif HAVE_LISTMNTENT */
431 /* 4.4BSD and Mac OS X (getfsstat) or NetBSD (getvfsstat) */
432 #elif HAVE_GETVFSSTAT || HAVE_GETFSSTAT
433 static cu_mount_t *cu_mount_getfsstat (void)
436 # define STRUCT_STATFS struct statvfs
437 # define CMD_STATFS getvfsstat
438 # define FLAGS_STATFS ST_NOWAIT
439 /* #endif HAVE_GETVFSSTAT */
441 # define STRUCT_STATFS struct statfs
442 # define CMD_STATFS getfsstat
443 # define FLAGS_STATFS MNT_NOWAIT
444 #endif /* HAVE_GETFSSTAT */
452 cu_mount_t *first = NULL;
453 cu_mount_t *last = NULL;
454 cu_mount_t *new = NULL;
456 /* Get the number of mounted file systems */
457 if ((bufsize = CMD_STATFS (NULL, 0, FLAGS_STATFS)) < 1)
461 DEBUG ("utils_mount: getv?fsstat failed: %s",
462 sstrerror (errno, errbuf, sizeof (errbuf)));
463 #endif /* COLLECT_DEBUG */
467 if ((buf = (STRUCT_STATFS *) malloc (bufsize * sizeof (STRUCT_STATFS)))
470 memset (buf, '\0', bufsize * sizeof (STRUCT_STATFS));
472 /* The bufsize needs to be passed in bytes. Really. This is not in the
474 if ((num = CMD_STATFS (buf, bufsize * sizeof (STRUCT_STATFS), FLAGS_STATFS)) < 1)
478 DEBUG ("utils_mount: getv?fsstat failed: %s",
479 sstrerror (errno, errbuf, sizeof (errbuf)));
480 #endif /* COLLECT_DEBUG */
485 for (i = 0; i < num; i++)
487 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
489 memset (new, '\0', sizeof (cu_mount_t));
491 /* Copy values from `struct mnttab' */
492 new->dir = sstrdup (buf[i].f_mntonname);
493 new->spec_device = sstrdup (buf[i].f_mntfromname);
494 new->type = sstrdup (buf[i].f_fstypename);
496 new->device = get_device_name (new->options);
516 /* #endif HAVE_GETVFSSTAT || HAVE_GETFSSTAT */
518 /* Solaris (SunOS 10): int getmntent(FILE *fp, struct mnttab *mp); */
519 #elif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT
520 static cu_mount_t *cu_mount_gen_getmntent (void)
525 cu_mount_t *first = NULL;
526 cu_mount_t *last = NULL;
527 cu_mount_t *new = NULL;
529 DEBUG ("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
531 if ((fp = fopen (COLLECTD_MNTTAB, "r")) == NULL)
534 ERROR ("fopen (%s): %s", COLLECTD_MNTTAB,
535 sstrerror (errno, errbuf, sizeof (errbuf)));
539 while (getmntent (fp, &mt) == 0)
541 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
543 memset (new, '\0', sizeof (cu_mount_t));
545 /* Copy values from `struct mnttab' */
546 new->dir = sstrdup (mt.mnt_mountp);
547 new->spec_device = sstrdup (mt.mnt_special);
548 new->type = sstrdup (mt.mnt_fstype);
549 new->options = sstrdup (mt.mnt_mntopts);
550 new->device = get_device_name (new->options);
569 } /* static cu_mount_t *cu_mount_gen_getmntent (void) */
570 /* #endif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT */
572 #elif HAVE_SEQ_GETMNTENT
573 #warn "This version of `getmntent' hat not yet been implemented!"
574 /* #endif HAVE_SEQ_GETMNTENT */
576 #elif HAVE_ONE_GETMNTENT
577 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 ((me = getmntent (fp)) != NULL)
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);
632 #endif /* HAVE_ONE_GETMNTENT */
634 /* *** *** *** ******************************************** *** *** *** */
635 /* *** *** *** *** *** *** public functions *** *** *** *** *** *** */
636 /* *** *** *** ******************************************** *** *** *** */
638 cu_mount_t *cu_mount_getlist(cu_mount_t **list)
641 cu_mount_t *first = NULL;
642 cu_mount_t *last = NULL;
651 while (last->next != NULL)
655 #if HAVE_LISTMNTENT && 0
656 new = cu_mount_listmntent ();
657 #elif HAVE_GETVFSSTAT || HAVE_GETFSSTAT
658 new = cu_mount_getfsstat ();
659 #elif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT
660 new = cu_mount_gen_getmntent ();
661 #elif HAVE_SEQ_GETMNTENT
662 # error "This version of `getmntent' hat not yet been implemented!"
663 #elif HAVE_ONE_GETMNTENT
664 new = cu_mount_getmntent ();
666 # error "Could not determine how to find mountpoints."
680 while ((last != NULL) && (last->next != NULL))
684 } /* cu_mount_t *cu_mount_getlist(cu_mount_t **list) */
686 void cu_mount_freelist (cu_mount_t *list)
691 for (this = list; this != NULL; this = next)
696 sfree (this->spec_device);
697 sfree (this->device);
699 sfree (this->options);
702 } /* void cu_mount_freelist(cu_mount_t *list) */
705 cu_mount_checkoption(char *line, char *keyword, int full)
708 int l = strlen(keyword);
711 if(line == NULL || keyword == NULL) {
718 line2 = sstrdup(line);
728 p2 = strchr(line, ',');
730 if(strncmp(line2+(p1-line)+1, keyword, l+full) == 0) {
736 p2 = strchr(p1+1, ',');
742 } /* char *cu_mount_checkoption(char *line, char *keyword, int full) */
745 cu_mount_getoptionvalue(char *line, char *keyword)
749 r = cu_mount_checkoption(line, keyword, 0);
752 r += strlen(keyword);
764 m = (char *)smalloc(p-r+1);
765 sstrncpy(m, r, p-r+1);
770 } /* char *cu_mount_getoptionvalue(char *line, char *keyword) */
773 cu_mount_type(const char *type)
775 if(strcmp(type, "ext3") == 0) return CUMT_EXT3;
776 if(strcmp(type, "ext2") == 0) return CUMT_EXT2;
777 if(strcmp(type, "ufs") == 0) return CUMT_UFS;
778 if(strcmp(type, "vxfs") == 0) return CUMT_VXFS;
779 if(strcmp(type, "zfs") == 0) return CUMT_ZFS;
781 } /* int cu_mount_type(const char *type) */