xref: /illumos-gate/usr/src/cmd/modload/update_drv.c (revision 3ed242f5)
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 /*
22f4da9be0Scth  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <locale.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include "addrem.h"
357c478bd9Sstevel@tonic-gate #include "errmsg.h"
367c478bd9Sstevel@tonic-gate #include "plcysubr.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /* function prototypes */
397c478bd9Sstevel@tonic-gate static void	usage();
407c478bd9Sstevel@tonic-gate static int	unload_drv(char *, int, int);
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * try to modunload driver.
457c478bd9Sstevel@tonic-gate  * return -1 on failure and 0 on success
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate static int
487c478bd9Sstevel@tonic-gate unload_drv(char *driver_name, int force_flag, int verbose_flag)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	int modid;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	get_modid(driver_name, &modid);
537c478bd9Sstevel@tonic-gate 	if (modid != -1) {
547c478bd9Sstevel@tonic-gate 		if (modctl(MODUNLOAD, modid) < 0) {
557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_MODUN), driver_name);
567c478bd9Sstevel@tonic-gate 			if (force_flag == 0) { /* no force flag */
577c478bd9Sstevel@tonic-gate 				if (verbose_flag) {
587c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
597c478bd9Sstevel@tonic-gate 					    gettext(NOUPDATE), driver_name);
607c478bd9Sstevel@tonic-gate 				}
617c478bd9Sstevel@tonic-gate 				/* clean up and exit. remove lock file */
627c478bd9Sstevel@tonic-gate 				err_exit();
637c478bd9Sstevel@tonic-gate 			}
647c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(FORCE_UPDATE),
657c478bd9Sstevel@tonic-gate 			    driver_name);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 			return (-1);
687c478bd9Sstevel@tonic-gate 		}
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	return (0);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static void
767c478bd9Sstevel@tonic-gate usage()
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(UPD_DRV_USAGE));
797c478bd9Sstevel@tonic-gate 	exit(1);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate int
847c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	int	error, opt, major;
877c478bd9Sstevel@tonic-gate 	int	cleanup_flag = 0;
887c478bd9Sstevel@tonic-gate 	int	update_conf = 1;	/* reload driver.conf by default */
897c478bd9Sstevel@tonic-gate 	int	verbose_flag = 0;	/* -v option */
907c478bd9Sstevel@tonic-gate 	int	force_flag = 0;		/* -f option */
917c478bd9Sstevel@tonic-gate 	int	a_flag = 0;		/* -a option */
927c478bd9Sstevel@tonic-gate 	int	d_flag = 0;		/* -d option */
937c478bd9Sstevel@tonic-gate 	int	i_flag = 0;		/* -i option */
947c478bd9Sstevel@tonic-gate 	int	l_flag = 0;		/* -l option */
957c478bd9Sstevel@tonic-gate 	int	m_flag = 0;		/* -m option */
967c478bd9Sstevel@tonic-gate 	char	*perms = NULL;
977c478bd9Sstevel@tonic-gate 	char	*aliases = 0;
987c478bd9Sstevel@tonic-gate 	char	*basedir = NULL;
997c478bd9Sstevel@tonic-gate 	char	*policy = 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 
1187c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "m:i: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),
138*3ed242f5Sjg 				    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;
1497c478bd9Sstevel@tonic-gate 		case 'p':
1507c478bd9Sstevel@tonic-gate 			policy = optarg;
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 		case 'v':
1537c478bd9Sstevel@tonic-gate 			verbose_flag++;
1547c478bd9Sstevel@tonic-gate 			break;
1557c478bd9Sstevel@tonic-gate 		case 'P':
1567c478bd9Sstevel@tonic-gate 			priv = optarg;
1577c478bd9Sstevel@tonic-gate 			break;
1587c478bd9Sstevel@tonic-gate 		case '?' :
1597c478bd9Sstevel@tonic-gate 		default:
1607c478bd9Sstevel@tonic-gate 			usage();
1617c478bd9Sstevel@tonic-gate 		}
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	/*
1657c478bd9Sstevel@tonic-gate 	 * check for flags and extra args
1667c478bd9Sstevel@tonic-gate 	 */
1677c478bd9Sstevel@tonic-gate 	if ((argv[optind] == NULL) || (optind + 1 != argc)) {
1687c478bd9Sstevel@tonic-gate 		usage();
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 * - cannot be adding and removing at the same time
1737c478bd9Sstevel@tonic-gate 	 * - if -a or -d is specified, it's an error if none of
1747c478bd9Sstevel@tonic-gate 	 *   -i/-m/-p/-P is specified.
1757c478bd9Sstevel@tonic-gate 	 */
1767c478bd9Sstevel@tonic-gate 	if ((a_flag && d_flag) ||
1777c478bd9Sstevel@tonic-gate 	    ((a_flag || d_flag) &&
1787c478bd9Sstevel@tonic-gate 	    !m_flag && !i_flag && priv == NULL && policy == NULL)) {
1797c478bd9Sstevel@tonic-gate 		usage();
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/*
1837c478bd9Sstevel@tonic-gate 	 * - with -d option or -a option either -i 'identify_name',
1847c478bd9Sstevel@tonic-gate 	 *	-m 'permission',  -p 'policy' or -P 'priv' should be specified
1857c478bd9Sstevel@tonic-gate 	 */
1867c478bd9Sstevel@tonic-gate 	if (m_flag || i_flag || policy != NULL || priv != NULL) {
1877c478bd9Sstevel@tonic-gate 		if (!(a_flag || d_flag))
1887c478bd9Sstevel@tonic-gate 			usage();
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	driver_name = argv[optind];
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	/* set up update_drv filenames */
1947c478bd9Sstevel@tonic-gate 	if ((build_filenames(basedir)) == ERROR) {
1957c478bd9Sstevel@tonic-gate 		exit(1);
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	/* no lock is needed for listing minor perm entry */
1997c478bd9Sstevel@tonic-gate 	if (l_flag) {
2007c478bd9Sstevel@tonic-gate 		list_entry(minor_perm, driver_name, ":");
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 		return (NOERR);
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	/* must be only running version of add_drv/update_drv/rem_drv */
2067c478bd9Sstevel@tonic-gate 	enter_lock();
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if ((check_perms_aliases(m_flag, i_flag)) == ERROR) {
2097c478bd9Sstevel@tonic-gate 		err_exit();
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/* update_drv doesn't modify /etc/name_to_major file */
2137c478bd9Sstevel@tonic-gate 	if ((check_name_to_major(R_OK)) == ERROR)
2147c478bd9Sstevel@tonic-gate 		err_exit();
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if (priv != NULL && check_priv_entry(priv, a_flag) != 0)
2177c478bd9Sstevel@tonic-gate 		err_exit();
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	if (policy != NULL && (policy = check_plcy_entry(policy, driver_name,
2207c478bd9Sstevel@tonic-gate 	    d_flag ? B_TRUE : B_FALSE)) == NULL)
2217c478bd9Sstevel@tonic-gate 		err_exit();
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/*
2247c478bd9Sstevel@tonic-gate 	 * ADD: -a option
2257c478bd9Sstevel@tonic-gate 	 * i_flag: update /etc/driver_aliases
2267c478bd9Sstevel@tonic-gate 	 * m_flag: update /etc/minor_perm
2277c478bd9Sstevel@tonic-gate 	 * -p: update /etc/security/device_policy
2287c478bd9Sstevel@tonic-gate 	 * -P: update /etc/security/extra_privs
2297c478bd9Sstevel@tonic-gate 	 * if force_flag is specified continue w/ the next operation
2307c478bd9Sstevel@tonic-gate 	 */
2317c478bd9Sstevel@tonic-gate 	if (a_flag) {
2327c478bd9Sstevel@tonic-gate 		if (m_flag) {
2337c478bd9Sstevel@tonic-gate 			/* check if the permissions are valid */
2347c478bd9Sstevel@tonic-gate 			if ((error = check_perm_opts(perms)) == ERROR) {
2357c478bd9Sstevel@tonic-gate 				if (force_flag == 0) { /* no force flag */
2367c478bd9Sstevel@tonic-gate 					exit_unlock();
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 					return (error);
2397c478bd9Sstevel@tonic-gate 				}
2407c478bd9Sstevel@tonic-gate 			}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 			/*
2437c478bd9Sstevel@tonic-gate 			 * update the file, if and only if
2447c478bd9Sstevel@tonic-gate 			 * we didn't run into error earlier.
2457c478bd9Sstevel@tonic-gate 			 */
2467c478bd9Sstevel@tonic-gate 			if ((error != ERROR) &&
2477c478bd9Sstevel@tonic-gate 			    (error = update_minor_entry(driver_name, perms))) {
2487c478bd9Sstevel@tonic-gate 				if (force_flag == 0) { /* no force flag */
2497c478bd9Sstevel@tonic-gate 					exit_unlock();
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 					return (error);
2527c478bd9Sstevel@tonic-gate 				}
2537c478bd9Sstevel@tonic-gate 			}
2547c478bd9Sstevel@tonic-gate 			cleanup_flag |= CLEAN_NAM_MAJ;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 			/*
2577c478bd9Sstevel@tonic-gate 			 * Notify running system of minor perm change
2587c478bd9Sstevel@tonic-gate 			 */
2597c478bd9Sstevel@tonic-gate 			if (basedir == NULL || (strcmp(basedir, "/") == 0)) {
2607c478bd9Sstevel@tonic-gate 				rval = devfs_add_minor_perm(driver_name,
2617c478bd9Sstevel@tonic-gate 				    log_minorperm_error);
2627c478bd9Sstevel@tonic-gate 				if (rval) {
2637c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
2647c478bd9Sstevel@tonic-gate 					    gettext(ERR_UPDATE_PERM),
2657c478bd9Sstevel@tonic-gate 					    driver_name);
2667c478bd9Sstevel@tonic-gate 				}
2677c478bd9Sstevel@tonic-gate 			}
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		if (priv != NULL) {
2717c478bd9Sstevel@tonic-gate 			(void) append_to_file(driver_name, priv, extra_privs,
272*3ed242f5Sjg 			    ',', ":", 0);
2737c478bd9Sstevel@tonic-gate 			cleanup_flag |= CLEAN_DRV_PRIV;
2747c478bd9Sstevel@tonic-gate 		}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 		if (policy != NULL) {
2777c478bd9Sstevel@tonic-gate 			if ((error = update_device_policy(device_policy,
2787c478bd9Sstevel@tonic-gate 			    policy, B_TRUE)) != 0) {
2797c478bd9Sstevel@tonic-gate 				exit_unlock();
2807c478bd9Sstevel@tonic-gate 				return (error);
2817c478bd9Sstevel@tonic-gate 			}
2827c478bd9Sstevel@tonic-gate 			cleanup_flag |= CLEAN_DEV_POLICY;
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 		if (i_flag) {
2867c478bd9Sstevel@tonic-gate 			found = get_major_no(driver_name, name_to_major);
2877c478bd9Sstevel@tonic-gate 			if (found == ERROR) {
2887c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_MAX_MAJOR),
2897c478bd9Sstevel@tonic-gate 				    name_to_major);
2907c478bd9Sstevel@tonic-gate 				err_exit();
2917c478bd9Sstevel@tonic-gate 			}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 			if (found == UNIQUE) {
2947c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2957c478bd9Sstevel@tonic-gate 				    gettext(ERR_NOT_INSTALLED), driver_name);
2967c478bd9Sstevel@tonic-gate 				err_exit();
2977c478bd9Sstevel@tonic-gate 			}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 			major_num = (major_t)found;
3007c478bd9Sstevel@tonic-gate 
301f9e4eebbScth 			/* check if the alias is unique */
302f9e4eebbScth 			if ((error = aliases_unique(aliases)) == ERROR) {
303f9e4eebbScth 				exit_unlock();
304f9e4eebbScth 
305f9e4eebbScth 				return (error);
306f9e4eebbScth 			}
307f9e4eebbScth 
308f4da9be0Scth 			/*
309f4da9be0Scth 			 * unless force_flag is specified check that
310f4da9be0Scth 			 * path-oriented aliases we are adding exist
311f4da9be0Scth 			 */
312f4da9be0Scth 			if ((force_flag == 0) &&
313f4da9be0Scth 			    ((error = aliases_paths_exist(aliases)) == ERROR)) {
314f4da9be0Scth 				exit_unlock();
315f4da9be0Scth 
316f4da9be0Scth 				return (error);
317f4da9be0Scth 			}
318f4da9be0Scth 
319f9e4eebbScth 			/* update the file */
320f9e4eebbScth 			if ((error = update_driver_aliases(driver_name,
321f9e4eebbScth 			    aliases)) == ERROR) {
322f9e4eebbScth 				exit_unlock();
323f9e4eebbScth 				return (error);
324f9e4eebbScth 			}
325f9e4eebbScth 
3267c478bd9Sstevel@tonic-gate 			/* paranoia - if we crash whilst configuring */
3277c478bd9Sstevel@tonic-gate 			sync();
3287c478bd9Sstevel@tonic-gate 
329*3ed242f5Sjg 			/* optionally update the running system - not -b */
330*3ed242f5Sjg 			if (update_conf) {
331*3ed242f5Sjg 				cleanup_flag |= CLEAN_DRV_ALIAS;
332*3ed242f5Sjg 				if (config_driver(driver_name, major_num,
333*3ed242f5Sjg 				    aliases, NULL, cleanup_flag,
334*3ed242f5Sjg 				    verbose_flag) == ERROR) {
335*3ed242f5Sjg 					err_exit();
336*3ed242f5Sjg 				}
3377c478bd9Sstevel@tonic-gate 			}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 		if (update_conf && (i_flag || policy != NULL))
3417c478bd9Sstevel@tonic-gate 			/* load the driver */
3427c478bd9Sstevel@tonic-gate 			load_driver(driver_name, verbose_flag);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 		exit_unlock();
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 		return (0);
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	/*
3517c478bd9Sstevel@tonic-gate 	 * DELETE: -d option
3527c478bd9Sstevel@tonic-gate 	 * i_flag: update /etc/driver_aliases
3537c478bd9Sstevel@tonic-gate 	 * m_flag: update /etc/minor_perm
3547c478bd9Sstevel@tonic-gate 	 * -p: update /etc/security/device_policy
3557c478bd9Sstevel@tonic-gate 	 * -P: update /etc/security/extra_privs
3567c478bd9Sstevel@tonic-gate 	 */
3577c478bd9Sstevel@tonic-gate 	if (d_flag) {
3587c478bd9Sstevel@tonic-gate 		int err = NOERR;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 		if (m_flag) {
3617c478bd9Sstevel@tonic-gate 			/*
3627c478bd9Sstevel@tonic-gate 			 * On a running system, we first need to
3637c478bd9Sstevel@tonic-gate 			 * remove devfs's idea of the minor perms.
3647c478bd9Sstevel@tonic-gate 			 * We don't have any ability to do this singly
3657c478bd9Sstevel@tonic-gate 			 * at this point.
3667c478bd9Sstevel@tonic-gate 			 */
3677c478bd9Sstevel@tonic-gate 			if (basedir == NULL || (strcmp(basedir, "/") == 0)) {
3687c478bd9Sstevel@tonic-gate 				rval = devfs_rm_minor_perm(driver_name,
3697c478bd9Sstevel@tonic-gate 				    log_minorperm_error);
3707c478bd9Sstevel@tonic-gate 				if (rval) {
3717c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
3727c478bd9Sstevel@tonic-gate 					    gettext(ERR_UPDATE_PERM),
3737c478bd9Sstevel@tonic-gate 					    driver_name);
3747c478bd9Sstevel@tonic-gate 				}
3757c478bd9Sstevel@tonic-gate 			}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 			if ((error = delete_entry(minor_perm,
3787c478bd9Sstevel@tonic-gate 			    driver_name, ":", perms)) != NOERR) {
3797c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_NO_ENTRY),
3807c478bd9Sstevel@tonic-gate 				    driver_name, minor_perm);
3817c478bd9Sstevel@tonic-gate 				err = error;
3827c478bd9Sstevel@tonic-gate 			}
3837c478bd9Sstevel@tonic-gate 			/*
3847c478bd9Sstevel@tonic-gate 			 * Notify running system of new minor perm state
3857c478bd9Sstevel@tonic-gate 			 */
3867c478bd9Sstevel@tonic-gate 			if (basedir == NULL || (strcmp(basedir, "/") == 0)) {
3877c478bd9Sstevel@tonic-gate 				rval = devfs_add_minor_perm(driver_name,
3887c478bd9Sstevel@tonic-gate 				    log_minorperm_error);
3897c478bd9Sstevel@tonic-gate 				if (rval) {
3907c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
3917c478bd9Sstevel@tonic-gate 					    gettext(ERR_UPDATE_PERM),
3927c478bd9Sstevel@tonic-gate 					    driver_name);
3937c478bd9Sstevel@tonic-gate 				}
3947c478bd9Sstevel@tonic-gate 			}
3957c478bd9Sstevel@tonic-gate 		}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		if (i_flag) {
3987c478bd9Sstevel@tonic-gate 			if ((error = delete_entry(driver_aliases,
3997c478bd9Sstevel@tonic-gate 			    driver_name, ":", aliases)) != NOERR) {
4007c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_NO_ENTRY),
4017c478bd9Sstevel@tonic-gate 				    driver_name, driver_aliases);
4027c478bd9Sstevel@tonic-gate 				if (err != NOERR)
4037c478bd9Sstevel@tonic-gate 					err = error;
4047c478bd9Sstevel@tonic-gate 			}
4057c478bd9Sstevel@tonic-gate 		}
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 		if (priv != NULL) {
4087c478bd9Sstevel@tonic-gate 			if ((error = delete_entry(extra_privs, driver_name, ":",
4097c478bd9Sstevel@tonic-gate 			    priv)) != NOERR) {
4107c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_NO_ENTRY),
4117c478bd9Sstevel@tonic-gate 				    driver_name, extra_privs);
4127c478bd9Sstevel@tonic-gate 				if (err != NOERR)
4137c478bd9Sstevel@tonic-gate 					err = error;
4147c478bd9Sstevel@tonic-gate 			}
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 		if (policy != NULL) {
4187c478bd9Sstevel@tonic-gate 			if ((error = delete_plcy_entry(device_policy,
419*3ed242f5Sjg 			    policy)) != NOERR) {
4207c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_NO_ENTRY),
4217c478bd9Sstevel@tonic-gate 				    driver_name, device_policy);
4227c478bd9Sstevel@tonic-gate 				if (err != NOERR)
4237c478bd9Sstevel@tonic-gate 					err = error;
4247c478bd9Sstevel@tonic-gate 			}
4257c478bd9Sstevel@tonic-gate 		}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 		if (err == NOERR && update_conf) {
4287c478bd9Sstevel@tonic-gate 			if (i_flag || m_flag) {
4297c478bd9Sstevel@tonic-gate 				/* try to unload the driver */
4307c478bd9Sstevel@tonic-gate 				(void) unload_drv(driver_name,
4317c478bd9Sstevel@tonic-gate 				    force_flag, verbose_flag);
4327c478bd9Sstevel@tonic-gate 			}
4337c478bd9Sstevel@tonic-gate 			/* reload the policy */
4347c478bd9Sstevel@tonic-gate 			if (policy != NULL)
4357c478bd9Sstevel@tonic-gate 				load_driver(driver_name, verbose_flag);
4367c478bd9Sstevel@tonic-gate 		}
4377c478bd9Sstevel@tonic-gate 		exit_unlock();
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 		return (err);
4407c478bd9Sstevel@tonic-gate 	}
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	/* driver name must exist (for update_conf stuff) */
4437c478bd9Sstevel@tonic-gate 	major = get_major_no(driver_name, name_to_major);
4447c478bd9Sstevel@tonic-gate 	if (major == ERROR) {
4457c478bd9Sstevel@tonic-gate 		err_exit();
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/*
4497c478bd9Sstevel@tonic-gate 	 * Update driver.conf file:
4507c478bd9Sstevel@tonic-gate 	 *	First try to unload driver module. If it fails, there may
4517c478bd9Sstevel@tonic-gate 	 *	be attached devices using the old driver.conf properties,
4527c478bd9Sstevel@tonic-gate 	 *	so we cannot safely update driver.conf
4537c478bd9Sstevel@tonic-gate 	 *
4547c478bd9Sstevel@tonic-gate 	 *	The user may specify -f to force a driver.conf update.
4557c478bd9Sstevel@tonic-gate 	 *	In this case, we will update driver.conf cache. All attached
4567c478bd9Sstevel@tonic-gate 	 *	devices still reference old driver.conf properties, including
4577c478bd9Sstevel@tonic-gate 	 *	driver global properties. Devices attached in the future will
4587c478bd9Sstevel@tonic-gate 	 *	referent properties in the updated driver.conf file.
4597c478bd9Sstevel@tonic-gate 	 */
4607c478bd9Sstevel@tonic-gate 	if (update_conf) {
4617c478bd9Sstevel@tonic-gate 		(void) unload_drv(driver_name, force_flag, verbose_flag);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		if ((modctl(MODUNLOADDRVCONF, major) != 0) ||
4647c478bd9Sstevel@tonic-gate 		    (modctl(MODLOADDRVCONF, major) != 0)) {
4657c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_DRVCONF),
4667c478bd9Sstevel@tonic-gate 			    driver_name);
4677c478bd9Sstevel@tonic-gate 			err_exit();
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		if (verbose_flag) {
4717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(DRVCONF_UPDATED),
4727c478bd9Sstevel@tonic-gate 			    driver_name);
4737c478bd9Sstevel@tonic-gate 		}
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	/* rebuild /devices & /dev */
4777c478bd9Sstevel@tonic-gate 	load_driver(driver_name, verbose_flag);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	exit_unlock();
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	return (NOERR);
4827c478bd9Sstevel@tonic-gate }
483