xref: /titanic_53/usr/src/cmd/modload/drvsubr.c (revision f8cb811f5f2909e15e8d1f21f046ac459d0640e5)
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 {
111*f8cb811fSEdward Pilatowicz 	int	i, len, line_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 
136*f8cb811fSEdward Pilatowicz 	line_len = strlen(driver_name) + len + 4;
137*f8cb811fSEdward Pilatowicz 	if (quoted)
138*f8cb811fSEdward Pilatowicz 		line_len += 2;
139*f8cb811fSEdward Pilatowicz 
140*f8cb811fSEdward Pilatowicz 	line = calloc(line_len, 1);
1417c478bd9Sstevel@tonic-gate 	if (line == NULL) {
1427c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
1437c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
1447c478bd9Sstevel@tonic-gate 		err_exit();
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/*
1487c478bd9Sstevel@tonic-gate 	 * get one entry at a time from list and append to <filename> file
1497c478bd9Sstevel@tonic-gate 	 */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	do {
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		for (i = 0; i <= len; i++)
1547c478bd9Sstevel@tonic-gate 			one_entry[i] = 0;
1557c478bd9Sstevel@tonic-gate 
156*f8cb811fSEdward Pilatowicz 		bzero(line, line_len);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 		current_head = get_entry(previous_head, one_entry,
159f4da9be0Scth 		    list_separator, quoted);
1607c478bd9Sstevel@tonic-gate 		previous_head = current_head;
1617c478bd9Sstevel@tonic-gate 
162*f8cb811fSEdward Pilatowicz 		(void) strlcpy(line, driver_name, line_len);
163*f8cb811fSEdward Pilatowicz 		(void) strlcat(line, entry_separator, line_len);
164f4da9be0Scth 		if (quoted)
165*f8cb811fSEdward Pilatowicz 			(void) strlcat(line, "\"", line_len);
166*f8cb811fSEdward Pilatowicz 		(void) strlcat(line, one_entry, line_len);
167f4da9be0Scth 		if (quoted)
168*f8cb811fSEdward Pilatowicz 			(void) strlcat(line, "\"", line_len);
169*f8cb811fSEdward Pilatowicz 		(void) strlcat(line, "\n", line_len);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 		if ((fputs(line, fp)) == EOF) {
1727c478bd9Sstevel@tonic-gate 			perror(NULL);
1737c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_UPDATE),
1747c478bd9Sstevel@tonic-gate 			    filename);
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	} while (*current_head != '\0');
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	(void) fflush(fp);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	fpint = fileno(fp);
1837c478bd9Sstevel@tonic-gate 	(void) fsync(fpint);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	free(one_entry);
1887c478bd9Sstevel@tonic-gate 	free(line);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	return (NOERR);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
19311ebc2d0SJerry Gilliam /*
19411ebc2d0SJerry Gilliam  * Require exact match to delete a driver alias/permission entry.
19511ebc2d0SJerry Gilliam  * Note line argument does not remain unchanged.  Return 1 if matched.
19611ebc2d0SJerry Gilliam  */
19711ebc2d0SJerry Gilliam static int
19811ebc2d0SJerry Gilliam match_entry(char *line, char *match)
19911ebc2d0SJerry Gilliam {
20011ebc2d0SJerry Gilliam 	char	*token, *p;
20111ebc2d0SJerry Gilliam 	int	n;
20211ebc2d0SJerry Gilliam 
20311ebc2d0SJerry Gilliam 	/* skip any leading white space */
20411ebc2d0SJerry Gilliam 	while (*line && ((*line == ' ') || (*line == '\t')))
20511ebc2d0SJerry Gilliam 		line++;
20611ebc2d0SJerry Gilliam 	/*
20711ebc2d0SJerry Gilliam 	 * Find separator for driver name, either space or colon
20811ebc2d0SJerry Gilliam 	 *	minor_perm: <driver>:<perm>
20911ebc2d0SJerry Gilliam 	 *	driver_aliases: <driver> <alias>
21011ebc2d0SJerry Gilliam 	 *	extra_privs: <driver>:<priv>
21111ebc2d0SJerry Gilliam 	 */
21211ebc2d0SJerry Gilliam 	if ((token = strpbrk(line, " :\t")) == NULL)
21311ebc2d0SJerry Gilliam 		return (0);
21411ebc2d0SJerry Gilliam 	token++;
21511ebc2d0SJerry Gilliam 	/* skip leading white space and quotes */
21611ebc2d0SJerry Gilliam 	while (*token && (*token == ' ' || *token == '\t' ||
21711ebc2d0SJerry Gilliam 	    *token == '"' || *token == '\''))
21811ebc2d0SJerry Gilliam 		token++;
21911ebc2d0SJerry Gilliam 	/* strip trailing newline, white space and quotes */
22011ebc2d0SJerry Gilliam 	n = strlen(token);
22111ebc2d0SJerry Gilliam 	p = token + n-1;
22211ebc2d0SJerry Gilliam 	while (n > 0 && (*p == '\n' || *p == ' ' || *p == '\t' ||
22311ebc2d0SJerry Gilliam 	    *p == '"' || *p == '\'')) {
22411ebc2d0SJerry Gilliam 		*p-- = 0;
22511ebc2d0SJerry Gilliam 		n--;
22611ebc2d0SJerry Gilliam 	}
22711ebc2d0SJerry Gilliam 	if (n == 0)
22811ebc2d0SJerry Gilliam 		return (0);
22911ebc2d0SJerry Gilliam 	return (strcmp(token, match) == 0);
23011ebc2d0SJerry Gilliam }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate /*
2337c478bd9Sstevel@tonic-gate  *  open file
2347c478bd9Sstevel@tonic-gate  * read thru file, deleting all entries if first
2357c478bd9Sstevel@tonic-gate  *    entry = driver_name
2367c478bd9Sstevel@tonic-gate  * close
2377c478bd9Sstevel@tonic-gate  * if error, leave original file intact with message
2387c478bd9Sstevel@tonic-gate  * assumption : drvconfig has been modified to work with clone
2397c478bd9Sstevel@tonic-gate  *  entries in /etc/minor_perm as driver:mummble NOT
2407c478bd9Sstevel@tonic-gate  *  clone:driver mummble
2417c478bd9Sstevel@tonic-gate  * this implementation will NOT find clone entries
2427c478bd9Sstevel@tonic-gate  * clone:driver mummble
2437c478bd9Sstevel@tonic-gate  * match:
2447c478bd9Sstevel@tonic-gate  *	delete just the matching entry
2457c478bd9Sstevel@tonic-gate  *
2467c478bd9Sstevel@tonic-gate  */
2477c478bd9Sstevel@tonic-gate int
2487c478bd9Sstevel@tonic-gate delete_entry(
2497c478bd9Sstevel@tonic-gate 	char *oldfile,
2507c478bd9Sstevel@tonic-gate 	char *driver_name,
2517c478bd9Sstevel@tonic-gate 	char *marker,
2527c478bd9Sstevel@tonic-gate 	char *match)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	int		rv, i;
2557c478bd9Sstevel@tonic-gate 	int		status = NOERR;
2567c478bd9Sstevel@tonic-gate 	int		drvr_found = 0;
2577c478bd9Sstevel@tonic-gate 	boolean_t 	nomatch = B_TRUE;
25811ebc2d0SJerry Gilliam 	char		*newfile, *tptr, *cp;
2597c478bd9Sstevel@tonic-gate 	char		line[MAX_DBFILE_ENTRY], drv[FILENAME_MAX + 1];
2607c478bd9Sstevel@tonic-gate 	FILE		*fp, *newfp;
2617c478bd9Sstevel@tonic-gate 	struct group	*sysgrp;
26211ebc2d0SJerry Gilliam 	char		*copy;		/* same size as line */
2632e107de7SJerry Gilliam 	char		*match2 = NULL;	/* match with quotes cleaned up */
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	/*
2662e107de7SJerry Gilliam 	 * if match is specified, sanity check it and clean it
2672e107de7SJerry Gilliam 	 * up by removing surrounding quotes as we require
2682e107de7SJerry Gilliam 	 * an exact match.
2697c478bd9Sstevel@tonic-gate 	 */
2702e107de7SJerry Gilliam 	if (match) {
2712e107de7SJerry Gilliam 		cp = match;
2722e107de7SJerry Gilliam 		while (*cp && (*cp == '"' || *cp == '\''))
2732e107de7SJerry Gilliam 			cp++;
2742e107de7SJerry Gilliam 		i = strlen(cp);
2752e107de7SJerry Gilliam 		if (i > 0) {
2762e107de7SJerry Gilliam 			if ((match2 = strdup(cp)) == NULL) {
2772e107de7SJerry Gilliam 				perror(NULL);
2782e107de7SJerry Gilliam 				(void) fprintf(stderr, gettext(ERR_NO_MEM));
2797c478bd9Sstevel@tonic-gate 				return (ERROR);
2807c478bd9Sstevel@tonic-gate 			}
2812e107de7SJerry Gilliam 			if ((cp = strchr(match2, '\'')) != NULL)
2822e107de7SJerry Gilliam 				*cp = 0;
2832e107de7SJerry Gilliam 			if ((cp = strchr(match2, '"')) != NULL)
2842e107de7SJerry Gilliam 				*cp = 0;
2852e107de7SJerry Gilliam 			if ((cp = strchr(match2, ' ')) != NULL)
2862e107de7SJerry Gilliam 				*cp = 0;
2872e107de7SJerry Gilliam 		}
2882e107de7SJerry Gilliam 		if (match2 == NULL || (strlen(match2) == 0)) {
2892e107de7SJerry Gilliam 			(void) fprintf(stderr,
2902e107de7SJerry Gilliam 			    gettext(ERR_INT_UPDATE), oldfile);
2912e107de7SJerry Gilliam 			return (ERROR);
2922e107de7SJerry Gilliam 		}
2932e107de7SJerry Gilliam 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	if ((fp = fopen(oldfile, "r")) == NULL) {
2967c478bd9Sstevel@tonic-gate 		perror(NULL);
2977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), oldfile);
2987c478bd9Sstevel@tonic-gate 		return (ERROR);
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 
30111ebc2d0SJerry Gilliam 	/* Space for defensive copy of input line */
30211ebc2d0SJerry Gilliam 	copy = calloc(sizeof (line), 1);
3037c478bd9Sstevel@tonic-gate 
30411ebc2d0SJerry Gilliam 	/* Build filename for temporary file */
30511ebc2d0SJerry Gilliam 	tptr = calloc(strlen(oldfile) + strlen(XEND) + 1, 1);
30611ebc2d0SJerry Gilliam 	if (tptr == NULL || copy == NULL) {
3077c478bd9Sstevel@tonic-gate 		perror(NULL);
3087c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
30911ebc2d0SJerry Gilliam 		return (ERROR);
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	(void) strcpy(tptr, oldfile);
3137c478bd9Sstevel@tonic-gate 	(void) strcat(tptr, XEND);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	/*
3167c478bd9Sstevel@tonic-gate 	 * Set gid so we preserve group attribute.  Ideally we wouldn't
3177c478bd9Sstevel@tonic-gate 	 * assume a gid of "sys" but we can't undo the damage on already
3187c478bd9Sstevel@tonic-gate 	 * installed systems unless we force the issue.
3197c478bd9Sstevel@tonic-gate 	 */
3207c478bd9Sstevel@tonic-gate 	if ((sysgrp = getgrnam("sys")) != NULL) {
3217c478bd9Sstevel@tonic-gate 		(void) setgid(sysgrp->gr_gid);
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	newfile = mktemp(tptr);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	if ((newfp = fopen(newfile, "w")) == NULL) {
3277c478bd9Sstevel@tonic-gate 		perror(NULL);
3287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
3297c478bd9Sstevel@tonic-gate 		    newfile);
3307c478bd9Sstevel@tonic-gate 		return (ERROR);
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	while ((fgets(line, sizeof (line), fp) != NULL) && status == NOERR) {
33411ebc2d0SJerry Gilliam 		/* copy the whole line */
33511ebc2d0SJerry Gilliam 		if (strlcpy(copy, line, sizeof (line)) >= sizeof (line)) {
33611ebc2d0SJerry Gilliam 			(void) fprintf(stderr, gettext(ERR_UPDATE), oldfile);
3371ca93273Seota 			status = ERROR;
3381ca93273Seota 			break;
3391ca93273Seota 		}
3401ca93273Seota 		/* cut off comments starting with '#' */
34111ebc2d0SJerry Gilliam 		if ((cp = strchr(copy, '#')) != NULL)
3421ca93273Seota 			*cp = '\0';
3431ca93273Seota 		/* ignore comment or blank lines */
34411ebc2d0SJerry Gilliam 		if (is_blank(copy)) {
3451ca93273Seota 			if (fputs(line, newfp) == EOF) {
3467c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_UPDATE),
3477c478bd9Sstevel@tonic-gate 				    oldfile);
3487c478bd9Sstevel@tonic-gate 				status = ERROR;
3497c478bd9Sstevel@tonic-gate 			}
3507c478bd9Sstevel@tonic-gate 			continue;
3517c478bd9Sstevel@tonic-gate 		}
3521ca93273Seota 
3531ca93273Seota 		/* get the driver name */
3542e107de7SJerry Gilliam 		/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
35511ebc2d0SJerry Gilliam 		if (sscanf(copy, "%s", drv) != 1) {
3567c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_BAD_LINE),
3577c478bd9Sstevel@tonic-gate 			    oldfile, line);
3587c478bd9Sstevel@tonic-gate 			status = ERROR;
3591ca93273Seota 			break;
3607c478bd9Sstevel@tonic-gate 		}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 		for (i = strcspn(drv, marker); i < FILENAME_MAX; i++) {
3637c478bd9Sstevel@tonic-gate 			drv[i] =  '\0';
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 		if (strcmp(driver_name, drv) != 0) {
3677c478bd9Sstevel@tonic-gate 			if ((fputs(line, newfp)) == EOF) {
3687c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_UPDATE),
3697c478bd9Sstevel@tonic-gate 				    oldfile);
3707c478bd9Sstevel@tonic-gate 				status = ERROR;
3717c478bd9Sstevel@tonic-gate 			}
3727c478bd9Sstevel@tonic-gate 		} else {
3737c478bd9Sstevel@tonic-gate 			drvr_found++;
3742e107de7SJerry Gilliam 			if (match2) {	/* Just delete one entry */
3757c478bd9Sstevel@tonic-gate 				/* for now delete just minor_perm and aliases */
3767c478bd9Sstevel@tonic-gate 				if ((strcmp(oldfile, minor_perm) == 0) ||
3777c478bd9Sstevel@tonic-gate 				    (strcmp(oldfile, extra_privs) == 0) ||
3787c478bd9Sstevel@tonic-gate 				    (strcmp(oldfile, driver_aliases) == 0)) {
37911ebc2d0SJerry Gilliam 
38011ebc2d0SJerry Gilliam 					/* make defensive copy */
38111ebc2d0SJerry Gilliam 					if (strlcpy(copy, line, sizeof (line))
38211ebc2d0SJerry Gilliam 					    >= sizeof (line)) {
38311ebc2d0SJerry Gilliam 						(void) fprintf(stderr,
38411ebc2d0SJerry Gilliam 						    gettext(ERR_UPDATE),
38511ebc2d0SJerry Gilliam 						    oldfile);
38611ebc2d0SJerry Gilliam 						status = ERROR;
38711ebc2d0SJerry Gilliam 						break;
38811ebc2d0SJerry Gilliam 					}
3892e107de7SJerry Gilliam 					if (match_entry(copy, match2)) {
3907c478bd9Sstevel@tonic-gate 						nomatch = B_FALSE;
3917c478bd9Sstevel@tonic-gate 					} else {
3927c478bd9Sstevel@tonic-gate 						if ((fputs(line, newfp)) ==
3937c478bd9Sstevel@tonic-gate 						    EOF) {
3947c478bd9Sstevel@tonic-gate 							(void) fprintf(stderr,
3957c478bd9Sstevel@tonic-gate 							    gettext(ERR_UPDATE),
3967c478bd9Sstevel@tonic-gate 							    oldfile);
3977c478bd9Sstevel@tonic-gate 							status = ERROR;
3987c478bd9Sstevel@tonic-gate 						}
3997c478bd9Sstevel@tonic-gate 						if (nomatch != B_FALSE)
4007c478bd9Sstevel@tonic-gate 							nomatch = B_TRUE;
4017c478bd9Sstevel@tonic-gate 					}
4027c478bd9Sstevel@tonic-gate 				}
4037c478bd9Sstevel@tonic-gate 			}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 		} /* end of else */
4067c478bd9Sstevel@tonic-gate 	} /* end of while */
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
40911ebc2d0SJerry Gilliam 	free(tptr);
41011ebc2d0SJerry Gilliam 	free(copy);
4112e107de7SJerry Gilliam 	if (match2)
4122e107de7SJerry Gilliam 		free(match2);
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	/* Make sure that the file is on disk */
4157c478bd9Sstevel@tonic-gate 	if (fflush(newfp) != 0 || fsync(fileno(newfp)) != 0)
4167c478bd9Sstevel@tonic-gate 		status = ERROR;
4177c478bd9Sstevel@tonic-gate 	else
4187c478bd9Sstevel@tonic-gate 		rv = NOERR;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	(void) fclose(newfp);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	/* no matching driver found */
4237c478bd9Sstevel@tonic-gate 	rv = NOERR;
4247c478bd9Sstevel@tonic-gate 	if (!drvr_found ||
4257c478bd9Sstevel@tonic-gate 	    (nomatch == B_TRUE)) {
4267c478bd9Sstevel@tonic-gate 		rv = NONE_FOUND;
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	/*
4307c478bd9Sstevel@tonic-gate 	 * if error, leave original file, delete new file
4317c478bd9Sstevel@tonic-gate 	 * if noerr, replace original file with new file
4327c478bd9Sstevel@tonic-gate 	 */
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	if (status == NOERR) {
4357c478bd9Sstevel@tonic-gate 		if (rename(oldfile, tmphold) == -1) {
4367c478bd9Sstevel@tonic-gate 			perror(NULL);
4377c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE), oldfile);
4387c478bd9Sstevel@tonic-gate 			(void) unlink(newfile);
4397c478bd9Sstevel@tonic-gate 			return (ERROR);
4407c478bd9Sstevel@tonic-gate 		} else if (rename(newfile, oldfile) == -1) {
4417c478bd9Sstevel@tonic-gate 			perror(NULL);
4427c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE), oldfile);
4437c478bd9Sstevel@tonic-gate 			(void) unlink(oldfile);
4447c478bd9Sstevel@tonic-gate 			(void) unlink(newfile);
4457c478bd9Sstevel@tonic-gate 			if (link(tmphold, oldfile) == -1) {
4467c478bd9Sstevel@tonic-gate 				perror(NULL);
4477c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_BAD_LINK),
4487c478bd9Sstevel@tonic-gate 				    oldfile, tmphold);
4497c478bd9Sstevel@tonic-gate 			}
4507c478bd9Sstevel@tonic-gate 			return (ERROR);
4517c478bd9Sstevel@tonic-gate 		}
4527c478bd9Sstevel@tonic-gate 		(void) unlink(tmphold);
4537c478bd9Sstevel@tonic-gate 	} else {
4547c478bd9Sstevel@tonic-gate 		/*
4557c478bd9Sstevel@tonic-gate 		 * since there's an error, leave file alone; remove
4567c478bd9Sstevel@tonic-gate 		 * new file
4577c478bd9Sstevel@tonic-gate 		 */
4587c478bd9Sstevel@tonic-gate 		if (unlink(newfile) == -1) {
4597c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_CANT_RM), newfile);
4607c478bd9Sstevel@tonic-gate 		}
4617c478bd9Sstevel@tonic-gate 		return (ERROR);
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	return (rv);
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate /*
4697c478bd9Sstevel@tonic-gate  * wrapper for call to get_name_to_major_entry(): given driver name,
4707c478bd9Sstevel@tonic-gate  * retrieve major number.
4717c478bd9Sstevel@tonic-gate  */
4727c478bd9Sstevel@tonic-gate int
4737c478bd9Sstevel@tonic-gate get_major_no(char *driver_name, char *file_name)
4747c478bd9Sstevel@tonic-gate {
4757c478bd9Sstevel@tonic-gate 	int major = UNIQUE;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	if (get_name_to_major_entry(&major, driver_name, file_name) == ERROR)
4787c478bd9Sstevel@tonic-gate 		return (ERROR);
4797c478bd9Sstevel@tonic-gate 	else
4807c478bd9Sstevel@tonic-gate 		return (major);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate /*
4847c478bd9Sstevel@tonic-gate  * wrapper for call to get_name_to_major_entry(): given major number,
4857c478bd9Sstevel@tonic-gate  * retrieve driver name.
4867c478bd9Sstevel@tonic-gate  */
4877c478bd9Sstevel@tonic-gate int
4887c478bd9Sstevel@tonic-gate get_driver_name(int major, char *file_name, char *buf)
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate 	if (major < 0)
4917c478bd9Sstevel@tonic-gate 		return (ERROR);
4927c478bd9Sstevel@tonic-gate 	return (get_name_to_major_entry(&major, buf, file_name));
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate /*
4977c478bd9Sstevel@tonic-gate  * return pointer to cached name_to_major file - reads file into
4987c478bd9Sstevel@tonic-gate  * cache if this has not already been done.  Since there may be
4997c478bd9Sstevel@tonic-gate  * requests for multiple name_to_major files (rem_name_to_major,
5007c478bd9Sstevel@tonic-gate  * name_to_major), this routine keeps a list of cached files.
5017c478bd9Sstevel@tonic-gate  */
5027c478bd9Sstevel@tonic-gate static int
5037c478bd9Sstevel@tonic-gate get_cached_n_to_m_file(char *filename, char ***cache)
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	struct n_to_m_cache {
5067c478bd9Sstevel@tonic-gate 		char *file;
5077c478bd9Sstevel@tonic-gate 		char **cached_file;
5087c478bd9Sstevel@tonic-gate 		int size;
5097c478bd9Sstevel@tonic-gate 		struct n_to_m_cache *next;
5107c478bd9Sstevel@tonic-gate 	};
5117c478bd9Sstevel@tonic-gate 	static struct n_to_m_cache *head = NULL;
5127c478bd9Sstevel@tonic-gate 	struct n_to_m_cache *ptr;
5137c478bd9Sstevel@tonic-gate 	FILE *fp;
5147c478bd9Sstevel@tonic-gate 	char drv[FILENAME_MAX + 1];
5157c478bd9Sstevel@tonic-gate 	char entry[FILENAME_MAX + 1];
5161ca93273Seota 	char line[MAX_N2M_ALIAS_LINE], *cp;
5177c478bd9Sstevel@tonic-gate 	int maj;
5187c478bd9Sstevel@tonic-gate 	int size = 0;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	/*
5227c478bd9Sstevel@tonic-gate 	 * see if the file is already cached - either
5237c478bd9Sstevel@tonic-gate 	 * rem_name_to_major or name_to_major
5247c478bd9Sstevel@tonic-gate 	 */
5257c478bd9Sstevel@tonic-gate 	ptr = head;
5267c478bd9Sstevel@tonic-gate 	while (ptr != NULL) {
5277c478bd9Sstevel@tonic-gate 		if (strcmp(ptr->file, filename) == 0)
5287c478bd9Sstevel@tonic-gate 			break;
5297c478bd9Sstevel@tonic-gate 		ptr = ptr->next;
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {	/* we need to cache the contents */
5337c478bd9Sstevel@tonic-gate 		if ((fp = fopen(filename, "r")) == NULL) {
5347c478bd9Sstevel@tonic-gate 			perror(NULL);
5357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_CANT_OPEN),
5367c478bd9Sstevel@tonic-gate 			    filename);
5377c478bd9Sstevel@tonic-gate 			return (ERROR);
5387c478bd9Sstevel@tonic-gate 		}
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 		while (fgets(line, sizeof (line), fp) != NULL) {
5411ca93273Seota 			/* cut off comments starting with '#' */
5421ca93273Seota 			if ((cp = strchr(line, '#')) != NULL)
5431ca93273Seota 				*cp = '\0';
5441ca93273Seota 			/* ignore comment or blank lines */
5451ca93273Seota 			if (is_blank(line))
5461ca93273Seota 				continue;
5471ca93273Seota 			/* sanity-check */
5482e107de7SJerry Gilliam 			/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
5497c478bd9Sstevel@tonic-gate 			if (sscanf(line, "%s%s", drv, entry) != 2) {
5507c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_BAD_LINE),
5517c478bd9Sstevel@tonic-gate 				    filename, line);
5527c478bd9Sstevel@tonic-gate 				continue;
5537c478bd9Sstevel@tonic-gate 			}
5547c478bd9Sstevel@tonic-gate 			maj = atoi(entry);
5557c478bd9Sstevel@tonic-gate 			if (maj > size)
5567c478bd9Sstevel@tonic-gate 				size = maj;
5577c478bd9Sstevel@tonic-gate 		}
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 		/* allocate struct to cache the file */
5607c478bd9Sstevel@tonic-gate 		ptr = (struct n_to_m_cache *)calloc(1,
5617c478bd9Sstevel@tonic-gate 		    sizeof (struct n_to_m_cache));
5627c478bd9Sstevel@tonic-gate 		if (ptr == NULL) {
5637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_MEM));
5647c478bd9Sstevel@tonic-gate 			return (ERROR);
5657c478bd9Sstevel@tonic-gate 		}
5667c478bd9Sstevel@tonic-gate 		ptr->size = size + 1;
5677c478bd9Sstevel@tonic-gate 		/* allocate space to cache contents of file */
5687c478bd9Sstevel@tonic-gate 		ptr->cached_file = (char **)calloc(ptr->size, sizeof (char *));
5697c478bd9Sstevel@tonic-gate 		if (ptr->cached_file == NULL) {
5707c478bd9Sstevel@tonic-gate 			free(ptr);
5717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_MEM));
5727c478bd9Sstevel@tonic-gate 			return (ERROR);
5737c478bd9Sstevel@tonic-gate 		}
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 		rewind(fp);
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 		/*
5787c478bd9Sstevel@tonic-gate 		 * now fill the cache
5797c478bd9Sstevel@tonic-gate 		 * the cache is an array of char pointers indexed by major
5807c478bd9Sstevel@tonic-gate 		 * number
5817c478bd9Sstevel@tonic-gate 		 */
5827c478bd9Sstevel@tonic-gate 		while (fgets(line, sizeof (line), fp) != NULL) {
5831ca93273Seota 			/* cut off comments starting with '#' */
5841ca93273Seota 			if ((cp = strchr(line, '#')) != NULL)
5851ca93273Seota 				*cp = '\0';
5861ca93273Seota 			/* ignore comment or blank lines */
5871ca93273Seota 			if (is_blank(line))
5881ca93273Seota 				continue;
5891ca93273Seota 			/* sanity-check */
5902e107de7SJerry Gilliam 			/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
5917c478bd9Sstevel@tonic-gate 			if (sscanf(line, "%s%s", drv, entry) != 2) {
5927c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_BAD_LINE),
5937c478bd9Sstevel@tonic-gate 				    filename, line);
5947c478bd9Sstevel@tonic-gate 				continue;
5957c478bd9Sstevel@tonic-gate 			}
5967c478bd9Sstevel@tonic-gate 			maj = atoi(entry);
5977c478bd9Sstevel@tonic-gate 			if ((ptr->cached_file[maj] = strdup(drv)) == NULL) {
5987c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_NO_MEM));
5997c478bd9Sstevel@tonic-gate 				free(ptr->cached_file);
6007c478bd9Sstevel@tonic-gate 				free(ptr);
6017c478bd9Sstevel@tonic-gate 				return (ERROR);
6027c478bd9Sstevel@tonic-gate 			}
6037c478bd9Sstevel@tonic-gate 			(void) strcpy(ptr->cached_file[maj], drv);
6047c478bd9Sstevel@tonic-gate 		}
6057c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
6067c478bd9Sstevel@tonic-gate 		/* link the cache struct into the list of cached files */
6077c478bd9Sstevel@tonic-gate 		ptr->file = strdup(filename);
6087c478bd9Sstevel@tonic-gate 		if (ptr->file == NULL) {
6097c478bd9Sstevel@tonic-gate 			for (maj = 0; maj <= ptr->size; maj++)
6107c478bd9Sstevel@tonic-gate 				free(ptr->cached_file[maj]);
6117c478bd9Sstevel@tonic-gate 			free(ptr->cached_file);
6127c478bd9Sstevel@tonic-gate 			free(ptr);
6137c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_MEM));
6147c478bd9Sstevel@tonic-gate 			return (ERROR);
6157c478bd9Sstevel@tonic-gate 		}
6167c478bd9Sstevel@tonic-gate 		ptr->next = head;
6177c478bd9Sstevel@tonic-gate 		head = ptr;
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 	/* return value pointer to contents of file */
6207c478bd9Sstevel@tonic-gate 	*cache = ptr->cached_file;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	/* return size */
6237c478bd9Sstevel@tonic-gate 	return (ptr->size);
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate /*
6287c478bd9Sstevel@tonic-gate  * Using get_cached_n_to_m_file(), retrieve maximum major number
6297c478bd9Sstevel@tonic-gate  * found in the specificed file (name_to_major/rem_name_to_major).
6307c478bd9Sstevel@tonic-gate  *
6317c478bd9Sstevel@tonic-gate  * The return value is actually the size of the internal cache including 0.
6327c478bd9Sstevel@tonic-gate  */
6337c478bd9Sstevel@tonic-gate int
6347c478bd9Sstevel@tonic-gate get_max_major(char *file_name)
6357c478bd9Sstevel@tonic-gate {
6367c478bd9Sstevel@tonic-gate 	char **n_to_m_cache = NULL;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	return (get_cached_n_to_m_file(file_name, &n_to_m_cache));
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate /*
6437c478bd9Sstevel@tonic-gate  * searching name_to_major: if major_no == UNIQUE then the caller wants to
6447c478bd9Sstevel@tonic-gate  * use the driver name as the key.  Otherwise, the caller wants to use
6457c478bd9Sstevel@tonic-gate  * the major number as a key.
6467c478bd9Sstevel@tonic-gate  *
6477c478bd9Sstevel@tonic-gate  * This routine caches the contents of the name_to_major file on
6487c478bd9Sstevel@tonic-gate  * first call.  And it could be generalized to deal with other
6497c478bd9Sstevel@tonic-gate  * config files if necessary.
6507c478bd9Sstevel@tonic-gate  */
6517c478bd9Sstevel@tonic-gate static int
6527c478bd9Sstevel@tonic-gate get_name_to_major_entry(int *major_no, char *driver_name, char *file_name)
6537c478bd9Sstevel@tonic-gate {
6547c478bd9Sstevel@tonic-gate 	int maj;
6557c478bd9Sstevel@tonic-gate 	char **n_to_m_cache = NULL;
6567c478bd9Sstevel@tonic-gate 	int size = 0;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	int ret = NOT_UNIQUE;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	/*
6617c478bd9Sstevel@tonic-gate 	 * read the file in - we cache it in case caller wants to
6627c478bd9Sstevel@tonic-gate 	 * do multiple lookups
6637c478bd9Sstevel@tonic-gate 	 */
6647c478bd9Sstevel@tonic-gate 	size = get_cached_n_to_m_file(file_name, &n_to_m_cache);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	if (size == ERROR)
6677c478bd9Sstevel@tonic-gate 		return (ERROR);
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	/* search with driver name as key */
6707c478bd9Sstevel@tonic-gate 	if (*major_no == UNIQUE) {
6717c478bd9Sstevel@tonic-gate 		for (maj = 0; maj < size; maj++) {
6727c478bd9Sstevel@tonic-gate 			if ((n_to_m_cache[maj] != NULL) &&
6737c478bd9Sstevel@tonic-gate 			    (strcmp(driver_name, n_to_m_cache[maj]) == 0)) {
6747c478bd9Sstevel@tonic-gate 				*major_no = maj;
6757c478bd9Sstevel@tonic-gate 				break;
6767c478bd9Sstevel@tonic-gate 			}
6777c478bd9Sstevel@tonic-gate 		}
6787c478bd9Sstevel@tonic-gate 		if (maj >= size)
6797c478bd9Sstevel@tonic-gate 			ret = UNIQUE;
6807c478bd9Sstevel@tonic-gate 	/* search with major number as key */
6817c478bd9Sstevel@tonic-gate 	} else {
6827c478bd9Sstevel@tonic-gate 		/*
6837c478bd9Sstevel@tonic-gate 		 * Bugid 1254588, drvconfig dump core after loading driver
6847c478bd9Sstevel@tonic-gate 		 * with major number bigger than entries defined in
6857c478bd9Sstevel@tonic-gate 		 * /etc/name_to_major.
6867c478bd9Sstevel@tonic-gate 		 */
6877c478bd9Sstevel@tonic-gate 		if (*major_no >= size)
6887c478bd9Sstevel@tonic-gate 			return (UNIQUE);
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 		if (n_to_m_cache[*major_no] != NULL) {
6917c478bd9Sstevel@tonic-gate 			(void) strcpy(driver_name, n_to_m_cache[*major_no]);
6927c478bd9Sstevel@tonic-gate 		} else
6937c478bd9Sstevel@tonic-gate 			ret = UNIQUE;
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 	return (ret);
6967c478bd9Sstevel@tonic-gate }
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate /*
699f4da9be0Scth  * Given pointer to begining of member 'n' in a space (or separator)
700f4da9be0Scth  * separated list, return pointer to member 'n+1', and establish member 'n'
701f4da9be0Scth  * in *current_entry.  If unquote, then we skip a leading quote and treat
702f4da9be0Scth  * the trailing quote as a separator (and skip).
7037c478bd9Sstevel@tonic-gate  */
7047c478bd9Sstevel@tonic-gate char *
7057c478bd9Sstevel@tonic-gate get_entry(
7067c478bd9Sstevel@tonic-gate 	char *prev_member,
7077c478bd9Sstevel@tonic-gate 	char *current_entry,
708f4da9be0Scth 	char separator,
709f4da9be0Scth 	int  unquote)
7107c478bd9Sstevel@tonic-gate {
7117c478bd9Sstevel@tonic-gate 	char	*ptr;
712f4da9be0Scth 	int	quoted = 0;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	ptr = prev_member;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	/* skip white space */
7177c478bd9Sstevel@tonic-gate 	while (*ptr == '\t' || *ptr == ' ')
7187c478bd9Sstevel@tonic-gate 		ptr++;
7197c478bd9Sstevel@tonic-gate 
720f4da9be0Scth 	/* if unquote skip leading quote */
721f4da9be0Scth 	if (unquote && *ptr == '"') {
722f4da9be0Scth 		quoted++;
723f4da9be0Scth 		ptr++;
724f4da9be0Scth 	}
725f4da9be0Scth 
726f4da9be0Scth 	/* read thru the current entry looking for end, separator, or unquote */
727f4da9be0Scth 	while (*ptr &&
728f4da9be0Scth 	    (*ptr != separator) &&
729f4da9be0Scth 	    ((separator != ' ') || (*ptr != '\t')) &&
730f4da9be0Scth 	    (!quoted || (*ptr != '"'))) {
7317c478bd9Sstevel@tonic-gate 		*current_entry++ = *ptr++;
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 	*current_entry = '\0';
7347c478bd9Sstevel@tonic-gate 
735f4da9be0Scth 	if (separator && (*ptr == separator))
736f4da9be0Scth 		ptr++;	/* skip over separator */
737f4da9be0Scth 	if (quoted && (*ptr == '"'))
738f4da9be0Scth 		ptr++;	/* skip over trailing quote */
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	/* skip white space */
7417c478bd9Sstevel@tonic-gate 	while (*ptr == '\t' || *ptr == ' ') {
7427c478bd9Sstevel@tonic-gate 		ptr++;
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	return (ptr);
7467c478bd9Sstevel@tonic-gate }
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate void
7497c478bd9Sstevel@tonic-gate enter_lock(void)
7507c478bd9Sstevel@tonic-gate {
75149e92448Svikram 	struct flock lock;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	/*
7547c478bd9Sstevel@tonic-gate 	 * attempt to create the lock file
7557c478bd9Sstevel@tonic-gate 	 */
75649e92448Svikram 	add_rem_lock_fd = open(add_rem_lock, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
75749e92448Svikram 	if (add_rem_lock_fd < 0) {
75849e92448Svikram 		(void) fprintf(stderr, gettext(ERR_CREAT_LOCK),
75949e92448Svikram 		    add_rem_lock, strerror(errno));
76049e92448Svikram 		exit(1);
76149e92448Svikram 	}
76249e92448Svikram 
76349e92448Svikram 	lock.l_type = F_WRLCK;
76449e92448Svikram 	lock.l_whence = SEEK_SET;
76549e92448Svikram 	lock.l_start = 0;
76649e92448Svikram 	lock.l_len = 0;
76749e92448Svikram 
76849e92448Svikram 	/* Try for the lock but don't wait. */
76949e92448Svikram 	if (fcntl(add_rem_lock_fd, F_SETLK, &lock) == -1) {
77049e92448Svikram 		if (errno == EACCES || errno == EAGAIN) {
7717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_PROG_IN_USE));
7727c478bd9Sstevel@tonic-gate 		} else {
77349e92448Svikram 			(void) fprintf(stderr, gettext(ERR_LOCK),
77449e92448Svikram 			    add_rem_lock, strerror(errno));
7757c478bd9Sstevel@tonic-gate 		}
7767c478bd9Sstevel@tonic-gate 		exit(1);
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate }
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate void
7817c478bd9Sstevel@tonic-gate err_exit(void)
7827c478bd9Sstevel@tonic-gate {
7837c478bd9Sstevel@tonic-gate 	/* release memory allocated for moddir */
7847c478bd9Sstevel@tonic-gate 	cleanup_moddir();
7857c478bd9Sstevel@tonic-gate 	/* remove add_drv/rem_drv lock */
7867c478bd9Sstevel@tonic-gate 	exit_unlock();
7877c478bd9Sstevel@tonic-gate 	exit(1);
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate void
7917c478bd9Sstevel@tonic-gate cleanup_moddir(void)
7927c478bd9Sstevel@tonic-gate {
7937c478bd9Sstevel@tonic-gate 	struct drvmod_dir *walk_ptr;
7947c478bd9Sstevel@tonic-gate 	struct drvmod_dir *free_ptr = moddir;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	while (free_ptr != NULL) {
7977c478bd9Sstevel@tonic-gate 		walk_ptr = free_ptr->next;
7987c478bd9Sstevel@tonic-gate 		free(free_ptr);
7997c478bd9Sstevel@tonic-gate 		free_ptr = walk_ptr;
8007c478bd9Sstevel@tonic-gate 	}
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate void
8047c478bd9Sstevel@tonic-gate exit_unlock(void)
8057c478bd9Sstevel@tonic-gate {
80649e92448Svikram 	struct flock unlock;
8077c478bd9Sstevel@tonic-gate 
80849e92448Svikram 	if (add_rem_lock_fd < 0)
80949e92448Svikram 		return;
81049e92448Svikram 
81149e92448Svikram 	unlock.l_type = F_UNLCK;
81249e92448Svikram 	unlock.l_whence = SEEK_SET;
81349e92448Svikram 	unlock.l_start = 0;
81449e92448Svikram 	unlock.l_len = 0;
81549e92448Svikram 
81649e92448Svikram 	if (fcntl(add_rem_lock_fd, F_SETLK, &unlock) == -1) {
81749e92448Svikram 		(void) fprintf(stderr, gettext(ERR_UNLOCK),
81849e92448Svikram 		    add_rem_lock, strerror(errno));
81949e92448Svikram 	} else {
82049e92448Svikram 		(void) close(add_rem_lock_fd);
82149e92448Svikram 		add_rem_lock_fd = -1;
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate }
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate /*
8267c478bd9Sstevel@tonic-gate  * error adding driver; need to back out any changes to files.
8277c478bd9Sstevel@tonic-gate  * check flag to see which files need entries removed
8287c478bd9Sstevel@tonic-gate  * entry removal based on driver name
8297c478bd9Sstevel@tonic-gate  */
8307c478bd9Sstevel@tonic-gate void
8317c478bd9Sstevel@tonic-gate remove_entry(
8327c478bd9Sstevel@tonic-gate 	int c_flag,
8337c478bd9Sstevel@tonic-gate 	char *driver_name)
8347c478bd9Sstevel@tonic-gate {
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	if (c_flag & CLEAN_NAM_MAJ) {
8377c478bd9Sstevel@tonic-gate 		if (delete_entry(name_to_major, driver_name, " ",
8387c478bd9Sstevel@tonic-gate 		    NULL) == ERROR) {
8397c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_CLEAN),
8407c478bd9Sstevel@tonic-gate 			    name_to_major, driver_name);
8417c478bd9Sstevel@tonic-gate 		}
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	if (c_flag & CLEAN_DRV_ALIAS) {
8457c478bd9Sstevel@tonic-gate 		if (delete_entry(driver_aliases, driver_name, " ",
8467c478bd9Sstevel@tonic-gate 		    NULL) == ERROR) {
8477c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_DEL_ENTRY),
8487c478bd9Sstevel@tonic-gate 			    driver_name, driver_aliases);
8497c478bd9Sstevel@tonic-gate 		}
8507c478bd9Sstevel@tonic-gate 	}
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	if (c_flag & CLEAN_DRV_CLASSES) {
8537c478bd9Sstevel@tonic-gate 		if (delete_entry(driver_classes, driver_name, "\t", NULL) ==
8547c478bd9Sstevel@tonic-gate 		    ERROR) {
8557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_DEL_ENTRY),
8567c478bd9Sstevel@tonic-gate 			    driver_name, driver_classes);
8577c478bd9Sstevel@tonic-gate 		}
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	if (c_flag & CLEAN_MINOR_PERM) {
8617c478bd9Sstevel@tonic-gate 		if (delete_entry(minor_perm, driver_name, ":", NULL) == ERROR) {
8627c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_DEL_ENTRY),
8637c478bd9Sstevel@tonic-gate 			    driver_name, minor_perm);
8647c478bd9Sstevel@tonic-gate 		}
8657c478bd9Sstevel@tonic-gate 	}
8667c478bd9Sstevel@tonic-gate 	/*
8677c478bd9Sstevel@tonic-gate 	 * There's no point in removing entries from files that don't
8687c478bd9Sstevel@tonic-gate 	 * exist.  Prevent error messages by checking for file existence
8697c478bd9Sstevel@tonic-gate 	 * first.
8707c478bd9Sstevel@tonic-gate 	 */
8717c478bd9Sstevel@tonic-gate 	if ((c_flag & CLEAN_DEV_POLICY) != 0 &&
8727c478bd9Sstevel@tonic-gate 	    access(device_policy, F_OK) == 0) {
8737c478bd9Sstevel@tonic-gate 		if (delete_plcy_entry(device_policy, driver_name) == ERROR) {
8747c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_DEL_ENTRY),
8757c478bd9Sstevel@tonic-gate 			    driver_name, device_policy);
8767c478bd9Sstevel@tonic-gate 		}
8777c478bd9Sstevel@tonic-gate 	}
8787c478bd9Sstevel@tonic-gate 	if ((c_flag & CLEAN_DRV_PRIV) != 0 &&
8797c478bd9Sstevel@tonic-gate 	    access(extra_privs, F_OK) == 0) {
8807c478bd9Sstevel@tonic-gate 		if (delete_entry(extra_privs, driver_name, ":", NULL) ==
8817c478bd9Sstevel@tonic-gate 		    ERROR) {
8827c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_DEL_ENTRY),
8837c478bd9Sstevel@tonic-gate 			    driver_name, extra_privs);
8847c478bd9Sstevel@tonic-gate 		}
8857c478bd9Sstevel@tonic-gate 	}
8867c478bd9Sstevel@tonic-gate }
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate int
8897c478bd9Sstevel@tonic-gate check_perms_aliases(
8907c478bd9Sstevel@tonic-gate 	int m_flag,
8917c478bd9Sstevel@tonic-gate 	int i_flag)
8927c478bd9Sstevel@tonic-gate {
8937c478bd9Sstevel@tonic-gate 	/*
8947c478bd9Sstevel@tonic-gate 	 * If neither i_flag nor m_flag are specified no need to check the
8957c478bd9Sstevel@tonic-gate 	 * files for access permissions
8967c478bd9Sstevel@tonic-gate 	 */
8977c478bd9Sstevel@tonic-gate 	if (!m_flag && !i_flag)
8987c478bd9Sstevel@tonic-gate 		return (NOERR);
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	/* check minor_perm file : exits and is writable */
9017c478bd9Sstevel@tonic-gate 	if (m_flag) {
9027c478bd9Sstevel@tonic-gate 		if (access(minor_perm, R_OK | W_OK)) {
9037c478bd9Sstevel@tonic-gate 			perror(NULL);
9047c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
9057c478bd9Sstevel@tonic-gate 			    minor_perm);
9067c478bd9Sstevel@tonic-gate 			return (ERROR);
9077c478bd9Sstevel@tonic-gate 		}
9087c478bd9Sstevel@tonic-gate 	}
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	/* check driver_aliases file : exits and is writable */
9117c478bd9Sstevel@tonic-gate 	if (i_flag) {
9127c478bd9Sstevel@tonic-gate 		if (access(driver_aliases, R_OK | W_OK)) {
9137c478bd9Sstevel@tonic-gate 			perror(NULL);
9147c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
9157c478bd9Sstevel@tonic-gate 			    driver_aliases);
9167c478bd9Sstevel@tonic-gate 			return (ERROR);
9177c478bd9Sstevel@tonic-gate 		}
9187c478bd9Sstevel@tonic-gate 	}
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	return (NOERR);
9217c478bd9Sstevel@tonic-gate }
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate int
9257c478bd9Sstevel@tonic-gate check_name_to_major(int mode)
9267c478bd9Sstevel@tonic-gate {
9277c478bd9Sstevel@tonic-gate 	/* check name_to_major file : exists and is writable */
9287c478bd9Sstevel@tonic-gate 	if (access(name_to_major, mode)) {
9297c478bd9Sstevel@tonic-gate 		perror(NULL);
9307c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
9317c478bd9Sstevel@tonic-gate 		    name_to_major);
9327c478bd9Sstevel@tonic-gate 		return (ERROR);
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	return (NOERR);
9367c478bd9Sstevel@tonic-gate }
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate /*
9407c478bd9Sstevel@tonic-gate  * All this stuff is to support a server installing
9417c478bd9Sstevel@tonic-gate  * drivers on diskless clients.  When on the server
9427c478bd9Sstevel@tonic-gate  * need to prepend the basedir
9437c478bd9Sstevel@tonic-gate  */
9447c478bd9Sstevel@tonic-gate int
9457c478bd9Sstevel@tonic-gate build_filenames(char *basedir)
9467c478bd9Sstevel@tonic-gate {
9477c478bd9Sstevel@tonic-gate 	int	len;
9482e107de7SJerry Gilliam 	int	driver_aliases_len;
9492e107de7SJerry Gilliam 	int	driver_classes_len;
9502e107de7SJerry Gilliam 	int	minor_perm_len;
9512e107de7SJerry Gilliam 	int	name_to_major_len;
9522e107de7SJerry Gilliam 	int	rem_name_to_major_len;
9532e107de7SJerry Gilliam 	int	add_rem_lock_len;
9542e107de7SJerry Gilliam 	int	tmphold_len;
9552e107de7SJerry Gilliam 	int	devfs_root_len;
9562e107de7SJerry Gilliam 	int	device_policy_len;
9572e107de7SJerry Gilliam 	int	extra_privs_len;
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	if (basedir == NULL) {
9607c478bd9Sstevel@tonic-gate 		driver_aliases = DRIVER_ALIAS;
9617c478bd9Sstevel@tonic-gate 		driver_classes = DRIVER_CLASSES;
9627c478bd9Sstevel@tonic-gate 		minor_perm = MINOR_PERM;
9637c478bd9Sstevel@tonic-gate 		name_to_major = NAM_TO_MAJ;
9647c478bd9Sstevel@tonic-gate 		rem_name_to_major = REM_NAM_TO_MAJ;
9657c478bd9Sstevel@tonic-gate 		add_rem_lock = ADD_REM_LOCK;
9667c478bd9Sstevel@tonic-gate 		tmphold = TMPHOLD;
9677c478bd9Sstevel@tonic-gate 		devfs_root = DEVFS_ROOT;
9687c478bd9Sstevel@tonic-gate 		device_policy = DEV_POLICY;
9697c478bd9Sstevel@tonic-gate 		extra_privs = EXTRA_PRIVS;
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	} else {
9722e107de7SJerry Gilliam 		len = strlen(basedir) + 1;
9737c478bd9Sstevel@tonic-gate 
9742e107de7SJerry Gilliam 		driver_aliases_len = len + sizeof (DRIVER_ALIAS);
9752e107de7SJerry Gilliam 		driver_classes_len = len + sizeof (DRIVER_CLASSES);
9762e107de7SJerry Gilliam 		minor_perm_len = len + sizeof (MINOR_PERM);
9772e107de7SJerry Gilliam 		name_to_major_len = len + sizeof (NAM_TO_MAJ);
9782e107de7SJerry Gilliam 		rem_name_to_major_len = len + sizeof (REM_NAM_TO_MAJ);
9792e107de7SJerry Gilliam 		add_rem_lock_len = len + sizeof (ADD_REM_LOCK);
9802e107de7SJerry Gilliam 		tmphold_len = len + sizeof (TMPHOLD);
9812e107de7SJerry Gilliam 		devfs_root_len = len + sizeof (DEVFS_ROOT);
9822e107de7SJerry Gilliam 		device_policy_len = len + sizeof (DEV_POLICY);
9832e107de7SJerry Gilliam 		extra_privs_len = len + sizeof (EXTRA_PRIVS);
9847c478bd9Sstevel@tonic-gate 
9852e107de7SJerry Gilliam 		driver_aliases = malloc(driver_aliases_len);
9862e107de7SJerry Gilliam 		driver_classes = malloc(driver_classes_len);
9872e107de7SJerry Gilliam 		minor_perm = malloc(minor_perm_len);
9882e107de7SJerry Gilliam 		name_to_major = malloc(name_to_major_len);
9892e107de7SJerry Gilliam 		rem_name_to_major = malloc(rem_name_to_major_len);
9902e107de7SJerry Gilliam 		add_rem_lock = malloc(add_rem_lock_len);
9912e107de7SJerry Gilliam 		tmphold = malloc(tmphold_len);
9922e107de7SJerry Gilliam 		devfs_root = malloc(devfs_root_len);
9932e107de7SJerry Gilliam 		device_policy = malloc(device_policy_len);
9942e107de7SJerry Gilliam 		extra_privs = malloc(extra_privs_len);
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 		if ((driver_aliases == NULL) ||
9977c478bd9Sstevel@tonic-gate 		    (driver_classes == NULL) ||
9987c478bd9Sstevel@tonic-gate 		    (minor_perm == NULL) ||
9997c478bd9Sstevel@tonic-gate 		    (name_to_major == NULL) ||
10007c478bd9Sstevel@tonic-gate 		    (rem_name_to_major == NULL) ||
10017c478bd9Sstevel@tonic-gate 		    (add_rem_lock == NULL) ||
10027c478bd9Sstevel@tonic-gate 		    (tmphold == NULL) ||
10037c478bd9Sstevel@tonic-gate 		    (devfs_root == NULL) ||
10047c478bd9Sstevel@tonic-gate 		    (device_policy == NULL) ||
10057c478bd9Sstevel@tonic-gate 		    (extra_privs == NULL)) {
10067c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_MEM));
10077c478bd9Sstevel@tonic-gate 			return (ERROR);
10087c478bd9Sstevel@tonic-gate 		}
10097c478bd9Sstevel@tonic-gate 
10102e107de7SJerry Gilliam 		(void) snprintf(driver_aliases, driver_aliases_len,
10112e107de7SJerry Gilliam 		    "%s%s", basedir, DRIVER_ALIAS);
10122e107de7SJerry Gilliam 		(void) snprintf(driver_classes, driver_classes_len,
10132e107de7SJerry Gilliam 		    "%s%s", basedir, DRIVER_CLASSES);
10142e107de7SJerry Gilliam 		(void) snprintf(minor_perm, minor_perm_len,
10152e107de7SJerry Gilliam 		    "%s%s", basedir, MINOR_PERM);
10162e107de7SJerry Gilliam 		(void) snprintf(name_to_major, name_to_major_len,
10172e107de7SJerry Gilliam 		    "%s%s", basedir, NAM_TO_MAJ);
10182e107de7SJerry Gilliam 		(void) snprintf(rem_name_to_major, rem_name_to_major_len,
10192e107de7SJerry Gilliam 		    "%s%s", basedir, REM_NAM_TO_MAJ);
10202e107de7SJerry Gilliam 		(void) snprintf(add_rem_lock, add_rem_lock_len,
10212e107de7SJerry Gilliam 		    "%s%s", basedir, ADD_REM_LOCK);
10222e107de7SJerry Gilliam 		(void) snprintf(tmphold, tmphold_len,
10232e107de7SJerry Gilliam 		    "%s%s", basedir, TMPHOLD);
10242e107de7SJerry Gilliam 		(void) snprintf(devfs_root, devfs_root_len,
10252e107de7SJerry Gilliam 		    "%s%s", basedir, DEVFS_ROOT);
10262e107de7SJerry Gilliam 		(void) snprintf(device_policy, device_policy_len,
10272e107de7SJerry Gilliam 		    "%s%s", basedir, DEV_POLICY);
10282e107de7SJerry Gilliam 		(void) snprintf(extra_privs, extra_privs_len,
10292e107de7SJerry Gilliam 		    "%s%s", basedir, EXTRA_PRIVS);
10307c478bd9Sstevel@tonic-gate 	}
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	return (NOERR);
10337c478bd9Sstevel@tonic-gate }
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate static int
10367c478bd9Sstevel@tonic-gate exec_command(char *path, char *cmdline[MAX_CMD_LINE])
10377c478bd9Sstevel@tonic-gate {
10387c478bd9Sstevel@tonic-gate 	pid_t pid;
10397c478bd9Sstevel@tonic-gate 	uint_t stat_loc;
10407c478bd9Sstevel@tonic-gate 	int waitstat;
10417c478bd9Sstevel@tonic-gate 	int exit_status;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	/* child */
10447c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == 0) {
10457c478bd9Sstevel@tonic-gate 		(void) execv(path, cmdline);
10467c478bd9Sstevel@tonic-gate 		perror(NULL);
10477c478bd9Sstevel@tonic-gate 		return (ERROR);
10487c478bd9Sstevel@tonic-gate 	} else if (pid == -1) {
10497c478bd9Sstevel@tonic-gate 		/* fork failed */
10507c478bd9Sstevel@tonic-gate 		perror(NULL);
10517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_FORK_FAIL), cmdline);
10527c478bd9Sstevel@tonic-gate 		return (ERROR);
10537c478bd9Sstevel@tonic-gate 	} else {
10547c478bd9Sstevel@tonic-gate 		/* parent */
10557c478bd9Sstevel@tonic-gate 		do {
10567c478bd9Sstevel@tonic-gate 			waitstat = waitpid(pid, (int *)&stat_loc, 0);
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 		} while ((!WIFEXITED(stat_loc) &&
10597c478bd9Sstevel@tonic-gate 		    !WIFSIGNALED(stat_loc)) || (waitstat == 0));
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 		exit_status = WEXITSTATUS(stat_loc);
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 		return (exit_status);
10647c478bd9Sstevel@tonic-gate 	}
10657c478bd9Sstevel@tonic-gate }
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate /*
10686532b960SJerry Gilliam  * Exec devfsadm to perform driver config/unconfig operation,
10696532b960SJerry Gilliam  * adding or removing aliases.
10707c478bd9Sstevel@tonic-gate  */
10716532b960SJerry Gilliam static int
10726532b960SJerry Gilliam exec_devfsadm(
10736532b960SJerry Gilliam 	boolean_t config,
10747c478bd9Sstevel@tonic-gate 	char *driver_name,
10757c478bd9Sstevel@tonic-gate 	major_t major_num,
10767c478bd9Sstevel@tonic-gate 	char *aliases,
10777c478bd9Sstevel@tonic-gate 	char *classes,
10786532b960SJerry Gilliam 	int verbose_flag,
10796532b960SJerry Gilliam 	int force_flag)
10807c478bd9Sstevel@tonic-gate {
10817c478bd9Sstevel@tonic-gate 	int n = 0;
10827c478bd9Sstevel@tonic-gate 	char *cmdline[MAX_CMD_LINE];
10837c478bd9Sstevel@tonic-gate 	char maj_num[128];
10847c478bd9Sstevel@tonic-gate 	char *previous;
10857c478bd9Sstevel@tonic-gate 	char *current;
10867c478bd9Sstevel@tonic-gate 	int len;
10876532b960SJerry Gilliam 	int rv;
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	/* build command line */
10907c478bd9Sstevel@tonic-gate 	cmdline[n++] = DRVCONFIG;
10916532b960SJerry Gilliam 	if (config == B_FALSE) {
10926532b960SJerry Gilliam 		cmdline[n++] = "-u";		/* unconfigure */
10936532b960SJerry Gilliam 		if (force_flag)
10946532b960SJerry Gilliam 			cmdline[n++] = "-f";	/* force if currently in use */
10956532b960SJerry Gilliam 	}
10967c478bd9Sstevel@tonic-gate 	if (verbose_flag) {
10977c478bd9Sstevel@tonic-gate 		cmdline[n++] = "-v";
10987c478bd9Sstevel@tonic-gate 	}
10997c478bd9Sstevel@tonic-gate 	cmdline[n++] = "-b";
11007c478bd9Sstevel@tonic-gate 	if (classes) {
11017c478bd9Sstevel@tonic-gate 		cmdline[n++] = "-c";
11027c478bd9Sstevel@tonic-gate 		cmdline[n++] = classes;
11037c478bd9Sstevel@tonic-gate 	}
11047c478bd9Sstevel@tonic-gate 	cmdline[n++] = "-i";
11057c478bd9Sstevel@tonic-gate 	cmdline[n++] = driver_name;
11067c478bd9Sstevel@tonic-gate 	cmdline[n++] = "-m";
11072e107de7SJerry Gilliam 	(void) snprintf(maj_num, sizeof (maj_num), "%lu", major_num);
11087c478bd9Sstevel@tonic-gate 	cmdline[n++] = maj_num;
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	if (aliases != NULL) {
11117c478bd9Sstevel@tonic-gate 		len = strlen(aliases);
11127c478bd9Sstevel@tonic-gate 		previous = aliases;
11137c478bd9Sstevel@tonic-gate 		do {
11147c478bd9Sstevel@tonic-gate 			cmdline[n++] = "-a";
11157c478bd9Sstevel@tonic-gate 			cmdline[n] = calloc(len + 1, 1);
11167c478bd9Sstevel@tonic-gate 			if (cmdline[n] == NULL) {
11177c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11187c478bd9Sstevel@tonic-gate 				    gettext(ERR_NO_MEM));
11197c478bd9Sstevel@tonic-gate 				return (ERROR);
11207c478bd9Sstevel@tonic-gate 			}
11217c478bd9Sstevel@tonic-gate 			current = get_entry(previous,
1122f4da9be0Scth 			    cmdline[n++], ' ', 0);
11237c478bd9Sstevel@tonic-gate 			previous = current;
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 		} while (*current != '\0');
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	}
11287c478bd9Sstevel@tonic-gate 	cmdline[n] = (char *)0;
11297c478bd9Sstevel@tonic-gate 
11306532b960SJerry Gilliam 	rv = exec_command(DRVCONFIG_PATH, cmdline);
11316532b960SJerry Gilliam 	if (rv == NOERR)
11326532b960SJerry Gilliam 		return (NOERR);
11336532b960SJerry Gilliam 	return (ERROR);
11346532b960SJerry Gilliam }
11357c478bd9Sstevel@tonic-gate 
11366532b960SJerry Gilliam int
11376532b960SJerry Gilliam unconfig_driver(
11386532b960SJerry Gilliam 	char *driver_name,
11396532b960SJerry Gilliam 	major_t major_num,
11406532b960SJerry Gilliam 	char *aliases,
11416532b960SJerry Gilliam 	int verbose_flag,
11426532b960SJerry Gilliam 	int force_flag)
11436532b960SJerry Gilliam {
11446532b960SJerry Gilliam 	return (exec_devfsadm(B_FALSE, driver_name, major_num,
11456532b960SJerry Gilliam 	    aliases, NULL, verbose_flag, force_flag));
11466532b960SJerry Gilliam }
11476532b960SJerry Gilliam 
11486532b960SJerry Gilliam /*
11496532b960SJerry Gilliam  * check that major_num doesn't exceed maximum on this machine
11506532b960SJerry Gilliam  * do this here to support add_drv on server for diskless clients
11516532b960SJerry Gilliam  */
11526532b960SJerry Gilliam int
11536532b960SJerry Gilliam config_driver(
11546532b960SJerry Gilliam 	char *driver_name,
11556532b960SJerry Gilliam 	major_t major_num,
11566532b960SJerry Gilliam 	char *aliases,
11576532b960SJerry Gilliam 	char *classes,
11586532b960SJerry Gilliam 	int cleanup_flag,
11596532b960SJerry Gilliam 	int verbose_flag)
11606532b960SJerry Gilliam {
11616532b960SJerry Gilliam 	int	max_dev;
11626532b960SJerry Gilliam 	int	rv;
11636532b960SJerry Gilliam 
11646532b960SJerry Gilliam 	if (modctl(MODRESERVED, NULL, &max_dev) < 0) {
11656532b960SJerry Gilliam 		perror(NULL);
11666532b960SJerry Gilliam 		(void) fprintf(stderr, gettext(ERR_MAX_MAJOR));
11676532b960SJerry Gilliam 		return (ERROR);
11686532b960SJerry Gilliam 	}
11696532b960SJerry Gilliam 
11706532b960SJerry Gilliam 	if (major_num >= max_dev) {
11716532b960SJerry Gilliam 		(void) fprintf(stderr, gettext(ERR_MAX_EXCEEDS),
11726532b960SJerry Gilliam 		    major_num, max_dev);
11736532b960SJerry Gilliam 		return (ERROR);
11746532b960SJerry Gilliam 	}
11756532b960SJerry Gilliam 
11766532b960SJerry Gilliam 	/* bind major number and driver name */
11776532b960SJerry Gilliam 	rv = exec_devfsadm(B_TRUE, driver_name, major_num,
11786532b960SJerry Gilliam 	    aliases, classes, verbose_flag, 0);
11796532b960SJerry Gilliam 
11806532b960SJerry Gilliam 	if (rv == NOERR)
11817c478bd9Sstevel@tonic-gate 		return (NOERR);
11827c478bd9Sstevel@tonic-gate 	perror(NULL);
11837c478bd9Sstevel@tonic-gate 	remove_entry(cleanup_flag, driver_name);
11847c478bd9Sstevel@tonic-gate 	return (ERROR);
11857c478bd9Sstevel@tonic-gate }
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate void
11887c478bd9Sstevel@tonic-gate load_driver(char *driver_name, int verbose_flag)
11897c478bd9Sstevel@tonic-gate {
11907c478bd9Sstevel@tonic-gate 	int n = 0;
11917c478bd9Sstevel@tonic-gate 	char *cmdline[MAX_CMD_LINE];
11927c478bd9Sstevel@tonic-gate 	int exec_status;
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	/* build command line */
11957c478bd9Sstevel@tonic-gate 	cmdline[n++] = DEVFSADM;
11967c478bd9Sstevel@tonic-gate 	if (verbose_flag) {
11977c478bd9Sstevel@tonic-gate 		cmdline[n++] = "-v";
11987c478bd9Sstevel@tonic-gate 	}
11997c478bd9Sstevel@tonic-gate 	cmdline[n++] = "-i";
12007c478bd9Sstevel@tonic-gate 	cmdline[n++] = driver_name;
12017c478bd9Sstevel@tonic-gate 	cmdline[n] = (char *)0;
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	exec_status = exec_command(DEVFSADM_PATH, cmdline);
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	if (exec_status != NOERR) {
12067c478bd9Sstevel@tonic-gate 		/* no clean : name and major number are bound */
120711ebc2d0SJerry Gilliam 		(void) fprintf(stderr, gettext(ERR_CONFIG), driver_name);
12087c478bd9Sstevel@tonic-gate 	}
12097c478bd9Sstevel@tonic-gate }
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate void
12127c478bd9Sstevel@tonic-gate get_modid(char *driver_name, int *mod)
12137c478bd9Sstevel@tonic-gate {
12147c478bd9Sstevel@tonic-gate 	struct modinfo	modinfo;
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	modinfo.mi_id = -1;
12177c478bd9Sstevel@tonic-gate 	modinfo.mi_info = MI_INFO_ALL;
12187c478bd9Sstevel@tonic-gate 	do {
12197c478bd9Sstevel@tonic-gate 		/*
12207c478bd9Sstevel@tonic-gate 		 * If we are at the end of the list of loaded modules
12217c478bd9Sstevel@tonic-gate 		 * then set *mod = -1 and return
12227c478bd9Sstevel@tonic-gate 		 */
12237c478bd9Sstevel@tonic-gate 		if (modctl(MODINFO, 0, &modinfo) < 0) {
12247c478bd9Sstevel@tonic-gate 			*mod = -1;
12257c478bd9Sstevel@tonic-gate 			return;
12267c478bd9Sstevel@tonic-gate 		}
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 		*mod = modinfo.mi_id;
12297c478bd9Sstevel@tonic-gate 	} while (strcmp(driver_name, modinfo.mi_name) != 0);
12307c478bd9Sstevel@tonic-gate }
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate int
12337c478bd9Sstevel@tonic-gate create_reconfig(char *basedir)
12347c478bd9Sstevel@tonic-gate {
12357c478bd9Sstevel@tonic-gate 	char reconfig_file[MAXPATHLEN + FILENAME_MAX + 1];
12367c478bd9Sstevel@tonic-gate 	FILE *reconfig_fp;
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	if (basedir != NULL) {
12397c478bd9Sstevel@tonic-gate 		(void) strcpy(reconfig_file, basedir);
12407c478bd9Sstevel@tonic-gate 		(void) strcat(reconfig_file, RECONFIGURE);
12417c478bd9Sstevel@tonic-gate 	} else {
12427c478bd9Sstevel@tonic-gate 		(void) strcpy(reconfig_file, RECONFIGURE);
12437c478bd9Sstevel@tonic-gate 	}
12447c478bd9Sstevel@tonic-gate 	if ((reconfig_fp = fopen(reconfig_file, "a")) == NULL)
12457c478bd9Sstevel@tonic-gate 		return (ERROR);
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	(void) fclose(reconfig_fp);
12487c478bd9Sstevel@tonic-gate 	return (NOERR);
12497c478bd9Sstevel@tonic-gate }
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate /*
12537c478bd9Sstevel@tonic-gate  * update_minor_entry:
12547c478bd9Sstevel@tonic-gate  *	open file
12557c478bd9Sstevel@tonic-gate  *	for each entry in list
12567c478bd9Sstevel@tonic-gate  *		where list entries are separated by <list_separator>
12577c478bd9Sstevel@tonic-gate  * 		modify entry : driver_name <entry_separator> entry
12587c478bd9Sstevel@tonic-gate  *	close file
12597c478bd9Sstevel@tonic-gate  *
12607c478bd9Sstevel@tonic-gate  *	return error/noerr
12617c478bd9Sstevel@tonic-gate  */
12627c478bd9Sstevel@tonic-gate int
12637c478bd9Sstevel@tonic-gate update_minor_entry(char *driver_name, char *perm_list)
12647c478bd9Sstevel@tonic-gate {
12657c478bd9Sstevel@tonic-gate 	FILE *fp;
12667c478bd9Sstevel@tonic-gate 	FILE *newfp;
12677c478bd9Sstevel@tonic-gate 	struct group *sysgrp;
12687c478bd9Sstevel@tonic-gate 	int match = 0;
12691ca93273Seota 	char line[MAX_DBFILE_ENTRY], *cp, *dup;
12701ca93273Seota 	char drv[FILENAME_MAX + 1], *drv_minor;
12717c478bd9Sstevel@tonic-gate 	char minor[FILENAME_MAX + 1], perm[OPT_LEN + 1];
12727c478bd9Sstevel@tonic-gate 	char own[OPT_LEN + 1], grp[OPT_LEN + 1];
12737c478bd9Sstevel@tonic-gate 	int status = NOERR, i;
12747c478bd9Sstevel@tonic-gate 	char *newfile, *tptr;
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 	if ((fp = fopen(minor_perm, "r")) == NULL) {
12777c478bd9Sstevel@tonic-gate 		perror(NULL);
12787c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
12797c478bd9Sstevel@tonic-gate 		    minor_perm);
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 		return (ERROR);
12827c478bd9Sstevel@tonic-gate 	}
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	/*
12857c478bd9Sstevel@tonic-gate 	 * Build filename for temporary file
12867c478bd9Sstevel@tonic-gate 	 */
12877c478bd9Sstevel@tonic-gate 	if ((tptr = calloc(strlen(minor_perm) + strlen(XEND) + 1, 1)) == NULL) {
12887c478bd9Sstevel@tonic-gate 		perror(NULL);
12897c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
12907c478bd9Sstevel@tonic-gate 	}
12917c478bd9Sstevel@tonic-gate 	(void) strcpy(tptr, minor_perm);
12927c478bd9Sstevel@tonic-gate 	(void) strcat(tptr, XEND);
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	/*
12957c478bd9Sstevel@tonic-gate 	 * Set gid so we preserve group attribute.  Ideally we wouldn't
12967c478bd9Sstevel@tonic-gate 	 * assume a gid of "sys" but we can't undo the damage on already
12977c478bd9Sstevel@tonic-gate 	 * installed systems unless we force the issue.
12987c478bd9Sstevel@tonic-gate 	 */
12997c478bd9Sstevel@tonic-gate 	if ((sysgrp = getgrnam("sys")) != NULL) {
13007c478bd9Sstevel@tonic-gate 		(void) setgid(sysgrp->gr_gid);
13017c478bd9Sstevel@tonic-gate 	}
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	newfile = mktemp(tptr);
13047c478bd9Sstevel@tonic-gate 	if ((newfp = fopen(newfile, "w")) == NULL) {
13057c478bd9Sstevel@tonic-gate 		perror(NULL);
13067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
13077c478bd9Sstevel@tonic-gate 		    newfile);
13087c478bd9Sstevel@tonic-gate 		return (ERROR);
13097c478bd9Sstevel@tonic-gate 	}
13107c478bd9Sstevel@tonic-gate 
13112e107de7SJerry Gilliam 	/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
13127c478bd9Sstevel@tonic-gate 	if (sscanf(perm_list, "%s%s%s%s", minor, perm, own, grp) != 4) {
13137c478bd9Sstevel@tonic-gate 		status = ERROR;
13147c478bd9Sstevel@tonic-gate 	}
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	while ((fgets(line, sizeof (line), fp) != NULL) && status == NOERR) {
13171ca93273Seota 		/* copy the whole line into dup */
13181ca93273Seota 		if ((dup = strdup(line)) == NULL) {
13191ca93273Seota 			perror(NULL);
13201ca93273Seota 			(void) fprintf(stderr, gettext(ERR_NO_MEM));
13211ca93273Seota 			status = ERROR;
13221ca93273Seota 			break;
13231ca93273Seota 		}
13241ca93273Seota 		/* cut off comments starting with '#' */
13251ca93273Seota 		if ((cp = strchr(dup, '#')) != NULL)
13261ca93273Seota 			*cp = '\0';
13271ca93273Seota 		/* ignore comment or blank lines */
13281ca93273Seota 		if (is_blank(dup)) {
13291ca93273Seota 			if (fputs(line, newfp) == EOF) {
13307c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_UPDATE),
13317c478bd9Sstevel@tonic-gate 				    minor_perm);
13327c478bd9Sstevel@tonic-gate 				status = ERROR;
13337c478bd9Sstevel@tonic-gate 			}
13341ca93273Seota 			free(dup);
13357c478bd9Sstevel@tonic-gate 			continue;
13367c478bd9Sstevel@tonic-gate 		}
13377c478bd9Sstevel@tonic-gate 
13381ca93273Seota 		/* get the driver name */
13392e107de7SJerry Gilliam 		/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
13401ca93273Seota 		if (sscanf(dup, "%s", drv) != 1) {
13417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_BAD_LINE),
13427c478bd9Sstevel@tonic-gate 			    minor_perm, line);
13437c478bd9Sstevel@tonic-gate 			status = ERROR;
13441ca93273Seota 			free(dup);
13451ca93273Seota 			break;
13467c478bd9Sstevel@tonic-gate 		}
13471ca93273Seota 
13481ca93273Seota 		/*
13491ca93273Seota 		 * get the minor name; place the NULL character at the
13501ca93273Seota 		 * end of the driver name, then make the drv_minor
13511ca93273Seota 		 * point to the first character of the minor name.
13521ca93273Seota 		 * the line missing ':' must be treated as a broken one.
13531ca93273Seota 		 */
13541ca93273Seota 		i = strcspn(drv, ":");
13551ca93273Seota 		if (i == strlen(drv)) {
13561ca93273Seota 			(void) fprintf(stderr, gettext(ERR_BAD_LINE),
13571ca93273Seota 			    minor_perm, line);
13581ca93273Seota 			status = ERROR;
13591ca93273Seota 			free(dup);
13601ca93273Seota 			break;
13611ca93273Seota 		}
13627c478bd9Sstevel@tonic-gate 		drv[i] =  '\0';
13631ca93273Seota 		drv_minor = &drv[strlen(drv) + 1];
13647c478bd9Sstevel@tonic-gate 
13651ca93273Seota 		/*
13661ca93273Seota 		 * compare both of the driver name and the minor name.
13671ca93273Seota 		 * then the new line should be written to the file if
13681ca93273Seota 		 * both of them match
13691ca93273Seota 		 */
13707c478bd9Sstevel@tonic-gate 		if ((strcmp(drv, driver_name) == 0) &&
13717c478bd9Sstevel@tonic-gate 		    (strcmp(minor, drv_minor) == 0)) {
13721ca93273Seota 			/* if it has a comment, keep it */
13731ca93273Seota 			if (cp != NULL) {
13741ca93273Seota 				cp++; /* skip a terminator */
13752e107de7SJerry Gilliam 				(void) snprintf(line, sizeof (line),
13762e107de7SJerry Gilliam 				    "%s:%s %s %s %s #%s\n",
13771ca93273Seota 				    drv, minor, perm, own, grp, cp);
13781ca93273Seota 			} else {
13792e107de7SJerry Gilliam 				(void) snprintf(line, sizeof (line),
13802e107de7SJerry Gilliam 				    "%s:%s %s %s %s\n",
13817c478bd9Sstevel@tonic-gate 				    drv, minor, perm, own, grp);
13821ca93273Seota 			}
13837c478bd9Sstevel@tonic-gate 			match = 1;
13847c478bd9Sstevel@tonic-gate 		}
13851ca93273Seota 		free(dup);
13867c478bd9Sstevel@tonic-gate 
13871ca93273Seota 		/* update the file */
13887c478bd9Sstevel@tonic-gate 		if ((fputs(line, newfp)) == EOF) {
13897c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE),
13907c478bd9Sstevel@tonic-gate 			    minor_perm);
13917c478bd9Sstevel@tonic-gate 			status = ERROR;
13927c478bd9Sstevel@tonic-gate 		}
13937c478bd9Sstevel@tonic-gate 	}
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	if (!match) {
13967c478bd9Sstevel@tonic-gate 		(void) bzero(line, sizeof (&line[0]));
13972e107de7SJerry Gilliam 		(void) snprintf(line, sizeof (line),
13982e107de7SJerry Gilliam 		    "%s:%s %s %s %s\n",
13997c478bd9Sstevel@tonic-gate 		    driver_name, minor, perm, own, grp);
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 		/* add the new entry */
14027c478bd9Sstevel@tonic-gate 		if ((fputs(line, newfp)) == EOF) {
14037c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE), minor_perm);
14047c478bd9Sstevel@tonic-gate 			status = ERROR;
14057c478bd9Sstevel@tonic-gate 		}
14067c478bd9Sstevel@tonic-gate 	}
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	if (fflush(newfp) != 0 || fsync(fileno(newfp)) != 0)
14117c478bd9Sstevel@tonic-gate 		status = ERROR;
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	(void) fclose(newfp);
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	/*
14167c478bd9Sstevel@tonic-gate 	 * if error, leave original file, delete new file
14177c478bd9Sstevel@tonic-gate 	 * if noerr, replace original file with new file
14187c478bd9Sstevel@tonic-gate 	 */
14197c478bd9Sstevel@tonic-gate 	if (status == NOERR) {
14207c478bd9Sstevel@tonic-gate 		if (rename(minor_perm, tmphold) == -1) {
14217c478bd9Sstevel@tonic-gate 			perror(NULL);
14227c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE), minor_perm);
14237c478bd9Sstevel@tonic-gate 			(void) unlink(newfile);
14247c478bd9Sstevel@tonic-gate 			return (ERROR);
14257c478bd9Sstevel@tonic-gate 		} else if (rename(newfile, minor_perm) == -1) {
14267c478bd9Sstevel@tonic-gate 			perror(NULL);
14277c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE), minor_perm);
14287c478bd9Sstevel@tonic-gate 			(void) unlink(minor_perm);
14297c478bd9Sstevel@tonic-gate 			(void) unlink(newfile);
14307c478bd9Sstevel@tonic-gate 			if (link(tmphold, minor_perm) == -1) {
14317c478bd9Sstevel@tonic-gate 				perror(NULL);
14327c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_BAD_LINK),
14337c478bd9Sstevel@tonic-gate 				    minor_perm, tmphold);
14347c478bd9Sstevel@tonic-gate 			}
14357c478bd9Sstevel@tonic-gate 			return (ERROR);
14367c478bd9Sstevel@tonic-gate 		}
14377c478bd9Sstevel@tonic-gate 		(void) unlink(tmphold);
14387c478bd9Sstevel@tonic-gate 	} else {
14397c478bd9Sstevel@tonic-gate 		/*
14407c478bd9Sstevel@tonic-gate 		 * since there's an error, leave file alone; remove
14417c478bd9Sstevel@tonic-gate 		 * new file
14427c478bd9Sstevel@tonic-gate 		 */
14437c478bd9Sstevel@tonic-gate 		if (unlink(newfile) == -1) {
14447c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_CANT_RM), newfile);
14457c478bd9Sstevel@tonic-gate 		}
14467c478bd9Sstevel@tonic-gate 		return (ERROR);
14477c478bd9Sstevel@tonic-gate 	}
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	return (NOERR);
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate }
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate /*
14557c478bd9Sstevel@tonic-gate  * list_entry:
14567c478bd9Sstevel@tonic-gate  *	open file
14577c478bd9Sstevel@tonic-gate  *	read thru file, listing all entries if first entry = driver_name
14587c478bd9Sstevel@tonic-gate  *	close
14597c478bd9Sstevel@tonic-gate  */
14607c478bd9Sstevel@tonic-gate void
14617c478bd9Sstevel@tonic-gate list_entry(
14627c478bd9Sstevel@tonic-gate 	char *oldfile,
14637c478bd9Sstevel@tonic-gate 	char *driver_name,
14647c478bd9Sstevel@tonic-gate 	char *marker)
14657c478bd9Sstevel@tonic-gate {
14667c478bd9Sstevel@tonic-gate 	FILE	*fp;
14677c478bd9Sstevel@tonic-gate 	int	i;
14681ca93273Seota 	char	line[MAX_DBFILE_ENTRY], *cp;
14697c478bd9Sstevel@tonic-gate 	char	drv[FILENAME_MAX + 1];
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	if ((fp = fopen(oldfile, "r")) == NULL) {
14727c478bd9Sstevel@tonic-gate 		perror(NULL);
14737c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), oldfile);
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 		return;
14767c478bd9Sstevel@tonic-gate 	}
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line), fp) != NULL) {
14791ca93273Seota 		/* cut off comments starting with '#' */
14801ca93273Seota 		if ((cp = strchr(line, '#')) != NULL)
14811ca93273Seota 			*cp = '\0';
14821ca93273Seota 		/* ignore comment or blank lines */
14831ca93273Seota 		if (is_blank(line))
14847c478bd9Sstevel@tonic-gate 			continue;
14851ca93273Seota 		/* sanity-check */
14862e107de7SJerry Gilliam 		/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
14877c478bd9Sstevel@tonic-gate 		if (sscanf(line, "%s", drv) != 1) {
14887c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_BAD_LINE),
14897c478bd9Sstevel@tonic-gate 			    oldfile, line);
14907c478bd9Sstevel@tonic-gate 		}
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 		for (i = strcspn(drv, marker); i < FILENAME_MAX; i++) {
14937c478bd9Sstevel@tonic-gate 			drv[i] =  '\0';
14947c478bd9Sstevel@tonic-gate 		}
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 		if (strcmp(driver_name, drv) == 0) {
14977c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "%s", line);
14987c478bd9Sstevel@tonic-gate 		}
14997c478bd9Sstevel@tonic-gate 	}
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
15027c478bd9Sstevel@tonic-gate }
15037c478bd9Sstevel@tonic-gate 
15041ca93273Seota static boolean_t
15051ca93273Seota is_token(char *tok)
15061ca93273Seota {
15071ca93273Seota 	/*
15081ca93273Seota 	 * Check the token here. According to IEEE1275 Open Firmware Boot
15091ca93273Seota 	 * Standard, the name is composed of 1 to 31 letters,
15101ca93273Seota 	 * digits and punctuation characters from the set ",._+-", and
15111ca93273Seota 	 * uppercase and lowercase characters are considered distinct.
15121ca93273Seota 	 * (ie. token := [a-zA-Z0-9,._+-]+, length(token) <= 31)
15131ca93273Seota 	 * However, since either the definition of driver or aliase names is
15141ca93273Seota 	 * not known well, only '#' is avoided explicitly. (the kernel lexical
15151ca93273Seota 	 * analyzer treats it as a start of a comment)
15161ca93273Seota 	 */
15171ca93273Seota 	for (/* nothing */; *tok != '\0'; tok++)
15181ca93273Seota 		if (*tok == '#' || iscntrl(*tok))
15191ca93273Seota 			return (B_FALSE);
15201ca93273Seota 
15211ca93273Seota 	return (B_TRUE);
15221ca93273Seota }
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate /*
15257c478bd9Sstevel@tonic-gate  * check each entry in perm_list for:
15267c478bd9Sstevel@tonic-gate  *	4 arguments
15277c478bd9Sstevel@tonic-gate  *	permission arg is in valid range
15287c478bd9Sstevel@tonic-gate  * permlist entries separated by comma
15297c478bd9Sstevel@tonic-gate  * return ERROR/NOERR
15307c478bd9Sstevel@tonic-gate  */
15317c478bd9Sstevel@tonic-gate int
15327c478bd9Sstevel@tonic-gate check_perm_opts(char *perm_list)
15337c478bd9Sstevel@tonic-gate {
15347c478bd9Sstevel@tonic-gate 	char *current_head;
15357c478bd9Sstevel@tonic-gate 	char *previous_head;
15367c478bd9Sstevel@tonic-gate 	char *one_entry;
15377c478bd9Sstevel@tonic-gate 	int i, len, scan_stat;
15387c478bd9Sstevel@tonic-gate 	char minor[FILENAME_MAX + 1];
15397c478bd9Sstevel@tonic-gate 	char perm[OPT_LEN + 1];
15407c478bd9Sstevel@tonic-gate 	char own[OPT_LEN + 1];
15417c478bd9Sstevel@tonic-gate 	char grp[OPT_LEN + 1];
15427c478bd9Sstevel@tonic-gate 	char dumb[OPT_LEN + 1];
15437c478bd9Sstevel@tonic-gate 	int status = NOERR;
15447c478bd9Sstevel@tonic-gate 	int intperm;
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	len = strlen(perm_list);
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	if (len == 0) {
15497c478bd9Sstevel@tonic-gate 		return (ERROR);
15507c478bd9Sstevel@tonic-gate 	}
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 	one_entry = calloc(len + 1, 1);
15537c478bd9Sstevel@tonic-gate 	if (one_entry == NULL) {
15547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
15557c478bd9Sstevel@tonic-gate 		return (ERROR);
15567c478bd9Sstevel@tonic-gate 	}
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	previous_head = perm_list;
15597c478bd9Sstevel@tonic-gate 	current_head = perm_list;
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	while (*current_head != '\0') {
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 		for (i = 0; i <= len; i++)
15647c478bd9Sstevel@tonic-gate 			one_entry[i] = 0;
15657c478bd9Sstevel@tonic-gate 
1566f4da9be0Scth 		current_head = get_entry(previous_head, one_entry, ',', 0);
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 		previous_head = current_head;
15692e107de7SJerry Gilliam 		/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
15707c478bd9Sstevel@tonic-gate 		scan_stat = sscanf(one_entry, "%s%s%s%s%s", minor, perm, own,
15717c478bd9Sstevel@tonic-gate 		    grp, dumb);
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 		if (scan_stat < 4) {
15747c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_MIS_TOK),
15757c478bd9Sstevel@tonic-gate 			    "-m", one_entry);
15767c478bd9Sstevel@tonic-gate 			status = ERROR;
15777c478bd9Sstevel@tonic-gate 		}
15787c478bd9Sstevel@tonic-gate 		if (scan_stat > 4) {
15797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_TOO_MANY_ARGS),
15807c478bd9Sstevel@tonic-gate 			    "-m", one_entry);
15817c478bd9Sstevel@tonic-gate 			status = ERROR;
15827c478bd9Sstevel@tonic-gate 		}
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 		intperm = atoi(perm);
15857c478bd9Sstevel@tonic-gate 		if (intperm < 0000 || intperm > 4777) {
15867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_BAD_MODE), perm);
15877c478bd9Sstevel@tonic-gate 			status = ERROR;
15887c478bd9Sstevel@tonic-gate 		}
15897c478bd9Sstevel@tonic-gate 	}
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 	free(one_entry);
15927c478bd9Sstevel@tonic-gate 	return (status);
15937c478bd9Sstevel@tonic-gate }
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate /*
15977c478bd9Sstevel@tonic-gate  * check each alias :
15987c478bd9Sstevel@tonic-gate  *	alias list members separated by white space
15997c478bd9Sstevel@tonic-gate  *	cannot exist as driver name in /etc/name_to_major
16007c478bd9Sstevel@tonic-gate  *	cannot exist as driver or alias name in /etc/driver_aliases
16017c478bd9Sstevel@tonic-gate  */
16027c478bd9Sstevel@tonic-gate int
16037c478bd9Sstevel@tonic-gate aliases_unique(char *aliases)
16047c478bd9Sstevel@tonic-gate {
16057c478bd9Sstevel@tonic-gate 	char *current_head;
16067c478bd9Sstevel@tonic-gate 	char *previous_head;
16077c478bd9Sstevel@tonic-gate 	char *one_entry;
16086532b960SJerry Gilliam 	int len;
16097c478bd9Sstevel@tonic-gate 	int is_unique;
16106532b960SJerry Gilliam 	int err;
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	len = strlen(aliases);
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	one_entry = calloc(len + 1, 1);
16157c478bd9Sstevel@tonic-gate 	if (one_entry == NULL) {
16167c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
16177c478bd9Sstevel@tonic-gate 		return (ERROR);
16187c478bd9Sstevel@tonic-gate 	}
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	previous_head = aliases;
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 	do {
16236532b960SJerry Gilliam 		bzero(one_entry, len+1);
1624f4da9be0Scth 		current_head = get_entry(previous_head, one_entry, ' ', 1);
16257c478bd9Sstevel@tonic-gate 		previous_head = current_head;
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 		if ((unique_driver_name(one_entry, name_to_major,
16286532b960SJerry Gilliam 		    &is_unique)) == ERROR)
16296532b960SJerry Gilliam 			goto err_out;
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 		if (is_unique != UNIQUE) {
16327c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_ALIAS_IN_NAM_MAJ),
16337c478bd9Sstevel@tonic-gate 			    one_entry);
16346532b960SJerry Gilliam 			goto err_out;
16357c478bd9Sstevel@tonic-gate 		}
16367c478bd9Sstevel@tonic-gate 
16376532b960SJerry Gilliam 		if ((err = unique_drv_alias(one_entry)) != UNIQUE) {
16386532b960SJerry Gilliam 			if (err == NOT_UNIQUE) {
16396532b960SJerry Gilliam 				(void) fprintf(stderr,
16406532b960SJerry Gilliam 				    gettext(ERR_ALIAS_IN_USE), one_entry);
16416532b960SJerry Gilliam 			}
16426532b960SJerry Gilliam 			goto err_out;
16437c478bd9Sstevel@tonic-gate 		}
16447c478bd9Sstevel@tonic-gate 
16451ca93273Seota 		if (!is_token(one_entry)) {
16461ca93273Seota 			(void) fprintf(stderr, gettext(ERR_BAD_TOK),
16471ca93273Seota 			    "-i", one_entry);
16486532b960SJerry Gilliam 			goto err_out;
16491ca93273Seota 		}
16501ca93273Seota 
16517c478bd9Sstevel@tonic-gate 	} while (*current_head != '\0');
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 	free(one_entry);
16547c478bd9Sstevel@tonic-gate 	return (NOERR);
16557c478bd9Sstevel@tonic-gate 
16566532b960SJerry Gilliam err_out:
16576532b960SJerry Gilliam 	free(one_entry);
16586532b960SJerry Gilliam 	return (ERROR);
16596532b960SJerry Gilliam }
16606532b960SJerry Gilliam 
16616532b960SJerry Gilliam /*
16626532b960SJerry Gilliam  * verify each alias :
16636532b960SJerry Gilliam  *	alias list members separated by white space and quoted
16646532b960SJerry Gilliam  *	exist as alias name in /etc/driver_aliases
16656532b960SJerry Gilliam  */
16666532b960SJerry Gilliam int
16672e107de7SJerry Gilliam aliases_exist(char *aliases)
16686532b960SJerry Gilliam {
16696532b960SJerry Gilliam 	char *current_head;
16706532b960SJerry Gilliam 	char *previous_head;
16716532b960SJerry Gilliam 	char *one_entry;
16726532b960SJerry Gilliam 	int len;
16736532b960SJerry Gilliam 
16746532b960SJerry Gilliam 	len = strlen(aliases);
16756532b960SJerry Gilliam 
16766532b960SJerry Gilliam 	one_entry = calloc(len + 1, 1);
16776532b960SJerry Gilliam 	if (one_entry == NULL) {
16786532b960SJerry Gilliam 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
16796532b960SJerry Gilliam 		return (ERROR);
16806532b960SJerry Gilliam 	}
16816532b960SJerry Gilliam 
16826532b960SJerry Gilliam 	previous_head = aliases;
16836532b960SJerry Gilliam 
16846532b960SJerry Gilliam 	do {
16856532b960SJerry Gilliam 		bzero(one_entry, len+1);
16866532b960SJerry Gilliam 		current_head = get_entry(previous_head, one_entry, ' ', 1);
16876532b960SJerry Gilliam 		previous_head = current_head;
16886532b960SJerry Gilliam 
16892e107de7SJerry Gilliam 		if (unique_drv_alias(one_entry) != NOT_UNIQUE)
16906532b960SJerry Gilliam 			goto err_out;
16916532b960SJerry Gilliam 
16926532b960SJerry Gilliam 		if (!is_token(one_entry)) {
16936532b960SJerry Gilliam 			(void) fprintf(stderr, gettext(ERR_BAD_TOK),
16946532b960SJerry Gilliam 			    "-i", one_entry);
16956532b960SJerry Gilliam 			goto err_out;
16966532b960SJerry Gilliam 		}
16976532b960SJerry Gilliam 
16986532b960SJerry Gilliam 	} while (*current_head != '\0');
16996532b960SJerry Gilliam 
17006532b960SJerry Gilliam 	free(one_entry);
17016532b960SJerry Gilliam 	return (NOERR);
17026532b960SJerry Gilliam 
17036532b960SJerry Gilliam err_out:
17046532b960SJerry Gilliam 	free(one_entry);
17056532b960SJerry Gilliam 	return (ERROR);
17067c478bd9Sstevel@tonic-gate }
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 
1709f4da9be0Scth /*
1710f4da9be0Scth  * check each alias :
1711f4da9be0Scth  *	if path-oriented alias, path exists
1712f4da9be0Scth  */
1713f4da9be0Scth int
1714f4da9be0Scth aliases_paths_exist(char *aliases)
1715f4da9be0Scth {
1716f4da9be0Scth 	char *current_head;
1717f4da9be0Scth 	char *previous_head;
1718f4da9be0Scth 	char *one_entry;
1719f4da9be0Scth 	int i, len;
1720f4da9be0Scth 	char path[MAXPATHLEN];
1721f4da9be0Scth 	struct stat buf;
1722f4da9be0Scth 
1723f4da9be0Scth 	len = strlen(aliases);
1724f4da9be0Scth 
1725f4da9be0Scth 	one_entry = calloc(len + 1, 1);
1726f4da9be0Scth 	if (one_entry == NULL) {
1727f4da9be0Scth 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
1728f4da9be0Scth 		return (ERROR);
1729f4da9be0Scth 	}
1730f4da9be0Scth 
1731f4da9be0Scth 	previous_head = aliases;
1732f4da9be0Scth 
1733f4da9be0Scth 	do {
1734f4da9be0Scth 		for (i = 0; i <= len; i++)
1735f4da9be0Scth 			one_entry[i] = 0;
1736f4da9be0Scth 
1737f4da9be0Scth 		current_head = get_entry(previous_head, one_entry, ' ', 1);
1738f4da9be0Scth 		previous_head = current_head;
1739f4da9be0Scth 
1740f4da9be0Scth 		/* if the alias is a path, ensure that the path exists */
1741f4da9be0Scth 		if (*one_entry != '/')
1742f4da9be0Scth 			continue;
1743f4da9be0Scth 		(void) snprintf(path, sizeof (path), "/devices/%s", one_entry);
1744f4da9be0Scth 		if (stat(path, &buf) == 0)
1745f4da9be0Scth 			continue;
1746f4da9be0Scth 
1747f4da9be0Scth 		/* no device at specified path-oriented alias path */
1748f4da9be0Scth 		(void) fprintf(stderr, gettext(ERR_PATH_ORIENTED_ALIAS),
1749f4da9be0Scth 		    one_entry);
1750f4da9be0Scth 		free(one_entry);
1751f4da9be0Scth 		return (ERROR);
1752f4da9be0Scth 
1753f4da9be0Scth 	} while (*current_head != '\0');
1754f4da9be0Scth 
1755f4da9be0Scth 	free(one_entry);
1756f4da9be0Scth 
1757f4da9be0Scth 	return (NOERR);
1758f4da9be0Scth }
1759f4da9be0Scth 
1760f4da9be0Scth 
17617c478bd9Sstevel@tonic-gate int
17627c478bd9Sstevel@tonic-gate update_driver_aliases(
17637c478bd9Sstevel@tonic-gate 	char *driver_name,
17647c478bd9Sstevel@tonic-gate 	char *aliases)
17657c478bd9Sstevel@tonic-gate {
17667c478bd9Sstevel@tonic-gate 	/* make call to update the aliases file */
1767f4da9be0Scth 	return (append_to_file(driver_name, aliases, driver_aliases,
1768f4da9be0Scth 	    ' ', " ", 1));
17697c478bd9Sstevel@tonic-gate }
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 
17726532b960SJerry Gilliam /*
17736532b960SJerry Gilliam  * Return:
17746532b960SJerry Gilliam  *	ERROR in case of memory or read error
17756532b960SJerry Gilliam  *	UNIQUE if there is no existing match to the supplied alias
17766532b960SJerry Gilliam  *	NOT_UNIQUE if there is a match
17776532b960SJerry Gilliam  * An error message is emitted in the case of ERROR,
17786532b960SJerry Gilliam  * up to the caller otherwise.
17796532b960SJerry Gilliam  */
17807c478bd9Sstevel@tonic-gate int
17817c478bd9Sstevel@tonic-gate unique_drv_alias(char *drv_alias)
17827c478bd9Sstevel@tonic-gate {
17837c478bd9Sstevel@tonic-gate 	FILE *fp;
17847c478bd9Sstevel@tonic-gate 	char drv[FILENAME_MAX + 1];
17851ca93273Seota 	char line[MAX_N2M_ALIAS_LINE + 1], *cp;
17867c478bd9Sstevel@tonic-gate 	char alias[FILENAME_MAX + 1];
1787f4da9be0Scth 	char *a;
17886532b960SJerry Gilliam 	int status = UNIQUE;
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 	fp = fopen(driver_aliases, "r");
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 	if (fp != NULL) {
17937c478bd9Sstevel@tonic-gate 		while ((fgets(line, sizeof (line), fp) != 0) &&
17946532b960SJerry Gilliam 		    status == UNIQUE) {
17951ca93273Seota 			/* cut off comments starting with '#' */
17961ca93273Seota 			if ((cp = strchr(line, '#')) != NULL)
17971ca93273Seota 				*cp = '\0';
17981ca93273Seota 			/* ignore comment or blank lines */
17991ca93273Seota 			if (is_blank(line))
18001ca93273Seota 				continue;
18011ca93273Seota 			/* sanity-check */
18022e107de7SJerry Gilliam 			/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
18037c478bd9Sstevel@tonic-gate 			if (sscanf(line, "%s %s", drv, alias) != 2)
18047c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_BAD_LINE),
18057c478bd9Sstevel@tonic-gate 				    driver_aliases, line);
18067c478bd9Sstevel@tonic-gate 
1807f4da9be0Scth 			/* unquote for compare */
1808f4da9be0Scth 			if ((*alias == '"') &&
1809f4da9be0Scth 			    (*(alias + strlen(alias) - 1) == '"')) {
1810f4da9be0Scth 				a = &alias[1];
1811f4da9be0Scth 				alias[strlen(alias) - 1] = '\0';
1812f4da9be0Scth 			} else
1813f4da9be0Scth 				a = alias;
1814f4da9be0Scth 
18157c478bd9Sstevel@tonic-gate 			if ((strcmp(drv_alias, drv) == 0) ||
1816f4da9be0Scth 			    (strcmp(drv_alias, a) == 0)) {
18176532b960SJerry Gilliam 				status = NOT_UNIQUE;
18186532b960SJerry Gilliam 				break;
18197c478bd9Sstevel@tonic-gate 			}
18207c478bd9Sstevel@tonic-gate 		}
18217c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
18227c478bd9Sstevel@tonic-gate 		return (status);
18237c478bd9Sstevel@tonic-gate 	} else {
18247c478bd9Sstevel@tonic-gate 		perror(NULL);
18257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_OPEN), driver_aliases);
18267c478bd9Sstevel@tonic-gate 		return (ERROR);
18277c478bd9Sstevel@tonic-gate 	}
18287c478bd9Sstevel@tonic-gate }
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate /*
18327c478bd9Sstevel@tonic-gate  * search for driver_name in first field of file file_name
18336532b960SJerry Gilliam  * searching name_to_major and driver_aliases: name separated
18346532b960SJerry Gilliam  * from the remainder of the line by white space.
18357c478bd9Sstevel@tonic-gate  */
18367c478bd9Sstevel@tonic-gate int
18377c478bd9Sstevel@tonic-gate unique_driver_name(char *driver_name, char *file_name,
18387c478bd9Sstevel@tonic-gate 	int *is_unique)
18397c478bd9Sstevel@tonic-gate {
18406532b960SJerry Gilliam 	int ret, err;
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	if ((ret = get_major_no(driver_name, file_name)) == ERROR) {
18437c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
18447c478bd9Sstevel@tonic-gate 		    file_name);
18457c478bd9Sstevel@tonic-gate 	} else {
18467c478bd9Sstevel@tonic-gate 		/* check alias file for name collision */
18476532b960SJerry Gilliam 		if ((err = unique_drv_alias(driver_name)) != UNIQUE) {
18486532b960SJerry Gilliam 			if (err == NOT_UNIQUE) {
18496532b960SJerry Gilliam 				(void) fprintf(stderr,
18506532b960SJerry Gilliam 				    gettext(ERR_ALIAS_IN_USE),
18516532b960SJerry Gilliam 				    driver_name);
18526532b960SJerry Gilliam 			}
18537c478bd9Sstevel@tonic-gate 			ret = ERROR;
18547c478bd9Sstevel@tonic-gate 		} else {
18557c478bd9Sstevel@tonic-gate 			if (ret != UNIQUE)
18567c478bd9Sstevel@tonic-gate 				*is_unique = NOT_UNIQUE;
18577c478bd9Sstevel@tonic-gate 			else
18587c478bd9Sstevel@tonic-gate 				*is_unique = ret;
18597c478bd9Sstevel@tonic-gate 			ret = NOERR;
18607c478bd9Sstevel@tonic-gate 		}
18617c478bd9Sstevel@tonic-gate 	}
18627c478bd9Sstevel@tonic-gate 	return (ret);
18637c478bd9Sstevel@tonic-gate }
18647c478bd9Sstevel@tonic-gate 
18650013e2d3SJerry Gilliam /*
18660013e2d3SJerry Gilliam  * returns:
18670013e2d3SJerry Gilliam  *	SUCCESS - not an existing driver alias
18680013e2d3SJerry Gilliam  *	NOT_UNIQUE - matching driver alias exists
18690013e2d3SJerry Gilliam  *	ERROR - an error occurred
18700013e2d3SJerry Gilliam  */
18710013e2d3SJerry Gilliam int
18720013e2d3SJerry Gilliam check_duplicate_driver_alias(char *driver_name, char *drv_alias)
18730013e2d3SJerry Gilliam {
18740013e2d3SJerry Gilliam 	FILE *fp;
18750013e2d3SJerry Gilliam 	char drv[FILENAME_MAX + 1];
18760013e2d3SJerry Gilliam 	char line[MAX_N2M_ALIAS_LINE + 1], *cp;
18770013e2d3SJerry Gilliam 	char alias[FILENAME_MAX + 1];
18780013e2d3SJerry Gilliam 	char *a;
18790013e2d3SJerry Gilliam 	int status = SUCCESS;
18800013e2d3SJerry Gilliam 
18810013e2d3SJerry Gilliam 	if ((fp = fopen(driver_aliases, "r")) == NULL) {
18820013e2d3SJerry Gilliam 		perror(NULL);
18830013e2d3SJerry Gilliam 		(void) fprintf(stderr, gettext(ERR_CANT_OPEN), driver_aliases);
18840013e2d3SJerry Gilliam 		return (ERROR);
18850013e2d3SJerry Gilliam 	}
18860013e2d3SJerry Gilliam 
18870013e2d3SJerry Gilliam 	while (fgets(line, sizeof (line), fp) != 0) {
18880013e2d3SJerry Gilliam 		/* cut off comments starting with '#' */
18890013e2d3SJerry Gilliam 		if ((cp = strchr(line, '#')) != NULL)
18900013e2d3SJerry Gilliam 			*cp = '\0';
18910013e2d3SJerry Gilliam 		/* ignore comment or blank lines */
18920013e2d3SJerry Gilliam 		if (is_blank(line))
18930013e2d3SJerry Gilliam 			continue;
18940013e2d3SJerry Gilliam 		/* sanity-check */
18952e107de7SJerry Gilliam 		/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
18960013e2d3SJerry Gilliam 		if (sscanf(line, "%s %s", drv, alias) != 2)
18970013e2d3SJerry Gilliam 			(void) fprintf(stderr, gettext(ERR_BAD_LINE),
18980013e2d3SJerry Gilliam 			    driver_aliases, line);
18990013e2d3SJerry Gilliam 
19000013e2d3SJerry Gilliam 		/* unquote for compare */
19010013e2d3SJerry Gilliam 		if ((*alias == '"') &&
19020013e2d3SJerry Gilliam 		    (*(alias + strlen(alias) - 1) == '"')) {
19030013e2d3SJerry Gilliam 			a = &alias[1];
19040013e2d3SJerry Gilliam 			alias[strlen(alias) - 1] = '\0';
19050013e2d3SJerry Gilliam 		} else
19060013e2d3SJerry Gilliam 			a = alias;
19070013e2d3SJerry Gilliam 
19080013e2d3SJerry Gilliam 		if ((strcmp(drv_alias, a) == 0) &&
19090013e2d3SJerry Gilliam 		    (strcmp(drv, driver_name) == 0)) {
19100013e2d3SJerry Gilliam 			status = NOT_UNIQUE;
19110013e2d3SJerry Gilliam 		}
19120013e2d3SJerry Gilliam 
19130013e2d3SJerry Gilliam 		if ((strcmp(drv_alias, drv) == 0) ||
19140013e2d3SJerry Gilliam 		    ((strcmp(drv_alias, a) == 0) &&
19150013e2d3SJerry Gilliam 		    (strcmp(drv, driver_name) != 0))) {
19160013e2d3SJerry Gilliam 			(void) fprintf(stderr,
19170013e2d3SJerry Gilliam 			    gettext(ERR_ALIAS_IN_USE),
19180013e2d3SJerry Gilliam 			    drv_alias);
19190013e2d3SJerry Gilliam 			status = ERROR;
19200013e2d3SJerry Gilliam 			goto done;
19210013e2d3SJerry Gilliam 		}
19220013e2d3SJerry Gilliam 	}
19230013e2d3SJerry Gilliam 
19240013e2d3SJerry Gilliam done:
19250013e2d3SJerry Gilliam 	(void) fclose(fp);
19260013e2d3SJerry Gilliam 	return (status);
19270013e2d3SJerry Gilliam }
19280013e2d3SJerry Gilliam 
19290013e2d3SJerry Gilliam int
19300013e2d3SJerry Gilliam trim_duplicate_aliases(char *driver_name, char *aliases, char **aliases2p)
19310013e2d3SJerry Gilliam {
19320013e2d3SJerry Gilliam 	char *current_head;
19330013e2d3SJerry Gilliam 	char *previous_head;
19340013e2d3SJerry Gilliam 	char *one_entry;
19350013e2d3SJerry Gilliam 	char *aliases2;
19360013e2d3SJerry Gilliam 	int rv, len;
19370013e2d3SJerry Gilliam 	int n = 0;
19380013e2d3SJerry Gilliam 
19390013e2d3SJerry Gilliam 	*aliases2p = NULL;
19400013e2d3SJerry Gilliam 	len = strlen(aliases) + 1;
19410013e2d3SJerry Gilliam 
19420013e2d3SJerry Gilliam 	one_entry = calloc(len, 1);
19430013e2d3SJerry Gilliam 	aliases2 = calloc(len, 1);
19440013e2d3SJerry Gilliam 	if (one_entry == NULL || aliases2 == NULL) {
19450013e2d3SJerry Gilliam 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
19460013e2d3SJerry Gilliam 		return (ERROR);
19470013e2d3SJerry Gilliam 	}
19480013e2d3SJerry Gilliam 
19490013e2d3SJerry Gilliam 	previous_head = aliases;
19500013e2d3SJerry Gilliam 
19510013e2d3SJerry Gilliam 	do {
19520013e2d3SJerry Gilliam 		(void) bzero(one_entry, len);
19530013e2d3SJerry Gilliam 		current_head = get_entry(previous_head, one_entry, ' ', 1);
19540013e2d3SJerry Gilliam 		previous_head = current_head;
19550013e2d3SJerry Gilliam 
19560013e2d3SJerry Gilliam 		rv = check_duplicate_driver_alias(driver_name, one_entry);
19570013e2d3SJerry Gilliam 		switch (rv) {
19580013e2d3SJerry Gilliam 		case SUCCESS:
19590013e2d3SJerry Gilliam 			/* not an existing driver alias: add it */
19600013e2d3SJerry Gilliam 			if (n > 0) {
19610013e2d3SJerry Gilliam 				if (strlcat(aliases2, " ", len) >= len)
19620013e2d3SJerry Gilliam 					goto err;
19630013e2d3SJerry Gilliam 			}
19640013e2d3SJerry Gilliam 			if (strlcat(aliases2, one_entry, len) >= len)
19650013e2d3SJerry Gilliam 				goto err;
19660013e2d3SJerry Gilliam 			n++;
19670013e2d3SJerry Gilliam 			break;
19680013e2d3SJerry Gilliam 		case NOT_UNIQUE:
19690013e2d3SJerry Gilliam 			/* matching driver alias exists: do not add it */
19700013e2d3SJerry Gilliam 			break;
19710013e2d3SJerry Gilliam 		case ERROR:
19720013e2d3SJerry Gilliam 			/* error reading the alias file */
19730013e2d3SJerry Gilliam 			goto err;
19740013e2d3SJerry Gilliam 		default:
19750013e2d3SJerry Gilliam 			goto err;
19760013e2d3SJerry Gilliam 		}
19770013e2d3SJerry Gilliam 
19780013e2d3SJerry Gilliam 		if (!is_token(one_entry)) {
19790013e2d3SJerry Gilliam 			(void) fprintf(stderr, gettext(ERR_BAD_TOK),
19800013e2d3SJerry Gilliam 			    "-i", one_entry);
19810013e2d3SJerry Gilliam 			goto err;
19820013e2d3SJerry Gilliam 		}
19830013e2d3SJerry Gilliam 	} while (*current_head != '\0');
19840013e2d3SJerry Gilliam 
19850013e2d3SJerry Gilliam 	/*
19860013e2d3SJerry Gilliam 	 * If all the aliases listed are already
19870013e2d3SJerry Gilliam 	 * present we actually have none to do.
19880013e2d3SJerry Gilliam 	 */
19890013e2d3SJerry Gilliam 	if (n == 0) {
19900013e2d3SJerry Gilliam 		free(aliases2);
19910013e2d3SJerry Gilliam 	} else {
19920013e2d3SJerry Gilliam 		*aliases2p = aliases2;
19930013e2d3SJerry Gilliam 	}
19940013e2d3SJerry Gilliam 	free(one_entry);
19950013e2d3SJerry Gilliam 	return (NOERR);
19960013e2d3SJerry Gilliam 
19970013e2d3SJerry Gilliam err:
19980013e2d3SJerry Gilliam 	free(aliases2);
19990013e2d3SJerry Gilliam 	free(one_entry);
20000013e2d3SJerry Gilliam 	return (ERROR);
20010013e2d3SJerry Gilliam }
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate int
20047c478bd9Sstevel@tonic-gate check_space_within_quote(char *str)
20057c478bd9Sstevel@tonic-gate {
20067c478bd9Sstevel@tonic-gate 	register int i;
20077c478bd9Sstevel@tonic-gate 	register int len;
20087c478bd9Sstevel@tonic-gate 	int quoted = 0;
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 	len = strlen(str);
20117c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++, str++) {
20127c478bd9Sstevel@tonic-gate 		if (*str == '"') {
20137c478bd9Sstevel@tonic-gate 			if (quoted == 0)
20147c478bd9Sstevel@tonic-gate 				quoted++;
20157c478bd9Sstevel@tonic-gate 			else
20167c478bd9Sstevel@tonic-gate 				quoted--;
20177c478bd9Sstevel@tonic-gate 		} else if (*str == ' ' && quoted)
20187c478bd9Sstevel@tonic-gate 			return (ERROR);
20197c478bd9Sstevel@tonic-gate 	}
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 	return (0);
20227c478bd9Sstevel@tonic-gate }
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate /*
20267c478bd9Sstevel@tonic-gate  * get major number
20277c478bd9Sstevel@tonic-gate  * write driver_name major_num to name_to_major file
20287c478bd9Sstevel@tonic-gate  * major_num returned in major_num
20297c478bd9Sstevel@tonic-gate  * return success/failure
20307c478bd9Sstevel@tonic-gate  */
20317c478bd9Sstevel@tonic-gate int
20327c478bd9Sstevel@tonic-gate update_name_to_major(char *driver_name, major_t *major_num, int server)
20337c478bd9Sstevel@tonic-gate {
20347c478bd9Sstevel@tonic-gate 	char major[MAX_STR_MAJOR + 1];
20357c478bd9Sstevel@tonic-gate 	struct stat buf;
20367c478bd9Sstevel@tonic-gate 	char *num_list;
20377c478bd9Sstevel@tonic-gate 	char drv_majnum_str[MAX_STR_MAJOR + 1];
20387c478bd9Sstevel@tonic-gate 	int new_maj = -1;
20397c478bd9Sstevel@tonic-gate 	int i, tmp = 0, is_unique, have_rem_n2m = 0;
20407c478bd9Sstevel@tonic-gate 	int max_dev = 0;
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	/*
20437c478bd9Sstevel@tonic-gate 	 * if driver_name already in rem_name_to_major
20447c478bd9Sstevel@tonic-gate 	 * 	delete entry from rem_nam_to_major
20457c478bd9Sstevel@tonic-gate 	 *	put entry into name_to_major
20467c478bd9Sstevel@tonic-gate 	 */
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 	if (stat(rem_name_to_major, &buf) == 0) {
20497c478bd9Sstevel@tonic-gate 		have_rem_n2m = 1;
20507c478bd9Sstevel@tonic-gate 	}
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 	if (have_rem_n2m) {
20537c478bd9Sstevel@tonic-gate 		if ((is_unique = get_major_no(driver_name, rem_name_to_major))
20547c478bd9Sstevel@tonic-gate 		    == ERROR)
20557c478bd9Sstevel@tonic-gate 			return (ERROR);
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 		/*
20587c478bd9Sstevel@tonic-gate 		 * found a match in rem_name_to_major
20597c478bd9Sstevel@tonic-gate 		 */
20607c478bd9Sstevel@tonic-gate 		if (is_unique != UNIQUE) {
20617c478bd9Sstevel@tonic-gate 			char scratch[FILENAME_MAX];
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 			/*
20647c478bd9Sstevel@tonic-gate 			 * If there is a match in /etc/rem_name_to_major then
20657c478bd9Sstevel@tonic-gate 			 * be paranoid: is that major number already in
20667c478bd9Sstevel@tonic-gate 			 * /etc/name_to_major (potentially under another name)?
20677c478bd9Sstevel@tonic-gate 			 */
20687c478bd9Sstevel@tonic-gate 			if (get_driver_name(is_unique, name_to_major,
20697c478bd9Sstevel@tonic-gate 			    scratch) != UNIQUE) {
20707c478bd9Sstevel@tonic-gate 				/*
20717c478bd9Sstevel@tonic-gate 				 * nuke the rem_name_to_major entry-- it
20727c478bd9Sstevel@tonic-gate 				 * isn't helpful.
20737c478bd9Sstevel@tonic-gate 				 */
20747c478bd9Sstevel@tonic-gate 				(void) delete_entry(rem_name_to_major,
20757c478bd9Sstevel@tonic-gate 				    driver_name, " ", NULL);
20767c478bd9Sstevel@tonic-gate 			} else {
20777c478bd9Sstevel@tonic-gate 				(void) snprintf(major, sizeof (major),
20787c478bd9Sstevel@tonic-gate 				    "%d", is_unique);
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 				if (append_to_file(driver_name, major,
2081f4da9be0Scth 				    name_to_major, ' ', " ", 0) == ERROR) {
20827c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
20837c478bd9Sstevel@tonic-gate 					    gettext(ERR_NO_UPDATE),
20847c478bd9Sstevel@tonic-gate 					    name_to_major);
20857c478bd9Sstevel@tonic-gate 					return (ERROR);
20867c478bd9Sstevel@tonic-gate 				}
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 				if (delete_entry(rem_name_to_major,
20897c478bd9Sstevel@tonic-gate 				    driver_name, " ", NULL) == ERROR) {
20907c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
20917c478bd9Sstevel@tonic-gate 					    gettext(ERR_DEL_ENTRY), driver_name,
20927c478bd9Sstevel@tonic-gate 					    rem_name_to_major);
20937c478bd9Sstevel@tonic-gate 					return (ERROR);
20947c478bd9Sstevel@tonic-gate 				}
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 				/* found matching entry : no errors */
20977c478bd9Sstevel@tonic-gate 				*major_num = is_unique;
20987c478bd9Sstevel@tonic-gate 				return (NOERR);
20997c478bd9Sstevel@tonic-gate 			}
21007c478bd9Sstevel@tonic-gate 		}
21017c478bd9Sstevel@tonic-gate 	}
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	/*
21047c478bd9Sstevel@tonic-gate 	 * Bugid: 1264079
21057c478bd9Sstevel@tonic-gate 	 * In a server case (with -b option), we can't use modctl() to find
21067c478bd9Sstevel@tonic-gate 	 *    the maximum major number, we need to dig thru client's
21077c478bd9Sstevel@tonic-gate 	 *    /etc/name_to_major and /etc/rem_name_to_major for the max_dev.
21087c478bd9Sstevel@tonic-gate 	 *
21097c478bd9Sstevel@tonic-gate 	 * if (server)
21107c478bd9Sstevel@tonic-gate 	 *    get maximum major number thru (rem_)name_to_major file on client
21117c478bd9Sstevel@tonic-gate 	 * else
21127c478bd9Sstevel@tonic-gate 	 *    get maximum major number allowable on current system using modctl
21137c478bd9Sstevel@tonic-gate 	 */
21147c478bd9Sstevel@tonic-gate 	if (server) {
21157c478bd9Sstevel@tonic-gate 		max_dev = 0;
21167c478bd9Sstevel@tonic-gate 		tmp = 0;
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 		max_dev = get_max_major(name_to_major);
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 		/* If rem_name_to_major exists, we need to check it too */
21217c478bd9Sstevel@tonic-gate 		if (have_rem_n2m) {
21227c478bd9Sstevel@tonic-gate 			tmp = get_max_major(rem_name_to_major);
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 			/*
21257c478bd9Sstevel@tonic-gate 			 * If name_to_major is missing, we can get max_dev from
21267c478bd9Sstevel@tonic-gate 			 * /etc/rem_name_to_major.  If both missing, bail out!
21277c478bd9Sstevel@tonic-gate 			 */
21287c478bd9Sstevel@tonic-gate 			if ((max_dev == ERROR) && (tmp == ERROR)) {
21297c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
21307c478bd9Sstevel@tonic-gate 				    gettext(ERR_CANT_ACCESS_FILE),
21317c478bd9Sstevel@tonic-gate 				    name_to_major);
21327c478bd9Sstevel@tonic-gate 				return (ERROR);
21337c478bd9Sstevel@tonic-gate 			}
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 			/* guard against bigger maj_num in rem_name_to_major */
21367c478bd9Sstevel@tonic-gate 			if (tmp > max_dev)
21377c478bd9Sstevel@tonic-gate 				max_dev = tmp;
21387c478bd9Sstevel@tonic-gate 		} else {
21397c478bd9Sstevel@tonic-gate 			/*
21407c478bd9Sstevel@tonic-gate 			 * If we can't get major from name_to_major file
21417c478bd9Sstevel@tonic-gate 			 * and there is no /etc/rem_name_to_major file,
21427c478bd9Sstevel@tonic-gate 			 * then we don't have a max_dev, bail out quick!
21437c478bd9Sstevel@tonic-gate 			 */
21447c478bd9Sstevel@tonic-gate 			if (max_dev == ERROR)
21457c478bd9Sstevel@tonic-gate 				return (ERROR);
21467c478bd9Sstevel@tonic-gate 		}
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate 		/*
21497c478bd9Sstevel@tonic-gate 		 * In case there is no more slack in current name_to_major
21507c478bd9Sstevel@tonic-gate 		 * table, provide at least 1 extra entry so the add_drv can
21517c478bd9Sstevel@tonic-gate 		 * succeed.  Since only one add_drv process is allowed at one
21527c478bd9Sstevel@tonic-gate 		 * time, and hence max_dev will be re-calculated each time
21537c478bd9Sstevel@tonic-gate 		 * add_drv is ran, we don't need to worry about adding more
21547c478bd9Sstevel@tonic-gate 		 * than 1 extra slot for max_dev.
21557c478bd9Sstevel@tonic-gate 		 */
21567c478bd9Sstevel@tonic-gate 		max_dev++;
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 	} else {
21597c478bd9Sstevel@tonic-gate 		if (modctl(MODRESERVED, NULL, &max_dev) < 0) {
21607c478bd9Sstevel@tonic-gate 			perror(NULL);
21617c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_MAX_MAJOR));
21627c478bd9Sstevel@tonic-gate 			return (ERROR);
21637c478bd9Sstevel@tonic-gate 		}
21647c478bd9Sstevel@tonic-gate 	}
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 	/*
21677c478bd9Sstevel@tonic-gate 	 * max_dev is really how many slots the kernel has allocated for
21687c478bd9Sstevel@tonic-gate 	 * devices... [0 , maxdev-1], not the largest available device num.
21697c478bd9Sstevel@tonic-gate 	 */
21707c478bd9Sstevel@tonic-gate 	if ((num_list = calloc(max_dev, 1)) == NULL) {
21717c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
21727c478bd9Sstevel@tonic-gate 		return (ERROR);
21737c478bd9Sstevel@tonic-gate 	}
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	/*
21767c478bd9Sstevel@tonic-gate 	 * Populate the num_list array
21777c478bd9Sstevel@tonic-gate 	 */
21787c478bd9Sstevel@tonic-gate 	if (fill_n2m_array(name_to_major, &num_list, &max_dev) != 0) {
21797c478bd9Sstevel@tonic-gate 		return (ERROR);
21807c478bd9Sstevel@tonic-gate 	}
21817c478bd9Sstevel@tonic-gate 	if (have_rem_n2m) {
21827c478bd9Sstevel@tonic-gate 		if (fill_n2m_array(rem_name_to_major, &num_list, &max_dev) != 0)
21837c478bd9Sstevel@tonic-gate 			return (ERROR);
21847c478bd9Sstevel@tonic-gate 	}
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 	/* find first free major number */
21877c478bd9Sstevel@tonic-gate 	for (i = 0; i < max_dev; i++) {
21887c478bd9Sstevel@tonic-gate 		if (num_list[i] != 1) {
21897c478bd9Sstevel@tonic-gate 			new_maj = i;
21907c478bd9Sstevel@tonic-gate 			break;
21917c478bd9Sstevel@tonic-gate 		}
21927c478bd9Sstevel@tonic-gate 	}
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 	if (new_maj == -1) {
21957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_FREE_MAJOR));
21967c478bd9Sstevel@tonic-gate 		return (ERROR);
21977c478bd9Sstevel@tonic-gate 	}
21987c478bd9Sstevel@tonic-gate 
21992e107de7SJerry Gilliam 	(void) snprintf(drv_majnum_str, sizeof (drv_majnum_str),
22002e107de7SJerry Gilliam 	    "%d", new_maj);
22017c478bd9Sstevel@tonic-gate 	if (do_the_update(driver_name, drv_majnum_str) == ERROR) {
22027c478bd9Sstevel@tonic-gate 		return (ERROR);
22037c478bd9Sstevel@tonic-gate 	}
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	*major_num = new_maj;
22067c478bd9Sstevel@tonic-gate 	return (NOERR);
22077c478bd9Sstevel@tonic-gate }
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate int
22117c478bd9Sstevel@tonic-gate fill_n2m_array(char *filename, char **array, int *nelems)
22127c478bd9Sstevel@tonic-gate {
22137c478bd9Sstevel@tonic-gate 	FILE *fp;
22141ca93273Seota 	char line[MAX_N2M_ALIAS_LINE + 1], *cp;
22157c478bd9Sstevel@tonic-gate 	char drv[FILENAME_MAX + 1];
22167c478bd9Sstevel@tonic-gate 	u_longlong_t dnum;
22177c478bd9Sstevel@tonic-gate 	major_t drv_majnum;
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 	/*
22207c478bd9Sstevel@tonic-gate 	 * Read through the file, marking each major number found
22217c478bd9Sstevel@tonic-gate 	 * order is not relevant
22227c478bd9Sstevel@tonic-gate 	 */
22237c478bd9Sstevel@tonic-gate 	if ((fp = fopen(filename, "r")) == NULL) {
22247c478bd9Sstevel@tonic-gate 		perror(NULL);
22257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), filename);
22267c478bd9Sstevel@tonic-gate 		return (ERROR);
22277c478bd9Sstevel@tonic-gate 	}
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line), fp) != 0) {
22301ca93273Seota 		/* cut off comments starting with '#' */
22311ca93273Seota 		if ((cp = strchr(line, '#')) != NULL)
22321ca93273Seota 			*cp = '\0';
22331ca93273Seota 		/* ignore comment or blank lines */
22341ca93273Seota 		if (is_blank(line))
22351ca93273Seota 			continue;
22361ca93273Seota 		/* sanity-check */
22372e107de7SJerry Gilliam 		/* LINTED E_SEC_SCANF_UNBOUNDED_COPY */
22387c478bd9Sstevel@tonic-gate 		if (sscanf(line, "%s %llu", drv, &dnum) != 2) {
22397c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_BAD_LINE),
22407c478bd9Sstevel@tonic-gate 			    filename, line);
22417c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
22427c478bd9Sstevel@tonic-gate 			return (ERROR);
22437c478bd9Sstevel@tonic-gate 		}
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 		if (dnum > L_MAXMAJ32) {
22467c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_MAJ_TOOBIG), drv,
22477c478bd9Sstevel@tonic-gate 			    dnum, filename, L_MAXMAJ32);
22487c478bd9Sstevel@tonic-gate 			continue;
22497c478bd9Sstevel@tonic-gate 		}
22507c478bd9Sstevel@tonic-gate 		/*
22517c478bd9Sstevel@tonic-gate 		 * cast down to a major_t; we can be sure this is safe because
22527c478bd9Sstevel@tonic-gate 		 * of the above range-check.
22537c478bd9Sstevel@tonic-gate 		 */
22547c478bd9Sstevel@tonic-gate 		drv_majnum = (major_t)dnum;
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate 		if (drv_majnum >= *nelems) {
22577c478bd9Sstevel@tonic-gate 			/*
22587c478bd9Sstevel@tonic-gate 			 * Allocate some more space, up to drv_majnum + 1 so
22597c478bd9Sstevel@tonic-gate 			 * we can accomodate 0 through drv_majnum.
22607c478bd9Sstevel@tonic-gate 			 *
22617c478bd9Sstevel@tonic-gate 			 * Note that in the failure case, we leak all of the
22627c478bd9Sstevel@tonic-gate 			 * old contents of array.  It's ok, since we just
22637c478bd9Sstevel@tonic-gate 			 * wind up exiting immediately anyway.
22647c478bd9Sstevel@tonic-gate 			 */
22657c478bd9Sstevel@tonic-gate 			*nelems = drv_majnum + 1;
22667c478bd9Sstevel@tonic-gate 			*array = realloc(*array, *nelems);
22677c478bd9Sstevel@tonic-gate 			if (*array == NULL) {
22687c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_NO_MEM));
22697c478bd9Sstevel@tonic-gate 				return (ERROR);
22707c478bd9Sstevel@tonic-gate 			}
22717c478bd9Sstevel@tonic-gate 		}
22727c478bd9Sstevel@tonic-gate 		(*array)[drv_majnum] = 1;
22737c478bd9Sstevel@tonic-gate 	}
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
22767c478bd9Sstevel@tonic-gate 	return (0);
22777c478bd9Sstevel@tonic-gate }
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate int
22817c478bd9Sstevel@tonic-gate do_the_update(char *driver_name, char *major_number)
22827c478bd9Sstevel@tonic-gate {
22837c478bd9Sstevel@tonic-gate 	return (append_to_file(driver_name, major_number, name_to_major,
2284f4da9be0Scth 	    ' ', " ", 0));
22857c478bd9Sstevel@tonic-gate }
22861ca93273Seota 
22871ca93273Seota /*
22881ca93273Seota  * is_blank() returns 1 (true) if a line specified is composed of
22891ca93273Seota  * whitespace characters only. otherwise, it returns 0 (false).
22901ca93273Seota  *
22911ca93273Seota  * Note. the argument (line) must be null-terminated.
22921ca93273Seota  */
22931ca93273Seota static int
22941ca93273Seota is_blank(char *line)
22951ca93273Seota {
22961ca93273Seota 	for (/* nothing */; *line != '\0'; line++)
22971ca93273Seota 		if (!isspace(*line))
22981ca93273Seota 			return (0);
22991ca93273Seota 	return (1);
23001ca93273Seota }
2301