xref: /illumos-gate/usr/src/cmd/modload/update_drv.c (revision 6532b960)
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 <locale.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include "addrem.h"
337c478bd9Sstevel@tonic-gate #include "errmsg.h"
347c478bd9Sstevel@tonic-gate #include "plcysubr.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /* function prototypes */
377c478bd9Sstevel@tonic-gate static void	usage();
387c478bd9Sstevel@tonic-gate static int	unload_drv(char *, int, int);
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * try to modunload driver.
437c478bd9Sstevel@tonic-gate  * return -1 on failure and 0 on success
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate static int
467c478bd9Sstevel@tonic-gate unload_drv(char *driver_name, int force_flag, int verbose_flag)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	int modid;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	get_modid(driver_name, &modid);
517c478bd9Sstevel@tonic-gate 	if (modid != -1) {
527c478bd9Sstevel@tonic-gate 		if (modctl(MODUNLOAD, modid) < 0) {
537c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_MODUN), driver_name);
547c478bd9Sstevel@tonic-gate 			if (force_flag == 0) { /* no force flag */
557c478bd9Sstevel@tonic-gate 				if (verbose_flag) {
567c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
577c478bd9Sstevel@tonic-gate 					    gettext(NOUPDATE), driver_name);
587c478bd9Sstevel@tonic-gate 				}
597c478bd9Sstevel@tonic-gate 				/* clean up and exit. remove lock file */
607c478bd9Sstevel@tonic-gate 				err_exit();
617c478bd9Sstevel@tonic-gate 			}
627c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(FORCE_UPDATE),
637c478bd9Sstevel@tonic-gate 			    driver_name);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 			return (-1);
667c478bd9Sstevel@tonic-gate 		}
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	return (0);
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static void
747c478bd9Sstevel@tonic-gate usage()
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(UPD_DRV_USAGE));
777c478bd9Sstevel@tonic-gate 	exit(1);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate int
827c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	int	error, opt, major;
857c478bd9Sstevel@tonic-gate 	int	cleanup_flag = 0;
867c478bd9Sstevel@tonic-gate 	int	update_conf = 1;	/* reload driver.conf by default */
877c478bd9Sstevel@tonic-gate 	int	verbose_flag = 0;	/* -v option */
887c478bd9Sstevel@tonic-gate 	int	force_flag = 0;		/* -f option */
897c478bd9Sstevel@tonic-gate 	int	a_flag = 0;		/* -a option */
907c478bd9Sstevel@tonic-gate 	int	d_flag = 0;		/* -d option */
917c478bd9Sstevel@tonic-gate 	int	i_flag = 0;		/* -i option */
927c478bd9Sstevel@tonic-gate 	int	l_flag = 0;		/* -l option */
937c478bd9Sstevel@tonic-gate 	int	m_flag = 0;		/* -m option */
947e485317SJerry Gilliam 	int	n_flag = 0;		/* -n option */
957c478bd9Sstevel@tonic-gate 	char	*perms = NULL;
960013e2d3SJerry Gilliam 	char	*aliases = NULL;
977c478bd9Sstevel@tonic-gate 	char	*basedir = NULL;
987c478bd9Sstevel@tonic-gate 	char	*policy = NULL;
990013e2d3SJerry Gilliam 	char	*aliases2 = NULL;
1007c478bd9Sstevel@tonic-gate 	char	*priv = NULL;
1017c478bd9Sstevel@tonic-gate 	char	*driver_name;
1027c478bd9Sstevel@tonic-gate 	int	found;
1037c478bd9Sstevel@tonic-gate 	major_t major_num;
1047c478bd9Sstevel@tonic-gate 	int	rval;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1077c478bd9Sstevel@tonic-gate #if	!defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1087c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1097c478bd9Sstevel@tonic-gate #endif
1107c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	/*  must be run by root */
1137c478bd9Sstevel@tonic-gate 	if (getuid() != 0) {
1147c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NOT_ROOT));
1157c478bd9Sstevel@tonic-gate 		exit(1);
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187e485317SJerry Gilliam 	while ((opt = getopt(argc, argv, "m:ni:b:p:adlfuvP:")) != EOF) {
1197c478bd9Sstevel@tonic-gate 		switch (opt) {
1207c478bd9Sstevel@tonic-gate 		case 'a':
1217c478bd9Sstevel@tonic-gate 			a_flag++;
1227c478bd9Sstevel@tonic-gate 			break;
1237c478bd9Sstevel@tonic-gate 		case 'b':
1247c478bd9Sstevel@tonic-gate 			update_conf = 0;	/* don't update .conf file */
1257c478bd9Sstevel@tonic-gate 			basedir = optarg;
1267c478bd9Sstevel@tonic-gate 			break;
1277c478bd9Sstevel@tonic-gate 		case 'd':
1287c478bd9Sstevel@tonic-gate 			d_flag++;
1297c478bd9Sstevel@tonic-gate 			break;
1307c478bd9Sstevel@tonic-gate 		case 'f':
1317c478bd9Sstevel@tonic-gate 			force_flag++;
1327c478bd9Sstevel@tonic-gate 			break;
1337c478bd9Sstevel@tonic-gate 		case 'i':
1347c478bd9Sstevel@tonic-gate 			i_flag++;
1357c478bd9Sstevel@tonic-gate 			aliases = optarg;
1367c478bd9Sstevel@tonic-gate 			if (check_space_within_quote(aliases) == ERROR) {
1377c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_NO_SPACE),
1383ed242f5Sjg 				    aliases);
1397c478bd9Sstevel@tonic-gate 				exit(1);
1407c478bd9Sstevel@tonic-gate 			}
1417c478bd9Sstevel@tonic-gate 			break;
1427c478bd9Sstevel@tonic-gate 		case 'l':	/* private option */
1437c478bd9Sstevel@tonic-gate 			l_flag++;
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 		case 'm':
1467c478bd9Sstevel@tonic-gate 			m_flag++;
1477c478bd9Sstevel@tonic-gate 			perms = optarg;
1487c478bd9Sstevel@tonic-gate 			break;
1497e485317SJerry Gilliam 		case 'n':
1507e485317SJerry Gilliam 			n_flag++;
1517e485317SJerry Gilliam 			update_conf = 0;
1527e485317SJerry Gilliam 			break;
1537c478bd9Sstevel@tonic-gate 		case 'p':
1547c478bd9Sstevel@tonic-gate 			policy = optarg;
1557c478bd9Sstevel@tonic-gate 			break;
1567c478bd9Sstevel@tonic-gate 		case 'v':
1577c478bd9Sstevel@tonic-gate 			verbose_flag++;
1587c478bd9Sstevel@tonic-gate 			break;
1597c478bd9Sstevel@tonic-gate 		case 'P':
1607c478bd9Sstevel@tonic-gate 			priv = optarg;
1617c478bd9Sstevel@tonic-gate 			break;
1627c478bd9Sstevel@tonic-gate 		case '?' :
1637c478bd9Sstevel@tonic-gate 		default:
1647c478bd9Sstevel@tonic-gate 			usage();
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	/*
1697c478bd9Sstevel@tonic-gate 	 * check for flags and extra args
1707c478bd9Sstevel@tonic-gate 	 */
1717c478bd9Sstevel@tonic-gate 	if ((argv[optind] == NULL) || (optind + 1 != argc)) {
1727c478bd9Sstevel@tonic-gate 		usage();
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/*
1767c478bd9Sstevel@tonic-gate 	 * - cannot be adding and removing at the same time
1777c478bd9Sstevel@tonic-gate 	 * - if -a or -d is specified, it's an error if none of
1787c478bd9Sstevel@tonic-gate 	 *   -i/-m/-p/-P is specified.
1797c478bd9Sstevel@tonic-gate 	 */
1807c478bd9Sstevel@tonic-gate 	if ((a_flag && d_flag) ||
1817c478bd9Sstevel@tonic-gate 	    ((a_flag || d_flag) &&
1827c478bd9Sstevel@tonic-gate 	    !m_flag && !i_flag && priv == NULL && policy == NULL)) {
1837c478bd9Sstevel@tonic-gate 		usage();
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	/*
1877c478bd9Sstevel@tonic-gate 	 * - with -d option or -a option either -i 'identify_name',
1887c478bd9Sstevel@tonic-gate 	 *	-m 'permission',  -p 'policy' or -P 'priv' should be specified
1897c478bd9Sstevel@tonic-gate 	 */
1907c478bd9Sstevel@tonic-gate 	if (m_flag || i_flag || policy != NULL || priv != NULL) {
1917c478bd9Sstevel@tonic-gate 		if (!(a_flag || d_flag))
1927c478bd9Sstevel@tonic-gate 			usage();
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	driver_name = argv[optind];
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/* set up update_drv filenames */
1987c478bd9Sstevel@tonic-gate 	if ((build_filenames(basedir)) == ERROR) {
1997c478bd9Sstevel@tonic-gate 		exit(1);
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	/* no lock is needed for listing minor perm entry */
2037c478bd9Sstevel@tonic-gate 	if (l_flag) {
2047c478bd9Sstevel@tonic-gate 		list_entry(minor_perm, driver_name, ":");
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 		return (NOERR);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/* must be only running version of add_drv/update_drv/rem_drv */
2107c478bd9Sstevel@tonic-gate 	enter_lock();
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	if ((check_perms_aliases(m_flag, i_flag)) == ERROR) {
2137c478bd9Sstevel@tonic-gate 		err_exit();
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	/* update_drv doesn't modify /etc/name_to_major file */
2177c478bd9Sstevel@tonic-gate 	if ((check_name_to_major(R_OK)) == ERROR)
2187c478bd9Sstevel@tonic-gate 		err_exit();
2197c478bd9Sstevel@tonic-gate 
2207e485317SJerry Gilliam 	if ((n_flag == 0) &&
2217e485317SJerry Gilliam 	    (basedir == NULL || (strcmp(basedir, "/") == 0)) &&
2227e485317SJerry Gilliam 	    (priv != NULL) && check_priv_entry(priv, a_flag) != 0)
2237c478bd9Sstevel@tonic-gate 		err_exit();
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	if (policy != NULL && (policy = check_plcy_entry(policy, driver_name,
2267c478bd9Sstevel@tonic-gate 	    d_flag ? B_TRUE : B_FALSE)) == NULL)
2277c478bd9Sstevel@tonic-gate 		err_exit();
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/*
2307c478bd9Sstevel@tonic-gate 	 * ADD: -a option
2317c478bd9Sstevel@tonic-gate 	 * i_flag: update /etc/driver_aliases
2327c478bd9Sstevel@tonic-gate 	 * m_flag: update /etc/minor_perm
2337c478bd9Sstevel@tonic-gate 	 * -p: update /etc/security/device_policy
2347c478bd9Sstevel@tonic-gate 	 * -P: update /etc/security/extra_privs
2357c478bd9Sstevel@tonic-gate 	 * if force_flag is specified continue w/ the next operation
2367c478bd9Sstevel@tonic-gate 	 */
2377c478bd9Sstevel@tonic-gate 	if (a_flag) {
2387c478bd9Sstevel@tonic-gate 		if (m_flag) {
2397c478bd9Sstevel@tonic-gate 			/* check if the permissions are valid */
2407c478bd9Sstevel@tonic-gate 			if ((error = check_perm_opts(perms)) == ERROR) {
2417c478bd9Sstevel@tonic-gate 				if (force_flag == 0) { /* no force flag */
2427c478bd9Sstevel@tonic-gate 					exit_unlock();
2437c478bd9Sstevel@tonic-gate 					return (error);
2447c478bd9Sstevel@tonic-gate 				}
2457c478bd9Sstevel@tonic-gate 			}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 			/*
2487c478bd9Sstevel@tonic-gate 			 * update the file, if and only if
2497c478bd9Sstevel@tonic-gate 			 * we didn't run into error earlier.
2507c478bd9Sstevel@tonic-gate 			 */
2517c478bd9Sstevel@tonic-gate 			if ((error != ERROR) &&
2527c478bd9Sstevel@tonic-gate 			    (error = update_minor_entry(driver_name, perms))) {
2537c478bd9Sstevel@tonic-gate 				if (force_flag == 0) { /* no force flag */
2547c478bd9Sstevel@tonic-gate 					exit_unlock();
2557c478bd9Sstevel@tonic-gate 					return (error);
2567c478bd9Sstevel@tonic-gate 				}
2577c478bd9Sstevel@tonic-gate 			}
2587c478bd9Sstevel@tonic-gate 			cleanup_flag |= CLEAN_NAM_MAJ;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 			/*
2617c478bd9Sstevel@tonic-gate 			 * Notify running system of minor perm change
2627c478bd9Sstevel@tonic-gate 			 */
2637e485317SJerry Gilliam 			if ((n_flag == 0) &&
2647e485317SJerry Gilliam 			    (basedir == NULL || (strcmp(basedir, "/") == 0))) {
2657c478bd9Sstevel@tonic-gate 				rval = devfs_add_minor_perm(driver_name,
2667c478bd9Sstevel@tonic-gate 				    log_minorperm_error);
2677c478bd9Sstevel@tonic-gate 				if (rval) {
2687c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
2697c478bd9Sstevel@tonic-gate 					    gettext(ERR_UPDATE_PERM),
2707c478bd9Sstevel@tonic-gate 					    driver_name);
2717c478bd9Sstevel@tonic-gate 				}
2727c478bd9Sstevel@tonic-gate 			}
2737c478bd9Sstevel@tonic-gate 		}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 		if (priv != NULL) {
2767c478bd9Sstevel@tonic-gate 			(void) append_to_file(driver_name, priv, extra_privs,
2773ed242f5Sjg 			    ',', ":", 0);
2787c478bd9Sstevel@tonic-gate 			cleanup_flag |= CLEAN_DRV_PRIV;
2797c478bd9Sstevel@tonic-gate 		}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 		if (policy != NULL) {
2827c478bd9Sstevel@tonic-gate 			if ((error = update_device_policy(device_policy,
2837c478bd9Sstevel@tonic-gate 			    policy, B_TRUE)) != 0) {
2847c478bd9Sstevel@tonic-gate 				exit_unlock();
2857c478bd9Sstevel@tonic-gate 				return (error);
2867c478bd9Sstevel@tonic-gate 			}
2877c478bd9Sstevel@tonic-gate 			cleanup_flag |= CLEAN_DEV_POLICY;
2887c478bd9Sstevel@tonic-gate 		}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		if (i_flag) {
2917c478bd9Sstevel@tonic-gate 			found = get_major_no(driver_name, name_to_major);
2927c478bd9Sstevel@tonic-gate 			if (found == ERROR) {
2937c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_MAX_MAJOR),
2947c478bd9Sstevel@tonic-gate 				    name_to_major);
2957c478bd9Sstevel@tonic-gate 				err_exit();
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 			if (found == UNIQUE) {
2997c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3007c478bd9Sstevel@tonic-gate 				    gettext(ERR_NOT_INSTALLED), driver_name);
3017c478bd9Sstevel@tonic-gate 				err_exit();
3027c478bd9Sstevel@tonic-gate 			}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 			major_num = (major_t)found;
3057c478bd9Sstevel@tonic-gate 
3060013e2d3SJerry Gilliam 			/*
3070013e2d3SJerry Gilliam 			 * To ease the nuisance of using update_drv
3080013e2d3SJerry Gilliam 			 * in packaging scripts, do not require that
3090013e2d3SJerry Gilliam 			 * existing driver aliases be trimmed from
3100013e2d3SJerry Gilliam 			 * the command line.  If an invocation asks
3110013e2d3SJerry Gilliam 			 * to add an alias and it's already there,
3120013e2d3SJerry Gilliam 			 * drive on.  We implement this by removing
3130013e2d3SJerry Gilliam 			 * duplicates now and add the remainder.
3140013e2d3SJerry Gilliam 			 */
3150013e2d3SJerry Gilliam 			error = trim_duplicate_aliases(driver_name,
3160013e2d3SJerry Gilliam 			    aliases, &aliases2);
3170013e2d3SJerry Gilliam 			if (error == ERROR) {
318f9e4eebbScth 				exit_unlock();
319f9e4eebbScth 				return (error);
320f9e4eebbScth 			}
321f9e4eebbScth 
3220013e2d3SJerry Gilliam 			/*
3230013e2d3SJerry Gilliam 			 * if the list of aliases to be added is
3240013e2d3SJerry Gilliam 			 * now empty, we're done.
3250013e2d3SJerry Gilliam 			 */
3260013e2d3SJerry Gilliam 			if (aliases2 == NULL)
3270013e2d3SJerry Gilliam 				goto done;
3280013e2d3SJerry Gilliam 
329f4da9be0Scth 			/*
330f4da9be0Scth 			 * unless force_flag is specified check that
331f4da9be0Scth 			 * path-oriented aliases we are adding exist
332f4da9be0Scth 			 */
3330013e2d3SJerry Gilliam 			if ((force_flag == 0) && ((error =
3340013e2d3SJerry Gilliam 			    aliases_paths_exist(aliases2)) == ERROR)) {
335f4da9be0Scth 				exit_unlock();
336f4da9be0Scth 				return (error);
337f4da9be0Scth 			}
338f4da9be0Scth 
339f9e4eebbScth 			/* update the file */
340f9e4eebbScth 			if ((error = update_driver_aliases(driver_name,
3410013e2d3SJerry Gilliam 			    aliases2)) == ERROR) {
342f9e4eebbScth 				exit_unlock();
343f9e4eebbScth 				return (error);
344f9e4eebbScth 			}
345f9e4eebbScth 
3467c478bd9Sstevel@tonic-gate 
3473ed242f5Sjg 			/* optionally update the running system - not -b */
3483ed242f5Sjg 			if (update_conf) {
349*6532b960SJerry Gilliam 				/* paranoia - if we crash whilst configuring */
350*6532b960SJerry Gilliam 				sync();
3513ed242f5Sjg 				cleanup_flag |= CLEAN_DRV_ALIAS;
3523ed242f5Sjg 				if (config_driver(driver_name, major_num,
3530013e2d3SJerry Gilliam 				    aliases2, NULL, cleanup_flag,
3543ed242f5Sjg 				    verbose_flag) == ERROR) {
3553ed242f5Sjg 					err_exit();
3563ed242f5Sjg 				}
3577c478bd9Sstevel@tonic-gate 			}
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 		}
3600013e2d3SJerry Gilliam 
3610013e2d3SJerry Gilliam done:
362dcedc87dSjg 		if (update_conf && (i_flag || policy != NULL)) {
3637c478bd9Sstevel@tonic-gate 			/* load the driver */
3647c478bd9Sstevel@tonic-gate 			load_driver(driver_name, verbose_flag);
365dcedc87dSjg 		}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 		exit_unlock();
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 		return (0);
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	/*
3747c478bd9Sstevel@tonic-gate 	 * DELETE: -d option
3757c478bd9Sstevel@tonic-gate 	 * i_flag: update /etc/driver_aliases
3767c478bd9Sstevel@tonic-gate 	 * m_flag: update /etc/minor_perm
3777c478bd9Sstevel@tonic-gate 	 * -p: update /etc/security/device_policy
3787c478bd9Sstevel@tonic-gate 	 * -P: update /etc/security/extra_privs
3797c478bd9Sstevel@tonic-gate 	 */
3807c478bd9Sstevel@tonic-gate 	if (d_flag) {
3817c478bd9Sstevel@tonic-gate 		int err = NOERR;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 		if (m_flag) {
3847c478bd9Sstevel@tonic-gate 			/*
3857c478bd9Sstevel@tonic-gate 			 * On a running system, we first need to
3867c478bd9Sstevel@tonic-gate 			 * remove devfs's idea of the minor perms.
3877c478bd9Sstevel@tonic-gate 			 * We don't have any ability to do this singly
3887c478bd9Sstevel@tonic-gate 			 * at this point.
3897c478bd9Sstevel@tonic-gate 			 */
3907e485317SJerry Gilliam 			if ((n_flag == 0) &&
3917e485317SJerry Gilliam 			    (basedir == NULL || (strcmp(basedir, "/") == 0))) {
3927c478bd9Sstevel@tonic-gate 				rval = devfs_rm_minor_perm(driver_name,
3937c478bd9Sstevel@tonic-gate 				    log_minorperm_error);
3947c478bd9Sstevel@tonic-gate 				if (rval) {
3957c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
3967c478bd9Sstevel@tonic-gate 					    gettext(ERR_UPDATE_PERM),
3977c478bd9Sstevel@tonic-gate 					    driver_name);
3987c478bd9Sstevel@tonic-gate 				}
3997c478bd9Sstevel@tonic-gate 			}
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 			if ((error = delete_entry(minor_perm,
4027c478bd9Sstevel@tonic-gate 			    driver_name, ":", perms)) != NOERR) {
4037c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_NO_ENTRY),
4047c478bd9Sstevel@tonic-gate 				    driver_name, minor_perm);
4057c478bd9Sstevel@tonic-gate 				err = error;
4067c478bd9Sstevel@tonic-gate 			}
4077c478bd9Sstevel@tonic-gate 			/*
4087c478bd9Sstevel@tonic-gate 			 * Notify running system of new minor perm state
4097c478bd9Sstevel@tonic-gate 			 */
4107e485317SJerry Gilliam 			if ((n_flag == 0) &&
4117e485317SJerry Gilliam 			    (basedir == NULL || (strcmp(basedir, "/") == 0))) {
4127c478bd9Sstevel@tonic-gate 				rval = devfs_add_minor_perm(driver_name,
4137c478bd9Sstevel@tonic-gate 				    log_minorperm_error);
4147c478bd9Sstevel@tonic-gate 				if (rval) {
4157c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
4167c478bd9Sstevel@tonic-gate 					    gettext(ERR_UPDATE_PERM),
4177c478bd9Sstevel@tonic-gate 					    driver_name);
4187c478bd9Sstevel@tonic-gate 				}
4197c478bd9Sstevel@tonic-gate 			}
4207c478bd9Sstevel@tonic-gate 		}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 		if (i_flag) {
423*6532b960SJerry Gilliam 			found = get_major_no(driver_name, name_to_major);
424*6532b960SJerry Gilliam 			if (found == ERROR) {
425*6532b960SJerry Gilliam 				(void) fprintf(stderr, gettext(ERR_MAX_MAJOR),
426*6532b960SJerry Gilliam 				    name_to_major);
427*6532b960SJerry Gilliam 				err_exit();
428*6532b960SJerry Gilliam 			}
429*6532b960SJerry Gilliam 
430*6532b960SJerry Gilliam 			if (found == UNIQUE) {
431*6532b960SJerry Gilliam 				(void) fprintf(stderr,
432*6532b960SJerry Gilliam 				    gettext(ERR_NOT_INSTALLED), driver_name);
433*6532b960SJerry Gilliam 				err_exit();
434*6532b960SJerry Gilliam 			}
435*6532b960SJerry Gilliam 
436*6532b960SJerry Gilliam 			major_num = (major_t)found;
437*6532b960SJerry Gilliam 
438*6532b960SJerry Gilliam 			/*
439*6532b960SJerry Gilliam 			 * verify that the aliases to be deleted exist
440*6532b960SJerry Gilliam 			 * before removal.  With -f, failing to
441*6532b960SJerry Gilliam 			 * remove an alias is not an error so we
442*6532b960SJerry Gilliam 			 * can continue on to update the kernel.
443*6532b960SJerry Gilliam 			 */
444*6532b960SJerry Gilliam 			error = NOERR;
445*6532b960SJerry Gilliam 			rval = aliases_exist(driver_name, aliases);
446*6532b960SJerry Gilliam 			if (rval == ERROR && (force_flag == 0)) {
447*6532b960SJerry Gilliam 				(void) fprintf(stderr,
448*6532b960SJerry Gilliam 				    gettext(ERR_ALIAS_NOT_BOUND),
449*6532b960SJerry Gilliam 				    driver_name);
450*6532b960SJerry Gilliam 				if (err != NOERR)
451*6532b960SJerry Gilliam 					err = rval;
452*6532b960SJerry Gilliam 			}
453*6532b960SJerry Gilliam 			if (rval == NOERR)
454*6532b960SJerry Gilliam 				error = delete_entry(driver_aliases,
455*6532b960SJerry Gilliam 				    driver_name, ":", aliases);
456*6532b960SJerry Gilliam 			if (error != NOERR && (force_flag == 0)) {
4577c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_NO_ENTRY),
4587c478bd9Sstevel@tonic-gate 				    driver_name, driver_aliases);
4597c478bd9Sstevel@tonic-gate 				if (err != NOERR)
4607c478bd9Sstevel@tonic-gate 					err = error;
4617c478bd9Sstevel@tonic-gate 			}
462*6532b960SJerry Gilliam 
463*6532b960SJerry Gilliam 			/*
464*6532b960SJerry Gilliam 			 * optionally update the running system - not -b.
465*6532b960SJerry Gilliam 			 * Unless -f is specified, error if one or more
466*6532b960SJerry Gilliam 			 * devices remain bound to the alias.
467*6532b960SJerry Gilliam 			 */
468*6532b960SJerry Gilliam 			if (err == NOERR && update_conf) {
469*6532b960SJerry Gilliam 				/* paranoia - if we crash whilst configuring */
470*6532b960SJerry Gilliam 				sync();
471*6532b960SJerry Gilliam 				error = unconfig_driver(driver_name, major_num,
472*6532b960SJerry Gilliam 				    aliases, verbose_flag, force_flag);
473*6532b960SJerry Gilliam 				if (error == ERROR && force_flag == 0) {
474*6532b960SJerry Gilliam 					(void) fprintf(stderr,
475*6532b960SJerry Gilliam 					    gettext(ERR_DEV_IN_USE),
476*6532b960SJerry Gilliam 					    driver_name);
477*6532b960SJerry Gilliam 					if (err != NOERR)
478*6532b960SJerry Gilliam 						err = error;
479*6532b960SJerry Gilliam 				}
480*6532b960SJerry Gilliam 			}
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 		if (priv != NULL) {
4847c478bd9Sstevel@tonic-gate 			if ((error = delete_entry(extra_privs, driver_name, ":",
4857c478bd9Sstevel@tonic-gate 			    priv)) != NOERR) {
4867c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_NO_ENTRY),
4877c478bd9Sstevel@tonic-gate 				    driver_name, extra_privs);
4887c478bd9Sstevel@tonic-gate 				if (err != NOERR)
4897c478bd9Sstevel@tonic-gate 					err = error;
4907c478bd9Sstevel@tonic-gate 			}
4917c478bd9Sstevel@tonic-gate 		}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 		if (policy != NULL) {
4947c478bd9Sstevel@tonic-gate 			if ((error = delete_plcy_entry(device_policy,
4953ed242f5Sjg 			    policy)) != NOERR) {
4967c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_NO_ENTRY),
4977c478bd9Sstevel@tonic-gate 				    driver_name, device_policy);
4987c478bd9Sstevel@tonic-gate 				if (err != NOERR)
4997c478bd9Sstevel@tonic-gate 					err = error;
5007c478bd9Sstevel@tonic-gate 			}
5017c478bd9Sstevel@tonic-gate 		}
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 		if (err == NOERR && update_conf) {
5047c478bd9Sstevel@tonic-gate 			if (i_flag || m_flag) {
5057c478bd9Sstevel@tonic-gate 				/* try to unload the driver */
5067c478bd9Sstevel@tonic-gate 				(void) unload_drv(driver_name,
5077c478bd9Sstevel@tonic-gate 				    force_flag, verbose_flag);
5087c478bd9Sstevel@tonic-gate 			}
5097c478bd9Sstevel@tonic-gate 			/* reload the policy */
5107c478bd9Sstevel@tonic-gate 			if (policy != NULL)
5117c478bd9Sstevel@tonic-gate 				load_driver(driver_name, verbose_flag);
5127c478bd9Sstevel@tonic-gate 		}
5137c478bd9Sstevel@tonic-gate 		exit_unlock();
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 		return (err);
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	/* driver name must exist (for update_conf stuff) */
5197c478bd9Sstevel@tonic-gate 	major = get_major_no(driver_name, name_to_major);
5207c478bd9Sstevel@tonic-gate 	if (major == ERROR) {
5217c478bd9Sstevel@tonic-gate 		err_exit();
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/*
5257c478bd9Sstevel@tonic-gate 	 * Update driver.conf file:
5267c478bd9Sstevel@tonic-gate 	 *	First try to unload driver module. If it fails, there may
5277c478bd9Sstevel@tonic-gate 	 *	be attached devices using the old driver.conf properties,
5287c478bd9Sstevel@tonic-gate 	 *	so we cannot safely update driver.conf
5297c478bd9Sstevel@tonic-gate 	 *
5307c478bd9Sstevel@tonic-gate 	 *	The user may specify -f to force a driver.conf update.
5317c478bd9Sstevel@tonic-gate 	 *	In this case, we will update driver.conf cache. All attached
5327c478bd9Sstevel@tonic-gate 	 *	devices still reference old driver.conf properties, including
5337c478bd9Sstevel@tonic-gate 	 *	driver global properties. Devices attached in the future will
5347c478bd9Sstevel@tonic-gate 	 *	referent properties in the updated driver.conf file.
5357c478bd9Sstevel@tonic-gate 	 */
5367c478bd9Sstevel@tonic-gate 	if (update_conf) {
5377c478bd9Sstevel@tonic-gate 		(void) unload_drv(driver_name, force_flag, verbose_flag);
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 		if ((modctl(MODUNLOADDRVCONF, major) != 0) ||
5407c478bd9Sstevel@tonic-gate 		    (modctl(MODLOADDRVCONF, major) != 0)) {
5417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_DRVCONF),
5427c478bd9Sstevel@tonic-gate 			    driver_name);
5437c478bd9Sstevel@tonic-gate 			err_exit();
5447c478bd9Sstevel@tonic-gate 		}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 		if (verbose_flag) {
5477c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(DRVCONF_UPDATED),
5487c478bd9Sstevel@tonic-gate 			    driver_name);
5497c478bd9Sstevel@tonic-gate 		}
550dcedc87dSjg 		load_driver(driver_name, verbose_flag);
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	exit_unlock();
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	return (NOERR);
5567c478bd9Sstevel@tonic-gate }
557