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