Merged 516:518 from `trunk' to `tags/collectd-3.8.2'
[collectd.git] / src / utils_mount.c
1 /**
2  * collectd - src/utils_mount.c
3  * Copyright (C) 2005  Niki W. Waibel
4  *
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.
9  *
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.
14  *
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,
18  * USA.
19  *
20  * Author:
21  *   Niki W. Waibel <niki.waibel@gmx.net>
22 **/
23
24
25
26 #include "common.h"
27 #if HAVE_XFS_XQM_H
28 # include <xfs/xqm.h>
29 #define XFS_SUPER_MAGIC_STR "XFSB"
30 #define XFS_SUPER_MAGIC2_STR "BSFX"
31 #endif
32
33 #include "utils_debug.h"
34 #include "utils_mount.h"
35
36 #if HAVE_GETFSSTAT
37 #  if HAVE_SYS_PARAM_H
38 #    include <sys/param.h>
39 #  endif
40 #  if HAVE_SYS_UCRED_H
41 #    include <sys/ucred.h>
42 #  endif
43 #  if HAVE_SYS_MOUNT_H
44 #    include <sys/mount.h>
45 #  endif
46 #endif /* HAVE_GETMNTINFO */
47
48 #if HAVE_MNTENT_H
49 #  include <mntent.h>
50 #endif
51 #if HAVE_SYS_MNTTAB_H
52 #  include <sys/mnttab.h>
53 #endif
54
55 #if HAVE_PATHS_H
56 #  include <paths.h>
57 #endif
58
59 #ifdef COLLECTD_MNTTAB
60 #  undef COLLECTD_MNTTAB
61 #endif
62
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
71 #elif defined(KMTAB)
72 #  define COLLECTD_MNTTAB KMTAB
73 #else
74 #  define COLLECTD_MNTTAB "/etc/mnttab"
75 #endif
76
77 /* *** *** *** ********************************************* *** *** *** */
78 /* *** *** *** *** *** ***   private functions   *** *** *** *** *** *** */
79 /* *** *** *** ********************************************* *** *** *** */
80
81 /* stolen from quota-3.13 (quota-tools) */
82
83 #define PROC_PARTITIONS "/proc/partitions"
84 #define DEVLABELDIR     "/dev"
85 #define UUID   1
86 #define VOL    2
87
88 static struct uuidCache_s {
89         struct uuidCache_s *next;
90         char uuid[16];
91         char *label;
92         char *device;
93 } *uuidCache = NULL;
94
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];
102 };
103 #define ext2magic(s) ((unsigned int)s.s_magic[0] \
104         + (((unsigned int)s.s_magic[1]) << 8))
105
106 #if HAVE_XFS_XQM_H
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];
112         char s_fsname[12];
113 };
114 #endif /* HAVE_XFS_XQM_H */
115
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];
123 };
124
125 /* for now, only ext2 and xfs are supported */
126 static int
127 get_label_uuid(const char *device, char **label, char *uuid)
128 {
129         /* start with ext2 and xfs tests, taken from mount_guess_fstype */
130         /* should merge these later */
131         int fd, rv = 1;
132         size_t namesize;
133         struct ext2_super_block e2sb;
134 #if HAVE_XFS_XQM_H
135         struct xfs_super_block xfsb;
136 #endif
137         struct reiserfs_super_block reisersb;
138
139         fd = open(device, O_RDONLY);
140         if(fd == -1) {
141                 return rv;
142         }
143
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);
151                 rv = 0;
152 #if HAVE_XFS_XQM_H
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);
161                 rv = 0;
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);
170                 rv = 0;
171         }
172         close(fd);
173         return rv;
174 }
175
176 static void
177 uuidcache_addentry(char *device, char *label, char *uuid)
178 {
179         struct uuidCache_s *last;
180
181         if(!uuidCache) {
182                 last = uuidCache = smalloc(sizeof(*uuidCache));
183         } else {
184                 for(last = uuidCache; last->next; last = last->next);
185                 last->next = smalloc(sizeof(*uuidCache));
186                 last = last->next;
187         }
188         last->next = NULL;
189         last->device = device;
190         last->label = label;
191         memcpy(last->uuid, uuid, sizeof(last->uuid));
192 }
193
194 static void
195 uuidcache_init(void)
196 {
197         char line[100];
198         char *s;
199         int ma, mi, sz;
200         static char ptname[100];
201         FILE *procpt;
202         char uuid[16], *label = NULL;
203         char device[110];
204         int firstPass;
205         int handleOnFirst;
206
207         if(uuidCache) {
208                 return;
209         }
210
211         procpt = fopen(PROC_PARTITIONS, "r");
212         if(procpt == NULL) {
213                 return;
214         }
215
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)
221                         {
222                                 continue;
223                         }
224
225                         /* skip extended partitions (heuristic: size 1) */
226                         if(sz == 1) {
227                                 continue;
228                         }
229
230                         /* look only at md devices on first pass */
231                         handleOnFirst = !strncmp(ptname, "md", 2);
232                         if(firstPass != handleOnFirst) {
233                                 continue;
234                         }
235
236                         /* skip entire disk (minor 0, 64, ... on ide;
237                         0, 16, ... on sd) */
238                         /* heuristic: partition name ends in a digit */
239
240                         for(s = ptname; *s; s++);
241
242                         if(isdigit((int)s[-1])) {
243                         /*
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
250                         * be accessed.)
251                         */
252                                 snprintf(device, sizeof(device), "%s/%s",
253                                         DEVLABELDIR, ptname);
254                                 if(!get_label_uuid(device, &label, uuid)) {
255                                         uuidcache_addentry(sstrdup(device),
256                                                 label, uuid);
257                                 }
258                         }
259                 }
260         }
261         fclose(procpt);
262 }
263
264 static unsigned char
265 fromhex(char c)
266 {
267         if(isdigit((int)c)) {
268                 return (c - '0');
269         } else if(islower((int)c)) {
270                 return (c - 'a' + 10);
271         } else {
272                 return (c - 'A' + 10);
273         }
274 }
275
276 static char *
277 get_spec_by_x(int n, const char *t)
278 {
279         struct uuidCache_s *uc;
280
281         uuidcache_init();
282         uc = uuidCache;
283
284         while(uc) {
285                 switch(n) {
286                 case UUID:
287                         if(!memcmp(t, uc->uuid, sizeof(uc->uuid))) {
288                                 return sstrdup(uc->device);
289                         }
290                         break;
291                 case VOL:
292                         if(!strcmp(t, uc->label)) {
293                                 return sstrdup(uc->device);
294                         }
295                         break;
296                 }
297                 uc = uc->next;
298         }
299         return NULL;
300 }
301
302 static char *
303 get_spec_by_uuid(const char *s)
304 {
305         char uuid[16];
306         int i;
307
308         if(strlen(s) != 36
309         || s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-') {
310                 goto bad_uuid;
311         }
312
313         for(i=0; i<16; i++) {
314                 if(*s == '-') {
315                         s++;
316                 }
317                 if(!isxdigit((int)s[0]) || !isxdigit((int)s[1])) {
318                         goto bad_uuid;
319                 }
320                 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
321                 s += 2;
322         }
323         return get_spec_by_x(UUID, uuid);
324
325         bad_uuid:
326                 DBG("Found an invalid UUID: %s", s);
327         return NULL;
328 }
329
330 static char *get_spec_by_volume_label(const char *s)
331 {
332         return get_spec_by_x (VOL, s);
333 }
334
335 static char *get_device_name(const char *optstr)
336 {
337         char *rc;
338
339         if (optstr == NULL)
340         {
341                 return (NULL);
342         }
343         else if (strncmp (optstr, "UUID=", 5) == 0)
344         {
345                 DBG ("TODO: check UUID= code!");
346                 rc = get_spec_by_uuid (optstr + 5);
347         }
348         else if (strncmp (optstr, "LABEL=", 6) == 0)
349         {
350                 DBG ("TODO: check LABEL= code!");
351                 rc = get_spec_by_volume_label (optstr + 6);
352         }
353         else
354         {
355                 rc = sstrdup (optstr);
356         }
357
358         if(!rc)
359         {
360                 DBG ("Error checking device name: optstr = %s", optstr);
361         }
362         return rc;
363 }
364
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)
368 {
369         cu_mount_t *last = *list;
370         struct tabmntent *p;
371         struct mntent *mnt;
372
373         struct tabmntent *mntlist;
374         if(listmntent(&mntlist, COLLECTD_MNTTAB, NULL, NULL) < 0) {
375                 DBG("calling listmntent() failed: %s", strerror(errno));
376         }
377
378         for(p = mntlist; p; p = p->next) {
379                 char *loop = NULL, *device = NULL;
380
381                 mnt = p->ment;
382                 loop = cu_mount_getoptionvalue(mnt->mnt_opts, "loop=");
383                 if(loop == NULL) {   /* no loop= mount */
384                         device = get_device_name(mnt->mnt_fsname);
385                         if(device == NULL) {
386                                 DBG("can't get devicename for fs (%s) %s (%s)"
387                                         ": ignored", mnt->mnt_type,
388                                         mnt->mnt_dir, mnt->mnt_fsname);
389                                 continue;
390                         }
391                 } else {
392                         device = loop;
393                 }
394                 if(*list == NULL) {
395                         *list = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
396                         last = *list;
397                 } else {
398                         while(last->next != NULL) { /* is last really last? */
399                                 last = last->next;
400                         }
401                         last->next = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
402                         last = last->next;
403                 }
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);
409                 last->next = NULL;
410         } /* for(p = mntlist; p; p = p->next) */
411
412         return(last);
413 } /* cu_mount_t *cu_mount_listmntent(void) */
414 /* #endif HAVE_LISTMNTENT */
415
416 /* 4.4BSD and Mac OS X */
417 #elif HAVE_GETFSSTAT
418 static cu_mount_t *cu_mount_getfsstat (void)
419 {
420         int bufsize;
421         struct statfs *buf;
422
423         int num;
424         int i;
425
426         cu_mount_t *first = NULL;
427         cu_mount_t *last  = NULL;
428         cu_mount_t *new   = NULL;
429
430         /* Get the number of mounted file systems */
431         if ((bufsize = getfsstat (NULL, 0, MNT_NOWAIT)) < 1)
432                 return (NULL);
433
434         if ((buf = (struct statfs *) malloc (bufsize * sizeof (struct statfs))) == NULL)
435                 return (NULL);
436         memset (buf, '\0', bufsize * sizeof (struct statfs));
437
438         /* FIXME: If `bufsize' in bytes or structures? */
439         if ((num = getfsstat (buf, bufsize, MNT_NOWAIT)) < 1)
440         {
441                 free (buf);
442                 return (NULL);
443         }
444
445         for (i = 0; i < num; i++)
446         {
447                 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
448                         break;
449                 memset (new, '\0', sizeof (cu_mount_t));
450                 
451                 /* Copy values from `struct mnttab' */
452                 new->dir         = sstrdup (buf[i].f_mntonname);
453                 new->spec_device = sstrdup (buf[i].f_mntfromname);
454                 new->type        = sstrdup (buf[i].f_fstypename);
455                 new->options     = NULL;
456                 new->device      = get_device_name (new->options);
457                 new->next = NULL;
458
459                 /* Append to list */
460                 if (first == NULL)
461                 {
462                         first = new;
463                         last  = new;
464                 }
465                 else
466                 {
467                         last->next = new;
468                         last       = new;
469                 }
470         }
471
472         free (buf);
473
474         return (first);
475 }
476 /* #endif HAVE_GETFSSTAT */
477
478 /* Solaris (SunOS 10): int getmntent(FILE *fp, struct mnttab *mp); */
479 #elif HAVE_GEN_GETMNTENT
480 static cu_mount_t *cu_mount_gen_getmntent (void)
481 {
482         struct mnttab mt;
483         FILE *fp;
484
485         cu_mount_t *first = NULL;
486         cu_mount_t *last  = NULL;
487         cu_mount_t *new   = NULL;
488
489         DBG ("(void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
490
491         if ((fp = fopen (COLLECTD_MNTTAB, "r")) == NULL)
492         {
493                 syslog (LOG_ERR, "fopen (%s): %s", COLLECTD_MNTTAB, strerror (errno));
494                 return (NULL);
495         }
496
497         while (getmntent (fp, &mt) == 0)
498         {
499                 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
500                         break;
501                 memset (new, '\0', sizeof (cu_mount_t));
502                 
503                 /* Copy values from `struct mnttab' */
504                 new->dir         = sstrdup (mt.mnt_mountp);
505                 new->spec_device = sstrdup (mt.mnt_special);
506                 new->type        = sstrdup (mt.mnt_fstype);
507                 new->options     = sstrdup (mt.mnt_mntopts);
508                 new->device      = get_device_name (new->options);
509                 new->next = NULL;
510
511                 /* Append to list */
512                 if (first == NULL)
513                 {
514                         first = new;
515                         last  = new;
516                 }
517                 else
518                 {
519                         last->next = new;
520                         last       = new;
521                 }
522         }
523
524         fclose (fp);
525
526         return (first);
527 } /* static cu_mount_t *cu_mount_gen_getmntent (void) */
528 /* #endif HAVE_GEN_GETMNTENT */
529
530 #elif HAVE_SEQ_GETMNTENT
531 #warn "This version of `getmntent' hat not yet been implemented!"
532 /* #endif HAVE_SEQ_GETMNTENT */
533
534 #elif HAVE_SUN_GETMNTENT
535 #warn "This version of `getmntent' hat not yet been implemented!"
536 /* #endif HAVE_SUN_GETMNTENT */
537
538 #elif HAVE_GETMNTENT
539 static cu_mount_t *cu_mount_getmntent (void)
540 {
541         FILE *fp;
542         struct mntent *me;
543
544         cu_mount_t *first = NULL;
545         cu_mount_t *last  = NULL;
546         cu_mount_t *new   = NULL;
547
548         DBG ("(void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
549
550         if ((fp = setmntent (COLLECTD_MNTTAB, "r")) == NULL)
551         {
552                 syslog (LOG_ERR, "setmntent (%s): %s", COLLECTD_MNTTAB, strerror (errno));
553                 return (NULL);
554         }
555
556         while ((me = getmntent (fp)) != NULL)
557         {
558                 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
559                         break;
560                 memset (new, '\0', sizeof (cu_mount_t));
561                 
562                 /* Copy values from `struct mntent *' */
563                 new->dir         = sstrdup (me->mnt_dir);
564                 new->spec_device = sstrdup (me->mnt_fsname);
565                 new->type        = sstrdup (me->mnt_type);
566                 new->options     = sstrdup (me->mnt_opts);
567                 new->device      = get_device_name (new->options);
568                 new->next        = NULL;
569
570                 DBG ("new = {dir = %s, spec_device = %s, type = %s, options = %s, device = %s}",
571                                 new->dir, new->spec_device, new->type, new->options, new->device);
572
573                 /* Append to list */
574                 if (first == NULL)
575                 {
576                         first = new;
577                         last  = new;
578                 }
579                 else
580                 {
581                         last->next = new;
582                         last       = new;
583                 }
584         }
585
586         endmntent (fp);
587
588         DBG ("return (0x%p)", (void *) first);
589
590         return (first);
591 }
592 #endif /* HAVE_GETMNTENT */
593
594 /* *** *** *** ******************************************** *** *** *** */
595 /* *** *** *** *** *** ***   public functions   *** *** *** *** *** *** */
596 /* *** *** *** ******************************************** *** *** *** */
597
598 cu_mount_t *cu_mount_getlist(cu_mount_t **list)
599 {
600         cu_mount_t *new;
601         cu_mount_t *first = NULL;
602         cu_mount_t *last  = NULL;
603
604         if (list == NULL)
605                 return (NULL);
606
607         if (*list != NULL)
608         {
609                 first = *list;
610                 last  =  first;
611                 while (last->next != NULL)
612                         last = last->next;
613         }
614
615 #if HAVE_LISTMNTENT && 0
616         new = cu_mount_listmntent ();
617 #elif HAVE_GETFSSTAT
618         new = cu_mount_getfsstat ();
619 #elif HAVE_GEN_GETMNTENT
620         new = cu_mount_gen_getmntent ();
621 #elif HAVE_GETMNTENT
622         new = cu_mount_getmntent ();
623 #else
624         new = NULL;
625 #endif
626
627         if (first != NULL)
628         {
629                 last->next = new;
630         }
631         else
632         {
633                 first = new;
634                 last  = new;
635                 *list = first;
636         }
637
638         while ((last != NULL) && (last->next != NULL))
639                 last = last->next;
640
641         return (last);
642 } /* cu_mount_t *cu_mount_getlist(cu_mount_t **list) */
643
644 void cu_mount_freelist (cu_mount_t *list)
645 {
646         cu_mount_t *this;
647         cu_mount_t *next;
648
649         DBG ("(list = 0x%p)", (void *) list);
650
651         for (this = list; this != NULL; this = next)
652         {
653                 next = this->next;
654
655                 sfree (this->dir);
656                 sfree (this->spec_device);
657                 sfree (this->device);
658                 sfree (this->type);
659                 sfree (this->options);
660                 sfree (this);
661         }
662 } /* void cu_mount_freelist(cu_mount_t *list) */
663
664 char *
665 cu_mount_checkoption(char *line, char *keyword, int full)
666 {
667         char *line2, *l2;
668         int l = strlen(keyword);
669         char *p1, *p2;
670
671         if(line == NULL || keyword == NULL) {
672                 return NULL;
673         }
674         if(full != 0) {
675                 full = 1;
676         }
677
678         line2 = sstrdup(line);
679         l2 = line2;
680         while(*l2 != '\0') {
681                 if(*l2 == ',') {
682                         *l2 = '\0';
683                 }
684                 l2++;
685         }
686
687         p1 = line - 1;
688         p2 = strchr(line, ',');
689         do {
690                 if(strncmp(line2+(p1-line)+1, keyword, l+full) == 0) {
691                         free(line2);
692                         return p1+1;
693                 }
694                 p1 = p2;
695                 if(p1 != NULL) {
696                         p2 = strchr(p1+1, ',');
697                 }
698         } while(p1 != NULL);
699
700         free(line2);
701         return NULL;
702 } /* char *cu_mount_checkoption(char *line, char *keyword, int full) */
703
704 char *
705 cu_mount_getoptionvalue(char *line, char *keyword)
706 {
707         char *r;
708
709         r = cu_mount_checkoption(line, keyword, 0);
710         if(r != NULL) {
711                 char *p;
712                 r += strlen(keyword);
713                 p = strchr(r, ',');
714                 if(p == NULL) {
715                         if(strlen(r) == 0) {
716                                 return NULL;
717                         }
718                         return sstrdup(r);
719                 } else {
720                         char *m;
721                         if((p-r) == 1) {
722                                 return NULL;
723                         }
724                         m = (char *)smalloc(p-r+1);
725                         sstrncpy(m, r, p-r+1);
726                         return m;
727                 }
728         }
729         return r;
730 } /* char *cu_mount_getoptionvalue(char *line, char *keyword) */
731
732
733
734 int
735 cu_mount_type(const char *type)
736 {
737         if(strcmp(type, "ext3") == 0) return CUMT_EXT3;
738         if(strcmp(type, "ext2") == 0) return CUMT_EXT2;
739         if(strcmp(type, "ufs")  == 0) return CUMT_UFS;
740         if(strcmp(type, "vxfs") == 0) return CUMT_VXFS;
741         if(strcmp(type, "zfs")  == 0) return CUMT_ZFS;
742         return CUMT_UNKNOWN;
743 } /* int cu_mount_type(const char *type) */
744
745
746