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