umount.c revision 1.50 1 /* $NetBSD: umount.c,v 1.50 2016/06/26 03:40:39 dholland 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.50 2016/06/26 03:40:39 dholland 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 #include <nfs/nfsmount.h>
59 #endif /* !SMALL */
60
61 #include <err.h>
62 #include <errno.h>
63 #include <fstab.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68
69 typedef enum { MNTANY, MNTON, MNTFROM } mntwhat;
70
71 #ifndef SMALL
72 #include "mountprog.h"
73
74 static int fake, verbose;
75 static char *nfshost;
76 static struct addrinfo *nfshost_ai = NULL;
77
78 static int namematch(const struct addrinfo *);
79 static int sacmp(const struct sockaddr *, const struct sockaddr *);
80 static int xdr_dir(XDR *, char *);
81 static const char *getmntproto(const char *);
82 #endif /* !SMALL */
83
84 static int fflag;
85 static char *getmntname(const char *, mntwhat, char **);
86 static int umountfs(const char *, const char **, int);
87 static void usage(void) __dead;
88
89 int
90 main(int argc, char *argv[])
91 {
92 int ch, errs, all = 0, raw = 0;
93 #ifndef SMALL
94 int mnts;
95 struct statvfs *mntbuf;
96 struct addrinfo hints;
97 #endif /* SMALL */
98 const char **typelist = NULL;
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 const char *proto = NULL;
185 #endif /* !SMALL */
186 const char *mntpt;
187 char *type, rname[MAXPATHLEN], umountprog[MAXPATHLEN];
188 mntwhat what;
189 struct stat sb;
190
191 if (raw) {
192 mntpt = name;
193 } else {
194
195 what = MNTANY;
196 if (realpath(name, rname) != NULL) {
197 name = rname;
198
199 if (stat(name, &sb) == 0) {
200 if (S_ISBLK(sb.st_mode))
201 what = MNTON;
202 else if (S_ISDIR(sb.st_mode))
203 what = MNTFROM;
204 }
205 }
206 #ifdef SMALL
207 else {
208 warn("%s", name);
209 return 1;
210 }
211 #endif /* SMALL */
212 mntpt = name;
213
214 switch (what) {
215 case MNTON:
216 if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
217 warnx("%s: not currently mounted", name);
218 return (1);
219 }
220 break;
221 case MNTFROM:
222 if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
223 warnx("%s: not currently mounted", mntpt);
224 return (1);
225 }
226 break;
227 default:
228 if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
229 name = mntpt;
230 if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
231 warnx("%s: not currently mounted", name);
232 return 1;
233 }
234 }
235 }
236
237 #ifndef SMALL
238 if (checkvfsname(type, typelist))
239 return 1;
240
241 (void)memset(&hints, 0, sizeof hints);
242 if (!strncmp(type, MOUNT_NFS,
243 sizeof(((struct statvfs *)NULL)->f_fstypename))) {
244 char *delimp;
245 proto = getmntproto(mntpt);
246 /* look for host:mountpoint */
247 if ((delimp = strrchr(name, ':')) != NULL) {
248 int len = delimp - name;
249 hostp = malloc(len + 1);
250 if (hostp == NULL)
251 return 1;
252 memcpy(hostp, name, len);
253 hostp[len] = 0;
254 name += len + 1;
255 getaddrinfo(hostp, NULL, &hints, &ai);
256 }
257 }
258
259 if (!namematch(ai))
260 return 1;
261 #endif /* ! SMALL */
262 }
263
264 snprintf(umountprog, sizeof(umountprog), "umount_%s", type);
265
266 #ifndef SMALL
267 if (verbose) {
268 (void)printf("%s: unmount from %s\n", name, mntpt);
269 /* put this before the test of FAKE */
270 if (!raw) {
271 (void)printf("Trying unmount program %s\n",
272 umountprog);
273 }
274 }
275 if (fake)
276 return 0;
277 #endif /* ! SMALL */
278
279 if (!raw) {
280 /*
281 * The only options that need to be passed on are -f
282 * and -v.
283 */
284 char *args[3];
285 unsigned nargs = 0;
286
287 args[nargs++] = umountprog;
288 if (fflag == MNT_FORCE) {
289 args[nargs++] = __UNCONST("-f");
290 }
291 #ifndef SMALL
292 if (verbose) {
293 args[nargs++] = __UNCONST("-v");
294 }
295 #endif
296 execvp(umountprog, args);
297 if (errno != ENOENT) {
298 warn("%s: execvp", umountprog);
299 }
300 }
301
302 #ifndef SMALL
303 if (verbose)
304 (void)printf("(No separate unmount program.)\n");
305 #endif
306
307 if (unmount(mntpt, fflag) == -1) {
308 warn("%s", mntpt);
309 return 1;
310 }
311
312 #ifndef SMALL
313 if (ai != NULL && !(fflag & MNT_FORCE)) {
314 clp = clnt_create(hostp, RPCPROG_MNT, RPCMNT_VER1, proto);
315 if (clp == NULL) {
316 clnt_pcreateerror("Cannot MNT PRC");
317 return 1;
318 }
319 clp->cl_auth = authsys_create_default();
320 try.tv_sec = 20;
321 try.tv_usec = 0;
322 clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir,
323 __UNCONST(name), xdr_void, NULL, try);
324 if (clnt_stat != RPC_SUCCESS) {
325 clnt_perror(clp, "Bad MNT RPC");
326 return 1;
327 }
328 auth_destroy(clp->cl_auth);
329 clnt_destroy(clp);
330 }
331 #endif /* ! SMALL */
332 return 0;
333 }
334
335 static char *
336 getmntname(const char *name, mntwhat what, char **type)
337 {
338 static struct statvfs *mntbuf;
339 static int mntsize;
340 int i;
341
342 if (mntbuf == NULL &&
343 (mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
344 warn("getmntinfo");
345 return (NULL);
346 }
347 for (i = mntsize - 1; i >= 0; i--) {
348 if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
349 if (type)
350 *type = mntbuf[i].f_fstypename;
351 return (mntbuf[i].f_mntonname);
352 }
353 if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
354 if (type)
355 *type = mntbuf[i].f_fstypename;
356 return (mntbuf[i].f_mntfromname);
357 }
358 }
359 return (NULL);
360 }
361
362 #ifndef SMALL
363 static int
364 sacmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
365 {
366 const void *p1, *p2;
367 size_t len;
368
369 if (sa1->sa_family != sa2->sa_family)
370 return 1;
371
372 switch (sa1->sa_family) {
373 case AF_INET:
374 p1 = &((const struct sockaddr_in *)sa1)->sin_addr;
375 p2 = &((const struct sockaddr_in *)sa2)->sin_addr;
376 len = 4;
377 break;
378 case AF_INET6:
379 p1 = &((const struct sockaddr_in6 *)sa1)->sin6_addr;
380 p2 = &((const struct sockaddr_in6 *)sa2)->sin6_addr;
381 len = 16;
382 if (((const struct sockaddr_in6 *)sa1)->sin6_scope_id !=
383 ((const struct sockaddr_in6 *)sa2)->sin6_scope_id)
384 return 1;
385 break;
386 default:
387 return 1;
388 }
389
390 return memcmp(p1, p2, len);
391 }
392
393 static int
394 namematch(const struct addrinfo *ai)
395 {
396 struct addrinfo *aip;
397
398 if (nfshost == NULL || nfshost_ai == NULL)
399 return (1);
400
401 while (ai != NULL) {
402 aip = nfshost_ai;
403 while (aip != NULL) {
404 if (sacmp(ai->ai_addr, aip->ai_addr) == 0)
405 return 1;
406 aip = aip->ai_next;
407 }
408 ai = ai->ai_next;
409 }
410
411 return 0;
412 }
413
414 /*
415 * xdr routines for mount rpc's
416 */
417 static int
418 xdr_dir(XDR *xdrsp, char *dirp)
419 {
420 return xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN);
421 }
422
423 static const char *
424 getmntproto(const char *name)
425 {
426 struct nfs_args nfsargs;
427 struct sockaddr_storage ss;
428
429 nfsargs.sotype = SOCK_DGRAM;
430 nfsargs.addr = (struct sockaddr *)&ss;
431 nfsargs.addrlen = sizeof(ss);
432 (void)mount("nfs", name, MNT_GETARGS, &nfsargs, sizeof(nfsargs));
433 return nfsargs.sotype == SOCK_STREAM ? "tcp" : "udp";
434 }
435 #endif /* !SMALL */
436
437 static void
438 usage(void)
439 {
440 #ifdef SMALL
441 (void)fprintf(stderr,
442 "Usage: %s [-fR] special | node\n", getprogname());
443 #else
444 (void)fprintf(stderr,
445 "Usage: %s [-fvFR] [-t fstypelist] special | node\n"
446 "\t %s -a[fvF] [-h host] [-t fstypelist]\n", getprogname(),
447 getprogname());
448 #endif /* SMALL */
449 exit(1);
450 }
451