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