xref: /netbsd-src/sbin/umount/umount.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*	$NetBSD: umount.c,v 1.43 2008/08/05 20:57:45 pooka Exp $	*/
2 
3 /*-
4  * Copyright (c) 1980, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1989, 1993\
35  The Regents of the University of California.  All rights reserved.");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)umount.c	8.8 (Berkeley) 5/8/95";
41 #else
42 __RCSID("$NetBSD: umount.c,v 1.43 2008/08/05 20:57:45 pooka Exp $");
43 #endif
44 #endif /* not lint */
45 
46 #include <sys/param.h>
47 #include <sys/stat.h>
48 #include <sys/mount.h>
49 #include <sys/time.h>
50 #ifndef SMALL
51 #include <sys/socket.h>
52 
53 #include <netdb.h>
54 #include <rpc/rpc.h>
55 #include <rpc/pmap_clnt.h>
56 #include <rpc/pmap_prot.h>
57 #include <nfs/rpcv2.h>
58 #endif /* !SMALL */
59 
60 #include <err.h>
61 #include <fstab.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66 
67 typedef enum { MNTANY, MNTON, MNTFROM } mntwhat;
68 
69 #ifndef SMALL
70 #include "mountprog.h"
71 
72 static int	 fake, verbose;
73 static char	*nfshost;
74 static struct addrinfo *nfshost_ai = NULL;
75 
76 static int	 namematch(const struct addrinfo *);
77 static int	 sacmp(const struct sockaddr *, const struct sockaddr *);
78 static int	 xdr_dir(XDR *, char *);
79 #endif /* !SMALL */
80 
81 static int	 fflag;
82 static char	*getmntname(const char *, mntwhat, char **);
83 static int	 umountfs(const char *, const char **, int);
84 static void	 usage(void) __dead;
85 
86 int
87 main(int argc, char *argv[])
88 {
89 	int ch, errs, all = 0, raw = 0;
90 #ifndef SMALL
91 	int mnts;
92 	struct statvfs *mntbuf;
93 	struct addrinfo hints;
94 #endif /* SMALL */
95 	const char **typelist = NULL;
96 
97 	/* Start disks transferring immediately. */
98 	sync();
99 
100 #ifdef SMALL
101 #define OPTS "fR"
102 #else
103 #define OPTS "AaFfh:Rt:v"
104 #endif
105 	while ((ch = getopt(argc, argv, OPTS)) != -1)
106 		switch (ch) {
107 		case 'f':
108 			fflag = MNT_FORCE;
109 			break;
110 		case 'R':
111 			raw = 1;
112 			break;
113 #ifndef SMALL
114 		case 'A':
115 		case 'a':
116 			all = 1;
117 			break;
118 		case 'F':
119 			fake = 1;
120 			break;
121 		case 'h':	/* -h implies -A. */
122 			all = 1;
123 			nfshost = optarg;
124 			break;
125 		case 't':
126 			if (typelist != NULL)
127 				errx(1, "only one -t option may be specified.");
128 			typelist = makevfslist(optarg);
129 			break;
130 		case 'v':
131 			verbose = 1;
132 			break;
133 #endif /* !SMALL */
134 		default:
135 			usage();
136 			/* NOTREACHED */
137 		}
138 	argc -= optind;
139 	argv += optind;
140 
141 	if ((argc == 0 && !all) || (argc != 0 && all) || (all && raw))
142 		usage();
143 
144 #ifndef SMALL
145 	/* -h implies "-t nfs" if no -t flag. */
146 	if ((nfshost != NULL) && (typelist == NULL))
147 		typelist = makevfslist("nfs");
148 
149 	if (nfshost != NULL) {
150 		memset(&hints, 0, sizeof hints);
151 		getaddrinfo(nfshost, NULL, &hints, &nfshost_ai);
152 	}
153 
154 	errs = 0;
155 	if (all) {
156 		if ((mnts = getmntinfo(&mntbuf, ST_NOWAIT)) == 0) {
157 			warn("getmntinfo");
158 			errs = 1;
159 		}
160 		for (errs = 0, mnts--; mnts > 0; mnts--) {
161 			if (checkvfsname(mntbuf[mnts].f_fstypename, typelist))
162 				continue;
163 			if (umountfs(mntbuf[mnts].f_mntonname, typelist,
164 			             1) != 0)
165 				errs = 1;
166 		}
167 	} else
168 #endif /* !SMALL */
169 		for (errs = 0; *argv != NULL; ++argv)
170 			if (umountfs(*argv, typelist, raw) != 0)
171 				errs = 1;
172 	return errs;
173 }
174 
175 static int
176 umountfs(const char *name, const char **typelist, int raw)
177 {
178 #ifndef SMALL
179 	enum clnt_stat clnt_stat;
180 	struct timeval try;
181 	CLIENT *clp;
182 	char *hostp = NULL;
183 	struct addrinfo *ai = NULL, hints;
184 #endif /* !SMALL */
185 	const char *mntpt;
186 	char *type, rname[MAXPATHLEN];
187 	mntwhat what;
188 	struct stat sb;
189 
190 	if (raw) {
191 		mntpt = name;
192 	} else {
193 
194 		what = MNTANY;
195 		if (realpath(name, rname) != NULL) {
196 			name = rname;
197 
198 			if (stat(name, &sb) == 0) {
199 				if (S_ISBLK(sb.st_mode))
200 					what = MNTON;
201 				else if (S_ISDIR(sb.st_mode))
202 					what = MNTFROM;
203 			}
204 		}
205 #ifdef SMALL
206 		else {
207  			warn("%s", rname);
208  			return 1;
209 		}
210 #endif /* SMALL */
211 		mntpt = name;
212 
213 		switch (what) {
214 		case MNTON:
215 			if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
216 				warnx("%s: not currently mounted", name);
217 				return (1);
218 			}
219 			break;
220 		case MNTFROM:
221 			if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
222 				warnx("%s: not currently mounted", mntpt);
223 				return (1);
224 			}
225 			break;
226 		default:
227 			if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
228 				name = mntpt;
229 				if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
230 					warnx("%s: not currently mounted", name);
231 					return 1;
232 				}
233 			}
234 		}
235 
236 #ifndef SMALL
237 		if (checkvfsname(type, typelist))
238 			return 1;
239 
240 		(void)memset(&hints, 0, sizeof hints);
241 		if (!strncmp(type, MOUNT_NFS,
242 		    sizeof(((struct statvfs *)NULL)->f_fstypename))) {
243 			char *delimp;
244 			/* look for host:mountpoint */
245 			if ((delimp = strrchr(name, ':')) != NULL) {
246 				int len = delimp - name;
247 				hostp = malloc(len + 1);
248 				if (hostp == NULL)
249 				    	return 1;
250 				memcpy(hostp, name, len);
251 				hostp[len] = 0;
252 				name += len + 1;
253 				getaddrinfo(hostp, NULL, &hints, &ai);
254 			}
255 		}
256 
257 		if (!namematch(ai))
258 			return 1;
259 #endif /* ! SMALL */
260 	}
261 
262 #ifndef SMALL
263 	if (verbose)
264 		(void)printf("%s: unmount from %s\n", name, mntpt);
265 	if (fake)
266 		return 0;
267 #endif /* ! SMALL */
268 
269 	if (unmount(mntpt, fflag) == -1) {
270 		warn("%s", mntpt);
271 		return 1;
272 	}
273 
274 #ifndef SMALL
275 	if (ai != NULL && !(fflag & MNT_FORCE)) {
276 		clp = clnt_create(hostp, RPCPROG_MNT, RPCMNT_VER1, "udp");
277 		if (clp  == NULL) {
278 			clnt_pcreateerror("Cannot MNT PRC");
279 			return 1;
280 		}
281 		clp->cl_auth = authsys_create_default();
282 		try.tv_sec = 20;
283 		try.tv_usec = 0;
284 		clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir,
285 		    __UNCONST(name), xdr_void, NULL, try);
286 		if (clnt_stat != RPC_SUCCESS) {
287 			clnt_perror(clp, "Bad MNT RPC");
288 			return 1;
289 		}
290 		auth_destroy(clp->cl_auth);
291 		clnt_destroy(clp);
292 	}
293 #endif /* ! SMALL */
294 	return 0;
295 }
296 
297 static char *
298 getmntname(const char *name, mntwhat what, char **type)
299 {
300 	static struct statvfs *mntbuf;
301 	static int mntsize;
302 	int i;
303 
304 	if (mntbuf == NULL &&
305 	    (mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
306 		warn("getmntinfo");
307 		return (NULL);
308 	}
309 	for (i = mntsize - 1; i >= 0; i--) {
310 		if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
311 			if (type)
312 				*type = mntbuf[i].f_fstypename;
313 			return (mntbuf[i].f_mntonname);
314 		}
315 		if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
316 			if (type)
317 				*type = mntbuf[i].f_fstypename;
318 			return (mntbuf[i].f_mntfromname);
319 		}
320 	}
321 	return (NULL);
322 }
323 
324 #ifndef SMALL
325 static int
326 sacmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
327 {
328 	const void *p1, *p2;
329 	size_t len;
330 
331 	if (sa1->sa_family != sa2->sa_family)
332 		return 1;
333 
334 	switch (sa1->sa_family) {
335 	case AF_INET:
336 		p1 = &((const struct sockaddr_in *)sa1)->sin_addr;
337 		p2 = &((const struct sockaddr_in *)sa2)->sin_addr;
338 		len = 4;
339 		break;
340 	case AF_INET6:
341 		p1 = &((const struct sockaddr_in6 *)sa1)->sin6_addr;
342 		p2 = &((const struct sockaddr_in6 *)sa2)->sin6_addr;
343 		len = 16;
344 		if (((const struct sockaddr_in6 *)sa1)->sin6_scope_id !=
345 		    ((const struct sockaddr_in6 *)sa2)->sin6_scope_id)
346 			return 1;
347 		break;
348 	default:
349 		return 1;
350 	}
351 
352 	return memcmp(p1, p2, len);
353 }
354 
355 static int
356 namematch(const struct addrinfo *ai)
357 {
358 	struct addrinfo *aip;
359 
360 	if (nfshost == NULL || nfshost_ai == NULL)
361 		return (1);
362 
363 	while (ai != NULL) {
364 		aip = nfshost_ai;
365 		while (aip != NULL) {
366 			if (sacmp(ai->ai_addr, aip->ai_addr) == 0)
367 				return 1;
368 			aip = aip->ai_next;
369 		}
370 		ai = ai->ai_next;
371 	}
372 
373 	return 0;
374 }
375 
376 /*
377  * xdr routines for mount rpc's
378  */
379 static int
380 xdr_dir(XDR *xdrsp, char *dirp)
381 {
382 	return xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN);
383 }
384 #endif /* !SMALL */
385 
386 static void
387 usage(void)
388 {
389 #ifdef SMALL
390 	(void)fprintf(stderr,
391 	    "Usage: %s [-fR]  special | node\n", getprogname());
392 #else
393 	(void)fprintf(stderr,
394 	    "Usage: %s [-fvFR] [-t fstypelist] special | node\n"
395 	    "\t %s -a[fvF] [-h host] [-t fstypelist]\n", getprogname(),
396 	    getprogname());
397 #endif /* SMALL */
398 	exit(1);
399 }
400