xref: /titanic_50/usr/src/cmd/modload/rem_drv.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <libintl.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <sys/buf.h>
37 #include <sys/stat.h>
38 #include <sys/wait.h>
39 #include <limits.h>
40 #include <malloc.h>
41 #include <locale.h>
42 #include <ftw.h>
43 #include <sys/types.h>
44 #include <sys/mkdev.h>
45 #include "addrem.h"
46 #include "errmsg.h"
47 
48 #define	FT_DEPTH	15	/* device tree depth for nftw() */
49 
50 static void usage(void);
51 
52 int
53 main(int argc, char *argv[])
54 {
55 	int opt;
56 	char *basedir = NULL, *driver_name = NULL;
57 	int server = 0, mod_unloaded = 0;
58 	int modid, found;
59 	char maj_num[MAX_STR_MAJOR + 1];
60 	int err;
61 
62 	(void) setlocale(LC_ALL, "");
63 #if	!defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
64 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
65 #endif
66 	(void) textdomain(TEXT_DOMAIN);
67 
68 	/*  must be run by root */
69 
70 	if (getuid() != 0) {
71 		(void) fprintf(stderr, gettext(ERR_NOT_ROOT));
72 		exit(1);
73 	}
74 
75 	while ((opt = getopt(argc, argv, "b:")) != -1) {
76 		switch (opt) {
77 		case 'b' :
78 			server = 1;
79 			basedir = calloc(strlen(optarg) + 1, 1);
80 			if (basedir == NULL) {
81 				(void) fprintf(stderr, gettext(ERR_NO_MEM));
82 				exit(1);
83 			}
84 			(void) strcat(basedir, optarg);
85 			break;
86 		case '?' :
87 			usage();
88 			exit(1);
89 		}
90 	}
91 
92 	if (argv[optind] != NULL) {
93 		driver_name = calloc(strlen(argv[optind]) + 1, 1);
94 		if (driver_name == NULL) {
95 			(void) fprintf(stderr, gettext(ERR_NO_MEM));
96 			exit(1);
97 
98 		}
99 		(void) strcat(driver_name, argv[optind]);
100 		/*
101 		 * check for extra args
102 		 */
103 		if ((optind + 1) != argc) {
104 			usage();
105 			exit(1);
106 		}
107 
108 	} else {
109 		usage();
110 		exit(1);
111 	}
112 
113 	/* set up add_drv filenames */
114 	if ((build_filenames(basedir)) == ERROR) {
115 		exit(1);
116 	}
117 
118 	/* must be only running version of add_drv/mod_drv/rem_drv */
119 	enter_lock();
120 
121 	if ((check_perms_aliases(1, 1)) == ERROR)
122 		err_exit();
123 
124 	if ((check_name_to_major(R_OK | W_OK)) == ERROR)
125 		err_exit();
126 
127 	/* look up the major number of the driver being removed. */
128 	if ((found = get_major_no(driver_name, name_to_major)) == ERROR) {
129 		(void) fprintf(stderr, gettext(ERR_MAX_MAJOR), name_to_major);
130 		err_exit();
131 	}
132 	if (found == UNIQUE) {
133 		(void) fprintf(stderr, gettext(ERR_NOT_INSTALLED),
134 		    driver_name);
135 		err_exit();
136 	}
137 
138 	if (!server) {
139 		mod_unloaded = 1;
140 
141 		/* get the module id for this driver */
142 		get_modid(driver_name, &modid);
143 
144 		/* module is installed */
145 		if (modid != -1) {
146 			if (modctl(MODUNLOAD, modid) < 0) {
147 				perror(NULL);
148 				(void) fprintf(stderr, gettext(ERR_MODUN),
149 				    driver_name);
150 				mod_unloaded = 0;
151 			}
152 		}
153 		/* unload driver.conf file */
154 		if (modctl(MODUNLOADDRVCONF, (major_t)found) < 0) {
155 			perror(NULL);
156 			(void) fprintf(stderr,
157 			    gettext("cannot unload %s.conf\n"), driver_name);
158 		}
159 	}
160 
161 	if (mod_unloaded && (modctl(MODREMMAJBIND, (major_t)found) < 0)) {
162 		perror(NULL);
163 		(void) fprintf(stderr, gettext(ERR_MODREMMAJ), found);
164 	}
165 	/*
166 	 * add driver to rem_name_to_major; if this fails, don`t
167 	 * delete from name_to_major
168 	 */
169 	(void) sprintf(maj_num, "%d", found);
170 
171 	if (append_to_file(driver_name, maj_num,
172 	    rem_name_to_major, ' ', " ") == ERROR) {
173 		(void) fprintf(stderr, gettext(ERR_NO_UPDATE),
174 		    rem_name_to_major);
175 		err_exit();
176 	}
177 
178 	/*
179 	 * If removing the driver from the running system, notify
180 	 * kernel dynamically to remove minor perm entries.
181 	 */
182 	if (basedir == NULL || (strcmp(basedir, "/") == 0)) {
183 		err = devfs_rm_minor_perm(driver_name, log_minorperm_error);
184 		if (err != 0) {
185 			(void) fprintf(stderr, gettext(ERR_UPDATE_PERM),
186 				driver_name, err);
187 		}
188 	}
189 
190 	/*
191 	 * delete references to driver in add_drv/rem_drv database
192 	 */
193 	remove_entry(CLEAN_ALL, driver_name);
194 
195 	/*
196 	 * Clean up any dangling devfs shadow nodes for this
197 	 * driver so that, in the event the driver is re-added
198 	 * to the system, newly created nodes won't incorrectly
199 	 * pick up these stale shadow node permissions.
200 	 */
201 	if (basedir == NULL || (strcmp(basedir, "/") == 0)) {
202 		err = modctl(MODREMDRVCLEANUP, driver_name, 0, NULL);
203 		if (err != 0) {
204 			(void) fprintf(stderr, gettext(ERR_REMDRV_CLEANUP),
205 				driver_name, err);
206 		}
207 	}
208 
209 	exit_unlock();
210 
211 	return (NOERR);
212 }
213 
214 static void
215 usage()
216 {
217 	(void) fprintf(stderr, gettext(REM_USAGE1));
218 }
219