xref: /onnv-gate/usr/src/cmd/modload/rem_drv.c (revision 10585:e59699fcc4b6)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54145Scth  * Common Development and Distribution License (the "License").
64145Scth  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
228565SJerry.Gilliam@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <stdio.h>
270Sstevel@tonic-gate #include <stdlib.h>
280Sstevel@tonic-gate #include <unistd.h>
290Sstevel@tonic-gate #include <errno.h>
300Sstevel@tonic-gate #include <libintl.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <fcntl.h>
330Sstevel@tonic-gate #include <sys/buf.h>
340Sstevel@tonic-gate #include <sys/stat.h>
350Sstevel@tonic-gate #include <sys/wait.h>
360Sstevel@tonic-gate #include <limits.h>
370Sstevel@tonic-gate #include <malloc.h>
380Sstevel@tonic-gate #include <locale.h>
390Sstevel@tonic-gate #include <ftw.h>
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <sys/mkdev.h>
424870Sjg #include <sys/modctl.h>
434870Sjg #include <sys/instance.h>
444870Sjg #include <libdevinfo.h>
45*10585SDhanaraj.M@Sun.COM #include <zone.h>
464870Sjg 
470Sstevel@tonic-gate #include "addrem.h"
480Sstevel@tonic-gate #include "errmsg.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #define	FT_DEPTH	15	/* device tree depth for nftw() */
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static void usage(void);
534870Sjg static void cleanup_devfs_attributes(char *, char *);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate int
main(int argc,char * argv[])560Sstevel@tonic-gate main(int argc, char *argv[])
570Sstevel@tonic-gate {
580Sstevel@tonic-gate 	int opt;
590Sstevel@tonic-gate 	char *basedir = NULL, *driver_name = NULL;
600Sstevel@tonic-gate 	int server = 0, mod_unloaded = 0;
610Sstevel@tonic-gate 	int modid, found;
620Sstevel@tonic-gate 	char maj_num[MAX_STR_MAJOR + 1];
634870Sjg 	int cleanup = 0;
640Sstevel@tonic-gate 	int err;
658565SJerry.Gilliam@Sun.COM 	int n_flag = 0;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
680Sstevel@tonic-gate #if	!defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
690Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
700Sstevel@tonic-gate #endif
710Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	/*  must be run by root */
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	if (getuid() != 0) {
760Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NOT_ROOT));
770Sstevel@tonic-gate 		exit(1);
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 
808565SJerry.Gilliam@Sun.COM 	while ((opt = getopt(argc, argv, "b:Cn")) != -1) {
810Sstevel@tonic-gate 		switch (opt) {
820Sstevel@tonic-gate 		case 'b' :
830Sstevel@tonic-gate 			server = 1;
840Sstevel@tonic-gate 			basedir = calloc(strlen(optarg) + 1, 1);
850Sstevel@tonic-gate 			if (basedir == NULL) {
860Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_NO_MEM));
870Sstevel@tonic-gate 				exit(1);
880Sstevel@tonic-gate 			}
890Sstevel@tonic-gate 			(void) strcat(basedir, optarg);
900Sstevel@tonic-gate 			break;
914870Sjg 		case 'C':
924870Sjg 			cleanup = 1;
934870Sjg 			break;
948565SJerry.Gilliam@Sun.COM 		case 'n':
958565SJerry.Gilliam@Sun.COM 			n_flag = 1;
968565SJerry.Gilliam@Sun.COM 			break;
970Sstevel@tonic-gate 		case '?' :
980Sstevel@tonic-gate 			usage();
990Sstevel@tonic-gate 			exit(1);
1000Sstevel@tonic-gate 		}
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	if (argv[optind] != NULL) {
1040Sstevel@tonic-gate 		driver_name = calloc(strlen(argv[optind]) + 1, 1);
1050Sstevel@tonic-gate 		if (driver_name == NULL) {
1060Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_MEM));
1070Sstevel@tonic-gate 			exit(1);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 		}
1100Sstevel@tonic-gate 		(void) strcat(driver_name, argv[optind]);
1110Sstevel@tonic-gate 		/*
1120Sstevel@tonic-gate 		 * check for extra args
1130Sstevel@tonic-gate 		 */
1140Sstevel@tonic-gate 		if ((optind + 1) != argc) {
1150Sstevel@tonic-gate 			usage();
1160Sstevel@tonic-gate 			exit(1);
1170Sstevel@tonic-gate 		}
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	} else {
1200Sstevel@tonic-gate 		usage();
1210Sstevel@tonic-gate 		exit(1);
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 
124*10585SDhanaraj.M@Sun.COM 	if (getzoneid() != GLOBAL_ZONEID) {
125*10585SDhanaraj.M@Sun.COM 		(void) fprintf(stderr, gettext(ERR_NOT_GLOBAL_ZONE));
126*10585SDhanaraj.M@Sun.COM 		exit(1);
127*10585SDhanaraj.M@Sun.COM 	}
128*10585SDhanaraj.M@Sun.COM 
1290Sstevel@tonic-gate 	/* set up add_drv filenames */
1300Sstevel@tonic-gate 	if ((build_filenames(basedir)) == ERROR) {
1310Sstevel@tonic-gate 		exit(1);
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	/* must be only running version of add_drv/mod_drv/rem_drv */
1350Sstevel@tonic-gate 	enter_lock();
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	if ((check_perms_aliases(1, 1)) == ERROR)
1380Sstevel@tonic-gate 		err_exit();
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	if ((check_name_to_major(R_OK | W_OK)) == ERROR)
1410Sstevel@tonic-gate 		err_exit();
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	/* look up the major number of the driver being removed. */
1440Sstevel@tonic-gate 	if ((found = get_major_no(driver_name, name_to_major)) == ERROR) {
1450Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_MAX_MAJOR), name_to_major);
1460Sstevel@tonic-gate 		err_exit();
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 	if (found == UNIQUE) {
1490Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NOT_INSTALLED),
1500Sstevel@tonic-gate 		    driver_name);
1510Sstevel@tonic-gate 		err_exit();
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 
1548565SJerry.Gilliam@Sun.COM 	if (n_flag == 0 && !server) {
1550Sstevel@tonic-gate 		mod_unloaded = 1;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 		/* get the module id for this driver */
1580Sstevel@tonic-gate 		get_modid(driver_name, &modid);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 		/* module is installed */
1610Sstevel@tonic-gate 		if (modid != -1) {
1620Sstevel@tonic-gate 			if (modctl(MODUNLOAD, modid) < 0) {
1630Sstevel@tonic-gate 				perror(NULL);
1640Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_MODUN),
1650Sstevel@tonic-gate 				    driver_name);
1660Sstevel@tonic-gate 				mod_unloaded = 0;
1670Sstevel@tonic-gate 			}
1680Sstevel@tonic-gate 		}
1690Sstevel@tonic-gate 		/* unload driver.conf file */
1700Sstevel@tonic-gate 		if (modctl(MODUNLOADDRVCONF, (major_t)found) < 0) {
1710Sstevel@tonic-gate 			perror(NULL);
1720Sstevel@tonic-gate 			(void) fprintf(stderr,
1730Sstevel@tonic-gate 			    gettext("cannot unload %s.conf\n"), driver_name);
1740Sstevel@tonic-gate 		}
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (mod_unloaded && (modctl(MODREMMAJBIND, (major_t)found) < 0)) {
1780Sstevel@tonic-gate 		perror(NULL);
1790Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_MODREMMAJ), found);
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 	/*
1820Sstevel@tonic-gate 	 * add driver to rem_name_to_major; if this fails, don`t
1830Sstevel@tonic-gate 	 * delete from name_to_major
1840Sstevel@tonic-gate 	 */
1850Sstevel@tonic-gate 	(void) sprintf(maj_num, "%d", found);
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	if (append_to_file(driver_name, maj_num,
1884145Scth 	    rem_name_to_major, ' ', " ", 0) == ERROR) {
1890Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_UPDATE),
1900Sstevel@tonic-gate 		    rem_name_to_major);
1910Sstevel@tonic-gate 		err_exit();
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	/*
1950Sstevel@tonic-gate 	 * If removing the driver from the running system, notify
1960Sstevel@tonic-gate 	 * kernel dynamically to remove minor perm entries.
1970Sstevel@tonic-gate 	 */
1988565SJerry.Gilliam@Sun.COM 	if ((n_flag == 0) &&
1998565SJerry.Gilliam@Sun.COM 	    (basedir == NULL || (strcmp(basedir, "/") == 0))) {
2000Sstevel@tonic-gate 		err = devfs_rm_minor_perm(driver_name, log_minorperm_error);
2010Sstevel@tonic-gate 		if (err != 0) {
2020Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE_PERM),
2034870Sjg 			    driver_name, err);
2040Sstevel@tonic-gate 		}
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	/*
2080Sstevel@tonic-gate 	 * delete references to driver in add_drv/rem_drv database
2090Sstevel@tonic-gate 	 */
2100Sstevel@tonic-gate 	remove_entry(CLEAN_ALL, driver_name);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	/*
2134870Sjg 	 * Optionally clean up any dangling devfs shadow nodes for
2144870Sjg 	 * this driver so that, in the event the driver is re-added
2150Sstevel@tonic-gate 	 * to the system, newly created nodes won't incorrectly
2160Sstevel@tonic-gate 	 * pick up these stale shadow node permissions.
2170Sstevel@tonic-gate 	 */
2188565SJerry.Gilliam@Sun.COM 	if ((n_flag == 0) && cleanup) {
2194870Sjg 		if ((basedir == NULL || (strcmp(basedir, "/") == 0))) {
2204870Sjg 			err = modctl(MODREMDRVCLEANUP, driver_name, 0, NULL);
2214870Sjg 			if (err != 0) {
2224870Sjg 				(void) fprintf(stderr,
2234870Sjg 				    gettext(ERR_REMDRV_CLEANUP),
2244870Sjg 				    driver_name, err);
2254870Sjg 			}
2264870Sjg 		} else if (strcmp(basedir, "/") != 0) {
2274870Sjg 			cleanup_devfs_attributes(basedir, driver_name);
2280Sstevel@tonic-gate 		}
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	exit_unlock();
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	return (NOERR);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
2364870Sjg /*
2374870Sjg  * Optionally remove attribute nodes for a driver when
2384870Sjg  * removing drivers on a mounted root image.  Useful
2394870Sjg  * when reprovisioning a machine to return to default
2404870Sjg  * permission/ownership settings if the driver is
2414870Sjg  * re-installed.
2424870Sjg  */
2434870Sjg typedef struct cleanup_arg {
2444870Sjg 	char	*ca_basedir;
2454870Sjg 	char	*ca_drvname;
2464870Sjg } cleanup_arg_t;
2474870Sjg 
2484870Sjg 
2494870Sjg /*
2504870Sjg  * Callback to remove a minor node for a device
2514870Sjg  */
2524870Sjg /*ARGSUSED*/
2534870Sjg static int
cleanup_minor_walker(void * cb_arg,const char * minor_path)2544870Sjg cleanup_minor_walker(void *cb_arg, const char *minor_path)
2554870Sjg {
2564870Sjg 	if (unlink(minor_path) == -1) {
2579268SJerry.Gilliam@Sun.COM 		(void) fprintf(stderr, "rem_drv: error removing %s - %s\n",
2584870Sjg 		    minor_path, strerror(errno));
2594870Sjg 	}
2604870Sjg 	return (DI_WALK_CONTINUE);
2614870Sjg }
2624870Sjg 
2634870Sjg /*
2644870Sjg  * Callback for each device registered in the binding file (path_to_inst)
2654870Sjg  */
2669268SJerry.Gilliam@Sun.COM /*ARGSUSED*/
2674870Sjg static int
cleanup_device_walker(void * cb_arg,const char * inst_path,int inst_number,const char * inst_driver)2684870Sjg cleanup_device_walker(void *cb_arg, const char *inst_path,
2694870Sjg     int inst_number, const char *inst_driver)
2704870Sjg {
2714870Sjg 	char path[MAXPATHLEN];
2724870Sjg 	cleanup_arg_t *arg = (cleanup_arg_t *)cb_arg;
2734870Sjg 	int rv = DI_WALK_CONTINUE;
2744870Sjg 
2754870Sjg 	if (strcmp(inst_driver, arg->ca_drvname) == 0) {
2764870Sjg 		if (snprintf(path, MAXPATHLEN, "%s/devices%s",
2774870Sjg 		    arg->ca_basedir, inst_path) < MAXPATHLEN) {
2784870Sjg 			rv = devfs_walk_minor_nodes(path,
2794870Sjg 			    cleanup_minor_walker, NULL);
2804870Sjg 		}
2814870Sjg 	}
2824870Sjg 	return (rv);
2834870Sjg }
2844870Sjg 
2854870Sjg static void
cleanup_devfs_attributes(char * basedir,char * driver_name)2864870Sjg cleanup_devfs_attributes(char *basedir, char *driver_name)
2874870Sjg {
2884870Sjg 	cleanup_arg_t arg;
2894870Sjg 	char binding_path[MAXPATHLEN+1];
2904870Sjg 
2914870Sjg 	(void) snprintf(binding_path, MAXPATHLEN,
2924870Sjg 	    "%s%s", basedir, INSTANCE_FILE);
2934870Sjg 
2944870Sjg 	arg.ca_basedir = basedir;
2954870Sjg 	arg.ca_drvname = driver_name;
2964870Sjg 	(void) devfs_parse_binding_file(binding_path,
2974870Sjg 	    cleanup_device_walker, (void *)&arg);
2984870Sjg }
2994870Sjg 
3000Sstevel@tonic-gate static void
usage()3010Sstevel@tonic-gate usage()
3020Sstevel@tonic-gate {
3030Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(REM_USAGE1));
3040Sstevel@tonic-gate }
305