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