Merge pull request #1830 from rubenk/move-collectd-header
[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 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 = calloc (bufsize, sizeof (*buf))) == NULL)
472                 return (NULL);
473
474         /* The bufsize needs to be passed in bytes. Really. This is not in the
475          * manpage.. -octo */
476         if ((num = CMD_STATFS (buf, bufsize * sizeof (STRUCT_STATFS), FLAGS_STATFS)) < 1)
477         {
478 #if COLLECT_DEBUG
479                 char errbuf[1024];
480                 DEBUG ("utils_mount: getv?fsstat failed: %s",
481                                 sstrerror (errno, errbuf, sizeof (errbuf)));
482 #endif /* COLLECT_DEBUG */
483                 free (buf);
484                 return (NULL);
485         }
486
487         for (i = 0; i < num; i++)
488         {
489                 if ((new = calloc (1, sizeof (*new))) == NULL)
490                         break;
491
492                 /* Copy values from `struct mnttab' */
493                 new->dir         = sstrdup (buf[i].f_mntonname);
494                 new->spec_device = sstrdup (buf[i].f_mntfromname);
495                 new->type        = sstrdup (buf[i].f_fstypename);
496                 new->options     = NULL;
497                 new->device      = get_device_name (new->options);
498                 new->next = NULL;
499
500                 /* Append to list */
501                 if (first == NULL)
502                 {
503                         first = new;
504                         last  = new;
505                 }
506                 else
507                 {
508                         last->next = new;
509                         last       = new;
510                 }
511         }
512
513         free (buf);
514
515         return (first);
516 }
517 /* #endif HAVE_GETVFSSTAT || HAVE_GETFSSTAT */
518
519 /* Solaris (SunOS 10): int getmntent(FILE *fp, struct mnttab *mp); */
520 #elif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT
521 static cu_mount_t *cu_mount_gen_getmntent (void)
522 {
523         struct mnttab mt;
524         FILE *fp;
525
526         cu_mount_t *first = NULL;
527         cu_mount_t *last  = NULL;
528         cu_mount_t *new   = NULL;
529
530         DEBUG ("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
531
532         if ((fp = fopen (COLLECTD_MNTTAB, "r")) == NULL)
533         {
534                 char errbuf[1024];
535                 ERROR ("fopen (%s): %s", COLLECTD_MNTTAB,
536                                 sstrerror (errno, errbuf, sizeof (errbuf)));
537                 return (NULL);
538         }
539
540         while (getmntent (fp, &mt) == 0)
541         {
542                 if ((new = calloc (1, sizeof (*new))) == NULL)
543                         break;
544
545                 /* Copy values from `struct mnttab' */
546                 new->dir         = sstrdup (mt.mnt_mountp);
547                 new->spec_device = sstrdup (mt.mnt_special);
548                 new->type        = sstrdup (mt.mnt_fstype);
549                 new->options     = sstrdup (mt.mnt_mntopts);
550                 new->device      = get_device_name (new->options);
551                 new->next = NULL;
552
553                 /* Append to list */
554                 if (first == NULL)
555                 {
556                         first = new;
557                         last  = new;
558                 }
559                 else
560                 {
561                         last->next = new;
562                         last       = new;
563                 }
564         }
565
566         fclose (fp);
567
568         return (first);
569 } /* static cu_mount_t *cu_mount_gen_getmntent (void) */
570 /* #endif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT */
571
572 #elif HAVE_SEQ_GETMNTENT
573 #warn "This version of `getmntent' hat not yet been implemented!"
574 /* #endif HAVE_SEQ_GETMNTENT */
575
576 #elif HAVE_GETMNTENT_R
577 static cu_mount_t *cu_mount_getmntent (void)
578 {
579         FILE *fp;
580         struct mntent me;
581         char mntbuf[1024];
582
583         cu_mount_t *first = NULL;
584         cu_mount_t *last  = NULL;
585         cu_mount_t *new   = NULL;
586
587         DEBUG ("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
588
589         if ((fp = setmntent (COLLECTD_MNTTAB, "r")) == NULL)
590         {
591                 char errbuf[1024];
592                 ERROR ("setmntent (%s): %s", COLLECTD_MNTTAB,
593                                 sstrerror (errno, errbuf, sizeof (errbuf)));
594                 return (NULL);
595         }
596
597         while (getmntent_r (fp, &me, mntbuf, sizeof (mntbuf) ))
598         {
599                 if ((new = calloc (1, sizeof (*new))) == NULL)
600                         break;
601
602                 /* Copy values from `struct mntent *' */
603                 new->dir         = sstrdup (me.mnt_dir);
604                 new->spec_device = sstrdup (me.mnt_fsname);
605                 new->type        = sstrdup (me.mnt_type);
606                 new->options     = sstrdup (me.mnt_opts);
607                 new->device      = get_device_name (new->options);
608                 new->next        = NULL;
609
610                 DEBUG ("utils_mount: new = {dir = %s, spec_device = %s, type = %s, options = %s, device = %s}",
611                                 new->dir, new->spec_device, new->type, new->options, new->device);
612
613                 /* Append to list */
614                 if (first == NULL)
615                 {
616                         first = new;
617                         last  = new;
618                 }
619                 else
620                 {
621                         last->next = new;
622                         last       = new;
623                 }
624         }
625
626         endmntent (fp);
627
628         DEBUG ("utils_mount: return (0x%p)", (void *) first);
629
630         return (first);
631 } /* HAVE_GETMNTENT_R */
632
633 #elif HAVE_ONE_GETMNTENT
634 static cu_mount_t *cu_mount_getmntent (void)
635 {
636         FILE *fp;
637         struct mntent *me;
638
639         cu_mount_t *first = NULL;
640         cu_mount_t *last  = NULL;
641         cu_mount_t *new   = NULL;
642
643         DEBUG ("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
644
645         if ((fp = setmntent (COLLECTD_MNTTAB, "r")) == NULL)
646         {
647                 char errbuf[1024];
648                 ERROR ("setmntent (%s): %s", COLLECTD_MNTTAB,
649                                 sstrerror (errno, errbuf, sizeof (errbuf)));
650                 return (NULL);
651         }
652
653         while ((me = getmntent (fp)) != NULL)
654         {
655                 if ((new = calloc (1, sizeof (*new))) == NULL)
656                         break;
657
658                 /* Copy values from `struct mntent *' */
659                 new->dir         = sstrdup (me->mnt_dir);
660                 new->spec_device = sstrdup (me->mnt_fsname);
661                 new->type        = sstrdup (me->mnt_type);
662                 new->options     = sstrdup (me->mnt_opts);
663                 new->device      = get_device_name (new->options);
664                 new->next        = NULL;
665
666                 DEBUG ("utils_mount: new = {dir = %s, spec_device = %s, type = %s, options = %s, device = %s}",
667                                 new->dir, new->spec_device, new->type, new->options, new->device);
668
669                 /* Append to list */
670                 if (first == NULL)
671                 {
672                         first = new;
673                         last  = new;
674                 }
675                 else
676                 {
677                         last->next = new;
678                         last       = new;
679                 }
680         }
681
682         endmntent (fp);
683
684         DEBUG ("utils_mount: return (0x%p)", (void *) first);
685
686         return (first);
687 }
688 #endif /* HAVE_ONE_GETMNTENT */
689
690 /* *** *** *** ******************************************** *** *** *** */
691 /* *** *** *** *** *** ***   public functions   *** *** *** *** *** *** */
692 /* *** *** *** ******************************************** *** *** *** */
693
694 cu_mount_t *cu_mount_getlist(cu_mount_t **list)
695 {
696         cu_mount_t *new;
697         cu_mount_t *first = NULL;
698         cu_mount_t *last  = NULL;
699
700         if (list == NULL)
701                 return (NULL);
702
703         if (*list != NULL)
704         {
705                 first = *list;
706                 last  =  first;
707                 while (last->next != NULL)
708                         last = last->next;
709         }
710
711 #if HAVE_LISTMNTENT && 0
712         new = cu_mount_listmntent ();
713 #elif HAVE_GETVFSSTAT || HAVE_GETFSSTAT
714         new = cu_mount_getfsstat ();
715 #elif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT
716         new = cu_mount_gen_getmntent ();
717 #elif HAVE_SEQ_GETMNTENT
718 # error "This version of `getmntent' hat not yet been implemented!"
719 #elif HAVE_ONE_GETMNTENT
720         new = cu_mount_getmntent ();
721 #else
722 # error "Could not determine how to find mountpoints."
723 #endif
724
725         if (first != NULL)
726         {
727                 last->next = new;
728         }
729         else
730         {
731                 first = new;
732                 last  = new;
733                 *list = first;
734         }
735
736         while ((last != NULL) && (last->next != NULL))
737                 last = last->next;
738
739         return (last);
740 } /* cu_mount_t *cu_mount_getlist(cu_mount_t **list) */
741
742 void cu_mount_freelist (cu_mount_t *list)
743 {
744         cu_mount_t *this;
745         cu_mount_t *next;
746
747         for (this = list; this != NULL; this = next)
748         {
749                 next = this->next;
750
751                 sfree (this->dir);
752                 sfree (this->spec_device);
753                 sfree (this->device);
754                 sfree (this->type);
755                 sfree (this->options);
756                 sfree (this);
757         }
758 } /* void cu_mount_freelist(cu_mount_t *list) */
759
760 char *
761 cu_mount_checkoption(char *line, const char *keyword, int full)
762 {
763         char *line2, *l2, *p1, *p2;
764         int l;
765
766         if(line == NULL || keyword == NULL) {
767                 return NULL;
768         }
769
770         if(full != 0) {
771                 full = 1;
772         }
773
774         line2 = sstrdup(line);
775         l2 = line2;
776         while(*l2 != '\0') {
777                 if(*l2 == ',') {
778                         *l2 = '\0';
779                 }
780                 l2++;
781         }
782
783         l = strlen(keyword);
784         p1 = line - 1;
785         p2 = strchr(line, ',');
786         do {
787                 if(strncmp(line2+(p1-line)+1, keyword, l+full) == 0) {
788                         free(line2);
789                         return p1+1;
790                 }
791                 p1 = p2;
792                 if(p1 != NULL) {
793                         p2 = strchr(p1+1, ',');
794                 }
795         } while(p1 != NULL);
796
797         free(line2);
798         return NULL;
799 } /* char *cu_mount_checkoption(char *line, char *keyword, int full) */
800
801 char *
802 cu_mount_getoptionvalue(char *line, const char *keyword)
803 {
804         char *r;
805
806         r = cu_mount_checkoption(line, keyword, 0);
807         if(r != NULL) {
808                 char *p;
809                 r += strlen(keyword);
810                 p = strchr(r, ',');
811                 if(p == NULL) {
812                         return sstrdup(r);
813                 } else {
814                         char *m;
815                         if((p-r) == 1) {
816                                 return NULL;
817                         }
818                         m = smalloc(p-r+1);
819                         sstrncpy(m, r, p-r+1);
820                         return m;
821                 }
822         }
823         return r;
824 } /* char *cu_mount_getoptionvalue(char *line, const char *keyword) */
825
826 int
827 cu_mount_type(const char *type)
828 {
829         if(strcmp(type, "ext3") == 0) return CUMT_EXT3;
830         if(strcmp(type, "ext2") == 0) return CUMT_EXT2;
831         if(strcmp(type, "ufs")  == 0) return CUMT_UFS;
832         if(strcmp(type, "vxfs") == 0) return CUMT_VXFS;
833         if(strcmp(type, "zfs")  == 0) return CUMT_ZFS;
834         return CUMT_UNKNOWN;
835 } /* int cu_mount_type(const char *type) */
836