xref: /titanic_54/usr/src/cmd/modload/drvsubr.c (revision 2e107de79998f3036decec2454002940afb9a6ff)
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
51ca93273Seota  * Common Development and Distribution License (the "License").
61ca93273Seota  * 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 /*
226532b960SJerry 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>
281ca93273Seota #include <ctype.h>
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
317c478bd9Sstevel@tonic-gate #include <libintl.h>
327c478bd9Sstevel@tonic-gate #include <wait.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
340013e2d3SJerry Gilliam #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
377c478bd9Sstevel@tonic-gate #include <signal.h>
387c478bd9Sstevel@tonic-gate #include <sys/buf.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <grp.h>
417c478bd9Sstevel@tonic-gate #include "addrem.h"
427c478bd9Sstevel@tonic-gate #include "errmsg.h"
437c478bd9Sstevel@tonic-gate #include "plcysubr.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static char *add_rem_lock;	/* lock file */
467c478bd9Sstevel@tonic-gate static char *tmphold;		/* temperary file for updating */
4749e92448Svikram static int  add_rem_lock_fd = -1;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static int get_cached_n_to_m_file(char *filename, char ***cache);
507c478bd9Sstevel@tonic-gate static int get_name_to_major_entry(int *major_no, char *driver_name,
517c478bd9Sstevel@tonic-gate     char *file_name);
527c478bd9Sstevel@tonic-gate 
531ca93273Seota static int is_blank(char *);
541ca93273Seota 
557c478bd9Sstevel@tonic-gate /*ARGSUSED*/
567c478bd9Sstevel@tonic-gate void
577c478bd9Sstevel@tonic-gate log_minorperm_error(minorperm_err_t err, int key)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	switch (err) {
607c478bd9Sstevel@tonic-gate 	case MP_FOPEN_ERR:
617c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
627c478bd9Sstevel@tonic-gate 		    MINOR_PERM_FILE);
637c478bd9Sstevel@tonic-gate 		break;
647c478bd9Sstevel@tonic-gate 	case MP_FCLOSE_ERR:
657c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_UPDATE),
667c478bd9Sstevel@tonic-gate 		    MINOR_PERM_FILE);
677c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
687c478bd9Sstevel@tonic-gate 		break;
697c478bd9Sstevel@tonic-gate 	case MP_IGNORING_LINE_ERR:
707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_UPDATE),
717c478bd9Sstevel@tonic-gate 		    MINOR_PERM_FILE);
727c478bd9Sstevel@tonic-gate 		break;
737c478bd9Sstevel@tonic-gate 	case MP_ALLOC_ERR:
747c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_UPDATE),
757c478bd9Sstevel@tonic-gate 		    MINOR_PERM_FILE);
767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
777c478bd9Sstevel@tonic-gate 		break;
787c478bd9Sstevel@tonic-gate 	case MP_NVLIST_ERR:
797c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_UPDATE),
807c478bd9Sstevel@tonic-gate 		    MINOR_PERM_FILE);
817c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
827c478bd9Sstevel@tonic-gate 		break;
837c478bd9Sstevel@tonic-gate 	case MP_CANT_FIND_USER_ERR:
847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_UPDATE),
857c478bd9Sstevel@tonic-gate 		    MINOR_PERM_FILE);
867c478bd9Sstevel@tonic-gate 		break;
877c478bd9Sstevel@tonic-gate 	case MP_CANT_FIND_GROUP_ERR:
887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_UPDATE),
897c478bd9Sstevel@tonic-gate 		    MINOR_PERM_FILE);
907c478bd9Sstevel@tonic-gate 		break;
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  *  open file
967c478bd9Sstevel@tonic-gate  * for each entry in list
977c478bd9Sstevel@tonic-gate  *	where list entries are separated by <list_separator>
987c478bd9Sstevel@tonic-gate  * 	append entry : driver_name <entry_separator> entry
997c478bd9Sstevel@tonic-gate  * close file
1007c478bd9Sstevel@tonic-gate  * return error/noerr
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate int
1037c478bd9Sstevel@tonic-gate append_to_file(
1047c478bd9Sstevel@tonic-gate 	char *driver_name,
1057c478bd9Sstevel@tonic-gate 	char *entry_list,
1067c478bd9Sstevel@tonic-gate 	char *filename,
1077c478bd9Sstevel@tonic-gate 	char list_separator,
108f4da9be0Scth 	char *entry_separator,
109f4da9be0Scth 	int quoted)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	int	i, len;
1127c478bd9Sstevel@tonic-gate 	int	fpint;
1137c478bd9Sstevel@tonic-gate 	char	*current_head, *previous_head;
1147c478bd9Sstevel@tonic-gate 	char	*line, *one_entry;
1157c478bd9Sstevel@tonic-gate 	FILE	*fp;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if ((fp = fopen(filename, "a")) == NULL) {
1187c478bd9Sstevel@tonic-gate 		perror(NULL);
1197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
1207c478bd9Sstevel@tonic-gate 		    filename);
1217c478bd9Sstevel@tonic-gate 		return (ERROR);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	len = strlen(entry_list);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	one_entry = calloc(len + 1, 1);
1277c478bd9Sstevel@tonic-gate 	if (one_entry == NULL) {
1287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_UPDATE), filename);
1297c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
1307c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
1317c478bd9Sstevel@tonic-gate 		return (ERROR);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	previous_head = entry_list;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	line = calloc(strlen(driver_name) + len + 4, 1);
1377c478bd9Sstevel@tonic-gate 	if (line == NULL) {
1387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
1397c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
1407c478bd9Sstevel@tonic-gate 		err_exit();
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	/*
1447c478bd9Sstevel@tonic-gate 	 * get one entry at a time from list and append to <filename> file
1457c478bd9Sstevel@tonic-gate 	 */
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	do {
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		for (i = 0; i <= len; i++)
1507c478bd9Sstevel@tonic-gate 			one_entry[i] = 0;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		for (i = 0; i <= (int)strlen(line); i++)
1537c478bd9Sstevel@tonic-gate 			line[i] = 0;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 		current_head = get_entry(previous_head, one_entry,
156f4da9be0Scth 		    list_separator, quoted);
1577c478bd9Sstevel@tonic-gate 		previous_head = current_head;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 		(void) strcpy(line, driver_name);
1607c478bd9Sstevel@tonic-gate 		(void) strcat(line, entry_separator);
161f4da9be0Scth 		if (quoted)
162f4da9be0Scth 			(void) strcat(line, "\"");
1637c478bd9Sstevel@tonic-gate 		(void) strcat(line, one_entry);
164f4da9be0Scth 		if (quoted)
165f4da9be0Scth 			(void) strcat(line, "\"");
1667c478bd9Sstevel@tonic-gate 		(void) strcat(line, "\n");
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 		if ((fputs(line, fp)) == EOF) {
1697c478bd9Sstevel@tonic-gate 			perror(NULL);
1707c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_UPDATE),
1717c478bd9Sstevel@tonic-gate 			    filename);
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	} while (*current_head != '\0');
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	(void) fflush(fp);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	fpint = fileno(fp);
1807c478bd9Sstevel@tonic-gate 	(void) fsync(fpint);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	free(one_entry);
1857c478bd9Sstevel@tonic-gate 	free(line);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	return (NOERR);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
19011ebc2d0SJerry Gilliam /*
19111ebc2d0SJerry Gilliam  * Require exact match to delete a driver alias/permission entry.
19211ebc2d0SJerry Gilliam  * Note line argument does not remain unchanged.  Return 1 if matched.
19311ebc2d0SJerry Gilliam  */
19411ebc2d0SJerry Gilliam static int
19511ebc2d0SJerry Gilliam match_entry(char *line, char *match)
19611ebc2d0SJerry Gilliam {
19711ebc2d0SJerry Gilliam 	char	*token, *p;
19811ebc2d0SJerry Gilliam 	int	n;
19911ebc2d0SJerry Gilliam 
20011ebc2d0SJerry Gilliam 	/* skip any leading white space */
20111ebc2d0SJerry Gilliam 	while (*line && ((*line == ' ') || (*line == '\t')))
20211ebc2d0SJerry Gilliam 		line++;
20311ebc2d0SJerry Gilliam 	/*
20411ebc2d0SJerry Gilliam 	 * Find separator for driver name, either space or colon
20511ebc2d0SJerry Gilliam 	 *	minor_perm: <driver>:<perm>
20611ebc2d0SJerry Gilliam 	 *	driver_aliases: <driver> <alias>
20711ebc2d0SJerry Gilliam 	 *	extra_privs: <driver>:<priv>
20811ebc2d0SJerry Gilliam 	 */
20911ebc2d0SJerry Gilliam 	if ((token = strpbrk(line, " :\t")) == NULL)
21011ebc2d0SJerry Gilliam 		return (0);
21111ebc2d0SJerry Gilliam 	token++;
21211ebc2d0SJerry Gilliam 	/* skip leading white space and quotes */
21311ebc2d0SJerry Gilliam 	while (*token && (*token == ' ' || *token == '\t' ||
21411ebc2d0SJerry Gilliam 	    *token == '"' || *token == '\''))
21511ebc2d0SJerry Gilliam 		token++;
21611ebc2d0SJerry Gilliam 	/* strip trailing newline, white space and quotes */
21711ebc2d0SJerry Gilliam 	n = strlen(token);
21811ebc2d0SJerry Gilliam 	p = token + n-1;
21911ebc2d0SJerry Gilliam 	while (n > 0 && (*p == '\n' || *p == ' ' || *p == '\t' ||
22011ebc2d0SJerry Gilliam 	    *p == '"' || *p == '\'')) {
22111ebc2d0SJerry Gilliam 		*p-- = 0;
22211ebc2d0SJerry Gilliam 		n--;
22311ebc2d0SJerry Gilliam 	}
22411ebc2d0SJerry Gilliam 	if (n == 0)
22511ebc2d0SJerry Gilliam 		return (0);
22611ebc2d0SJerry Gilliam 	return (strcmp(token, match) == 0);
22711ebc2d0SJerry Gilliam }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate  *  open file
2317c478bd9Sstevel@tonic-gate  * read thru file, deleting all entries if first
2327c478bd9Sstevel@tonic-gate  *    entry = driver_name
2337c478bd9Sstevel@tonic-gate  * close
2347c478bd9Sstevel@tonic-gate  * if error, leave original file intact with message
2357c478bd9Sstevel@tonic-gate  * assumption : drvconfig has been modified to work with clone
2367c478bd9Sstevel@tonic-gate  *  entries in /etc/minor_perm as driver:mummble NOT
2377c478bd9Sstevel@tonic-gate  *  clone:driver mummble
2387c478bd9Sstevel@tonic-gate  * this implementation will NOT find clone entries
2397c478bd9Sstevel@tonic-gate  * clone:driver mummble
2407c478bd9Sstevel@tonic-gate  * match:
2417c478bd9Sstevel@tonic-gate  *	delete just the matching entry
2427c478bd9Sstevel@tonic-gate  *
2437c478bd9Sstevel@tonic-gate  */
2447c478bd9Sstevel@tonic-gate int
2457c478bd9Sstevel@tonic-gate delete_entry(
2467c478bd9Sstevel@tonic-gate 	char *oldfile,
2477c478bd9Sstevel@tonic-gate 	char *driver_name,
2487c478bd9Sstevel@tonic-gate 	char *marker,
2497c478bd9Sstevel@tonic-gate 	char *match)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	int		rv, i;
2527c478bd9Sstevel@tonic-gate 	int		status = NOERR;
2537c478bd9Sstevel@tonic-gate 	int		drvr_found = 0;
2547c478bd9Sstevel@tonic-gate 	boolean_t 	nomatch = B_TRUE;
25511ebc2d0SJerry Gilliam 	char		*newfile, *tptr, *cp;
2567c478bd9Sstevel@tonic-gate 	char		line[MAX_DBFILE_ENTRY], drv[FILENAME_MAX + 1];
2577c478bd9Sstevel@tonic-gate 	FILE		*fp, *newfp;
2587c478bd9Sstevel@tonic-gate 	struct group	*sysgrp;
25911ebc2d0SJerry Gilliam 	char		*copy;		/* same size as line */
260*2e107de7SJerry Gilliam 	char		*match2 = NULL;	/* match with quotes cleaned up */
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	/*
263*2e107de7SJerry Gilliam 	 * if match is specified, sanity check it and clean it
264*2e107de7SJerry Gilliam 	 * up by removing surrounding quotes as we require
265*2e107de7SJerry Gilliam 	 * an exact match.
2667c478bd9Sstevel@tonic-gate 	 */
267*2e107de7SJerry Gilliam 	if (match) {
268*2e107de7SJerry Gilliam 		cp = match;
269*2e107de7SJerry Gilliam 		while (*cp && (*cp == '"' || *cp == '\''))
270*2e107de7SJerry Gilliam 			cp++;
271*2e107de7SJerry Gilliam 		i = strlen(cp);
272*2e107de7SJerry Gilliam 		if (i > 0) {
273*2e107de7SJerry Gilliam 			if ((match2 = strdup(cp)) == NULL) {
274*2e107de7SJerry Gilliam 				perror(NULL);
275*2e107de7SJerry Gilliam 				(void) fprintf(stderr, gettext(ERR_NO_MEM));
2767c478bd9Sstevel@tonic-gate 				return (ERROR);
2777c478bd9Sstevel@tonic-gate 			}
278*2e107de7SJerry Gilliam 			if ((cp = strchr(match2, '\'')) != NULL)
279*2e107de7SJerry Gilliam 				*cp = 0;
280*2e107de7SJerry Gilliam 			if ((cp = strchr(match2, '"')) != NULL)
281*2e107de7SJerry Gilliam 				*cp = 0;
282*2e107de7SJerry Gilliam 			if ((cp = strchr(match2, ' ')) != NULL)
283*2e107de7SJerry Gilliam 				*cp = 0;
284*2e107de7SJerry Gilliam 		}
285*2e107de7SJerry Gilliam 		if (match2 == NULL || (strlen(match2) == 0)) {
286*2e107de7SJerry Gilliam 			(void) fprintf(stderr,
287*2e107de7SJerry Gilliam 			    gettext(ERR_INT_UPDATE), oldfile);
288*2e107de7SJerry Gilliam 			return (ERROR);
289*2e107de7SJerry Gilliam 		}
290*2e107de7SJerry Gilliam 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	if ((fp = fopen(oldfile, "r")) == NULL) {
2937c478bd9Sstevel@tonic-gate 		perror(NULL);
2947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), oldfile);
2957c478bd9Sstevel@tonic-gate 		return (ERROR);
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
29811ebc2d0SJerry Gilliam 	/* Space for defensive copy of input line */
29911ebc2d0SJerry Gilliam 	copy = calloc(sizeof (line), 1);
3007c478bd9Sstevel@tonic-gate 
30111ebc2d0SJerry Gilliam 	/* Build filename for temporary file */
30211ebc2d0SJerry Gilliam 	tptr = calloc(strlen(oldfile) + strlen(XEND) + 1, 1);
30311ebc2d0SJerry Gilliam 	if (tptr == NULL || copy == NULL) {
3047c478bd9Sstevel@tonic-gate 		perror(NULL);
3057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
30611ebc2d0SJerry Gilliam 		return (ERROR);
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	(void) strcpy(tptr, oldfile);
3107c478bd9Sstevel@tonic-gate 	(void) strcat(tptr, XEND);
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	/*
3137c478bd9Sstevel@tonic-gate 	 * Set gid so we preserve group attribute.  Ideally we wouldn't
3147c478bd9Sstevel@tonic-gate 	 * assume a gid of "sys" but we can't undo the damage on already
3157c478bd9Sstevel@tonic-gate 	 * installed systems unless we force the issue.
3167c478bd9Sstevel@tonic-gate 	 */
3177c478bd9Sstevel@tonic-gate 	if ((sysgrp = getgrnam("sys")) != NULL) {
3187c478bd9Sstevel@tonic-gate 		(void) setgid(sysgrp->gr_gid);
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	newfile = mktemp(tptr);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if ((newfp = fopen(newfile, "w")) == NULL) {
3247c478bd9Sstevel@tonic-gate 		perror(NULL);
3257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
3267c478bd9Sstevel@tonic-gate 		    newfile);
3277c478bd9Sstevel@tonic-gate 		return (ERROR);
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	while ((fgets(line, sizeof (line), fp) != NULL) && status == NOERR) {
33111ebc2d0SJerry Gilliam 		/* copy the whole line */
33211ebc2d0SJerry Gilliam 		if (strlcpy(copy, line, sizeof (line)) >= sizeof (line)) {
33311ebc2d0SJerry Gilliam 			(void) fprintf(stderr, gettext(ERR_UPDATE), oldfile);
3341ca93273Seota 			status = ERROR;
3351ca93273Seota 			break;
3361ca93273Seota 		}
3371ca93273Seota 		/* cut off comments starting with '#' */
33811ebc2d0SJerry Gilliam 		if ((cp = strchr(copy, '#')) != NULL)
3391ca93273Seota 			*cp = '\0';
3401ca93273Seota 		/* ignore comment or blank lines */
34111ebc2d0SJerry Gilliam 		if (is_blank(copy)) {
3421ca93273Seota 			if (fputs(line, newfp) == EOF) {
3437c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_UPDATE),
3447c478bd9Sstevel@tonic-gate 				    oldfile);
3457c478bd9Sstevel@tonic-gate 				status = ERROR;
3467c478bd9Sstevel@tonic-gate 			}
3477c478bd9Sstevel@tonic-gate 			continue;
3487c478bd9Sstevel@tonic-gate 		}
3491ca93273Seota 
3501ca93273Seota 		/* get the driver name */
351*2e107de7SJerry Gilliam 		/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
35211ebc2d0SJerry Gilliam 		if (sscanf(copy, "%s", drv) != 1) {
3537c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_BAD_LINE),
3547c478bd9Sstevel@tonic-gate 			    oldfile, line);
3557c478bd9Sstevel@tonic-gate 			status = ERROR;
3561ca93273Seota 			break;
3577c478bd9Sstevel@tonic-gate 		}
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 		for (i = strcspn(drv, marker); i < FILENAME_MAX; i++) {
3607c478bd9Sstevel@tonic-gate 			drv[i] =  '\0';
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 		if (strcmp(driver_name, drv) != 0) {
3647c478bd9Sstevel@tonic-gate 			if ((fputs(line, newfp)) == EOF) {
3657c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_UPDATE),
3667c478bd9Sstevel@tonic-gate 				    oldfile);
3677c478bd9Sstevel@tonic-gate 				status = ERROR;
3687c478bd9Sstevel@tonic-gate 			}
3697c478bd9Sstevel@tonic-gate 		} else {
3707c478bd9Sstevel@tonic-gate 			drvr_found++;
371*2e107de7SJerry Gilliam 			if (match2) {	/* Just delete one entry */
3727c478bd9Sstevel@tonic-gate 				/* for now delete just minor_perm and aliases */
3737c478bd9Sstevel@tonic-gate 				if ((strcmp(oldfile, minor_perm) == 0) ||
3747c478bd9Sstevel@tonic-gate 				    (strcmp(oldfile, extra_privs) == 0) ||
3757c478bd9Sstevel@tonic-gate 				    (strcmp(oldfile, driver_aliases) == 0)) {
37611ebc2d0SJerry Gilliam 
37711ebc2d0SJerry Gilliam 					/* make defensive copy */
37811ebc2d0SJerry Gilliam 					if (strlcpy(copy, line, sizeof (line))
37911ebc2d0SJerry Gilliam 					    >= sizeof (line)) {
38011ebc2d0SJerry Gilliam 						(void) fprintf(stderr,
38111ebc2d0SJerry Gilliam 						    gettext(ERR_UPDATE),
38211ebc2d0SJerry Gilliam 						    oldfile);
38311ebc2d0SJerry Gilliam 						status = ERROR;
38411ebc2d0SJerry Gilliam 						break;
38511ebc2d0SJerry Gilliam 					}
386*2e107de7SJerry Gilliam 					if (match_entry(copy, match2)) {
3877c478bd9Sstevel@tonic-gate 						nomatch = B_FALSE;
3887c478bd9Sstevel@tonic-gate 					} else {
3897c478bd9Sstevel@tonic-gate 						if ((fputs(line, newfp)) ==
3907c478bd9Sstevel@tonic-gate 						    EOF) {
3917c478bd9Sstevel@tonic-gate 							(void) fprintf(stderr,
3927c478bd9Sstevel@tonic-gate 							    gettext(ERR_UPDATE),
3937c478bd9Sstevel@tonic-gate 							    oldfile);
3947c478bd9Sstevel@tonic-gate 							status = ERROR;
3957c478bd9Sstevel@tonic-gate 						}
3967c478bd9Sstevel@tonic-gate 						if (nomatch != B_FALSE)
3977c478bd9Sstevel@tonic-gate 							nomatch = B_TRUE;
3987c478bd9Sstevel@tonic-gate 					}
3997c478bd9Sstevel@tonic-gate 				}
4007c478bd9Sstevel@tonic-gate 			}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 		} /* end of else */
4037c478bd9Sstevel@tonic-gate 	} /* end of while */
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
40611ebc2d0SJerry Gilliam 	free(tptr);
40711ebc2d0SJerry Gilliam 	free(copy);
408*2e107de7SJerry Gilliam 	if (match2)
409*2e107de7SJerry Gilliam 		free(match2);
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	/* Make sure that the file is on disk */
4127c478bd9Sstevel@tonic-gate 	if (fflush(newfp) != 0 || fsync(fileno(newfp)) != 0)
4137c478bd9Sstevel@tonic-gate 		status = ERROR;
4147c478bd9Sstevel@tonic-gate 	else
4157c478bd9Sstevel@tonic-gate 		rv = NOERR;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	(void) fclose(newfp);
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	/* no matching driver found */
4207c478bd9Sstevel@tonic-gate 	rv = NOERR;
4217c478bd9Sstevel@tonic-gate 	if (!drvr_found ||
4227c478bd9Sstevel@tonic-gate 	    (nomatch == B_TRUE)) {
4237c478bd9Sstevel@tonic-gate 		rv = NONE_FOUND;
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * if error, leave original file, delete new file
4287c478bd9Sstevel@tonic-gate 	 * if noerr, replace original file with new file
4297c478bd9Sstevel@tonic-gate 	 */
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	if (status == NOERR) {
4327c478bd9Sstevel@tonic-gate 		if (rename(oldfile, tmphold) == -1) {
4337c478bd9Sstevel@tonic-gate 			perror(NULL);
4347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE), oldfile);
4357c478bd9Sstevel@tonic-gate 			(void) unlink(newfile);
4367c478bd9Sstevel@tonic-gate 			return (ERROR);
4377c478bd9Sstevel@tonic-gate 		} else if (rename(newfile, oldfile) == -1) {
4387c478bd9Sstevel@tonic-gate 			perror(NULL);
4397c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE), oldfile);
4407c478bd9Sstevel@tonic-gate 			(void) unlink(oldfile);
4417c478bd9Sstevel@tonic-gate 			(void) unlink(newfile);
4427c478bd9Sstevel@tonic-gate 			if (link(tmphold, oldfile) == -1) {
4437c478bd9Sstevel@tonic-gate 				perror(NULL);
4447c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_BAD_LINK),
4457c478bd9Sstevel@tonic-gate 				    oldfile, tmphold);
4467c478bd9Sstevel@tonic-gate 			}
4477c478bd9Sstevel@tonic-gate 			return (ERROR);
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 		(void) unlink(tmphold);
4507c478bd9Sstevel@tonic-gate 	} else {
4517c478bd9Sstevel@tonic-gate 		/*
4527c478bd9Sstevel@tonic-gate 		 * since there's an error, leave file alone; remove
4537c478bd9Sstevel@tonic-gate 		 * new file
4547c478bd9Sstevel@tonic-gate 		 */
4557c478bd9Sstevel@tonic-gate 		if (unlink(newfile) == -1) {
4567c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_CANT_RM), newfile);
4577c478bd9Sstevel@tonic-gate 		}
4587c478bd9Sstevel@tonic-gate 		return (ERROR);
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	return (rv);
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate /*
4667c478bd9Sstevel@tonic-gate  * wrapper for call to get_name_to_major_entry(): given driver name,
4677c478bd9Sstevel@tonic-gate  * retrieve major number.
4687c478bd9Sstevel@tonic-gate  */
4697c478bd9Sstevel@tonic-gate int
4707c478bd9Sstevel@tonic-gate get_major_no(char *driver_name, char *file_name)
4717c478bd9Sstevel@tonic-gate {
4727c478bd9Sstevel@tonic-gate 	int major = UNIQUE;
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	if (get_name_to_major_entry(&major, driver_name, file_name) == ERROR)
4757c478bd9Sstevel@tonic-gate 		return (ERROR);
4767c478bd9Sstevel@tonic-gate 	else
4777c478bd9Sstevel@tonic-gate 		return (major);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate /*
4817c478bd9Sstevel@tonic-gate  * wrapper for call to get_name_to_major_entry(): given major number,
4827c478bd9Sstevel@tonic-gate  * retrieve driver name.
4837c478bd9Sstevel@tonic-gate  */
4847c478bd9Sstevel@tonic-gate int
4857c478bd9Sstevel@tonic-gate get_driver_name(int major, char *file_name, char *buf)
4867c478bd9Sstevel@tonic-gate {
4877c478bd9Sstevel@tonic-gate 	if (major < 0)
4887c478bd9Sstevel@tonic-gate 		return (ERROR);
4897c478bd9Sstevel@tonic-gate 	return (get_name_to_major_entry(&major, buf, file_name));
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate /*
4947c478bd9Sstevel@tonic-gate  * return pointer to cached name_to_major file - reads file into
4957c478bd9Sstevel@tonic-gate  * cache if this has not already been done.  Since there may be
4967c478bd9Sstevel@tonic-gate  * requests for multiple name_to_major files (rem_name_to_major,
4977c478bd9Sstevel@tonic-gate  * name_to_major), this routine keeps a list of cached files.
4987c478bd9Sstevel@tonic-gate  */
4997c478bd9Sstevel@tonic-gate static int
5007c478bd9Sstevel@tonic-gate get_cached_n_to_m_file(char *filename, char ***cache)
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate 	struct n_to_m_cache {
5037c478bd9Sstevel@tonic-gate 		char *file;
5047c478bd9Sstevel@tonic-gate 		char **cached_file;
5057c478bd9Sstevel@tonic-gate 		int size;
5067c478bd9Sstevel@tonic-gate 		struct n_to_m_cache *next;
5077c478bd9Sstevel@tonic-gate 	};
5087c478bd9Sstevel@tonic-gate 	static struct n_to_m_cache *head = NULL;
5097c478bd9Sstevel@tonic-gate 	struct n_to_m_cache *ptr;
5107c478bd9Sstevel@tonic-gate 	FILE *fp;
5117c478bd9Sstevel@tonic-gate 	char drv[FILENAME_MAX + 1];
5127c478bd9Sstevel@tonic-gate 	char entry[FILENAME_MAX + 1];
5131ca93273Seota 	char line[MAX_N2M_ALIAS_LINE], *cp;
5147c478bd9Sstevel@tonic-gate 	int maj;
5157c478bd9Sstevel@tonic-gate 	int size = 0;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	/*
5197c478bd9Sstevel@tonic-gate 	 * see if the file is already cached - either
5207c478bd9Sstevel@tonic-gate 	 * rem_name_to_major or name_to_major
5217c478bd9Sstevel@tonic-gate 	 */
5227c478bd9Sstevel@tonic-gate 	ptr = head;
5237c478bd9Sstevel@tonic-gate 	while (ptr != NULL) {
5247c478bd9Sstevel@tonic-gate 		if (strcmp(ptr->file, filename) == 0)
5257c478bd9Sstevel@tonic-gate 			break;
5267c478bd9Sstevel@tonic-gate 		ptr = ptr->next;
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {	/* we need to cache the contents */
5307c478bd9Sstevel@tonic-gate 		if ((fp = fopen(filename, "r")) == NULL) {
5317c478bd9Sstevel@tonic-gate 			perror(NULL);
5327c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_CANT_OPEN),
5337c478bd9Sstevel@tonic-gate 			    filename);
5347c478bd9Sstevel@tonic-gate 			return (ERROR);
5357c478bd9Sstevel@tonic-gate 		}
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 		while (fgets(line, sizeof (line), fp) != NULL) {
5381ca93273Seota 			/* cut off comments starting with '#' */
5391ca93273Seota 			if ((cp = strchr(line, '#')) != NULL)
5401ca93273Seota 				*cp = '\0';
5411ca93273Seota 			/* ignore comment or blank lines */
5421ca93273Seota 			if (is_blank(line))
5431ca93273Seota 				continue;
5441ca93273Seota 			/* sanity-check */
545*2e107de7SJerry Gilliam 			/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
5467c478bd9Sstevel@tonic-gate 			if (sscanf(line, "%s%s", drv, entry) != 2) {
5477c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_BAD_LINE),
5487c478bd9Sstevel@tonic-gate 				    filename, line);
5497c478bd9Sstevel@tonic-gate 				continue;
5507c478bd9Sstevel@tonic-gate 			}
5517c478bd9Sstevel@tonic-gate 			maj = atoi(entry);
5527c478bd9Sstevel@tonic-gate 			if (maj > size)
5537c478bd9Sstevel@tonic-gate 				size = maj;
5547c478bd9Sstevel@tonic-gate 		}
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 		/* allocate struct to cache the file */
5577c478bd9Sstevel@tonic-gate 		ptr = (struct n_to_m_cache *)calloc(1,
5587c478bd9Sstevel@tonic-gate 		    sizeof (struct n_to_m_cache));
5597c478bd9Sstevel@tonic-gate 		if (ptr == NULL) {
5607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_MEM));
5617c478bd9Sstevel@tonic-gate 			return (ERROR);
5627c478bd9Sstevel@tonic-gate 		}
5637c478bd9Sstevel@tonic-gate 		ptr->size = size + 1;
5647c478bd9Sstevel@tonic-gate 		/* allocate space to cache contents of file */
5657c478bd9Sstevel@tonic-gate 		ptr->cached_file = (char **)calloc(ptr->size, sizeof (char *));
5667c478bd9Sstevel@tonic-gate 		if (ptr->cached_file == NULL) {
5677c478bd9Sstevel@tonic-gate 			free(ptr);
5687c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_MEM));
5697c478bd9Sstevel@tonic-gate 			return (ERROR);
5707c478bd9Sstevel@tonic-gate 		}
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 		rewind(fp);
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		/*
5757c478bd9Sstevel@tonic-gate 		 * now fill the cache
5767c478bd9Sstevel@tonic-gate 		 * the cache is an array of char pointers indexed by major
5777c478bd9Sstevel@tonic-gate 		 * number
5787c478bd9Sstevel@tonic-gate 		 */
5797c478bd9Sstevel@tonic-gate 		while (fgets(line, sizeof (line), fp) != NULL) {
5801ca93273Seota 			/* cut off comments starting with '#' */
5811ca93273Seota 			if ((cp = strchr(line, '#')) != NULL)
5821ca93273Seota 				*cp = '\0';
5831ca93273Seota 			/* ignore comment or blank lines */
5841ca93273Seota 			if (is_blank(line))
5851ca93273Seota 				continue;
5861ca93273Seota 			/* sanity-check */
587*2e107de7SJerry Gilliam 			/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
5887c478bd9Sstevel@tonic-gate 			if (sscanf(line, "%s%s", drv, entry) != 2) {
5897c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_BAD_LINE),
5907c478bd9Sstevel@tonic-gate 				    filename, line);
5917c478bd9Sstevel@tonic-gate 				continue;
5927c478bd9Sstevel@tonic-gate 			}
5937c478bd9Sstevel@tonic-gate 			maj = atoi(entry);
5947c478bd9Sstevel@tonic-gate 			if ((ptr->cached_file[maj] = strdup(drv)) == NULL) {
5957c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_NO_MEM));
5967c478bd9Sstevel@tonic-gate 				free(ptr->cached_file);
5977c478bd9Sstevel@tonic-gate 				free(ptr);
5987c478bd9Sstevel@tonic-gate 				return (ERROR);
5997c478bd9Sstevel@tonic-gate 			}
6007c478bd9Sstevel@tonic-gate 			(void) strcpy(ptr->cached_file[maj], drv);
6017c478bd9Sstevel@tonic-gate 		}
6027c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
6037c478bd9Sstevel@tonic-gate 		/* link the cache struct into the list of cached files */
6047c478bd9Sstevel@tonic-gate 		ptr->file = strdup(filename);
6057c478bd9Sstevel@tonic-gate 		if (ptr->file == NULL) {
6067c478bd9Sstevel@tonic-gate 			for (maj = 0; maj <= ptr->size; maj++)
6077c478bd9Sstevel@tonic-gate 				free(ptr->cached_file[maj]);
6087c478bd9Sstevel@tonic-gate 			free(ptr->cached_file);
6097c478bd9Sstevel@tonic-gate 			free(ptr);
6107c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_MEM));
6117c478bd9Sstevel@tonic-gate 			return (ERROR);
6127c478bd9Sstevel@tonic-gate 		}
6137c478bd9Sstevel@tonic-gate 		ptr->next = head;
6147c478bd9Sstevel@tonic-gate 		head = ptr;
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 	/* return value pointer to contents of file */
6177c478bd9Sstevel@tonic-gate 	*cache = ptr->cached_file;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	/* return size */
6207c478bd9Sstevel@tonic-gate 	return (ptr->size);
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate  * Using get_cached_n_to_m_file(), retrieve maximum major number
6267c478bd9Sstevel@tonic-gate  * found in the specificed file (name_to_major/rem_name_to_major).
6277c478bd9Sstevel@tonic-gate  *
6287c478bd9Sstevel@tonic-gate  * The return value is actually the size of the internal cache including 0.
6297c478bd9Sstevel@tonic-gate  */
6307c478bd9Sstevel@tonic-gate int
6317c478bd9Sstevel@tonic-gate get_max_major(char *file_name)
6327c478bd9Sstevel@tonic-gate {
6337c478bd9Sstevel@tonic-gate 	char **n_to_m_cache = NULL;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	return (get_cached_n_to_m_file(file_name, &n_to_m_cache));
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate /*
6407c478bd9Sstevel@tonic-gate  * searching name_to_major: if major_no == UNIQUE then the caller wants to
6417c478bd9Sstevel@tonic-gate  * use the driver name as the key.  Otherwise, the caller wants to use
6427c478bd9Sstevel@tonic-gate  * the major number as a key.
6437c478bd9Sstevel@tonic-gate  *
6447c478bd9Sstevel@tonic-gate  * This routine caches the contents of the name_to_major file on
6457c478bd9Sstevel@tonic-gate  * first call.  And it could be generalized to deal with other
6467c478bd9Sstevel@tonic-gate  * config files if necessary.
6477c478bd9Sstevel@tonic-gate  */
6487c478bd9Sstevel@tonic-gate static int
6497c478bd9Sstevel@tonic-gate get_name_to_major_entry(int *major_no, char *driver_name, char *file_name)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate 	int maj;
6527c478bd9Sstevel@tonic-gate 	char **n_to_m_cache = NULL;
6537c478bd9Sstevel@tonic-gate 	int size = 0;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	int ret = NOT_UNIQUE;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	/*
6587c478bd9Sstevel@tonic-gate 	 * read the file in - we cache it in case caller wants to
6597c478bd9Sstevel@tonic-gate 	 * do multiple lookups
6607c478bd9Sstevel@tonic-gate 	 */
6617c478bd9Sstevel@tonic-gate 	size = get_cached_n_to_m_file(file_name, &n_to_m_cache);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	if (size == ERROR)
6647c478bd9Sstevel@tonic-gate 		return (ERROR);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	/* search with driver name as key */
6677c478bd9Sstevel@tonic-gate 	if (*major_no == UNIQUE) {
6687c478bd9Sstevel@tonic-gate 		for (maj = 0; maj < size; maj++) {
6697c478bd9Sstevel@tonic-gate 			if ((n_to_m_cache[maj] != NULL) &&
6707c478bd9Sstevel@tonic-gate 			    (strcmp(driver_name, n_to_m_cache[maj]) == 0)) {
6717c478bd9Sstevel@tonic-gate 				*major_no = maj;
6727c478bd9Sstevel@tonic-gate 				break;
6737c478bd9Sstevel@tonic-gate 			}
6747c478bd9Sstevel@tonic-gate 		}
6757c478bd9Sstevel@tonic-gate 		if (maj >= size)
6767c478bd9Sstevel@tonic-gate 			ret = UNIQUE;
6777c478bd9Sstevel@tonic-gate 	/* search with major number as key */
6787c478bd9Sstevel@tonic-gate 	} else {
6797c478bd9Sstevel@tonic-gate 		/*
6807c478bd9Sstevel@tonic-gate 		 * Bugid 1254588, drvconfig dump core after loading driver
6817c478bd9Sstevel@tonic-gate 		 * with major number bigger than entries defined in
6827c478bd9Sstevel@tonic-gate 		 * /etc/name_to_major.
6837c478bd9Sstevel@tonic-gate 		 */
6847c478bd9Sstevel@tonic-gate 		if (*major_no >= size)
6857c478bd9Sstevel@tonic-gate 			return (UNIQUE);
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 		if (n_to_m_cache[*major_no] != NULL) {
6887c478bd9Sstevel@tonic-gate 			(void) strcpy(driver_name, n_to_m_cache[*major_no]);
6897c478bd9Sstevel@tonic-gate 		} else
6907c478bd9Sstevel@tonic-gate 			ret = UNIQUE;
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate 	return (ret);
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate /*
696f4da9be0Scth  * Given pointer to begining of member 'n' in a space (or separator)
697f4da9be0Scth  * separated list, return pointer to member 'n+1', and establish member 'n'
698f4da9be0Scth  * in *current_entry.  If unquote, then we skip a leading quote and treat
699f4da9be0Scth  * the trailing quote as a separator (and skip).
7007c478bd9Sstevel@tonic-gate  */
7017c478bd9Sstevel@tonic-gate char *
7027c478bd9Sstevel@tonic-gate get_entry(
7037c478bd9Sstevel@tonic-gate 	char *prev_member,
7047c478bd9Sstevel@tonic-gate 	char *current_entry,
705f4da9be0Scth 	char separator,
706f4da9be0Scth 	int  unquote)
7077c478bd9Sstevel@tonic-gate {
7087c478bd9Sstevel@tonic-gate 	char	*ptr;
709f4da9be0Scth 	int	quoted = 0;
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	ptr = prev_member;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	/* skip white space */
7147c478bd9Sstevel@tonic-gate 	while (*ptr == '\t' || *ptr == ' ')
7157c478bd9Sstevel@tonic-gate 		ptr++;
7167c478bd9Sstevel@tonic-gate 
717f4da9be0Scth 	/* if unquote skip leading quote */
718f4da9be0Scth 	if (unquote && *ptr == '"') {
719f4da9be0Scth 		quoted++;
720f4da9be0Scth 		ptr++;
721f4da9be0Scth 	}
722f4da9be0Scth 
723f4da9be0Scth 	/* read thru the current entry looking for end, separator, or unquote */
724f4da9be0Scth 	while (*ptr &&
725f4da9be0Scth 	    (*ptr != separator) &&
726f4da9be0Scth 	    ((separator != ' ') || (*ptr != '\t')) &&
727f4da9be0Scth 	    (!quoted || (*ptr != '"'))) {
7287c478bd9Sstevel@tonic-gate 		*current_entry++ = *ptr++;
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 	*current_entry = '\0';
7317c478bd9Sstevel@tonic-gate 
732f4da9be0Scth 	if (separator && (*ptr == separator))
733f4da9be0Scth 		ptr++;	/* skip over separator */
734f4da9be0Scth 	if (quoted && (*ptr == '"'))
735f4da9be0Scth 		ptr++;	/* skip over trailing quote */
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	/* skip white space */
7387c478bd9Sstevel@tonic-gate 	while (*ptr == '\t' || *ptr == ' ') {
7397c478bd9Sstevel@tonic-gate 		ptr++;
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	return (ptr);
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate void
7467c478bd9Sstevel@tonic-gate enter_lock(void)
7477c478bd9Sstevel@tonic-gate {
74849e92448Svikram 	struct flock lock;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	/*
7517c478bd9Sstevel@tonic-gate 	 * attempt to create the lock file
7527c478bd9Sstevel@tonic-gate 	 */
75349e92448Svikram 	add_rem_lock_fd = open(add_rem_lock, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
75449e92448Svikram 	if (add_rem_lock_fd < 0) {
75549e92448Svikram 		(void) fprintf(stderr, gettext(ERR_CREAT_LOCK),
75649e92448Svikram 		    add_rem_lock, strerror(errno));
75749e92448Svikram 		exit(1);
75849e92448Svikram 	}
75949e92448Svikram 
76049e92448Svikram 	lock.l_type = F_WRLCK;
76149e92448Svikram 	lock.l_whence = SEEK_SET;
76249e92448Svikram 	lock.l_start = 0;
76349e92448Svikram 	lock.l_len = 0;
76449e92448Svikram 
76549e92448Svikram 	/* Try for the lock but don't wait. */
76649e92448Svikram 	if (fcntl(add_rem_lock_fd, F_SETLK, &lock) == -1) {
76749e92448Svikram 		if (errno == EACCES || errno == EAGAIN) {
7687c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_PROG_IN_USE));
7697c478bd9Sstevel@tonic-gate 		} else {
77049e92448Svikram 			(void) fprintf(stderr, gettext(ERR_LOCK),
77149e92448Svikram 			    add_rem_lock, strerror(errno));
7727c478bd9Sstevel@tonic-gate 		}
7737c478bd9Sstevel@tonic-gate 		exit(1);
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate }
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate void
7787c478bd9Sstevel@tonic-gate err_exit(void)
7797c478bd9Sstevel@tonic-gate {
7807c478bd9Sstevel@tonic-gate 	/* release memory allocated for moddir */
7817c478bd9Sstevel@tonic-gate 	cleanup_moddir();
7827c478bd9Sstevel@tonic-gate 	/* remove add_drv/rem_drv lock */
7837c478bd9Sstevel@tonic-gate 	exit_unlock();
7847c478bd9Sstevel@tonic-gate 	exit(1);
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate void
7887c478bd9Sstevel@tonic-gate cleanup_moddir(void)
7897c478bd9Sstevel@tonic-gate {
7907c478bd9Sstevel@tonic-gate 	struct drvmod_dir *walk_ptr;
7917c478bd9Sstevel@tonic-gate 	struct drvmod_dir *free_ptr = moddir;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	while (free_ptr != NULL) {
7947c478bd9Sstevel@tonic-gate 		walk_ptr = free_ptr->next;
7957c478bd9Sstevel@tonic-gate 		free(free_ptr);
7967c478bd9Sstevel@tonic-gate 		free_ptr = walk_ptr;
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate void
8017c478bd9Sstevel@tonic-gate exit_unlock(void)
8027c478bd9Sstevel@tonic-gate {
80349e92448Svikram 	struct flock unlock;
8047c478bd9Sstevel@tonic-gate 
80549e92448Svikram 	if (add_rem_lock_fd < 0)
80649e92448Svikram 		return;
80749e92448Svikram 
80849e92448Svikram 	unlock.l_type = F_UNLCK;
80949e92448Svikram 	unlock.l_whence = SEEK_SET;
81049e92448Svikram 	unlock.l_start = 0;
81149e92448Svikram 	unlock.l_len = 0;
81249e92448Svikram 
81349e92448Svikram 	if (fcntl(add_rem_lock_fd, F_SETLK, &unlock) == -1) {
81449e92448Svikram 		(void) fprintf(stderr, gettext(ERR_UNLOCK),
81549e92448Svikram 		    add_rem_lock, strerror(errno));
81649e92448Svikram 	} else {
81749e92448Svikram 		(void) close(add_rem_lock_fd);
81849e92448Svikram 		add_rem_lock_fd = -1;
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate }
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate /*
8237c478bd9Sstevel@tonic-gate  * error adding driver; need to back out any changes to files.
8247c478bd9Sstevel@tonic-gate  * check flag to see which files need entries removed
8257c478bd9Sstevel@tonic-gate  * entry removal based on driver name
8267c478bd9Sstevel@tonic-gate  */
8277c478bd9Sstevel@tonic-gate void
8287c478bd9Sstevel@tonic-gate remove_entry(
8297c478bd9Sstevel@tonic-gate 	int c_flag,
8307c478bd9Sstevel@tonic-gate 	char *driver_name)
8317c478bd9Sstevel@tonic-gate {
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	if (c_flag & CLEAN_NAM_MAJ) {
8347c478bd9Sstevel@tonic-gate 		if (delete_entry(name_to_major, driver_name, " ",
8357c478bd9Sstevel@tonic-gate 		    NULL) == ERROR) {
8367c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_CLEAN),
8377c478bd9Sstevel@tonic-gate 			    name_to_major, driver_name);
8387c478bd9Sstevel@tonic-gate 		}
8397c478bd9Sstevel@tonic-gate 	}
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	if (c_flag & CLEAN_DRV_ALIAS) {
8427c478bd9Sstevel@tonic-gate 		if (delete_entry(driver_aliases, driver_name, " ",
8437c478bd9Sstevel@tonic-gate 		    NULL) == ERROR) {
8447c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_DEL_ENTRY),
8457c478bd9Sstevel@tonic-gate 			    driver_name, driver_aliases);
8467c478bd9Sstevel@tonic-gate 		}
8477c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	if (c_flag & CLEAN_DRV_CLASSES) {
8507c478bd9Sstevel@tonic-gate 		if (delete_entry(driver_classes, driver_name, "\t", NULL) ==
8517c478bd9Sstevel@tonic-gate 		    ERROR) {
8527c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_DEL_ENTRY),
8537c478bd9Sstevel@tonic-gate 			    driver_name, driver_classes);
8547c478bd9Sstevel@tonic-gate 		}
8557c478bd9Sstevel@tonic-gate 	}
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	if (c_flag & CLEAN_MINOR_PERM) {
8587c478bd9Sstevel@tonic-gate 		if (delete_entry(minor_perm, driver_name, ":", NULL) == ERROR) {
8597c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_DEL_ENTRY),
8607c478bd9Sstevel@tonic-gate 			    driver_name, minor_perm);
8617c478bd9Sstevel@tonic-gate 		}
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 	/*
8647c478bd9Sstevel@tonic-gate 	 * There's no point in removing entries from files that don't
8657c478bd9Sstevel@tonic-gate 	 * exist.  Prevent error messages by checking for file existence
8667c478bd9Sstevel@tonic-gate 	 * first.
8677c478bd9Sstevel@tonic-gate 	 */
8687c478bd9Sstevel@tonic-gate 	if ((c_flag & CLEAN_DEV_POLICY) != 0 &&
8697c478bd9Sstevel@tonic-gate 	    access(device_policy, F_OK) == 0) {
8707c478bd9Sstevel@tonic-gate 		if (delete_plcy_entry(device_policy, driver_name) == ERROR) {
8717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_DEL_ENTRY),
8727c478bd9Sstevel@tonic-gate 			    driver_name, device_policy);
8737c478bd9Sstevel@tonic-gate 		}
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 	if ((c_flag & CLEAN_DRV_PRIV) != 0 &&
8767c478bd9Sstevel@tonic-gate 	    access(extra_privs, F_OK) == 0) {
8777c478bd9Sstevel@tonic-gate 		if (delete_entry(extra_privs, driver_name, ":", NULL) ==
8787c478bd9Sstevel@tonic-gate 		    ERROR) {
8797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_DEL_ENTRY),
8807c478bd9Sstevel@tonic-gate 			    driver_name, extra_privs);
8817c478bd9Sstevel@tonic-gate 		}
8827c478bd9Sstevel@tonic-gate 	}
8837c478bd9Sstevel@tonic-gate }
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate int
8867c478bd9Sstevel@tonic-gate check_perms_aliases(
8877c478bd9Sstevel@tonic-gate 	int m_flag,
8887c478bd9Sstevel@tonic-gate 	int i_flag)
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate 	/*
8917c478bd9Sstevel@tonic-gate 	 * If neither i_flag nor m_flag are specified no need to check the
8927c478bd9Sstevel@tonic-gate 	 * files for access permissions
8937c478bd9Sstevel@tonic-gate 	 */
8947c478bd9Sstevel@tonic-gate 	if (!m_flag && !i_flag)
8957c478bd9Sstevel@tonic-gate 		return (NOERR);
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	/* check minor_perm file : exits and is writable */
8987c478bd9Sstevel@tonic-gate 	if (m_flag) {
8997c478bd9Sstevel@tonic-gate 		if (access(minor_perm, R_OK | W_OK)) {
9007c478bd9Sstevel@tonic-gate 			perror(NULL);
9017c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
9027c478bd9Sstevel@tonic-gate 			    minor_perm);
9037c478bd9Sstevel@tonic-gate 			return (ERROR);
9047c478bd9Sstevel@tonic-gate 		}
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	/* check driver_aliases file : exits and is writable */
9087c478bd9Sstevel@tonic-gate 	if (i_flag) {
9097c478bd9Sstevel@tonic-gate 		if (access(driver_aliases, R_OK | W_OK)) {
9107c478bd9Sstevel@tonic-gate 			perror(NULL);
9117c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
9127c478bd9Sstevel@tonic-gate 			    driver_aliases);
9137c478bd9Sstevel@tonic-gate 			return (ERROR);
9147c478bd9Sstevel@tonic-gate 		}
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	return (NOERR);
9187c478bd9Sstevel@tonic-gate }
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate int
9227c478bd9Sstevel@tonic-gate check_name_to_major(int mode)
9237c478bd9Sstevel@tonic-gate {
9247c478bd9Sstevel@tonic-gate 	/* check name_to_major file : exists and is writable */
9257c478bd9Sstevel@tonic-gate 	if (access(name_to_major, mode)) {
9267c478bd9Sstevel@tonic-gate 		perror(NULL);
9277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
9287c478bd9Sstevel@tonic-gate 		    name_to_major);
9297c478bd9Sstevel@tonic-gate 		return (ERROR);
9307c478bd9Sstevel@tonic-gate 	}
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	return (NOERR);
9337c478bd9Sstevel@tonic-gate }
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate /*
9377c478bd9Sstevel@tonic-gate  * All this stuff is to support a server installing
9387c478bd9Sstevel@tonic-gate  * drivers on diskless clients.  When on the server
9397c478bd9Sstevel@tonic-gate  * need to prepend the basedir
9407c478bd9Sstevel@tonic-gate  */
9417c478bd9Sstevel@tonic-gate int
9427c478bd9Sstevel@tonic-gate build_filenames(char *basedir)
9437c478bd9Sstevel@tonic-gate {
9447c478bd9Sstevel@tonic-gate 	int	len;
945*2e107de7SJerry Gilliam 	int	driver_aliases_len;
946*2e107de7SJerry Gilliam 	int	driver_classes_len;
947*2e107de7SJerry Gilliam 	int	minor_perm_len;
948*2e107de7SJerry Gilliam 	int	name_to_major_len;
949*2e107de7SJerry Gilliam 	int	rem_name_to_major_len;
950*2e107de7SJerry Gilliam 	int	add_rem_lock_len;
951*2e107de7SJerry Gilliam 	int	tmphold_len;
952*2e107de7SJerry Gilliam 	int	devfs_root_len;
953*2e107de7SJerry Gilliam 	int	device_policy_len;
954*2e107de7SJerry Gilliam 	int	extra_privs_len;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	if (basedir == NULL) {
9577c478bd9Sstevel@tonic-gate 		driver_aliases = DRIVER_ALIAS;
9587c478bd9Sstevel@tonic-gate 		driver_classes = DRIVER_CLASSES;
9597c478bd9Sstevel@tonic-gate 		minor_perm = MINOR_PERM;
9607c478bd9Sstevel@tonic-gate 		name_to_major = NAM_TO_MAJ;
9617c478bd9Sstevel@tonic-gate 		rem_name_to_major = REM_NAM_TO_MAJ;
9627c478bd9Sstevel@tonic-gate 		add_rem_lock = ADD_REM_LOCK;
9637c478bd9Sstevel@tonic-gate 		tmphold = TMPHOLD;
9647c478bd9Sstevel@tonic-gate 		devfs_root = DEVFS_ROOT;
9657c478bd9Sstevel@tonic-gate 		device_policy = DEV_POLICY;
9667c478bd9Sstevel@tonic-gate 		extra_privs = EXTRA_PRIVS;
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	} else {
969*2e107de7SJerry Gilliam 		len = strlen(basedir) + 1;
9707c478bd9Sstevel@tonic-gate 
971*2e107de7SJerry Gilliam 		driver_aliases_len = len + sizeof (DRIVER_ALIAS);
972*2e107de7SJerry Gilliam 		driver_classes_len = len + sizeof (DRIVER_CLASSES);
973*2e107de7SJerry Gilliam 		minor_perm_len = len + sizeof (MINOR_PERM);
974*2e107de7SJerry Gilliam 		name_to_major_len = len + sizeof (NAM_TO_MAJ);
975*2e107de7SJerry Gilliam 		rem_name_to_major_len = len + sizeof (REM_NAM_TO_MAJ);
976*2e107de7SJerry Gilliam 		add_rem_lock_len = len + sizeof (ADD_REM_LOCK);
977*2e107de7SJerry Gilliam 		tmphold_len = len + sizeof (TMPHOLD);
978*2e107de7SJerry Gilliam 		devfs_root_len = len + sizeof (DEVFS_ROOT);
979*2e107de7SJerry Gilliam 		device_policy_len = len + sizeof (DEV_POLICY);
980*2e107de7SJerry Gilliam 		extra_privs_len = len + sizeof (EXTRA_PRIVS);
9817c478bd9Sstevel@tonic-gate 
982*2e107de7SJerry Gilliam 		driver_aliases = malloc(driver_aliases_len);
983*2e107de7SJerry Gilliam 		driver_classes = malloc(driver_classes_len);
984*2e107de7SJerry Gilliam 		minor_perm = malloc(minor_perm_len);
985*2e107de7SJerry Gilliam 		name_to_major = malloc(name_to_major_len);
986*2e107de7SJerry Gilliam 		rem_name_to_major = malloc(rem_name_to_major_len);
987*2e107de7SJerry Gilliam 		add_rem_lock = malloc(add_rem_lock_len);
988*2e107de7SJerry Gilliam 		tmphold = malloc(tmphold_len);
989*2e107de7SJerry Gilliam 		devfs_root = malloc(devfs_root_len);
990*2e107de7SJerry Gilliam 		device_policy = malloc(device_policy_len);
991*2e107de7SJerry Gilliam 		extra_privs = malloc(extra_privs_len);
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 		if ((driver_aliases == NULL) ||
9947c478bd9Sstevel@tonic-gate 		    (driver_classes == NULL) ||
9957c478bd9Sstevel@tonic-gate 		    (minor_perm == NULL) ||
9967c478bd9Sstevel@tonic-gate 		    (name_to_major == NULL) ||
9977c478bd9Sstevel@tonic-gate 		    (rem_name_to_major == NULL) ||
9987c478bd9Sstevel@tonic-gate 		    (add_rem_lock == NULL) ||
9997c478bd9Sstevel@tonic-gate 		    (tmphold == NULL) ||
10007c478bd9Sstevel@tonic-gate 		    (devfs_root == NULL) ||
10017c478bd9Sstevel@tonic-gate 		    (device_policy == NULL) ||
10027c478bd9Sstevel@tonic-gate 		    (extra_privs == NULL)) {
10037c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_MEM));
10047c478bd9Sstevel@tonic-gate 			return (ERROR);
10057c478bd9Sstevel@tonic-gate 		}
10067c478bd9Sstevel@tonic-gate 
1007*2e107de7SJerry Gilliam 		(void) snprintf(driver_aliases, driver_aliases_len,
1008*2e107de7SJerry Gilliam 		    "%s%s", basedir, DRIVER_ALIAS);
1009*2e107de7SJerry Gilliam 		(void) snprintf(driver_classes, driver_classes_len,
1010*2e107de7SJerry Gilliam 		    "%s%s", basedir, DRIVER_CLASSES);
1011*2e107de7SJerry Gilliam 		(void) snprintf(minor_perm, minor_perm_len,
1012*2e107de7SJerry Gilliam 		    "%s%s", basedir, MINOR_PERM);
1013*2e107de7SJerry Gilliam 		(void) snprintf(name_to_major, name_to_major_len,
1014*2e107de7SJerry Gilliam 		    "%s%s", basedir, NAM_TO_MAJ);
1015*2e107de7SJerry Gilliam 		(void) snprintf(rem_name_to_major, rem_name_to_major_len,
1016*2e107de7SJerry Gilliam 		    "%s%s", basedir, REM_NAM_TO_MAJ);
1017*2e107de7SJerry Gilliam 		(void) snprintf(add_rem_lock, add_rem_lock_len,
1018*2e107de7SJerry Gilliam 		    "%s%s", basedir, ADD_REM_LOCK);
1019*2e107de7SJerry Gilliam 		(void) snprintf(tmphold, tmphold_len,
1020*2e107de7SJerry Gilliam 		    "%s%s", basedir, TMPHOLD);
1021*2e107de7SJerry Gilliam 		(void) snprintf(devfs_root, devfs_root_len,
1022*2e107de7SJerry Gilliam 		    "%s%s", basedir, DEVFS_ROOT);
1023*2e107de7SJerry Gilliam 		(void) snprintf(device_policy, device_policy_len,
1024*2e107de7SJerry Gilliam 		    "%s%s", basedir, DEV_POLICY);
1025*2e107de7SJerry Gilliam 		(void) snprintf(extra_privs, extra_privs_len,
1026*2e107de7SJerry Gilliam 		    "%s%s", basedir, EXTRA_PRIVS);
10277c478bd9Sstevel@tonic-gate 	}
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 	return (NOERR);
10307c478bd9Sstevel@tonic-gate }
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate static int
10337c478bd9Sstevel@tonic-gate exec_command(char *path, char *cmdline[MAX_CMD_LINE])
10347c478bd9Sstevel@tonic-gate {
10357c478bd9Sstevel@tonic-gate 	pid_t pid;
10367c478bd9Sstevel@tonic-gate 	uint_t stat_loc;
10377c478bd9Sstevel@tonic-gate 	int waitstat;
10387c478bd9Sstevel@tonic-gate 	int exit_status;
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	/* child */
10417c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == 0) {
10427c478bd9Sstevel@tonic-gate 		(void) execv(path, cmdline);
10437c478bd9Sstevel@tonic-gate 		perror(NULL);
10447c478bd9Sstevel@tonic-gate 		return (ERROR);
10457c478bd9Sstevel@tonic-gate 	} else if (pid == -1) {
10467c478bd9Sstevel@tonic-gate 		/* fork failed */
10477c478bd9Sstevel@tonic-gate 		perror(NULL);
10487c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_FORK_FAIL), cmdline);
10497c478bd9Sstevel@tonic-gate 		return (ERROR);
10507c478bd9Sstevel@tonic-gate 	} else {
10517c478bd9Sstevel@tonic-gate 		/* parent */
10527c478bd9Sstevel@tonic-gate 		do {
10537c478bd9Sstevel@tonic-gate 			waitstat = waitpid(pid, (int *)&stat_loc, 0);
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 		} while ((!WIFEXITED(stat_loc) &&
10567c478bd9Sstevel@tonic-gate 		    !WIFSIGNALED(stat_loc)) || (waitstat == 0));
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 		exit_status = WEXITSTATUS(stat_loc);
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 		return (exit_status);
10617c478bd9Sstevel@tonic-gate 	}
10627c478bd9Sstevel@tonic-gate }
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate /*
10656532b960SJerry Gilliam  * Exec devfsadm to perform driver config/unconfig operation,
10666532b960SJerry Gilliam  * adding or removing aliases.
10677c478bd9Sstevel@tonic-gate  */
10686532b960SJerry Gilliam static int
10696532b960SJerry Gilliam exec_devfsadm(
10706532b960SJerry Gilliam 	boolean_t config,
10717c478bd9Sstevel@tonic-gate 	char *driver_name,
10727c478bd9Sstevel@tonic-gate 	major_t major_num,
10737c478bd9Sstevel@tonic-gate 	char *aliases,
10747c478bd9Sstevel@tonic-gate 	char *classes,
10756532b960SJerry Gilliam 	int verbose_flag,
10766532b960SJerry Gilliam 	int force_flag)
10777c478bd9Sstevel@tonic-gate {
10787c478bd9Sstevel@tonic-gate 	int n = 0;
10797c478bd9Sstevel@tonic-gate 	char *cmdline[MAX_CMD_LINE];
10807c478bd9Sstevel@tonic-gate 	char maj_num[128];
10817c478bd9Sstevel@tonic-gate 	char *previous;
10827c478bd9Sstevel@tonic-gate 	char *current;
10837c478bd9Sstevel@tonic-gate 	int len;
10846532b960SJerry Gilliam 	int rv;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	/* build command line */
10877c478bd9Sstevel@tonic-gate 	cmdline[n++] = DRVCONFIG;
10886532b960SJerry Gilliam 	if (config == B_FALSE) {
10896532b960SJerry Gilliam 		cmdline[n++] = "-u";		/* unconfigure */
10906532b960SJerry Gilliam 		if (force_flag)
10916532b960SJerry Gilliam 			cmdline[n++] = "-f";	/* force if currently in use */
10926532b960SJerry Gilliam 	}
10937c478bd9Sstevel@tonic-gate 	if (verbose_flag) {
10947c478bd9Sstevel@tonic-gate 		cmdline[n++] = "-v";
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 	cmdline[n++] = "-b";
10977c478bd9Sstevel@tonic-gate 	if (classes) {
10987c478bd9Sstevel@tonic-gate 		cmdline[n++] = "-c";
10997c478bd9Sstevel@tonic-gate 		cmdline[n++] = classes;
11007c478bd9Sstevel@tonic-gate 	}
11017c478bd9Sstevel@tonic-gate 	cmdline[n++] = "-i";
11027c478bd9Sstevel@tonic-gate 	cmdline[n++] = driver_name;
11037c478bd9Sstevel@tonic-gate 	cmdline[n++] = "-m";
1104*2e107de7SJerry Gilliam 	(void) snprintf(maj_num, sizeof (maj_num), "%lu", major_num);
11057c478bd9Sstevel@tonic-gate 	cmdline[n++] = maj_num;
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	if (aliases != NULL) {
11087c478bd9Sstevel@tonic-gate 		len = strlen(aliases);
11097c478bd9Sstevel@tonic-gate 		previous = aliases;
11107c478bd9Sstevel@tonic-gate 		do {
11117c478bd9Sstevel@tonic-gate 			cmdline[n++] = "-a";
11127c478bd9Sstevel@tonic-gate 			cmdline[n] = calloc(len + 1, 1);
11137c478bd9Sstevel@tonic-gate 			if (cmdline[n] == NULL) {
11147c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11157c478bd9Sstevel@tonic-gate 				    gettext(ERR_NO_MEM));
11167c478bd9Sstevel@tonic-gate 				return (ERROR);
11177c478bd9Sstevel@tonic-gate 			}
11187c478bd9Sstevel@tonic-gate 			current = get_entry(previous,
1119f4da9be0Scth 			    cmdline[n++], ' ', 0);
11207c478bd9Sstevel@tonic-gate 			previous = current;
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 		} while (*current != '\0');
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	}
11257c478bd9Sstevel@tonic-gate 	cmdline[n] = (char *)0;
11267c478bd9Sstevel@tonic-gate 
11276532b960SJerry Gilliam 	rv = exec_command(DRVCONFIG_PATH, cmdline);
11286532b960SJerry Gilliam 	if (rv == NOERR)
11296532b960SJerry Gilliam 		return (NOERR);
11306532b960SJerry Gilliam 	return (ERROR);
11316532b960SJerry Gilliam }
11327c478bd9Sstevel@tonic-gate 
11336532b960SJerry Gilliam int
11346532b960SJerry Gilliam unconfig_driver(
11356532b960SJerry Gilliam 	char *driver_name,
11366532b960SJerry Gilliam 	major_t major_num,
11376532b960SJerry Gilliam 	char *aliases,
11386532b960SJerry Gilliam 	int verbose_flag,
11396532b960SJerry Gilliam 	int force_flag)
11406532b960SJerry Gilliam {
11416532b960SJerry Gilliam 	return (exec_devfsadm(B_FALSE, driver_name, major_num,
11426532b960SJerry Gilliam 	    aliases, NULL, verbose_flag, force_flag));
11436532b960SJerry Gilliam }
11446532b960SJerry Gilliam 
11456532b960SJerry Gilliam /*
11466532b960SJerry Gilliam  * check that major_num doesn't exceed maximum on this machine
11476532b960SJerry Gilliam  * do this here to support add_drv on server for diskless clients
11486532b960SJerry Gilliam  */
11496532b960SJerry Gilliam int
11506532b960SJerry Gilliam config_driver(
11516532b960SJerry Gilliam 	char *driver_name,
11526532b960SJerry Gilliam 	major_t major_num,
11536532b960SJerry Gilliam 	char *aliases,
11546532b960SJerry Gilliam 	char *classes,
11556532b960SJerry Gilliam 	int cleanup_flag,
11566532b960SJerry Gilliam 	int verbose_flag)
11576532b960SJerry Gilliam {
11586532b960SJerry Gilliam 	int	max_dev;
11596532b960SJerry Gilliam 	int	rv;
11606532b960SJerry Gilliam 
11616532b960SJerry Gilliam 	if (modctl(MODRESERVED, NULL, &max_dev) < 0) {
11626532b960SJerry Gilliam 		perror(NULL);
11636532b960SJerry Gilliam 		(void) fprintf(stderr, gettext(ERR_MAX_MAJOR));
11646532b960SJerry Gilliam 		return (ERROR);
11656532b960SJerry Gilliam 	}
11666532b960SJerry Gilliam 
11676532b960SJerry Gilliam 	if (major_num >= max_dev) {
11686532b960SJerry Gilliam 		(void) fprintf(stderr, gettext(ERR_MAX_EXCEEDS),
11696532b960SJerry Gilliam 		    major_num, max_dev);
11706532b960SJerry Gilliam 		return (ERROR);
11716532b960SJerry Gilliam 	}
11726532b960SJerry Gilliam 
11736532b960SJerry Gilliam 	/* bind major number and driver name */
11746532b960SJerry Gilliam 	rv = exec_devfsadm(B_TRUE, driver_name, major_num,
11756532b960SJerry Gilliam 	    aliases, classes, verbose_flag, 0);
11766532b960SJerry Gilliam 
11776532b960SJerry Gilliam 	if (rv == NOERR)
11787c478bd9Sstevel@tonic-gate 		return (NOERR);
11797c478bd9Sstevel@tonic-gate 	perror(NULL);
11807c478bd9Sstevel@tonic-gate 	remove_entry(cleanup_flag, driver_name);
11817c478bd9Sstevel@tonic-gate 	return (ERROR);
11827c478bd9Sstevel@tonic-gate }
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate void
11857c478bd9Sstevel@tonic-gate load_driver(char *driver_name, int verbose_flag)
11867c478bd9Sstevel@tonic-gate {
11877c478bd9Sstevel@tonic-gate 	int n = 0;
11887c478bd9Sstevel@tonic-gate 	char *cmdline[MAX_CMD_LINE];
11897c478bd9Sstevel@tonic-gate 	int exec_status;
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	/* build command line */
11927c478bd9Sstevel@tonic-gate 	cmdline[n++] = DEVFSADM;
11937c478bd9Sstevel@tonic-gate 	if (verbose_flag) {
11947c478bd9Sstevel@tonic-gate 		cmdline[n++] = "-v";
11957c478bd9Sstevel@tonic-gate 	}
11967c478bd9Sstevel@tonic-gate 	cmdline[n++] = "-i";
11977c478bd9Sstevel@tonic-gate 	cmdline[n++] = driver_name;
11987c478bd9Sstevel@tonic-gate 	cmdline[n] = (char *)0;
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	exec_status = exec_command(DEVFSADM_PATH, cmdline);
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	if (exec_status != NOERR) {
12037c478bd9Sstevel@tonic-gate 		/* no clean : name and major number are bound */
120411ebc2d0SJerry Gilliam 		(void) fprintf(stderr, gettext(ERR_CONFIG), driver_name);
12057c478bd9Sstevel@tonic-gate 	}
12067c478bd9Sstevel@tonic-gate }
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate void
12097c478bd9Sstevel@tonic-gate get_modid(char *driver_name, int *mod)
12107c478bd9Sstevel@tonic-gate {
12117c478bd9Sstevel@tonic-gate 	struct modinfo	modinfo;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	modinfo.mi_id = -1;
12147c478bd9Sstevel@tonic-gate 	modinfo.mi_info = MI_INFO_ALL;
12157c478bd9Sstevel@tonic-gate 	do {
12167c478bd9Sstevel@tonic-gate 		/*
12177c478bd9Sstevel@tonic-gate 		 * If we are at the end of the list of loaded modules
12187c478bd9Sstevel@tonic-gate 		 * then set *mod = -1 and return
12197c478bd9Sstevel@tonic-gate 		 */
12207c478bd9Sstevel@tonic-gate 		if (modctl(MODINFO, 0, &modinfo) < 0) {
12217c478bd9Sstevel@tonic-gate 			*mod = -1;
12227c478bd9Sstevel@tonic-gate 			return;
12237c478bd9Sstevel@tonic-gate 		}
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 		*mod = modinfo.mi_id;
12267c478bd9Sstevel@tonic-gate 	} while (strcmp(driver_name, modinfo.mi_name) != 0);
12277c478bd9Sstevel@tonic-gate }
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate int
12307c478bd9Sstevel@tonic-gate create_reconfig(char *basedir)
12317c478bd9Sstevel@tonic-gate {
12327c478bd9Sstevel@tonic-gate 	char reconfig_file[MAXPATHLEN + FILENAME_MAX + 1];
12337c478bd9Sstevel@tonic-gate 	FILE *reconfig_fp;
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	if (basedir != NULL) {
12367c478bd9Sstevel@tonic-gate 		(void) strcpy(reconfig_file, basedir);
12377c478bd9Sstevel@tonic-gate 		(void) strcat(reconfig_file, RECONFIGURE);
12387c478bd9Sstevel@tonic-gate 	} else {
12397c478bd9Sstevel@tonic-gate 		(void) strcpy(reconfig_file, RECONFIGURE);
12407c478bd9Sstevel@tonic-gate 	}
12417c478bd9Sstevel@tonic-gate 	if ((reconfig_fp = fopen(reconfig_file, "a")) == NULL)
12427c478bd9Sstevel@tonic-gate 		return (ERROR);
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	(void) fclose(reconfig_fp);
12457c478bd9Sstevel@tonic-gate 	return (NOERR);
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate /*
12507c478bd9Sstevel@tonic-gate  * update_minor_entry:
12517c478bd9Sstevel@tonic-gate  *	open file
12527c478bd9Sstevel@tonic-gate  *	for each entry in list
12537c478bd9Sstevel@tonic-gate  *		where list entries are separated by <list_separator>
12547c478bd9Sstevel@tonic-gate  * 		modify entry : driver_name <entry_separator> entry
12557c478bd9Sstevel@tonic-gate  *	close file
12567c478bd9Sstevel@tonic-gate  *
12577c478bd9Sstevel@tonic-gate  *	return error/noerr
12587c478bd9Sstevel@tonic-gate  */
12597c478bd9Sstevel@tonic-gate int
12607c478bd9Sstevel@tonic-gate update_minor_entry(char *driver_name, char *perm_list)
12617c478bd9Sstevel@tonic-gate {
12627c478bd9Sstevel@tonic-gate 	FILE *fp;
12637c478bd9Sstevel@tonic-gate 	FILE *newfp;
12647c478bd9Sstevel@tonic-gate 	struct group *sysgrp;
12657c478bd9Sstevel@tonic-gate 	int match = 0;
12661ca93273Seota 	char line[MAX_DBFILE_ENTRY], *cp, *dup;
12671ca93273Seota 	char drv[FILENAME_MAX + 1], *drv_minor;
12687c478bd9Sstevel@tonic-gate 	char minor[FILENAME_MAX + 1], perm[OPT_LEN + 1];
12697c478bd9Sstevel@tonic-gate 	char own[OPT_LEN + 1], grp[OPT_LEN + 1];
12707c478bd9Sstevel@tonic-gate 	int status = NOERR, i;
12717c478bd9Sstevel@tonic-gate 	char *newfile, *tptr;
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 	if ((fp = fopen(minor_perm, "r")) == NULL) {
12747c478bd9Sstevel@tonic-gate 		perror(NULL);
12757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
12767c478bd9Sstevel@tonic-gate 		    minor_perm);
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 		return (ERROR);
12797c478bd9Sstevel@tonic-gate 	}
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	/*
12827c478bd9Sstevel@tonic-gate 	 * Build filename for temporary file
12837c478bd9Sstevel@tonic-gate 	 */
12847c478bd9Sstevel@tonic-gate 	if ((tptr = calloc(strlen(minor_perm) + strlen(XEND) + 1, 1)) == NULL) {
12857c478bd9Sstevel@tonic-gate 		perror(NULL);
12867c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
12877c478bd9Sstevel@tonic-gate 	}
12887c478bd9Sstevel@tonic-gate 	(void) strcpy(tptr, minor_perm);
12897c478bd9Sstevel@tonic-gate 	(void) strcat(tptr, XEND);
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	/*
12927c478bd9Sstevel@tonic-gate 	 * Set gid so we preserve group attribute.  Ideally we wouldn't
12937c478bd9Sstevel@tonic-gate 	 * assume a gid of "sys" but we can't undo the damage on already
12947c478bd9Sstevel@tonic-gate 	 * installed systems unless we force the issue.
12957c478bd9Sstevel@tonic-gate 	 */
12967c478bd9Sstevel@tonic-gate 	if ((sysgrp = getgrnam("sys")) != NULL) {
12977c478bd9Sstevel@tonic-gate 		(void) setgid(sysgrp->gr_gid);
12987c478bd9Sstevel@tonic-gate 	}
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	newfile = mktemp(tptr);
13017c478bd9Sstevel@tonic-gate 	if ((newfp = fopen(newfile, "w")) == NULL) {
13027c478bd9Sstevel@tonic-gate 		perror(NULL);
13037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
13047c478bd9Sstevel@tonic-gate 		    newfile);
13057c478bd9Sstevel@tonic-gate 		return (ERROR);
13067c478bd9Sstevel@tonic-gate 	}
13077c478bd9Sstevel@tonic-gate 
1308*2e107de7SJerry Gilliam 	/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
13097c478bd9Sstevel@tonic-gate 	if (sscanf(perm_list, "%s%s%s%s", minor, perm, own, grp) != 4) {
13107c478bd9Sstevel@tonic-gate 		status = ERROR;
13117c478bd9Sstevel@tonic-gate 	}
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	while ((fgets(line, sizeof (line), fp) != NULL) && status == NOERR) {
13141ca93273Seota 		/* copy the whole line into dup */
13151ca93273Seota 		if ((dup = strdup(line)) == NULL) {
13161ca93273Seota 			perror(NULL);
13171ca93273Seota 			(void) fprintf(stderr, gettext(ERR_NO_MEM));
13181ca93273Seota 			status = ERROR;
13191ca93273Seota 			break;
13201ca93273Seota 		}
13211ca93273Seota 		/* cut off comments starting with '#' */
13221ca93273Seota 		if ((cp = strchr(dup, '#')) != NULL)
13231ca93273Seota 			*cp = '\0';
13241ca93273Seota 		/* ignore comment or blank lines */
13251ca93273Seota 		if (is_blank(dup)) {
13261ca93273Seota 			if (fputs(line, newfp) == EOF) {
13277c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_UPDATE),
13287c478bd9Sstevel@tonic-gate 				    minor_perm);
13297c478bd9Sstevel@tonic-gate 				status = ERROR;
13307c478bd9Sstevel@tonic-gate 			}
13311ca93273Seota 			free(dup);
13327c478bd9Sstevel@tonic-gate 			continue;
13337c478bd9Sstevel@tonic-gate 		}
13347c478bd9Sstevel@tonic-gate 
13351ca93273Seota 		/* get the driver name */
1336*2e107de7SJerry Gilliam 		/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
13371ca93273Seota 		if (sscanf(dup, "%s", drv) != 1) {
13387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_BAD_LINE),
13397c478bd9Sstevel@tonic-gate 			    minor_perm, line);
13407c478bd9Sstevel@tonic-gate 			status = ERROR;
13411ca93273Seota 			free(dup);
13421ca93273Seota 			break;
13437c478bd9Sstevel@tonic-gate 		}
13441ca93273Seota 
13451ca93273Seota 		/*
13461ca93273Seota 		 * get the minor name; place the NULL character at the
13471ca93273Seota 		 * end of the driver name, then make the drv_minor
13481ca93273Seota 		 * point to the first character of the minor name.
13491ca93273Seota 		 * the line missing ':' must be treated as a broken one.
13501ca93273Seota 		 */
13511ca93273Seota 		i = strcspn(drv, ":");
13521ca93273Seota 		if (i == strlen(drv)) {
13531ca93273Seota 			(void) fprintf(stderr, gettext(ERR_BAD_LINE),
13541ca93273Seota 			    minor_perm, line);
13551ca93273Seota 			status = ERROR;
13561ca93273Seota 			free(dup);
13571ca93273Seota 			break;
13581ca93273Seota 		}
13597c478bd9Sstevel@tonic-gate 		drv[i] =  '\0';
13601ca93273Seota 		drv_minor = &drv[strlen(drv) + 1];
13617c478bd9Sstevel@tonic-gate 
13621ca93273Seota 		/*
13631ca93273Seota 		 * compare both of the driver name and the minor name.
13641ca93273Seota 		 * then the new line should be written to the file if
13651ca93273Seota 		 * both of them match
13661ca93273Seota 		 */
13677c478bd9Sstevel@tonic-gate 		if ((strcmp(drv, driver_name) == 0) &&
13687c478bd9Sstevel@tonic-gate 		    (strcmp(minor, drv_minor) == 0)) {
13691ca93273Seota 			/* if it has a comment, keep it */
13701ca93273Seota 			if (cp != NULL) {
13711ca93273Seota 				cp++; /* skip a terminator */
1372*2e107de7SJerry Gilliam 				(void) snprintf(line, sizeof (line),
1373*2e107de7SJerry Gilliam 				    "%s:%s %s %s %s #%s\n",
13741ca93273Seota 				    drv, minor, perm, own, grp, cp);
13751ca93273Seota 			} else {
1376*2e107de7SJerry Gilliam 				(void) snprintf(line, sizeof (line),
1377*2e107de7SJerry Gilliam 				    "%s:%s %s %s %s\n",
13787c478bd9Sstevel@tonic-gate 				    drv, minor, perm, own, grp);
13791ca93273Seota 			}
13807c478bd9Sstevel@tonic-gate 			match = 1;
13817c478bd9Sstevel@tonic-gate 		}
13821ca93273Seota 		free(dup);
13837c478bd9Sstevel@tonic-gate 
13841ca93273Seota 		/* update the file */
13857c478bd9Sstevel@tonic-gate 		if ((fputs(line, newfp)) == EOF) {
13867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE),
13877c478bd9Sstevel@tonic-gate 			    minor_perm);
13887c478bd9Sstevel@tonic-gate 			status = ERROR;
13897c478bd9Sstevel@tonic-gate 		}
13907c478bd9Sstevel@tonic-gate 	}
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	if (!match) {
13937c478bd9Sstevel@tonic-gate 		(void) bzero(line, sizeof (&line[0]));
1394*2e107de7SJerry Gilliam 		(void) snprintf(line, sizeof (line),
1395*2e107de7SJerry Gilliam 		    "%s:%s %s %s %s\n",
13967c478bd9Sstevel@tonic-gate 		    driver_name, minor, perm, own, grp);
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 		/* add the new entry */
13997c478bd9Sstevel@tonic-gate 		if ((fputs(line, newfp)) == EOF) {
14007c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE), minor_perm);
14017c478bd9Sstevel@tonic-gate 			status = ERROR;
14027c478bd9Sstevel@tonic-gate 		}
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	if (fflush(newfp) != 0 || fsync(fileno(newfp)) != 0)
14087c478bd9Sstevel@tonic-gate 		status = ERROR;
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	(void) fclose(newfp);
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	/*
14137c478bd9Sstevel@tonic-gate 	 * if error, leave original file, delete new file
14147c478bd9Sstevel@tonic-gate 	 * if noerr, replace original file with new file
14157c478bd9Sstevel@tonic-gate 	 */
14167c478bd9Sstevel@tonic-gate 	if (status == NOERR) {
14177c478bd9Sstevel@tonic-gate 		if (rename(minor_perm, tmphold) == -1) {
14187c478bd9Sstevel@tonic-gate 			perror(NULL);
14197c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE), minor_perm);
14207c478bd9Sstevel@tonic-gate 			(void) unlink(newfile);
14217c478bd9Sstevel@tonic-gate 			return (ERROR);
14227c478bd9Sstevel@tonic-gate 		} else if (rename(newfile, minor_perm) == -1) {
14237c478bd9Sstevel@tonic-gate 			perror(NULL);
14247c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE), minor_perm);
14257c478bd9Sstevel@tonic-gate 			(void) unlink(minor_perm);
14267c478bd9Sstevel@tonic-gate 			(void) unlink(newfile);
14277c478bd9Sstevel@tonic-gate 			if (link(tmphold, minor_perm) == -1) {
14287c478bd9Sstevel@tonic-gate 				perror(NULL);
14297c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_BAD_LINK),
14307c478bd9Sstevel@tonic-gate 				    minor_perm, tmphold);
14317c478bd9Sstevel@tonic-gate 			}
14327c478bd9Sstevel@tonic-gate 			return (ERROR);
14337c478bd9Sstevel@tonic-gate 		}
14347c478bd9Sstevel@tonic-gate 		(void) unlink(tmphold);
14357c478bd9Sstevel@tonic-gate 	} else {
14367c478bd9Sstevel@tonic-gate 		/*
14377c478bd9Sstevel@tonic-gate 		 * since there's an error, leave file alone; remove
14387c478bd9Sstevel@tonic-gate 		 * new file
14397c478bd9Sstevel@tonic-gate 		 */
14407c478bd9Sstevel@tonic-gate 		if (unlink(newfile) == -1) {
14417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_CANT_RM), newfile);
14427c478bd9Sstevel@tonic-gate 		}
14437c478bd9Sstevel@tonic-gate 		return (ERROR);
14447c478bd9Sstevel@tonic-gate 	}
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	return (NOERR);
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate }
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate /*
14527c478bd9Sstevel@tonic-gate  * list_entry:
14537c478bd9Sstevel@tonic-gate  *	open file
14547c478bd9Sstevel@tonic-gate  *	read thru file, listing all entries if first entry = driver_name
14557c478bd9Sstevel@tonic-gate  *	close
14567c478bd9Sstevel@tonic-gate  */
14577c478bd9Sstevel@tonic-gate void
14587c478bd9Sstevel@tonic-gate list_entry(
14597c478bd9Sstevel@tonic-gate 	char *oldfile,
14607c478bd9Sstevel@tonic-gate 	char *driver_name,
14617c478bd9Sstevel@tonic-gate 	char *marker)
14627c478bd9Sstevel@tonic-gate {
14637c478bd9Sstevel@tonic-gate 	FILE	*fp;
14647c478bd9Sstevel@tonic-gate 	int	i;
14651ca93273Seota 	char	line[MAX_DBFILE_ENTRY], *cp;
14667c478bd9Sstevel@tonic-gate 	char	drv[FILENAME_MAX + 1];
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	if ((fp = fopen(oldfile, "r")) == NULL) {
14697c478bd9Sstevel@tonic-gate 		perror(NULL);
14707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), oldfile);
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 		return;
14737c478bd9Sstevel@tonic-gate 	}
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line), fp) != NULL) {
14761ca93273Seota 		/* cut off comments starting with '#' */
14771ca93273Seota 		if ((cp = strchr(line, '#')) != NULL)
14781ca93273Seota 			*cp = '\0';
14791ca93273Seota 		/* ignore comment or blank lines */
14801ca93273Seota 		if (is_blank(line))
14817c478bd9Sstevel@tonic-gate 			continue;
14821ca93273Seota 		/* sanity-check */
1483*2e107de7SJerry Gilliam 		/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
14847c478bd9Sstevel@tonic-gate 		if (sscanf(line, "%s", drv) != 1) {
14857c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_BAD_LINE),
14867c478bd9Sstevel@tonic-gate 			    oldfile, line);
14877c478bd9Sstevel@tonic-gate 		}
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 		for (i = strcspn(drv, marker); i < FILENAME_MAX; i++) {
14907c478bd9Sstevel@tonic-gate 			drv[i] =  '\0';
14917c478bd9Sstevel@tonic-gate 		}
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 		if (strcmp(driver_name, drv) == 0) {
14947c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "%s", line);
14957c478bd9Sstevel@tonic-gate 		}
14967c478bd9Sstevel@tonic-gate 	}
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
14997c478bd9Sstevel@tonic-gate }
15007c478bd9Sstevel@tonic-gate 
15011ca93273Seota static boolean_t
15021ca93273Seota is_token(char *tok)
15031ca93273Seota {
15041ca93273Seota 	/*
15051ca93273Seota 	 * Check the token here. According to IEEE1275 Open Firmware Boot
15061ca93273Seota 	 * Standard, the name is composed of 1 to 31 letters,
15071ca93273Seota 	 * digits and punctuation characters from the set ",._+-", and
15081ca93273Seota 	 * uppercase and lowercase characters are considered distinct.
15091ca93273Seota 	 * (ie. token := [a-zA-Z0-9,._+-]+, length(token) <= 31)
15101ca93273Seota 	 * However, since either the definition of driver or aliase names is
15111ca93273Seota 	 * not known well, only '#' is avoided explicitly. (the kernel lexical
15121ca93273Seota 	 * analyzer treats it as a start of a comment)
15131ca93273Seota 	 */
15141ca93273Seota 	for (/* nothing */; *tok != '\0'; tok++)
15151ca93273Seota 		if (*tok == '#' || iscntrl(*tok))
15161ca93273Seota 			return (B_FALSE);
15171ca93273Seota 
15181ca93273Seota 	return (B_TRUE);
15191ca93273Seota }
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate /*
15227c478bd9Sstevel@tonic-gate  * check each entry in perm_list for:
15237c478bd9Sstevel@tonic-gate  *	4 arguments
15247c478bd9Sstevel@tonic-gate  *	permission arg is in valid range
15257c478bd9Sstevel@tonic-gate  * permlist entries separated by comma
15267c478bd9Sstevel@tonic-gate  * return ERROR/NOERR
15277c478bd9Sstevel@tonic-gate  */
15287c478bd9Sstevel@tonic-gate int
15297c478bd9Sstevel@tonic-gate check_perm_opts(char *perm_list)
15307c478bd9Sstevel@tonic-gate {
15317c478bd9Sstevel@tonic-gate 	char *current_head;
15327c478bd9Sstevel@tonic-gate 	char *previous_head;
15337c478bd9Sstevel@tonic-gate 	char *one_entry;
15347c478bd9Sstevel@tonic-gate 	int i, len, scan_stat;
15357c478bd9Sstevel@tonic-gate 	char minor[FILENAME_MAX + 1];
15367c478bd9Sstevel@tonic-gate 	char perm[OPT_LEN + 1];
15377c478bd9Sstevel@tonic-gate 	char own[OPT_LEN + 1];
15387c478bd9Sstevel@tonic-gate 	char grp[OPT_LEN + 1];
15397c478bd9Sstevel@tonic-gate 	char dumb[OPT_LEN + 1];
15407c478bd9Sstevel@tonic-gate 	int status = NOERR;
15417c478bd9Sstevel@tonic-gate 	int intperm;
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 	len = strlen(perm_list);
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 	if (len == 0) {
15467c478bd9Sstevel@tonic-gate 		return (ERROR);
15477c478bd9Sstevel@tonic-gate 	}
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	one_entry = calloc(len + 1, 1);
15507c478bd9Sstevel@tonic-gate 	if (one_entry == NULL) {
15517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
15527c478bd9Sstevel@tonic-gate 		return (ERROR);
15537c478bd9Sstevel@tonic-gate 	}
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	previous_head = perm_list;
15567c478bd9Sstevel@tonic-gate 	current_head = perm_list;
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	while (*current_head != '\0') {
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 		for (i = 0; i <= len; i++)
15617c478bd9Sstevel@tonic-gate 			one_entry[i] = 0;
15627c478bd9Sstevel@tonic-gate 
1563f4da9be0Scth 		current_head = get_entry(previous_head, one_entry, ',', 0);
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 		previous_head = current_head;
1566*2e107de7SJerry Gilliam 		/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
15677c478bd9Sstevel@tonic-gate 		scan_stat = sscanf(one_entry, "%s%s%s%s%s", minor, perm, own,
15687c478bd9Sstevel@tonic-gate 		    grp, dumb);
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate 		if (scan_stat < 4) {
15717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_MIS_TOK),
15727c478bd9Sstevel@tonic-gate 			    "-m", one_entry);
15737c478bd9Sstevel@tonic-gate 			status = ERROR;
15747c478bd9Sstevel@tonic-gate 		}
15757c478bd9Sstevel@tonic-gate 		if (scan_stat > 4) {
15767c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_TOO_MANY_ARGS),
15777c478bd9Sstevel@tonic-gate 			    "-m", one_entry);
15787c478bd9Sstevel@tonic-gate 			status = ERROR;
15797c478bd9Sstevel@tonic-gate 		}
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 		intperm = atoi(perm);
15827c478bd9Sstevel@tonic-gate 		if (intperm < 0000 || intperm > 4777) {
15837c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_BAD_MODE), perm);
15847c478bd9Sstevel@tonic-gate 			status = ERROR;
15857c478bd9Sstevel@tonic-gate 		}
15867c478bd9Sstevel@tonic-gate 	}
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 	free(one_entry);
15897c478bd9Sstevel@tonic-gate 	return (status);
15907c478bd9Sstevel@tonic-gate }
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate /*
15947c478bd9Sstevel@tonic-gate  * check each alias :
15957c478bd9Sstevel@tonic-gate  *	alias list members separated by white space
15967c478bd9Sstevel@tonic-gate  *	cannot exist as driver name in /etc/name_to_major
15977c478bd9Sstevel@tonic-gate  *	cannot exist as driver or alias name in /etc/driver_aliases
15987c478bd9Sstevel@tonic-gate  */
15997c478bd9Sstevel@tonic-gate int
16007c478bd9Sstevel@tonic-gate aliases_unique(char *aliases)
16017c478bd9Sstevel@tonic-gate {
16027c478bd9Sstevel@tonic-gate 	char *current_head;
16037c478bd9Sstevel@tonic-gate 	char *previous_head;
16047c478bd9Sstevel@tonic-gate 	char *one_entry;
16056532b960SJerry Gilliam 	int len;
16067c478bd9Sstevel@tonic-gate 	int is_unique;
16076532b960SJerry Gilliam 	int err;
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	len = strlen(aliases);
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	one_entry = calloc(len + 1, 1);
16127c478bd9Sstevel@tonic-gate 	if (one_entry == NULL) {
16137c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
16147c478bd9Sstevel@tonic-gate 		return (ERROR);
16157c478bd9Sstevel@tonic-gate 	}
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	previous_head = aliases;
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	do {
16206532b960SJerry Gilliam 		bzero(one_entry, len+1);
1621f4da9be0Scth 		current_head = get_entry(previous_head, one_entry, ' ', 1);
16227c478bd9Sstevel@tonic-gate 		previous_head = current_head;
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 		if ((unique_driver_name(one_entry, name_to_major,
16256532b960SJerry Gilliam 		    &is_unique)) == ERROR)
16266532b960SJerry Gilliam 			goto err_out;
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 		if (is_unique != UNIQUE) {
16297c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_ALIAS_IN_NAM_MAJ),
16307c478bd9Sstevel@tonic-gate 			    one_entry);
16316532b960SJerry Gilliam 			goto err_out;
16327c478bd9Sstevel@tonic-gate 		}
16337c478bd9Sstevel@tonic-gate 
16346532b960SJerry Gilliam 		if ((err = unique_drv_alias(one_entry)) != UNIQUE) {
16356532b960SJerry Gilliam 			if (err == NOT_UNIQUE) {
16366532b960SJerry Gilliam 				(void) fprintf(stderr,
16376532b960SJerry Gilliam 				    gettext(ERR_ALIAS_IN_USE), one_entry);
16386532b960SJerry Gilliam 			}
16396532b960SJerry Gilliam 			goto err_out;
16407c478bd9Sstevel@tonic-gate 		}
16417c478bd9Sstevel@tonic-gate 
16421ca93273Seota 		if (!is_token(one_entry)) {
16431ca93273Seota 			(void) fprintf(stderr, gettext(ERR_BAD_TOK),
16441ca93273Seota 			    "-i", one_entry);
16456532b960SJerry Gilliam 			goto err_out;
16461ca93273Seota 		}
16471ca93273Seota 
16487c478bd9Sstevel@tonic-gate 	} while (*current_head != '\0');
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	free(one_entry);
16517c478bd9Sstevel@tonic-gate 	return (NOERR);
16527c478bd9Sstevel@tonic-gate 
16536532b960SJerry Gilliam err_out:
16546532b960SJerry Gilliam 	free(one_entry);
16556532b960SJerry Gilliam 	return (ERROR);
16566532b960SJerry Gilliam }
16576532b960SJerry Gilliam 
16586532b960SJerry Gilliam /*
16596532b960SJerry Gilliam  * verify each alias :
16606532b960SJerry Gilliam  *	alias list members separated by white space and quoted
16616532b960SJerry Gilliam  *	exist as alias name in /etc/driver_aliases
16626532b960SJerry Gilliam  */
16636532b960SJerry Gilliam int
1664*2e107de7SJerry Gilliam aliases_exist(char *aliases)
16656532b960SJerry Gilliam {
16666532b960SJerry Gilliam 	char *current_head;
16676532b960SJerry Gilliam 	char *previous_head;
16686532b960SJerry Gilliam 	char *one_entry;
16696532b960SJerry Gilliam 	int len;
16706532b960SJerry Gilliam 
16716532b960SJerry Gilliam 	len = strlen(aliases);
16726532b960SJerry Gilliam 
16736532b960SJerry Gilliam 	one_entry = calloc(len + 1, 1);
16746532b960SJerry Gilliam 	if (one_entry == NULL) {
16756532b960SJerry Gilliam 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
16766532b960SJerry Gilliam 		return (ERROR);
16776532b960SJerry Gilliam 	}
16786532b960SJerry Gilliam 
16796532b960SJerry Gilliam 	previous_head = aliases;
16806532b960SJerry Gilliam 
16816532b960SJerry Gilliam 	do {
16826532b960SJerry Gilliam 		bzero(one_entry, len+1);
16836532b960SJerry Gilliam 		current_head = get_entry(previous_head, one_entry, ' ', 1);
16846532b960SJerry Gilliam 		previous_head = current_head;
16856532b960SJerry Gilliam 
1686*2e107de7SJerry Gilliam 		if (unique_drv_alias(one_entry) != NOT_UNIQUE)
16876532b960SJerry Gilliam 			goto err_out;
16886532b960SJerry Gilliam 
16896532b960SJerry Gilliam 		if (!is_token(one_entry)) {
16906532b960SJerry Gilliam 			(void) fprintf(stderr, gettext(ERR_BAD_TOK),
16916532b960SJerry Gilliam 			    "-i", one_entry);
16926532b960SJerry Gilliam 			goto err_out;
16936532b960SJerry Gilliam 		}
16946532b960SJerry Gilliam 
16956532b960SJerry Gilliam 	} while (*current_head != '\0');
16966532b960SJerry Gilliam 
16976532b960SJerry Gilliam 	free(one_entry);
16986532b960SJerry Gilliam 	return (NOERR);
16996532b960SJerry Gilliam 
17006532b960SJerry Gilliam err_out:
17016532b960SJerry Gilliam 	free(one_entry);
17026532b960SJerry Gilliam 	return (ERROR);
17037c478bd9Sstevel@tonic-gate }
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 
1706f4da9be0Scth /*
1707f4da9be0Scth  * check each alias :
1708f4da9be0Scth  *	if path-oriented alias, path exists
1709f4da9be0Scth  */
1710f4da9be0Scth int
1711f4da9be0Scth aliases_paths_exist(char *aliases)
1712f4da9be0Scth {
1713f4da9be0Scth 	char *current_head;
1714f4da9be0Scth 	char *previous_head;
1715f4da9be0Scth 	char *one_entry;
1716f4da9be0Scth 	int i, len;
1717f4da9be0Scth 	char path[MAXPATHLEN];
1718f4da9be0Scth 	struct stat buf;
1719f4da9be0Scth 
1720f4da9be0Scth 	len = strlen(aliases);
1721f4da9be0Scth 
1722f4da9be0Scth 	one_entry = calloc(len + 1, 1);
1723f4da9be0Scth 	if (one_entry == NULL) {
1724f4da9be0Scth 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
1725f4da9be0Scth 		return (ERROR);
1726f4da9be0Scth 	}
1727f4da9be0Scth 
1728f4da9be0Scth 	previous_head = aliases;
1729f4da9be0Scth 
1730f4da9be0Scth 	do {
1731f4da9be0Scth 		for (i = 0; i <= len; i++)
1732f4da9be0Scth 			one_entry[i] = 0;
1733f4da9be0Scth 
1734f4da9be0Scth 		current_head = get_entry(previous_head, one_entry, ' ', 1);
1735f4da9be0Scth 		previous_head = current_head;
1736f4da9be0Scth 
1737f4da9be0Scth 		/* if the alias is a path, ensure that the path exists */
1738f4da9be0Scth 		if (*one_entry != '/')
1739f4da9be0Scth 			continue;
1740f4da9be0Scth 		(void) snprintf(path, sizeof (path), "/devices/%s", one_entry);
1741f4da9be0Scth 		if (stat(path, &buf) == 0)
1742f4da9be0Scth 			continue;
1743f4da9be0Scth 
1744f4da9be0Scth 		/* no device at specified path-oriented alias path */
1745f4da9be0Scth 		(void) fprintf(stderr, gettext(ERR_PATH_ORIENTED_ALIAS),
1746f4da9be0Scth 		    one_entry);
1747f4da9be0Scth 		free(one_entry);
1748f4da9be0Scth 		return (ERROR);
1749f4da9be0Scth 
1750f4da9be0Scth 	} while (*current_head != '\0');
1751f4da9be0Scth 
1752f4da9be0Scth 	free(one_entry);
1753f4da9be0Scth 
1754f4da9be0Scth 	return (NOERR);
1755f4da9be0Scth }
1756f4da9be0Scth 
1757f4da9be0Scth 
17587c478bd9Sstevel@tonic-gate int
17597c478bd9Sstevel@tonic-gate update_driver_aliases(
17607c478bd9Sstevel@tonic-gate 	char *driver_name,
17617c478bd9Sstevel@tonic-gate 	char *aliases)
17627c478bd9Sstevel@tonic-gate {
17637c478bd9Sstevel@tonic-gate 	/* make call to update the aliases file */
1764f4da9be0Scth 	return (append_to_file(driver_name, aliases, driver_aliases,
1765f4da9be0Scth 	    ' ', " ", 1));
17667c478bd9Sstevel@tonic-gate }
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 
17696532b960SJerry Gilliam /*
17706532b960SJerry Gilliam  * Return:
17716532b960SJerry Gilliam  *	ERROR in case of memory or read error
17726532b960SJerry Gilliam  *	UNIQUE if there is no existing match to the supplied alias
17736532b960SJerry Gilliam  *	NOT_UNIQUE if there is a match
17746532b960SJerry Gilliam  * An error message is emitted in the case of ERROR,
17756532b960SJerry Gilliam  * up to the caller otherwise.
17766532b960SJerry Gilliam  */
17777c478bd9Sstevel@tonic-gate int
17787c478bd9Sstevel@tonic-gate unique_drv_alias(char *drv_alias)
17797c478bd9Sstevel@tonic-gate {
17807c478bd9Sstevel@tonic-gate 	FILE *fp;
17817c478bd9Sstevel@tonic-gate 	char drv[FILENAME_MAX + 1];
17821ca93273Seota 	char line[MAX_N2M_ALIAS_LINE + 1], *cp;
17837c478bd9Sstevel@tonic-gate 	char alias[FILENAME_MAX + 1];
1784f4da9be0Scth 	char *a;
17856532b960SJerry Gilliam 	int status = UNIQUE;
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 	fp = fopen(driver_aliases, "r");
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 	if (fp != NULL) {
17907c478bd9Sstevel@tonic-gate 		while ((fgets(line, sizeof (line), fp) != 0) &&
17916532b960SJerry Gilliam 		    status == UNIQUE) {
17921ca93273Seota 			/* cut off comments starting with '#' */
17931ca93273Seota 			if ((cp = strchr(line, '#')) != NULL)
17941ca93273Seota 				*cp = '\0';
17951ca93273Seota 			/* ignore comment or blank lines */
17961ca93273Seota 			if (is_blank(line))
17971ca93273Seota 				continue;
17981ca93273Seota 			/* sanity-check */
1799*2e107de7SJerry Gilliam 			/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
18007c478bd9Sstevel@tonic-gate 			if (sscanf(line, "%s %s", drv, alias) != 2)
18017c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_BAD_LINE),
18027c478bd9Sstevel@tonic-gate 				    driver_aliases, line);
18037c478bd9Sstevel@tonic-gate 
1804f4da9be0Scth 			/* unquote for compare */
1805f4da9be0Scth 			if ((*alias == '"') &&
1806f4da9be0Scth 			    (*(alias + strlen(alias) - 1) == '"')) {
1807f4da9be0Scth 				a = &alias[1];
1808f4da9be0Scth 				alias[strlen(alias) - 1] = '\0';
1809f4da9be0Scth 			} else
1810f4da9be0Scth 				a = alias;
1811f4da9be0Scth 
18127c478bd9Sstevel@tonic-gate 			if ((strcmp(drv_alias, drv) == 0) ||
1813f4da9be0Scth 			    (strcmp(drv_alias, a) == 0)) {
18146532b960SJerry Gilliam 				status = NOT_UNIQUE;
18156532b960SJerry Gilliam 				break;
18167c478bd9Sstevel@tonic-gate 			}
18177c478bd9Sstevel@tonic-gate 		}
18187c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
18197c478bd9Sstevel@tonic-gate 		return (status);
18207c478bd9Sstevel@tonic-gate 	} else {
18217c478bd9Sstevel@tonic-gate 		perror(NULL);
18227c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_OPEN), driver_aliases);
18237c478bd9Sstevel@tonic-gate 		return (ERROR);
18247c478bd9Sstevel@tonic-gate 	}
18257c478bd9Sstevel@tonic-gate }
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate /*
18297c478bd9Sstevel@tonic-gate  * search for driver_name in first field of file file_name
18306532b960SJerry Gilliam  * searching name_to_major and driver_aliases: name separated
18316532b960SJerry Gilliam  * from the remainder of the line by white space.
18327c478bd9Sstevel@tonic-gate  */
18337c478bd9Sstevel@tonic-gate int
18347c478bd9Sstevel@tonic-gate unique_driver_name(char *driver_name, char *file_name,
18357c478bd9Sstevel@tonic-gate 	int *is_unique)
18367c478bd9Sstevel@tonic-gate {
18376532b960SJerry Gilliam 	int ret, err;
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	if ((ret = get_major_no(driver_name, file_name)) == ERROR) {
18407c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
18417c478bd9Sstevel@tonic-gate 		    file_name);
18427c478bd9Sstevel@tonic-gate 	} else {
18437c478bd9Sstevel@tonic-gate 		/* check alias file for name collision */
18446532b960SJerry Gilliam 		if ((err = unique_drv_alias(driver_name)) != UNIQUE) {
18456532b960SJerry Gilliam 			if (err == NOT_UNIQUE) {
18466532b960SJerry Gilliam 				(void) fprintf(stderr,
18476532b960SJerry Gilliam 				    gettext(ERR_ALIAS_IN_USE),
18486532b960SJerry Gilliam 				    driver_name);
18496532b960SJerry Gilliam 			}
18507c478bd9Sstevel@tonic-gate 			ret = ERROR;
18517c478bd9Sstevel@tonic-gate 		} else {
18527c478bd9Sstevel@tonic-gate 			if (ret != UNIQUE)
18537c478bd9Sstevel@tonic-gate 				*is_unique = NOT_UNIQUE;
18547c478bd9Sstevel@tonic-gate 			else
18557c478bd9Sstevel@tonic-gate 				*is_unique = ret;
18567c478bd9Sstevel@tonic-gate 			ret = NOERR;
18577c478bd9Sstevel@tonic-gate 		}
18587c478bd9Sstevel@tonic-gate 	}
18597c478bd9Sstevel@tonic-gate 	return (ret);
18607c478bd9Sstevel@tonic-gate }
18617c478bd9Sstevel@tonic-gate 
18620013e2d3SJerry Gilliam /*
18630013e2d3SJerry Gilliam  * returns:
18640013e2d3SJerry Gilliam  *	SUCCESS - not an existing driver alias
18650013e2d3SJerry Gilliam  *	NOT_UNIQUE - matching driver alias exists
18660013e2d3SJerry Gilliam  *	ERROR - an error occurred
18670013e2d3SJerry Gilliam  */
18680013e2d3SJerry Gilliam int
18690013e2d3SJerry Gilliam check_duplicate_driver_alias(char *driver_name, char *drv_alias)
18700013e2d3SJerry Gilliam {
18710013e2d3SJerry Gilliam 	FILE *fp;
18720013e2d3SJerry Gilliam 	char drv[FILENAME_MAX + 1];
18730013e2d3SJerry Gilliam 	char line[MAX_N2M_ALIAS_LINE + 1], *cp;
18740013e2d3SJerry Gilliam 	char alias[FILENAME_MAX + 1];
18750013e2d3SJerry Gilliam 	char *a;
18760013e2d3SJerry Gilliam 	int status = SUCCESS;
18770013e2d3SJerry Gilliam 
18780013e2d3SJerry Gilliam 	if ((fp = fopen(driver_aliases, "r")) == NULL) {
18790013e2d3SJerry Gilliam 		perror(NULL);
18800013e2d3SJerry Gilliam 		(void) fprintf(stderr, gettext(ERR_CANT_OPEN), driver_aliases);
18810013e2d3SJerry Gilliam 		return (ERROR);
18820013e2d3SJerry Gilliam 	}
18830013e2d3SJerry Gilliam 
18840013e2d3SJerry Gilliam 	while (fgets(line, sizeof (line), fp) != 0) {
18850013e2d3SJerry Gilliam 		/* cut off comments starting with '#' */
18860013e2d3SJerry Gilliam 		if ((cp = strchr(line, '#')) != NULL)
18870013e2d3SJerry Gilliam 			*cp = '\0';
18880013e2d3SJerry Gilliam 		/* ignore comment or blank lines */
18890013e2d3SJerry Gilliam 		if (is_blank(line))
18900013e2d3SJerry Gilliam 			continue;
18910013e2d3SJerry Gilliam 		/* sanity-check */
1892*2e107de7SJerry Gilliam 		/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
18930013e2d3SJerry Gilliam 		if (sscanf(line, "%s %s", drv, alias) != 2)
18940013e2d3SJerry Gilliam 			(void) fprintf(stderr, gettext(ERR_BAD_LINE),
18950013e2d3SJerry Gilliam 			    driver_aliases, line);
18960013e2d3SJerry Gilliam 
18970013e2d3SJerry Gilliam 		/* unquote for compare */
18980013e2d3SJerry Gilliam 		if ((*alias == '"') &&
18990013e2d3SJerry Gilliam 		    (*(alias + strlen(alias) - 1) == '"')) {
19000013e2d3SJerry Gilliam 			a = &alias[1];
19010013e2d3SJerry Gilliam 			alias[strlen(alias) - 1] = '\0';
19020013e2d3SJerry Gilliam 		} else
19030013e2d3SJerry Gilliam 			a = alias;
19040013e2d3SJerry Gilliam 
19050013e2d3SJerry Gilliam 		if ((strcmp(drv_alias, a) == 0) &&
19060013e2d3SJerry Gilliam 		    (strcmp(drv, driver_name) == 0)) {
19070013e2d3SJerry Gilliam 			status = NOT_UNIQUE;
19080013e2d3SJerry Gilliam 		}
19090013e2d3SJerry Gilliam 
19100013e2d3SJerry Gilliam 		if ((strcmp(drv_alias, drv) == 0) ||
19110013e2d3SJerry Gilliam 		    ((strcmp(drv_alias, a) == 0) &&
19120013e2d3SJerry Gilliam 		    (strcmp(drv, driver_name) != 0))) {
19130013e2d3SJerry Gilliam 			(void) fprintf(stderr,
19140013e2d3SJerry Gilliam 			    gettext(ERR_ALIAS_IN_USE),
19150013e2d3SJerry Gilliam 			    drv_alias);
19160013e2d3SJerry Gilliam 			status = ERROR;
19170013e2d3SJerry Gilliam 			goto done;
19180013e2d3SJerry Gilliam 		}
19190013e2d3SJerry Gilliam 	}
19200013e2d3SJerry Gilliam 
19210013e2d3SJerry Gilliam done:
19220013e2d3SJerry Gilliam 	(void) fclose(fp);
19230013e2d3SJerry Gilliam 	return (status);
19240013e2d3SJerry Gilliam }
19250013e2d3SJerry Gilliam 
19260013e2d3SJerry Gilliam int
19270013e2d3SJerry Gilliam trim_duplicate_aliases(char *driver_name, char *aliases, char **aliases2p)
19280013e2d3SJerry Gilliam {
19290013e2d3SJerry Gilliam 	char *current_head;
19300013e2d3SJerry Gilliam 	char *previous_head;
19310013e2d3SJerry Gilliam 	char *one_entry;
19320013e2d3SJerry Gilliam 	char *aliases2;
19330013e2d3SJerry Gilliam 	int rv, len;
19340013e2d3SJerry Gilliam 	int n = 0;
19350013e2d3SJerry Gilliam 
19360013e2d3SJerry Gilliam 	*aliases2p = NULL;
19370013e2d3SJerry Gilliam 	len = strlen(aliases) + 1;
19380013e2d3SJerry Gilliam 
19390013e2d3SJerry Gilliam 	one_entry = calloc(len, 1);
19400013e2d3SJerry Gilliam 	aliases2 = calloc(len, 1);
19410013e2d3SJerry Gilliam 	if (one_entry == NULL || aliases2 == NULL) {
19420013e2d3SJerry Gilliam 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
19430013e2d3SJerry Gilliam 		return (ERROR);
19440013e2d3SJerry Gilliam 	}
19450013e2d3SJerry Gilliam 
19460013e2d3SJerry Gilliam 	previous_head = aliases;
19470013e2d3SJerry Gilliam 
19480013e2d3SJerry Gilliam 	do {
19490013e2d3SJerry Gilliam 		(void) bzero(one_entry, len);
19500013e2d3SJerry Gilliam 		current_head = get_entry(previous_head, one_entry, ' ', 1);
19510013e2d3SJerry Gilliam 		previous_head = current_head;
19520013e2d3SJerry Gilliam 
19530013e2d3SJerry Gilliam 		rv = check_duplicate_driver_alias(driver_name, one_entry);
19540013e2d3SJerry Gilliam 		switch (rv) {
19550013e2d3SJerry Gilliam 		case SUCCESS:
19560013e2d3SJerry Gilliam 			/* not an existing driver alias: add it */
19570013e2d3SJerry Gilliam 			if (n > 0) {
19580013e2d3SJerry Gilliam 				if (strlcat(aliases2, " ", len) >= len)
19590013e2d3SJerry Gilliam 					goto err;
19600013e2d3SJerry Gilliam 			}
19610013e2d3SJerry Gilliam 			if (strlcat(aliases2, one_entry, len) >= len)
19620013e2d3SJerry Gilliam 				goto err;
19630013e2d3SJerry Gilliam 			n++;
19640013e2d3SJerry Gilliam 			break;
19650013e2d3SJerry Gilliam 		case NOT_UNIQUE:
19660013e2d3SJerry Gilliam 			/* matching driver alias exists: do not add it */
19670013e2d3SJerry Gilliam 			break;
19680013e2d3SJerry Gilliam 		case ERROR:
19690013e2d3SJerry Gilliam 			/* error reading the alias file */
19700013e2d3SJerry Gilliam 			goto err;
19710013e2d3SJerry Gilliam 		default:
19720013e2d3SJerry Gilliam 			goto err;
19730013e2d3SJerry Gilliam 		}
19740013e2d3SJerry Gilliam 
19750013e2d3SJerry Gilliam 		if (!is_token(one_entry)) {
19760013e2d3SJerry Gilliam 			(void) fprintf(stderr, gettext(ERR_BAD_TOK),
19770013e2d3SJerry Gilliam 			    "-i", one_entry);
19780013e2d3SJerry Gilliam 			goto err;
19790013e2d3SJerry Gilliam 		}
19800013e2d3SJerry Gilliam 	} while (*current_head != '\0');
19810013e2d3SJerry Gilliam 
19820013e2d3SJerry Gilliam 	/*
19830013e2d3SJerry Gilliam 	 * If all the aliases listed are already
19840013e2d3SJerry Gilliam 	 * present we actually have none to do.
19850013e2d3SJerry Gilliam 	 */
19860013e2d3SJerry Gilliam 	if (n == 0) {
19870013e2d3SJerry Gilliam 		free(aliases2);
19880013e2d3SJerry Gilliam 	} else {
19890013e2d3SJerry Gilliam 		*aliases2p = aliases2;
19900013e2d3SJerry Gilliam 	}
19910013e2d3SJerry Gilliam 	free(one_entry);
19920013e2d3SJerry Gilliam 	return (NOERR);
19930013e2d3SJerry Gilliam 
19940013e2d3SJerry Gilliam err:
19950013e2d3SJerry Gilliam 	free(aliases2);
19960013e2d3SJerry Gilliam 	free(one_entry);
19970013e2d3SJerry Gilliam 	return (ERROR);
19980013e2d3SJerry Gilliam }
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate int
20017c478bd9Sstevel@tonic-gate check_space_within_quote(char *str)
20027c478bd9Sstevel@tonic-gate {
20037c478bd9Sstevel@tonic-gate 	register int i;
20047c478bd9Sstevel@tonic-gate 	register int len;
20057c478bd9Sstevel@tonic-gate 	int quoted = 0;
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	len = strlen(str);
20087c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++, str++) {
20097c478bd9Sstevel@tonic-gate 		if (*str == '"') {
20107c478bd9Sstevel@tonic-gate 			if (quoted == 0)
20117c478bd9Sstevel@tonic-gate 				quoted++;
20127c478bd9Sstevel@tonic-gate 			else
20137c478bd9Sstevel@tonic-gate 				quoted--;
20147c478bd9Sstevel@tonic-gate 		} else if (*str == ' ' && quoted)
20157c478bd9Sstevel@tonic-gate 			return (ERROR);
20167c478bd9Sstevel@tonic-gate 	}
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	return (0);
20197c478bd9Sstevel@tonic-gate }
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate /*
20237c478bd9Sstevel@tonic-gate  * get major number
20247c478bd9Sstevel@tonic-gate  * write driver_name major_num to name_to_major file
20257c478bd9Sstevel@tonic-gate  * major_num returned in major_num
20267c478bd9Sstevel@tonic-gate  * return success/failure
20277c478bd9Sstevel@tonic-gate  */
20287c478bd9Sstevel@tonic-gate int
20297c478bd9Sstevel@tonic-gate update_name_to_major(char *driver_name, major_t *major_num, int server)
20307c478bd9Sstevel@tonic-gate {
20317c478bd9Sstevel@tonic-gate 	char major[MAX_STR_MAJOR + 1];
20327c478bd9Sstevel@tonic-gate 	struct stat buf;
20337c478bd9Sstevel@tonic-gate 	char *num_list;
20347c478bd9Sstevel@tonic-gate 	char drv_majnum_str[MAX_STR_MAJOR + 1];
20357c478bd9Sstevel@tonic-gate 	int new_maj = -1;
20367c478bd9Sstevel@tonic-gate 	int i, tmp = 0, is_unique, have_rem_n2m = 0;
20377c478bd9Sstevel@tonic-gate 	int max_dev = 0;
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 	/*
20407c478bd9Sstevel@tonic-gate 	 * if driver_name already in rem_name_to_major
20417c478bd9Sstevel@tonic-gate 	 * 	delete entry from rem_nam_to_major
20427c478bd9Sstevel@tonic-gate 	 *	put entry into name_to_major
20437c478bd9Sstevel@tonic-gate 	 */
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	if (stat(rem_name_to_major, &buf) == 0) {
20467c478bd9Sstevel@tonic-gate 		have_rem_n2m = 1;
20477c478bd9Sstevel@tonic-gate 	}
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 	if (have_rem_n2m) {
20507c478bd9Sstevel@tonic-gate 		if ((is_unique = get_major_no(driver_name, rem_name_to_major))
20517c478bd9Sstevel@tonic-gate 		    == ERROR)
20527c478bd9Sstevel@tonic-gate 			return (ERROR);
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 		/*
20557c478bd9Sstevel@tonic-gate 		 * found a match in rem_name_to_major
20567c478bd9Sstevel@tonic-gate 		 */
20577c478bd9Sstevel@tonic-gate 		if (is_unique != UNIQUE) {
20587c478bd9Sstevel@tonic-gate 			char scratch[FILENAME_MAX];
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 			/*
20617c478bd9Sstevel@tonic-gate 			 * If there is a match in /etc/rem_name_to_major then
20627c478bd9Sstevel@tonic-gate 			 * be paranoid: is that major number already in
20637c478bd9Sstevel@tonic-gate 			 * /etc/name_to_major (potentially under another name)?
20647c478bd9Sstevel@tonic-gate 			 */
20657c478bd9Sstevel@tonic-gate 			if (get_driver_name(is_unique, name_to_major,
20667c478bd9Sstevel@tonic-gate 			    scratch) != UNIQUE) {
20677c478bd9Sstevel@tonic-gate 				/*
20687c478bd9Sstevel@tonic-gate 				 * nuke the rem_name_to_major entry-- it
20697c478bd9Sstevel@tonic-gate 				 * isn't helpful.
20707c478bd9Sstevel@tonic-gate 				 */
20717c478bd9Sstevel@tonic-gate 				(void) delete_entry(rem_name_to_major,
20727c478bd9Sstevel@tonic-gate 				    driver_name, " ", NULL);
20737c478bd9Sstevel@tonic-gate 			} else {
20747c478bd9Sstevel@tonic-gate 				(void) snprintf(major, sizeof (major),
20757c478bd9Sstevel@tonic-gate 				    "%d", is_unique);
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 				if (append_to_file(driver_name, major,
2078f4da9be0Scth 				    name_to_major, ' ', " ", 0) == ERROR) {
20797c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
20807c478bd9Sstevel@tonic-gate 					    gettext(ERR_NO_UPDATE),
20817c478bd9Sstevel@tonic-gate 					    name_to_major);
20827c478bd9Sstevel@tonic-gate 					return (ERROR);
20837c478bd9Sstevel@tonic-gate 				}
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate 				if (delete_entry(rem_name_to_major,
20867c478bd9Sstevel@tonic-gate 				    driver_name, " ", NULL) == ERROR) {
20877c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
20887c478bd9Sstevel@tonic-gate 					    gettext(ERR_DEL_ENTRY), driver_name,
20897c478bd9Sstevel@tonic-gate 					    rem_name_to_major);
20907c478bd9Sstevel@tonic-gate 					return (ERROR);
20917c478bd9Sstevel@tonic-gate 				}
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 				/* found matching entry : no errors */
20947c478bd9Sstevel@tonic-gate 				*major_num = is_unique;
20957c478bd9Sstevel@tonic-gate 				return (NOERR);
20967c478bd9Sstevel@tonic-gate 			}
20977c478bd9Sstevel@tonic-gate 		}
20987c478bd9Sstevel@tonic-gate 	}
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 	/*
21017c478bd9Sstevel@tonic-gate 	 * Bugid: 1264079
21027c478bd9Sstevel@tonic-gate 	 * In a server case (with -b option), we can't use modctl() to find
21037c478bd9Sstevel@tonic-gate 	 *    the maximum major number, we need to dig thru client's
21047c478bd9Sstevel@tonic-gate 	 *    /etc/name_to_major and /etc/rem_name_to_major for the max_dev.
21057c478bd9Sstevel@tonic-gate 	 *
21067c478bd9Sstevel@tonic-gate 	 * if (server)
21077c478bd9Sstevel@tonic-gate 	 *    get maximum major number thru (rem_)name_to_major file on client
21087c478bd9Sstevel@tonic-gate 	 * else
21097c478bd9Sstevel@tonic-gate 	 *    get maximum major number allowable on current system using modctl
21107c478bd9Sstevel@tonic-gate 	 */
21117c478bd9Sstevel@tonic-gate 	if (server) {
21127c478bd9Sstevel@tonic-gate 		max_dev = 0;
21137c478bd9Sstevel@tonic-gate 		tmp = 0;
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 		max_dev = get_max_major(name_to_major);
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 		/* If rem_name_to_major exists, we need to check it too */
21187c478bd9Sstevel@tonic-gate 		if (have_rem_n2m) {
21197c478bd9Sstevel@tonic-gate 			tmp = get_max_major(rem_name_to_major);
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 			/*
21227c478bd9Sstevel@tonic-gate 			 * If name_to_major is missing, we can get max_dev from
21237c478bd9Sstevel@tonic-gate 			 * /etc/rem_name_to_major.  If both missing, bail out!
21247c478bd9Sstevel@tonic-gate 			 */
21257c478bd9Sstevel@tonic-gate 			if ((max_dev == ERROR) && (tmp == ERROR)) {
21267c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
21277c478bd9Sstevel@tonic-gate 				    gettext(ERR_CANT_ACCESS_FILE),
21287c478bd9Sstevel@tonic-gate 				    name_to_major);
21297c478bd9Sstevel@tonic-gate 				return (ERROR);
21307c478bd9Sstevel@tonic-gate 			}
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 			/* guard against bigger maj_num in rem_name_to_major */
21337c478bd9Sstevel@tonic-gate 			if (tmp > max_dev)
21347c478bd9Sstevel@tonic-gate 				max_dev = tmp;
21357c478bd9Sstevel@tonic-gate 		} else {
21367c478bd9Sstevel@tonic-gate 			/*
21377c478bd9Sstevel@tonic-gate 			 * If we can't get major from name_to_major file
21387c478bd9Sstevel@tonic-gate 			 * and there is no /etc/rem_name_to_major file,
21397c478bd9Sstevel@tonic-gate 			 * then we don't have a max_dev, bail out quick!
21407c478bd9Sstevel@tonic-gate 			 */
21417c478bd9Sstevel@tonic-gate 			if (max_dev == ERROR)
21427c478bd9Sstevel@tonic-gate 				return (ERROR);
21437c478bd9Sstevel@tonic-gate 		}
21447c478bd9Sstevel@tonic-gate 
21457c478bd9Sstevel@tonic-gate 		/*
21467c478bd9Sstevel@tonic-gate 		 * In case there is no more slack in current name_to_major
21477c478bd9Sstevel@tonic-gate 		 * table, provide at least 1 extra entry so the add_drv can
21487c478bd9Sstevel@tonic-gate 		 * succeed.  Since only one add_drv process is allowed at one
21497c478bd9Sstevel@tonic-gate 		 * time, and hence max_dev will be re-calculated each time
21507c478bd9Sstevel@tonic-gate 		 * add_drv is ran, we don't need to worry about adding more
21517c478bd9Sstevel@tonic-gate 		 * than 1 extra slot for max_dev.
21527c478bd9Sstevel@tonic-gate 		 */
21537c478bd9Sstevel@tonic-gate 		max_dev++;
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 	} else {
21567c478bd9Sstevel@tonic-gate 		if (modctl(MODRESERVED, NULL, &max_dev) < 0) {
21577c478bd9Sstevel@tonic-gate 			perror(NULL);
21587c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_MAX_MAJOR));
21597c478bd9Sstevel@tonic-gate 			return (ERROR);
21607c478bd9Sstevel@tonic-gate 		}
21617c478bd9Sstevel@tonic-gate 	}
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	/*
21647c478bd9Sstevel@tonic-gate 	 * max_dev is really how many slots the kernel has allocated for
21657c478bd9Sstevel@tonic-gate 	 * devices... [0 , maxdev-1], not the largest available device num.
21667c478bd9Sstevel@tonic-gate 	 */
21677c478bd9Sstevel@tonic-gate 	if ((num_list = calloc(max_dev, 1)) == NULL) {
21687c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
21697c478bd9Sstevel@tonic-gate 		return (ERROR);
21707c478bd9Sstevel@tonic-gate 	}
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate 	/*
21737c478bd9Sstevel@tonic-gate 	 * Populate the num_list array
21747c478bd9Sstevel@tonic-gate 	 */
21757c478bd9Sstevel@tonic-gate 	if (fill_n2m_array(name_to_major, &num_list, &max_dev) != 0) {
21767c478bd9Sstevel@tonic-gate 		return (ERROR);
21777c478bd9Sstevel@tonic-gate 	}
21787c478bd9Sstevel@tonic-gate 	if (have_rem_n2m) {
21797c478bd9Sstevel@tonic-gate 		if (fill_n2m_array(rem_name_to_major, &num_list, &max_dev) != 0)
21807c478bd9Sstevel@tonic-gate 			return (ERROR);
21817c478bd9Sstevel@tonic-gate 	}
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	/* find first free major number */
21847c478bd9Sstevel@tonic-gate 	for (i = 0; i < max_dev; i++) {
21857c478bd9Sstevel@tonic-gate 		if (num_list[i] != 1) {
21867c478bd9Sstevel@tonic-gate 			new_maj = i;
21877c478bd9Sstevel@tonic-gate 			break;
21887c478bd9Sstevel@tonic-gate 		}
21897c478bd9Sstevel@tonic-gate 	}
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate 	if (new_maj == -1) {
21927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_FREE_MAJOR));
21937c478bd9Sstevel@tonic-gate 		return (ERROR);
21947c478bd9Sstevel@tonic-gate 	}
21957c478bd9Sstevel@tonic-gate 
2196*2e107de7SJerry Gilliam 	(void) snprintf(drv_majnum_str, sizeof (drv_majnum_str),
2197*2e107de7SJerry Gilliam 	    "%d", new_maj);
21987c478bd9Sstevel@tonic-gate 	if (do_the_update(driver_name, drv_majnum_str) == ERROR) {
21997c478bd9Sstevel@tonic-gate 		return (ERROR);
22007c478bd9Sstevel@tonic-gate 	}
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 	*major_num = new_maj;
22037c478bd9Sstevel@tonic-gate 	return (NOERR);
22047c478bd9Sstevel@tonic-gate }
22057c478bd9Sstevel@tonic-gate 
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate int
22087c478bd9Sstevel@tonic-gate fill_n2m_array(char *filename, char **array, int *nelems)
22097c478bd9Sstevel@tonic-gate {
22107c478bd9Sstevel@tonic-gate 	FILE *fp;
22111ca93273Seota 	char line[MAX_N2M_ALIAS_LINE + 1], *cp;
22127c478bd9Sstevel@tonic-gate 	char drv[FILENAME_MAX + 1];
22137c478bd9Sstevel@tonic-gate 	u_longlong_t dnum;
22147c478bd9Sstevel@tonic-gate 	major_t drv_majnum;
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	/*
22177c478bd9Sstevel@tonic-gate 	 * Read through the file, marking each major number found
22187c478bd9Sstevel@tonic-gate 	 * order is not relevant
22197c478bd9Sstevel@tonic-gate 	 */
22207c478bd9Sstevel@tonic-gate 	if ((fp = fopen(filename, "r")) == NULL) {
22217c478bd9Sstevel@tonic-gate 		perror(NULL);
22227c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), filename);
22237c478bd9Sstevel@tonic-gate 		return (ERROR);
22247c478bd9Sstevel@tonic-gate 	}
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line), fp) != 0) {
22271ca93273Seota 		/* cut off comments starting with '#' */
22281ca93273Seota 		if ((cp = strchr(line, '#')) != NULL)
22291ca93273Seota 			*cp = '\0';
22301ca93273Seota 		/* ignore comment or blank lines */
22311ca93273Seota 		if (is_blank(line))
22321ca93273Seota 			continue;
22331ca93273Seota 		/* sanity-check */
2234*2e107de7SJerry Gilliam 		/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
22357c478bd9Sstevel@tonic-gate 		if (sscanf(line, "%s %llu", drv, &dnum) != 2) {
22367c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_BAD_LINE),
22377c478bd9Sstevel@tonic-gate 			    filename, line);
22387c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
22397c478bd9Sstevel@tonic-gate 			return (ERROR);
22407c478bd9Sstevel@tonic-gate 		}
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 		if (dnum > L_MAXMAJ32) {
22437c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_MAJ_TOOBIG), drv,
22447c478bd9Sstevel@tonic-gate 			    dnum, filename, L_MAXMAJ32);
22457c478bd9Sstevel@tonic-gate 			continue;
22467c478bd9Sstevel@tonic-gate 		}
22477c478bd9Sstevel@tonic-gate 		/*
22487c478bd9Sstevel@tonic-gate 		 * cast down to a major_t; we can be sure this is safe because
22497c478bd9Sstevel@tonic-gate 		 * of the above range-check.
22507c478bd9Sstevel@tonic-gate 		 */
22517c478bd9Sstevel@tonic-gate 		drv_majnum = (major_t)dnum;
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate 		if (drv_majnum >= *nelems) {
22547c478bd9Sstevel@tonic-gate 			/*
22557c478bd9Sstevel@tonic-gate 			 * Allocate some more space, up to drv_majnum + 1 so
22567c478bd9Sstevel@tonic-gate 			 * we can accomodate 0 through drv_majnum.
22577c478bd9Sstevel@tonic-gate 			 *
22587c478bd9Sstevel@tonic-gate 			 * Note that in the failure case, we leak all of the
22597c478bd9Sstevel@tonic-gate 			 * old contents of array.  It's ok, since we just
22607c478bd9Sstevel@tonic-gate 			 * wind up exiting immediately anyway.
22617c478bd9Sstevel@tonic-gate 			 */
22627c478bd9Sstevel@tonic-gate 			*nelems = drv_majnum + 1;
22637c478bd9Sstevel@tonic-gate 			*array = realloc(*array, *nelems);
22647c478bd9Sstevel@tonic-gate 			if (*array == NULL) {
22657c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_NO_MEM));
22667c478bd9Sstevel@tonic-gate 				return (ERROR);
22677c478bd9Sstevel@tonic-gate 			}
22687c478bd9Sstevel@tonic-gate 		}
22697c478bd9Sstevel@tonic-gate 		(*array)[drv_majnum] = 1;
22707c478bd9Sstevel@tonic-gate 	}
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
22737c478bd9Sstevel@tonic-gate 	return (0);
22747c478bd9Sstevel@tonic-gate }
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate int
22787c478bd9Sstevel@tonic-gate do_the_update(char *driver_name, char *major_number)
22797c478bd9Sstevel@tonic-gate {
22807c478bd9Sstevel@tonic-gate 	return (append_to_file(driver_name, major_number, name_to_major,
2281f4da9be0Scth 	    ' ', " ", 0));
22827c478bd9Sstevel@tonic-gate }
22831ca93273Seota 
22841ca93273Seota /*
22851ca93273Seota  * is_blank() returns 1 (true) if a line specified is composed of
22861ca93273Seota  * whitespace characters only. otherwise, it returns 0 (false).
22871ca93273Seota  *
22881ca93273Seota  * Note. the argument (line) must be null-terminated.
22891ca93273Seota  */
22901ca93273Seota static int
22911ca93273Seota is_blank(char *line)
22921ca93273Seota {
22931ca93273Seota 	for (/* nothing */; *line != '\0'; line++)
22941ca93273Seota 		if (!isspace(*line))
22951ca93273Seota 			return (0);
22961ca93273Seota 	return (1);
22971ca93273Seota }
2298