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 #if HAVE_XFS_XQM_H
28 # define _GNU_SOURCE
29 # include <xfs/xqm.h>
30 #define XFS_SUPER_MAGIC_STR "XFSB"
31 #define XFS_SUPER_MAGIC2_STR "BSFX"
32 #endif
33
34 #include "collectd.h"
35
36 #include "utils_mount.h"
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 handleOnFirst;
220
221         if(uuidCache) {
222                 return;
223         }
224
225         procpt = fopen(PROC_PARTITIONS, "r");
226         if(procpt == NULL) {
227                 return;
228         }
229
230         for(int firstPass = 1; firstPass >= 0; firstPass--) {
231                 fseek(procpt, 0, SEEK_SET);
232                 while(fgets(line, sizeof(line), procpt)) {
233                         if(sscanf(line, " %d %d %d %[^\n ]",
234                                 &ma, &mi, &sz, ptname) != 4)
235                         {
236                                 continue;
237                         }
238
239                         /* skip extended partitions (heuristic: size 1) */
240                         if(sz == 1) {
241                                 continue;
242                         }
243
244                         /* look only at md devices on first pass */
245                         handleOnFirst = !strncmp(ptname, "md", 2);
246                         if(firstPass != handleOnFirst) {
247                                 continue;
248                         }
249
250                         /* skip entire disk (minor 0, 64, ... on ide;
251                         0, 16, ... on sd) */
252                         /* heuristic: partition name ends in a digit */
253
254                         for(s = ptname; *s; s++);
255
256                         if(isdigit((int)s[-1])) {
257                         /*
258                         * Note: this is a heuristic only - there is no reason
259                         * why these devices should live in /dev.
260                         * Perhaps this directory should be specifiable by option.
261                         * One might for example have /devlabel with links to /dev
262                         * for the devices that may be accessed in this way.
263                         * (This is useful, if the cdrom on /dev/hdc must not
264                         * be accessed.)
265                         */
266                                 ssnprintf(device, sizeof(device), "%s/%s",
267                                         DEVLABELDIR, ptname);
268                                 if(!get_label_uuid(device, &label, uuid)) {
269                                         uuidcache_addentry(sstrdup(device),
270                                                 label, uuid);
271                                 }
272                         }
273                 }
274         }
275         fclose(procpt);
276 }
277
278 static unsigned char
279 fromhex(char c)
280 {
281         if(isdigit((int)c)) {
282                 return (c - '0');
283         } else if(islower((int)c)) {
284                 return (c - 'a' + 10);
285         } else {
286                 return (c - 'A' + 10);
287         }
288 }
289
290 static char *
291 get_spec_by_x(int n, const char *t)
292 {
293         struct uuidCache_s *uc;
294
295         uuidcache_init();
296         uc = uuidCache;
297
298         while(uc) {
299                 switch(n) {
300                 case UUID:
301                         if(!memcmp(t, uc->uuid, sizeof(uc->uuid))) {
302                                 return sstrdup(uc->device);
303                         }
304                         break;
305                 case VOL:
306                         if(!strcmp(t, uc->label)) {
307                                 return sstrdup(uc->device);
308                         }
309                         break;
310                 }
311                 uc = uc->next;
312         }
313         return NULL;
314 }
315
316 static char *
317 get_spec_by_uuid(const char *s)
318 {
319         char uuid[16];
320
321         if(strlen(s) != 36
322         || s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-') {
323                 goto bad_uuid;
324         }
325
326         for(int i=0; i<16; i++) {
327                 if(*s == '-') {
328                         s++;
329                 }
330                 if(!isxdigit((int)s[0]) || !isxdigit((int)s[1])) {
331                         goto bad_uuid;
332                 }
333                 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
334                 s += 2;
335         }
336         return get_spec_by_x(UUID, uuid);
337
338         bad_uuid:
339                 DEBUG("utils_mount: Found an invalid UUID: %s", s);
340         return NULL;
341 }
342
343 static char *get_spec_by_volume_label(const char *s)
344 {
345         return get_spec_by_x (VOL, s);
346 }
347
348 static char *get_device_name(const char *optstr)
349 {
350         char *rc;
351
352         if (optstr == NULL)
353         {
354                 return (NULL);
355         }
356         else if (strncmp (optstr, "UUID=", 5) == 0)
357         {
358                 DEBUG ("utils_mount: TODO: check UUID= code!");
359                 rc = get_spec_by_uuid (optstr + 5);
360         }
361         else if (strncmp (optstr, "LABEL=", 6) == 0)
362         {
363                 DEBUG ("utils_mount: TODO: check LABEL= code!");
364                 rc = get_spec_by_volume_label (optstr + 6);
365         }
366         else
367         {
368                 rc = sstrdup (optstr);
369         }
370
371         if(!rc)
372         {
373                 DEBUG ("utils_mount: Error checking device name: optstr = %s", optstr);
374         }
375         return rc;
376 }
377
378 /* What weird OS is this..? I can't find any info with google :/ -octo */
379 #if HAVE_LISTMNTENT && 0
380 static cu_mount_t *cu_mount_listmntent (void)
381 {
382         cu_mount_t *last = *list;
383         struct mntent *mnt;
384
385         struct tabmntent *mntlist;
386         if(listmntent(&mntlist, COLLECTD_MNTTAB, NULL, NULL) < 0) {
387 #if COLLECT_DEBUG
388                 char errbuf[1024];
389                 DEBUG("utils_mount: calling listmntent() failed: %s",
390                                 sstrerror (errno, errbuf, sizeof (errbuf)));
391 #endif /* COLLECT_DEBUG */
392         }
393
394         for(struct tabmntent *p = mntlist; p; p = p->next) {
395                 char *loop = NULL, *device = NULL;
396
397                 mnt = p->ment;
398                 loop = cu_mount_getoptionvalue(mnt->mnt_opts, "loop=");
399                 if(loop == NULL) {   /* no loop= mount */
400                         device = get_device_name(mnt->mnt_fsname);
401                         if(device == NULL) {
402                                 DEBUG("utils_mount: can't get devicename for fs (%s) %s (%s)"
403                                         ": ignored", mnt->mnt_type,
404                                         mnt->mnt_dir, mnt->mnt_fsname);
405                                 continue;
406                         }
407                 } else {
408                         device = loop;
409                 }
410                 if(*list == NULL) {
411                         *list = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
412                         last = *list;
413                 } else {
414                         while(last->next != NULL) { /* is last really last? */
415                                 last = last->next;
416                         }
417                         last->next = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
418                         last = last->next;
419                 }
420                 last->dir = sstrdup(mnt->mnt_dir);
421                 last->spec_device = sstrdup(mnt->mnt_fsname);
422                 last->device = device;
423                 last->type = sstrdup(mnt->mnt_type);
424                 last->options = sstrdup(mnt->mnt_opts);
425                 last->next = NULL;
426         } /* for(p = mntlist; p; p = p->next) */
427
428         return(last);
429 } /* cu_mount_t *cu_mount_listmntent(void) */
430 /* #endif HAVE_LISTMNTENT */
431
432 /* 4.4BSD and Mac OS X (getfsstat) or NetBSD (getvfsstat) */
433 #elif HAVE_GETVFSSTAT || HAVE_GETFSSTAT
434 static cu_mount_t *cu_mount_getfsstat (void)
435 {
436 #if HAVE_GETFSSTAT
437 #  define STRUCT_STATFS struct statfs
438 #  define CMD_STATFS    getfsstat
439 #  define FLAGS_STATFS  MNT_NOWAIT
440 /* #endif HAVE_GETFSSTAT */
441 #elif HAVE_GETVFSSTAT
442 #  define STRUCT_STATFS struct statvfs
443 #  define CMD_STATFS    getvfsstat
444 #  define FLAGS_STATFS  ST_NOWAIT
445 #endif /* HAVE_GETVFSSTAT */
446
447         int bufsize;
448         STRUCT_STATFS *buf;
449
450         int num;
451
452         cu_mount_t *first = NULL;
453         cu_mount_t *last  = NULL;
454         cu_mount_t *new   = NULL;
455
456         /* Get the number of mounted file systems */
457         if ((bufsize = CMD_STATFS (NULL, 0, FLAGS_STATFS)) < 1)
458         {
459 #if COLLECT_DEBUG
460                 char errbuf[1024];
461                 DEBUG ("utils_mount: getv?fsstat failed: %s",
462                                 sstrerror (errno, errbuf, sizeof (errbuf)));
463 #endif /* COLLECT_DEBUG */
464                 return (NULL);
465         }
466
467         if ((buf = calloc (bufsize, sizeof (*buf))) == NULL)
468                 return (NULL);
469
470         /* The bufsize needs to be passed in bytes. Really. This is not in the
471          * manpage.. -octo */
472         if ((num = CMD_STATFS (buf, bufsize * sizeof (STRUCT_STATFS), FLAGS_STATFS)) < 1)
473         {
474 #if COLLECT_DEBUG
475                 char errbuf[1024];
476                 DEBUG ("utils_mount: getv?fsstat failed: %s",
477                                 sstrerror (errno, errbuf, sizeof (errbuf)));
478 #endif /* COLLECT_DEBUG */
479                 free (buf);
480                 return (NULL);
481         }
482
483         for (int i = 0; i < num; i++)
484         {
485                 if ((new = calloc (1, sizeof (*new))) == NULL)
486                         break;
487
488                 /* Copy values from `struct mnttab' */
489                 new->dir         = sstrdup (buf[i].f_mntonname);
490                 new->spec_device = sstrdup (buf[i].f_mntfromname);
491                 new->type        = sstrdup (buf[i].f_fstypename);
492                 new->options     = NULL;
493                 new->device      = get_device_name (new->options);
494                 new->next = NULL;
495
496                 /* Append to list */
497                 if (first == NULL)
498                 {
499                         first = new;
500                         last  = new;
501                 }
502                 else
503                 {
504                         last->next = new;
505                         last       = new;
506                 }
507         }
508
509         free (buf);
510
511         return (first);
512 }
513 /* #endif HAVE_GETVFSSTAT || HAVE_GETFSSTAT */
514
515 /* Solaris (SunOS 10): int getmntent(FILE *fp, struct mnttab *mp); */
516 #elif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT
517 static cu_mount_t *cu_mount_gen_getmntent (void)
518 {
519         struct mnttab mt;
520         FILE *fp;
521
522         cu_mount_t *first = NULL;
523         cu_mount_t *last  = NULL;
524         cu_mount_t *new   = NULL;
525
526         DEBUG ("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
527
528         if ((fp = fopen (COLLECTD_MNTTAB, "r")) == NULL)
529         {
530                 char errbuf[1024];
531                 ERROR ("fopen (%s): %s", COLLECTD_MNTTAB,
532                                 sstrerror (errno, errbuf, sizeof (errbuf)));
533                 return (NULL);
534         }
535
536         while (getmntent (fp, &mt) == 0)
537         {
538                 if ((new = calloc (1, sizeof (*new))) == NULL)
539                         break;
540
541                 /* Copy values from `struct mnttab' */
542                 new->dir         = sstrdup (mt.mnt_mountp);
543                 new->spec_device = sstrdup (mt.mnt_special);
544                 new->type        = sstrdup (mt.mnt_fstype);
545                 new->options     = sstrdup (mt.mnt_mntopts);
546                 new->device      = get_device_name (new->options);
547                 new->next = NULL;
548
549                 /* Append to list */
550                 if (first == NULL)
551                 {
552                         first = new;
553                         last  = new;
554                 }
555                 else
556                 {
557                         last->next = new;
558                         last       = new;
559                 }
560         }
561
562         fclose (fp);
563
564         return (first);
565 } /* static cu_mount_t *cu_mount_gen_getmntent (void) */
566 /* #endif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT */
567
568 #elif HAVE_SEQ_GETMNTENT
569 #warn "This version of `getmntent' hat not yet been implemented!"
570 /* #endif HAVE_SEQ_GETMNTENT */
571
572 #elif HAVE_GETMNTENT_R
573 static cu_mount_t *cu_mount_getmntent (void)
574 {
575         FILE *fp;
576         struct mntent me;
577         char mntbuf[1024];
578
579         cu_mount_t *first = NULL;
580         cu_mount_t *last  = NULL;
581         cu_mount_t *new   = NULL;
582
583         DEBUG ("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
584
585         if ((fp = setmntent (COLLECTD_MNTTAB, "r")) == NULL)
586         {
587                 char errbuf[1024];
588                 ERROR ("setmntent (%s): %s", COLLECTD_MNTTAB,
589                                 sstrerror (errno, errbuf, sizeof (errbuf)));
590                 return (NULL);
591         }
592
593         while (getmntent_r (fp, &me, mntbuf, sizeof (mntbuf) ))
594         {
595                 if ((new = calloc (1, sizeof (*new))) == NULL)
596                         break;
597
598                 /* Copy values from `struct mntent *' */
599                 new->dir         = sstrdup (me.mnt_dir);
600                 new->spec_device = sstrdup (me.mnt_fsname);
601                 new->type        = sstrdup (me.mnt_type);
602                 new->options     = sstrdup (me.mnt_opts);
603                 new->device      = get_device_name (new->options);
604                 new->next        = NULL;
605
606                 DEBUG ("utils_mount: new = {dir = %s, spec_device = %s, type = %s, options = %s, device = %s}",
607                                 new->dir, new->spec_device, new->type, new->options, new->device);
608
609                 /* Append to list */
610                 if (first == NULL)
611                 {
612                         first = new;
613                         last  = new;
614                 }
615                 else
616                 {
617                         last->next = new;
618                         last       = new;
619                 }
620         }
621
622         endmntent (fp);
623
624         DEBUG ("utils_mount: return (0x%p)", (void *) first);
625
626         return (first);
627 } /* HAVE_GETMNTENT_R */
628
629 #elif HAVE_ONE_GETMNTENT
630 static cu_mount_t *cu_mount_getmntent (void)
631 {
632         FILE *fp;
633         struct mntent *me;
634
635         cu_mount_t *first = NULL;
636         cu_mount_t *last  = NULL;
637         cu_mount_t *new   = NULL;
638
639         DEBUG ("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
640
641         if ((fp = setmntent (COLLECTD_MNTTAB, "r")) == NULL)
642         {
643                 char errbuf[1024];
644                 ERROR ("setmntent (%s): %s", COLLECTD_MNTTAB,
645                                 sstrerror (errno, errbuf, sizeof (errbuf)));
646                 return (NULL);
647         }
648
649         while ((me = getmntent (fp)) != NULL)
650         {
651                 if ((new = calloc (1, sizeof (*new))) == NULL)
652                         break;
653
654                 /* Copy values from `struct mntent *' */
655                 new->dir         = sstrdup (me->mnt_dir);
656                 new->spec_device = sstrdup (me->mnt_fsname);
657                 new->type        = sstrdup (me->mnt_type);
658                 new->options     = sstrdup (me->mnt_opts);
659                 new->device      = get_device_name (new->options);
660                 new->next        = NULL;
661
662                 DEBUG ("utils_mount: new = {dir = %s, spec_device = %s, type = %s, options = %s, device = %s}",
663                                 new->dir, new->spec_device, new->type, new->options, new->device);
664
665                 /* Append to list */
666                 if (first == NULL)
667                 {
668                         first = new;
669                         last  = new;
670                 }
671                 else
672                 {
673                         last->next = new;
674                         last       = new;
675                 }
676         }
677
678         endmntent (fp);
679
680         DEBUG ("utils_mount: return (0x%p)", (void *) first);
681
682         return (first);
683 }
684 #endif /* HAVE_ONE_GETMNTENT */
685
686 /* *** *** *** ******************************************** *** *** *** */
687 /* *** *** *** *** *** ***   public functions   *** *** *** *** *** *** */
688 /* *** *** *** ******************************************** *** *** *** */
689
690 cu_mount_t *cu_mount_getlist(cu_mount_t **list)
691 {
692         cu_mount_t *new;
693         cu_mount_t *first = NULL;
694         cu_mount_t *last  = NULL;
695
696         if (list == NULL)
697                 return (NULL);
698
699         if (*list != NULL)
700         {
701                 first = *list;
702                 last  =  first;
703                 while (last->next != NULL)
704                         last = last->next;
705         }
706
707 #if HAVE_LISTMNTENT && 0
708         new = cu_mount_listmntent ();
709 #elif HAVE_GETVFSSTAT || HAVE_GETFSSTAT
710         new = cu_mount_getfsstat ();
711 #elif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT
712         new = cu_mount_gen_getmntent ();
713 #elif HAVE_SEQ_GETMNTENT
714 # error "This version of `getmntent' hat not yet been implemented!"
715 #elif HAVE_ONE_GETMNTENT
716         new = cu_mount_getmntent ();
717 #else
718 # error "Could not determine how to find mountpoints."
719 #endif
720
721         if (first != NULL)
722         {
723                 last->next = new;
724         }
725         else
726         {
727                 first = new;
728                 last  = new;
729                 *list = first;
730         }
731
732         while ((last != NULL) && (last->next != NULL))
733                 last = last->next;
734
735         return (last);
736 } /* cu_mount_t *cu_mount_getlist(cu_mount_t **list) */
737
738 void cu_mount_freelist (cu_mount_t *list)
739 {
740         cu_mount_t *next;
741
742         for (cu_mount_t *this = list; this != NULL; this = next)
743         {
744                 next = this->next;
745
746                 sfree (this->dir);
747                 sfree (this->spec_device);
748                 sfree (this->device);
749                 sfree (this->type);
750                 sfree (this->options);
751                 sfree (this);
752         }
753 } /* void cu_mount_freelist(cu_mount_t *list) */
754
755 char *
756 cu_mount_checkoption(char *line, const char *keyword, int full)
757 {
758         char *line2, *l2, *p1, *p2;
759         int l;
760
761         if(line == NULL || keyword == NULL) {
762                 return NULL;
763         }
764
765         if(full != 0) {
766                 full = 1;
767         }
768
769         line2 = sstrdup(line);
770         l2 = line2;
771         while(*l2 != '\0') {
772                 if(*l2 == ',') {
773                         *l2 = '\0';
774                 }
775                 l2++;
776         }
777
778         l = strlen(keyword);
779         p1 = line - 1;
780         p2 = strchr(line, ',');
781         do {
782                 if(strncmp(line2+(p1-line)+1, keyword, l+full) == 0) {
783                         free(line2);
784                         return p1+1;
785                 }
786                 p1 = p2;
787                 if(p1 != NULL) {
788                         p2 = strchr(p1+1, ',');
789                 }
790         } while(p1 != NULL);
791
792         free(line2);
793         return NULL;
794 } /* char *cu_mount_checkoption(char *line, char *keyword, int full) */
795
796 char *
797 cu_mount_getoptionvalue(char *line, const char *keyword)
798 {
799         char *r;
800
801         r = cu_mount_checkoption(line, keyword, 0);
802         if(r != NULL) {
803                 char *p;
804                 r += strlen(keyword);
805                 p = strchr(r, ',');
806                 if(p == NULL) {
807                         return sstrdup(r);
808                 } else {
809                         char *m;
810                         if((p-r) == 1) {
811                                 return NULL;
812                         }
813                         m = smalloc(p-r+1);
814                         sstrncpy(m, r, p-r+1);
815                         return m;
816                 }
817         }
818         return r;
819 } /* char *cu_mount_getoptionvalue(char *line, const char *keyword) */
820
821 int
822 cu_mount_type(const char *type)
823 {
824         if(strcmp(type, "ext3") == 0) return CUMT_EXT3;
825         if(strcmp(type, "ext2") == 0) return CUMT_EXT2;
826         if(strcmp(type, "ufs")  == 0) return CUMT_UFS;
827         if(strcmp(type, "vxfs") == 0) return CUMT_VXFS;
828         if(strcmp(type, "zfs")  == 0) return CUMT_ZFS;
829         return CUMT_UNKNOWN;
830 } /* int cu_mount_type(const char *type) */
831