mountlist.c 20 KB

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