umount.c revision 1.34 1 /* $NetBSD: umount.c,v 1.34 2004/04/21 15:20:40 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\n\
35 The Regents of the University of California. All rights reserved.\n");
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.34 2004/04/21 15:20:40 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 #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 "vfslist.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, raw;
82 static char *getmntname(const char *, mntwhat, char **);
83 static int umountfs(const char *, const char **);
84 static void usage(void) __attribute__((__noreturn__));
85
86 int main(int, char *[]);
87
88 int
89 main(int argc, char *argv[])
90 {
91 int ch, errs, all = 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) != 0)
166 errs = 1;
167 }
168 } else
169 #endif /* !SMALL */
170 for (errs = 0; *argv != NULL; ++argv)
171 if (umountfs(*argv, typelist) != 0)
172 errs = 1;
173 return errs;
174 }
175
176 static int
177 umountfs(const char *name, const char **typelist)
178 {
179 #ifndef SMALL
180 enum clnt_stat clnt_stat;
181 struct timeval try;
182 CLIENT *clp;
183 char *hostp = NULL;
184 struct addrinfo *ai = NULL, hints;
185 #endif /* !SMALL */
186 const char *mntpt;
187 char *type, rname[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", rname);
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, MFSNAMELEN)) {
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) < 0) {
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,
285 RPCMNT_UMOUNT, xdr_dir, name, xdr_void, (caddr_t)0, 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 void *p1, *p2;
329 int 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 = &((struct sockaddr_in *)sa1)->sin_addr;
337 p2 = &((struct sockaddr_in *)sa2)->sin_addr;
338 len = 4;
339 break;
340 case AF_INET6:
341 p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
342 p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
343 len = 16;
344 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
345 ((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