Merge branch 'collectd-5.4' into collectd-5.5
[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 #include "collectd.h"
24 #include "utils_mount.h"
25
26 #include "common.h" /* sstrncpy() et alii */
27 #include "plugin.h" /* ERROR() macro */
28
29 #if HAVE_XFS_XQM_H
30 # include <xfs/xqm.h>
31 #define XFS_SUPER_MAGIC_STR "XFSB"
32 #define XFS_SUPER_MAGIC2_STR "BSFX"
33 #endif
34
35 #if HAVE_GETVFSSTAT
36 #  if HAVE_SYS_TYPES_H
37 #    include <sys/types.h>
38 #  endif
39 #  if HAVE_SYS_STATVFS_H
40 #    include <sys/statvfs.h>
41 #  endif
42 /* #endif HAVE_GETVFSSTAT */
43
44 #elif HAVE_GETFSSTAT
45 #  if HAVE_SYS_PARAM_H
46 #    include <sys/param.h>
47 #  endif
48 #  if HAVE_SYS_UCRED_H
49 #    include <sys/ucred.h>
50 #  endif
51 #  if HAVE_SYS_MOUNT_H
52 #    include <sys/mount.h>
53 #  endif
54 #endif /* HAVE_GETFSSTAT */
55
56 #if HAVE_MNTENT_H
57 #  include <mntent.h>
58 #endif
59 #if HAVE_SYS_MNTTAB_H
60 #  include <sys/mnttab.h>
61 #endif
62
63 #if HAVE_PATHS_H
64 #  include <paths.h>
65 #endif
66
67 #ifdef COLLECTD_MNTTAB
68 #  undef COLLECTD_MNTTAB
69 #endif
70
71 #if defined(_PATH_MOUNTED) /* glibc */
72 #  define COLLECTD_MNTTAB _PATH_MOUNTED
73 #elif defined(MNTTAB) /* Solaris */
74 #  define COLLECTD_MNTTAB MNTTAB
75 #elif defined(MNT_MNTTAB)
76 #  define COLLECTD_MNTTAB MNT_MNTTAB
77 #elif defined(MNTTABNAME)
78 #  define COLLECTD_MNTTAB MNTTABNAME
79 #elif defined(KMTAB)
80 #  define COLLECTD_MNTTAB KMTAB
81 #else
82 #  define COLLECTD_MNTTAB "/etc/mnttab"
83 #endif
84
85 /* *** *** *** ********************************************* *** *** *** */
86 /* *** *** *** *** *** ***   private functions   *** *** *** *** *** *** */
87 /* *** *** *** ********************************************* *** *** *** */
88
89 /* stolen from quota-3.13 (quota-tools) */
90
91 #define PROC_PARTITIONS "/proc/partitions"
92 #define DEVLABELDIR     "/dev"
93 #define UUID   1
94 #define VOL    2
95
96 static struct uuidCache_s {
97         struct uuidCache_s *next;
98         char uuid[16];
99         char *label;
100         char *device;
101 } *uuidCache = NULL;
102
103 #define EXT2_SUPER_MAGIC 0xEF53
104 struct ext2_super_block {
105         unsigned char s_dummy1[56];
106         unsigned char s_magic[2];
107         unsigned char s_dummy2[46];
108         unsigned char s_uuid[16];
109         char s_volume_name[16];
110 };
111 #define ext2magic(s) ((unsigned int)s.s_magic[0] \
112         + (((unsigned int)s.s_magic[1]) << 8))
113
114 #if HAVE_XFS_XQM_H
115 struct xfs_super_block {
116         unsigned char s_magic[4];
117         unsigned char s_dummy[28];
118         unsigned char s_uuid[16];
119         unsigned char s_dummy2[60];
120         char s_fsname[12];
121 };
122 #endif /* HAVE_XFS_XQM_H */
123
124 #define REISER_SUPER_MAGIC "ReIsEr2Fs"
125 struct reiserfs_super_block {
126         unsigned char s_dummy1[52];
127         unsigned char s_magic[10];
128         unsigned char s_dummy2[22];
129         unsigned char s_uuid[16];
130         char s_volume_name[16];
131 };
132
133 /* for now, only ext2 and xfs are supported */
134 static int
135 get_label_uuid(const char *device, char **label, char *uuid)
136 {
137         /* start with ext2 and xfs tests, taken from mount_guess_fstype */
138         /* should merge these later */
139         int fd, rv = 1;
140         size_t namesize;
141         struct ext2_super_block e2sb;
142 #if HAVE_XFS_XQM_H
143         struct xfs_super_block xfsb;
144 #endif
145         struct reiserfs_super_block reisersb;
146
147         fd = open(device, O_RDONLY);
148         if(fd == -1) {
149                 return rv;
150         }
151
152         if(lseek(fd, 1024, SEEK_SET) == 1024
153         && read(fd, (char *)&e2sb, sizeof(e2sb)) == sizeof(e2sb)
154         && ext2magic(e2sb) == EXT2_SUPER_MAGIC) {
155                 memcpy(uuid, e2sb.s_uuid, sizeof(e2sb.s_uuid));
156                 namesize = sizeof(e2sb.s_volume_name);
157                 *label = smalloc(namesize + 1);
158                 sstrncpy(*label, e2sb.s_volume_name, namesize);
159                 rv = 0;
160 #if HAVE_XFS_XQM_H
161         } else if(lseek(fd, 0, SEEK_SET) == 0
162         && read(fd, (char *)&xfsb, sizeof(xfsb)) == sizeof(xfsb)
163         && (strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC_STR, 4) == 0 ||
164         strncmp((char *)&xfsb.s_magic, XFS_SUPER_MAGIC2_STR, 4) == 0)) {
165                 memcpy(uuid, xfsb.s_uuid, sizeof(xfsb.s_uuid));
166                 namesize = sizeof(xfsb.s_fsname);
167                 *label = smalloc(namesize + 1);
168                 sstrncpy(*label, xfsb.s_fsname, namesize);
169                 rv = 0;
170 #endif /* HAVE_XFS_XQM_H */
171         } else if(lseek(fd, 65536, SEEK_SET) == 65536
172         && read(fd, (char *)&reisersb, sizeof(reisersb)) == sizeof(reisersb)
173         && !strncmp((char *)&reisersb.s_magic, REISER_SUPER_MAGIC, 9)) {
174                 memcpy(uuid, reisersb.s_uuid, sizeof(reisersb.s_uuid));
175                 namesize = sizeof(reisersb.s_volume_name);
176                 *label = smalloc(namesize + 1);
177                 sstrncpy(*label, reisersb.s_volume_name, namesize);
178                 rv = 0;
179         }
180         close(fd);
181         return rv;
182 }
183
184 static void
185 uuidcache_addentry(char *device, char *label, char *uuid)
186 {
187         struct uuidCache_s *last;
188
189         if(!uuidCache) {
190                 last = uuidCache = smalloc(sizeof(*uuidCache));
191         } else {
192                 for(last = uuidCache; last->next; last = last->next);
193                 last->next = smalloc(sizeof(*uuidCache));
194                 last = last->next;
195         }
196         last->next = NULL;
197         last->device = device;
198         last->label = label;
199         memcpy(last->uuid, uuid, sizeof(last->uuid));
200 }
201
202 static void
203 uuidcache_init(void)
204 {
205         char line[100];
206         char *s;
207         int ma, mi, sz;
208         static char ptname[100];
209         FILE *procpt;
210         char uuid[16], *label = NULL;
211         char device[110];
212         int firstPass;
213         int handleOnFirst;
214
215         if(uuidCache) {
216                 return;
217         }
218
219         procpt = fopen(PROC_PARTITIONS, "r");
220         if(procpt == NULL) {
221                 return;
222         }
223
224         for(firstPass = 1; firstPass >= 0; firstPass--) {
225                 fseek(procpt, 0, SEEK_SET);
226                 while(fgets(line, sizeof(line), procpt)) {
227                         if(sscanf(line, " %d %d %d %[^\n ]",
228                                 &ma, &mi, &sz, ptname) != 4)
229                         {
230                                 continue;
231                         }
232
233                         /* skip extended partitions (heuristic: size 1) */
234                         if(sz == 1) {
235                                 continue;
236                         }
237
238                         /* look only at md devices on first pass */
239                         handleOnFirst = !strncmp(ptname, "md", 2);
240                         if(firstPass != handleOnFirst) {
241                                 continue;
242                         }
243
244                         /* skip entire disk (minor 0, 64, ... on ide;
245                         0, 16, ... on sd) */
246                         /* heuristic: partition name ends in a digit */
247
248                         for(s = ptname; *s; s++);
249
250                         if(isdigit((int)s[-1])) {
251                         /*
252                         * Note: this is a heuristic only - there is no reason
253                         * why these devices should live in /dev.
254                         * Perhaps this directory should be specifiable by option.
255                         * One might for example have /devlabel with links to /dev
256                         * for the devices that may be accessed in this way.
257                         * (This is useful, if the cdrom on /dev/hdc must not
258                         * be accessed.)
259                         */
260                                 ssnprintf(device, sizeof(device), "%s/%s",
261                                         DEVLABELDIR, ptname);
262                                 if(!get_label_uuid(device, &label, uuid)) {
263                                         uuidcache_addentry(sstrdup(device),
264                                                 label, uuid);
265                                 }
266                         }
267                 }
268         }
269         fclose(procpt);
270 }
271
272 static unsigned char
273 fromhex(char c)
274 {
275         if(isdigit((int)c)) {
276                 return (c - '0');
277         } else if(islower((int)c)) {
278                 return (c - 'a' + 10);
279         } else {
280                 return (c - 'A' + 10);
281         }
282 }
283
284 static char *
285 get_spec_by_x(int n, const char *t)
286 {
287         struct uuidCache_s *uc;
288
289         uuidcache_init();
290         uc = uuidCache;
291
292         while(uc) {
293                 switch(n) {
294                 case UUID:
295                         if(!memcmp(t, uc->uuid, sizeof(uc->uuid))) {
296                                 return sstrdup(uc->device);
297                         }
298                         break;
299                 case VOL:
300                         if(!strcmp(t, uc->label)) {
301                                 return sstrdup(uc->device);
302                         }
303                         break;
304                 }
305                 uc = uc->next;
306         }
307         return NULL;
308 }
309
310 static char *
311 get_spec_by_uuid(const char *s)
312 {
313         char uuid[16];
314         int i;
315
316         if(strlen(s) != 36
317         || s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-') {
318                 goto bad_uuid;
319         }
320
321         for(i=0; i<16; i++) {
322                 if(*s == '-') {
323                         s++;
324                 }
325                 if(!isxdigit((int)s[0]) || !isxdigit((int)s[1])) {
326                         goto bad_uuid;
327                 }
328                 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
329                 s += 2;
330         }
331         return get_spec_by_x(UUID, uuid);
332
333         bad_uuid:
334                 DEBUG("utils_mount: Found an invalid UUID: %s", s);
335         return NULL;
336 }
337
338 static char *get_spec_by_volume_label(const char *s)
339 {
340         return get_spec_by_x (VOL, s);
341 }
342
343 static char *get_device_name(const char *optstr)
344 {
345         char *rc;
346
347         if (optstr == NULL)
348         {
349                 return (NULL);
350         }
351         else if (strncmp (optstr, "UUID=", 5) == 0)
352         {
353                 DEBUG ("utils_mount: TODO: check UUID= code!");
354                 rc = get_spec_by_uuid (optstr + 5);
355         }
356         else if (strncmp (optstr, "LABEL=", 6) == 0)
357         {
358                 DEBUG ("utils_mount: TODO: check LABEL= code!");
359                 rc = get_spec_by_volume_label (optstr + 6);
360         }
361         else
362         {
363                 rc = sstrdup (optstr);
364         }
365
366         if(!rc)
367         {
368                 DEBUG ("utils_mount: Error checking device name: optstr = %s", optstr);
369         }
370         return rc;
371 }
372
373 /* What weird OS is this..? I can't find any info with google :/ -octo */
374 #if HAVE_LISTMNTENT && 0
375 static cu_mount_t *cu_mount_listmntent (void)
376 {
377         cu_mount_t *last = *list;
378         struct tabmntent *p;
379         struct mntent *mnt;
380
381         struct tabmntent *mntlist;
382         if(listmntent(&mntlist, COLLECTD_MNTTAB, NULL, NULL) < 0) {
383 #if COLLECT_DEBUG
384                 char errbuf[1024];
385                 DEBUG("utils_mount: calling listmntent() failed: %s",
386                                 sstrerror (errno, errbuf, sizeof (errbuf)));
387 #endif /* COLLECT_DEBUG */
388         }
389
390         for(p = mntlist; p; p = p->next) {
391                 char *loop = NULL, *device = NULL;
392
393                 mnt = p->ment;
394                 loop = cu_mount_getoptionvalue(mnt->mnt_opts, "loop=");
395                 if(loop == NULL) {   /* no loop= mount */
396                         device = get_device_name(mnt->mnt_fsname);
397                         if(device == NULL) {
398                                 DEBUG("utils_mount: can't get devicename for fs (%s) %s (%s)"
399                                         ": ignored", mnt->mnt_type,
400                                         mnt->mnt_dir, mnt->mnt_fsname);
401                                 continue;
402                         }
403                 } else {
404                         device = loop;
405                 }
406                 if(*list == NULL) {
407                         *list = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
408                         last = *list;
409                 } else {
410                         while(last->next != NULL) { /* is last really last? */
411                                 last = last->next;
412                         }
413                         last->next = (cu_mount_t *)smalloc(sizeof(cu_mount_t));
414                         last = last->next;
415                 }
416                 last->dir = sstrdup(mnt->mnt_dir);
417                 last->spec_device = sstrdup(mnt->mnt_fsname);
418                 last->device = device;
419                 last->type = sstrdup(mnt->mnt_type);
420                 last->options = sstrdup(mnt->mnt_opts);
421                 last->next = NULL;
422         } /* for(p = mntlist; p; p = p->next) */
423
424         return(last);
425 } /* cu_mount_t *cu_mount_listmntent(void) */
426 /* #endif HAVE_LISTMNTENT */
427
428 /* 4.4BSD and Mac OS X (getfsstat) or NetBSD (getvfsstat) */
429 #elif HAVE_GETVFSSTAT || HAVE_GETFSSTAT
430 static cu_mount_t *cu_mount_getfsstat (void)
431 {
432 #if HAVE_GETVFSSTAT
433 #  define STRUCT_STATFS struct statvfs
434 #  define CMD_STATFS    getvfsstat
435 #  define FLAGS_STATFS  ST_NOWAIT
436 /* #endif HAVE_GETVFSSTAT */
437 #elif HAVE_GETFSSTAT
438 #  define STRUCT_STATFS struct statfs
439 #  define CMD_STATFS    getfsstat
440 #  define FLAGS_STATFS  MNT_NOWAIT
441 #endif /* HAVE_GETFSSTAT */
442
443         int bufsize;
444         STRUCT_STATFS *buf;
445
446         int num;
447         int i;
448
449         cu_mount_t *first = NULL;
450         cu_mount_t *last  = NULL;
451         cu_mount_t *new   = NULL;
452
453         /* Get the number of mounted file systems */
454         if ((bufsize = CMD_STATFS (NULL, 0, FLAGS_STATFS)) < 1)
455         {
456 #if COLLECT_DEBUG
457                 char errbuf[1024];
458                 DEBUG ("utils_mount: getv?fsstat failed: %s",
459                                 sstrerror (errno, errbuf, sizeof (errbuf)));
460 #endif /* COLLECT_DEBUG */
461                 return (NULL);
462         }
463
464         if ((buf = (STRUCT_STATFS *) malloc (bufsize * sizeof (STRUCT_STATFS)))
465                         == NULL)
466                 return (NULL);
467         memset (buf, '\0', bufsize * sizeof (STRUCT_STATFS));
468
469         /* The bufsize needs to be passed in bytes. Really. This is not in the
470          * manpage.. -octo */
471         if ((num = CMD_STATFS (buf, bufsize * sizeof (STRUCT_STATFS), FLAGS_STATFS)) < 1)
472         {
473 #if COLLECT_DEBUG
474                 char errbuf[1024];
475                 DEBUG ("utils_mount: getv?fsstat failed: %s",
476                                 sstrerror (errno, errbuf, sizeof (errbuf)));
477 #endif /* COLLECT_DEBUG */
478                 free (buf);
479                 return (NULL);
480         }
481
482         for (i = 0; i < num; i++)
483         {
484                 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
485                         break;
486                 memset (new, '\0', sizeof (cu_mount_t));
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 = malloc (sizeof (cu_mount_t))) == NULL)
539                         break;
540                 memset (new, '\0', sizeof (cu_mount_t));
541                 
542                 /* Copy values from `struct mnttab' */
543                 new->dir         = sstrdup (mt.mnt_mountp);
544                 new->spec_device = sstrdup (mt.mnt_special);
545                 new->type        = sstrdup (mt.mnt_fstype);
546                 new->options     = sstrdup (mt.mnt_mntopts);
547                 new->device      = get_device_name (new->options);
548                 new->next = NULL;
549
550                 /* Append to list */
551                 if (first == NULL)
552                 {
553                         first = new;
554                         last  = new;
555                 }
556                 else
557                 {
558                         last->next = new;
559                         last       = new;
560                 }
561         }
562
563         fclose (fp);
564
565         return (first);
566 } /* static cu_mount_t *cu_mount_gen_getmntent (void) */
567 /* #endif HAVE_TWO_GETMNTENT || HAVE_GEN_GETMNTENT || HAVE_SUN_GETMNTENT */
568
569 #elif HAVE_SEQ_GETMNTENT
570 #warn "This version of `getmntent' hat not yet been implemented!"
571 /* #endif HAVE_SEQ_GETMNTENT */
572
573 #elif HAVE_GETMNTENT_R
574 static cu_mount_t *cu_mount_getmntent (void)
575 {
576         FILE *fp;
577         struct mntent me;
578         char mntbuf[1024];
579
580         cu_mount_t *first = NULL;
581         cu_mount_t *last  = NULL;
582         cu_mount_t *new   = NULL;
583
584         DEBUG ("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
585
586         if ((fp = setmntent (COLLECTD_MNTTAB, "r")) == NULL)
587         {
588                 char errbuf[1024];
589                 ERROR ("setmntent (%s): %s", COLLECTD_MNTTAB,
590                                 sstrerror (errno, errbuf, sizeof (errbuf)));
591                 return (NULL);
592         }
593
594         while (getmntent_r (fp, &me, mntbuf, sizeof (mntbuf) ))
595         {
596                 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
597                         break;
598                 memset (new, '\0', sizeof (cu_mount_t));
599
600                 /* Copy values from `struct mntent *' */
601                 new->dir         = sstrdup (me.mnt_dir);
602                 new->spec_device = sstrdup (me.mnt_fsname);
603                 new->type        = sstrdup (me.mnt_type);
604                 new->options     = sstrdup (me.mnt_opts);
605                 new->device      = get_device_name (new->options);
606                 new->next        = NULL;
607
608                 DEBUG ("utils_mount: new = {dir = %s, spec_device = %s, type = %s, options = %s, device = %s}",
609                                 new->dir, new->spec_device, new->type, new->options, new->device);
610
611                 /* Append to list */
612                 if (first == NULL)
613                 {
614                         first = new;
615                         last  = new;
616                 }
617                 else
618                 {
619                         last->next = new;
620                         last       = new;
621                 }
622         }
623
624         endmntent (fp);
625
626         DEBUG ("utils_mount: return (0x%p)", (void *) first);
627
628         return (first);
629 } /* HAVE_GETMNTENT_R */
630
631 #elif HAVE_ONE_GETMNTENT
632 static cu_mount_t *cu_mount_getmntent (void)
633 {
634         FILE *fp;
635         struct mntent *me;
636
637         cu_mount_t *first = NULL;
638         cu_mount_t *last  = NULL;
639         cu_mount_t *new   = NULL;
640
641         DEBUG ("utils_mount: (void); COLLECTD_MNTTAB = %s", COLLECTD_MNTTAB);
642
643         if ((fp = setmntent (COLLECTD_MNTTAB, "r")) == NULL)
644         {
645                 char errbuf[1024];
646                 ERROR ("setmntent (%s): %s", COLLECTD_MNTTAB,
647                                 sstrerror (errno, errbuf, sizeof (errbuf)));
648                 return (NULL);
649         }
650
651         while ((me = getmntent (fp)) != NULL)
652         {
653                 if ((new = malloc (sizeof (cu_mount_t))) == NULL)
654                         break;
655                 memset (new, '\0', sizeof (cu_mount_t));
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, 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, 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 = (char *)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, 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