Print this page
10133 smatch fixes for usr/src/cmd/fs.d
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/fs.d/smbclnt/umount/umount.c
+++ new/usr/src/cmd/fs.d/smbclnt/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
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 /*
27 + * Copyright (c) 2018, Joyent, Inc.
28 + */
29 +
30 +/*
27 31 * smbfs umount
28 32 */
29 33
30 34 #include <stdio.h>
31 35 #include <stdlib.h>
32 36 #include <string.h>
33 37 #include <stdarg.h>
34 38 #include <signal.h>
35 39 #include <unistd.h>
36 40 #include <kstat.h>
37 41 #include <rpc/rpc.h>
38 42 #include <sys/mnttab.h>
39 43 #include <sys/mount.h>
40 44 #include <sys/mntent.h>
41 45 #include <errno.h>
42 46 #include <locale.h>
43 47 #include <fslib.h>
44 48 #include <priv_utils.h>
45 49
46 50 #define RET_OK 0
47 51 #define RET_ERR 32
48 52
49 53 static void pr_err(const char *fmt, ...);
50 54 static void usage();
51 55 static int smbfs_unmount(char *, int);
52 56 static struct extmnttab *mnttab_find();
53 57
54 58 static char *myname;
55 59 static char typename[64];
56 60
57 61 int
58 62 main(int argc, char *argv[])
59 63 {
60 64 extern int optind;
61 65 int c;
62 66 int umnt_flag = 0;
63 67
64 68 (void) setlocale(LC_ALL, "");
65 69
66 70 #if !defined(TEXT_DOMAIN)
67 71 #define TEXT_DOMAIN "SYS_TEST"
68 72 #endif
69 73 (void) textdomain(TEXT_DOMAIN);
70 74
71 75 /*
72 76 * Normal users are allowed to umount smbfs mounts they own.
73 77 * To allow that, this program is installed setuid root, and
74 78 * it adds SYS_MOUNT privilege here (if needed), and then
75 79 * restores the user's normal privileges.
76 80 */
77 81 if (__init_suid_priv(0, PRIV_SYS_MOUNT, (char *)NULL) < 0) {
78 82 (void) fprintf(stderr,
79 83 gettext("Insufficient privileges, "
80 84 "%s must be set-uid root\n"), argv[0]);
81 85 exit(RET_ERR);
82 86 }
83 87
84 88 myname = strrchr(argv[0], '/');
85 89 myname = myname ? myname+1 : argv[0];
86 90 (void) sprintf(typename, "smbfs %s", myname);
87 91 argv[0] = typename;
88 92
89 93 /*
90 94 * Set options
91 95 */
92 96 while ((c = getopt(argc, argv, "f")) != EOF) {
93 97 switch (c) {
94 98 case 'f':
95 99 umnt_flag |= MS_FORCE; /* forced unmount is desired */
96 100 break;
97 101 default:
98 102 usage();
99 103 exit(RET_ERR);
100 104 }
101 105 }
102 106 if (argc - optind != 1) {
103 107 usage();
104 108 exit(RET_ERR);
105 109 }
106 110
107 111 return (smbfs_unmount(argv[optind], umnt_flag));
108 112 }
109 113
110 114 static void
111 115 pr_err(const char *fmt, ...)
112 116 {
113 117 va_list ap;
114 118
115 119 va_start(ap, fmt);
116 120 (void) fprintf(stderr, "%s: ", typename);
117 121 (void) vfprintf(stderr, fmt, ap);
118 122 (void) fflush(stderr);
119 123 va_end(ap);
120 124 }
121 125
122 126 static void
123 127 usage()
124 128 {
125 129 (void) fprintf(stderr,
126 130 gettext("Usage: smbfs umount [-o opts] {//server/share | dir}\n"));
127 131 exit(RET_ERR);
128 132 }
129 133
130 134 static int
131 135 smbfs_unmount(char *pathname, int umnt_flag)
132 136 {
133 137 struct extmnttab *mntp;
134 138 int rc;
135 139
136 140 mntp = mnttab_find(pathname);
137 141 if (mntp) {
138 142 pathname = mntp->mnt_mountp;
139 143 }
140 144
141 145 /* Need sys_mount privilege for the umount call. */
142 146 (void) __priv_bracket(PRIV_ON);
143 147 rc = umount2(pathname, umnt_flag);
144 148 (void) __priv_bracket(PRIV_OFF);
145 149
146 150 if (rc < 0) {
147 151 pr_err(gettext("%s: %s\n"), pathname, strerror(errno));
148 152 return (RET_ERR);
149 153 }
150 154
151 155 return (RET_OK);
152 156 }
153 157
154 158 /*
155 159 * Find the mnttab entry that corresponds to "name".
156 160 * We're not sure what the name represents: either
157 161 * a mountpoint name, or a special name (server:/path).
158 162 * Return the last entry in the file that matches.
159 163 */
160 164 static struct extmnttab *
161 165 mnttab_find(dirname)
162 166 char *dirname;
163 167 {
164 168 FILE *fp;
165 169 struct extmnttab mnt;
166 170 struct extmnttab *res = NULL;
167 171
168 172 fp = fopen(MNTTAB, "r");
169 173 if (fp == NULL) {
170 174 pr_err("%s: %s\n", MNTTAB, strerror(errno));
171 175 return (NULL);
↓ open down ↓ |
135 lines elided |
↑ open up ↑ |
172 176 }
173 177 while (getextmntent(fp, &mnt, sizeof (struct extmnttab)) == 0) {
174 178 if (strcmp(mnt.mnt_mountp, dirname) == 0 ||
175 179 strcmp(mnt.mnt_special, dirname) == 0) {
176 180 if (res)
177 181 fsfreemnttab(res);
178 182 res = fsdupmnttab(&mnt);
179 183 }
180 184 }
181 185
182 - fclose(fp);
186 + (void) fclose(fp);
183 187 return (res);
184 188 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX