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