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