xref: /titanic_51/usr/src/cmd/fs.d/smbclnt/umount/umount.c (revision b453864f3587ccc3324d7a3b0438a1e542dcfde7)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * smbfs umount
30  */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdarg.h>
36 #include <signal.h>
37 #include <unistd.h>
38 #include <kstat.h>
39 #include <rpc/rpc.h>
40 #include <sys/mnttab.h>
41 #include <sys/mount.h>
42 #include <sys/mntent.h>
43 #include <errno.h>
44 #include <locale.h>
45 #include <fslib.h>
46 #include <priv.h>
47 
48 #define	RET_OK	0
49 #define	RET_ERR	32
50 
51 static void pr_err(const char *fmt, ...);
52 static void usage();
53 static int smbfs_unmount(char *, int);
54 static struct extmnttab *mnttab_find();
55 
56 static char *myname;
57 static char typename[64];
58 
59 int
60 main(int argc, char *argv[])
61 {
62 	extern int optind;
63 	int c;
64 	int umnt_flag = 0;
65 
66 	(void) setlocale(LC_ALL, "");
67 
68 #if !defined(TEXT_DOMAIN)
69 #define	TEXT_DOMAIN "SYS_TEST"
70 #endif
71 	(void) textdomain(TEXT_DOMAIN);
72 
73 	myname = strrchr(argv[0], '/');
74 	myname = myname ? myname+1 : argv[0];
75 	(void) sprintf(typename, "smbfs %s", myname);
76 	argv[0] = typename;
77 
78 	/*
79 	 * Set options
80 	 */
81 	while ((c = getopt(argc, argv, "f")) != EOF) {
82 		switch (c) {
83 		case 'f':
84 			umnt_flag |= MS_FORCE; /* forced unmount is desired */
85 			break;
86 		default:
87 			usage();
88 			exit(RET_ERR);
89 		}
90 	}
91 	if (argc - optind != 1) {
92 		usage();
93 		exit(RET_ERR);
94 	}
95 
96 	return (smbfs_unmount(argv[optind], umnt_flag));
97 }
98 
99 static void
100 pr_err(const char *fmt, ...)
101 {
102 	va_list ap;
103 
104 	va_start(ap, fmt);
105 	(void) fprintf(stderr, "%s: ", typename);
106 	(void) vfprintf(stderr, fmt, ap);
107 	(void) fflush(stderr);
108 	va_end(ap);
109 }
110 
111 static void
112 usage()
113 {
114 	(void) fprintf(stderr,
115 	    gettext("Usage: smbfs umount [-o opts] {//server/share | dir}\n"));
116 	exit(RET_ERR);
117 }
118 
119 static int
120 smbfs_unmount(char *pathname, int umnt_flag)
121 {
122 	struct extmnttab *mntp;
123 
124 	mntp = mnttab_find(pathname);
125 	if (mntp) {
126 		pathname = mntp->mnt_mountp;
127 	}
128 
129 	if (umount2(pathname, umnt_flag) < 0) {
130 		pr_err(gettext("%s: %s\n"), pathname, strerror(errno));
131 		return (RET_ERR);
132 	}
133 
134 	return (RET_OK);
135 }
136 
137 /*
138  *  Find the mnttab entry that corresponds to "name".
139  *  We're not sure what the name represents: either
140  *  a mountpoint name, or a special name (server:/path).
141  *  Return the last entry in the file that matches.
142  */
143 static struct extmnttab *
144 mnttab_find(dirname)
145 	char *dirname;
146 {
147 	FILE *fp;
148 	struct extmnttab mnt;
149 	struct extmnttab *res = NULL;
150 
151 	fp = fopen(MNTTAB, "r");
152 	if (fp == NULL) {
153 		pr_err("%s: %s\n", MNTTAB, strerror(errno));
154 		return (NULL);
155 	}
156 	while (getextmntent(fp, &mnt, sizeof (struct extmnttab)) == 0) {
157 		if (strcmp(mnt.mnt_mountp, dirname) == 0 ||
158 		    strcmp(mnt.mnt_special, dirname) == 0) {
159 			if (res)
160 				fsfreemnttab(res);
161 			res = fsdupmnttab(&mnt);
162 		}
163 	}
164 
165 	fclose(fp);
166 	return (res);
167 }
168