2 * collectd - src/utils_mount.c
3 * Copyright (C) 2005 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"
32 #include "utils_debug.h"
33 #include "utils_mount.h"
37 # include <sys/param.h>
40 # include <sys/ucred.h>
43 # include <sys/mount.h>
45 #endif /* HAVE_GETMNTINFO */
51 # include <sys/mnttab.h>
55 # if defined(_PATH_MOUNTED)
56 # define MNTTAB _PATH_MOUNTED
57 # elif defined(MNT_MNTTAB)
58 # define MNTTAB MNT_MNTTAB
59 # elif defined(MNTTABNAME)
60 # define MNTTAB MNTTABNAME
64 # define MNTTAB "/etc/mnttab"
68 /* *** *** *** ********************************************* *** *** *** */
69 /* *** *** *** *** *** *** private functions *** *** *** *** *** *** */
70 /* *** *** *** ********************************************* *** *** *** */
72 /* stolen from quota-3.13 (quota-tools) */
74 #define PROC_PARTITIONS "/proc/partitions"
75 #define DEVLABELDIR "/dev"
79 static struct uuidCache_s {
80 struct uuidCache_s *next;
86 #define EXT2_SUPER_MAGIC 0xEF53
87 struct ext2_super_block {
88 unsigned char s_dummy1[56];
89 unsigned char s_magic[2];
90 unsigned char s_dummy2[46];
91 unsigned char s_uuid[16];
92 char s_volume_name[16];
94 #define ext2magic(s) ((unsigned int)s.s_magic[0] \
95 + (((unsigned int)s.s_magic[1]) << 8))
98 struct xfs_super_block {
99 unsigned char s_magic[4];
100 unsigned char s_dummy[28];
101 unsigned char s_uuid[16];
102 unsigned char s_dummy2[60];
105 #endif /* HAVE_XFS_XQM_H */
107 #define REISER_SUPER_MAGIC "ReIsEr2Fs"
108 struct reiserfs_super_block {
109 unsigned char s_dummy1[52];
110 unsigned char s_magic[10];
111 unsigned char s_dummy2[22];
112 unsigned char s_uuid[16];
113 char s_volume_name[16];
116 /* for now, only ext2 and xfs are supported */
118 get_label_uuid(const char *device, char **label, char *uuid)
120 /* start with ext2 and xfs tests, taken from mount_guess_fstype */
121 /* should merge these later */
124 struct ext2_super_block e2sb;
126 struct xfs_super_block xfsb;
128 struct reiserfs_super_block reisersb;
130 fd = open(device, O_RDONLY);
135 if(lseek(fd, 1024, SEEK_SET) == 1024
136 && read(fd, (char *)&e2sb, sizeof(e2sb)) == sizeof(e2sb)
137 && ext2magic(e2sb) == EXT2_SUPER_MAGIC) {
138 memcpy(uuid, e2sb.s_uuid, sizeof(e2sb.s_uuid));
139 namesize = sizeof(e2sb.s_volume_name);
140 *label = smalloc(namesize + 1);
141 sstrncpy(*label, e2sb.s_volume_name, namesize);
144 } else if(lseek(fd, 0, SEEK_SET) == 0
145 && read(fd, (char *)&xfsb, sizeof(xfsb)) == sizeof(xfsb)
146 && (strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC_STR, 4) == 0 ||
147 strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC2_STR, 4) == 0)) {
148 memcpy(uuid, xfsb.s_uuid, sizeof(xfsb.s_uuid));
149 namesize = sizeof(xfsb.s_fsname);
150 *label = smalloc(namesize + 1);
151 sstrncpy(*label, xfsb.s_fsname, namesize);
153 #endif /* HAVE_XFS_XQM_H */
154 } else if(lseek(fd, 65536, SEEK_SET) == 65536
155 && read(fd, (char *)&reisersb, sizeof(reisersb)) == sizeof(reisersb)
156 && !strncmp((char *)&reisersb.s_magic, REISER_SUPER_MAGIC, 9)) {
157 memcpy(uuid, reisersb.s_uuid, sizeof(reisersb.s_uuid));
158 namesize = sizeof(reisersb.s_volume_name);
159 *label = smalloc(namesize + 1);
160 sstrncpy(*label, reisersb.s_volume_name, namesize);
168 uuidcache_addentry(char *device, char *label, char *uuid)
170 struct uuidCache_s *last;
173 last = uuidCache = smalloc(sizeof(*uuidCache));
175 for(last = uuidCache; last->next; last = last->next);
176 last->next = smalloc(sizeof(*uuidCache));
180 last->device = device;
182 memcpy(last->uuid, uuid, sizeof(last->uuid));
191 static char ptname[100];
193 char uuid[16], *label = NULL;
202 procpt = fopen(PROC_PARTITIONS, "r");
207 for(firstPass = 1; firstPass >= 0; firstPass--) {
208 fseek(procpt, 0, SEEK_SET);
209 while(fgets(line, sizeof(line), procpt)) {
210 if(sscanf(line, " %d %d %d %[^\n ]",
211 &ma, &mi, &sz, ptname) != 4)
216 /* skip extended partitions (heuristic: size 1) */
221 /* look only at md devices on first pass */
222 handleOnFirst = !strncmp(ptname, "md", 2);
223 if(firstPass != handleOnFirst) {
227 /* skip entire disk (minor 0, 64, ... on ide;
229 /* heuristic: partition name ends in a digit */
231 for(s = ptname; *s; s++);
233 if(isdigit((int)s[-1])) {
235 * Note: this is a heuristic only - there is no reason
236 * why these devices should live in /dev.
237 * Perhaps this directory should be specifiable by option.
238 * One might for example have /devlabel with links to /dev
239 * for the devices that may be accessed in this way.
240 * (This is useful, if the cdrom on /dev/hdc must not
243 snprintf(device, sizeof(device), "%s/%s",
244 DEVLABELDIR, ptname);
245 if(!get_label_uuid(device, &label, uuid)) {
246 uuidcache_addentry(sstrdup(device),
258 if(isdigit((int)c)) {
260 } else if(islower((int)c)) {
261 return (c - 'a' + 10);
263 return (c - 'A' + 10);
268 get_spec_by_x(int n, const char *t)
270 struct uuidCache_s *uc;
278 if(!memcmp(t, uc->uuid, sizeof(uc->uuid))) {
279 return sstrdup(uc->device);
283 if(!strcmp(t, uc->label)) {
284 return sstrdup(uc->device);
294 get_spec_by_uuid(const char *s)
300 || s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-') {
304 for(i=0; i<16; i++) {
308 if(!isxdigit((int)s[0]) || !isxdigit((int)s[1])) {
311 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
314 return get_spec_by_x(UUID, uuid);
317 DBG("Found an invalid UUID: %s", s);
321 static char *get_spec_by_volume_label(const char *s)
323 return get_spec_by_x (VOL, s);
326 static char *get_device_name(const char *optstr)
334 else if (strncmp (optstr, "UUID=", 5) == 0)
336 DBG ("TODO: check UUID= code!");
337 rc = get_spec_by_uuid (optstr + 5);
339 else if (strncmp (optstr, "LABEL=", 6) == 0)
341 DBG ("TODO: check LABEL= code!");
342 rc = get_spec_by_volume_label (optstr + 6);
346 rc = sstrdup (optstr);
351 DBG ("Error checking device name: optstr = %s", optstr);
356 /* What weird OS is this..? I can't find any info with google :/ -octo */
357 #if HAVE_LISTMNTENT && 0
358 static cu_mount_t *cu_mount_listmntent (void)
360 cu_mount_t *last = *list;
364 struct tabmntent *mntlist;
365 if(listmntent(&mntlist, MNTTAB, NULL, NULL) < 0) {
366 DBG("calling listmntent() failed: %s", strerror(errno));
369 for(p = mntlist; p; p = p->next) {
370 char *loop = NULL, *device = NULL;
373 loop = cu_mount_getoptionvalue(mnt->mnt_opts, "loop=");
374 if(loop == NULL) { /* no loop= mount */
375 device = get_device_name(mnt->mnt_fsname);
377 DBG("can't get devicename for fs (%s) %s (%s)"
378 ": ignored", mnt->mnt_type,
379 mnt->mnt_dir, mnt->mnt_fsname);
386 *list = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
389 while(last->next != NULL) { /* is last really last? */
392 last->next = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
395 last->dir = sstrdup(mnt->mnt_dir);
396 last->spec_device = sstrdup(mnt->mnt_fsname);
397 last->device = device;
398 last->type = sstrdup(mnt->mnt_type);
399 last->options = sstrdup(mnt->mnt_opts);
401 } /* for(p = mntlist; p; p = p->next) */
404 } /* cu_mount_t *cu_mount_listmntent(void) */
405 /* #endif HAVE_LISTMNTENT */
407 /* 4.4BSD and Mac OS X */
409 static cu_mount_t *cu_mount_getfsstat (void)
417 cu_mount_t *first = NULL;
418 cu_mount_t *last = NULL;
419 cu_mount_t *new = NULL;
421 /* Get the number of mounted file systems */
422 if ((bufsize = getfsstat (NULL, 0, MNT_NOWAIT)) < 1)
425 if ((buf = (struct statfs *) malloc (bufsize * sizeof (struct statfs))) == NULL)
427 memset (buf, '\0', bufsize * sizeof (struct statfs));
429 /* FIXME: If `bufsize' in bytes or structures? */
430 if ((num = getfsstat (buf, bufsize, MNT_NOWAIT)) < 1)
436 for (i = 0; i < num; i++)
438 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
440 memset (new, '\0', sizeof (cu_mount_t));
442 /* Copy values from `struct mnttab' */
443 new->dir = sstrdup (buf[i].f_mntonname);
444 new->spec_device = sstrdup (buf[i].f_mntfromname);
445 new->type = sstrdup (buf[i].f_fstypename);
447 new->device = get_device_name (new->options);
467 /* #endif HAVE_GETFSSTAT */
469 /* Solaris (SunOS 10): int getmntent(FILE *fp, struct mnttab *mp); */
470 #elif HAVE_GEN_GETMNTENT
471 static cu_mount_t *cu_mount_gen_getmntent (void)
476 cu_mount_t *first = NULL;
477 cu_mount_t *last = NULL;
478 cu_mount_t *new = NULL;
480 if ((fp = fopen (MNTTAB, "r")) == NULL)
483 while (getmntent (fp, &mt) == 0)
485 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
487 memset (new, '\0', sizeof (cu_mount_t));
489 /* Copy values from `struct mnttab' */
490 new->dir = sstrdup (mt.mnt_mountp);
491 new->spec_device = sstrdup (mt.mnt_special);
492 new->type = sstrdup (mt.mnt_fstype);
493 new->options = sstrdup (mt.mnt_mntopts);
494 new->device = get_device_name (new->options);
513 } /* static cu_mount_t *cu_mount_gen_getmntent (void) */
514 /* #endif HAVE_GEN_GETMNTENT */
516 #elif HAVE_SEQ_GETMNTENT
517 #warn "This version of `getmntent' hat not yet been implemented!"
518 /* #endif HAVE_SEQ_GETMNTENT */
520 #elif HAVE_SUN_GETMNTENT
521 #warn "This version of `getmntent' hat not yet been implemented!"
522 /* #endif HAVE_SUN_GETMNTENT */
525 static cu_mount_t *cu_mount_getmntent (void)
530 cu_mount_t *first = NULL;
531 cu_mount_t *last = NULL;
532 cu_mount_t *new = NULL;
534 if ((fp = setmntent (MNTTAB, "r")) == NULL)
537 while ((me = getmntent (fp)) != NULL)
539 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
541 memset (new, '\0', sizeof (cu_mount_t));
543 /* Copy values from `struct mntent *' */
544 new->dir = sstrdup (me->mnt_dir);
545 new->spec_device = sstrdup (me->mnt_fsname);
546 new->type = sstrdup (me->mnt_type);
547 new->options = sstrdup (me->mnt_opts);
548 new->device = get_device_name (new->options);
568 #endif /* HAVE_GETMNTENT */
570 /* *** *** *** ******************************************** *** *** *** */
571 /* *** *** *** *** *** *** public functions *** *** *** *** *** *** */
572 /* *** *** *** ******************************************** *** *** *** */
574 cu_mount_t *cu_mount_getlist(cu_mount_t **list)
577 cu_mount_t *first = NULL;
578 cu_mount_t *last = NULL;
587 while (last->next != NULL)
591 #if HAVE_LISTMNTENT && 0
592 new = cu_mount_listmntent ();
594 new = cu_mount_getfsstat ();
595 #elif HAVE_GEN_GETMNTENT
596 new = cu_mount_gen_getmntent ();
598 new = cu_mount_getmntent ();
614 while ((last != NULL) && (last->next != NULL))
618 } /* cu_mount_t *cu_mount_getlist(cu_mount_t **list) */
620 void cu_mount_freelist (cu_mount_t *list)
625 for (this = list; this != NULL; this = next)
630 sfree (this->spec_device);
631 sfree (this->device);
633 sfree (this->options);
636 } /* void cu_mount_freelist(cu_mount_t *list) */
639 cu_mount_checkoption(char *line, char *keyword, int full)
642 int l = strlen(keyword);
645 if(line == NULL || keyword == NULL) {
652 line2 = sstrdup(line);
662 p2 = strchr(line, ',');
664 if(strncmp(line2+(p1-line)+1, keyword, l+full) == 0) {
670 p2 = strchr(p1+1, ',');
676 } /* char *cu_mount_checkoption(char *line, char *keyword, int full) */
679 cu_mount_getoptionvalue(char *line, char *keyword)
683 r = cu_mount_checkoption(line, keyword, 0);
686 r += strlen(keyword);
698 m = (char *)smalloc(p-r+1);
699 sstrncpy(m, r, p-r+1);
704 } /* char *cu_mount_getoptionvalue(char *line, char *keyword) */
709 cu_mount_type(const char *type)
711 if(strcmp(type, "ext3") == 0) return CUMT_EXT3;
712 if(strcmp(type, "ext2") == 0) return CUMT_EXT2;
713 if(strcmp(type, "ufs") == 0) return CUMT_UFS;
714 if(strcmp(type, "vxfs") == 0) return CUMT_VXFS;
715 if(strcmp(type, "zfs") == 0) return CUMT_ZFS;
717 } /* int cu_mount_type(const char *type) */