mountlist.c 19 KB

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