Merge remote-tracking branch 'github/pr/1909' into collectd-5.5
[collectd.git] / src / utils_mount.c
1 /**
2  * collectd - src/utils_mount.c
3  * Copyright (C) 2005,2006  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 License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Author:
20  *   Niki W. Waibel <niki.waibel@gmx.net>
21 **/
22
23 #if HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #define _GNU_SOURCE
28
29 #include "collectd.h"
30 #include "utils_mount.h"
31
32 #if HAVE_XFS_XQM_H
33 # include <xfs/xqm.h>
34 #define XFS_SUPER_MAGIC_STR "XFSB"
35 #define XFS_SUPER_MAGIC2_STR "BSFX"
36 #endif
37
38 #include "common.h" /* sstrncpy() et alii */
39 #include "plugin.h" /* ERROR() macro */
40
41
42 #if HAVE_GETVFSSTAT
43 #  if HAVE_SYS_TYPES_H
44 #    include <sys/types.h>
45 #  endif
46 #  if HAVE_SYS_STATVFS_H
47 #    include <sys/statvfs.h>
48 #  endif
49 /* #endif HAVE_GETVFSSTAT */
50
51 #elif HAVE_GETFSSTAT
52 #  if HAVE_SYS_PARAM_H
53 #    include <sys/param.h>
54 #  endif
55 #  if HAVE_SYS_UCRED_H
56 #    include <sys/ucred.h>
57 #  endif
58 #  if HAVE_SYS_MOUNT_H
59 #    include <sys/mount.h>
60 #  endif
61 #endif /* HAVE_GETFSSTAT */
62
63 #if HAVE_MNTENT_H
64 #  include <mntent.h>
65 #endif
66 #if HAVE_SYS_MNTTAB_H
67 #  include <sys/mnttab.h>
68 #endif
69
70 #if HAVE_PATHS_H
71 #  include <paths.h>
72 #endif
73
74 #ifdef COLLECTD_MNTTAB
75 #  undef COLLECTD_MNTTAB
76 #endif
77
78 #if defined(_PATH_MOUNTED) /* glibc */
79 #  define COLLECTD_MNTTAB _PATH_MOUNTED
80 #elif defined(MNTTAB) /* Solaris */
81 #  define COLLECTD_MNTTAB MNTTAB
82 #elif defined(MNT_MNTTAB)
83 #  define COLLECTD_MNTTAB MNT_MNTTAB
84 #elif defined(MNTTABNAME)
85 #  define COLLECTD_MNTTAB MNTTABNAME
86 #elif defined(KMTAB)
87 #  define COLLECTD_MNTTAB KMTAB
88 #else
89 #  define COLLECTD_MNTTAB "/etc/mnttab"
90 #endif
91
92 /* *** *** *** ********************************************* *** *** *** */
93 /* *** *** *** *** *** ***   private functions   *** *** *** *** *** *** */
94 /* *** *** *** ********************************************* *** *** *** */
95
96 /* stolen from quota-3.13 (quota-tools) */
97
98 #define PROC_PARTITIONS "/proc/partitions"
99 #define DEVLABELDIR     "/dev"
100 #define UUID   1
101 #define VOL    2
102
103 static struct uuidCache_s {
104         struct uuidCache_s *next;
105         char uuid[16];
106         char *label;
107         char *device;
108 } *uuidCache = NULL;
109
110 #define EXT2_SUPER_MAGIC 0xEF53
111 struct ext2_super_block {
112         unsigned char s_dummy1[56];
113         unsigned char s_magic[2];
114         unsigned char s_dummy2[46];
115         unsigned char s_uuid[16];
116         char s_volume_name[16];
117 };
118 #define ext2magic(s) ((unsigned int)s.s_magic[0] \
119         + (((unsigned int)s.s_magic[1]) << 8))
120
121 #if HAVE_XFS_XQM_H
122 struct xfs_super_block {
123         unsigned char s_magic[4];
124         unsigned char s_dummy[28];
125         unsigned char s_uuid[16];
126         unsigned char s_dummy2[60];
127         char s_fsname[12];
128 };
129 #endif /* HAVE_XFS_XQM_H */
130
131 #define REISER_SUPER_MAGIC "ReIsEr2Fs"
132 struct reiserfs_super_block {
133         unsigned char s_dummy1[52];
134         unsigned char s_magic[10];
135         unsigned char s_dummy2[22];
136         unsigned char s_uuid[16];
137         char s_volume_name[16];
138 };
139
140 /* for now, only ext2 and xfs are supported */
141 static int
142 get_label_uuid(const char *device, char **label, char *uuid)
143 {
144         /* start with ext2 and xfs tests, taken from mount_guess_fstype */
145         /* should merge these later */
146         int fd, rv = 1;
147         size_t namesize;
148         struct ext2_super_block e2sb;
149 #if HAVE_XFS_XQM_H
150         struct xfs_super_block xfsb;
151 #endif
152         struct reiserfs_super_block reisersb;
153
154         fd = open(device, O_RDONLY);
155         if(fd == -1) {
156                 return rv;
157         }
158
159         if(lseek(fd, 1024, SEEK_SET) == 1024
160         && read(fd, (char *)&e2sb, sizeof(e2sb)) == sizeof(e2sb)
161         && ext2magic(e2sb) == EXT2_SUPER_MAGIC) {
162                 memcpy(uuid, e2sb.s_uuid, sizeof(e2sb.s_uuid));
163                 namesize = sizeof(e2sb.s_volume_name);
164                 *label = smalloc(namesize + 1);
165                 sstrncpy(*label, e2sb.s_volume_name, namesize);
166                 rv = 0;
167 #if HAVE_XFS_XQM_H
168         } else if(lseek(fd, 0, SEEK_SET) == 0
169         && read(fd, (char *)&xfsb, sizeof(xfsb)) == sizeof(xfsb)
170         && (strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC_STR, 4) == 0 ||
171         strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC2_STR, 4) == 0)) {
172                 memcpy(uuid, xfsb.s_uuid, sizeof(xfsb.s_uuid));
173                 namesize = sizeof(xfsb.s_fsname);
174                 *label = smalloc(namesize + 1);
175                 sstrncpy(*label, xfsb.s_fsname, namesize);
176                 rv = 0;
177 #endif /* HAVE_XFS_XQM_H */
178         } else if(lseek(fd, 65536, SEEK_SET) == 65536
179         && read(fd, (char *)&reisersb, sizeof(reisersb)) == sizeof(reisersb)
180         && !strncmp((char *)&reisersb.s_magic, REISER_SUPER_MAGIC, 9)) {
181                 memcpy(uuid, reisersb.s_uuid, sizeof(reisersb.s_uuid));
182                 namesize = sizeof(reisersb.s_volume_name);
183                 *label = smalloc(namesize + 1);
184                 sstrncpy(*label, reisersb.s_volume_name, namesize);
185                 rv = 0;
186         }
187         close(fd);
188         return rv;
189 }
190
191 static void
192 uuidcache_addentry(char *device, char *label, char *uuid)
193 {
194         struct uuidCache_s *last;
195
196         if(!uuidCache) {
197                 last = uuidCache = smalloc(sizeof(*uuidCache));
198         } else {
199                 for(last = uuidCache; last->next; last = last->next);
200                 last->next = smalloc(sizeof(*uuidCache));
201                 last = last->next;
202         }
203         last->next = NULL;
204         last->device = device;
205         last->label = label;
206         memcpy(last->uuid, uuid, sizeof(last->uuid));
207 }
208
209 static void
210 uuidcache_init(void)
211 {
212         char line[100];
213         char *s;
214         int ma, mi, sz;
215         static char ptname[100];
216         FILE *procpt;
217         char uuid[16], *label = NULL;
218         char device[110];
219         int firstPass;
220         int handleOnFirst;
221
222         if(uuidCache) {
223                 return;
224         }
225
226         procpt = fopen(PROC_PARTITIONS, "r");
227         if(procpt == NULL) {
228                 return;
229         }
230
231         for(firstPass = 1; firstPass >= 0; firstPass--) {
232                 fseek(procpt, 0, SEEK_SET);
233                 while(fgets(line, sizeof(line), procpt)) {
234                         if(sscanf(line, " %d %d %d %[^\n ]",
235                                 &ma, &mi, &sz, ptname) != 4)
236                         {
237                                 continue;
238                         }
239
240                         /* skip extended partitions (heuristic: size 1) */
241                         if(sz == 1) {
242                                 continue;
243                         }
244
245                         /* look only at md devices on first pass */
246                         handleOnFirst = !strncmp(ptname, "md", 2);
247                         if(firstPass != handleOnFirst) {
248                                 continue;
249                         }
250
251                         /* skip entire disk (minor 0, 64, ... on ide;
252                         0, 16, ... on sd) */
253                         /* heuristic: partition name ends in a digit */
254
255                         for(s = ptname; *s; s++);
256
257                         if(isdigit((int)s[-1])) {
258                         /*
259                         * Note: this is a heuristic only - there is no reason
260                         * why these devices should live in /dev.
261                         * Perhaps this directory should be specifiable by option.
262                         * One might for example have /devlabel with links to /dev
263                         * for the devices that may be accessed in this way.
264                         * (This is useful, if the cdrom on /dev/hdc must not
265                         * be accessed.)
266                         */
267                                 ssnprintf(device, sizeof(device), "%s/%s",
268                                         DEVLABELDIR, ptname);
269                                 if(!get_label_uuid(device, &label, uuid)) {
270                                         uuidcache_addentry(sstrdup(device),
271                                                 label, uuid);
272                                 }
273                         }
274                 }
275         }
276         fclose(procpt);
277 }
278
279 static unsigned char
280 fromhex(char c)
281 {
282         if(isdigit((int)c)) {
283                 return (c - '0');
284         } else if(islower((int)c)) {
285                 return (c - 'a' + 10);
286         } else {
287                 return (c - 'A' + 10);
288         }
289 }
290
291 static char *
292 get_spec_by_x(int n, const char *t)
293 {
294         struct uuidCache_s *uc;
295
296         uuidcache_init();
297         uc = uuidCache;
298
299         while(uc) {
300                 switch(n) {
301                 case UUID:
302                         if(!memcmp(t, uc->uuid, sizeof(uc->uuid))) {
303                                 return sstrdup(uc->device);
304                         }
305                         break;
306                 case VOL:
307                         if(!strcmp(t, uc->label)) {
308                                 return sstrdup(uc->device);
309                         }
310                         break;
311                 }
312                 uc = uc->next;
313         }
314         return NULL;
315 }
316
317 static char *
318 get_spec_by_uuid(const char *s)
319 {
320         char uuid[16];
321         int i;
322
323         if(strlen(s) != 36
324         || s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-') {
325                 goto bad_uuid;
326         }
327
328         for(i=0; i<16; i++) {
329                 if(*s == '-') {
330                         s++;
331                 }
332                 if(!isxdigit((int)s[0]) || !isxdigit((int)s[1])) {
333                         goto bad_uuid;
334                 }
335                 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
336                 s += 2;
337         }
338         return get_spec_by_x(UUID, uuid);
339
340         bad_uuid:
341                 DEBUG("utils_mount: Found an invalid UUID: %s", s);
342         return NULL;
343 }
344
345 static char *get_spec_by_volume_label(const char *s)
346 {
347         return get_spec_by_x (VOL, s);
348 }
349
350 static char *get_device_name(const char *optstr)
351 {
352         char *rc;
353
354         if (optstr == NULL)
355         {
356                 return (NULL);
357         }
358         else if (strncmp (optstr, "UUID=", 5) == 0)
359         {
360                 DEBUG ("utils_mount: TODO: check UUID= code!");
361                 rc = get_spec_by_uuid (optstr + 5);
362         }
363         else if (strncmp (optstr, "LABEL=", 6) == 0)
364         {
365                 DEBUG ("utils_mount: TODO: check LABEL= code!");
366                 rc = get_spec_by_volume_label (optstr + 6);
367         }
368         else
369         {
370                 rc = sstrdup (optstr);
371         }
372
373         if(!rc)
374         {
375                 DEBUG ("utils_mount: Error checking device name: optstr = %s", optstr);
376         }
377         return rc;
378 }
379
380 /* What weird OS is this..? I can't find any info with google :/ -octo */
381 #if HAVE_LISTMNTENT && 0
382 static cu_mount_t *cu_mount_listmntent (void)
383 {
384         cu_mount_t *last = *list;
385         struct tabmntent *p;
386         struct mntent *mnt;
387
388         struct tabmntent *mntlist;
389         if(listmntent(&mntlist, COLLECTD_MNTTAB, NULL, NULL) < 0) {
390 #if COLLECT_DEBUG
391                 char errbuf[1024];
392                 DEBUG("utils_mount: calling listmntent() failed: %s",
393                                 sstrerror (errno, errbuf, sizeof (errbuf)));
394 #endif /* COLLECT_DEBUG */
395         }
396
397         for(p = mntlist; p; p = p->next) {
398                 char *loop = NULL, *device = NULL;
399
400                 mnt = p->ment;
401                 loop = cu_mount_getoptionvalue(mnt->mnt_opts, "loop=");
402                 if(loop == NULL) {   /* no loop= mount */
403                         device = get_device_name(mnt->mnt_fsname);
404                         if(device == NULL) {
405                                 DEBUG("utils_mount: can't get devicename for fs (%s) %s (%s)"
406                                         ": ignored", mnt->mnt_type,
407                                         mnt->mnt_dir, mnt->mnt_fsname);
408                                 continue;
409                         }
410                 } else {
411                         device = loop;
412                 }
413                 if(*list == NULL) {
414                         *list = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
415                         last = *list;
416                 } else {
417                         while(last->next != NULL) { /* is last really last? */
418                                 last = last->next;
419                         }
420                         last->next = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
421                         last = last->next;
422                 }
423                 last->dir = sstrdup(mnt->mnt_dir);
424                 last->spec_device = sstrdup(mnt->mnt_fsname);
425                 last->device = device;
426                 last->type = sstrdup(mnt->mnt_type);
427                 last->options = sstrdup(mnt->mnt_opts);
428                 last->next = NULL;
429         } /* for(p = mntlist; p; p = p->next) */
430
431         return(last);
432 } /* cu_mount_t *cu_mount_listmntent(void) */
433 /* #endif HAVE_LISTMNTENT */
434
435 /* 4.4BSD and Mac OS X (getfsstat) or NetBSD (getvfsstat) */
436 #elif HAVE_GETVFSSTAT || HAVE_GETFSSTAT
437 static cu_mount_t *cu_mount_getfsstat (void)
438 {
439 #if HAVE_GETFSSTAT
440 #  define STRUCT_STATFS struct statfs
441 #  define CMD_STATFS    getfsstat
442 #  define FLAGS_STATFS  MNT_NOWAIT
443 /* #endif HAVE_GETFSSTAT */
444 #elif HAVE_GETVFSSTAT
445 #  define STRUCT_STATFS struct statvfs
446 #  define CMD_STATFS    getvfsstat
447 #  define FLAGS_STATFS  ST_NOWAIT
448 #endif /* HAVE_GETVFSSTAT */
449
450         int bufsize;
451         STRUCT_STATFS *buf;
452
453         int num;
454         int i;
455
456         cu_mount_t *first = NULL;
457         cu_mount_t *last  = NULL;
458         cu_mount_t *new   = NULL;
459
460         /* Get the number of mounted file systems */
461         if ((bufsize = CMD_STATFS (NULL, 0, FLAGS_STATFS)) < 1)
462         {
463 #if COLLECT_DEBUG
464                 char errbuf[1024];
465                 DEBUG ("utils_mount: getv?fsstat failed: %s",
466                                 sstrerror (errno, errbuf, sizeof (errbuf)));
467 #endif /* COLLECT_DEBUG */
468                 return (NULL);
469         }
470
471         if ((buf = (STRUCT_STATFS *) malloc (bufsize * sizeof (STRUCT_STATFS)))
472                         == NULL)
473                 return (NULL);
474         memset (buf, '\0', bufsize * sizeof (STRUCT_STATFS));
475
476         /* The bufsize needs to be passed in bytes. Really. This is not in the
477          * manpage.. -octo */
478         if ((num = CMD_STATFS (buf, bufsize * sizeof (STRUCT_STATFS), FLAGS_STATFS)) < 1)
479         {
480 #if COLLECT_DEBUG
481                 char errbuf[1024];
482                 DEBUG ("utils_mount: getv?fsstat failed: %s",
483                                 sstrerror (errno, errbuf, sizeof (errbuf)));
484 #endif /* COLLECT_DEBUG */
485                 free (buf);
486                 return (NULL);
487         }
488
489         for (i = 0; i < num; i++)
490         {
491                 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
492                         break;
493                 memset (new, '\0', sizeof (cu_mount_t));
494                 
495                 /* Copy values from `struct mnttab' */
496                 new->dir         = sstrdup (buf[i].f_mntonname);
497                 new->spec_device = sstrdup (buf[i].f_mntfromname);
498                 new->type        = sstrdup (buf[i].f_fstypename);
499                 new->options     = NULL;
500                 new->device      = get_device_name (new->options);
501                 new->next = NULL;
502
503                 /* Append to list */
504                 if (first == NULL)
505                 {
506                         first = new;
507                         last  = new;
508                 }
509                 else
510                 {
511                         last->next = new;
512                         last       = new;
513                 }
514         }
515
516         free (buf);
517
518         return (first);
519 }
520 /* #endif HAVE_GETVFSSTAT || HAVE_GETFSSTAT */
521
522 /* Solaris (SunOS 10): int getmntent(FILE *fp, struct mnttab *mp); */
523 #elif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT
524 static cu_mount_t *cu_mount_gen_getmntent (void)
525 {
526         struct mnttab mt;
527         FILE *fp;
528
529         cu_mount_t *first = NULL;
530         cu_mount_t *last  = NULL;
531         cu_mount_t *new   = NULL;
532
533         DEBUG ("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
534
535         if ((fp = fopen (COLLECTD_MNTTAB, "r")) == NULL)
536         {
537                 char errbuf[1024];
538                 ERROR ("fopen (%s): %s", COLLECTD_MNTTAB,
539                                 sstrerror (errno, errbuf, sizeof (errbuf)));
540                 return (NULL);
541         }
542
543         while (getmntent (fp, &mt) == 0)
544         {
545                 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
546                         break;
547                 memset (new, '\0', sizeof (cu_mount_t));
548                 
549                 /* Copy values from `struct mnttab' */
550                 new->dir         = sstrdup (mt.mnt_mountp);
551                 new->spec_device = sstrdup (mt.mnt_special);
552                 new->type        = sstrdup (mt.mnt_fstype);
553                 new->options     = sstrdup (mt.mnt_mntopts);
554                 new->device      = get_device_name (new->options);
555                 new->next = NULL;
556
557                 /* Append to list */
558                 if (first == NULL)
559                 {
560                         first = new;
561                         last  = new;
562                 }
563                 else
564                 {
565                         last->next = new;
566                         last       = new;
567                 }
568         }
569
570         fclose (fp);
571
572         return (first);
573 } /* static cu_mount_t *cu_mount_gen_getmntent (void) */
574 /* #endif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT */
575
576 #elif HAVE_SEQ_GETMNTENT
577 #warn "This version of `getmntent' hat not yet been implemented!"
578 /* #endif HAVE_SEQ_GETMNTENT */
579
580 #elif HAVE_GETMNTENT_R
581 static cu_mount_t *cu_mount_getmntent (void)
582 {
583         FILE *fp;
584         struct mntent me;
585         char mntbuf[1024];
586
587         cu_mount_t *first = NULL;
588         cu_mount_t *last  = NULL;
589         cu_mount_t *new   = NULL;
590
591         DEBUG ("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
592
593         if ((fp = setmntent (COLLECTD_MNTTAB, "r")) == NULL)
594         {
595                 char errbuf[1024];
596                 ERROR ("setmntent (%s): %s", COLLECTD_MNTTAB,
597                                 sstrerror (errno, errbuf, sizeof (errbuf)));
598                 return (NULL);
599         }
600
601         while (getmntent_r (fp, &me, mntbuf, sizeof (mntbuf) ))
602         {
603                 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
604                         break;
605                 memset (new, '\0', sizeof (cu_mount_t));
606
607                 /* Copy values from `struct mntent *' */
608                 new->dir         = sstrdup (me.mnt_dir);
609                 new->spec_device = sstrdup (me.mnt_fsname);
610                 new->type        = sstrdup (me.mnt_type);
611                 new->options     = sstrdup (me.mnt_opts);
612                 new->device      = get_device_name (new->options);
613                 new->next        = NULL;
614
615                 DEBUG ("utils_mount: new = {dir = %s, spec_device = %s, type = %s, options = %s, device = %s}",
616                                 new->dir, new->spec_device, new->type, new->options, new->device);
617
618                 /* Append to list */
619                 if (first == NULL)
620                 {
621                         first = new;
622                         last  = new;
623                 }
624                 else
625                 {
626                         last->next = new;
627                         last       = new;
628                 }
629         }
630
631         endmntent (fp);
632
633         DEBUG ("utils_mount: return (0x%p)", (void *) first);
634
635         return (first);
636 } /* HAVE_GETMNTENT_R */
637
638 #elif HAVE_ONE_GETMNTENT
639 static cu_mount_t *cu_mount_getmntent (void)
640 {
641         FILE *fp;
642         struct mntent *me;
643
644         cu_mount_t *first = NULL;
645         cu_mount_t *last  = NULL;
646         cu_mount_t *new   = NULL;
647
648         DEBUG ("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
649
650         if ((fp = setmntent (COLLECTD_MNTTAB, "r")) == NULL)
651         {
652                 char errbuf[1024];
653                 ERROR ("setmntent (%s): %s", COLLECTD_MNTTAB,
654                                 sstrerror (errno, errbuf, sizeof (errbuf)));
655                 return (NULL);
656         }
657
658         while ((me = getmntent (fp)) != NULL)
659         {
660                 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
661                         break;
662                 memset (new, '\0', sizeof (cu_mount_t));
663                 
664                 /* Copy values from `struct mntent *' */
665                 new->dir         = sstrdup (me->mnt_dir);
666                 new->spec_device = sstrdup (me->mnt_fsname);
667                 new->type        = sstrdup (me->mnt_type);
668                 new->options     = sstrdup (me->mnt_opts);
669                 new->device      = get_device_name (new->options);
670                 new->next        = NULL;
671
672                 DEBUG ("utils_mount: new = {dir = %s, spec_device = %s, type = %s, options = %s, device = %s}",
673                                 new->dir, new->spec_device, new->type, new->options, new->device);
674
675                 /* Append to list */
676                 if (first == NULL)
677                 {
678                         first = new;
679                         last  = new;
680                 }
681                 else
682                 {
683                         last->next = new;
684                         last       = new;
685                 }
686         }
687
688         endmntent (fp);
689
690         DEBUG ("utils_mount: return (0x%p)", (void *) first);
691
692         return (first);
693 }
694 #endif /* HAVE_ONE_GETMNTENT */
695
696 /* *** *** *** ******************************************** *** *** *** */
697 /* *** *** *** *** *** ***   public functions   *** *** *** *** *** *** */
698 /* *** *** *** ******************************************** *** *** *** */
699
700 cu_mount_t *cu_mount_getlist(cu_mount_t **list)
701 {
702         cu_mount_t *new;
703         cu_mount_t *first = NULL;
704         cu_mount_t *last  = NULL;
705
706         if (list == NULL)
707                 return (NULL);
708
709         if (*list != NULL)
710         {
711                 first = *list;
712                 last  =  first;
713                 while (last->next != NULL)
714                         last = last->next;
715         }
716
717 #if HAVE_LISTMNTENT && 0
718         new = cu_mount_listmntent ();
719 #elif HAVE_GETVFSSTAT || HAVE_GETFSSTAT
720         new = cu_mount_getfsstat ();
721 #elif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT
722         new = cu_mount_gen_getmntent ();
723 #elif HAVE_SEQ_GETMNTENT
724 # error "This version of `getmntent' hat not yet been implemented!"
725 #elif HAVE_ONE_GETMNTENT
726         new = cu_mount_getmntent ();
727 #else
728 # error "Could not determine how to find mountpoints."
729 #endif
730
731         if (first != NULL)
732         {
733                 last->next = new;
734         }
735         else
736         {
737                 first = new;
738                 last  = new;
739                 *list = first;
740         }
741
742         while ((last != NULL) && (last->next != NULL))
743                 last = last->next;
744
745         return (last);
746 } /* cu_mount_t *cu_mount_getlist(cu_mount_t **list) */
747
748 void cu_mount_freelist (cu_mount_t *list)
749 {
750         cu_mount_t *this;
751         cu_mount_t *next;
752
753         for (this = list; this != NULL; this = next)
754         {
755                 next = this->next;
756
757                 sfree (this->dir);
758                 sfree (this->spec_device);
759                 sfree (this->device);
760                 sfree (this->type);
761                 sfree (this->options);
762                 sfree (this);
763         }
764 } /* void cu_mount_freelist(cu_mount_t *list) */
765
766 char *
767 cu_mount_checkoption(char *line, char *keyword, int full)
768 {
769         char *line2, *l2, *p1, *p2;
770         int l;
771
772         if(line == NULL || keyword == NULL) {
773                 return NULL;
774         }
775
776         if(full != 0) {
777                 full = 1;
778         }
779
780         line2 = sstrdup(line);
781         l2 = line2;
782         while(*l2 != '\0') {
783                 if(*l2 == ',') {
784                         *l2 = '\0';
785                 }
786                 l2++;
787         }
788
789         l = strlen(keyword);
790         p1 = line - 1;
791         p2 = strchr(line, ',');
792         do {
793                 if(strncmp(line2+(p1-line)+1, keyword, l+full) == 0) {
794                         free(line2);
795                         return p1+1;
796                 }
797                 p1 = p2;
798                 if(p1 != NULL) {
799                         p2 = strchr(p1+1, ',');
800                 }
801         } while(p1 != NULL);
802
803         free(line2);
804         return NULL;
805 } /* char *cu_mount_checkoption(char *line, char *keyword, int full) */
806
807 char *
808 cu_mount_getoptionvalue(char *line, char *keyword)
809 {
810         char *r;
811
812         r = cu_mount_checkoption(line, keyword, 0);
813         if(r != NULL) {
814                 char *p;
815                 r += strlen(keyword);
816                 p = strchr(r, ',');
817                 if(p == NULL) {
818                         return sstrdup(r);
819                 } else {
820                         char *m;
821                         if((p-r) == 1) {
822                                 return NULL;
823                         }
824                         m = (char *)smalloc(p-r+1);
825                         sstrncpy(m, r, p-r+1);
826                         return m;
827                 }
828         }
829         return r;
830 } /* char *cu_mount_getoptionvalue(char *line, char *keyword) */
831
832 int
833 cu_mount_type(const char *type)
834 {
835         if(strcmp(type, "ext3") == 0) return CUMT_EXT3;
836         if(strcmp(type, "ext2") == 0) return CUMT_EXT2;
837         if(strcmp(type, "ufs")  == 0) return CUMT_UFS;
838         if(strcmp(type, "vxfs") == 0) return CUMT_VXFS;
839         if(strcmp(type, "zfs")  == 0) return CUMT_ZFS;
840         return CUMT_UNKNOWN;
841 } /* int cu_mount_type(const char *type) */
842