Print this page
*** NO COMMENTS ***
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/fs.d/nfs/umount/umount.c
+++ new/usr/src/cmd/fs.d/nfs/umount/umount.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 27 /* All Rights Reserved */
28 28
29 29 /*
30 30 * University Copyright- Copyright (c) 1982, 1986, 1988
31 31 * The Regents of the University of California
32 32 * All Rights Reserved
33 33 *
34 34 * University Acknowledgment- Portions of this document are derived from
35 35 * software developed by the University of California, Berkeley, and its
36 36 * contributors.
37 37 */
38 38
39 39 #pragma ident "%Z%%M% %I% %E% SMI"
40 40
41 41 /*
42 42 * nfs umount
43 43 */
44 44
45 45 #include <stdio.h>
46 46 #include <stdlib.h>
47 47 #include <string.h>
48 48 #include <stdarg.h>
49 49 #include <signal.h>
50 50 #include <unistd.h>
51 51 #include <kstat.h>
52 52 #include <rpc/rpc.h>
53 53 #include <sys/mnttab.h>
54 54 #include <sys/mount.h>
55 55 #include <sys/mntent.h>
56 56 #include <nfs/nfs.h>
57 57 #include <nfs/nfs_clnt.h>
58 58 #include <rpcsvc/mount.h>
59 59 #include <errno.h>
60 60 #include <locale.h>
61 61 #include <fslib.h>
62 62 #include <priv.h>
63 63 #include <tsol/label.h>
64 64 #include "replica.h"
65 65
66 66 #define RET_OK 0
67 67 #define RET_ERR 32
↓ open down ↓ |
67 lines elided |
↑ open up ↑ |
68 68
69 69 #ifdef __STDC__
70 70 static void pr_err(const char *fmt, ...);
71 71 #else
72 72 static void pr_err(char *fmt, va_dcl);
73 73 #endif
74 74 static void usage();
75 75 static int nfs_unmount(char *, int);
76 76 static void inform_server(char *, char *, bool_t);
77 77 static struct extmnttab *mnttab_find();
78 -extern int __clnt_bindresvport();
78 +extern int __clnt_bindresvport(CLIENT *);
79 79
80 80 static int is_v4_mount(struct extmnttab *);
81 81
82 82 static char *myname;
83 83 static char typename[64];
84 84
85 85 int
86 86 main(int argc, char *argv[])
87 87 {
88 88 extern int optind;
89 89 int c;
90 90 int umnt_flag = 0;
↓ open down ↓ |
2 lines elided |
↑ open up ↑ |
91 91
92 92 (void) setlocale(LC_ALL, "");
93 93
94 94 #if !defined(TEXT_DOMAIN)
95 95 #define TEXT_DOMAIN "SYS_TEST"
96 96 #endif
97 97 (void) textdomain(TEXT_DOMAIN);
98 98
99 99 myname = strrchr(argv[0], '/');
100 100 myname = myname ? myname+1 : argv[0];
101 - (void) sprintf(typename, "nfs %s", myname);
101 + (void) snprintf(typename, sizeof (typename), "nfs %s", myname);
102 102 argv[0] = typename;
103 103
104 104 /*
105 105 * Set options
106 106 */
107 107 while ((c = getopt(argc, argv, "f")) != EOF) {
108 108 switch (c) {
109 109 case 'f':
110 110 umnt_flag |= MS_FORCE; /* forced unmount is desired */
111 111 break;
112 112 default:
113 113 usage();
114 114 exit(RET_ERR);
115 115 }
116 116 }
117 117 if (argc - optind != 1) {
118 118 usage();
119 119 exit(RET_ERR);
120 120 }
121 121
122 122 if (!priv_ineffect(PRIV_SYS_MOUNT) ||
123 123 !priv_ineffect(PRIV_NET_PRIVADDR)) {
124 124 pr_err(gettext("insufficient privileges\n"));
125 125 exit(RET_ERR);
126 126 }
127 127
128 128 /*
129 129 * On a labeled system, a MAC flag may be needed if the mount is
130 130 * read-down, so attempt to assert it but ignore errors in any case.
131 131 */
132 132 if (is_system_labeled())
133 133 (void) setpflags(NET_MAC_AWARE, 1);
134 134
135 135 /*
136 136 * exit, really
137 137 */
138 138 return (nfs_unmount(argv[optind], umnt_flag));
139 139 }
140 140
141 141 static void
142 142 pr_err(const char *fmt, ...)
143 143 {
144 144 va_list ap;
145 145
146 146 va_start(ap, fmt);
↓ open down ↓ |
35 lines elided |
↑ open up ↑ |
147 147 (void) fprintf(stderr, "%s: ", typename);
148 148 (void) vfprintf(stderr, fmt, ap);
149 149 (void) fflush(stderr);
150 150 va_end(ap);
151 151 }
152 152
153 153 static void
154 154 usage()
155 155 {
156 156 (void) fprintf(stderr,
157 - gettext("Usage: nfs umount [-o opts] {server:path | dir}\n"));
157 + gettext("Usage: nfs umount [-f] {server:path | dir}\n"));
158 158 exit(RET_ERR);
159 159 }
160 160
161 161 static int
162 162 nfs_unmount(char *pathname, int umnt_flag)
163 163 {
164 164 struct extmnttab *mntp;
165 165 bool_t quick = FALSE;
166 166 int is_v4 = FALSE;
167 167
168 168 mntp = mnttab_find(pathname);
169 169 if (mntp) {
170 170 pathname = mntp->mnt_mountp;
171 171 }
172 172
173 173 if (mntp)
174 174 is_v4 = is_v4_mount(mntp);
175 175
176 176 /* Forced unmount will almost always be successful */
177 177 if (umount2(pathname, umnt_flag) < 0) {
178 178 if (errno == EBUSY) {
179 179 pr_err(gettext("%s: is busy\n"), pathname);
180 180 } else {
181 181 pr_err(gettext("%s: not mounted\n"), pathname);
182 182 }
183 183 return (RET_ERR);
184 184 }
185 185
186 186 /* All done if it's v4 */
187 187 if (is_v4 == TRUE)
188 188 return (RET_OK);
189 189
190 190 /* Inform server quickly in case of forced unmount */
191 191 if (umnt_flag & MS_FORCE)
192 192 quick = TRUE;
193 193
194 194 if (mntp) {
195 195 inform_server(mntp->mnt_special, mntp->mnt_mntopts, quick);
196 196 }
197 197
198 198 return (RET_OK);
199 199 }
200 200
201 201 /*
202 202 * Find the mnttab entry that corresponds to "name".
203 203 * We're not sure what the name represents: either
204 204 * a mountpoint name, or a special name (server:/path).
205 205 * Return the last entry in the file that matches.
206 206 */
207 207 static struct extmnttab *
208 208 mnttab_find(dirname)
209 209 char *dirname;
210 210 {
211 211 FILE *fp;
212 212 struct extmnttab mnt;
213 213 struct extmnttab *res = NULL;
214 214
215 215 fp = fopen(MNTTAB, "r");
216 216 if (fp == NULL) {
217 217 pr_err("%s: %s\n", MNTTAB, strerror(errno));
218 218 return (NULL);
↓ open down ↓ |
51 lines elided |
↑ open up ↑ |
219 219 }
220 220 while (getextmntent(fp, &mnt, sizeof (struct extmnttab)) == 0) {
221 221 if (strcmp(mnt.mnt_mountp, dirname) == 0 ||
222 222 strcmp(mnt.mnt_special, dirname) == 0) {
223 223 if (res)
224 224 fsfreemnttab(res);
225 225 res = fsdupmnttab(&mnt);
226 226 }
227 227 }
228 228
229 - fclose(fp);
229 + (void) fclose(fp);
230 230 return (res);
231 231 }
232 232
233 233 /*
234 234 * If quick is TRUE, it will try to inform server quickly
235 235 * as possible.
236 236 */
237 237 static void
238 238 inform_server(char *string, char *opts, bool_t quick)
239 239 {
240 240 struct timeval timeout;
241 241 CLIENT *cl;
242 242 enum clnt_stat rpc_stat;
243 243 struct replica *list;
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
244 244 int i, n;
245 245 char *p = NULL;
246 246 static struct timeval create_timeout = {5, 0};
247 247 static struct timeval *timep;
248 248
249 249 list = parse_replica(string, &n);
250 250
251 251 if (list == NULL) {
252 252 if (n < 0)
253 253 pr_err(gettext("%s is not hostname:path format\n"),
254 - string);
254 + string);
255 255 else
256 256 pr_err(gettext("no memory\n"));
257 257 return;
258 258 }
259 259
260 260 /*
261 261 * If mounted with -o public, then no need to contact server
262 262 * because mount protocol was not used.
263 263 */
264 264 if (opts != NULL)
265 265 p = strstr(opts, MNTOPT_PUBLIC);
266 266
267 267 if (p != NULL) {
268 268 i = strlen(MNTOPT_PUBLIC);
269 269 /*
270 270 * Now make sure the match of "public" isn't a substring
271 271 * of another option.
272 272 */
273 273 if (((p == opts) || (*(p-1) == ',')) &&
274 274 ((p[i] == ',') || (p[i] == '\0')))
275 275 return;
276 276 }
277 277
278 278 for (i = 0; i < n; i++) {
279 279 int vers;
280 280
281 281 /*
282 282 * Skip file systems mounted using WebNFS, because mount
283 283 * protocol was not used.
284 284 */
285 285 if (strcmp(list[i].host, "nfs") == 0 && strncmp(list[i].path,
286 286 "//", 2) == 0)
287 287 continue;
288 288
↓ open down ↓ |
24 lines elided |
↑ open up ↑ |
289 289 vers = MOUNTVERS;
290 290 retry:
291 291 /*
292 292 * Use 5 sec. timeout if file system is forced unmounted,
293 293 * otherwise use default timeout to create a client handle.
294 294 * This would minimize the time to force unmount a file
295 295 * system reside on a server that is down.
296 296 */
297 297 timep = (quick ? &create_timeout : NULL);
298 298 cl = clnt_create_timed(list[i].host, MOUNTPROG, vers,
299 - "datagram_n", timep);
299 + "datagram_n", timep);
300 300 /*
301 301 * Do not print any error messages in case of forced
302 302 * unmount.
303 303 */
304 304 if (cl == NULL) {
305 305 if (!quick)
306 306 pr_err("%s:%s %s\n", list[i].host, list[i].path,
307 307 clnt_spcreateerror(
308 - "server not responding"));
308 + "server not responding"));
309 309 continue;
310 310 }
311 311 /*
312 312 * Now it is most likely that the NFS client will be able
313 313 * to contact the server since rpcbind is running on
314 314 * the server. There is still a small window in which
315 315 * server can be unreachable.
316 316 */
317 317
318 318 if (__clnt_bindresvport(cl) < 0) {
319 319 if (!quick)
320 320 pr_err(gettext(
321 321 "couldn't bind to reserved port\n"));
322 322 clnt_destroy(cl);
323 323 return;
324 324 }
325 325 if ((cl->cl_auth = authsys_create_default()) == NULL) {
326 326 if (!quick)
↓ open down ↓ |
8 lines elided |
↑ open up ↑ |
327 327 pr_err(gettext(
328 328 "couldn't create authsys structure\n"));
329 329 clnt_destroy(cl);
330 330 return;
331 331 }
332 332 timeout.tv_usec = 0;
333 333 timeout.tv_sec = 5;
334 334 clnt_control(cl, CLSET_RETRY_TIMEOUT, (char *)&timeout);
335 335 timeout.tv_sec = 25;
336 336 rpc_stat = clnt_call(cl, MOUNTPROC_UMNT, xdr_dirpath,
337 - (char *)&list[i].path, xdr_void, (char *)NULL,
338 - timeout);
337 + (char *)&list[i].path, xdr_void, (char *)NULL,
338 + timeout);
339 339 AUTH_DESTROY(cl->cl_auth);
340 340 clnt_destroy(cl);
341 341 if (rpc_stat == RPC_PROGVERSMISMATCH && vers == MOUNTVERS) {
342 342 /*
343 343 * The rare case of a v3-only server
344 344 */
345 345 vers = MOUNTVERS3;
346 346 goto retry;
347 347 }
348 348 if (rpc_stat != RPC_SUCCESS)
349 349 pr_err("%s\n", clnt_sperror(cl, "unmount"));
350 350 }
351 351
352 352 free_replica(list, n);
353 353 }
↓ open down ↓ |
5 lines elided |
↑ open up ↑ |
354 354
355 355 /*
356 356 * This function's behavior is taken from nfsstat.
357 357 * Trying to determine what NFS version was used for the mount.
358 358 */
359 359 int
360 360 is_v4_mount(struct extmnttab *mntp)
361 361 {
362 362 kstat_ctl_t *kc = NULL; /* libkstat cookie */
363 363 kstat_t *ksp;
364 - ulong_t fsid;
365 364 struct mntinfo_kstat mik;
366 365
367 366 if (mntp == NULL)
368 367 return (FALSE);
369 368
370 369 if ((kc = kstat_open()) == NULL)
371 370 return (FALSE);
372 371
373 372 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
374 373 if (ksp->ks_type != KSTAT_TYPE_RAW)
375 374 continue;
376 375 if (strcmp(ksp->ks_module, "nfs") != 0)
377 376 continue;
378 377 if (strcmp(ksp->ks_name, "mntinfo") != 0)
379 378 continue;
380 379 if (mntp->mnt_minor != ksp->ks_instance)
381 380 continue;
382 381
383 382 if (kstat_read(kc, ksp, &mik) == -1)
384 383 continue;
385 384
386 385 if (mik.mik_vers == 4)
387 386 return (TRUE);
388 387 else
389 388 return (FALSE);
390 389 }
391 390 return (FALSE);
392 391 }
↓ open down ↓ |
18 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX