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