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