PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/watchdog-5.12/src/umount.c

#
C | 452 lines | 342 code | 51 blank | 59 comment | 100 complexity | 7754aa5648f097dd3d1da1376d4b872a MD5 | raw file
  1. /* $Header: /cvsroot/watchdog/watchdog/src/umount.c,v 1.2 2006/07/31 09:39:23 meskes Exp $ */
  2. /*
  3. * A umount(8) for Linux 0.99.
  4. * umount.c,v 1.1.1.1 1993/11/18 08:40:51 jrs Exp
  5. *
  6. * Wed Sep 14 22:43:54 1994: Sebastian Lederer
  7. * (lederer@next-pc.informatik.uni-bonn.de) added support for sending an
  8. * unmount RPC call to the server when an NFS-filesystem is unmounted.
  9. *
  10. * Tue Sep 26 16:33:09 1995: Added patches from Greg Page (greg@caldera.com)
  11. * so that NetWare filesystems can be unmounted.
  12. *
  13. * 951213: Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>:
  14. * Ignore any RPC errors, so that you can umount an nfs mounted filesystem
  15. * if the server is down.
  16. *
  17. * 960223: aeb - several minor changes
  18. * 960324: aeb - added some changes from Rob Leslie <rob@mars.org>
  19. * 960823: aeb - also try umount(spec) when umount(node) fails
  20. * 970307: aeb - canonise names from fstab
  21. * 970726: aeb - remount read-only in cases where umount fails
  22. */
  23. #include <unistd.h>
  24. #include <getopt.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <sys/stat.h>
  29. #include <sys/mount.h>
  30. #include "mount_constants.h"
  31. #include "sundries.h"
  32. #include "lomount.h"
  33. #include "loop.h"
  34. #include "fstab.h"
  35. #if HAVE_NFS
  36. #include <sys/socket.h>
  37. #include <sys/time.h>
  38. #include <netdb.h>
  39. #include <rpc/rpc.h>
  40. #include <rpc/pmap_clnt.h>
  41. #include <rpc/pmap_prot.h>
  42. #include "nfsmount.h"
  43. #include <arpa/inet.h>
  44. #endif
  45. #ifdef notyet
  46. /* Nonzero for force umount (-f). This needs kernel support we don't have. */
  47. int force = 0;
  48. #endif
  49. /* When umount fails, attempt a read-only remount (-r). */
  50. int remount = 0;
  51. /* Don't write a entry in /etc/mtab (-n). */
  52. int umount_nomtab = 0;
  53. /* Nonzero for chatty (-v). This is a nonstandard flag (not in BSD). */
  54. int umount_verbose = 0;
  55. /* True if ruid != euid. */
  56. int umount_suid = 0;
  57. #if HAVE_NFS
  58. static int xdr_dir(XDR *xdrsp, char *dirp)
  59. {
  60. return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
  61. }
  62. static int
  63. nfs_umount_rpc_call(const char *spec, const char *opts)
  64. {
  65. register CLIENT *clp;
  66. struct sockaddr_in saddr;
  67. struct timeval pertry, try;
  68. enum clnt_stat clnt_stat;
  69. int so = RPC_ANYSOCK;
  70. struct hostent *hostp;
  71. char *hostname;
  72. char *dirname;
  73. char *p;
  74. if (spec == NULL || (p = strchr(spec,':')) == NULL)
  75. return 0;
  76. hostname = xstrndup(spec, p-spec);
  77. dirname = xstrdup(p+1);
  78. #ifdef DEBUG
  79. printf("host: %s, directory: %s\n", hostname, dirname);
  80. #endif
  81. if (opts && (p = strstr(opts, "addr="))) {
  82. char *q;
  83. free(hostname);
  84. p += 5;
  85. q = p;
  86. while (*q && *q != ',') q++;
  87. hostname = xstrndup(p,q-p);
  88. }
  89. if (hostname[0] >= '0' && hostname[0] <= '9')
  90. saddr.sin_addr.s_addr = inet_addr(hostname);
  91. else {
  92. if ((hostp = gethostbyname(hostname)) == NULL) {
  93. fprintf(stderr, "umount: can't get address for %s\n",
  94. hostname);
  95. return 1;
  96. }
  97. if (hostp->h_length > sizeof(struct in_addr)) {
  98. fprintf(stderr, "umount: got bad hostp->h_length\n");
  99. hostp->h_length = sizeof(struct in_addr);
  100. }
  101. memcpy(&saddr.sin_addr, hostp->h_addr, hostp->h_length);
  102. }
  103. saddr.sin_family = AF_INET;
  104. saddr.sin_port = 0;
  105. pertry.tv_sec = 3;
  106. pertry.tv_usec = 0;
  107. if ((clp = clntudp_create(&saddr, MOUNTPROG, MOUNTVERS,
  108. pertry, &so)) == NULL) {
  109. clnt_pcreateerror("Cannot MOUNTPROG RPC");
  110. return (1);
  111. }
  112. clp->cl_auth = authunix_create_default();
  113. try.tv_sec = 20;
  114. try.tv_usec = 0;
  115. clnt_stat = clnt_call(clp, MOUNTPROC_UMNT,
  116. (xdrproc_t) xdr_dir, dirname,
  117. (xdrproc_t) xdr_void, (caddr_t) 0,
  118. try);
  119. if (clnt_stat != RPC_SUCCESS) {
  120. clnt_perror(clp, "Bad UMNT RPC");
  121. return (1);
  122. }
  123. auth_destroy(clp->cl_auth);
  124. clnt_destroy(clp);
  125. return (0);
  126. }
  127. #endif /* HAVE_NFS */
  128. /* complain about a failed umount */
  129. static void complain(int err, const char *dev) {
  130. switch (err) {
  131. case ENXIO:
  132. error ("umount: %s: invalid block device", dev); break;
  133. case EINVAL:
  134. error ("umount: %s: not mounted", dev); break;
  135. case EIO:
  136. error ("umount: %s: can't write superblock", dev); break;
  137. case EBUSY:
  138. /* Let us hope fstab has a line "proc /proc ..."
  139. and not "none /proc ..."*/
  140. error ("umount: %s: device is busy", dev); break;
  141. case ENOENT:
  142. error ("umount: %s: not found", dev); break;
  143. case EPERM:
  144. error ("umount: %s: must be superuser to umount", dev); break;
  145. case EACCES:
  146. error ("umount: %s: block devices not permitted on fs", dev); break;
  147. default:
  148. error ("umount: %s: %s", dev, strerror (err)); break;
  149. }
  150. }
  151. /* Umount a single device. Return a status code, so don't exit
  152. on a non-fatal error. We lock/unlock around each umount. */
  153. static int
  154. umount_one (const char *spec, const char *node, const char *type,
  155. const char *opts)
  156. {
  157. int umnt_err, umnt_err2;
  158. int isroot;
  159. int res;
  160. /* Special case for root. As of 0.99pl10 we can (almost) unmount root;
  161. the kernel will remount it readonly so that we can carry on running
  162. afterwards. The readonly remount is illegal if any files are opened
  163. for writing at the time, so we can't update mtab for an unmount of
  164. root. As it is only really a remount, this doesn't matter too
  165. much. [sct May 29, 1993] */
  166. isroot = (streq (node, "/") || streq (node, "root")
  167. || streq (node, "rootfs"));
  168. if (isroot)
  169. umount_nomtab++;
  170. #if HAVE_NFS
  171. /* Ignore any RPC errors, so that you can umount the filesystem
  172. if the server is down. */
  173. if (strcasecmp(type, "nfs") == 0)
  174. nfs_umount_rpc_call(spec, opts);
  175. #endif
  176. umnt_err = umnt_err2 = 0;
  177. res = umount (node);
  178. if (res < 0) {
  179. umnt_err = errno;
  180. /* A device might have been mounted on a node that has since
  181. been deleted or renamed, so if node fails, also try spec. */
  182. /* if (umnt_err == ENOENT || umnt_err == EINVAL) */
  183. if (umnt_err != EBUSY && strcmp(node, spec)) {
  184. if (umount_verbose)
  185. printf ("could not umount %s - trying %s instead\n",
  186. node, spec);
  187. res = umount (spec);
  188. if (res < 0)
  189. umnt_err2 = errno;
  190. /* Do not complain about remote NFS mount points */
  191. if (errno == ENOENT && strchr(spec, ':'))
  192. umnt_err2 = 0;
  193. }
  194. }
  195. if (res < 0 && remount && (umnt_err == EBUSY || umnt_err2 == EBUSY)) {
  196. /* Umount failed - let us try a remount */
  197. res=mount(spec, node, NULL, MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
  198. if (res == 0) {
  199. struct mntent remnt;
  200. fprintf(stderr, "umount: %s busy - remounted read-only\n", spec);
  201. remnt.mnt_type = remnt.mnt_fsname = NULL;
  202. remnt.mnt_dir = xstrdup(node);
  203. remnt.mnt_opts = "ro";
  204. update_mtab(node, &remnt);
  205. return 0;
  206. } else if (errno != EBUSY) { /* hmm ... */
  207. perror("remount");
  208. fprintf(stderr, "umount: could not remount %s read-only\n",
  209. spec);
  210. }
  211. }
  212. if (res >= 0) {
  213. /* Umount succeeded, update mtab. */
  214. if (umount_verbose)
  215. printf ("%s umounted\n", spec);
  216. if (!umount_nomtab && mtab_is_writable()) {
  217. struct mntentchn *mc;
  218. /* Special stuff for loop devices */
  219. if ((mc = getmntfile (spec)) || (mc = getmntfile (node))) {
  220. char *opts;
  221. /* old style mtab line? */
  222. if (streq(mc->mnt_type, "loop"))
  223. if (del_loop(spec))
  224. goto fail;
  225. /* new style mtab line? */
  226. opts = mc->mnt_opts ? xstrdup(mc->mnt_opts) : "";
  227. for (opts = strtok (opts, ","); opts; opts = strtok (NULL, ",")) {
  228. if (!strncmp(opts, "loop=", 5)) {
  229. if (del_loop(opts+5))
  230. goto fail;
  231. break;
  232. }
  233. }
  234. } else {
  235. /* maybe spec is a loop device? */
  236. /* no del_loop() - just delete it from mtab */
  237. if ((mc = getmntoptfile (spec)) != NULL)
  238. node = mc->mnt_dir;
  239. }
  240. /* Non-loop stuff */
  241. update_mtab (node, NULL);
  242. }
  243. return 0;
  244. }
  245. fail:
  246. /* Umount or del_loop failed, complain, but don't die. */
  247. if (!umount_nomtab) {
  248. /* remove obsolete entry */
  249. if (umnt_err == EINVAL || umnt_err == ENOENT)
  250. update_mtab (node, NULL);
  251. }
  252. if (umnt_err2)
  253. complain(umnt_err2, spec);
  254. if (umnt_err && umnt_err != umnt_err2)
  255. complain(umnt_err, node);
  256. return 1;
  257. }
  258. /* Unmount all filesystems of type VFSTYPES found in mtab. Since we are
  259. concurrently updating mtab after every succesful umount, we have to
  260. slurp in the entire file before we start. This isn't too bad, because
  261. in any case it's important to umount mtab entries in reverse order
  262. to mount, e.g. /usr/spool before /usr. */
  263. int
  264. umount_all (string_list types) {
  265. struct mntentchn *mc, *hd;
  266. int errors = 0;
  267. hd = mtab_head();
  268. if (!hd->prev)
  269. die (2, "umount: cannot find list of filesystems to unmount");
  270. for (mc = hd->prev; mc != hd; mc = mc->prev) {
  271. if (matching_type (mc->mnt_type, types)) {
  272. errors |= umount_one (mc->mnt_fsname, mc->mnt_dir,
  273. mc->mnt_type, mc->mnt_opts);
  274. }
  275. }
  276. sync ();
  277. return errors;
  278. }
  279. extern char version[];
  280. static struct option longopts[] =
  281. {
  282. { "all", 0, 0, 'a' },
  283. { "force", 0, 0, 'f' },
  284. { "help", 0, 0, 'h' },
  285. { "no-mtab", 0, 0, 'n' },
  286. { "umount_verbose", 0, 0, 'v' },
  287. { "version", 0, 0, 'V' },
  288. { "read-only", 0, 0, 'r' },
  289. { "types", 1, 0, 't' },
  290. { NULL, 0, 0, 0 }
  291. };
  292. char *umount_usage_string = "\
  293. usage: umount [-hV]\n\
  294. umount -a [-r] [-n] [-v] [-t vfstypes]\n\
  295. umount [-r] [-n] [-v] special | node...\n\
  296. ";
  297. static void
  298. usage (FILE *fp, int n)
  299. {
  300. fprintf (fp, "%s", umount_usage_string);
  301. exit (n);
  302. }
  303. int umount_mount_quiet = 0;
  304. int
  305. this_was_main_int_mount_c (int argc, char *argv[])
  306. {
  307. int c;
  308. int all = 0;
  309. string_list types = NULL;
  310. string_list options;
  311. struct mntentchn *mc, *fs;
  312. char *file;
  313. int result = 0;
  314. while ((c = getopt_long (argc, argv, "afhnrvVt:", longopts, NULL)) != EOF)
  315. switch (c) {
  316. case 'a': /* umount everything */
  317. ++all;
  318. break;
  319. case 'f': /* force umount (needs kernel support) */
  320. #if 0
  321. ++force;
  322. #else
  323. die (2, "umount: forced umount not supported yet");
  324. #endif
  325. break;
  326. case 'h': /* help */
  327. usage (stdout, 0);
  328. break;
  329. case 'n':
  330. ++umount_nomtab;
  331. break;
  332. case 'r': /* remount read-only if umount fails */
  333. ++remount;
  334. break;
  335. case 'v': /* make noise */
  336. ++umount_verbose;
  337. break;
  338. case 'V': /* version */
  339. printf ("umount: %s\n", version);
  340. exit (0);
  341. case 't': /* specify file system type */
  342. types = parse_list (optarg);
  343. break;
  344. case 0:
  345. break;
  346. case '?':
  347. default:
  348. usage (stderr, 1);
  349. }
  350. if (getuid () != geteuid ())
  351. {
  352. umount_suid = 1;
  353. if (all || types || umount_nomtab)
  354. die (2, "umount: only root can do that");
  355. }
  356. argc -= optind;
  357. argv += optind;
  358. if (all) {
  359. if (types == NULL)
  360. types = parse_list(xstrdup("noproc"));
  361. result = umount_all (types);
  362. } else if (argc < 1) {
  363. usage (stderr, 2);
  364. } else while (argc--) {
  365. file = canonicalize (*argv); /* mtab paths are canonicalized */
  366. if (umount_verbose > 1)
  367. printf("Trying to umount %s\n", file);
  368. mc = getmntfile (file);
  369. if (!mc && umount_verbose)
  370. printf("Could not find %s in mtab\n", file);
  371. if (umount_suid) {
  372. if (!mc)
  373. die (2, "umount: %s is not mounted (according to mtab)", file);
  374. if (!(fs = getfsspec (file)) && !(fs = getfsfile (file)))
  375. die (2, "umount: %s is not in the fstab (and you are not root)",
  376. file);
  377. if ((!streq (mc->mnt_fsname, fs->mnt_fsname) &&
  378. !streq (mc->mnt_fsname, canonicalize (fs->mnt_fsname)))
  379. || (!streq (mc->mnt_dir, fs->mnt_dir) &&
  380. !streq (mc->mnt_dir, canonicalize (fs->mnt_dir)))) {
  381. die (2, "umount: %s mount disagrees with the fstab", file);
  382. }
  383. options = parse_list (fs->mnt_opts);
  384. while (options) {
  385. if (streq (car (options), "user"))
  386. break;
  387. options = cdr (options);
  388. }
  389. if (!options)
  390. die (2, "umount: only root can unmount %s from %s",
  391. fs->mnt_fsname, fs->mnt_dir);
  392. }
  393. if (mc)
  394. result = umount_one (xstrdup(mc->mnt_fsname), xstrdup(mc->mnt_dir),
  395. xstrdup(mc->mnt_type), xstrdup(mc->mnt_opts));
  396. else
  397. result = umount_one (*argv, *argv, *argv, *argv);
  398. argv++;
  399. }
  400. exit (result);
  401. }