umount.c revision 1.6 1 1.1 cgd /*-
2 1.1 cgd * Copyright (c) 1980, 1989 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.1 cgd char copyright[] =
36 1.1 cgd "@(#) Copyright (c) 1980, 1989 The Regents of the University of California.\n\
37 1.1 cgd All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.4 mycroft /*static char sccsid[] = "from: @(#)umount.c 5.16 (Berkeley) 6/3/91";*/
42 1.6 cgd static char rcsid[] = "$Id: umount.c,v 1.6 1994/04/14 03:25:38 cgd Exp $";
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/param.h>
46 1.1 cgd #include <sys/stat.h>
47 1.1 cgd #include <sys/mount.h>
48 1.1 cgd
49 1.1 cgd #ifdef NFS
50 1.1 cgd #include <sys/time.h>
51 1.1 cgd #include <sys/socket.h>
52 1.1 cgd #include <sys/socketvar.h>
53 1.1 cgd #include <netdb.h>
54 1.1 cgd #include <rpc/rpc.h>
55 1.1 cgd #include <rpc/pmap_clnt.h>
56 1.1 cgd #include <rpc/pmap_prot.h>
57 1.1 cgd #include <nfs/rpcv2.h>
58 1.1 cgd #endif
59 1.1 cgd
60 1.6 cgd #include <err.h>
61 1.1 cgd #include <fstab.h>
62 1.1 cgd #include <stdio.h>
63 1.1 cgd #include <string.h>
64 1.6 cgd #include <stdlib.h>
65 1.6 cgd #include <unistd.h>
66 1.1 cgd
67 1.1 cgd #ifdef NFS
68 1.1 cgd char *nfshost;
69 1.1 cgd #endif
70 1.1 cgd
71 1.1 cgd int vflag, all, errs, fake;
72 1.1 cgd int fflag = MNT_NOFORCE;
73 1.1 cgd
74 1.1 cgd #define MNTON 1
75 1.1 cgd #define MNTFROM 2
76 1.1 cgd #define MNTTYPE 3
77 1.1 cgd
78 1.6 cgd char **typelist;
79 1.1 cgd
80 1.6 cgd struct fstab *allocfsent __P((struct fstab *));
81 1.6 cgd int badtype __P((char *, char **));
82 1.6 cgd void freefsent __P((struct fstab *));
83 1.6 cgd char *getmntname __P((char *, int, char *));
84 1.6 cgd char **maketypelist __P((char *));
85 1.6 cgd void umountall __P((char **));
86 1.6 cgd int umountfs __P((char *, char **, int));
87 1.6 cgd void usage __P((void));
88 1.6 cgd #ifdef NFS
89 1.6 cgd int namematch __P((struct hostent *, char *));
90 1.6 cgd int xdr_dir __P((XDR *, char *));
91 1.6 cgd #endif
92 1.6 cgd
93 1.6 cgd int
94 1.1 cgd main(argc, argv)
95 1.1 cgd int argc;
96 1.1 cgd char **argv;
97 1.1 cgd {
98 1.1 cgd extern char *optarg;
99 1.1 cgd extern int optind;
100 1.1 cgd int ch;
101 1.1 cgd
102 1.1 cgd sync();
103 1.1 cgd while ((ch = getopt(argc, argv, "afFh:t:v")) != EOF)
104 1.1 cgd switch((char)ch) {
105 1.1 cgd case 'v':
106 1.1 cgd vflag++;
107 1.1 cgd break;
108 1.1 cgd case 'f':
109 1.1 cgd fflag = MNT_FORCE;
110 1.1 cgd break;
111 1.1 cgd case 'F':
112 1.1 cgd fake++;
113 1.1 cgd break;
114 1.1 cgd case 'a':
115 1.1 cgd all++;
116 1.1 cgd break;
117 1.1 cgd case 't':
118 1.1 cgd typelist = maketypelist(optarg);
119 1.1 cgd break;
120 1.1 cgd #ifdef NFS
121 1.1 cgd case 'h':
122 1.1 cgd /* -h flag implies -a, and "-t nfs" if no -t flag */
123 1.1 cgd nfshost = optarg;
124 1.1 cgd all++;
125 1.1 cgd if (typelist == NULL)
126 1.6 cgd typelist = maketypelist(MOUNT_NFS);
127 1.1 cgd break;
128 1.1 cgd #endif /* NFS */
129 1.1 cgd case '?':
130 1.1 cgd default:
131 1.1 cgd usage();
132 1.1 cgd /* NOTREACHED */
133 1.1 cgd }
134 1.1 cgd argc -= optind;
135 1.1 cgd argv += optind;
136 1.1 cgd
137 1.1 cgd if (argc == 0 && !all)
138 1.1 cgd usage();
139 1.1 cgd if (all) {
140 1.1 cgd if (argc > 0)
141 1.1 cgd usage();
142 1.1 cgd if (setfsent() == 0)
143 1.6 cgd err(1, "%s", FSTAB);
144 1.1 cgd umountall(typelist);
145 1.1 cgd exit(0);
146 1.1 cgd } else
147 1.1 cgd setfsent();
148 1.1 cgd while (argc > 0) {
149 1.6 cgd if (umountfs(*argv++, 0, 0) == 0)
150 1.1 cgd errs++;
151 1.1 cgd argc--;
152 1.1 cgd }
153 1.1 cgd exit(errs);
154 1.1 cgd }
155 1.1 cgd
156 1.6 cgd void
157 1.1 cgd usage()
158 1.1 cgd {
159 1.1 cgd fprintf(stderr,
160 1.1 cgd "%s\n%s\n",
161 1.1 cgd "Usage: umount [-fv] special | node",
162 1.1 cgd #ifndef NFS
163 1.1 cgd " or umount -a[fv] [-t fstypelist]"
164 1.1 cgd #else
165 1.1 cgd " or umount -a[fv] [-h host] [-t fstypelist]"
166 1.1 cgd #endif
167 1.1 cgd );
168 1.1 cgd exit(1);
169 1.1 cgd }
170 1.1 cgd
171 1.6 cgd void
172 1.1 cgd umountall(typelist)
173 1.1 cgd char **typelist;
174 1.1 cgd {
175 1.1 cgd register struct fstab *fs;
176 1.1 cgd struct fstab *allocfsent();
177 1.1 cgd
178 1.1 cgd if ((fs = getfsent()) == (struct fstab *)0)
179 1.1 cgd return;
180 1.1 cgd fs = allocfsent(fs);
181 1.1 cgd umountall(typelist);
182 1.1 cgd if (strcmp(fs->fs_file, "/") == 0) {
183 1.1 cgd freefsent(fs);
184 1.1 cgd return;
185 1.1 cgd }
186 1.1 cgd if (strcmp(fs->fs_type, FSTAB_RW) &&
187 1.1 cgd strcmp(fs->fs_type, FSTAB_RO) &&
188 1.1 cgd strcmp(fs->fs_type, FSTAB_RQ)) {
189 1.1 cgd freefsent(fs);
190 1.1 cgd return;
191 1.1 cgd }
192 1.6 cgd (void) umountfs(fs->fs_file, typelist, 1);
193 1.1 cgd freefsent(fs);
194 1.1 cgd }
195 1.1 cgd
196 1.1 cgd struct fstab *
197 1.1 cgd allocfsent(fs)
198 1.1 cgd register struct fstab *fs;
199 1.1 cgd {
200 1.1 cgd register struct fstab *new;
201 1.1 cgd register char *cp;
202 1.1 cgd
203 1.1 cgd new = (struct fstab *)malloc((unsigned)sizeof (*fs));
204 1.1 cgd cp = (char *)malloc((unsigned)strlen(fs->fs_file) + 1);
205 1.1 cgd strcpy(cp, fs->fs_file);
206 1.1 cgd new->fs_file = cp;
207 1.1 cgd cp = (char *)malloc((unsigned)strlen(fs->fs_type) + 1);
208 1.1 cgd strcpy(cp, fs->fs_type);
209 1.1 cgd new->fs_type = cp;
210 1.1 cgd cp = (char *)malloc((unsigned)strlen(fs->fs_spec) + 1);
211 1.1 cgd strcpy(cp, fs->fs_spec);
212 1.1 cgd new->fs_spec = cp;
213 1.1 cgd new->fs_passno = fs->fs_passno;
214 1.1 cgd new->fs_freq = fs->fs_freq;
215 1.1 cgd return (new);
216 1.1 cgd }
217 1.1 cgd
218 1.6 cgd void
219 1.1 cgd freefsent(fs)
220 1.1 cgd register struct fstab *fs;
221 1.1 cgd {
222 1.1 cgd
223 1.6 cgd if (fs->fs_file != NULL)
224 1.1 cgd free(fs->fs_file);
225 1.6 cgd if (fs->fs_spec != NULL)
226 1.1 cgd free(fs->fs_spec);
227 1.6 cgd if (fs->fs_type != NULL)
228 1.1 cgd free(fs->fs_type);
229 1.1 cgd free((char *)fs);
230 1.1 cgd }
231 1.1 cgd
232 1.6 cgd int
233 1.6 cgd umountfs(name, typelist, all)
234 1.1 cgd char *name;
235 1.6 cgd char **typelist;
236 1.6 cgd int all;
237 1.1 cgd {
238 1.1 cgd char *mntpt;
239 1.1 cgd struct stat stbuf;
240 1.6 cgd char type[MFSNAMELEN+1];
241 1.1 cgd #ifdef NFS
242 1.1 cgd register CLIENT *clp;
243 1.1 cgd struct hostent *hp = 0;
244 1.1 cgd struct sockaddr_in saddr;
245 1.1 cgd struct timeval pertry, try;
246 1.1 cgd enum clnt_stat clnt_stat;
247 1.1 cgd int so = RPC_ANYSOCK;
248 1.1 cgd char *hostp, *delimp;
249 1.1 cgd #endif /* NFS */
250 1.1 cgd
251 1.1 cgd if (stat(name, &stbuf) < 0) {
252 1.6 cgd if (getmntname(name, MNTFROM, type) != 0)
253 1.1 cgd mntpt = name;
254 1.6 cgd else if ((mntpt = getmntname(name, MNTON, type)) == 0) {
255 1.6 cgd if (!all)
256 1.6 cgd warnx("%s: not currently mounted\n", name);
257 1.1 cgd return (0);
258 1.1 cgd }
259 1.1 cgd } else if ((stbuf.st_mode & S_IFMT) == S_IFBLK) {
260 1.6 cgd if ((mntpt = getmntname(name, MNTON, type)) == 0) {
261 1.6 cgd if (!all)
262 1.6 cgd warnx("%s: not currently mounted\n", name);
263 1.1 cgd return (0);
264 1.1 cgd }
265 1.1 cgd } else if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
266 1.1 cgd mntpt = name;
267 1.6 cgd if (getmntname(mntpt, MNTFROM, type) == 0) {
268 1.6 cgd if (!all)
269 1.6 cgd warnx("%s: not currently mounted\n", name);
270 1.1 cgd return (0);
271 1.1 cgd }
272 1.1 cgd } else {
273 1.6 cgd if (!all)
274 1.6 cgd warnx("%s: not a directory or special device\n", name);
275 1.1 cgd return (0);
276 1.1 cgd }
277 1.1 cgd
278 1.1 cgd if (badtype(type, typelist))
279 1.1 cgd return(1);
280 1.1 cgd #ifdef NFS
281 1.6 cgd if ((delimp = strchr(name, '@')) != NULL) {
282 1.1 cgd hostp = delimp + 1;
283 1.1 cgd *delimp = '\0';
284 1.1 cgd hp = gethostbyname(hostp);
285 1.1 cgd *delimp = '@';
286 1.6 cgd } else if ((delimp = strchr(name, ':')) != NULL) {
287 1.1 cgd *delimp = '\0';
288 1.1 cgd hostp = name;
289 1.1 cgd hp = gethostbyname(hostp);
290 1.1 cgd name = delimp+1;
291 1.1 cgd *delimp = ':';
292 1.1 cgd }
293 1.1 cgd
294 1.1 cgd if (!namematch(hp, nfshost))
295 1.1 cgd return(1);
296 1.1 cgd #endif /* NFS */
297 1.1 cgd if (!fake && unmount(mntpt, fflag) < 0) {
298 1.6 cgd warn("%s", mntpt);
299 1.1 cgd return (0);
300 1.1 cgd }
301 1.1 cgd if (vflag)
302 1.1 cgd fprintf(stderr, "%s: Unmounted from %s\n", name, mntpt);
303 1.1 cgd
304 1.1 cgd #ifdef NFS
305 1.1 cgd if (!fake && hp != NULL && (fflag & MNT_FORCE) == 0) {
306 1.1 cgd *delimp = '\0';
307 1.5 deraadt bzero((char *)&saddr, sizeof saddr);
308 1.1 cgd bcopy(hp->h_addr,(caddr_t)&saddr.sin_addr,hp->h_length);
309 1.1 cgd saddr.sin_family = AF_INET;
310 1.1 cgd saddr.sin_port = 0;
311 1.1 cgd pertry.tv_sec = 3;
312 1.1 cgd pertry.tv_usec = 0;
313 1.1 cgd if ((clp = clntudp_create(&saddr, RPCPROG_MNT, RPCMNT_VER1,
314 1.1 cgd pertry, &so)) == NULL) {
315 1.1 cgd clnt_pcreateerror("Cannot MNT PRC");
316 1.1 cgd return (1);
317 1.1 cgd }
318 1.1 cgd clp->cl_auth = authunix_create_default();
319 1.1 cgd try.tv_sec = 20;
320 1.1 cgd try.tv_usec = 0;
321 1.1 cgd clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir, name,
322 1.1 cgd xdr_void, (caddr_t)0, try);
323 1.1 cgd if (clnt_stat != RPC_SUCCESS) {
324 1.1 cgd clnt_perror(clp, "Bad MNT RPC");
325 1.1 cgd return (1);
326 1.1 cgd }
327 1.1 cgd auth_destroy(clp->cl_auth);
328 1.1 cgd clnt_destroy(clp);
329 1.1 cgd }
330 1.1 cgd #endif /* NFS */
331 1.1 cgd return (1);
332 1.1 cgd }
333 1.1 cgd
334 1.1 cgd char *
335 1.1 cgd getmntname(name, what, type)
336 1.1 cgd char *name;
337 1.1 cgd int what;
338 1.6 cgd char *type;
339 1.1 cgd {
340 1.1 cgd int mntsize, i;
341 1.1 cgd struct statfs *mntbuf;
342 1.1 cgd
343 1.1 cgd if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
344 1.6 cgd warn("umount");
345 1.1 cgd return (0);
346 1.1 cgd }
347 1.1 cgd for (i = 0; i < mntsize; i++) {
348 1.1 cgd if (what == MNTON && !strcmp(mntbuf[i].f_mntfromname, name)) {
349 1.6 cgd if (type) {
350 1.6 cgd strncpy(type, mntbuf[i].f_fstypename,
351 1.6 cgd MFSNAMELEN);
352 1.6 cgd type[MFSNAMELEN] = '\0';
353 1.6 cgd }
354 1.1 cgd return (mntbuf[i].f_mntonname);
355 1.1 cgd }
356 1.1 cgd if (what == MNTFROM && !strcmp(mntbuf[i].f_mntonname, name)) {
357 1.6 cgd if (type) {
358 1.6 cgd strncpy(type, mntbuf[i].f_fstypename,
359 1.6 cgd MFSNAMELEN);
360 1.6 cgd type[MFSNAMELEN] = '\0';
361 1.6 cgd }
362 1.1 cgd return (mntbuf[i].f_mntfromname);
363 1.1 cgd }
364 1.1 cgd }
365 1.1 cgd return (0);
366 1.1 cgd }
367 1.1 cgd
368 1.1 cgd static int skipvfs;
369 1.1 cgd
370 1.6 cgd int
371 1.1 cgd badtype(type, typelist)
372 1.6 cgd char *type;
373 1.6 cgd char **typelist;
374 1.1 cgd {
375 1.6 cgd if (typelist == NULL)
376 1.1 cgd return(0);
377 1.1 cgd while (*typelist) {
378 1.6 cgd if (!strcmp(type, *typelist))
379 1.1 cgd return(skipvfs);
380 1.1 cgd typelist++;
381 1.1 cgd }
382 1.1 cgd return(!skipvfs);
383 1.1 cgd }
384 1.1 cgd
385 1.6 cgd char **
386 1.1 cgd maketypelist(fslist)
387 1.1 cgd char *fslist;
388 1.1 cgd {
389 1.6 cgd register char *nextcp, **av;
390 1.6 cgd register int i;
391 1.1 cgd
392 1.1 cgd if (fslist == NULL)
393 1.1 cgd return(NULL);
394 1.1 cgd if (fslist[0] == 'n' && fslist[1] == 'o') {
395 1.1 cgd fslist += 2;
396 1.1 cgd skipvfs = 1;
397 1.1 cgd } else
398 1.1 cgd skipvfs = 0;
399 1.1 cgd for (i = 0, nextcp = fslist; *nextcp; nextcp++)
400 1.1 cgd if (*nextcp == ',')
401 1.1 cgd i++;
402 1.6 cgd av = (char **)malloc((i+2) * sizeof(int));
403 1.1 cgd if (av == NULL)
404 1.1 cgd return(NULL);
405 1.1 cgd for (i = 0; fslist; fslist = nextcp) {
406 1.6 cgd if ((nextcp = strchr(fslist, ',')) != NULL)
407 1.1 cgd *nextcp++ = '\0';
408 1.6 cgd av[i++] = strdup(fslist);
409 1.1 cgd }
410 1.1 cgd av[i++] = 0;
411 1.1 cgd return(av);
412 1.1 cgd }
413 1.1 cgd
414 1.1 cgd #ifdef NFS
415 1.6 cgd int
416 1.1 cgd namematch(hp, nfshost)
417 1.1 cgd struct hostent *hp;
418 1.1 cgd char *nfshost;
419 1.1 cgd {
420 1.1 cgd register char *cp;
421 1.1 cgd register char **np;
422 1.1 cgd
423 1.1 cgd if (hp == NULL || nfshost == NULL)
424 1.1 cgd return(1);
425 1.1 cgd if (strcasecmp(nfshost, hp->h_name) == 0)
426 1.1 cgd return(1);
427 1.6 cgd if ((cp = strchr(hp->h_name, '.')) != NULL) {
428 1.1 cgd *cp = '\0';
429 1.1 cgd if (strcasecmp(nfshost, hp->h_name) == 0)
430 1.1 cgd return(1);
431 1.1 cgd }
432 1.1 cgd for (np = hp->h_aliases; *np; np++) {
433 1.1 cgd if (strcasecmp(nfshost, *np) == 0)
434 1.1 cgd return(1);
435 1.6 cgd if ((cp = strchr(*np, '.')) != NULL) {
436 1.1 cgd *cp = '\0';
437 1.1 cgd if (strcasecmp(nfshost, *np) == 0)
438 1.1 cgd return(1);
439 1.1 cgd }
440 1.1 cgd }
441 1.1 cgd return(0);
442 1.1 cgd }
443 1.1 cgd
444 1.1 cgd /*
445 1.1 cgd * xdr routines for mount rpc's
446 1.1 cgd */
447 1.6 cgd int
448 1.1 cgd xdr_dir(xdrsp, dirp)
449 1.1 cgd XDR *xdrsp;
450 1.1 cgd char *dirp;
451 1.1 cgd {
452 1.1 cgd return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
453 1.1 cgd }
454 1.1 cgd #endif /* NFS */
455