Backed out changeset e05f5705283d (wrong name)
[unleashed.git] / usr / src / cmd / modload / rem_drv.c
blob14b6d4595939b87a858d623faa28c2dc6274d2d5
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 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <libintl.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <sys/buf.h>
34 #include <sys/stat.h>
35 #include <sys/wait.h>
36 #include <limits.h>
37 #include <malloc.h>
38 #include <locale.h>
39 #include <ftw.h>
40 #include <sys/types.h>
41 #include <sys/mkdev.h>
42 #include <sys/modctl.h>
43 #include <sys/instance.h>
44 #include <libdevinfo.h>
45 #include <zone.h>
47 #include "addrem.h"
48 #include "errmsg.h"
50 #define FT_DEPTH 15 /* device tree depth for nftw() */
52 static void usage(void);
53 static void cleanup_devfs_attributes(char *, char *);
55 int
56 main(int argc, char *argv[])
58 int opt;
59 char *basedir = NULL, *driver_name = NULL;
60 int server = 0, mod_unloaded = 0;
61 int modid, found;
62 char maj_num[MAX_STR_MAJOR + 1];
63 int cleanup = 0;
64 int err;
65 int n_flag = 0;
67 (void) setlocale(LC_ALL, "");
68 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
69 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
70 #endif
71 (void) textdomain(TEXT_DOMAIN);
73 /* must be run by root */
75 if (getuid() != 0) {
76 (void) fprintf(stderr, gettext(ERR_NOT_ROOT));
77 exit(1);
80 while ((opt = getopt(argc, argv, "b:Cn")) != -1) {
81 switch (opt) {
82 case 'b' :
83 server = 1;
84 basedir = calloc(strlen(optarg) + 1, 1);
85 if (basedir == NULL) {
86 (void) fprintf(stderr, gettext(ERR_NO_MEM));
87 exit(1);
89 (void) strcat(basedir, optarg);
90 break;
91 case 'C':
92 cleanup = 1;
93 break;
94 case 'n':
95 n_flag = 1;
96 break;
97 case '?' :
98 usage();
99 exit(1);
103 if (argv[optind] != NULL) {
104 driver_name = calloc(strlen(argv[optind]) + 1, 1);
105 if (driver_name == NULL) {
106 (void) fprintf(stderr, gettext(ERR_NO_MEM));
107 exit(1);
110 (void) strcat(driver_name, argv[optind]);
112 * check for extra args
114 if ((optind + 1) != argc) {
115 usage();
116 exit(1);
119 } else {
120 usage();
121 exit(1);
124 if (getzoneid() != GLOBAL_ZONEID) {
125 (void) fprintf(stderr, gettext(ERR_NOT_GLOBAL_ZONE));
126 exit(1);
129 /* set up add_drv filenames */
130 if ((build_filenames(basedir)) == ERROR) {
131 exit(1);
134 /* must be only running version of add_drv/mod_drv/rem_drv */
135 enter_lock();
137 if ((check_perms_aliases(1, 1)) == ERROR)
138 err_exit();
140 if ((check_name_to_major(R_OK | W_OK)) == ERROR)
141 err_exit();
143 /* look up the major number of the driver being removed. */
144 if ((found = get_major_no(driver_name, name_to_major)) == ERROR) {
145 (void) fprintf(stderr, gettext(ERR_MAX_MAJOR), name_to_major);
146 err_exit();
148 if (found == UNIQUE) {
149 (void) fprintf(stderr, gettext(ERR_NOT_INSTALLED),
150 driver_name);
151 err_exit();
154 if (n_flag == 0 && !server) {
155 mod_unloaded = 1;
157 /* get the module id for this driver */
158 get_modid(driver_name, &modid);
160 /* module is installed */
161 if (modid != -1) {
162 if (modctl(MODUNLOAD, modid) < 0) {
163 perror(NULL);
164 (void) fprintf(stderr, gettext(ERR_MODUN),
165 driver_name);
166 mod_unloaded = 0;
169 /* unload driver.conf file */
170 if (modctl(MODUNLOADDRVCONF, (major_t)found) < 0) {
171 perror(NULL);
172 (void) fprintf(stderr,
173 gettext("cannot unload %s.conf\n"), driver_name);
177 if (mod_unloaded && (modctl(MODREMMAJBIND, (major_t)found) < 0)) {
178 perror(NULL);
179 (void) fprintf(stderr, gettext(ERR_MODREMMAJ), found);
182 * add driver to rem_name_to_major; if this fails, don`t
183 * delete from name_to_major
185 (void) sprintf(maj_num, "%d", found);
187 if (append_to_file(driver_name, maj_num,
188 rem_name_to_major, ' ', " ", 0) == ERROR) {
189 (void) fprintf(stderr, gettext(ERR_NO_UPDATE),
190 rem_name_to_major);
191 err_exit();
195 * If removing the driver from the running system, notify
196 * kernel dynamically to remove minor perm entries.
198 if ((n_flag == 0) &&
199 (basedir == NULL || (strcmp(basedir, "/") == 0))) {
200 err = devfs_rm_minor_perm(driver_name, log_minorperm_error);
201 if (err != 0) {
202 (void) fprintf(stderr, gettext(ERR_UPDATE_PERM),
203 driver_name, err);
208 * delete references to driver in add_drv/rem_drv database
210 remove_entry(CLEAN_ALL, driver_name);
213 * Optionally clean up any dangling devfs shadow nodes for
214 * this driver so that, in the event the driver is re-added
215 * to the system, newly created nodes won't incorrectly
216 * pick up these stale shadow node permissions.
218 if ((n_flag == 0) && cleanup) {
219 if ((basedir == NULL || (strcmp(basedir, "/") == 0))) {
220 err = modctl(MODREMDRVCLEANUP, driver_name, 0, NULL);
221 if (err != 0) {
222 (void) fprintf(stderr,
223 gettext(ERR_REMDRV_CLEANUP),
224 driver_name, err);
226 } else if (strcmp(basedir, "/") != 0) {
227 cleanup_devfs_attributes(basedir, driver_name);
231 exit_unlock();
233 return (NOERR);
237 * Optionally remove attribute nodes for a driver when
238 * removing drivers on a mounted root image. Useful
239 * when reprovisioning a machine to return to default
240 * permission/ownership settings if the driver is
241 * re-installed.
243 typedef struct cleanup_arg {
244 char *ca_basedir;
245 char *ca_drvname;
246 } cleanup_arg_t;
250 * Callback to remove a minor node for a device
252 /*ARGSUSED*/
253 static int
254 cleanup_minor_walker(void *cb_arg, const char *minor_path)
256 if (unlink(minor_path) == -1) {
257 (void) fprintf(stderr, "rem_drv: error removing %s - %s\n",
258 minor_path, strerror(errno));
260 return (DI_WALK_CONTINUE);
264 * Callback for each device registered in the binding file (path_to_inst)
266 /*ARGSUSED*/
267 static int
268 cleanup_device_walker(void *cb_arg, const char *inst_path,
269 int inst_number, const char *inst_driver)
271 char path[MAXPATHLEN];
272 cleanup_arg_t *arg = (cleanup_arg_t *)cb_arg;
273 int rv = DI_WALK_CONTINUE;
275 if (strcmp(inst_driver, arg->ca_drvname) == 0) {
276 if (snprintf(path, MAXPATHLEN, "%s/devices%s",
277 arg->ca_basedir, inst_path) < MAXPATHLEN) {
278 rv = devfs_walk_minor_nodes(path,
279 cleanup_minor_walker, NULL);
282 return (rv);
285 static void
286 cleanup_devfs_attributes(char *basedir, char *driver_name)
288 cleanup_arg_t arg;
289 char binding_path[MAXPATHLEN+1];
291 (void) snprintf(binding_path, MAXPATHLEN,
292 "%s%s", basedir, INSTANCE_FILE);
294 arg.ca_basedir = basedir;
295 arg.ca_drvname = driver_name;
296 (void) devfs_parse_binding_file(binding_path,
297 cleanup_device_walker, (void *)&arg);
300 static void
301 usage()
303 (void) fprintf(stderr, gettext(REM_USAGE1));