umount.c revision 1.47 1 /* $NetBSD: umount.c,v 1.47 2013/07/02 01:39:17 christos 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.47 2013/07/02 01:39:17 christos 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 <fstab.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <unistd.h>
67
68 typedef enum { MNTANY, MNTON, MNTFROM } mntwhat;
69
70 #ifndef SMALL
71 #include "mountprog.h"
72
73 static int fake, verbose;
74 static char *nfshost;
75 static struct addrinfo *nfshost_ai = NULL;
76
77 static int namematch(const struct addrinfo *);
78 static int sacmp(const struct sockaddr *, const struct sockaddr *);
79 static int xdr_dir(XDR *, char *);
80 static const char *getmntproto(const char *);
81 #endif /* !SMALL */
82
83 static int fflag;
84 static char *getmntname(const char *, mntwhat, char **);
85 static int umountfs(const char *, const char **, int);
86 static void usage(void) __dead;
87
88 int
89 main(int argc, char *argv[])
90 {
91 int ch, errs, all = 0, raw = 0;
92 #ifndef SMALL
93 int mnts;
94 struct statvfs *mntbuf;
95 struct addrinfo hints;
96 #endif /* SMALL */
97 const char **typelist = NULL;
98
99 /* Start disks transferring immediately. */
100 sync();
101
102 #ifdef SMALL
103 #define OPTS "fR"
104 #else
105 #define OPTS "AaFfh:Rt:v"
106 #endif
107 while ((ch = getopt(argc, argv, OPTS)) != -1)
108 switch (ch) {
109 case 'f':
110 fflag = MNT_FORCE;
111 break;
112 case 'R':
113 raw = 1;
114 break;
115 #ifndef SMALL
116 case 'A':
117 case 'a':
118 all = 1;
119 break;
120 case 'F':
121 fake = 1;
122 break;
123 case 'h': /* -h implies -A. */
124 all = 1;
125 nfshost = optarg;
126 break;
127 case 't':
128 if (typelist != NULL)
129 errx(1, "only one -t option may be specified.");
130 typelist = makevfslist(optarg);
131 break;
132 case 'v':
133 verbose = 1;
134 break;
135 #endif /* !SMALL */
136 default:
137 usage();
138 /* NOTREACHED */
139 }
140 argc -= optind;
141 argv += optind;
142
143 if ((argc == 0 && !all) || (argc != 0 && all) || (all && raw))
144 usage();
145
146 #ifndef SMALL
147 /* -h implies "-t nfs" if no -t flag. */
148 if ((nfshost != NULL) && (typelist == NULL))
149 typelist = makevfslist("nfs");
150
151 if (nfshost != NULL) {
152 memset(&hints, 0, sizeof hints);
153 getaddrinfo(nfshost, NULL, &hints, &nfshost_ai);
154 }
155
156 errs = 0;
157 if (all) {
158 if ((mnts = getmntinfo(&mntbuf, ST_NOWAIT)) == 0) {
159 warn("getmntinfo");
160 errs = 1;
161 }
162 for (errs = 0, mnts--; mnts > 0; mnts--) {
163 if (checkvfsname(mntbuf[mnts].f_fstypename, typelist))
164 continue;
165 if (umountfs(mntbuf[mnts].f_mntonname, typelist,
166 1) != 0)
167 errs = 1;
168 }
169 } else
170 #endif /* !SMALL */
171 for (errs = 0; *argv != NULL; ++argv)
172 if (umountfs(*argv, typelist, raw) != 0)
173 errs = 1;
174 return errs;
175 }
176
177 static int
178 umountfs(const char *name, const char **typelist, int raw)
179 {
180 #ifndef SMALL
181 enum clnt_stat clnt_stat;
182 struct timeval try;
183 CLIENT *clp;
184 char *hostp = NULL;
185 struct addrinfo *ai = NULL, hints;
186 const char *proto = NULL;
187 #endif /* !SMALL */
188 const char *mntpt;
189 char *type, rname[MAXPATHLEN];
190 mntwhat what;
191 struct stat sb;
192
193 if (raw) {
194 mntpt = name;
195 } else {
196
197 what = MNTANY;
198 if (realpath(name, rname) != NULL) {
199 name = rname;
200
201 if (stat(name, &sb) == 0) {
202 if (S_ISBLK(sb.st_mode))
203 what = MNTON;
204 else if (S_ISDIR(sb.st_mode))
205 what = MNTFROM;
206 }
207 }
208 #ifdef SMALL
209 else {
210 warn("%s", rname);
211 return 1;
212 }
213 #endif /* SMALL */
214 mntpt = name;
215
216 switch (what) {
217 case MNTON:
218 if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
219 warnx("%s: not currently mounted", name);
220 return (1);
221 }
222 break;
223 case MNTFROM:
224 if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
225 warnx("%s: not currently mounted", mntpt);
226 return (1);
227 }
228 break;
229 default:
230 if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) {
231 name = mntpt;
232 if ((mntpt = getmntname(name, MNTON, &type)) == NULL) {
233 warnx("%s: not currently mounted", name);
234 return 1;
235 }
236 }
237 }
238
239 #ifndef SMALL
240 if (checkvfsname(type, typelist))
241 return 1;
242
243 (void)memset(&hints, 0, sizeof hints);
244 if (!strncmp(type, MOUNT_NFS,
245 sizeof(((struct statvfs *)NULL)->f_fstypename))) {
246 char *delimp;
247 proto = getmntproto(mntpt);
248 /* look for host:mountpoint */
249 if ((delimp = strrchr(name, ':')) != NULL) {
250 int len = delimp - name;
251 hostp = malloc(len + 1);
252 if (hostp == NULL)
253 return 1;
254 memcpy(hostp, name, len);
255 hostp[len] = 0;
256 name += len + 1;
257 getaddrinfo(hostp, NULL, &hints, &ai);
258 }
259 }
260
261 if (!namematch(ai))
262 return 1;
263 #endif /* ! SMALL */
264 }
265
266 #ifndef SMALL
267 if (verbose)
268 (void)printf("%s: unmount from %s\n", name, mntpt);
269 if (fake)
270 return 0;
271 #endif /* ! SMALL */
272
273 if (unmount(mntpt, fflag) == -1) {
274 warn("%s", mntpt);
275 return 1;
276 }
277
278 #ifndef SMALL
279 if (ai != NULL && !(fflag & MNT_FORCE)) {
280 clp = clnt_create(hostp, RPCPROG_MNT, RPCMNT_VER1, proto);
281 if (clp == NULL) {
282 clnt_pcreateerror("Cannot MNT PRC");
283 return 1;
284 }
285 clp->cl_auth = authsys_create_default();
286 try.tv_sec = 20;
287 try.tv_usec = 0;
288 clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir,
289 __UNCONST(name), xdr_void, NULL, try);
290 if (clnt_stat != RPC_SUCCESS) {
291 clnt_perror(clp, "Bad MNT RPC");
292 return 1;
293 }
294 auth_destroy(clp->cl_auth);
295 clnt_destroy(clp);
296 }
297 #endif /* ! SMALL */
298 return 0;
299 }
300
301 static char *
302 getmntname(const char *name, mntwhat what, char **type)
303 {
304 static struct statvfs *mntbuf;
305 static int mntsize;
306 int i;
307
308 if (mntbuf == NULL &&
309 (mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
310 warn("getmntinfo");
311 return (NULL);
312 }
313 for (i = mntsize - 1; i >= 0; i--) {
314 if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
315 if (type)
316 *type = mntbuf[i].f_fstypename;
317 return (mntbuf[i].f_mntonname);
318 }
319 if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
320 if (type)
321 *type = mntbuf[i].f_fstypename;
322 return (mntbuf[i].f_mntfromname);
323 }
324 }
325 return (NULL);
326 }
327
328 #ifndef SMALL
329 static int
330 sacmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
331 {
332 const void *p1, *p2;
333 size_t len;
334
335 if (sa1->sa_family != sa2->sa_family)
336 return 1;
337
338 switch (sa1->sa_family) {
339 case AF_INET:
340 p1 = &((const struct sockaddr_in *)sa1)->sin_addr;
341 p2 = &((const struct sockaddr_in *)sa2)->sin_addr;
342 len = 4;
343 break;
344 case AF_INET6:
345 p1 = &((const struct sockaddr_in6 *)sa1)->sin6_addr;
346 p2 = &((const struct sockaddr_in6 *)sa2)->sin6_addr;
347 len = 16;
348 if (((const struct sockaddr_in6 *)sa1)->sin6_scope_id !=
349 ((const struct sockaddr_in6 *)sa2)->sin6_scope_id)
350 return 1;
351 break;
352 default:
353 return 1;
354 }
355
356 return memcmp(p1, p2, len);
357 }
358
359 static int
360 namematch(const struct addrinfo *ai)
361 {
362 struct addrinfo *aip;
363
364 if (nfshost == NULL || nfshost_ai == NULL)
365 return (1);
366
367 while (ai != NULL) {
368 aip = nfshost_ai;
369 while (aip != NULL) {
370 if (sacmp(ai->ai_addr, aip->ai_addr) == 0)
371 return 1;
372 aip = aip->ai_next;
373 }
374 ai = ai->ai_next;
375 }
376
377 return 0;
378 }
379
380 /*
381 * xdr routines for mount rpc's
382 */
383 static int
384 xdr_dir(XDR *xdrsp, char *dirp)
385 {
386 return xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN);
387 }
388
389 static const char *
390 getmntproto(const char *name)
391 {
392 struct nfs_args nfsargs;
393 struct sockaddr_storage ss;
394
395 nfsargs.sotype = SOCK_DGRAM;
396 nfsargs.addr = (struct sockaddr *)&ss;
397 nfsargs.addrlen = sizeof(ss);
398 (void)mount("nfs", name, MNT_GETARGS, &nfsargs, sizeof(nfsargs));
399 return nfsargs.sotype == SOCK_STREAM ? "tcp" : "udp";
400 }
401 #endif /* !SMALL */
402
403 static void
404 usage(void)
405 {
406 #ifdef SMALL
407 (void)fprintf(stderr,
408 "Usage: %s [-fR] special | node\n", getprogname());
409 #else
410 (void)fprintf(stderr,
411 "Usage: %s [-fvFR] [-t fstypelist] special | node\n"
412 "\t %s -a[fvF] [-h host] [-t fstypelist]\n", getprogname(),
413 getprogname());
414 #endif /* SMALL */
415 exit(1);
416 }
417