mountlist.c 23 KB

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