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