dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / fs.d / smbclnt / umount / umount.c
blobeae1f1585577e6f36bb9394e04b4d09e6851af5a
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * smbfs umount
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <signal.h>
35 #include <unistd.h>
36 #include <kstat.h>
37 #include <rpc/rpc.h>
38 #include <sys/mnttab.h>
39 #include <sys/mount.h>
40 #include <sys/mntent.h>
41 #include <errno.h>
42 #include <locale.h>
43 #include <fslib.h>
44 #include <priv_utils.h>
46 #define RET_OK 0
47 #define RET_ERR 32
49 static void pr_err(const char *fmt, ...);
50 static void usage();
51 static int smbfs_unmount(char *, int);
52 static struct extmnttab *mnttab_find();
54 static char *myname;
55 static char typename[64];
57 int
58 main(int argc, char *argv[])
60 extern int optind;
61 int c;
62 int umnt_flag = 0;
64 (void) setlocale(LC_ALL, "");
66 #if !defined(TEXT_DOMAIN)
67 #define TEXT_DOMAIN "SYS_TEST"
68 #endif
69 (void) textdomain(TEXT_DOMAIN);
72 * Normal users are allowed to umount smbfs mounts they own.
73 * To allow that, this program is installed setuid root, and
74 * it adds SYS_MOUNT privilege here (if needed), and then
75 * restores the user's normal privileges.
77 if (__init_suid_priv(0, PRIV_SYS_MOUNT, NULL) < 0) {
78 (void) fprintf(stderr,
79 gettext("Insufficient privileges, "
80 "%s must be set-uid root\n"), argv[0]);
81 exit(RET_ERR);
84 myname = strrchr(argv[0], '/');
85 myname = myname ? myname+1 : argv[0];
86 (void) sprintf(typename, "smbfs %s", myname);
87 argv[0] = typename;
90 * Set options
92 while ((c = getopt(argc, argv, "f")) != EOF) {
93 switch (c) {
94 case 'f':
95 umnt_flag |= MS_FORCE; /* forced unmount is desired */
96 break;
97 default:
98 usage();
99 exit(RET_ERR);
102 if (argc - optind != 1) {
103 usage();
104 exit(RET_ERR);
107 return (smbfs_unmount(argv[optind], umnt_flag));
110 static void
111 pr_err(const char *fmt, ...)
113 va_list ap;
115 va_start(ap, fmt);
116 (void) fprintf(stderr, "%s: ", typename);
117 (void) vfprintf(stderr, fmt, ap);
118 (void) fflush(stderr);
119 va_end(ap);
122 static void
123 usage()
125 (void) fprintf(stderr,
126 gettext("Usage: smbfs umount [-o opts] {//server/share | dir}\n"));
127 exit(RET_ERR);
130 static int
131 smbfs_unmount(char *pathname, int umnt_flag)
133 struct extmnttab *mntp;
134 int rc;
136 mntp = mnttab_find(pathname);
137 if (mntp) {
138 pathname = mntp->mnt_mountp;
141 /* Need sys_mount privilege for the umount call. */
142 (void) __priv_bracket(PRIV_ON);
143 rc = umount2(pathname, umnt_flag);
144 (void) __priv_bracket(PRIV_OFF);
146 if (rc < 0) {
147 pr_err(gettext("%s: %s\n"), pathname, strerror(errno));
148 return (RET_ERR);
151 return (RET_OK);
155 * Find the mnttab entry that corresponds to "name".
156 * We're not sure what the name represents: either
157 * a mountpoint name, or a special name (server:/path).
158 * Return the last entry in the file that matches.
160 static struct extmnttab *
161 mnttab_find(dirname)
162 char *dirname;
164 FILE *fp;
165 struct extmnttab mnt;
166 struct extmnttab *res = NULL;
168 fp = fopen(MNTTAB, "r");
169 if (fp == NULL) {
170 pr_err("%s: %s\n", MNTTAB, strerror(errno));
171 return (NULL);
173 while (getextmntent(fp, &mnt, sizeof (struct extmnttab)) == 0) {
174 if (strcmp(mnt.mnt_mountp, dirname) == 0 ||
175 strcmp(mnt.mnt_special, dirname) == 0) {
176 if (res)
177 fsfreemnttab(res);
178 res = fsdupmnttab(&mnt);
182 fclose(fp);
183 return (res);