umount.c revision 1.17 1 /* $NetBSD: umount.c,v 1.17 1997/05/21 21:47:07 pk 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 #ifndef lint
37 static char copyright[] =
38 "@(#) 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.3 (Berkeley) 2/20/94";
45 #else
46 static char rcsid[] = "$NetBSD: umount.c,v 1.17 1997/05/21 21:47:07 pk 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 #include <sys/socketvar.h>
56
57 #include <netdb.h>
58 #include <rpc/rpc.h>
59 #include <rpc/pmap_clnt.h>
60 #include <rpc/pmap_prot.h>
61 #include <nfs/rpcv2.h>
62
63 #include <err.h>
64 #include <fstab.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <unistd.h>
69
70 typedef enum { MNTON, MNTFROM } mntwhat;
71
72 int fake, fflag, verbose;
73 char **typelist = NULL;
74 char *nfshost;
75
76 char *getmntname __P((char *, mntwhat, char *));
77 void maketypelist __P((char *));
78 int selected __P((const char *));
79 int namematch __P((struct hostent *));
80 int umountall __P((void));
81 int umountfs __P((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;
91
92 /* Start disks transferring immediately. */
93 sync();
94
95 all = 0;
96 while ((ch = getopt(argc, argv, "aFfh:t:v")) != EOF)
97 switch (ch) {
98 case 'a':
99 all = 1;
100 break;
101 case 'F':
102 fake = 1;
103 break;
104 case 'f':
105 fflag = MNT_FORCE;
106 break;
107 case 'h': /* -h implies -a. */
108 all = 1;
109 nfshost = optarg;
110 break;
111 case 't':
112 if (typelist != NULL)
113 errx(1, "only one -t option may be specified.");
114 maketypelist(optarg);
115 break;
116 case 'v':
117 verbose = 1;
118 break;
119 default:
120 usage();
121 /* NOTREACHED */
122 }
123 argc -= optind;
124 argv += optind;
125
126 if (argc == 0 && !all || argc != 0 && all)
127 usage();
128
129 /* -h implies "-t nfs" if no -t flag. */
130 if ((nfshost != NULL) && (typelist == NULL))
131 maketypelist("nfs");
132
133 if (all)
134 errs = umountall();
135 else
136 for (errs = 0; *argv != NULL; ++argv)
137 if (umountfs(*argv) != 0)
138 errs = 1;
139 exit(errs);
140 }
141
142 int
143 umountall()
144 {
145 struct statfs *fs;
146 int n;
147 int rval;
148
149 n = getmntinfo(&fs, MNT_NOWAIT);
150 if (n == 0)
151 err(1, NULL);
152
153 rval = 0;
154 while (--n >= 0) {
155 /* Ignore the root. */
156 if (strncmp(fs[n].f_mntonname, "/", MNAMELEN) == 0)
157 continue;
158
159 if (!selected(fs[n].f_fstypename))
160 continue;
161
162 if (umountfs(fs[n].f_mntonname))
163 rval = 1;
164 }
165 return (rval);
166 }
167
168 int
169 umountfs(name)
170 char *name;
171 {
172 enum clnt_stat clnt_stat;
173 struct hostent *hp;
174 struct sockaddr_in saddr;
175 struct stat sb;
176 struct timeval pertry, try;
177 CLIENT *clp;
178 int so;
179 char *delimp, *hostp, *mntpt, rname[MAXPATHLEN], type[MFSNAMELEN];
180
181 if (realpath(name, rname) == NULL) {
182 warn("%s", rname);
183 return (1);
184 }
185
186 mntpt = name = rname;
187
188 if (stat(name, &sb) == 0 &&
189 (S_ISBLK(sb.st_mode) || S_ISDIR(sb.st_mode))) {
190 if (S_ISBLK(sb.st_mode)) {
191 if ((mntpt = getmntname(name, MNTON, type)) == NULL) {
192 warnx("%s: not currently mounted", name);
193 return (1);
194 }
195 } else if (S_ISDIR(sb.st_mode)) {
196 if ((name = getmntname(mntpt, MNTFROM, type)) == NULL) {
197 warnx("%s: not currently mounted", mntpt);
198 return (1);
199 }
200 }
201 } else {
202 if ((name = getmntname(mntpt, MNTFROM, type)) == NULL) {
203 name = rname;
204 if ((mntpt = getmntname(name, MNTON, type)) == NULL) {
205 warnx("%s: not currently mounted", name);
206 return (1);
207 }
208 }
209 }
210
211 if (!selected(type))
212 return (1);
213
214 if (!strncmp(type, MOUNT_NFS, MFSNAMELEN)) {
215 if ((delimp = strchr(name, '@')) != NULL) {
216 hostp = delimp + 1;
217 *delimp = '\0';
218 hp = gethostbyname(hostp);
219 *delimp = '@';
220 } else if ((delimp = strchr(name, ':')) != NULL) {
221 *delimp = '\0';
222 hostp = name;
223 hp = gethostbyname(hostp);
224 name = delimp + 1;
225 *delimp = ':';
226 } else
227 hp = NULL;
228 if (!namematch(hp))
229 return (1);
230 }
231
232 if (verbose)
233 (void)printf("%s: unmount from %s\n", name, mntpt);
234 if (fake)
235 return (0);
236
237 if (unmount(mntpt, fflag) < 0) {
238 warn("%s", mntpt);
239 return (1);
240 }
241
242 if (!strncmp(type, MOUNT_NFS, MFSNAMELEN) &&
243 (hp != NULL) && !(fflag & MNT_FORCE)) {
244 *delimp = '\0';
245 memset(&saddr, 0, sizeof(saddr));
246 saddr.sin_family = AF_INET;
247 saddr.sin_port = 0;
248 memmove(&saddr.sin_addr, hp->h_addr, hp->h_length);
249 pertry.tv_sec = 3;
250 pertry.tv_usec = 0;
251 so = RPC_ANYSOCK;
252 if ((clp = clntudp_create(&saddr,
253 RPCPROG_MNT, RPCMNT_VER1, pertry, &so)) == NULL) {
254 clnt_pcreateerror("Cannot MNT PRC");
255 return (1);
256 }
257 clp->cl_auth = authunix_create_default();
258 try.tv_sec = 20;
259 try.tv_usec = 0;
260 clnt_stat = clnt_call(clp,
261 RPCMNT_UMOUNT, xdr_dir, name, xdr_void, (caddr_t)0, try);
262 if (clnt_stat != RPC_SUCCESS) {
263 clnt_perror(clp, "Bad MNT RPC");
264 return (1);
265 }
266 auth_destroy(clp->cl_auth);
267 clnt_destroy(clp);
268 }
269 return (0);
270 }
271
272 char *
273 getmntname(name, what, type)
274 char *name;
275 mntwhat what;
276 char *type;
277 {
278 struct statfs *mntbuf;
279 int i, mntsize;
280
281 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
282 warn("getmntinfo");
283 return (NULL);
284 }
285 for (i = 0; i < mntsize; i++) {
286 if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) {
287 if (type)
288 memcpy(type, mntbuf[i].f_fstypename,
289 sizeof(mntbuf[i].f_fstypename));
290 return (mntbuf[i].f_mntonname);
291 }
292 if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) {
293 if (type)
294 memcpy(type, mntbuf[i].f_fstypename,
295 sizeof(mntbuf[i].f_fstypename));
296 return (mntbuf[i].f_mntfromname);
297 }
298 }
299 return (NULL);
300 }
301
302 static enum { IN_LIST, NOT_IN_LIST } which;
303
304 int
305 selected(type)
306 const char *type;
307 {
308 char **av;
309
310 /* If no type specified, it's always selected. */
311 if (typelist == NULL)
312 return (1);
313 for (av = typelist; *av != NULL; ++av)
314 if (!strncmp(type, *av, MFSNAMELEN))
315 return (which == IN_LIST ? 1 : 0);
316 return (which == IN_LIST ? 0 : 1);
317 }
318
319 void
320 maketypelist(fslist)
321 char *fslist;
322 {
323 int i;
324 char *nextcp, **av;
325
326 if ((fslist == NULL) || (fslist[0] == '\0'))
327 errx(1, "empty type list");
328
329 /*
330 * XXX
331 * Note: the syntax is "noxxx,yyy" for no xxx's and
332 * no yyy's, not the more intuitive "noyyy,noyyy".
333 */
334 if (fslist[0] == 'n' && fslist[1] == 'o') {
335 fslist += 2;
336 which = NOT_IN_LIST;
337 } else
338 which = IN_LIST;
339
340 /* Count the number of types. */
341 for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++)
342 ++nextcp;
343
344 /* Build an array of that many types. */
345 if ((av = typelist = malloc((i + 1) * sizeof(char *))) == NULL)
346 err(1, NULL);
347 av[0] = fslist;
348 for (i = 1, nextcp = fslist; nextcp = strchr(nextcp, ','); i++) {
349 *nextcp = '\0';
350 av[i] = ++nextcp;
351 }
352 /* Terminate the array. */
353 av[i] = NULL;
354 }
355
356 int
357 namematch(hp)
358 struct hostent *hp;
359 {
360 char *cp, **np;
361
362 if ((hp == NULL) || (nfshost == NULL))
363 return (1);
364
365 if (strcasecmp(nfshost, hp->h_name) == 0)
366 return (1);
367
368 if ((cp = strchr(hp->h_name, '.')) != NULL) {
369 *cp = '\0';
370 if (strcasecmp(nfshost, hp->h_name) == 0)
371 return (1);
372 }
373 for (np = hp->h_aliases; *np; np++) {
374 if (strcasecmp(nfshost, *np) == 0)
375 return (1);
376 if ((cp = strchr(*np, '.')) != NULL) {
377 *cp = '\0';
378 if (strcasecmp(nfshost, *np) == 0)
379 return (1);
380 }
381 }
382 return (0);
383 }
384
385 /*
386 * xdr routines for mount rpc's
387 */
388 int
389 xdr_dir(xdrsp, dirp)
390 XDR *xdrsp;
391 char *dirp;
392 {
393 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
394 }
395
396 void
397 usage()
398 {
399 (void)fprintf(stderr,
400 "usage: %s\n %s\n",
401 "umount [-fv] [-t fstypelist] special | node",
402 "umount -a[fv] [-h host] [-t fstypelist]");
403 exit(1);
404 }
405