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