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