044a77055b0b4f38cc20b80aa3c58ceaba1e0ce3
[gnulib.git] / lib / mountlist.c
1 /* mountlist.c -- return a list of mounted filesystems
2    Copyright (C) 1991, 1992, 1997, 1998 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #if HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include "mountlist.h"
25
26 #ifdef STDC_HEADERS
27 # include <stdlib.h>
28 #else
29 void free ();
30 #endif
31 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
32 # include <string.h>
33 #else
34 # include <strings.h>
35 #endif
36
37 #ifndef strstr
38 char *strstr ();
39 #endif
40 char *xmalloc ();
41 char *xrealloc ();
42 char *xstrdup ();
43 void error ();
44
45 #include <errno.h>
46 #ifndef errno
47 extern int errno;
48 #endif
49
50 #ifdef HAVE_FCNTL_H
51 # include <fcntl.h>
52 #endif
53
54 #ifdef HAVE_UNISTD_H
55 # include <unistd.h>
56 #endif
57
58 #if HAVE_SYS_PARAM_H
59 # include <sys/param.h>
60 #endif
61
62 #if defined (MOUNTED_GETFSSTAT) /* __alpha running OSF_1 */
63 # include <sys/mount.h>
64 # include <sys/fs_types.h>
65 #endif /* MOUNTED_GETFSSTAT */
66
67 #ifdef MOUNTED_GETMNTENT1       /* 4.3BSD, SunOS, HP-UX, Dynix, Irix.  */
68 # include <mntent.h>
69 # if !defined(MOUNTED)
70 #  if defined(MNT_MNTTAB)       /* HP-UX.  */
71 #   define MOUNTED MNT_MNTTAB
72 #  endif
73 #  if defined(MNTTABNAME)       /* Dynix.  */
74 #   define MOUNTED MNTTABNAME
75 #  endif
76 # endif
77 #endif
78
79 #ifdef MOUNTED_GETMNTINFO       /* 4.4BSD.  */
80 # include <sys/mount.h>
81 #endif
82
83 #ifdef MOUNTED_GETMNT           /* Ultrix.  */
84 # include <sys/mount.h>
85 # include <sys/fs_types.h>
86 #endif
87
88 #ifdef MOUNTED_FREAD            /* SVR2.  */
89 # include <mnttab.h>
90 #endif
91
92 #ifdef MOUNTED_FREAD_FSTYP      /* SVR3.  */
93 # include <mnttab.h>
94 # include <sys/fstyp.h>
95 # include <sys/statfs.h>
96 #endif
97
98 #ifdef MOUNTED_LISTMNTENT
99 # include <mntent.h>
100 #endif
101
102 #ifdef MOUNTED_GETMNTENT2       /* SVR4.  */
103 # include <sys/mnttab.h>
104 #endif
105
106 #ifdef MOUNTED_VMOUNT           /* AIX.  */
107 # include <fshelp.h>
108 # include <sys/vfs.h>
109 #endif
110
111 #ifdef DOLPHIN
112 /* So special that it's not worth putting this in autoconf.  */
113 # undef MOUNTED_FREAD_FSTYP
114 # define MOUNTED_GETMNTTBL
115 #endif
116
117 #if HAVE_SYS_MNTENT_H
118 /* This is to get MNTOPT_IGNORE on e.g. SVR4.  */
119 # include <sys/mntent.h>
120 #endif
121
122 #if defined (MNTOPT_IGNORE) && defined (HAVE_HASMNTOPT)
123 # define MNT_IGNORE(M) hasmntopt ((M), MNTOPT_IGNORE)
124 #else
125 # define MNT_IGNORE(M) 0
126 #endif
127
128 #ifdef MOUNTED_GETMNTENT1       /* 4.3BSD, SunOS, HP-UX, Dynix, Irix.  */
129 /* Return the value of the hexadecimal number represented by CP.
130    No prefix (like '0x') or suffix (like 'h') is expected to be
131    part of CP. */
132
133 static int
134 xatoi (cp)
135      char *cp;
136 {
137   int val;
138
139   val = 0;
140   while (*cp)
141     {
142       if (*cp >= 'a' && *cp <= 'f')
143         val = val * 16 + *cp - 'a' + 10;
144       else if (*cp >= 'A' && *cp <= 'F')
145         val = val * 16 + *cp - 'A' + 10;
146       else if (*cp >= '0' && *cp <= '9')
147         val = val * 16 + *cp - '0';
148       else
149         break;
150       cp++;
151     }
152   return val;
153 }
154 #endif /* MOUNTED_GETMNTENT1.  */
155
156 #if MOUNTED_GETMNTINFO
157
158 # if ! HAVE_F_FSTYPENAME_IN_STATFS
159 static char *
160 fstype_to_string (short t)
161 {
162   switch (t)
163     {
164 #  ifdef MOUNT_PC
165     case MOUNT_PC:
166       return "pc";
167 #  endif
168 #  ifdef MOUNT_MFS
169     case MOUNT_MFS:
170       return "mfs";
171 #  endif
172 #  ifdef MOUNT_LO
173     case MOUNT_LO:
174       return "lo";
175 #  endif
176 #  ifdef MOUNT_TFS
177     case MOUNT_TFS:
178       return "tfs";
179 #  endif
180 #  ifdef MOUNT_TMP
181     case MOUNT_TMP:
182       return "tmp";
183 #  endif
184 #  ifdef MOUNT_UFS
185    case MOUNT_UFS:
186      return "ufs" ;
187 #  endif
188 #  ifdef MOUNT_NFS
189    case MOUNT_NFS:
190      return "nfs" ;
191 #  endif
192 #  ifdef MOUNT_MSDOS
193    case MOUNT_MSDOS:
194      return "msdos" ;
195 #  endif
196 #  ifdef MOUNT_LFS
197    case MOUNT_LFS:
198      return "lfs" ;
199 #  endif
200 #  ifdef MOUNT_LOFS
201    case MOUNT_LOFS:
202      return "lofs" ;
203 #  endif
204 #  ifdef MOUNT_FDESC
205    case MOUNT_FDESC:
206      return "fdesc" ;
207 #  endif
208 #  ifdef MOUNT_PORTAL
209    case MOUNT_PORTAL:
210      return "portal" ;
211 #  endif
212 #  ifdef MOUNT_NULL
213    case MOUNT_NULL:
214      return "null" ;
215 #  endif
216 #  ifdef MOUNT_UMAP
217    case MOUNT_UMAP:
218      return "umap" ;
219 #  endif
220 #  ifdef MOUNT_KERNFS
221    case MOUNT_KERNFS:
222      return "kernfs" ;
223 #  endif
224 #  ifdef MOUNT_PROCFS
225    case MOUNT_PROCFS:
226      return "procfs" ;
227 #  endif
228 #  ifdef MOUNT_AFS
229    case MOUNT_AFS:
230      return "afs" ;
231 #  endif
232 #  ifdef MOUNT_CD9660
233    case MOUNT_CD9660:
234      return "cd9660" ;
235 #  endif
236 #  ifdef MOUNT_UNION
237    case MOUNT_UNION:
238      return "union" ;
239 #  endif
240 #  ifdef MOUNT_DEVFS
241    case MOUNT_DEVFS:
242      return "devfs" ;
243 #  endif
244 #  ifdef MOUNT_EXT2FS
245    case MOUNT_EXT2FS:
246      return "ext2fs" ;
247 #  endif
248     default:
249       return "?";
250     }
251 }
252 # endif /* ! HAVE_F_FSTYPENAME_IN_STATFS */
253
254 /* __NetBSD__ || BSD_NET2 || __OpenBSD__ */
255 static char *
256 fsp_to_string (const struct statfs *fsp)
257 {
258 # if defined HAVE_F_FSTYPENAME_IN_STATFS
259   return xstrdup (fsp->f_fstypename);
260 # else
261   return fstype_to_string (fsp->f_type);
262 # endif
263 }
264
265 #endif /* MOUNTED_GETMNTINFO */
266
267 #ifdef MOUNTED_VMOUNT           /* AIX.  */
268 static char *
269 fstype_to_string (t)
270      int t;
271 {
272   struct vfs_ent *e;
273
274   e = getvfsbytype (t);
275   if (!e || !e->vfsent_name)
276     return "none";
277   else
278     return e->vfsent_name;
279 }
280 #endif /* MOUNTED_VMOUNT */
281
282 /* Return a list of the currently mounted filesystems, or NULL on error.
283    Add each entry to the tail of the list so that they stay in order.
284    If NEED_FS_TYPE is nonzero, ensure that the filesystem type fields in
285    the returned list are valid.  Otherwise, they might not be.
286    If ALL_FS is zero, do not return entries for filesystems that
287    are automounter (dummy) entries.  */
288
289 struct mount_entry *
290 read_filesystem_list (need_fs_type, all_fs)
291      int need_fs_type, all_fs;
292 {
293   struct mount_entry *mount_list;
294   struct mount_entry *me;
295   struct mount_entry **mtail = &mount_list;
296
297 #ifdef MOUNTED_LISTMNTENT
298   {
299     struct tabmntent *mntlist, *p;
300     struct mntent *mnt;
301     struct mount_entry *me;
302
303     /* the third and fourth arguments could be used to filter mounts,
304        but Crays doesn't seem to have any mounts that we want to
305        remove. Specifically, automount create normal NFS mounts.
306        */
307
308     if(listmntent(&mntlist, KMTAB, NULL, NULL) < 0)
309       return NULL;
310     p = mntlist;
311     while(p){
312       mnt = p->ment;
313       me = (struct mount_entry*) xmalloc(sizeof (struct mount_entry));
314       me->me_devname = xstrdup(mnt->mnt_fsname);
315       me->me_mountdir = xstrdup(mnt->mnt_dir);
316       me->me_type = xstrdup(mnt->mnt_type);
317       me->me_dev = -1;
318       *mtail = me;
319       mtail = &me->me_next;
320       p = p->next;
321     }
322     freemntlist(mntlist);
323   }
324 #endif
325
326 #ifdef MOUNTED_GETMNTENT1       /* 4.3BSD, SunOS, HP-UX, Dynix, Irix.  */
327   {
328     struct mntent *mnt;
329     char *table = MOUNTED;
330     FILE *fp;
331     char *devopt;
332
333     fp = setmntent (table, "r");
334     if (fp == NULL)
335       return NULL;
336
337     while ((mnt = getmntent (fp)))
338       {
339         if (!all_fs && (!strcmp (mnt->mnt_type, "ignore")
340                         || !strcmp (mnt->mnt_type, "auto")))
341           continue;
342
343         me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
344         me->me_devname = xstrdup (mnt->mnt_fsname);
345         me->me_mountdir = xstrdup (mnt->mnt_dir);
346         me->me_type = xstrdup (mnt->mnt_type);
347         devopt = strstr (mnt->mnt_opts, "dev=");
348         if (devopt)
349           {
350             if (devopt[4] == '0' && (devopt[5] == 'x' || devopt[5] == 'X'))
351               me->me_dev = xatoi (devopt + 6);
352             else
353               me->me_dev = xatoi (devopt + 4);
354           }
355         else
356           me->me_dev = (dev_t) -1;      /* Magic; means not known yet. */
357
358         /* Add to the linked list. */
359         *mtail = me;
360         mtail = &me->me_next;
361       }
362
363     if (endmntent (fp) == 0)
364       goto free_then_fail;
365   }
366 #endif /* MOUNTED_GETMNTENT1. */
367
368 #ifdef MOUNTED_GETMNTINFO       /* 4.4BSD.  */
369   {
370     struct statfs *fsp;
371     int entries;
372
373     entries = getmntinfo (&fsp, MNT_NOWAIT);
374     if (entries < 0)
375       return NULL;
376     while (entries-- > 0)
377       {
378         me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
379         me->me_devname = xstrdup (fsp->f_mntfromname);
380         me->me_mountdir = xstrdup (fsp->f_mntonname);
381         me->me_type = fsp_to_string (fsp);
382         me->me_dev = (dev_t) -1;        /* Magic; means not known yet. */
383
384         /* Add to the linked list. */
385         *mtail = me;
386         mtail = &me->me_next;
387         fsp++;
388       }
389   }
390 #endif /* MOUNTED_GETMNTINFO */
391
392 #ifdef MOUNTED_GETMNT           /* Ultrix.  */
393   {
394     int offset = 0;
395     int val;
396     struct fs_data fsd;
397
398     while (errno = 0,
399            0 <= (val = getmnt (&offset, &fsd, sizeof (fsd), NOSTAT_MANY,
400                                (char *) 0)))
401       {
402         me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
403         me->me_devname = xstrdup (fsd.fd_req.devname);
404         me->me_mountdir = xstrdup (fsd.fd_req.path);
405         me->me_type = gt_names[fsd.fd_req.fstype];
406         me->me_dev = fsd.fd_req.dev;
407
408         /* Add to the linked list. */
409         *mtail = me;
410         mtail = &me->me_next;
411       }
412     if (val < 0)
413       goto free_then_fail;
414   }
415 #endif /* MOUNTED_GETMNT. */
416
417 #if defined (MOUNTED_GETFSSTAT) /* __alpha running OSF_1 */
418   {
419     int numsys, counter, bufsize;
420     struct statfs *stats;
421
422     numsys = getfsstat ((struct statfs *)0, 0L, MNT_WAIT);
423     if (numsys < 0)
424       return (NULL);
425
426     bufsize = (1 + numsys) * sizeof (struct statfs);
427     stats = (struct statfs *)xmalloc (bufsize);
428     numsys = getfsstat (stats, bufsize, MNT_WAIT);
429
430     if (numsys < 0)
431       {
432         free (stats);
433         return (NULL);
434       }
435
436     for (counter = 0; counter < numsys; counter++)
437       {
438         me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
439         me->me_devname = xstrdup (stats[counter].f_mntfromname);
440         me->me_mountdir = xstrdup (stats[counter].f_mntonname);
441         me->me_type = mnt_names[stats[counter].f_type];
442         me->me_dev = (dev_t) -1;        /* Magic; means not known yet. */
443
444         /* Add to the linked list. */
445         *mtail = me;
446         mtail = &me->me_next;
447       }
448
449     free (stats);
450   }
451 #endif /* MOUNTED_GETFSSTAT */
452
453 #if defined (MOUNTED_FREAD) || defined (MOUNTED_FREAD_FSTYP) /* SVR[23].  */
454   {
455     struct mnttab mnt;
456     char *table = "/etc/mnttab";
457     FILE *fp;
458
459     fp = fopen (table, "r");
460     if (fp == NULL)
461       return NULL;
462
463     while (fread (&mnt, sizeof mnt, 1, fp) > 0)
464       {
465         me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
466 # ifdef GETFSTYP                        /* SVR3.  */
467         me->me_devname = xstrdup (mnt.mt_dev);
468 # else
469         me->me_devname = xmalloc (strlen (mnt.mt_dev) + 6);
470         strcpy (me->me_devname, "/dev/");
471         strcpy (me->me_devname + 5, mnt.mt_dev);
472 # endif
473         me->me_mountdir = xstrdup (mnt.mt_filsys);
474         me->me_dev = (dev_t) -1;        /* Magic; means not known yet. */
475         me->me_type = "";
476 # ifdef GETFSTYP                        /* SVR3.  */
477         if (need_fs_type)
478           {
479             struct statfs fsd;
480             char typebuf[FSTYPSZ];
481
482             if (statfs (me->me_mountdir, &fsd, sizeof fsd, 0) != -1
483                 && sysfs (GETFSTYP, fsd.f_fstyp, typebuf) != -1)
484               me->me_type = xstrdup (typebuf);
485           }
486 # endif
487
488         /* Add to the linked list. */
489         *mtail = me;
490         mtail = &me->me_next;
491       }
492
493     if (ferror (fp))
494       {
495         int saved_errno = errno;
496         fclose (fp);
497         errno = saved_errno;
498         goto free_then_fail;
499       }
500
501     if (fclose (fp) == EOF)
502       goto free_then_fail;
503   }
504 #endif /* MOUNTED_FREAD || MOUNTED_FREAD_FSTYP.  */
505
506 #ifdef MOUNTED_GETMNTTBL        /* DolphinOS goes it's own way */
507   {
508     struct mntent **mnttbl=getmnttbl(),**ent;
509     for (ent=mnttbl;*ent;ent++)
510       {
511         me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
512         me->me_devname = xstrdup ( (*ent)->mt_resource);
513         me->me_mountdir = xstrdup( (*ent)->mt_directory);
514         me->me_type =  xstrdup ((*ent)->mt_fstype);
515         me->me_dev = (dev_t) -1;        /* Magic; means not known yet. */
516
517         /* Add to the linked list. */
518         *mtail = me;
519         mtail = &me->me_next;
520       }
521     endmnttbl();
522   }
523 #endif
524
525 #ifdef MOUNTED_GETMNTENT2       /* SVR4.  */
526   {
527     struct mnttab mnt;
528     char *table = MNTTAB;
529     FILE *fp;
530     int ret;
531     int lockfd = -1;
532
533 # if defined F_RDLCK && defined F_SETLKW
534     /* MNTTAB_LOCK is a macro name of our own invention; it's not present in
535        e.g. Solaris 2.6.  If the SVR4 folks ever define a macro
536        for this file name, we should use their macro name instead.
537        (Why not just lock MNTTAB directly?  We don't know.)  */
538 #  ifndef MNTTAB_LOCK
539 #   define MNTTAB_LOCK "/etc/.mnttab.lock"
540 #  endif
541     lockfd = open (MNTTAB_LOCK, O_RDONLY);
542     if (0 <= lockfd)
543       {
544         struct flock flock;
545         flock.l_type = F_RDLCK;
546         flock.l_whence = SEEK_SET;
547         flock.l_start = 0;
548         flock.l_len = 0;
549         while (fcntl (lockfd, F_SETLKW, &flock) == -1)
550           if (errno != EINTR)
551             {
552               int saved_errno = errno;
553               close (lockfd);
554               errno = saved_errno;
555               return NULL;
556             }
557       }
558     else if (errno != ENOENT)
559       return NULL;
560 # endif
561
562     errno = 0;
563     fp = fopen (table, "r");
564     if (fp == NULL)
565       ret = errno;
566     else
567       {
568         while ((ret = getmntent (fp, &mnt)) == 0)
569           {
570             /* Don't show automounted filesystems twice on e.g., Solaris.  */
571             if (!all_fs && MNT_IGNORE (&mnt))
572               continue;
573
574             me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
575             me->me_devname = xstrdup (mnt.mnt_special);
576             me->me_mountdir = xstrdup (mnt.mnt_mountp);
577             me->me_type = xstrdup (mnt.mnt_fstype);
578             me->me_dev = (dev_t) -1;    /* Magic; means not known yet. */
579
580             /* Add to the linked list. */
581             *mtail = me;
582             mtail = &me->me_next;
583           }
584
585         ret = fclose (fp) == EOF ? errno : 0 < ret ? 0 : -1;
586       }
587
588     if (0 <= lockfd && close (lockfd) != 0)
589       ret = errno;
590
591     if (0 <= ret)
592       {
593         errno = ret;
594         goto free_then_fail;
595       }
596   }
597 #endif /* MOUNTED_GETMNTENT2.  */
598
599 #ifdef MOUNTED_VMOUNT           /* AIX.  */
600   {
601     int bufsize;
602     char *entries, *thisent;
603     struct vmount *vmp;
604
605     /* Ask how many bytes to allocate for the mounted filesystem info.  */
606     mntctl (MCTL_QUERY, sizeof bufsize, (struct vmount *) &bufsize);
607     entries = xmalloc (bufsize);
608
609     /* Get the list of mounted filesystems.  */
610     mntctl (MCTL_QUERY, bufsize, (struct vmount *) entries);
611
612     for (thisent = entries; thisent < entries + bufsize;
613          thisent += vmp->vmt_length)
614       {
615         vmp = (struct vmount *) thisent;
616         me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
617         if (vmp->vmt_flags & MNT_REMOTE)
618           {
619             char *host, *path;
620
621             /* Prepend the remote pathname.  */
622             host = thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off;
623             path = thisent + vmp->vmt_data[VMT_OBJECT].vmt_off;
624             me->me_devname = xmalloc (strlen (host) + strlen (path) + 2);
625             strcpy (me->me_devname, host);
626             strcat (me->me_devname, ":");
627             strcat (me->me_devname, path);
628           }
629         else
630           {
631             me->me_devname = xstrdup (thisent +
632                                       vmp->vmt_data[VMT_OBJECT].vmt_off);
633           }
634         me->me_mountdir = xstrdup (thisent + vmp->vmt_data[VMT_STUB].vmt_off);
635         me->me_type = xstrdup (fstype_to_string (vmp->vmt_gfstype));
636         me->me_dev = (dev_t) -1; /* vmt_fsid might be the info we want.  */
637
638         /* Add to the linked list. */
639         *mtail = me;
640         mtail = &me->me_next;
641       }
642     free (entries);
643   }
644 #endif /* MOUNTED_VMOUNT. */
645
646   *mtail = NULL;
647   return mount_list;
648
649
650  free_then_fail:
651   {
652     int saved_errno = errno;
653     *mtail = NULL;
654     
655     while (mount_list)
656       {
657         me = mount_list->me_next;
658         free (mount_list);
659         mount_list = me;
660       }
661
662     errno = saved_errno;
663     return NULL;
664   }
665 }