mountlist.c 26 KB

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