4
0

mountlist.c 28 KB

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