xref: /original-bsd/sbin/umount/umount.c (revision deff14a8)
1 /*-
2  * Copyright (c) 1980, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1980, 1989, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)umount.c	8.5 (Berkeley) 10/09/94";
16 #endif /* not lint */
17 
18 #include <sys/param.h>
19 #include <sys/stat.h>
20 #include <sys/mount.h>
21 #include <sys/time.h>
22 #include <sys/socket.h>
23 #include <sys/socketvar.h>
24 
25 #include <netdb.h>
26 #include <rpc/rpc.h>
27 #include <rpc/pmap_clnt.h>
28 #include <rpc/pmap_prot.h>
29 #include <nfs/rpcv2.h>
30 
31 #include <err.h>
32 #include <fstab.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 
38 typedef enum { MNTON, MNTFROM } mntwhat;
39 
40 int	fake, fflag, vflag, *typelist;
41 char	*nfshost;
42 
43 int	 fsnametotype __P((char *));
44 char	*getmntname __P((char *, mntwhat, int *));
45 void	 maketypelist __P((char *));
46 int	 selected __P((int));
47 int	 namematch __P((struct hostent *));
48 int	 umountall __P((void));
49 int	 umountfs __P((char *));
50 void	 usage __P((void));
51 int	 xdr_dir __P((XDR *, char *));
52 
53 int
54 main(argc, argv)
55 	int argc;
56 	char *argv[];
57 {
58 	int all, ch, errs;
59 
60 	/* Start disks transferring immediately. */
61 	sync();
62 
63 	all = 0;
64 	while ((ch = getopt(argc, argv, "aFfh:t:v")) != EOF)
65 		switch (ch) {
66 		case 'a':
67 			all = 1;
68 			break;
69 		case 'F':
70 			fake = 1;
71 			break;
72 		case 'f':
73 			fflag = MNT_FORCE;
74 			break;
75 		case 'h':	/* -h implies -a. */
76 			all = 1;
77 			nfshost = optarg;
78 			break;
79 		case 't':
80 			maketypelist(optarg);
81 			break;
82 		case 'v':
83 			vflag = 1;
84 			break;
85 		default:
86 			usage();
87 			/* NOTREACHED */
88 		}
89 	argc -= optind;
90 	argv += optind;
91 
92 	if (argc == 0 && !all || argc != 0 && all)
93 		usage();
94 
95 	/* -h implies "-t nfs" if no -t flag. */
96 	if ((nfshost != NULL) && (typelist == NULL))
97 		maketypelist("nfs");
98 
99 	if (all) {
100 		if (setfsent() == 0)
101 			err(1, "%s", _PATH_FSTAB);
102 		errs = umountall();
103 	} else
104 		for (errs = 0; *argv != NULL; ++argv)
105 			if (umountfs(*argv) != 0)
106 				errs = 1;
107 	exit(errs);
108 }
109 
110 int
111 umountall()
112 {
113 	struct fstab *fs;
114 	int rval, type;
115 	char *cp;
116 
117 	while ((fs = getfsent()) != NULL) {
118 		/* Ignore the root. */
119 		if (strcmp(fs->fs_file, "/") == 0)
120 			continue;
121 		/*
122 		 * !!!
123 		 * Historic practice: ignore unknown FSTAB_* fields.
124 		 */
125 		if (strcmp(fs->fs_type, FSTAB_RW) &&
126 		    strcmp(fs->fs_type, FSTAB_RO) &&
127 		    strcmp(fs->fs_type, FSTAB_RQ))
128 			continue;
129 		/* If an unknown file system type, complain. */
130 		if ((type = fsnametotype(fs->fs_vfstype)) == MOUNT_NONE) {
131 			warnx("%s: unknown mount type", fs->fs_vfstype);
132 			continue;
133 		}
134 		if (!selected(type))
135 			continue;
136 
137 		/*
138 		 * We want to unmount the file systems in the reverse order
139 		 * that they were mounted.  So, we save off the file name
140 		 * in some allocated memory, and then call recursively.
141 		 */
142 		if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL)
143 			err(1, NULL);
144 		(void)strcpy(cp, fs->fs_file);
145 		rval = umountall();
146 		return (umountfs(cp) || rval);
147 	}
148 	return (0);
149 }
150 
151 int
152 umountfs(name)
153 	char *name;
154 {
155 	enum clnt_stat clnt_stat;
156 	struct hostent *hp;
157 	struct sockaddr_in saddr;
158 	struct stat sb;
159 	struct timeval pertry, try;
160 	CLIENT *clp;
161 	int so, type;
162 	char *delimp, *hostp, *mntpt, rname[MAXPATHLEN];
163 
164 	if (realpath(name, rname) == NULL) {
165 		warn("%s", rname);
166 		return (1);
167 	}
168 
169 	name = rname;
170 
171 	if (stat(name, &sb) < 0) {
172 		if (((mntpt = getmntname(name, MNTFROM, &type)) == NULL) &&
173 		    ((mntpt = getmntname(name, MNTON, &type)) == NULL)) {
174 			warnx("%s: not currently mounted", name);
175 			return (1);
176 		}
177 	} else if (S_ISBLK(sb.st_mode)) {
178 		if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
179 			warnx("%s: not currently mounted", name);
180 			return (1);
181 		}
182 	} else if (S_ISDIR(sb.st_mode)) {
183 		mntpt = name;
184 		if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
185 			warnx("%s: not currently mounted", mntpt);
186 			return (1);
187 		}
188 	} else {
189 		warnx("%s: not a directory or special device", name);
190 		return (1);
191 	}
192 
193 	if (!selected(type))
194 		return (1);
195 
196 	if ((delimp = strchr(name, '@')) != NULL) {
197 		hostp = delimp + 1;
198 		*delimp = '\0';
199 		hp = gethostbyname(hostp);
200 		*delimp = '@';
201 	} else if ((delimp = strchr(name, ':')) != NULL) {
202 		*delimp = '\0';
203 		hostp = name;
204 		hp = gethostbyname(hostp);
205 		name = delimp + 1;
206 		*delimp = ':';
207 	} else
208 		hp = NULL;
209 	if (!namematch(hp))
210 		return (1);
211 
212 	if (vflag)
213 		(void)printf("%s: unmount from %s\n", name, mntpt);
214 	if (fake)
215 		return (0);
216 
217 	if (unmount(mntpt, fflag) < 0) {
218 		warn("%s", mntpt);
219 		return (1);
220 	}
221 
222 	if ((hp != NULL) && !(fflag & MNT_FORCE)) {
223 		*delimp = '\0';
224 		memset(&saddr, 0, sizeof(saddr));
225 		saddr.sin_family = AF_INET;
226 		saddr.sin_port = 0;
227 		memmove(&saddr.sin_addr, hp->h_addr, hp->h_length);
228 		pertry.tv_sec = 3;
229 		pertry.tv_usec = 0;
230 		so = RPC_ANYSOCK;
231 		if ((clp = clntudp_create(&saddr,
232 		    RPCPROG_MNT, RPCMNT_VER1, pertry, &so)) == NULL) {
233 			clnt_pcreateerror("Cannot MNT PRC");
234 			return (1);
235 		}
236 		clp->cl_auth = authunix_create_default();
237 		try.tv_sec = 20;
238 		try.tv_usec = 0;
239 		clnt_stat = clnt_call(clp,
240 		    RPCMNT_UMOUNT, xdr_dir, name, xdr_void, (caddr_t)0, try);
241 		if (clnt_stat != RPC_SUCCESS) {
242 			clnt_perror(clp, "Bad MNT RPC");
243 			return (1);
244 		}
245 		auth_destroy(clp->cl_auth);
246 		clnt_destroy(clp);
247 	}
248 	return (0);
249 }
250 
251 char *
252 getmntname(name, what, type)
253 	char *name;
254 	mntwhat what;
255 	int *type;
256 {
257 	struct statfs *mntbuf;
258 	int i, mntsize;
259 
260 	if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
261 		warn("getmntinfo");
262 		return (NULL);
263 	}
264 	for (i = 0; i < mntsize; i++) {
265 		if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
266 			if (type)
267 				*type = mntbuf[i].f_type;
268 			return (mntbuf[i].f_mntonname);
269 		}
270 		if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
271 			if (type)
272 				*type = mntbuf[i].f_type;
273 			return (mntbuf[i].f_mntfromname);
274 		}
275 	}
276 	return (NULL);
277 }
278 
279 static enum { IN_LIST, NOT_IN_LIST } which;
280 
281 int
282 selected(type)
283 	int type;
284 {
285 	int *av;
286 
287 	/* If no type specified, it's always selected. */
288 	if (typelist == NULL)
289 		return (1);
290 	for (av = typelist; *av != NULL; ++av)
291 		if (type == *typelist)
292 			return (which == IN_LIST ? 1 : 0);
293 	return (which == IN_LIST ? 0 : 1);
294 }
295 
296 void
297 maketypelist(fslist)
298 	char *fslist;
299 {
300 	int *av, i;
301 	char *nextcp;
302 
303 	if ((fslist == NULL) || (fslist[0] == '\0'))
304 		errx(1, "empty type list");
305 
306 	/*
307 	 * XXX
308 	 * Note: the syntax is "noxxx,yyy" for no xxx's and
309 	 * no yyy's, not the more intuitive "noyyy,noyyy".
310 	 */
311 	if (fslist[0] == 'n' && fslist[1] == 'o') {
312 		fslist += 2;
313 		which = NOT_IN_LIST;
314 	} else
315 		which = IN_LIST;
316 
317 	/* Count the number of types. */
318 	for (i = 0, nextcp = fslist; *nextcp != NULL; ++nextcp)
319 		if (*nextcp == ',')
320 			i++;
321 
322 	/* Build an array of that many types. */
323 	if ((av = typelist = malloc((i + 2) * sizeof(int))) == NULL)
324 		err(1, NULL);
325 	for (i = 0; fslist != NULL; fslist = nextcp, ++i) {
326 		if ((nextcp = strchr(fslist, ',')) != NULL)
327 			*nextcp++ = '\0';
328 		av[i] = fsnametotype(fslist);
329 		if (av[i] == MOUNT_NONE)
330 			errx(1, "%s: unknown mount type", fslist);
331 	}
332 	/* Terminate the array. */
333 	av[i++] = MOUNT_NONE;
334 }
335 
336 int
337 fsnametotype(name)
338 	char *name;
339 {
340 	static char const *namelist[] = INITMOUNTNAMES;
341 	char const **cp;
342 
343 	for (cp = namelist; *cp; ++cp)
344 		if (strcmp(name, *cp) == 0)
345 			return (cp - namelist);
346 	return (MOUNT_NONE);
347 }
348 
349 int
350 namematch(hp)
351 	struct hostent *hp;
352 {
353 	char *cp, **np;
354 
355 	if ((hp == NULL) || (nfshost == NULL))
356 		return (1);
357 
358 	if (strcasecmp(nfshost, hp->h_name) == 0)
359 		return (1);
360 
361 	if ((cp = strchr(hp->h_name, '.')) != NULL) {
362 		*cp = '\0';
363 		if (strcasecmp(nfshost, hp->h_name) == 0)
364 			return (1);
365 	}
366 	for (np = hp->h_aliases; *np; np++) {
367 		if (strcasecmp(nfshost, *np) == 0)
368 			return (1);
369 		if ((cp = strchr(*np, '.')) != NULL) {
370 			*cp = '\0';
371 			if (strcasecmp(nfshost, *np) == 0)
372 				return (1);
373 		}
374 	}
375 	return (0);
376 }
377 
378 /*
379  * xdr routines for mount rpc's
380  */
381 int
382 xdr_dir(xdrsp, dirp)
383 	XDR *xdrsp;
384 	char *dirp;
385 {
386 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
387 }
388 
389 void
390 usage()
391 {
392 	(void)fprintf(stderr,
393 	    "usage: %s\n       %s\n",
394 	    "umount [-fv] [-t fstypelist] special | node",
395 	    "umount -a[fv] [-h host] [-t fstypelist]");
396 	exit(1);
397 }
398