xref: /illumos-gate/usr/src/cmd/modload/rem_drv.c (revision bbf21555)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5f4da9be0Scth  * Common Development and Distribution License (the "License").
6f4da9be0Scth  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
227e485317SJerry Gilliam  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <unistd.h>
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <libintl.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <fcntl.h>
337c478bd9Sstevel@tonic-gate #include <sys/buf.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate #include <sys/wait.h>
367c478bd9Sstevel@tonic-gate #include <limits.h>
377c478bd9Sstevel@tonic-gate #include <malloc.h>
387c478bd9Sstevel@tonic-gate #include <locale.h>
397c478bd9Sstevel@tonic-gate #include <ftw.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
421aef0e11Sjg #include <sys/modctl.h>
431aef0e11Sjg #include <sys/instance.h>
441aef0e11Sjg #include <libdevinfo.h>
4517e9b2b7SDhanaraj M #include <zone.h>
461aef0e11Sjg 
477c478bd9Sstevel@tonic-gate #include "addrem.h"
487c478bd9Sstevel@tonic-gate #include "errmsg.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #define	FT_DEPTH	15	/* device tree depth for nftw() */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static void usage(void);
531aef0e11Sjg static void cleanup_devfs_attributes(char *, char *);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])567c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	int opt;
597c478bd9Sstevel@tonic-gate 	char *basedir = NULL, *driver_name = NULL;
607c478bd9Sstevel@tonic-gate 	int server = 0, mod_unloaded = 0;
617c478bd9Sstevel@tonic-gate 	int modid, found;
627c478bd9Sstevel@tonic-gate 	char maj_num[MAX_STR_MAJOR + 1];
631aef0e11Sjg 	int cleanup = 0;
647c478bd9Sstevel@tonic-gate 	int err;
657e485317SJerry Gilliam 	int n_flag = 0;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
687c478bd9Sstevel@tonic-gate #if	!defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
697c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
707c478bd9Sstevel@tonic-gate #endif
717c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	/*  must be run by root */
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	if (getuid() != 0) {
767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NOT_ROOT));
777c478bd9Sstevel@tonic-gate 		exit(1);
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 
807e485317SJerry Gilliam 	while ((opt = getopt(argc, argv, "b:Cn")) != -1) {
817c478bd9Sstevel@tonic-gate 		switch (opt) {
827c478bd9Sstevel@tonic-gate 		case 'b' :
837c478bd9Sstevel@tonic-gate 			server = 1;
847c478bd9Sstevel@tonic-gate 			basedir = calloc(strlen(optarg) + 1, 1);
857c478bd9Sstevel@tonic-gate 			if (basedir == NULL) {
867c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_NO_MEM));
877c478bd9Sstevel@tonic-gate 				exit(1);
887c478bd9Sstevel@tonic-gate 			}
897c478bd9Sstevel@tonic-gate 			(void) strcat(basedir, optarg);
907c478bd9Sstevel@tonic-gate 			break;
911aef0e11Sjg 		case 'C':
921aef0e11Sjg 			cleanup = 1;
931aef0e11Sjg 			break;
947e485317SJerry Gilliam 		case 'n':
957e485317SJerry Gilliam 			n_flag = 1;
967e485317SJerry Gilliam 			break;
977c478bd9Sstevel@tonic-gate 		case '?' :
987c478bd9Sstevel@tonic-gate 			usage();
997c478bd9Sstevel@tonic-gate 			exit(1);
1007c478bd9Sstevel@tonic-gate 		}
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	if (argv[optind] != NULL) {
1047c478bd9Sstevel@tonic-gate 		driver_name = calloc(strlen(argv[optind]) + 1, 1);
1057c478bd9Sstevel@tonic-gate 		if (driver_name == NULL) {
1067c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_MEM));
1077c478bd9Sstevel@tonic-gate 			exit(1);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 		}
1107c478bd9Sstevel@tonic-gate 		(void) strcat(driver_name, argv[optind]);
1117c478bd9Sstevel@tonic-gate 		/*
1127c478bd9Sstevel@tonic-gate 		 * check for extra args
1137c478bd9Sstevel@tonic-gate 		 */
1147c478bd9Sstevel@tonic-gate 		if ((optind + 1) != argc) {
1157c478bd9Sstevel@tonic-gate 			usage();
1167c478bd9Sstevel@tonic-gate 			exit(1);
1177c478bd9Sstevel@tonic-gate 		}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	} else {
1207c478bd9Sstevel@tonic-gate 		usage();
1217c478bd9Sstevel@tonic-gate 		exit(1);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
12417e9b2b7SDhanaraj M 	if (getzoneid() != GLOBAL_ZONEID) {
12517e9b2b7SDhanaraj M 		(void) fprintf(stderr, gettext(ERR_NOT_GLOBAL_ZONE));
12617e9b2b7SDhanaraj M 		exit(1);
12717e9b2b7SDhanaraj M 	}
12817e9b2b7SDhanaraj M 
1297c478bd9Sstevel@tonic-gate 	/* set up add_drv filenames */
1307c478bd9Sstevel@tonic-gate 	if ((build_filenames(basedir)) == ERROR) {
1317c478bd9Sstevel@tonic-gate 		exit(1);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
134*bbf21555SRichard Lowe 	/* must be only running version of add_drv/update_drv/rem_drv */
1357c478bd9Sstevel@tonic-gate 	enter_lock();
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if ((check_perms_aliases(1, 1)) == ERROR)
1387c478bd9Sstevel@tonic-gate 		err_exit();
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if ((check_name_to_major(R_OK | W_OK)) == ERROR)
1417c478bd9Sstevel@tonic-gate 		err_exit();
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	/* look up the major number of the driver being removed. */
1447c478bd9Sstevel@tonic-gate 	if ((found = get_major_no(driver_name, name_to_major)) == ERROR) {
1457c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_MAX_MAJOR), name_to_major);
1467c478bd9Sstevel@tonic-gate 		err_exit();
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 	if (found == UNIQUE) {
1497c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NOT_INSTALLED),
1507c478bd9Sstevel@tonic-gate 		    driver_name);
1517c478bd9Sstevel@tonic-gate 		err_exit();
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 
1547e485317SJerry Gilliam 	if (n_flag == 0 && !server) {
1557c478bd9Sstevel@tonic-gate 		mod_unloaded = 1;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		/* get the module id for this driver */
1587c478bd9Sstevel@tonic-gate 		get_modid(driver_name, &modid);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		/* module is installed */
1617c478bd9Sstevel@tonic-gate 		if (modid != -1) {
1627c478bd9Sstevel@tonic-gate 			if (modctl(MODUNLOAD, modid) < 0) {
1637c478bd9Sstevel@tonic-gate 				perror(NULL);
1647c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_MODUN),
1657c478bd9Sstevel@tonic-gate 				    driver_name);
1667c478bd9Sstevel@tonic-gate 				mod_unloaded = 0;
1677c478bd9Sstevel@tonic-gate 			}
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 		/* unload driver.conf file */
1707c478bd9Sstevel@tonic-gate 		if (modctl(MODUNLOADDRVCONF, (major_t)found) < 0) {
1717c478bd9Sstevel@tonic-gate 			perror(NULL);
1727c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1737c478bd9Sstevel@tonic-gate 			    gettext("cannot unload %s.conf\n"), driver_name);
1747c478bd9Sstevel@tonic-gate 		}
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (mod_unloaded && (modctl(MODREMMAJBIND, (major_t)found) < 0)) {
1787c478bd9Sstevel@tonic-gate 		perror(NULL);
1797c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_MODREMMAJ), found);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 	/*
1827c478bd9Sstevel@tonic-gate 	 * add driver to rem_name_to_major; if this fails, don`t
1837c478bd9Sstevel@tonic-gate 	 * delete from name_to_major
1847c478bd9Sstevel@tonic-gate 	 */
1857c478bd9Sstevel@tonic-gate 	(void) sprintf(maj_num, "%d", found);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if (append_to_file(driver_name, maj_num,
188f4da9be0Scth 	    rem_name_to_major, ' ', " ", 0) == ERROR) {
1897c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_UPDATE),
1907c478bd9Sstevel@tonic-gate 		    rem_name_to_major);
1917c478bd9Sstevel@tonic-gate 		err_exit();
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	/*
1957c478bd9Sstevel@tonic-gate 	 * If removing the driver from the running system, notify
1967c478bd9Sstevel@tonic-gate 	 * kernel dynamically to remove minor perm entries.
1977c478bd9Sstevel@tonic-gate 	 */
1987e485317SJerry Gilliam 	if ((n_flag == 0) &&
1997e485317SJerry Gilliam 	    (basedir == NULL || (strcmp(basedir, "/") == 0))) {
2007c478bd9Sstevel@tonic-gate 		err = devfs_rm_minor_perm(driver_name, log_minorperm_error);
2017c478bd9Sstevel@tonic-gate 		if (err != 0) {
2027c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE_PERM),
2031aef0e11Sjg 			    driver_name, err);
2047c478bd9Sstevel@tonic-gate 		}
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	/*
2087c478bd9Sstevel@tonic-gate 	 * delete references to driver in add_drv/rem_drv database
2097c478bd9Sstevel@tonic-gate 	 */
2107c478bd9Sstevel@tonic-gate 	remove_entry(CLEAN_ALL, driver_name);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/*
2131aef0e11Sjg 	 * Optionally clean up any dangling devfs shadow nodes for
2141aef0e11Sjg 	 * this driver so that, in the event the driver is re-added
2157c478bd9Sstevel@tonic-gate 	 * to the system, newly created nodes won't incorrectly
2167c478bd9Sstevel@tonic-gate 	 * pick up these stale shadow node permissions.
2177c478bd9Sstevel@tonic-gate 	 */
2187e485317SJerry Gilliam 	if ((n_flag == 0) && cleanup) {
2191aef0e11Sjg 		if ((basedir == NULL || (strcmp(basedir, "/") == 0))) {
2201aef0e11Sjg 			err = modctl(MODREMDRVCLEANUP, driver_name, 0, NULL);
2211aef0e11Sjg 			if (err != 0) {
2221aef0e11Sjg 				(void) fprintf(stderr,
2231aef0e11Sjg 				    gettext(ERR_REMDRV_CLEANUP),
2241aef0e11Sjg 				    driver_name, err);
2251aef0e11Sjg 			}
2261aef0e11Sjg 		} else if (strcmp(basedir, "/") != 0) {
2271aef0e11Sjg 			cleanup_devfs_attributes(basedir, driver_name);
2287c478bd9Sstevel@tonic-gate 		}
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	exit_unlock();
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	return (NOERR);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2361aef0e11Sjg /*
2371aef0e11Sjg  * Optionally remove attribute nodes for a driver when
2381aef0e11Sjg  * removing drivers on a mounted root image.  Useful
2391aef0e11Sjg  * when reprovisioning a machine to return to default
2401aef0e11Sjg  * permission/ownership settings if the driver is
2411aef0e11Sjg  * re-installed.
2421aef0e11Sjg  */
2431aef0e11Sjg typedef struct cleanup_arg {
2441aef0e11Sjg 	char	*ca_basedir;
2451aef0e11Sjg 	char	*ca_drvname;
2461aef0e11Sjg } cleanup_arg_t;
2471aef0e11Sjg 
2481aef0e11Sjg 
2491aef0e11Sjg /*
2501aef0e11Sjg  * Callback to remove a minor node for a device
2511aef0e11Sjg  */
2521aef0e11Sjg /*ARGSUSED*/
2531aef0e11Sjg static int
cleanup_minor_walker(void * cb_arg,const char * minor_path)2541aef0e11Sjg cleanup_minor_walker(void *cb_arg, const char *minor_path)
2551aef0e11Sjg {
2561aef0e11Sjg 	if (unlink(minor_path) == -1) {
2572e107de7SJerry Gilliam 		(void) fprintf(stderr, "rem_drv: error removing %s - %s\n",
2581aef0e11Sjg 		    minor_path, strerror(errno));
2591aef0e11Sjg 	}
2601aef0e11Sjg 	return (DI_WALK_CONTINUE);
2611aef0e11Sjg }
2621aef0e11Sjg 
2631aef0e11Sjg /*
2641aef0e11Sjg  * Callback for each device registered in the binding file (path_to_inst)
2651aef0e11Sjg  */
2662e107de7SJerry Gilliam /*ARGSUSED*/
2671aef0e11Sjg static int
cleanup_device_walker(void * cb_arg,const char * inst_path,int inst_number,const char * inst_driver)2681aef0e11Sjg cleanup_device_walker(void *cb_arg, const char *inst_path,
2691aef0e11Sjg     int inst_number, const char *inst_driver)
2701aef0e11Sjg {
2711aef0e11Sjg 	char path[MAXPATHLEN];
2721aef0e11Sjg 	cleanup_arg_t *arg = (cleanup_arg_t *)cb_arg;
2731aef0e11Sjg 	int rv = DI_WALK_CONTINUE;
2741aef0e11Sjg 
2751aef0e11Sjg 	if (strcmp(inst_driver, arg->ca_drvname) == 0) {
2761aef0e11Sjg 		if (snprintf(path, MAXPATHLEN, "%s/devices%s",
2771aef0e11Sjg 		    arg->ca_basedir, inst_path) < MAXPATHLEN) {
2781aef0e11Sjg 			rv = devfs_walk_minor_nodes(path,
2791aef0e11Sjg 			    cleanup_minor_walker, NULL);
2801aef0e11Sjg 		}
2811aef0e11Sjg 	}
2821aef0e11Sjg 	return (rv);
2831aef0e11Sjg }
2841aef0e11Sjg 
2851aef0e11Sjg static void
cleanup_devfs_attributes(char * basedir,char * driver_name)2861aef0e11Sjg cleanup_devfs_attributes(char *basedir, char *driver_name)
2871aef0e11Sjg {
2881aef0e11Sjg 	cleanup_arg_t arg;
2891aef0e11Sjg 	char binding_path[MAXPATHLEN+1];
2901aef0e11Sjg 
2911aef0e11Sjg 	(void) snprintf(binding_path, MAXPATHLEN,
2921aef0e11Sjg 	    "%s%s", basedir, INSTANCE_FILE);
2931aef0e11Sjg 
2941aef0e11Sjg 	arg.ca_basedir = basedir;
2951aef0e11Sjg 	arg.ca_drvname = driver_name;
2961aef0e11Sjg 	(void) devfs_parse_binding_file(binding_path,
2971aef0e11Sjg 	    cleanup_device_walker, (void *)&arg);
2981aef0e11Sjg }
2991aef0e11Sjg 
3007c478bd9Sstevel@tonic-gate static void
usage()3017c478bd9Sstevel@tonic-gate usage()
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(REM_USAGE1));
3047c478bd9Sstevel@tonic-gate }
305