mountlist.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /* mountlist.c -- return a list of mounted file systems
  2. Copyright (C) 1991, 1992, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
  3. 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
  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 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. #include "mountlist.h"
  16. #include <limits.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "xalloc.h"
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24. #if HAVE_SYS_PARAM_H
  25. # include <sys/param.h>
  26. #endif
  27. #if defined MOUNTED_GETFSSTAT /* OSF_1 and Darwin1.3.x */
  28. # if HAVE_SYS_UCRED_H
  29. # include <grp.h> /* needed on OSF V4.0 for definition of NGROUPS,
  30. NGROUPS is used as an array dimension in ucred.h */
  31. # include <sys/ucred.h> /* needed by powerpc-apple-darwin1.3.7 */
  32. # endif
  33. # if HAVE_SYS_MOUNT_H
  34. # include <sys/mount.h>
  35. # endif
  36. # if HAVE_SYS_FS_TYPES_H
  37. # include <sys/fs_types.h> /* needed by powerpc-apple-darwin1.3.7 */
  38. # endif
  39. # if HAVE_STRUCT_FSSTAT_F_FSTYPENAME
  40. # define FS_TYPE(Ent) ((Ent).f_fstypename)
  41. # else
  42. # define FS_TYPE(Ent) mnt_names[(Ent).f_type]
  43. # endif
  44. #endif /* MOUNTED_GETFSSTAT */
  45. #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
  46. # include <mntent.h>
  47. # if !defined MOUNTED
  48. # if defined _PATH_MOUNTED /* GNU libc */
  49. # define MOUNTED _PATH_MOUNTED
  50. # endif
  51. # if defined MNT_MNTTAB /* HP-UX. */
  52. # define MOUNTED MNT_MNTTAB
  53. # endif
  54. # if defined MNTTABNAME /* Dynix. */
  55. # define MOUNTED MNTTABNAME
  56. # endif
  57. # endif
  58. #endif
  59. #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
  60. # include <sys/mount.h>
  61. #endif
  62. #ifdef MOUNTED_GETMNTINFO2 /* NetBSD 3.0. */
  63. # include <sys/statvfs.h>
  64. #endif
  65. #ifdef MOUNTED_GETMNT /* Ultrix. */
  66. # include <sys/mount.h>
  67. # include <sys/fs_types.h>
  68. #endif
  69. #ifdef MOUNTED_FS_STAT_DEV /* BeOS. */
  70. # include <fs_info.h>
  71. # include <dirent.h>
  72. #endif
  73. #ifdef MOUNTED_FREAD /* SVR2. */
  74. # include <mnttab.h>
  75. #endif
  76. #ifdef MOUNTED_FREAD_FSTYP /* SVR3. */
  77. # include <mnttab.h>
  78. # include <sys/fstyp.h>
  79. # include <sys/statfs.h>
  80. #endif
  81. #ifdef MOUNTED_LISTMNTENT
  82. # include <mntent.h>
  83. #endif
  84. #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
  85. # include <sys/mnttab.h>
  86. #endif
  87. #ifdef MOUNTED_VMOUNT /* AIX. */
  88. # include <fshelp.h>
  89. # include <sys/vfs.h>
  90. #endif
  91. #ifdef DOLPHIN
  92. /* So special that it's not worth putting this in autoconf. */
  93. # undef MOUNTED_FREAD_FSTYP
  94. # define MOUNTED_GETMNTTBL
  95. #endif
  96. #if HAVE_SYS_MNTENT_H
  97. /* This is to get MNTOPT_IGNORE on e.g. SVR4. */
  98. # include <sys/mntent.h>
  99. #endif
  100. #undef MNT_IGNORE
  101. #if defined MNTOPT_IGNORE && defined HAVE_HASMNTOPT
  102. # define MNT_IGNORE(M) hasmntopt ((M), MNTOPT_IGNORE)
  103. #else
  104. # define MNT_IGNORE(M) 0
  105. #endif
  106. #if USE_UNLOCKED_IO
  107. # include "unlocked-io.h"
  108. #endif
  109. #ifndef SIZE_MAX
  110. # define SIZE_MAX ((size_t) -1)
  111. #endif
  112. /* The results of open() in this file are not used with fchdir,
  113. therefore save some unnecessary work in fchdir.c. */
  114. #undef open
  115. #undef close
  116. /* The results of opendir() in this file are not used with dirfd and fchdir,
  117. therefore save some unnecessary work in fchdir.c. */
  118. #undef opendir
  119. #undef closedir
  120. #ifndef ME_DUMMY
  121. # define ME_DUMMY(Fs_name, Fs_type) \
  122. (strcmp (Fs_type, "autofs") == 0 \
  123. || strcmp (Fs_type, "none") == 0 \
  124. || strcmp (Fs_type, "proc") == 0 \
  125. || strcmp (Fs_type, "subfs") == 0 \
  126. /* for NetBSD 3.0 */ \
  127. || strcmp (Fs_type, "kernfs") == 0 \
  128. /* for Irix 6.5 */ \
  129. || strcmp (Fs_type, "ignore") == 0)
  130. #endif
  131. #ifndef ME_REMOTE
  132. /* A file system is `remote' if its Fs_name contains a `:'
  133. or if (it is of type (smbfs or cifs) and its Fs_name starts with `//'). */
  134. # define ME_REMOTE(Fs_name, Fs_type) \
  135. (strchr (Fs_name, ':') != NULL \
  136. || ((Fs_name)[0] == '/' \
  137. && (Fs_name)[1] == '/' \
  138. && (strcmp (Fs_type, "smbfs") == 0 \
  139. || strcmp (Fs_type, "cifs") == 0)))
  140. #endif
  141. #if MOUNTED_GETMNTINFO
  142. # if ! HAVE_STRUCT_STATFS_F_FSTYPENAME
  143. static char *
  144. fstype_to_string (short int t)
  145. {
  146. switch (t)
  147. {
  148. # ifdef MOUNT_PC
  149. case MOUNT_PC:
  150. return "pc";
  151. # endif
  152. # ifdef MOUNT_MFS
  153. case MOUNT_MFS:
  154. return "mfs";
  155. # endif
  156. # ifdef MOUNT_LO
  157. case MOUNT_LO:
  158. return "lo";
  159. # endif
  160. # ifdef MOUNT_TFS
  161. case MOUNT_TFS:
  162. return "tfs";
  163. # endif
  164. # ifdef MOUNT_TMP
  165. case MOUNT_TMP:
  166. return "tmp";
  167. # endif
  168. # ifdef MOUNT_UFS
  169. case MOUNT_UFS:
  170. return "ufs" ;
  171. # endif
  172. # ifdef MOUNT_NFS
  173. case MOUNT_NFS:
  174. return "nfs" ;
  175. # endif
  176. # ifdef MOUNT_MSDOS
  177. case MOUNT_MSDOS:
  178. return "msdos" ;
  179. # endif
  180. # ifdef MOUNT_LFS
  181. case MOUNT_LFS:
  182. return "lfs" ;
  183. # endif
  184. # ifdef MOUNT_LOFS
  185. case MOUNT_LOFS:
  186. return "lofs" ;
  187. # endif
  188. # ifdef MOUNT_FDESC
  189. case MOUNT_FDESC:
  190. return "fdesc" ;
  191. # endif
  192. # ifdef MOUNT_PORTAL
  193. case MOUNT_PORTAL:
  194. return "portal" ;
  195. # endif
  196. # ifdef MOUNT_NULL
  197. case MOUNT_NULL:
  198. return "null" ;
  199. # endif
  200. # ifdef MOUNT_UMAP
  201. case MOUNT_UMAP:
  202. return "umap" ;
  203. # endif
  204. # ifdef MOUNT_KERNFS
  205. case MOUNT_KERNFS:
  206. return "kernfs" ;
  207. # endif
  208. # ifdef MOUNT_PROCFS
  209. case MOUNT_PROCFS:
  210. return "procfs" ;
  211. # endif
  212. # ifdef MOUNT_AFS
  213. case MOUNT_AFS:
  214. return "afs" ;
  215. # endif
  216. # ifdef MOUNT_CD9660
  217. case MOUNT_CD9660:
  218. return "cd9660" ;
  219. # endif
  220. # ifdef MOUNT_UNION
  221. case MOUNT_UNION:
  222. return "union" ;
  223. # endif
  224. # ifdef MOUNT_DEVFS
  225. case MOUNT_DEVFS:
  226. return "devfs" ;
  227. # endif
  228. # ifdef MOUNT_EXT2FS
  229. case MOUNT_EXT2FS:
  230. return "ext2fs" ;
  231. # endif
  232. default:
  233. return "?";
  234. }
  235. }
  236. # endif
  237. static char *
  238. fsp_to_string (const struct statfs *fsp)
  239. {
  240. # if HAVE_STRUCT_STATFS_F_FSTYPENAME
  241. return (char *) (fsp->f_fstypename);
  242. # else
  243. return fstype_to_string (fsp->f_type);
  244. # endif
  245. }
  246. #endif /* MOUNTED_GETMNTINFO */
  247. #ifdef MOUNTED_VMOUNT /* AIX. */
  248. static char *
  249. fstype_to_string (int t)
  250. {
  251. struct vfs_ent *e;
  252. e = getvfsbytype (t);
  253. if (!e || !e->vfsent_name)
  254. return "none";
  255. else
  256. return e->vfsent_name;
  257. }
  258. #endif /* MOUNTED_VMOUNT */
  259. #if defined MOUNTED_GETMNTENT1 || defined MOUNTED_GETMNTENT2
  260. /* Return the device number from MOUNT_OPTIONS, if possible.
  261. Otherwise return (dev_t) -1. */
  262. static dev_t
  263. dev_from_mount_options (char const *mount_options)
  264. {
  265. /* GNU/Linux allows file system implementations to define their own
  266. meaning for "dev=" mount options, so don't trust the meaning
  267. here. */
  268. # ifndef __linux__
  269. static char const dev_pattern[] = ",dev=";
  270. char const *devopt = strstr (mount_options, dev_pattern);
  271. if (devopt)
  272. {
  273. char const *optval = devopt + sizeof dev_pattern - 1;
  274. char *optvalend;
  275. unsigned long int dev;
  276. errno = 0;
  277. dev = strtoul (optval, &optvalend, 16);
  278. if (optval != optvalend
  279. && (*optvalend == '\0' || *optvalend == ',')
  280. && ! (dev == ULONG_MAX && errno == ERANGE)
  281. && dev == (dev_t) dev)
  282. return dev;
  283. }
  284. # endif
  285. return -1;
  286. }
  287. #endif
  288. /* Return a list of the currently mounted file systems, or NULL on error.
  289. Add each entry to the tail of the list so that they stay in order.
  290. If NEED_FS_TYPE is true, ensure that the file system type fields in
  291. the returned list are valid. Otherwise, they might not be. */
  292. struct mount_entry *
  293. read_file_system_list (bool need_fs_type)
  294. {
  295. struct mount_entry *mount_list;
  296. struct mount_entry *me;
  297. struct mount_entry **mtail = &mount_list;
  298. #ifdef MOUNTED_LISTMNTENT
  299. {
  300. struct tabmntent *mntlist, *p;
  301. struct mntent *mnt;
  302. struct mount_entry *me;
  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. if (listmntent (&mntlist, KMTAB, NULL, NULL) < 0)
  308. return NULL;
  309. for (p = mntlist; p; p = p->next) {
  310. mnt = p->ment;
  311. me = xmalloc (sizeof *me);
  312. me->me_devname = xstrdup (mnt->mnt_fsname);
  313. me->me_mountdir = xstrdup (mnt->mnt_dir);
  314. me->me_type = xstrdup (mnt->mnt_type);
  315. me->me_type_malloced = 1;
  316. me->me_dummy = ME_DUMMY (me->me_devname, me->me_type);
  317. me->me_remote = ME_REMOTE (me->me_devname, me->me_type);
  318. me->me_dev = -1;
  319. *mtail = me;
  320. mtail = &me->me_next;
  321. }
  322. freemntlist (mntlist);
  323. }
  324. #endif
  325. #ifdef MOUNTED_GETMNTENT1 /* GNU/Linux, 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
  326. {
  327. struct mntent *mnt;
  328. char *table = MOUNTED;
  329. FILE *fp;
  330. fp = setmntent (table, "r");
  331. if (fp == NULL)
  332. return NULL;
  333. while ((mnt = getmntent (fp)))
  334. {
  335. me = xmalloc (sizeof *me);
  336. me->me_devname = xstrdup (mnt->mnt_fsname);
  337. me->me_mountdir = xstrdup (mnt->mnt_dir);
  338. me->me_type = xstrdup (mnt->mnt_type);
  339. me->me_type_malloced = 1;
  340. me->me_dummy = ME_DUMMY (me->me_devname, me->me_type);
  341. me->me_remote = ME_REMOTE (me->me_devname, me->me_type);
  342. me->me_dev = dev_from_mount_options (mnt->mnt_opts);
  343. /* Add to the linked list. */
  344. *mtail = me;
  345. mtail = &me->me_next;
  346. }
  347. if (endmntent (fp) == 0)
  348. goto free_then_fail;
  349. }
  350. #endif /* MOUNTED_GETMNTENT1. */
  351. #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
  352. {
  353. struct statfs *fsp;
  354. int entries;
  355. entries = getmntinfo (&fsp, MNT_NOWAIT);
  356. if (entries < 0)
  357. return NULL;
  358. for (; entries-- > 0; fsp++)
  359. {
  360. char *fs_type = fsp_to_string (fsp);
  361. me = xmalloc (sizeof *me);
  362. me->me_devname = xstrdup (fsp->f_mntfromname);
  363. me->me_mountdir = xstrdup (fsp->f_mntonname);
  364. me->me_type = fs_type;
  365. me->me_type_malloced = 0;
  366. me->me_dummy = ME_DUMMY (me->me_devname, me->me_type);
  367. me->me_remote = ME_REMOTE (me->me_devname, me->me_type);
  368. me->me_dev = (dev_t) -1; /* Magic; means not known yet. */
  369. /* Add to the linked list. */
  370. *mtail = me;
  371. mtail = &me->me_next;
  372. }
  373. }
  374. #endif /* MOUNTED_GETMNTINFO */
  375. #ifdef MOUNTED_GETMNTINFO2 /* NetBSD 3.0. */
  376. {
  377. struct statvfs *fsp;
  378. int entries;
  379. entries = getmntinfo (&fsp, MNT_NOWAIT);
  380. if (entries < 0)
  381. return NULL;
  382. for (; entries-- > 0; fsp++)
  383. {
  384. me = xmalloc (sizeof *me);
  385. me->me_devname = xstrdup (fsp->f_mntfromname);
  386. me->me_mountdir = xstrdup (fsp->f_mntonname);
  387. me->me_type = xstrdup (fsp->f_fstypename);
  388. me->me_type_malloced = 1;
  389. me->me_dummy = ME_DUMMY (me->me_devname, me->me_type);
  390. me->me_remote = ME_REMOTE (me->me_devname, me->me_type);
  391. me->me_dev = (dev_t) -1; /* Magic; means not known yet. */
  392. /* Add to the linked list. */
  393. *mtail = me;
  394. mtail = &me->me_next;
  395. }
  396. }
  397. #endif /* MOUNTED_GETMNTINFO2 */
  398. #ifdef MOUNTED_GETMNT /* Ultrix. */
  399. {
  400. int offset = 0;
  401. int val;
  402. struct fs_data fsd;
  403. while (errno = 0,
  404. 0 < (val = getmnt (&offset, &fsd, sizeof (fsd), NOSTAT_MANY,
  405. (char *) 0)))
  406. {
  407. me = xmalloc (sizeof *me);
  408. me->me_devname = xstrdup (fsd.fd_req.devname);
  409. me->me_mountdir = xstrdup (fsd.fd_req.path);
  410. me->me_type = gt_names[fsd.fd_req.fstype];
  411. me->me_type_malloced = 0;
  412. me->me_dummy = ME_DUMMY (me->me_devname, me->me_type);
  413. me->me_remote = ME_REMOTE (me->me_devname, me->me_type);
  414. me->me_dev = fsd.fd_req.dev;
  415. /* Add to the linked list. */
  416. *mtail = me;
  417. mtail = &me->me_next;
  418. }
  419. if (val < 0)
  420. goto free_then_fail;
  421. }
  422. #endif /* MOUNTED_GETMNT. */
  423. #if defined MOUNTED_FS_STAT_DEV /* BeOS */
  424. {
  425. /* The next_dev() and fs_stat_dev() system calls give the list of
  426. all file systems, including the information returned by statvfs()
  427. (fs type, total blocks, free blocks etc.), but without the mount
  428. point. But on BeOS all file systems except / are mounted in the
  429. rootfs, directly under /.
  430. The directory name of the mount point is often, but not always,
  431. identical to the volume name of the device.
  432. We therefore get the list of subdirectories of /, and the list
  433. of all file systems, and match the two lists. */
  434. DIR *dirp;
  435. struct rootdir_entry
  436. {
  437. char *name;
  438. dev_t dev;
  439. ino_t ino;
  440. struct rootdir_entry *next;
  441. };
  442. struct rootdir_entry *rootdir_list;
  443. struct rootdir_entry **rootdir_tail;
  444. int32 pos;
  445. dev_t dev;
  446. fs_info fi;
  447. /* All volumes are mounted in the rootfs, directly under /. */
  448. rootdir_list = NULL;
  449. rootdir_tail = &rootdir_list;
  450. dirp = opendir ("/");
  451. if (dirp)
  452. {
  453. struct dirent *d;
  454. while ((d = readdir (dirp)) != NULL)
  455. {
  456. char *name;
  457. struct stat statbuf;
  458. if (strcmp (d->d_name, "..") == 0)
  459. continue;
  460. if (strcmp (d->d_name, ".") == 0)
  461. name = xstrdup ("/");
  462. else
  463. {
  464. name = xmalloc (1 + strlen (d->d_name) + 1);
  465. name[0] = '/';
  466. strcpy (name + 1, d->d_name);
  467. }
  468. if (lstat (name, &statbuf) >= 0 && S_ISDIR (statbuf.st_mode))
  469. {
  470. struct rootdir_entry *re = xmalloc (sizeof *re);
  471. re->name = name;
  472. re->dev = statbuf.st_dev;
  473. re->ino = statbuf.st_ino;
  474. /* Add to the linked list. */
  475. *rootdir_tail = re;
  476. rootdir_tail = &re->next;
  477. }
  478. else
  479. free (name);
  480. }
  481. closedir (dirp);
  482. }
  483. *rootdir_tail = NULL;
  484. for (pos = 0; (dev = next_dev (&pos)) >= 0; )
  485. if (fs_stat_dev (dev, &fi) >= 0)
  486. {
  487. /* Note: fi.dev == dev. */
  488. struct rootdir_entry *re;
  489. for (re = rootdir_list; re; re = re->next)
  490. if (re->dev == fi.dev && re->ino == fi.root)
  491. break;
  492. me = xmalloc (sizeof *me);
  493. me->me_devname = xstrdup (fi.device_name[0] != '\0' ? fi.device_name : fi.fsh_name);
  494. me->me_mountdir = xstrdup (re != NULL ? re->name : fi.fsh_name);
  495. me->me_type = xstrdup (fi.fsh_name);
  496. me->me_type_malloced = 1;
  497. me->me_dev = fi.dev;
  498. me->me_dummy = 0;
  499. me->me_remote = (fi.flags & B_FS_IS_SHARED) != 0;
  500. /* Add to the linked list. */
  501. *mtail = me;
  502. mtail = &me->me_next;
  503. }
  504. *mtail = NULL;
  505. while (rootdir_list != NULL)
  506. {
  507. struct rootdir_entry *re = rootdir_list;
  508. rootdir_list = re->next;
  509. free (re->name);
  510. free (re);
  511. }
  512. }
  513. #endif /* MOUNTED_FS_STAT_DEV */
  514. #if defined MOUNTED_GETFSSTAT /* __alpha running OSF_1 */
  515. {
  516. int numsys, counter;
  517. size_t bufsize;
  518. struct statfs *stats;
  519. numsys = getfsstat ((struct statfs *)0, 0L, MNT_NOWAIT);
  520. if (numsys < 0)
  521. return (NULL);
  522. if (SIZE_MAX / sizeof *stats <= numsys)
  523. xalloc_die ();
  524. bufsize = (1 + numsys) * sizeof *stats;
  525. stats = xmalloc (bufsize);
  526. numsys = getfsstat (stats, bufsize, MNT_NOWAIT);
  527. if (numsys < 0)
  528. {
  529. free (stats);
  530. return (NULL);
  531. }
  532. for (counter = 0; counter < numsys; counter++)
  533. {
  534. me = xmalloc (sizeof *me);
  535. me->me_devname = xstrdup (stats[counter].f_mntfromname);
  536. me->me_mountdir = xstrdup (stats[counter].f_mntonname);
  537. me->me_type = xstrdup (FS_TYPE (stats[counter]));
  538. me->me_type_malloced = 1;
  539. me->me_dummy = ME_DUMMY (me->me_devname, me->me_type);
  540. me->me_remote = ME_REMOTE (me->me_devname, me->me_type);
  541. me->me_dev = (dev_t) -1; /* Magic; means not known yet. */
  542. /* Add to the linked list. */
  543. *mtail = me;
  544. mtail = &me->me_next;
  545. }
  546. free (stats);
  547. }
  548. #endif /* MOUNTED_GETFSSTAT */
  549. #if defined MOUNTED_FREAD || defined MOUNTED_FREAD_FSTYP /* SVR[23]. */
  550. {
  551. struct mnttab mnt;
  552. char *table = "/etc/mnttab";
  553. FILE *fp;
  554. fp = fopen (table, "r");
  555. if (fp == NULL)
  556. return NULL;
  557. while (fread (&mnt, sizeof mnt, 1, fp) > 0)
  558. {
  559. me = xmalloc (sizeof *me);
  560. # ifdef GETFSTYP /* SVR3. */
  561. me->me_devname = xstrdup (mnt.mt_dev);
  562. # else
  563. me->me_devname = xmalloc (strlen (mnt.mt_dev) + 6);
  564. strcpy (me->me_devname, "/dev/");
  565. strcpy (me->me_devname + 5, mnt.mt_dev);
  566. # endif
  567. me->me_mountdir = xstrdup (mnt.mt_filsys);
  568. me->me_dev = (dev_t) -1; /* Magic; means not known yet. */
  569. me->me_type = "";
  570. me->me_type_malloced = 0;
  571. # ifdef GETFSTYP /* SVR3. */
  572. if (need_fs_type)
  573. {
  574. struct statfs fsd;
  575. char typebuf[FSTYPSZ];
  576. if (statfs (me->me_mountdir, &fsd, sizeof fsd, 0) != -1
  577. && sysfs (GETFSTYP, fsd.f_fstyp, typebuf) != -1)
  578. {
  579. me->me_type = xstrdup (typebuf);
  580. me->me_type_malloced = 1;
  581. }
  582. }
  583. # endif
  584. me->me_dummy = ME_DUMMY (me->me_devname, me->me_type);
  585. me->me_remote = ME_REMOTE (me->me_devname, me->me_type);
  586. /* Add to the linked list. */
  587. *mtail = me;
  588. mtail = &me->me_next;
  589. }
  590. if (ferror (fp))
  591. {
  592. /* The last fread() call must have failed. */
  593. int saved_errno = errno;
  594. fclose (fp);
  595. errno = saved_errno;
  596. goto free_then_fail;
  597. }
  598. if (fclose (fp) == EOF)
  599. goto free_then_fail;
  600. }
  601. #endif /* MOUNTED_FREAD || MOUNTED_FREAD_FSTYP. */
  602. #ifdef MOUNTED_GETMNTTBL /* DolphinOS goes its own way. */
  603. {
  604. struct mntent **mnttbl = getmnttbl (), **ent;
  605. for (ent=mnttbl;*ent;ent++)
  606. {
  607. me = xmalloc (sizeof *me);
  608. me->me_devname = xstrdup ( (*ent)->mt_resource);
  609. me->me_mountdir = xstrdup ( (*ent)->mt_directory);
  610. me->me_type = xstrdup ((*ent)->mt_fstype);
  611. me->me_type_malloced = 1;
  612. me->me_dummy = ME_DUMMY (me->me_devname, me->me_type);
  613. me->me_remote = ME_REMOTE (me->me_devname, me->me_type);
  614. me->me_dev = (dev_t) -1; /* Magic; means not known yet. */
  615. /* Add to the linked list. */
  616. *mtail = me;
  617. mtail = &me->me_next;
  618. }
  619. endmnttbl ();
  620. }
  621. #endif
  622. #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
  623. {
  624. struct mnttab mnt;
  625. char *table = MNTTAB;
  626. FILE *fp;
  627. int ret;
  628. int lockfd = -1;
  629. # if defined F_RDLCK && defined F_SETLKW
  630. /* MNTTAB_LOCK is a macro name of our own invention; it's not present in
  631. e.g. Solaris 2.6. If the SVR4 folks ever define a macro
  632. for this file name, we should use their macro name instead.
  633. (Why not just lock MNTTAB directly? We don't know.) */
  634. # ifndef MNTTAB_LOCK
  635. # define MNTTAB_LOCK "/etc/.mnttab.lock"
  636. # endif
  637. lockfd = open (MNTTAB_LOCK, O_RDONLY);
  638. if (0 <= lockfd)
  639. {
  640. struct flock flock;
  641. flock.l_type = F_RDLCK;
  642. flock.l_whence = SEEK_SET;
  643. flock.l_start = 0;
  644. flock.l_len = 0;
  645. while (fcntl (lockfd, F_SETLKW, &flock) == -1)
  646. if (errno != EINTR)
  647. {
  648. int saved_errno = errno;
  649. close (lockfd);
  650. errno = saved_errno;
  651. return NULL;
  652. }
  653. }
  654. else if (errno != ENOENT)
  655. return NULL;
  656. # endif
  657. errno = 0;
  658. fp = fopen (table, "r");
  659. if (fp == NULL)
  660. ret = errno;
  661. else
  662. {
  663. while ((ret = getmntent (fp, &mnt)) == 0)
  664. {
  665. me = xmalloc (sizeof *me);
  666. me->me_devname = xstrdup (mnt.mnt_special);
  667. me->me_mountdir = xstrdup (mnt.mnt_mountp);
  668. me->me_type = xstrdup (mnt.mnt_fstype);
  669. me->me_type_malloced = 1;
  670. me->me_dummy = MNT_IGNORE (&mnt) != 0;
  671. me->me_remote = ME_REMOTE (me->me_devname, me->me_type);
  672. me->me_dev = dev_from_mount_options (mnt.mnt_mntopts);
  673. /* Add to the linked list. */
  674. *mtail = me;
  675. mtail = &me->me_next;
  676. }
  677. ret = fclose (fp) == EOF ? errno : 0 < ret ? 0 : -1;
  678. }
  679. if (0 <= lockfd && close (lockfd) != 0)
  680. ret = errno;
  681. if (0 <= ret)
  682. {
  683. errno = ret;
  684. goto free_then_fail;
  685. }
  686. }
  687. #endif /* MOUNTED_GETMNTENT2. */
  688. #ifdef MOUNTED_VMOUNT /* AIX. */
  689. {
  690. int bufsize;
  691. char *entries, *thisent;
  692. struct vmount *vmp;
  693. int n_entries;
  694. int i;
  695. /* Ask how many bytes to allocate for the mounted file system info. */
  696. if (mntctl (MCTL_QUERY, sizeof bufsize, (struct vmount *) &bufsize) != 0)
  697. return NULL;
  698. entries = xmalloc (bufsize);
  699. /* Get the list of mounted file systems. */
  700. n_entries = mntctl (MCTL_QUERY, bufsize, (struct vmount *) entries);
  701. if (n_entries < 0)
  702. {
  703. int saved_errno = errno;
  704. free (entries);
  705. errno = saved_errno;
  706. return NULL;
  707. }
  708. for (i = 0, thisent = entries;
  709. i < n_entries;
  710. i++, thisent += vmp->vmt_length)
  711. {
  712. char *options, *ignore;
  713. vmp = (struct vmount *) thisent;
  714. me = xmalloc (sizeof *me);
  715. if (vmp->vmt_flags & MNT_REMOTE)
  716. {
  717. char *host, *dir;
  718. me->me_remote = 1;
  719. /* Prepend the remote dirname. */
  720. host = thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off;
  721. dir = thisent + vmp->vmt_data[VMT_OBJECT].vmt_off;
  722. me->me_devname = xmalloc (strlen (host) + strlen (dir) + 2);
  723. strcpy (me->me_devname, host);
  724. strcat (me->me_devname, ":");
  725. strcat (me->me_devname, dir);
  726. }
  727. else
  728. {
  729. me->me_remote = 0;
  730. me->me_devname = xstrdup (thisent +
  731. vmp->vmt_data[VMT_OBJECT].vmt_off);
  732. }
  733. me->me_mountdir = xstrdup (thisent + vmp->vmt_data[VMT_STUB].vmt_off);
  734. me->me_type = xstrdup (fstype_to_string (vmp->vmt_gfstype));
  735. me->me_type_malloced = 1;
  736. options = thisent + vmp->vmt_data[VMT_ARGS].vmt_off;
  737. ignore = strstr (options, "ignore");
  738. me->me_dummy = (ignore
  739. && (ignore == options || ignore[-1] == ',')
  740. && (ignore[sizeof "ignore" - 1] == ','
  741. || ignore[sizeof "ignore" - 1] == '\0'));
  742. me->me_dev = (dev_t) -1; /* vmt_fsid might be the info we want. */
  743. /* Add to the linked list. */
  744. *mtail = me;
  745. mtail = &me->me_next;
  746. }
  747. free (entries);
  748. }
  749. #endif /* MOUNTED_VMOUNT. */
  750. *mtail = NULL;
  751. return mount_list;
  752. free_then_fail:
  753. {
  754. int saved_errno = errno;
  755. *mtail = NULL;
  756. while (mount_list)
  757. {
  758. me = mount_list->me_next;
  759. free (mount_list->me_devname);
  760. free (mount_list->me_mountdir);
  761. if (mount_list->me_type_malloced)
  762. free (mount_list->me_type);
  763. free (mount_list);
  764. mount_list = me;
  765. }
  766. errno = saved_errno;
  767. return NULL;
  768. }
  769. }