xref: /titanic_50/usr/src/cmd/modload/drvsubr.c (revision 6532b9600e063234d62bca681503353c01abad20)
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 /*
22*6532b960SJerry Gilliam  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
281ca93273Seota #include <ctype.h>
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
317c478bd9Sstevel@tonic-gate #include <libintl.h>
327c478bd9Sstevel@tonic-gate #include <wait.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
340013e2d3SJerry Gilliam #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
377c478bd9Sstevel@tonic-gate #include <signal.h>
387c478bd9Sstevel@tonic-gate #include <sys/buf.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <grp.h>
417c478bd9Sstevel@tonic-gate #include "addrem.h"
427c478bd9Sstevel@tonic-gate #include "errmsg.h"
437c478bd9Sstevel@tonic-gate #include "plcysubr.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static char *add_rem_lock;	/* lock file */
467c478bd9Sstevel@tonic-gate static char *tmphold;		/* temperary file for updating */
4749e92448Svikram static int  add_rem_lock_fd = -1;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static int get_cached_n_to_m_file(char *filename, char ***cache);
507c478bd9Sstevel@tonic-gate static int get_name_to_major_entry(int *major_no, char *driver_name,
517c478bd9Sstevel@tonic-gate     char *file_name);
527c478bd9Sstevel@tonic-gate 
531ca93273Seota static int is_blank(char *);
541ca93273Seota 
557c478bd9Sstevel@tonic-gate /*ARGSUSED*/
567c478bd9Sstevel@tonic-gate void
577c478bd9Sstevel@tonic-gate log_minorperm_error(minorperm_err_t err, int key)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	switch (err) {
607c478bd9Sstevel@tonic-gate 	case MP_FOPEN_ERR:
617c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
627c478bd9Sstevel@tonic-gate 		    MINOR_PERM_FILE);
637c478bd9Sstevel@tonic-gate 		break;
647c478bd9Sstevel@tonic-gate 	case MP_FCLOSE_ERR:
657c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_UPDATE),
667c478bd9Sstevel@tonic-gate 		    MINOR_PERM_FILE);
677c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
687c478bd9Sstevel@tonic-gate 		break;
697c478bd9Sstevel@tonic-gate 	case MP_IGNORING_LINE_ERR:
707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_UPDATE),
717c478bd9Sstevel@tonic-gate 		    MINOR_PERM_FILE);
727c478bd9Sstevel@tonic-gate 		break;
737c478bd9Sstevel@tonic-gate 	case MP_ALLOC_ERR:
747c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_UPDATE),
757c478bd9Sstevel@tonic-gate 		    MINOR_PERM_FILE);
767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
777c478bd9Sstevel@tonic-gate 		break;
787c478bd9Sstevel@tonic-gate 	case MP_NVLIST_ERR:
797c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_UPDATE),
807c478bd9Sstevel@tonic-gate 		    MINOR_PERM_FILE);
817c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
827c478bd9Sstevel@tonic-gate 		break;
837c478bd9Sstevel@tonic-gate 	case MP_CANT_FIND_USER_ERR:
847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_UPDATE),
857c478bd9Sstevel@tonic-gate 		    MINOR_PERM_FILE);
867c478bd9Sstevel@tonic-gate 		break;
877c478bd9Sstevel@tonic-gate 	case MP_CANT_FIND_GROUP_ERR:
887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_UPDATE),
897c478bd9Sstevel@tonic-gate 		    MINOR_PERM_FILE);
907c478bd9Sstevel@tonic-gate 		break;
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  *  open file
967c478bd9Sstevel@tonic-gate  * for each entry in list
977c478bd9Sstevel@tonic-gate  *	where list entries are separated by <list_separator>
987c478bd9Sstevel@tonic-gate  * 	append entry : driver_name <entry_separator> entry
997c478bd9Sstevel@tonic-gate  * close file
1007c478bd9Sstevel@tonic-gate  * return error/noerr
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate int
1037c478bd9Sstevel@tonic-gate append_to_file(
1047c478bd9Sstevel@tonic-gate 	char *driver_name,
1057c478bd9Sstevel@tonic-gate 	char *entry_list,
1067c478bd9Sstevel@tonic-gate 	char *filename,
1077c478bd9Sstevel@tonic-gate 	char list_separator,
108f4da9be0Scth 	char *entry_separator,
109f4da9be0Scth 	int quoted)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	int	i, len;
1127c478bd9Sstevel@tonic-gate 	int	fpint;
1137c478bd9Sstevel@tonic-gate 	char	*current_head, *previous_head;
1147c478bd9Sstevel@tonic-gate 	char	*line, *one_entry;
1157c478bd9Sstevel@tonic-gate 	FILE	*fp;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if ((fp = fopen(filename, "a")) == NULL) {
1187c478bd9Sstevel@tonic-gate 		perror(NULL);
1197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
1207c478bd9Sstevel@tonic-gate 		    filename);
1217c478bd9Sstevel@tonic-gate 		return (ERROR);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	len = strlen(entry_list);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	one_entry = calloc(len + 1, 1);
1277c478bd9Sstevel@tonic-gate 	if (one_entry == NULL) {
1287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_UPDATE), filename);
1297c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
1307c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
1317c478bd9Sstevel@tonic-gate 		return (ERROR);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	previous_head = entry_list;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	line = calloc(strlen(driver_name) + len + 4, 1);
1377c478bd9Sstevel@tonic-gate 	if (line == NULL) {
1387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
1397c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
1407c478bd9Sstevel@tonic-gate 		err_exit();
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	/*
1447c478bd9Sstevel@tonic-gate 	 * get one entry at a time from list and append to <filename> file
1457c478bd9Sstevel@tonic-gate 	 */
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	do {
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		for (i = 0; i <= len; i++)
1507c478bd9Sstevel@tonic-gate 			one_entry[i] = 0;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		for (i = 0; i <= (int)strlen(line); i++)
1537c478bd9Sstevel@tonic-gate 			line[i] = 0;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 		current_head = get_entry(previous_head, one_entry,
156f4da9be0Scth 		    list_separator, quoted);
1577c478bd9Sstevel@tonic-gate 		previous_head = current_head;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 		(void) strcpy(line, driver_name);
1607c478bd9Sstevel@tonic-gate 		(void) strcat(line, entry_separator);
161f4da9be0Scth 		if (quoted)
162f4da9be0Scth 			(void) strcat(line, "\"");
1637c478bd9Sstevel@tonic-gate 		(void) strcat(line, one_entry);
164f4da9be0Scth 		if (quoted)
165f4da9be0Scth 			(void) strcat(line, "\"");
1667c478bd9Sstevel@tonic-gate 		(void) strcat(line, "\n");
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 		if ((fputs(line, fp)) == EOF) {
1697c478bd9Sstevel@tonic-gate 			perror(NULL);
1707c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_UPDATE),
1717c478bd9Sstevel@tonic-gate 			    filename);
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	} while (*current_head != '\0');
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	(void) fflush(fp);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	fpint = fileno(fp);
1807c478bd9Sstevel@tonic-gate 	(void) fsync(fpint);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	free(one_entry);
1857c478bd9Sstevel@tonic-gate 	free(line);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	return (NOERR);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
19011ebc2d0SJerry Gilliam /*
19111ebc2d0SJerry Gilliam  * Require exact match to delete a driver alias/permission entry.
19211ebc2d0SJerry Gilliam  * Note line argument does not remain unchanged.  Return 1 if matched.
19311ebc2d0SJerry Gilliam  */
19411ebc2d0SJerry Gilliam static int
19511ebc2d0SJerry Gilliam match_entry(char *line, char *match)
19611ebc2d0SJerry Gilliam {
19711ebc2d0SJerry Gilliam 	char	*token, *p;
19811ebc2d0SJerry Gilliam 	int	n;
19911ebc2d0SJerry Gilliam 
20011ebc2d0SJerry Gilliam 	/* skip any leading white space */
20111ebc2d0SJerry Gilliam 	while (*line && ((*line == ' ') || (*line == '\t')))
20211ebc2d0SJerry Gilliam 		line++;
20311ebc2d0SJerry Gilliam 	/*
20411ebc2d0SJerry Gilliam 	 * Find separator for driver name, either space or colon
20511ebc2d0SJerry Gilliam 	 *	minor_perm: <driver>:<perm>
20611ebc2d0SJerry Gilliam 	 *	driver_aliases: <driver> <alias>
20711ebc2d0SJerry Gilliam 	 *	extra_privs: <driver>:<priv>
20811ebc2d0SJerry Gilliam 	 */
20911ebc2d0SJerry Gilliam 	if ((token = strpbrk(line, " :\t")) == NULL)
21011ebc2d0SJerry Gilliam 		return (0);
21111ebc2d0SJerry Gilliam 	token++;
21211ebc2d0SJerry Gilliam 	/* skip leading white space and quotes */
21311ebc2d0SJerry Gilliam 	while (*token && (*token == ' ' || *token == '\t' ||
21411ebc2d0SJerry Gilliam 	    *token == '"' || *token == '\''))
21511ebc2d0SJerry Gilliam 		token++;
21611ebc2d0SJerry Gilliam 	/* strip trailing newline, white space and quotes */
21711ebc2d0SJerry Gilliam 	n = strlen(token);
21811ebc2d0SJerry Gilliam 	p = token + n-1;
21911ebc2d0SJerry Gilliam 	while (n > 0 && (*p == '\n' || *p == ' ' || *p == '\t' ||
22011ebc2d0SJerry Gilliam 	    *p == '"' || *p == '\'')) {
22111ebc2d0SJerry Gilliam 		*p-- = 0;
22211ebc2d0SJerry Gilliam 		n--;
22311ebc2d0SJerry Gilliam 	}
22411ebc2d0SJerry Gilliam 	if (n == 0)
22511ebc2d0SJerry Gilliam 		return (0);
22611ebc2d0SJerry Gilliam 	return (strcmp(token, match) == 0);
22711ebc2d0SJerry Gilliam }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate  *  open file
2317c478bd9Sstevel@tonic-gate  * read thru file, deleting all entries if first
2327c478bd9Sstevel@tonic-gate  *    entry = driver_name
2337c478bd9Sstevel@tonic-gate  * close
2347c478bd9Sstevel@tonic-gate  * if error, leave original file intact with message
2357c478bd9Sstevel@tonic-gate  * assumption : drvconfig has been modified to work with clone
2367c478bd9Sstevel@tonic-gate  *  entries in /etc/minor_perm as driver:mummble NOT
2377c478bd9Sstevel@tonic-gate  *  clone:driver mummble
2387c478bd9Sstevel@tonic-gate  * this implementation will NOT find clone entries
2397c478bd9Sstevel@tonic-gate  * clone:driver mummble
2407c478bd9Sstevel@tonic-gate  * match:
2417c478bd9Sstevel@tonic-gate  *	delete just the matching entry
2427c478bd9Sstevel@tonic-gate  *
2437c478bd9Sstevel@tonic-gate  */
2447c478bd9Sstevel@tonic-gate int
2457c478bd9Sstevel@tonic-gate delete_entry(
2467c478bd9Sstevel@tonic-gate 	char *oldfile,
2477c478bd9Sstevel@tonic-gate 	char *driver_name,
2487c478bd9Sstevel@tonic-gate 	char *marker,
2497c478bd9Sstevel@tonic-gate 	char *match)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	int		rv, i;
2527c478bd9Sstevel@tonic-gate 	int		status = NOERR;
2537c478bd9Sstevel@tonic-gate 	int		drvr_found = 0;
2547c478bd9Sstevel@tonic-gate 	boolean_t 	nomatch = B_TRUE;
25511ebc2d0SJerry Gilliam 	char		*newfile, *tptr, *cp;
2567c478bd9Sstevel@tonic-gate 	char		line[MAX_DBFILE_ENTRY], drv[FILENAME_MAX + 1];
2577c478bd9Sstevel@tonic-gate 	FILE		*fp, *newfp;
2587c478bd9Sstevel@tonic-gate 	struct group	*sysgrp;
25911ebc2d0SJerry Gilliam 	char		*copy;		/* same size as line */
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	/*
2627c478bd9Sstevel@tonic-gate 	 * check if match is specified and if it equals " "
2637c478bd9Sstevel@tonic-gate 	 */
2647c478bd9Sstevel@tonic-gate 	if (match && (*match == ' ' && strlen(match) == 1)) {
2657c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_INT_UPDATE), oldfile);
2667c478bd9Sstevel@tonic-gate 		return (ERROR);
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	if ((fp = fopen(oldfile, "r")) == NULL) {
2707c478bd9Sstevel@tonic-gate 		perror(NULL);
2717c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), oldfile);
2727c478bd9Sstevel@tonic-gate 		return (ERROR);
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 
27511ebc2d0SJerry Gilliam 	/* Space for defensive copy of input line */
27611ebc2d0SJerry Gilliam 	copy = calloc(sizeof (line), 1);
2777c478bd9Sstevel@tonic-gate 
27811ebc2d0SJerry Gilliam 	/* Build filename for temporary file */
27911ebc2d0SJerry Gilliam 	tptr = calloc(strlen(oldfile) + strlen(XEND) + 1, 1);
28011ebc2d0SJerry Gilliam 	if (tptr == NULL || copy == NULL) {
2817c478bd9Sstevel@tonic-gate 		perror(NULL);
2827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
28311ebc2d0SJerry Gilliam 		return (ERROR);
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	(void) strcpy(tptr, oldfile);
2877c478bd9Sstevel@tonic-gate 	(void) strcat(tptr, XEND);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/*
2907c478bd9Sstevel@tonic-gate 	 * Set gid so we preserve group attribute.  Ideally we wouldn't
2917c478bd9Sstevel@tonic-gate 	 * assume a gid of "sys" but we can't undo the damage on already
2927c478bd9Sstevel@tonic-gate 	 * installed systems unless we force the issue.
2937c478bd9Sstevel@tonic-gate 	 */
2947c478bd9Sstevel@tonic-gate 	if ((sysgrp = getgrnam("sys")) != NULL) {
2957c478bd9Sstevel@tonic-gate 		(void) setgid(sysgrp->gr_gid);
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	newfile = mktemp(tptr);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if ((newfp = fopen(newfile, "w")) == NULL) {
3017c478bd9Sstevel@tonic-gate 		perror(NULL);
3027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
3037c478bd9Sstevel@tonic-gate 		    newfile);
3047c478bd9Sstevel@tonic-gate 		return (ERROR);
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	while ((fgets(line, sizeof (line), fp) != NULL) && status == NOERR) {
30811ebc2d0SJerry Gilliam 		/* copy the whole line */
30911ebc2d0SJerry Gilliam 		if (strlcpy(copy, line, sizeof (line)) >= sizeof (line)) {
31011ebc2d0SJerry Gilliam 			(void) fprintf(stderr, gettext(ERR_UPDATE), oldfile);
3111ca93273Seota 			status = ERROR;
3121ca93273Seota 			break;
3131ca93273Seota 		}
3141ca93273Seota 		/* cut off comments starting with '#' */
31511ebc2d0SJerry Gilliam 		if ((cp = strchr(copy, '#')) != NULL)
3161ca93273Seota 			*cp = '\0';
3171ca93273Seota 		/* ignore comment or blank lines */
31811ebc2d0SJerry Gilliam 		if (is_blank(copy)) {
3191ca93273Seota 			if (fputs(line, newfp) == EOF) {
3207c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_UPDATE),
3217c478bd9Sstevel@tonic-gate 				    oldfile);
3227c478bd9Sstevel@tonic-gate 				status = ERROR;
3237c478bd9Sstevel@tonic-gate 			}
3247c478bd9Sstevel@tonic-gate 			continue;
3257c478bd9Sstevel@tonic-gate 		}
3261ca93273Seota 
3271ca93273Seota 		/* get the driver name */
32811ebc2d0SJerry Gilliam 		if (sscanf(copy, "%s", drv) != 1) {
3297c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_BAD_LINE),
3307c478bd9Sstevel@tonic-gate 			    oldfile, line);
3317c478bd9Sstevel@tonic-gate 			status = ERROR;
3321ca93273Seota 			break;
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 		for (i = strcspn(drv, marker); i < FILENAME_MAX; i++) {
3367c478bd9Sstevel@tonic-gate 			drv[i] =  '\0';
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		if (strcmp(driver_name, drv) != 0) {
3407c478bd9Sstevel@tonic-gate 			if ((fputs(line, newfp)) == EOF) {
3417c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_UPDATE),
3427c478bd9Sstevel@tonic-gate 				    oldfile);
3437c478bd9Sstevel@tonic-gate 				status = ERROR;
3447c478bd9Sstevel@tonic-gate 			}
3457c478bd9Sstevel@tonic-gate 		} else {
3467c478bd9Sstevel@tonic-gate 			drvr_found++;
3477c478bd9Sstevel@tonic-gate 			if (match) {	/* Just delete one entry */
3487c478bd9Sstevel@tonic-gate 				/* for now delete just minor_perm and aliases */
3497c478bd9Sstevel@tonic-gate 				if ((strcmp(oldfile, minor_perm) == 0) ||
3507c478bd9Sstevel@tonic-gate 				    (strcmp(oldfile, extra_privs) == 0) ||
3517c478bd9Sstevel@tonic-gate 				    (strcmp(oldfile, driver_aliases) == 0)) {
35211ebc2d0SJerry Gilliam 
35311ebc2d0SJerry Gilliam 					/* make defensive copy */
35411ebc2d0SJerry Gilliam 					if (strlcpy(copy, line, sizeof (line))
35511ebc2d0SJerry Gilliam 					    >= sizeof (line)) {
35611ebc2d0SJerry Gilliam 						(void) fprintf(stderr,
35711ebc2d0SJerry Gilliam 						    gettext(ERR_UPDATE),
35811ebc2d0SJerry Gilliam 						    oldfile);
35911ebc2d0SJerry Gilliam 						status = ERROR;
36011ebc2d0SJerry Gilliam 						break;
36111ebc2d0SJerry Gilliam 					}
36211ebc2d0SJerry Gilliam 					if (match_entry(copy, match)) {
3637c478bd9Sstevel@tonic-gate 						nomatch = B_FALSE;
3647c478bd9Sstevel@tonic-gate 					} else {
3657c478bd9Sstevel@tonic-gate 						if ((fputs(line, newfp)) ==
3667c478bd9Sstevel@tonic-gate 						    EOF) {
3677c478bd9Sstevel@tonic-gate 							(void) fprintf(stderr,
3687c478bd9Sstevel@tonic-gate 							    gettext(ERR_UPDATE),
3697c478bd9Sstevel@tonic-gate 							    oldfile);
3707c478bd9Sstevel@tonic-gate 							status = ERROR;
3717c478bd9Sstevel@tonic-gate 						}
3727c478bd9Sstevel@tonic-gate 						if (nomatch != B_FALSE)
3737c478bd9Sstevel@tonic-gate 							nomatch = B_TRUE;
3747c478bd9Sstevel@tonic-gate 					}
3757c478bd9Sstevel@tonic-gate 				}
3767c478bd9Sstevel@tonic-gate 			}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 		} /* end of else */
3797c478bd9Sstevel@tonic-gate 	} /* end of while */
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
38211ebc2d0SJerry Gilliam 	free(tptr);
38311ebc2d0SJerry Gilliam 	free(copy);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	/* Make sure that the file is on disk */
3867c478bd9Sstevel@tonic-gate 	if (fflush(newfp) != 0 || fsync(fileno(newfp)) != 0)
3877c478bd9Sstevel@tonic-gate 		status = ERROR;
3887c478bd9Sstevel@tonic-gate 	else
3897c478bd9Sstevel@tonic-gate 		rv = NOERR;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	(void) fclose(newfp);
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	/* no matching driver found */
3947c478bd9Sstevel@tonic-gate 	rv = NOERR;
3957c478bd9Sstevel@tonic-gate 	if (!drvr_found ||
3967c478bd9Sstevel@tonic-gate 	    (nomatch == B_TRUE)) {
3977c478bd9Sstevel@tonic-gate 		rv = NONE_FOUND;
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	/*
4017c478bd9Sstevel@tonic-gate 	 * if error, leave original file, delete new file
4027c478bd9Sstevel@tonic-gate 	 * if noerr, replace original file with new file
4037c478bd9Sstevel@tonic-gate 	 */
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if (status == NOERR) {
4067c478bd9Sstevel@tonic-gate 		if (rename(oldfile, tmphold) == -1) {
4077c478bd9Sstevel@tonic-gate 			perror(NULL);
4087c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE), oldfile);
4097c478bd9Sstevel@tonic-gate 			(void) unlink(newfile);
4107c478bd9Sstevel@tonic-gate 			return (ERROR);
4117c478bd9Sstevel@tonic-gate 		} else if (rename(newfile, oldfile) == -1) {
4127c478bd9Sstevel@tonic-gate 			perror(NULL);
4137c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE), oldfile);
4147c478bd9Sstevel@tonic-gate 			(void) unlink(oldfile);
4157c478bd9Sstevel@tonic-gate 			(void) unlink(newfile);
4167c478bd9Sstevel@tonic-gate 			if (link(tmphold, oldfile) == -1) {
4177c478bd9Sstevel@tonic-gate 				perror(NULL);
4187c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_BAD_LINK),
4197c478bd9Sstevel@tonic-gate 				    oldfile, tmphold);
4207c478bd9Sstevel@tonic-gate 			}
4217c478bd9Sstevel@tonic-gate 			return (ERROR);
4227c478bd9Sstevel@tonic-gate 		}
4237c478bd9Sstevel@tonic-gate 		(void) unlink(tmphold);
4247c478bd9Sstevel@tonic-gate 	} else {
4257c478bd9Sstevel@tonic-gate 		/*
4267c478bd9Sstevel@tonic-gate 		 * since there's an error, leave file alone; remove
4277c478bd9Sstevel@tonic-gate 		 * new file
4287c478bd9Sstevel@tonic-gate 		 */
4297c478bd9Sstevel@tonic-gate 		if (unlink(newfile) == -1) {
4307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_CANT_RM), newfile);
4317c478bd9Sstevel@tonic-gate 		}
4327c478bd9Sstevel@tonic-gate 		return (ERROR);
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	return (rv);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate /*
4407c478bd9Sstevel@tonic-gate  * wrapper for call to get_name_to_major_entry(): given driver name,
4417c478bd9Sstevel@tonic-gate  * retrieve major number.
4427c478bd9Sstevel@tonic-gate  */
4437c478bd9Sstevel@tonic-gate int
4447c478bd9Sstevel@tonic-gate get_major_no(char *driver_name, char *file_name)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	int major = UNIQUE;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	if (get_name_to_major_entry(&major, driver_name, file_name) == ERROR)
4497c478bd9Sstevel@tonic-gate 		return (ERROR);
4507c478bd9Sstevel@tonic-gate 	else
4517c478bd9Sstevel@tonic-gate 		return (major);
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate /*
4557c478bd9Sstevel@tonic-gate  * wrapper for call to get_name_to_major_entry(): given major number,
4567c478bd9Sstevel@tonic-gate  * retrieve driver name.
4577c478bd9Sstevel@tonic-gate  */
4587c478bd9Sstevel@tonic-gate int
4597c478bd9Sstevel@tonic-gate get_driver_name(int major, char *file_name, char *buf)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate 	if (major < 0)
4627c478bd9Sstevel@tonic-gate 		return (ERROR);
4637c478bd9Sstevel@tonic-gate 	return (get_name_to_major_entry(&major, buf, file_name));
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate /*
4687c478bd9Sstevel@tonic-gate  * return pointer to cached name_to_major file - reads file into
4697c478bd9Sstevel@tonic-gate  * cache if this has not already been done.  Since there may be
4707c478bd9Sstevel@tonic-gate  * requests for multiple name_to_major files (rem_name_to_major,
4717c478bd9Sstevel@tonic-gate  * name_to_major), this routine keeps a list of cached files.
4727c478bd9Sstevel@tonic-gate  */
4737c478bd9Sstevel@tonic-gate static int
4747c478bd9Sstevel@tonic-gate get_cached_n_to_m_file(char *filename, char ***cache)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate 	struct n_to_m_cache {
4777c478bd9Sstevel@tonic-gate 		char *file;
4787c478bd9Sstevel@tonic-gate 		char **cached_file;
4797c478bd9Sstevel@tonic-gate 		int size;
4807c478bd9Sstevel@tonic-gate 		struct n_to_m_cache *next;
4817c478bd9Sstevel@tonic-gate 	};
4827c478bd9Sstevel@tonic-gate 	static struct n_to_m_cache *head = NULL;
4837c478bd9Sstevel@tonic-gate 	struct n_to_m_cache *ptr;
4847c478bd9Sstevel@tonic-gate 	FILE *fp;
4857c478bd9Sstevel@tonic-gate 	char drv[FILENAME_MAX + 1];
4867c478bd9Sstevel@tonic-gate 	char entry[FILENAME_MAX + 1];
4871ca93273Seota 	char line[MAX_N2M_ALIAS_LINE], *cp;
4887c478bd9Sstevel@tonic-gate 	int maj;
4897c478bd9Sstevel@tonic-gate 	int size = 0;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	/*
4937c478bd9Sstevel@tonic-gate 	 * see if the file is already cached - either
4947c478bd9Sstevel@tonic-gate 	 * rem_name_to_major or name_to_major
4957c478bd9Sstevel@tonic-gate 	 */
4967c478bd9Sstevel@tonic-gate 	ptr = head;
4977c478bd9Sstevel@tonic-gate 	while (ptr != NULL) {
4987c478bd9Sstevel@tonic-gate 		if (strcmp(ptr->file, filename) == 0)
4997c478bd9Sstevel@tonic-gate 			break;
5007c478bd9Sstevel@tonic-gate 		ptr = ptr->next;
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {	/* we need to cache the contents */
5047c478bd9Sstevel@tonic-gate 		if ((fp = fopen(filename, "r")) == NULL) {
5057c478bd9Sstevel@tonic-gate 			perror(NULL);
5067c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_CANT_OPEN),
5077c478bd9Sstevel@tonic-gate 			    filename);
5087c478bd9Sstevel@tonic-gate 			return (ERROR);
5097c478bd9Sstevel@tonic-gate 		}
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 		while (fgets(line, sizeof (line), fp) != NULL) {
5121ca93273Seota 			/* cut off comments starting with '#' */
5131ca93273Seota 			if ((cp = strchr(line, '#')) != NULL)
5141ca93273Seota 				*cp = '\0';
5151ca93273Seota 			/* ignore comment or blank lines */
5161ca93273Seota 			if (is_blank(line))
5171ca93273Seota 				continue;
5181ca93273Seota 			/* sanity-check */
5197c478bd9Sstevel@tonic-gate 			if (sscanf(line, "%s%s", drv, entry) != 2) {
5207c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_BAD_LINE),
5217c478bd9Sstevel@tonic-gate 				    filename, line);
5227c478bd9Sstevel@tonic-gate 				continue;
5237c478bd9Sstevel@tonic-gate 			}
5247c478bd9Sstevel@tonic-gate 			maj = atoi(entry);
5257c478bd9Sstevel@tonic-gate 			if (maj > size)
5267c478bd9Sstevel@tonic-gate 				size = maj;
5277c478bd9Sstevel@tonic-gate 		}
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 		/* allocate struct to cache the file */
5307c478bd9Sstevel@tonic-gate 		ptr = (struct n_to_m_cache *)calloc(1,
5317c478bd9Sstevel@tonic-gate 		    sizeof (struct n_to_m_cache));
5327c478bd9Sstevel@tonic-gate 		if (ptr == NULL) {
5337c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_MEM));
5347c478bd9Sstevel@tonic-gate 			return (ERROR);
5357c478bd9Sstevel@tonic-gate 		}
5367c478bd9Sstevel@tonic-gate 		ptr->size = size + 1;
5377c478bd9Sstevel@tonic-gate 		/* allocate space to cache contents of file */
5387c478bd9Sstevel@tonic-gate 		ptr->cached_file = (char **)calloc(ptr->size, sizeof (char *));
5397c478bd9Sstevel@tonic-gate 		if (ptr->cached_file == NULL) {
5407c478bd9Sstevel@tonic-gate 			free(ptr);
5417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_MEM));
5427c478bd9Sstevel@tonic-gate 			return (ERROR);
5437c478bd9Sstevel@tonic-gate 		}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 		rewind(fp);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		/*
5487c478bd9Sstevel@tonic-gate 		 * now fill the cache
5497c478bd9Sstevel@tonic-gate 		 * the cache is an array of char pointers indexed by major
5507c478bd9Sstevel@tonic-gate 		 * number
5517c478bd9Sstevel@tonic-gate 		 */
5527c478bd9Sstevel@tonic-gate 		while (fgets(line, sizeof (line), fp) != NULL) {
5531ca93273Seota 			/* cut off comments starting with '#' */
5541ca93273Seota 			if ((cp = strchr(line, '#')) != NULL)
5551ca93273Seota 				*cp = '\0';
5561ca93273Seota 			/* ignore comment or blank lines */
5571ca93273Seota 			if (is_blank(line))
5581ca93273Seota 				continue;
5591ca93273Seota 			/* sanity-check */
5607c478bd9Sstevel@tonic-gate 			if (sscanf(line, "%s%s", drv, entry) != 2) {
5617c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_BAD_LINE),
5627c478bd9Sstevel@tonic-gate 				    filename, line);
5637c478bd9Sstevel@tonic-gate 				continue;
5647c478bd9Sstevel@tonic-gate 			}
5657c478bd9Sstevel@tonic-gate 			maj = atoi(entry);
5667c478bd9Sstevel@tonic-gate 			if ((ptr->cached_file[maj] = strdup(drv)) == NULL) {
5677c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_NO_MEM));
5687c478bd9Sstevel@tonic-gate 				free(ptr->cached_file);
5697c478bd9Sstevel@tonic-gate 				free(ptr);
5707c478bd9Sstevel@tonic-gate 				return (ERROR);
5717c478bd9Sstevel@tonic-gate 			}
5727c478bd9Sstevel@tonic-gate 			(void) strcpy(ptr->cached_file[maj], drv);
5737c478bd9Sstevel@tonic-gate 		}
5747c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
5757c478bd9Sstevel@tonic-gate 		/* link the cache struct into the list of cached files */
5767c478bd9Sstevel@tonic-gate 		ptr->file = strdup(filename);
5777c478bd9Sstevel@tonic-gate 		if (ptr->file == NULL) {
5787c478bd9Sstevel@tonic-gate 			for (maj = 0; maj <= ptr->size; maj++)
5797c478bd9Sstevel@tonic-gate 				free(ptr->cached_file[maj]);
5807c478bd9Sstevel@tonic-gate 			free(ptr->cached_file);
5817c478bd9Sstevel@tonic-gate 			free(ptr);
5827c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_MEM));
5837c478bd9Sstevel@tonic-gate 			return (ERROR);
5847c478bd9Sstevel@tonic-gate 		}
5857c478bd9Sstevel@tonic-gate 		ptr->next = head;
5867c478bd9Sstevel@tonic-gate 		head = ptr;
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 	/* return value pointer to contents of file */
5897c478bd9Sstevel@tonic-gate 	*cache = ptr->cached_file;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	/* return size */
5927c478bd9Sstevel@tonic-gate 	return (ptr->size);
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate /*
5977c478bd9Sstevel@tonic-gate  * Using get_cached_n_to_m_file(), retrieve maximum major number
5987c478bd9Sstevel@tonic-gate  * found in the specificed file (name_to_major/rem_name_to_major).
5997c478bd9Sstevel@tonic-gate  *
6007c478bd9Sstevel@tonic-gate  * The return value is actually the size of the internal cache including 0.
6017c478bd9Sstevel@tonic-gate  */
6027c478bd9Sstevel@tonic-gate int
6037c478bd9Sstevel@tonic-gate get_max_major(char *file_name)
6047c478bd9Sstevel@tonic-gate {
6057c478bd9Sstevel@tonic-gate 	char **n_to_m_cache = NULL;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	return (get_cached_n_to_m_file(file_name, &n_to_m_cache));
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate /*
6127c478bd9Sstevel@tonic-gate  * searching name_to_major: if major_no == UNIQUE then the caller wants to
6137c478bd9Sstevel@tonic-gate  * use the driver name as the key.  Otherwise, the caller wants to use
6147c478bd9Sstevel@tonic-gate  * the major number as a key.
6157c478bd9Sstevel@tonic-gate  *
6167c478bd9Sstevel@tonic-gate  * This routine caches the contents of the name_to_major file on
6177c478bd9Sstevel@tonic-gate  * first call.  And it could be generalized to deal with other
6187c478bd9Sstevel@tonic-gate  * config files if necessary.
6197c478bd9Sstevel@tonic-gate  */
6207c478bd9Sstevel@tonic-gate static int
6217c478bd9Sstevel@tonic-gate get_name_to_major_entry(int *major_no, char *driver_name, char *file_name)
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	int maj;
6247c478bd9Sstevel@tonic-gate 	char **n_to_m_cache = NULL;
6257c478bd9Sstevel@tonic-gate 	int size = 0;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	int ret = NOT_UNIQUE;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	/*
6307c478bd9Sstevel@tonic-gate 	 * read the file in - we cache it in case caller wants to
6317c478bd9Sstevel@tonic-gate 	 * do multiple lookups
6327c478bd9Sstevel@tonic-gate 	 */
6337c478bd9Sstevel@tonic-gate 	size = get_cached_n_to_m_file(file_name, &n_to_m_cache);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	if (size == ERROR)
6367c478bd9Sstevel@tonic-gate 		return (ERROR);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	/* search with driver name as key */
6397c478bd9Sstevel@tonic-gate 	if (*major_no == UNIQUE) {
6407c478bd9Sstevel@tonic-gate 		for (maj = 0; maj < size; maj++) {
6417c478bd9Sstevel@tonic-gate 			if ((n_to_m_cache[maj] != NULL) &&
6427c478bd9Sstevel@tonic-gate 			    (strcmp(driver_name, n_to_m_cache[maj]) == 0)) {
6437c478bd9Sstevel@tonic-gate 				*major_no = maj;
6447c478bd9Sstevel@tonic-gate 				break;
6457c478bd9Sstevel@tonic-gate 			}
6467c478bd9Sstevel@tonic-gate 		}
6477c478bd9Sstevel@tonic-gate 		if (maj >= size)
6487c478bd9Sstevel@tonic-gate 			ret = UNIQUE;
6497c478bd9Sstevel@tonic-gate 	/* search with major number as key */
6507c478bd9Sstevel@tonic-gate 	} else {
6517c478bd9Sstevel@tonic-gate 		/*
6527c478bd9Sstevel@tonic-gate 		 * Bugid 1254588, drvconfig dump core after loading driver
6537c478bd9Sstevel@tonic-gate 		 * with major number bigger than entries defined in
6547c478bd9Sstevel@tonic-gate 		 * /etc/name_to_major.
6557c478bd9Sstevel@tonic-gate 		 */
6567c478bd9Sstevel@tonic-gate 		if (*major_no >= size)
6577c478bd9Sstevel@tonic-gate 			return (UNIQUE);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 		if (n_to_m_cache[*major_no] != NULL) {
6607c478bd9Sstevel@tonic-gate 			(void) strcpy(driver_name, n_to_m_cache[*major_no]);
6617c478bd9Sstevel@tonic-gate 		} else
6627c478bd9Sstevel@tonic-gate 			ret = UNIQUE;
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 	return (ret);
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate /*
668f4da9be0Scth  * Given pointer to begining of member 'n' in a space (or separator)
669f4da9be0Scth  * separated list, return pointer to member 'n+1', and establish member 'n'
670f4da9be0Scth  * in *current_entry.  If unquote, then we skip a leading quote and treat
671f4da9be0Scth  * the trailing quote as a separator (and skip).
6727c478bd9Sstevel@tonic-gate  */
6737c478bd9Sstevel@tonic-gate char *
6747c478bd9Sstevel@tonic-gate get_entry(
6757c478bd9Sstevel@tonic-gate 	char *prev_member,
6767c478bd9Sstevel@tonic-gate 	char *current_entry,
677f4da9be0Scth 	char separator,
678f4da9be0Scth 	int  unquote)
6797c478bd9Sstevel@tonic-gate {
6807c478bd9Sstevel@tonic-gate 	char	*ptr;
681f4da9be0Scth 	int	quoted = 0;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	ptr = prev_member;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	/* skip white space */
6867c478bd9Sstevel@tonic-gate 	while (*ptr == '\t' || *ptr == ' ')
6877c478bd9Sstevel@tonic-gate 		ptr++;
6887c478bd9Sstevel@tonic-gate 
689f4da9be0Scth 	/* if unquote skip leading quote */
690f4da9be0Scth 	if (unquote && *ptr == '"') {
691f4da9be0Scth 		quoted++;
692f4da9be0Scth 		ptr++;
693f4da9be0Scth 	}
694f4da9be0Scth 
695f4da9be0Scth 	/* read thru the current entry looking for end, separator, or unquote */
696f4da9be0Scth 	while (*ptr &&
697f4da9be0Scth 	    (*ptr != separator) &&
698f4da9be0Scth 	    ((separator != ' ') || (*ptr != '\t')) &&
699f4da9be0Scth 	    (!quoted || (*ptr != '"'))) {
7007c478bd9Sstevel@tonic-gate 		*current_entry++ = *ptr++;
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 	*current_entry = '\0';
7037c478bd9Sstevel@tonic-gate 
704f4da9be0Scth 	if (separator && (*ptr == separator))
705f4da9be0Scth 		ptr++;	/* skip over separator */
706f4da9be0Scth 	if (quoted && (*ptr == '"'))
707f4da9be0Scth 		ptr++;	/* skip over trailing quote */
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	/* skip white space */
7107c478bd9Sstevel@tonic-gate 	while (*ptr == '\t' || *ptr == ' ') {
7117c478bd9Sstevel@tonic-gate 		ptr++;
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	return (ptr);
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate void
7187c478bd9Sstevel@tonic-gate enter_lock(void)
7197c478bd9Sstevel@tonic-gate {
72049e92448Svikram 	struct flock lock;
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	/*
7237c478bd9Sstevel@tonic-gate 	 * attempt to create the lock file
7247c478bd9Sstevel@tonic-gate 	 */
72549e92448Svikram 	add_rem_lock_fd = open(add_rem_lock, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
72649e92448Svikram 	if (add_rem_lock_fd < 0) {
72749e92448Svikram 		(void) fprintf(stderr, gettext(ERR_CREAT_LOCK),
72849e92448Svikram 		    add_rem_lock, strerror(errno));
72949e92448Svikram 		exit(1);
73049e92448Svikram 	}
73149e92448Svikram 
73249e92448Svikram 	lock.l_type = F_WRLCK;
73349e92448Svikram 	lock.l_whence = SEEK_SET;
73449e92448Svikram 	lock.l_start = 0;
73549e92448Svikram 	lock.l_len = 0;
73649e92448Svikram 
73749e92448Svikram 	/* Try for the lock but don't wait. */
73849e92448Svikram 	if (fcntl(add_rem_lock_fd, F_SETLK, &lock) == -1) {
73949e92448Svikram 		if (errno == EACCES || errno == EAGAIN) {
7407c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_PROG_IN_USE));
7417c478bd9Sstevel@tonic-gate 		} else {
74249e92448Svikram 			(void) fprintf(stderr, gettext(ERR_LOCK),
74349e92448Svikram 			    add_rem_lock, strerror(errno));
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 		exit(1);
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate void
7507c478bd9Sstevel@tonic-gate err_exit(void)
7517c478bd9Sstevel@tonic-gate {
7527c478bd9Sstevel@tonic-gate 	/* release memory allocated for moddir */
7537c478bd9Sstevel@tonic-gate 	cleanup_moddir();
7547c478bd9Sstevel@tonic-gate 	/* remove add_drv/rem_drv lock */
7557c478bd9Sstevel@tonic-gate 	exit_unlock();
7567c478bd9Sstevel@tonic-gate 	exit(1);
7577c478bd9Sstevel@tonic-gate }
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate void
7607c478bd9Sstevel@tonic-gate cleanup_moddir(void)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	struct drvmod_dir *walk_ptr;
7637c478bd9Sstevel@tonic-gate 	struct drvmod_dir *free_ptr = moddir;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	while (free_ptr != NULL) {
7667c478bd9Sstevel@tonic-gate 		walk_ptr = free_ptr->next;
7677c478bd9Sstevel@tonic-gate 		free(free_ptr);
7687c478bd9Sstevel@tonic-gate 		free_ptr = walk_ptr;
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate void
7737c478bd9Sstevel@tonic-gate exit_unlock(void)
7747c478bd9Sstevel@tonic-gate {
77549e92448Svikram 	struct flock unlock;
7767c478bd9Sstevel@tonic-gate 
77749e92448Svikram 	if (add_rem_lock_fd < 0)
77849e92448Svikram 		return;
77949e92448Svikram 
78049e92448Svikram 	unlock.l_type = F_UNLCK;
78149e92448Svikram 	unlock.l_whence = SEEK_SET;
78249e92448Svikram 	unlock.l_start = 0;
78349e92448Svikram 	unlock.l_len = 0;
78449e92448Svikram 
78549e92448Svikram 	if (fcntl(add_rem_lock_fd, F_SETLK, &unlock) == -1) {
78649e92448Svikram 		(void) fprintf(stderr, gettext(ERR_UNLOCK),
78749e92448Svikram 		    add_rem_lock, strerror(errno));
78849e92448Svikram 	} else {
78949e92448Svikram 		(void) close(add_rem_lock_fd);
79049e92448Svikram 		add_rem_lock_fd = -1;
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate }
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate /*
7957c478bd9Sstevel@tonic-gate  * error adding driver; need to back out any changes to files.
7967c478bd9Sstevel@tonic-gate  * check flag to see which files need entries removed
7977c478bd9Sstevel@tonic-gate  * entry removal based on driver name
7987c478bd9Sstevel@tonic-gate  */
7997c478bd9Sstevel@tonic-gate void
8007c478bd9Sstevel@tonic-gate remove_entry(
8017c478bd9Sstevel@tonic-gate 	int c_flag,
8027c478bd9Sstevel@tonic-gate 	char *driver_name)
8037c478bd9Sstevel@tonic-gate {
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	if (c_flag & CLEAN_NAM_MAJ) {
8067c478bd9Sstevel@tonic-gate 		if (delete_entry(name_to_major, driver_name, " ",
8077c478bd9Sstevel@tonic-gate 		    NULL) == ERROR) {
8087c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_CLEAN),
8097c478bd9Sstevel@tonic-gate 			    name_to_major, driver_name);
8107c478bd9Sstevel@tonic-gate 		}
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	if (c_flag & CLEAN_DRV_ALIAS) {
8147c478bd9Sstevel@tonic-gate 		if (delete_entry(driver_aliases, driver_name, " ",
8157c478bd9Sstevel@tonic-gate 		    NULL) == ERROR) {
8167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_DEL_ENTRY),
8177c478bd9Sstevel@tonic-gate 			    driver_name, driver_aliases);
8187c478bd9Sstevel@tonic-gate 		}
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	if (c_flag & CLEAN_DRV_CLASSES) {
8227c478bd9Sstevel@tonic-gate 		if (delete_entry(driver_classes, driver_name, "\t", NULL) ==
8237c478bd9Sstevel@tonic-gate 		    ERROR) {
8247c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_DEL_ENTRY),
8257c478bd9Sstevel@tonic-gate 			    driver_name, driver_classes);
8267c478bd9Sstevel@tonic-gate 		}
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	if (c_flag & CLEAN_MINOR_PERM) {
8307c478bd9Sstevel@tonic-gate 		if (delete_entry(minor_perm, driver_name, ":", NULL) == ERROR) {
8317c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_DEL_ENTRY),
8327c478bd9Sstevel@tonic-gate 			    driver_name, minor_perm);
8337c478bd9Sstevel@tonic-gate 		}
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 	/*
8367c478bd9Sstevel@tonic-gate 	 * There's no point in removing entries from files that don't
8377c478bd9Sstevel@tonic-gate 	 * exist.  Prevent error messages by checking for file existence
8387c478bd9Sstevel@tonic-gate 	 * first.
8397c478bd9Sstevel@tonic-gate 	 */
8407c478bd9Sstevel@tonic-gate 	if ((c_flag & CLEAN_DEV_POLICY) != 0 &&
8417c478bd9Sstevel@tonic-gate 	    access(device_policy, F_OK) == 0) {
8427c478bd9Sstevel@tonic-gate 		if (delete_plcy_entry(device_policy, driver_name) == ERROR) {
8437c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_DEL_ENTRY),
8447c478bd9Sstevel@tonic-gate 			    driver_name, device_policy);
8457c478bd9Sstevel@tonic-gate 		}
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate 	if ((c_flag & CLEAN_DRV_PRIV) != 0 &&
8487c478bd9Sstevel@tonic-gate 	    access(extra_privs, F_OK) == 0) {
8497c478bd9Sstevel@tonic-gate 		if (delete_entry(extra_privs, driver_name, ":", NULL) ==
8507c478bd9Sstevel@tonic-gate 		    ERROR) {
8517c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_DEL_ENTRY),
8527c478bd9Sstevel@tonic-gate 			    driver_name, extra_privs);
8537c478bd9Sstevel@tonic-gate 		}
8547c478bd9Sstevel@tonic-gate 	}
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate int
8587c478bd9Sstevel@tonic-gate check_perms_aliases(
8597c478bd9Sstevel@tonic-gate 	int m_flag,
8607c478bd9Sstevel@tonic-gate 	int i_flag)
8617c478bd9Sstevel@tonic-gate {
8627c478bd9Sstevel@tonic-gate 	/*
8637c478bd9Sstevel@tonic-gate 	 * If neither i_flag nor m_flag are specified no need to check the
8647c478bd9Sstevel@tonic-gate 	 * files for access permissions
8657c478bd9Sstevel@tonic-gate 	 */
8667c478bd9Sstevel@tonic-gate 	if (!m_flag && !i_flag)
8677c478bd9Sstevel@tonic-gate 		return (NOERR);
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	/* check minor_perm file : exits and is writable */
8707c478bd9Sstevel@tonic-gate 	if (m_flag) {
8717c478bd9Sstevel@tonic-gate 		if (access(minor_perm, R_OK | W_OK)) {
8727c478bd9Sstevel@tonic-gate 			perror(NULL);
8737c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
8747c478bd9Sstevel@tonic-gate 			    minor_perm);
8757c478bd9Sstevel@tonic-gate 			return (ERROR);
8767c478bd9Sstevel@tonic-gate 		}
8777c478bd9Sstevel@tonic-gate 	}
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	/* check driver_aliases file : exits and is writable */
8807c478bd9Sstevel@tonic-gate 	if (i_flag) {
8817c478bd9Sstevel@tonic-gate 		if (access(driver_aliases, R_OK | W_OK)) {
8827c478bd9Sstevel@tonic-gate 			perror(NULL);
8837c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
8847c478bd9Sstevel@tonic-gate 			    driver_aliases);
8857c478bd9Sstevel@tonic-gate 			return (ERROR);
8867c478bd9Sstevel@tonic-gate 		}
8877c478bd9Sstevel@tonic-gate 	}
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	return (NOERR);
8907c478bd9Sstevel@tonic-gate }
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate int
8947c478bd9Sstevel@tonic-gate check_name_to_major(int mode)
8957c478bd9Sstevel@tonic-gate {
8967c478bd9Sstevel@tonic-gate 	/* check name_to_major file : exists and is writable */
8977c478bd9Sstevel@tonic-gate 	if (access(name_to_major, mode)) {
8987c478bd9Sstevel@tonic-gate 		perror(NULL);
8997c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
9007c478bd9Sstevel@tonic-gate 		    name_to_major);
9017c478bd9Sstevel@tonic-gate 		return (ERROR);
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	return (NOERR);
9057c478bd9Sstevel@tonic-gate }
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate /*
9097c478bd9Sstevel@tonic-gate  * All this stuff is to support a server installing
9107c478bd9Sstevel@tonic-gate  * drivers on diskless clients.  When on the server
9117c478bd9Sstevel@tonic-gate  * need to prepend the basedir
9127c478bd9Sstevel@tonic-gate  */
9137c478bd9Sstevel@tonic-gate int
9147c478bd9Sstevel@tonic-gate build_filenames(char *basedir)
9157c478bd9Sstevel@tonic-gate {
9167c478bd9Sstevel@tonic-gate 	int len;
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	if (basedir == NULL) {
9197c478bd9Sstevel@tonic-gate 		driver_aliases = DRIVER_ALIAS;
9207c478bd9Sstevel@tonic-gate 		driver_classes = DRIVER_CLASSES;
9217c478bd9Sstevel@tonic-gate 		minor_perm = MINOR_PERM;
9227c478bd9Sstevel@tonic-gate 		name_to_major = NAM_TO_MAJ;
9237c478bd9Sstevel@tonic-gate 		rem_name_to_major = REM_NAM_TO_MAJ;
9247c478bd9Sstevel@tonic-gate 		add_rem_lock = ADD_REM_LOCK;
9257c478bd9Sstevel@tonic-gate 		tmphold = TMPHOLD;
9267c478bd9Sstevel@tonic-gate 		devfs_root = DEVFS_ROOT;
9277c478bd9Sstevel@tonic-gate 		device_policy = DEV_POLICY;
9287c478bd9Sstevel@tonic-gate 		extra_privs = EXTRA_PRIVS;
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	} else {
9317c478bd9Sstevel@tonic-gate 		len = strlen(basedir);
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 		driver_aliases = malloc(len + sizeof (DRIVER_ALIAS));
9347c478bd9Sstevel@tonic-gate 		driver_classes = malloc(len + sizeof (DRIVER_CLASSES));
9357c478bd9Sstevel@tonic-gate 		minor_perm = malloc(len + sizeof (MINOR_PERM));
9367c478bd9Sstevel@tonic-gate 		name_to_major = malloc(len + sizeof (NAM_TO_MAJ));
9377c478bd9Sstevel@tonic-gate 		rem_name_to_major = malloc(len + sizeof (REM_NAM_TO_MAJ));
9387c478bd9Sstevel@tonic-gate 		add_rem_lock = malloc(len + sizeof (ADD_REM_LOCK));
9397c478bd9Sstevel@tonic-gate 		tmphold = malloc(len + sizeof (TMPHOLD));
9407c478bd9Sstevel@tonic-gate 		devfs_root = malloc(len + sizeof (DEVFS_ROOT));
9417c478bd9Sstevel@tonic-gate 		device_policy = malloc(len + sizeof (DEV_POLICY));
9427c478bd9Sstevel@tonic-gate 		extra_privs = malloc(len + sizeof (EXTRA_PRIVS));
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 		if ((driver_aliases == NULL) ||
9467c478bd9Sstevel@tonic-gate 		    (driver_classes == NULL) ||
9477c478bd9Sstevel@tonic-gate 		    (minor_perm == NULL) ||
9487c478bd9Sstevel@tonic-gate 		    (name_to_major == NULL) ||
9497c478bd9Sstevel@tonic-gate 		    (rem_name_to_major == NULL) ||
9507c478bd9Sstevel@tonic-gate 		    (add_rem_lock == NULL) ||
9517c478bd9Sstevel@tonic-gate 		    (tmphold == NULL) ||
9527c478bd9Sstevel@tonic-gate 		    (devfs_root == NULL) ||
9537c478bd9Sstevel@tonic-gate 		    (device_policy == NULL) ||
9547c478bd9Sstevel@tonic-gate 		    (extra_privs == NULL)) {
9557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_NO_MEM));
9567c478bd9Sstevel@tonic-gate 			return (ERROR);
9577c478bd9Sstevel@tonic-gate 		}
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 		(void) sprintf(driver_aliases, "%s%s", basedir, DRIVER_ALIAS);
9607c478bd9Sstevel@tonic-gate 		(void) sprintf(driver_classes, "%s%s", basedir, DRIVER_CLASSES);
9617c478bd9Sstevel@tonic-gate 		(void) sprintf(minor_perm, "%s%s", basedir, MINOR_PERM);
9627c478bd9Sstevel@tonic-gate 		(void) sprintf(name_to_major, "%s%s", basedir, NAM_TO_MAJ);
9637c478bd9Sstevel@tonic-gate 		(void) sprintf(rem_name_to_major, "%s%s", basedir,
9647c478bd9Sstevel@tonic-gate 		    REM_NAM_TO_MAJ);
9657c478bd9Sstevel@tonic-gate 		(void) sprintf(add_rem_lock, "%s%s", basedir, ADD_REM_LOCK);
9667c478bd9Sstevel@tonic-gate 		(void) sprintf(tmphold, "%s%s", basedir, TMPHOLD);
9677c478bd9Sstevel@tonic-gate 		(void) sprintf(devfs_root, "%s%s", basedir, DEVFS_ROOT);
9687c478bd9Sstevel@tonic-gate 		(void) sprintf(device_policy, "%s%s", basedir, DEV_POLICY);
9697c478bd9Sstevel@tonic-gate 		(void) sprintf(extra_privs, "%s%s", basedir, EXTRA_PRIVS);
9707c478bd9Sstevel@tonic-gate 	}
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	return (NOERR);
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate static int
9767c478bd9Sstevel@tonic-gate exec_command(char *path, char *cmdline[MAX_CMD_LINE])
9777c478bd9Sstevel@tonic-gate {
9787c478bd9Sstevel@tonic-gate 	pid_t pid;
9797c478bd9Sstevel@tonic-gate 	uint_t stat_loc;
9807c478bd9Sstevel@tonic-gate 	int waitstat;
9817c478bd9Sstevel@tonic-gate 	int exit_status;
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	/* child */
9847c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == 0) {
9857c478bd9Sstevel@tonic-gate 		(void) execv(path, cmdline);
9867c478bd9Sstevel@tonic-gate 		perror(NULL);
9877c478bd9Sstevel@tonic-gate 		return (ERROR);
9887c478bd9Sstevel@tonic-gate 	} else if (pid == -1) {
9897c478bd9Sstevel@tonic-gate 		/* fork failed */
9907c478bd9Sstevel@tonic-gate 		perror(NULL);
9917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_FORK_FAIL), cmdline);
9927c478bd9Sstevel@tonic-gate 		return (ERROR);
9937c478bd9Sstevel@tonic-gate 	} else {
9947c478bd9Sstevel@tonic-gate 		/* parent */
9957c478bd9Sstevel@tonic-gate 		do {
9967c478bd9Sstevel@tonic-gate 			waitstat = waitpid(pid, (int *)&stat_loc, 0);
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 		} while ((!WIFEXITED(stat_loc) &&
9997c478bd9Sstevel@tonic-gate 		    !WIFSIGNALED(stat_loc)) || (waitstat == 0));
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 		exit_status = WEXITSTATUS(stat_loc);
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 		return (exit_status);
10047c478bd9Sstevel@tonic-gate 	}
10057c478bd9Sstevel@tonic-gate }
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate /*
1008*6532b960SJerry Gilliam  * Exec devfsadm to perform driver config/unconfig operation,
1009*6532b960SJerry Gilliam  * adding or removing aliases.
10107c478bd9Sstevel@tonic-gate  */
1011*6532b960SJerry Gilliam static int
1012*6532b960SJerry Gilliam exec_devfsadm(
1013*6532b960SJerry Gilliam 	boolean_t config,
10147c478bd9Sstevel@tonic-gate 	char *driver_name,
10157c478bd9Sstevel@tonic-gate 	major_t major_num,
10167c478bd9Sstevel@tonic-gate 	char *aliases,
10177c478bd9Sstevel@tonic-gate 	char *classes,
1018*6532b960SJerry Gilliam 	int verbose_flag,
1019*6532b960SJerry Gilliam 	int force_flag)
10207c478bd9Sstevel@tonic-gate {
10217c478bd9Sstevel@tonic-gate 	int n = 0;
10227c478bd9Sstevel@tonic-gate 	char *cmdline[MAX_CMD_LINE];
10237c478bd9Sstevel@tonic-gate 	char maj_num[128];
10247c478bd9Sstevel@tonic-gate 	char *previous;
10257c478bd9Sstevel@tonic-gate 	char *current;
10267c478bd9Sstevel@tonic-gate 	int exec_status;
10277c478bd9Sstevel@tonic-gate 	int len;
1028*6532b960SJerry Gilliam 	int rv;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	/* build command line */
10317c478bd9Sstevel@tonic-gate 	cmdline[n++] = DRVCONFIG;
1032*6532b960SJerry Gilliam 	if (config == B_FALSE) {
1033*6532b960SJerry Gilliam 		cmdline[n++] = "-u";		/* unconfigure */
1034*6532b960SJerry Gilliam 		if (force_flag)
1035*6532b960SJerry Gilliam 			cmdline[n++] = "-f";	/* force if currently in use */
1036*6532b960SJerry Gilliam 	}
10377c478bd9Sstevel@tonic-gate 	if (verbose_flag) {
10387c478bd9Sstevel@tonic-gate 		cmdline[n++] = "-v";
10397c478bd9Sstevel@tonic-gate 	}
10407c478bd9Sstevel@tonic-gate 	cmdline[n++] = "-b";
10417c478bd9Sstevel@tonic-gate 	if (classes) {
10427c478bd9Sstevel@tonic-gate 		cmdline[n++] = "-c";
10437c478bd9Sstevel@tonic-gate 		cmdline[n++] = classes;
10447c478bd9Sstevel@tonic-gate 	}
10457c478bd9Sstevel@tonic-gate 	cmdline[n++] = "-i";
10467c478bd9Sstevel@tonic-gate 	cmdline[n++] = driver_name;
10477c478bd9Sstevel@tonic-gate 	cmdline[n++] = "-m";
10487c478bd9Sstevel@tonic-gate 	(void) sprintf(maj_num, "%lu", major_num);
10497c478bd9Sstevel@tonic-gate 	cmdline[n++] = maj_num;
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	if (aliases != NULL) {
10527c478bd9Sstevel@tonic-gate 		len = strlen(aliases);
10537c478bd9Sstevel@tonic-gate 		previous = aliases;
10547c478bd9Sstevel@tonic-gate 		do {
10557c478bd9Sstevel@tonic-gate 			cmdline[n++] = "-a";
10567c478bd9Sstevel@tonic-gate 			cmdline[n] = calloc(len + 1, 1);
10577c478bd9Sstevel@tonic-gate 			if (cmdline[n] == NULL) {
10587c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10597c478bd9Sstevel@tonic-gate 				    gettext(ERR_NO_MEM));
10607c478bd9Sstevel@tonic-gate 				return (ERROR);
10617c478bd9Sstevel@tonic-gate 			}
10627c478bd9Sstevel@tonic-gate 			current = get_entry(previous,
1063f4da9be0Scth 			    cmdline[n++], ' ', 0);
10647c478bd9Sstevel@tonic-gate 			previous = current;
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 		} while (*current != '\0');
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	}
10697c478bd9Sstevel@tonic-gate 	cmdline[n] = (char *)0;
10707c478bd9Sstevel@tonic-gate 
1071*6532b960SJerry Gilliam 	rv = exec_command(DRVCONFIG_PATH, cmdline);
1072*6532b960SJerry Gilliam 	if (rv == NOERR)
1073*6532b960SJerry Gilliam 		return (NOERR);
1074*6532b960SJerry Gilliam 	return (ERROR);
1075*6532b960SJerry Gilliam }
10767c478bd9Sstevel@tonic-gate 
1077*6532b960SJerry Gilliam int
1078*6532b960SJerry Gilliam unconfig_driver(
1079*6532b960SJerry Gilliam 	char *driver_name,
1080*6532b960SJerry Gilliam 	major_t major_num,
1081*6532b960SJerry Gilliam 	char *aliases,
1082*6532b960SJerry Gilliam 	int verbose_flag,
1083*6532b960SJerry Gilliam 	int force_flag)
1084*6532b960SJerry Gilliam {
1085*6532b960SJerry Gilliam 	return (exec_devfsadm(B_FALSE, driver_name, major_num,
1086*6532b960SJerry Gilliam 	    aliases, NULL, verbose_flag, force_flag));
1087*6532b960SJerry Gilliam }
1088*6532b960SJerry Gilliam 
1089*6532b960SJerry Gilliam /*
1090*6532b960SJerry Gilliam  * check that major_num doesn't exceed maximum on this machine
1091*6532b960SJerry Gilliam  * do this here to support add_drv on server for diskless clients
1092*6532b960SJerry Gilliam  */
1093*6532b960SJerry Gilliam int
1094*6532b960SJerry Gilliam config_driver(
1095*6532b960SJerry Gilliam 	char *driver_name,
1096*6532b960SJerry Gilliam 	major_t major_num,
1097*6532b960SJerry Gilliam 	char *aliases,
1098*6532b960SJerry Gilliam 	char *classes,
1099*6532b960SJerry Gilliam 	int cleanup_flag,
1100*6532b960SJerry Gilliam 	int verbose_flag)
1101*6532b960SJerry Gilliam {
1102*6532b960SJerry Gilliam 	int	max_dev;
1103*6532b960SJerry Gilliam 	int	rv;
1104*6532b960SJerry Gilliam 
1105*6532b960SJerry Gilliam 	if (modctl(MODRESERVED, NULL, &max_dev) < 0) {
1106*6532b960SJerry Gilliam 		perror(NULL);
1107*6532b960SJerry Gilliam 		(void) fprintf(stderr, gettext(ERR_MAX_MAJOR));
1108*6532b960SJerry Gilliam 		return (ERROR);
1109*6532b960SJerry Gilliam 	}
1110*6532b960SJerry Gilliam 
1111*6532b960SJerry Gilliam 	if (major_num >= max_dev) {
1112*6532b960SJerry Gilliam 		(void) fprintf(stderr, gettext(ERR_MAX_EXCEEDS),
1113*6532b960SJerry Gilliam 		    major_num, max_dev);
1114*6532b960SJerry Gilliam 		return (ERROR);
1115*6532b960SJerry Gilliam 	}
1116*6532b960SJerry Gilliam 
1117*6532b960SJerry Gilliam 	/* bind major number and driver name */
1118*6532b960SJerry Gilliam 	rv = exec_devfsadm(B_TRUE, driver_name, major_num,
1119*6532b960SJerry Gilliam 	    aliases, classes, verbose_flag, 0);
1120*6532b960SJerry Gilliam 
1121*6532b960SJerry Gilliam 	if (rv == NOERR)
11227c478bd9Sstevel@tonic-gate 		return (NOERR);
11237c478bd9Sstevel@tonic-gate 	perror(NULL);
11247c478bd9Sstevel@tonic-gate 	remove_entry(cleanup_flag, driver_name);
11257c478bd9Sstevel@tonic-gate 	return (ERROR);
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate void
11297c478bd9Sstevel@tonic-gate load_driver(char *driver_name, int verbose_flag)
11307c478bd9Sstevel@tonic-gate {
11317c478bd9Sstevel@tonic-gate 	int n = 0;
11327c478bd9Sstevel@tonic-gate 	char *cmdline[MAX_CMD_LINE];
11337c478bd9Sstevel@tonic-gate 	int exec_status;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	/* build command line */
11367c478bd9Sstevel@tonic-gate 	cmdline[n++] = DEVFSADM;
11377c478bd9Sstevel@tonic-gate 	if (verbose_flag) {
11387c478bd9Sstevel@tonic-gate 		cmdline[n++] = "-v";
11397c478bd9Sstevel@tonic-gate 	}
11407c478bd9Sstevel@tonic-gate 	cmdline[n++] = "-i";
11417c478bd9Sstevel@tonic-gate 	cmdline[n++] = driver_name;
11427c478bd9Sstevel@tonic-gate 	cmdline[n] = (char *)0;
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	exec_status = exec_command(DEVFSADM_PATH, cmdline);
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	if (exec_status != NOERR) {
11477c478bd9Sstevel@tonic-gate 		/* no clean : name and major number are bound */
114811ebc2d0SJerry Gilliam 		(void) fprintf(stderr, gettext(ERR_CONFIG), driver_name);
11497c478bd9Sstevel@tonic-gate 	}
11507c478bd9Sstevel@tonic-gate }
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate void
11537c478bd9Sstevel@tonic-gate get_modid(char *driver_name, int *mod)
11547c478bd9Sstevel@tonic-gate {
11557c478bd9Sstevel@tonic-gate 	struct modinfo	modinfo;
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	modinfo.mi_id = -1;
11587c478bd9Sstevel@tonic-gate 	modinfo.mi_info = MI_INFO_ALL;
11597c478bd9Sstevel@tonic-gate 	do {
11607c478bd9Sstevel@tonic-gate 		/*
11617c478bd9Sstevel@tonic-gate 		 * If we are at the end of the list of loaded modules
11627c478bd9Sstevel@tonic-gate 		 * then set *mod = -1 and return
11637c478bd9Sstevel@tonic-gate 		 */
11647c478bd9Sstevel@tonic-gate 		if (modctl(MODINFO, 0, &modinfo) < 0) {
11657c478bd9Sstevel@tonic-gate 			*mod = -1;
11667c478bd9Sstevel@tonic-gate 			return;
11677c478bd9Sstevel@tonic-gate 		}
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 		*mod = modinfo.mi_id;
11707c478bd9Sstevel@tonic-gate 	} while (strcmp(driver_name, modinfo.mi_name) != 0);
11717c478bd9Sstevel@tonic-gate }
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate int
11747c478bd9Sstevel@tonic-gate create_reconfig(char *basedir)
11757c478bd9Sstevel@tonic-gate {
11767c478bd9Sstevel@tonic-gate 	char reconfig_file[MAXPATHLEN + FILENAME_MAX + 1];
11777c478bd9Sstevel@tonic-gate 	FILE *reconfig_fp;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	if (basedir != NULL) {
11807c478bd9Sstevel@tonic-gate 		(void) strcpy(reconfig_file, basedir);
11817c478bd9Sstevel@tonic-gate 		(void) strcat(reconfig_file, RECONFIGURE);
11827c478bd9Sstevel@tonic-gate 	} else {
11837c478bd9Sstevel@tonic-gate 		(void) strcpy(reconfig_file, RECONFIGURE);
11847c478bd9Sstevel@tonic-gate 	}
11857c478bd9Sstevel@tonic-gate 	if ((reconfig_fp = fopen(reconfig_file, "a")) == NULL)
11867c478bd9Sstevel@tonic-gate 		return (ERROR);
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	(void) fclose(reconfig_fp);
11897c478bd9Sstevel@tonic-gate 	return (NOERR);
11907c478bd9Sstevel@tonic-gate }
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate /*
11947c478bd9Sstevel@tonic-gate  * update_minor_entry:
11957c478bd9Sstevel@tonic-gate  *	open file
11967c478bd9Sstevel@tonic-gate  *	for each entry in list
11977c478bd9Sstevel@tonic-gate  *		where list entries are separated by <list_separator>
11987c478bd9Sstevel@tonic-gate  * 		modify entry : driver_name <entry_separator> entry
11997c478bd9Sstevel@tonic-gate  *	close file
12007c478bd9Sstevel@tonic-gate  *
12017c478bd9Sstevel@tonic-gate  *	return error/noerr
12027c478bd9Sstevel@tonic-gate  */
12037c478bd9Sstevel@tonic-gate int
12047c478bd9Sstevel@tonic-gate update_minor_entry(char *driver_name, char *perm_list)
12057c478bd9Sstevel@tonic-gate {
12067c478bd9Sstevel@tonic-gate 	FILE *fp;
12077c478bd9Sstevel@tonic-gate 	FILE *newfp;
12087c478bd9Sstevel@tonic-gate 	struct group *sysgrp;
12097c478bd9Sstevel@tonic-gate 	int match = 0;
12101ca93273Seota 	char line[MAX_DBFILE_ENTRY], *cp, *dup;
12111ca93273Seota 	char drv[FILENAME_MAX + 1], *drv_minor;
12127c478bd9Sstevel@tonic-gate 	char minor[FILENAME_MAX + 1], perm[OPT_LEN + 1];
12137c478bd9Sstevel@tonic-gate 	char own[OPT_LEN + 1], grp[OPT_LEN + 1];
12147c478bd9Sstevel@tonic-gate 	int status = NOERR, i;
12157c478bd9Sstevel@tonic-gate 	char *newfile, *tptr;
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	if ((fp = fopen(minor_perm, "r")) == NULL) {
12187c478bd9Sstevel@tonic-gate 		perror(NULL);
12197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
12207c478bd9Sstevel@tonic-gate 		    minor_perm);
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 		return (ERROR);
12237c478bd9Sstevel@tonic-gate 	}
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	/*
12267c478bd9Sstevel@tonic-gate 	 * Build filename for temporary file
12277c478bd9Sstevel@tonic-gate 	 */
12287c478bd9Sstevel@tonic-gate 	if ((tptr = calloc(strlen(minor_perm) + strlen(XEND) + 1, 1)) == NULL) {
12297c478bd9Sstevel@tonic-gate 		perror(NULL);
12307c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
12317c478bd9Sstevel@tonic-gate 	}
12327c478bd9Sstevel@tonic-gate 	(void) strcpy(tptr, minor_perm);
12337c478bd9Sstevel@tonic-gate 	(void) strcat(tptr, XEND);
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	/*
12367c478bd9Sstevel@tonic-gate 	 * Set gid so we preserve group attribute.  Ideally we wouldn't
12377c478bd9Sstevel@tonic-gate 	 * assume a gid of "sys" but we can't undo the damage on already
12387c478bd9Sstevel@tonic-gate 	 * installed systems unless we force the issue.
12397c478bd9Sstevel@tonic-gate 	 */
12407c478bd9Sstevel@tonic-gate 	if ((sysgrp = getgrnam("sys")) != NULL) {
12417c478bd9Sstevel@tonic-gate 		(void) setgid(sysgrp->gr_gid);
12427c478bd9Sstevel@tonic-gate 	}
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	newfile = mktemp(tptr);
12457c478bd9Sstevel@tonic-gate 	if ((newfp = fopen(newfile, "w")) == NULL) {
12467c478bd9Sstevel@tonic-gate 		perror(NULL);
12477c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
12487c478bd9Sstevel@tonic-gate 		    newfile);
12497c478bd9Sstevel@tonic-gate 		return (ERROR);
12507c478bd9Sstevel@tonic-gate 	}
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	if (sscanf(perm_list, "%s%s%s%s", minor, perm, own, grp) != 4) {
12537c478bd9Sstevel@tonic-gate 		status = ERROR;
12547c478bd9Sstevel@tonic-gate 	}
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	while ((fgets(line, sizeof (line), fp) != NULL) && status == NOERR) {
12571ca93273Seota 		/* copy the whole line into dup */
12581ca93273Seota 		if ((dup = strdup(line)) == NULL) {
12591ca93273Seota 			perror(NULL);
12601ca93273Seota 			(void) fprintf(stderr, gettext(ERR_NO_MEM));
12611ca93273Seota 			status = ERROR;
12621ca93273Seota 			break;
12631ca93273Seota 		}
12641ca93273Seota 		/* cut off comments starting with '#' */
12651ca93273Seota 		if ((cp = strchr(dup, '#')) != NULL)
12661ca93273Seota 			*cp = '\0';
12671ca93273Seota 		/* ignore comment or blank lines */
12681ca93273Seota 		if (is_blank(dup)) {
12691ca93273Seota 			if (fputs(line, newfp) == EOF) {
12707c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_UPDATE),
12717c478bd9Sstevel@tonic-gate 				    minor_perm);
12727c478bd9Sstevel@tonic-gate 				status = ERROR;
12737c478bd9Sstevel@tonic-gate 			}
12741ca93273Seota 			free(dup);
12757c478bd9Sstevel@tonic-gate 			continue;
12767c478bd9Sstevel@tonic-gate 		}
12777c478bd9Sstevel@tonic-gate 
12781ca93273Seota 		/* get the driver name */
12791ca93273Seota 		if (sscanf(dup, "%s", drv) != 1) {
12807c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_BAD_LINE),
12817c478bd9Sstevel@tonic-gate 			    minor_perm, line);
12827c478bd9Sstevel@tonic-gate 			status = ERROR;
12831ca93273Seota 			free(dup);
12841ca93273Seota 			break;
12857c478bd9Sstevel@tonic-gate 		}
12861ca93273Seota 
12871ca93273Seota 		/*
12881ca93273Seota 		 * get the minor name; place the NULL character at the
12891ca93273Seota 		 * end of the driver name, then make the drv_minor
12901ca93273Seota 		 * point to the first character of the minor name.
12911ca93273Seota 		 * the line missing ':' must be treated as a broken one.
12921ca93273Seota 		 */
12931ca93273Seota 		i = strcspn(drv, ":");
12941ca93273Seota 		if (i == strlen(drv)) {
12951ca93273Seota 			(void) fprintf(stderr, gettext(ERR_BAD_LINE),
12961ca93273Seota 			    minor_perm, line);
12971ca93273Seota 			status = ERROR;
12981ca93273Seota 			free(dup);
12991ca93273Seota 			break;
13001ca93273Seota 		}
13017c478bd9Sstevel@tonic-gate 		drv[i] =  '\0';
13021ca93273Seota 		drv_minor = &drv[strlen(drv) + 1];
13037c478bd9Sstevel@tonic-gate 
13041ca93273Seota 		/*
13051ca93273Seota 		 * compare both of the driver name and the minor name.
13061ca93273Seota 		 * then the new line should be written to the file if
13071ca93273Seota 		 * both of them match
13081ca93273Seota 		 */
13097c478bd9Sstevel@tonic-gate 		if ((strcmp(drv, driver_name) == 0) &&
13107c478bd9Sstevel@tonic-gate 		    (strcmp(minor, drv_minor) == 0)) {
13111ca93273Seota 			/* if it has a comment, keep it */
13121ca93273Seota 			if (cp != NULL) {
13131ca93273Seota 				cp++; /* skip a terminator */
13141ca93273Seota 				(void) sprintf(line, "%s:%s %s %s %s #%s\n",
13151ca93273Seota 				    drv, minor, perm, own, grp, cp);
13161ca93273Seota 			} else {
13177c478bd9Sstevel@tonic-gate 				(void) sprintf(line, "%s:%s %s %s %s\n",
13187c478bd9Sstevel@tonic-gate 				    drv, minor, perm, own, grp);
13191ca93273Seota 			}
13207c478bd9Sstevel@tonic-gate 			match = 1;
13217c478bd9Sstevel@tonic-gate 		}
13221ca93273Seota 		free(dup);
13237c478bd9Sstevel@tonic-gate 
13241ca93273Seota 		/* update the file */
13257c478bd9Sstevel@tonic-gate 		if ((fputs(line, newfp)) == EOF) {
13267c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE),
13277c478bd9Sstevel@tonic-gate 			    minor_perm);
13287c478bd9Sstevel@tonic-gate 			status = ERROR;
13297c478bd9Sstevel@tonic-gate 		}
13307c478bd9Sstevel@tonic-gate 	}
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	if (!match) {
13337c478bd9Sstevel@tonic-gate 		(void) bzero(line, sizeof (&line[0]));
13347c478bd9Sstevel@tonic-gate 		(void) sprintf(line, "%s:%s %s %s %s\n",
13357c478bd9Sstevel@tonic-gate 		    driver_name, minor, perm, own, grp);
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 		/* add the new entry */
13387c478bd9Sstevel@tonic-gate 		if ((fputs(line, newfp)) == EOF) {
13397c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE), minor_perm);
13407c478bd9Sstevel@tonic-gate 			status = ERROR;
13417c478bd9Sstevel@tonic-gate 		}
13427c478bd9Sstevel@tonic-gate 	}
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	if (fflush(newfp) != 0 || fsync(fileno(newfp)) != 0)
13477c478bd9Sstevel@tonic-gate 		status = ERROR;
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 	(void) fclose(newfp);
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	/*
13527c478bd9Sstevel@tonic-gate 	 * if error, leave original file, delete new file
13537c478bd9Sstevel@tonic-gate 	 * if noerr, replace original file with new file
13547c478bd9Sstevel@tonic-gate 	 */
13557c478bd9Sstevel@tonic-gate 	if (status == NOERR) {
13567c478bd9Sstevel@tonic-gate 		if (rename(minor_perm, tmphold) == -1) {
13577c478bd9Sstevel@tonic-gate 			perror(NULL);
13587c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE), minor_perm);
13597c478bd9Sstevel@tonic-gate 			(void) unlink(newfile);
13607c478bd9Sstevel@tonic-gate 			return (ERROR);
13617c478bd9Sstevel@tonic-gate 		} else if (rename(newfile, minor_perm) == -1) {
13627c478bd9Sstevel@tonic-gate 			perror(NULL);
13637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_UPDATE), minor_perm);
13647c478bd9Sstevel@tonic-gate 			(void) unlink(minor_perm);
13657c478bd9Sstevel@tonic-gate 			(void) unlink(newfile);
13667c478bd9Sstevel@tonic-gate 			if (link(tmphold, minor_perm) == -1) {
13677c478bd9Sstevel@tonic-gate 				perror(NULL);
13687c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_BAD_LINK),
13697c478bd9Sstevel@tonic-gate 				    minor_perm, tmphold);
13707c478bd9Sstevel@tonic-gate 			}
13717c478bd9Sstevel@tonic-gate 			return (ERROR);
13727c478bd9Sstevel@tonic-gate 		}
13737c478bd9Sstevel@tonic-gate 		(void) unlink(tmphold);
13747c478bd9Sstevel@tonic-gate 	} else {
13757c478bd9Sstevel@tonic-gate 		/*
13767c478bd9Sstevel@tonic-gate 		 * since there's an error, leave file alone; remove
13777c478bd9Sstevel@tonic-gate 		 * new file
13787c478bd9Sstevel@tonic-gate 		 */
13797c478bd9Sstevel@tonic-gate 		if (unlink(newfile) == -1) {
13807c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_CANT_RM), newfile);
13817c478bd9Sstevel@tonic-gate 		}
13827c478bd9Sstevel@tonic-gate 		return (ERROR);
13837c478bd9Sstevel@tonic-gate 	}
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 	return (NOERR);
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate }
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate /*
13917c478bd9Sstevel@tonic-gate  * list_entry:
13927c478bd9Sstevel@tonic-gate  *	open file
13937c478bd9Sstevel@tonic-gate  *	read thru file, listing all entries if first entry = driver_name
13947c478bd9Sstevel@tonic-gate  *	close
13957c478bd9Sstevel@tonic-gate  */
13967c478bd9Sstevel@tonic-gate void
13977c478bd9Sstevel@tonic-gate list_entry(
13987c478bd9Sstevel@tonic-gate 	char *oldfile,
13997c478bd9Sstevel@tonic-gate 	char *driver_name,
14007c478bd9Sstevel@tonic-gate 	char *marker)
14017c478bd9Sstevel@tonic-gate {
14027c478bd9Sstevel@tonic-gate 	FILE	*fp;
14037c478bd9Sstevel@tonic-gate 	int	i;
14041ca93273Seota 	char	line[MAX_DBFILE_ENTRY], *cp;
14057c478bd9Sstevel@tonic-gate 	char	drv[FILENAME_MAX + 1];
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	if ((fp = fopen(oldfile, "r")) == NULL) {
14087c478bd9Sstevel@tonic-gate 		perror(NULL);
14097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), oldfile);
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 		return;
14127c478bd9Sstevel@tonic-gate 	}
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line), fp) != NULL) {
14151ca93273Seota 		/* cut off comments starting with '#' */
14161ca93273Seota 		if ((cp = strchr(line, '#')) != NULL)
14171ca93273Seota 			*cp = '\0';
14181ca93273Seota 		/* ignore comment or blank lines */
14191ca93273Seota 		if (is_blank(line))
14207c478bd9Sstevel@tonic-gate 			continue;
14211ca93273Seota 		/* sanity-check */
14227c478bd9Sstevel@tonic-gate 		if (sscanf(line, "%s", drv) != 1) {
14237c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_BAD_LINE),
14247c478bd9Sstevel@tonic-gate 			    oldfile, line);
14257c478bd9Sstevel@tonic-gate 		}
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 		for (i = strcspn(drv, marker); i < FILENAME_MAX; i++) {
14287c478bd9Sstevel@tonic-gate 			drv[i] =  '\0';
14297c478bd9Sstevel@tonic-gate 		}
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 		if (strcmp(driver_name, drv) == 0) {
14327c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "%s", line);
14337c478bd9Sstevel@tonic-gate 		}
14347c478bd9Sstevel@tonic-gate 	}
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
14377c478bd9Sstevel@tonic-gate }
14387c478bd9Sstevel@tonic-gate 
14391ca93273Seota static boolean_t
14401ca93273Seota is_token(char *tok)
14411ca93273Seota {
14421ca93273Seota 	/*
14431ca93273Seota 	 * Check the token here. According to IEEE1275 Open Firmware Boot
14441ca93273Seota 	 * Standard, the name is composed of 1 to 31 letters,
14451ca93273Seota 	 * digits and punctuation characters from the set ",._+-", and
14461ca93273Seota 	 * uppercase and lowercase characters are considered distinct.
14471ca93273Seota 	 * (ie. token := [a-zA-Z0-9,._+-]+, length(token) <= 31)
14481ca93273Seota 	 * However, since either the definition of driver or aliase names is
14491ca93273Seota 	 * not known well, only '#' is avoided explicitly. (the kernel lexical
14501ca93273Seota 	 * analyzer treats it as a start of a comment)
14511ca93273Seota 	 */
14521ca93273Seota 	for (/* nothing */; *tok != '\0'; tok++)
14531ca93273Seota 		if (*tok == '#' || iscntrl(*tok))
14541ca93273Seota 			return (B_FALSE);
14551ca93273Seota 
14561ca93273Seota 	return (B_TRUE);
14571ca93273Seota }
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate /*
14607c478bd9Sstevel@tonic-gate  * check each entry in perm_list for:
14617c478bd9Sstevel@tonic-gate  *	4 arguments
14627c478bd9Sstevel@tonic-gate  *	permission arg is in valid range
14637c478bd9Sstevel@tonic-gate  * permlist entries separated by comma
14647c478bd9Sstevel@tonic-gate  * return ERROR/NOERR
14657c478bd9Sstevel@tonic-gate  */
14667c478bd9Sstevel@tonic-gate int
14677c478bd9Sstevel@tonic-gate check_perm_opts(char *perm_list)
14687c478bd9Sstevel@tonic-gate {
14697c478bd9Sstevel@tonic-gate 	char *current_head;
14707c478bd9Sstevel@tonic-gate 	char *previous_head;
14717c478bd9Sstevel@tonic-gate 	char *one_entry;
14727c478bd9Sstevel@tonic-gate 	int i, len, scan_stat;
14737c478bd9Sstevel@tonic-gate 	char minor[FILENAME_MAX + 1];
14747c478bd9Sstevel@tonic-gate 	char perm[OPT_LEN + 1];
14757c478bd9Sstevel@tonic-gate 	char own[OPT_LEN + 1];
14767c478bd9Sstevel@tonic-gate 	char grp[OPT_LEN + 1];
14777c478bd9Sstevel@tonic-gate 	char dumb[OPT_LEN + 1];
14787c478bd9Sstevel@tonic-gate 	int status = NOERR;
14797c478bd9Sstevel@tonic-gate 	int intperm;
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 	len = strlen(perm_list);
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 	if (len == 0) {
14847c478bd9Sstevel@tonic-gate 		return (ERROR);
14857c478bd9Sstevel@tonic-gate 	}
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	one_entry = calloc(len + 1, 1);
14887c478bd9Sstevel@tonic-gate 	if (one_entry == NULL) {
14897c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
14907c478bd9Sstevel@tonic-gate 		return (ERROR);
14917c478bd9Sstevel@tonic-gate 	}
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 	previous_head = perm_list;
14947c478bd9Sstevel@tonic-gate 	current_head = perm_list;
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 	while (*current_head != '\0') {
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 		for (i = 0; i <= len; i++)
14997c478bd9Sstevel@tonic-gate 			one_entry[i] = 0;
15007c478bd9Sstevel@tonic-gate 
1501f4da9be0Scth 		current_head = get_entry(previous_head, one_entry, ',', 0);
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 		previous_head = current_head;
15047c478bd9Sstevel@tonic-gate 		scan_stat = sscanf(one_entry, "%s%s%s%s%s", minor, perm, own,
15057c478bd9Sstevel@tonic-gate 		    grp, dumb);
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 		if (scan_stat < 4) {
15087c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_MIS_TOK),
15097c478bd9Sstevel@tonic-gate 			    "-m", one_entry);
15107c478bd9Sstevel@tonic-gate 			status = ERROR;
15117c478bd9Sstevel@tonic-gate 		}
15127c478bd9Sstevel@tonic-gate 		if (scan_stat > 4) {
15137c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_TOO_MANY_ARGS),
15147c478bd9Sstevel@tonic-gate 			    "-m", one_entry);
15157c478bd9Sstevel@tonic-gate 			status = ERROR;
15167c478bd9Sstevel@tonic-gate 		}
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 		intperm = atoi(perm);
15197c478bd9Sstevel@tonic-gate 		if (intperm < 0000 || intperm > 4777) {
15207c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_BAD_MODE), perm);
15217c478bd9Sstevel@tonic-gate 			status = ERROR;
15227c478bd9Sstevel@tonic-gate 		}
15237c478bd9Sstevel@tonic-gate 	}
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	free(one_entry);
15267c478bd9Sstevel@tonic-gate 	return (status);
15277c478bd9Sstevel@tonic-gate }
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate /*
15317c478bd9Sstevel@tonic-gate  * check each alias :
15327c478bd9Sstevel@tonic-gate  *	alias list members separated by white space
15337c478bd9Sstevel@tonic-gate  *	cannot exist as driver name in /etc/name_to_major
15347c478bd9Sstevel@tonic-gate  *	cannot exist as driver or alias name in /etc/driver_aliases
15357c478bd9Sstevel@tonic-gate  */
15367c478bd9Sstevel@tonic-gate int
15377c478bd9Sstevel@tonic-gate aliases_unique(char *aliases)
15387c478bd9Sstevel@tonic-gate {
15397c478bd9Sstevel@tonic-gate 	char *current_head;
15407c478bd9Sstevel@tonic-gate 	char *previous_head;
15417c478bd9Sstevel@tonic-gate 	char *one_entry;
1542*6532b960SJerry Gilliam 	int len;
15437c478bd9Sstevel@tonic-gate 	int is_unique;
1544*6532b960SJerry Gilliam 	int err;
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	len = strlen(aliases);
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	one_entry = calloc(len + 1, 1);
15497c478bd9Sstevel@tonic-gate 	if (one_entry == NULL) {
15507c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
15517c478bd9Sstevel@tonic-gate 		return (ERROR);
15527c478bd9Sstevel@tonic-gate 	}
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	previous_head = aliases;
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	do {
1557*6532b960SJerry Gilliam 		bzero(one_entry, len+1);
1558f4da9be0Scth 		current_head = get_entry(previous_head, one_entry, ' ', 1);
15597c478bd9Sstevel@tonic-gate 		previous_head = current_head;
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 		if ((unique_driver_name(one_entry, name_to_major,
1562*6532b960SJerry Gilliam 		    &is_unique)) == ERROR)
1563*6532b960SJerry Gilliam 			goto err_out;
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 		if (is_unique != UNIQUE) {
15667c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_ALIAS_IN_NAM_MAJ),
15677c478bd9Sstevel@tonic-gate 			    one_entry);
1568*6532b960SJerry Gilliam 			goto err_out;
15697c478bd9Sstevel@tonic-gate 		}
15707c478bd9Sstevel@tonic-gate 
1571*6532b960SJerry Gilliam 		if ((err = unique_drv_alias(one_entry)) != UNIQUE) {
1572*6532b960SJerry Gilliam 			if (err == NOT_UNIQUE) {
1573*6532b960SJerry Gilliam 				(void) fprintf(stderr,
1574*6532b960SJerry Gilliam 				    gettext(ERR_ALIAS_IN_USE), one_entry);
1575*6532b960SJerry Gilliam 			}
1576*6532b960SJerry Gilliam 			goto err_out;
15777c478bd9Sstevel@tonic-gate 		}
15787c478bd9Sstevel@tonic-gate 
15791ca93273Seota 		if (!is_token(one_entry)) {
15801ca93273Seota 			(void) fprintf(stderr, gettext(ERR_BAD_TOK),
15811ca93273Seota 			    "-i", one_entry);
1582*6532b960SJerry Gilliam 			goto err_out;
15831ca93273Seota 		}
15841ca93273Seota 
15857c478bd9Sstevel@tonic-gate 	} while (*current_head != '\0');
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	free(one_entry);
15887c478bd9Sstevel@tonic-gate 	return (NOERR);
15897c478bd9Sstevel@tonic-gate 
1590*6532b960SJerry Gilliam err_out:
1591*6532b960SJerry Gilliam 	free(one_entry);
1592*6532b960SJerry Gilliam 	return (ERROR);
1593*6532b960SJerry Gilliam }
1594*6532b960SJerry Gilliam 
1595*6532b960SJerry Gilliam /*
1596*6532b960SJerry Gilliam  * verify each alias :
1597*6532b960SJerry Gilliam  *	alias list members separated by white space and quoted
1598*6532b960SJerry Gilliam  *	exist as alias name in /etc/driver_aliases
1599*6532b960SJerry Gilliam  */
1600*6532b960SJerry Gilliam int
1601*6532b960SJerry Gilliam aliases_exist(char *drvname, char *aliases)
1602*6532b960SJerry Gilliam {
1603*6532b960SJerry Gilliam 	char *current_head;
1604*6532b960SJerry Gilliam 	char *previous_head;
1605*6532b960SJerry Gilliam 	char *one_entry;
1606*6532b960SJerry Gilliam 	int len;
1607*6532b960SJerry Gilliam 	int is_unique;
1608*6532b960SJerry Gilliam 	int err;
1609*6532b960SJerry Gilliam 
1610*6532b960SJerry Gilliam 	len = strlen(aliases);
1611*6532b960SJerry Gilliam 
1612*6532b960SJerry Gilliam 	one_entry = calloc(len + 1, 1);
1613*6532b960SJerry Gilliam 	if (one_entry == NULL) {
1614*6532b960SJerry Gilliam 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
1615*6532b960SJerry Gilliam 		return (ERROR);
1616*6532b960SJerry Gilliam 	}
1617*6532b960SJerry Gilliam 
1618*6532b960SJerry Gilliam 	previous_head = aliases;
1619*6532b960SJerry Gilliam 
1620*6532b960SJerry Gilliam 	do {
1621*6532b960SJerry Gilliam 		bzero(one_entry, len+1);
1622*6532b960SJerry Gilliam 		current_head = get_entry(previous_head, one_entry, ' ', 1);
1623*6532b960SJerry Gilliam 		previous_head = current_head;
1624*6532b960SJerry Gilliam 
1625*6532b960SJerry Gilliam 		if ((err = unique_drv_alias(one_entry)) != NOT_UNIQUE)
1626*6532b960SJerry Gilliam 			goto err_out;
1627*6532b960SJerry Gilliam 
1628*6532b960SJerry Gilliam 		if (!is_token(one_entry)) {
1629*6532b960SJerry Gilliam 			(void) fprintf(stderr, gettext(ERR_BAD_TOK),
1630*6532b960SJerry Gilliam 			    "-i", one_entry);
1631*6532b960SJerry Gilliam 			goto err_out;
1632*6532b960SJerry Gilliam 		}
1633*6532b960SJerry Gilliam 
1634*6532b960SJerry Gilliam 	} while (*current_head != '\0');
1635*6532b960SJerry Gilliam 
1636*6532b960SJerry Gilliam 	free(one_entry);
1637*6532b960SJerry Gilliam 	return (NOERR);
1638*6532b960SJerry Gilliam 
1639*6532b960SJerry Gilliam err_out:
1640*6532b960SJerry Gilliam 	free(one_entry);
1641*6532b960SJerry Gilliam 	return (ERROR);
16427c478bd9Sstevel@tonic-gate }
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 
1645f4da9be0Scth /*
1646f4da9be0Scth  * check each alias :
1647f4da9be0Scth  *	if path-oriented alias, path exists
1648f4da9be0Scth  */
1649f4da9be0Scth int
1650f4da9be0Scth aliases_paths_exist(char *aliases)
1651f4da9be0Scth {
1652f4da9be0Scth 	char *current_head;
1653f4da9be0Scth 	char *previous_head;
1654f4da9be0Scth 	char *one_entry;
1655f4da9be0Scth 	int i, len;
1656f4da9be0Scth 	char path[MAXPATHLEN];
1657f4da9be0Scth 	struct stat buf;
1658f4da9be0Scth 
1659f4da9be0Scth 	len = strlen(aliases);
1660f4da9be0Scth 
1661f4da9be0Scth 	one_entry = calloc(len + 1, 1);
1662f4da9be0Scth 	if (one_entry == NULL) {
1663f4da9be0Scth 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
1664f4da9be0Scth 		return (ERROR);
1665f4da9be0Scth 	}
1666f4da9be0Scth 
1667f4da9be0Scth 	previous_head = aliases;
1668f4da9be0Scth 
1669f4da9be0Scth 	do {
1670f4da9be0Scth 		for (i = 0; i <= len; i++)
1671f4da9be0Scth 			one_entry[i] = 0;
1672f4da9be0Scth 
1673f4da9be0Scth 		current_head = get_entry(previous_head, one_entry, ' ', 1);
1674f4da9be0Scth 		previous_head = current_head;
1675f4da9be0Scth 
1676f4da9be0Scth 		/* if the alias is a path, ensure that the path exists */
1677f4da9be0Scth 		if (*one_entry != '/')
1678f4da9be0Scth 			continue;
1679f4da9be0Scth 		(void) snprintf(path, sizeof (path), "/devices/%s", one_entry);
1680f4da9be0Scth 		if (stat(path, &buf) == 0)
1681f4da9be0Scth 			continue;
1682f4da9be0Scth 
1683f4da9be0Scth 		/* no device at specified path-oriented alias path */
1684f4da9be0Scth 		(void) fprintf(stderr, gettext(ERR_PATH_ORIENTED_ALIAS),
1685f4da9be0Scth 		    one_entry);
1686f4da9be0Scth 		free(one_entry);
1687f4da9be0Scth 		return (ERROR);
1688f4da9be0Scth 
1689f4da9be0Scth 	} while (*current_head != '\0');
1690f4da9be0Scth 
1691f4da9be0Scth 	free(one_entry);
1692f4da9be0Scth 
1693f4da9be0Scth 	return (NOERR);
1694f4da9be0Scth }
1695f4da9be0Scth 
1696f4da9be0Scth 
16977c478bd9Sstevel@tonic-gate int
16987c478bd9Sstevel@tonic-gate update_driver_aliases(
16997c478bd9Sstevel@tonic-gate 	char *driver_name,
17007c478bd9Sstevel@tonic-gate 	char *aliases)
17017c478bd9Sstevel@tonic-gate {
17027c478bd9Sstevel@tonic-gate 	/* make call to update the aliases file */
1703f4da9be0Scth 	return (append_to_file(driver_name, aliases, driver_aliases,
1704f4da9be0Scth 	    ' ', " ", 1));
17057c478bd9Sstevel@tonic-gate }
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 
1708*6532b960SJerry Gilliam /*
1709*6532b960SJerry Gilliam  * Return:
1710*6532b960SJerry Gilliam  *	ERROR in case of memory or read error
1711*6532b960SJerry Gilliam  *	UNIQUE if there is no existing match to the supplied alias
1712*6532b960SJerry Gilliam  *	NOT_UNIQUE if there is a match
1713*6532b960SJerry Gilliam  * An error message is emitted in the case of ERROR,
1714*6532b960SJerry Gilliam  * up to the caller otherwise.
1715*6532b960SJerry Gilliam  */
17167c478bd9Sstevel@tonic-gate int
17177c478bd9Sstevel@tonic-gate unique_drv_alias(char *drv_alias)
17187c478bd9Sstevel@tonic-gate {
17197c478bd9Sstevel@tonic-gate 	FILE *fp;
17207c478bd9Sstevel@tonic-gate 	char drv[FILENAME_MAX + 1];
17211ca93273Seota 	char line[MAX_N2M_ALIAS_LINE + 1], *cp;
17227c478bd9Sstevel@tonic-gate 	char alias[FILENAME_MAX + 1];
1723f4da9be0Scth 	char *a;
1724*6532b960SJerry Gilliam 	int status = UNIQUE;
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 	fp = fopen(driver_aliases, "r");
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 	if (fp != NULL) {
17297c478bd9Sstevel@tonic-gate 		while ((fgets(line, sizeof (line), fp) != 0) &&
1730*6532b960SJerry Gilliam 		    status == UNIQUE) {
17311ca93273Seota 			/* cut off comments starting with '#' */
17321ca93273Seota 			if ((cp = strchr(line, '#')) != NULL)
17331ca93273Seota 				*cp = '\0';
17341ca93273Seota 			/* ignore comment or blank lines */
17351ca93273Seota 			if (is_blank(line))
17361ca93273Seota 				continue;
17371ca93273Seota 			/* sanity-check */
17387c478bd9Sstevel@tonic-gate 			if (sscanf(line, "%s %s", drv, alias) != 2)
17397c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_BAD_LINE),
17407c478bd9Sstevel@tonic-gate 				    driver_aliases, line);
17417c478bd9Sstevel@tonic-gate 
1742f4da9be0Scth 			/* unquote for compare */
1743f4da9be0Scth 			if ((*alias == '"') &&
1744f4da9be0Scth 			    (*(alias + strlen(alias) - 1) == '"')) {
1745f4da9be0Scth 				a = &alias[1];
1746f4da9be0Scth 				alias[strlen(alias) - 1] = '\0';
1747f4da9be0Scth 			} else
1748f4da9be0Scth 				a = alias;
1749f4da9be0Scth 
17507c478bd9Sstevel@tonic-gate 			if ((strcmp(drv_alias, drv) == 0) ||
1751f4da9be0Scth 			    (strcmp(drv_alias, a) == 0)) {
1752*6532b960SJerry Gilliam 				status = NOT_UNIQUE;
1753*6532b960SJerry Gilliam 				break;
17547c478bd9Sstevel@tonic-gate 			}
17557c478bd9Sstevel@tonic-gate 		}
17567c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
17577c478bd9Sstevel@tonic-gate 		return (status);
17587c478bd9Sstevel@tonic-gate 	} else {
17597c478bd9Sstevel@tonic-gate 		perror(NULL);
17607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_OPEN), driver_aliases);
17617c478bd9Sstevel@tonic-gate 		return (ERROR);
17627c478bd9Sstevel@tonic-gate 	}
17637c478bd9Sstevel@tonic-gate }
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate /*
17677c478bd9Sstevel@tonic-gate  * search for driver_name in first field of file file_name
1768*6532b960SJerry Gilliam  * searching name_to_major and driver_aliases: name separated
1769*6532b960SJerry Gilliam  * from the remainder of the line by white space.
17707c478bd9Sstevel@tonic-gate  */
17717c478bd9Sstevel@tonic-gate int
17727c478bd9Sstevel@tonic-gate unique_driver_name(char *driver_name, char *file_name,
17737c478bd9Sstevel@tonic-gate 	int *is_unique)
17747c478bd9Sstevel@tonic-gate {
1775*6532b960SJerry Gilliam 	int ret, err;
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	if ((ret = get_major_no(driver_name, file_name)) == ERROR) {
17787c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE),
17797c478bd9Sstevel@tonic-gate 		    file_name);
17807c478bd9Sstevel@tonic-gate 	} else {
17817c478bd9Sstevel@tonic-gate 		/* check alias file for name collision */
1782*6532b960SJerry Gilliam 		if ((err = unique_drv_alias(driver_name)) != UNIQUE) {
1783*6532b960SJerry Gilliam 			if (err == NOT_UNIQUE) {
1784*6532b960SJerry Gilliam 				(void) fprintf(stderr,
1785*6532b960SJerry Gilliam 				    gettext(ERR_ALIAS_IN_USE),
1786*6532b960SJerry Gilliam 				    driver_name);
1787*6532b960SJerry Gilliam 			}
17887c478bd9Sstevel@tonic-gate 			ret = ERROR;
17897c478bd9Sstevel@tonic-gate 		} else {
17907c478bd9Sstevel@tonic-gate 			if (ret != UNIQUE)
17917c478bd9Sstevel@tonic-gate 				*is_unique = NOT_UNIQUE;
17927c478bd9Sstevel@tonic-gate 			else
17937c478bd9Sstevel@tonic-gate 				*is_unique = ret;
17947c478bd9Sstevel@tonic-gate 			ret = NOERR;
17957c478bd9Sstevel@tonic-gate 		}
17967c478bd9Sstevel@tonic-gate 	}
17977c478bd9Sstevel@tonic-gate 	return (ret);
17987c478bd9Sstevel@tonic-gate }
17997c478bd9Sstevel@tonic-gate 
18000013e2d3SJerry Gilliam /*
18010013e2d3SJerry Gilliam  * returns:
18020013e2d3SJerry Gilliam  *	SUCCESS - not an existing driver alias
18030013e2d3SJerry Gilliam  *	NOT_UNIQUE - matching driver alias exists
18040013e2d3SJerry Gilliam  *	ERROR - an error occurred
18050013e2d3SJerry Gilliam  */
18060013e2d3SJerry Gilliam int
18070013e2d3SJerry Gilliam check_duplicate_driver_alias(char *driver_name, char *drv_alias)
18080013e2d3SJerry Gilliam {
18090013e2d3SJerry Gilliam 	FILE *fp;
18100013e2d3SJerry Gilliam 	char drv[FILENAME_MAX + 1];
18110013e2d3SJerry Gilliam 	char line[MAX_N2M_ALIAS_LINE + 1], *cp;
18120013e2d3SJerry Gilliam 	char alias[FILENAME_MAX + 1];
18130013e2d3SJerry Gilliam 	char *a;
18140013e2d3SJerry Gilliam 	int status = SUCCESS;
18150013e2d3SJerry Gilliam 
18160013e2d3SJerry Gilliam 	if ((fp = fopen(driver_aliases, "r")) == NULL) {
18170013e2d3SJerry Gilliam 		perror(NULL);
18180013e2d3SJerry Gilliam 		(void) fprintf(stderr, gettext(ERR_CANT_OPEN), driver_aliases);
18190013e2d3SJerry Gilliam 		return (ERROR);
18200013e2d3SJerry Gilliam 	}
18210013e2d3SJerry Gilliam 
18220013e2d3SJerry Gilliam 	while (fgets(line, sizeof (line), fp) != 0) {
18230013e2d3SJerry Gilliam 		/* cut off comments starting with '#' */
18240013e2d3SJerry Gilliam 		if ((cp = strchr(line, '#')) != NULL)
18250013e2d3SJerry Gilliam 			*cp = '\0';
18260013e2d3SJerry Gilliam 		/* ignore comment or blank lines */
18270013e2d3SJerry Gilliam 		if (is_blank(line))
18280013e2d3SJerry Gilliam 			continue;
18290013e2d3SJerry Gilliam 		/* sanity-check */
18300013e2d3SJerry Gilliam 		if (sscanf(line, "%s %s", drv, alias) != 2)
18310013e2d3SJerry Gilliam 			(void) fprintf(stderr, gettext(ERR_BAD_LINE),
18320013e2d3SJerry Gilliam 			    driver_aliases, line);
18330013e2d3SJerry Gilliam 
18340013e2d3SJerry Gilliam 		/* unquote for compare */
18350013e2d3SJerry Gilliam 		if ((*alias == '"') &&
18360013e2d3SJerry Gilliam 		    (*(alias + strlen(alias) - 1) == '"')) {
18370013e2d3SJerry Gilliam 			a = &alias[1];
18380013e2d3SJerry Gilliam 			alias[strlen(alias) - 1] = '\0';
18390013e2d3SJerry Gilliam 		} else
18400013e2d3SJerry Gilliam 			a = alias;
18410013e2d3SJerry Gilliam 
18420013e2d3SJerry Gilliam 		if ((strcmp(drv_alias, a) == 0) &&
18430013e2d3SJerry Gilliam 		    (strcmp(drv, driver_name) == 0)) {
18440013e2d3SJerry Gilliam 			status = NOT_UNIQUE;
18450013e2d3SJerry Gilliam 		}
18460013e2d3SJerry Gilliam 
18470013e2d3SJerry Gilliam 		if ((strcmp(drv_alias, drv) == 0) ||
18480013e2d3SJerry Gilliam 		    ((strcmp(drv_alias, a) == 0) &&
18490013e2d3SJerry Gilliam 		    (strcmp(drv, driver_name) != 0))) {
18500013e2d3SJerry Gilliam 			(void) fprintf(stderr,
18510013e2d3SJerry Gilliam 			    gettext(ERR_ALIAS_IN_USE),
18520013e2d3SJerry Gilliam 			    drv_alias);
18530013e2d3SJerry Gilliam 			status = ERROR;
18540013e2d3SJerry Gilliam 			goto done;
18550013e2d3SJerry Gilliam 		}
18560013e2d3SJerry Gilliam 	}
18570013e2d3SJerry Gilliam 
18580013e2d3SJerry Gilliam done:
18590013e2d3SJerry Gilliam 	(void) fclose(fp);
18600013e2d3SJerry Gilliam 	return (status);
18610013e2d3SJerry Gilliam }
18620013e2d3SJerry Gilliam 
18630013e2d3SJerry Gilliam int
18640013e2d3SJerry Gilliam trim_duplicate_aliases(char *driver_name, char *aliases, char **aliases2p)
18650013e2d3SJerry Gilliam {
18660013e2d3SJerry Gilliam 	char *current_head;
18670013e2d3SJerry Gilliam 	char *previous_head;
18680013e2d3SJerry Gilliam 	char *one_entry;
18690013e2d3SJerry Gilliam 	char *aliases2;
18700013e2d3SJerry Gilliam 	int rv, len;
18710013e2d3SJerry Gilliam 	int n = 0;
18720013e2d3SJerry Gilliam 
18730013e2d3SJerry Gilliam 	*aliases2p = NULL;
18740013e2d3SJerry Gilliam 	len = strlen(aliases) + 1;
18750013e2d3SJerry Gilliam 
18760013e2d3SJerry Gilliam 	one_entry = calloc(len, 1);
18770013e2d3SJerry Gilliam 	aliases2 = calloc(len, 1);
18780013e2d3SJerry Gilliam 	if (one_entry == NULL || aliases2 == NULL) {
18790013e2d3SJerry Gilliam 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
18800013e2d3SJerry Gilliam 		return (ERROR);
18810013e2d3SJerry Gilliam 	}
18820013e2d3SJerry Gilliam 
18830013e2d3SJerry Gilliam 	previous_head = aliases;
18840013e2d3SJerry Gilliam 
18850013e2d3SJerry Gilliam 	do {
18860013e2d3SJerry Gilliam 		(void) bzero(one_entry, len);
18870013e2d3SJerry Gilliam 		current_head = get_entry(previous_head, one_entry, ' ', 1);
18880013e2d3SJerry Gilliam 		previous_head = current_head;
18890013e2d3SJerry Gilliam 
18900013e2d3SJerry Gilliam 		rv = check_duplicate_driver_alias(driver_name, one_entry);
18910013e2d3SJerry Gilliam 		switch (rv) {
18920013e2d3SJerry Gilliam 		case SUCCESS:
18930013e2d3SJerry Gilliam 			/* not an existing driver alias: add it */
18940013e2d3SJerry Gilliam 			if (n > 0) {
18950013e2d3SJerry Gilliam 				if (strlcat(aliases2, " ", len) >= len)
18960013e2d3SJerry Gilliam 					goto err;
18970013e2d3SJerry Gilliam 			}
18980013e2d3SJerry Gilliam 			if (strlcat(aliases2, one_entry, len) >= len)
18990013e2d3SJerry Gilliam 				goto err;
19000013e2d3SJerry Gilliam 			n++;
19010013e2d3SJerry Gilliam 			break;
19020013e2d3SJerry Gilliam 		case NOT_UNIQUE:
19030013e2d3SJerry Gilliam 			/* matching driver alias exists: do not add it */
19040013e2d3SJerry Gilliam 			break;
19050013e2d3SJerry Gilliam 		case ERROR:
19060013e2d3SJerry Gilliam 			/* error reading the alias file */
19070013e2d3SJerry Gilliam 			goto err;
19080013e2d3SJerry Gilliam 		default:
19090013e2d3SJerry Gilliam 			goto err;
19100013e2d3SJerry Gilliam 		}
19110013e2d3SJerry Gilliam 
19120013e2d3SJerry Gilliam 		if (!is_token(one_entry)) {
19130013e2d3SJerry Gilliam 			(void) fprintf(stderr, gettext(ERR_BAD_TOK),
19140013e2d3SJerry Gilliam 			    "-i", one_entry);
19150013e2d3SJerry Gilliam 			goto err;
19160013e2d3SJerry Gilliam 		}
19170013e2d3SJerry Gilliam 	} while (*current_head != '\0');
19180013e2d3SJerry Gilliam 
19190013e2d3SJerry Gilliam 	/*
19200013e2d3SJerry Gilliam 	 * If all the aliases listed are already
19210013e2d3SJerry Gilliam 	 * present we actually have none to do.
19220013e2d3SJerry Gilliam 	 */
19230013e2d3SJerry Gilliam 	if (n == 0) {
19240013e2d3SJerry Gilliam 		free(aliases2);
19250013e2d3SJerry Gilliam 	} else {
19260013e2d3SJerry Gilliam 		*aliases2p = aliases2;
19270013e2d3SJerry Gilliam 	}
19280013e2d3SJerry Gilliam 	free(one_entry);
19290013e2d3SJerry Gilliam 	return (NOERR);
19300013e2d3SJerry Gilliam 
19310013e2d3SJerry Gilliam err:
19320013e2d3SJerry Gilliam 	free(aliases2);
19330013e2d3SJerry Gilliam 	free(one_entry);
19340013e2d3SJerry Gilliam 	return (ERROR);
19350013e2d3SJerry Gilliam }
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate int
19387c478bd9Sstevel@tonic-gate check_space_within_quote(char *str)
19397c478bd9Sstevel@tonic-gate {
19407c478bd9Sstevel@tonic-gate 	register int i;
19417c478bd9Sstevel@tonic-gate 	register int len;
19427c478bd9Sstevel@tonic-gate 	int quoted = 0;
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	len = strlen(str);
19457c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++, str++) {
19467c478bd9Sstevel@tonic-gate 		if (*str == '"') {
19477c478bd9Sstevel@tonic-gate 			if (quoted == 0)
19487c478bd9Sstevel@tonic-gate 				quoted++;
19497c478bd9Sstevel@tonic-gate 			else
19507c478bd9Sstevel@tonic-gate 				quoted--;
19517c478bd9Sstevel@tonic-gate 		} else if (*str == ' ' && quoted)
19527c478bd9Sstevel@tonic-gate 			return (ERROR);
19537c478bd9Sstevel@tonic-gate 	}
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	return (0);
19567c478bd9Sstevel@tonic-gate }
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate /*
19607c478bd9Sstevel@tonic-gate  * get major number
19617c478bd9Sstevel@tonic-gate  * write driver_name major_num to name_to_major file
19627c478bd9Sstevel@tonic-gate  * major_num returned in major_num
19637c478bd9Sstevel@tonic-gate  * return success/failure
19647c478bd9Sstevel@tonic-gate  */
19657c478bd9Sstevel@tonic-gate int
19667c478bd9Sstevel@tonic-gate update_name_to_major(char *driver_name, major_t *major_num, int server)
19677c478bd9Sstevel@tonic-gate {
19687c478bd9Sstevel@tonic-gate 	char major[MAX_STR_MAJOR + 1];
19697c478bd9Sstevel@tonic-gate 	struct stat buf;
19707c478bd9Sstevel@tonic-gate 	char *num_list;
19717c478bd9Sstevel@tonic-gate 	char drv_majnum_str[MAX_STR_MAJOR + 1];
19727c478bd9Sstevel@tonic-gate 	int new_maj = -1;
19737c478bd9Sstevel@tonic-gate 	int i, tmp = 0, is_unique, have_rem_n2m = 0;
19747c478bd9Sstevel@tonic-gate 	int max_dev = 0;
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate 	/*
19777c478bd9Sstevel@tonic-gate 	 * if driver_name already in rem_name_to_major
19787c478bd9Sstevel@tonic-gate 	 * 	delete entry from rem_nam_to_major
19797c478bd9Sstevel@tonic-gate 	 *	put entry into name_to_major
19807c478bd9Sstevel@tonic-gate 	 */
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 	if (stat(rem_name_to_major, &buf) == 0) {
19837c478bd9Sstevel@tonic-gate 		have_rem_n2m = 1;
19847c478bd9Sstevel@tonic-gate 	}
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate 	if (have_rem_n2m) {
19877c478bd9Sstevel@tonic-gate 		if ((is_unique = get_major_no(driver_name, rem_name_to_major))
19887c478bd9Sstevel@tonic-gate 		    == ERROR)
19897c478bd9Sstevel@tonic-gate 			return (ERROR);
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 		/*
19927c478bd9Sstevel@tonic-gate 		 * found a match in rem_name_to_major
19937c478bd9Sstevel@tonic-gate 		 */
19947c478bd9Sstevel@tonic-gate 		if (is_unique != UNIQUE) {
19957c478bd9Sstevel@tonic-gate 			char scratch[FILENAME_MAX];
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 			/*
19987c478bd9Sstevel@tonic-gate 			 * If there is a match in /etc/rem_name_to_major then
19997c478bd9Sstevel@tonic-gate 			 * be paranoid: is that major number already in
20007c478bd9Sstevel@tonic-gate 			 * /etc/name_to_major (potentially under another name)?
20017c478bd9Sstevel@tonic-gate 			 */
20027c478bd9Sstevel@tonic-gate 			if (get_driver_name(is_unique, name_to_major,
20037c478bd9Sstevel@tonic-gate 			    scratch) != UNIQUE) {
20047c478bd9Sstevel@tonic-gate 				/*
20057c478bd9Sstevel@tonic-gate 				 * nuke the rem_name_to_major entry-- it
20067c478bd9Sstevel@tonic-gate 				 * isn't helpful.
20077c478bd9Sstevel@tonic-gate 				 */
20087c478bd9Sstevel@tonic-gate 				(void) delete_entry(rem_name_to_major,
20097c478bd9Sstevel@tonic-gate 				    driver_name, " ", NULL);
20107c478bd9Sstevel@tonic-gate 			} else {
20117c478bd9Sstevel@tonic-gate 				(void) snprintf(major, sizeof (major),
20127c478bd9Sstevel@tonic-gate 				    "%d", is_unique);
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate 				if (append_to_file(driver_name, major,
2015f4da9be0Scth 				    name_to_major, ' ', " ", 0) == ERROR) {
20167c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
20177c478bd9Sstevel@tonic-gate 					    gettext(ERR_NO_UPDATE),
20187c478bd9Sstevel@tonic-gate 					    name_to_major);
20197c478bd9Sstevel@tonic-gate 					return (ERROR);
20207c478bd9Sstevel@tonic-gate 				}
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 				if (delete_entry(rem_name_to_major,
20237c478bd9Sstevel@tonic-gate 				    driver_name, " ", NULL) == ERROR) {
20247c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
20257c478bd9Sstevel@tonic-gate 					    gettext(ERR_DEL_ENTRY), driver_name,
20267c478bd9Sstevel@tonic-gate 					    rem_name_to_major);
20277c478bd9Sstevel@tonic-gate 					return (ERROR);
20287c478bd9Sstevel@tonic-gate 				}
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 				/* found matching entry : no errors */
20317c478bd9Sstevel@tonic-gate 				*major_num = is_unique;
20327c478bd9Sstevel@tonic-gate 				return (NOERR);
20337c478bd9Sstevel@tonic-gate 			}
20347c478bd9Sstevel@tonic-gate 		}
20357c478bd9Sstevel@tonic-gate 	}
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	/*
20387c478bd9Sstevel@tonic-gate 	 * Bugid: 1264079
20397c478bd9Sstevel@tonic-gate 	 * In a server case (with -b option), we can't use modctl() to find
20407c478bd9Sstevel@tonic-gate 	 *    the maximum major number, we need to dig thru client's
20417c478bd9Sstevel@tonic-gate 	 *    /etc/name_to_major and /etc/rem_name_to_major for the max_dev.
20427c478bd9Sstevel@tonic-gate 	 *
20437c478bd9Sstevel@tonic-gate 	 * if (server)
20447c478bd9Sstevel@tonic-gate 	 *    get maximum major number thru (rem_)name_to_major file on client
20457c478bd9Sstevel@tonic-gate 	 * else
20467c478bd9Sstevel@tonic-gate 	 *    get maximum major number allowable on current system using modctl
20477c478bd9Sstevel@tonic-gate 	 */
20487c478bd9Sstevel@tonic-gate 	if (server) {
20497c478bd9Sstevel@tonic-gate 		max_dev = 0;
20507c478bd9Sstevel@tonic-gate 		tmp = 0;
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 		max_dev = get_max_major(name_to_major);
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 		/* If rem_name_to_major exists, we need to check it too */
20557c478bd9Sstevel@tonic-gate 		if (have_rem_n2m) {
20567c478bd9Sstevel@tonic-gate 			tmp = get_max_major(rem_name_to_major);
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 			/*
20597c478bd9Sstevel@tonic-gate 			 * If name_to_major is missing, we can get max_dev from
20607c478bd9Sstevel@tonic-gate 			 * /etc/rem_name_to_major.  If both missing, bail out!
20617c478bd9Sstevel@tonic-gate 			 */
20627c478bd9Sstevel@tonic-gate 			if ((max_dev == ERROR) && (tmp == ERROR)) {
20637c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
20647c478bd9Sstevel@tonic-gate 				    gettext(ERR_CANT_ACCESS_FILE),
20657c478bd9Sstevel@tonic-gate 				    name_to_major);
20667c478bd9Sstevel@tonic-gate 				return (ERROR);
20677c478bd9Sstevel@tonic-gate 			}
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 			/* guard against bigger maj_num in rem_name_to_major */
20707c478bd9Sstevel@tonic-gate 			if (tmp > max_dev)
20717c478bd9Sstevel@tonic-gate 				max_dev = tmp;
20727c478bd9Sstevel@tonic-gate 		} else {
20737c478bd9Sstevel@tonic-gate 			/*
20747c478bd9Sstevel@tonic-gate 			 * If we can't get major from name_to_major file
20757c478bd9Sstevel@tonic-gate 			 * and there is no /etc/rem_name_to_major file,
20767c478bd9Sstevel@tonic-gate 			 * then we don't have a max_dev, bail out quick!
20777c478bd9Sstevel@tonic-gate 			 */
20787c478bd9Sstevel@tonic-gate 			if (max_dev == ERROR)
20797c478bd9Sstevel@tonic-gate 				return (ERROR);
20807c478bd9Sstevel@tonic-gate 		}
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 		/*
20837c478bd9Sstevel@tonic-gate 		 * In case there is no more slack in current name_to_major
20847c478bd9Sstevel@tonic-gate 		 * table, provide at least 1 extra entry so the add_drv can
20857c478bd9Sstevel@tonic-gate 		 * succeed.  Since only one add_drv process is allowed at one
20867c478bd9Sstevel@tonic-gate 		 * time, and hence max_dev will be re-calculated each time
20877c478bd9Sstevel@tonic-gate 		 * add_drv is ran, we don't need to worry about adding more
20887c478bd9Sstevel@tonic-gate 		 * than 1 extra slot for max_dev.
20897c478bd9Sstevel@tonic-gate 		 */
20907c478bd9Sstevel@tonic-gate 		max_dev++;
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate 	} else {
20937c478bd9Sstevel@tonic-gate 		if (modctl(MODRESERVED, NULL, &max_dev) < 0) {
20947c478bd9Sstevel@tonic-gate 			perror(NULL);
20957c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_MAX_MAJOR));
20967c478bd9Sstevel@tonic-gate 			return (ERROR);
20977c478bd9Sstevel@tonic-gate 		}
20987c478bd9Sstevel@tonic-gate 	}
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 	/*
21017c478bd9Sstevel@tonic-gate 	 * max_dev is really how many slots the kernel has allocated for
21027c478bd9Sstevel@tonic-gate 	 * devices... [0 , maxdev-1], not the largest available device num.
21037c478bd9Sstevel@tonic-gate 	 */
21047c478bd9Sstevel@tonic-gate 	if ((num_list = calloc(max_dev, 1)) == NULL) {
21057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_MEM));
21067c478bd9Sstevel@tonic-gate 		return (ERROR);
21077c478bd9Sstevel@tonic-gate 	}
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 	/*
21107c478bd9Sstevel@tonic-gate 	 * Populate the num_list array
21117c478bd9Sstevel@tonic-gate 	 */
21127c478bd9Sstevel@tonic-gate 	if (fill_n2m_array(name_to_major, &num_list, &max_dev) != 0) {
21137c478bd9Sstevel@tonic-gate 		return (ERROR);
21147c478bd9Sstevel@tonic-gate 	}
21157c478bd9Sstevel@tonic-gate 	if (have_rem_n2m) {
21167c478bd9Sstevel@tonic-gate 		if (fill_n2m_array(rem_name_to_major, &num_list, &max_dev) != 0)
21177c478bd9Sstevel@tonic-gate 			return (ERROR);
21187c478bd9Sstevel@tonic-gate 	}
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 	/* find first free major number */
21217c478bd9Sstevel@tonic-gate 	for (i = 0; i < max_dev; i++) {
21227c478bd9Sstevel@tonic-gate 		if (num_list[i] != 1) {
21237c478bd9Sstevel@tonic-gate 			new_maj = i;
21247c478bd9Sstevel@tonic-gate 			break;
21257c478bd9Sstevel@tonic-gate 		}
21267c478bd9Sstevel@tonic-gate 	}
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 	if (new_maj == -1) {
21297c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_NO_FREE_MAJOR));
21307c478bd9Sstevel@tonic-gate 		return (ERROR);
21317c478bd9Sstevel@tonic-gate 	}
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate 	(void) sprintf(drv_majnum_str, "%d", new_maj);
21347c478bd9Sstevel@tonic-gate 	if (do_the_update(driver_name, drv_majnum_str) == ERROR) {
21357c478bd9Sstevel@tonic-gate 		return (ERROR);
21367c478bd9Sstevel@tonic-gate 	}
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 	*major_num = new_maj;
21397c478bd9Sstevel@tonic-gate 	return (NOERR);
21407c478bd9Sstevel@tonic-gate }
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate int
21447c478bd9Sstevel@tonic-gate fill_n2m_array(char *filename, char **array, int *nelems)
21457c478bd9Sstevel@tonic-gate {
21467c478bd9Sstevel@tonic-gate 	FILE *fp;
21471ca93273Seota 	char line[MAX_N2M_ALIAS_LINE + 1], *cp;
21487c478bd9Sstevel@tonic-gate 	char drv[FILENAME_MAX + 1];
21497c478bd9Sstevel@tonic-gate 	u_longlong_t dnum;
21507c478bd9Sstevel@tonic-gate 	major_t drv_majnum;
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 	/*
21537c478bd9Sstevel@tonic-gate 	 * Read through the file, marking each major number found
21547c478bd9Sstevel@tonic-gate 	 * order is not relevant
21557c478bd9Sstevel@tonic-gate 	 */
21567c478bd9Sstevel@tonic-gate 	if ((fp = fopen(filename, "r")) == NULL) {
21577c478bd9Sstevel@tonic-gate 		perror(NULL);
21587c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), filename);
21597c478bd9Sstevel@tonic-gate 		return (ERROR);
21607c478bd9Sstevel@tonic-gate 	}
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line), fp) != 0) {
21631ca93273Seota 		/* cut off comments starting with '#' */
21641ca93273Seota 		if ((cp = strchr(line, '#')) != NULL)
21651ca93273Seota 			*cp = '\0';
21661ca93273Seota 		/* ignore comment or blank lines */
21671ca93273Seota 		if (is_blank(line))
21681ca93273Seota 			continue;
21691ca93273Seota 		/* sanity-check */
21707c478bd9Sstevel@tonic-gate 		if (sscanf(line, "%s %llu", drv, &dnum) != 2) {
21717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_BAD_LINE),
21727c478bd9Sstevel@tonic-gate 			    filename, line);
21737c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
21747c478bd9Sstevel@tonic-gate 			return (ERROR);
21757c478bd9Sstevel@tonic-gate 		}
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 		if (dnum > L_MAXMAJ32) {
21787c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(ERR_MAJ_TOOBIG), drv,
21797c478bd9Sstevel@tonic-gate 			    dnum, filename, L_MAXMAJ32);
21807c478bd9Sstevel@tonic-gate 			continue;
21817c478bd9Sstevel@tonic-gate 		}
21827c478bd9Sstevel@tonic-gate 		/*
21837c478bd9Sstevel@tonic-gate 		 * cast down to a major_t; we can be sure this is safe because
21847c478bd9Sstevel@tonic-gate 		 * of the above range-check.
21857c478bd9Sstevel@tonic-gate 		 */
21867c478bd9Sstevel@tonic-gate 		drv_majnum = (major_t)dnum;
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 		if (drv_majnum >= *nelems) {
21897c478bd9Sstevel@tonic-gate 			/*
21907c478bd9Sstevel@tonic-gate 			 * Allocate some more space, up to drv_majnum + 1 so
21917c478bd9Sstevel@tonic-gate 			 * we can accomodate 0 through drv_majnum.
21927c478bd9Sstevel@tonic-gate 			 *
21937c478bd9Sstevel@tonic-gate 			 * Note that in the failure case, we leak all of the
21947c478bd9Sstevel@tonic-gate 			 * old contents of array.  It's ok, since we just
21957c478bd9Sstevel@tonic-gate 			 * wind up exiting immediately anyway.
21967c478bd9Sstevel@tonic-gate 			 */
21977c478bd9Sstevel@tonic-gate 			*nelems = drv_majnum + 1;
21987c478bd9Sstevel@tonic-gate 			*array = realloc(*array, *nelems);
21997c478bd9Sstevel@tonic-gate 			if (*array == NULL) {
22007c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(ERR_NO_MEM));
22017c478bd9Sstevel@tonic-gate 				return (ERROR);
22027c478bd9Sstevel@tonic-gate 			}
22037c478bd9Sstevel@tonic-gate 		}
22047c478bd9Sstevel@tonic-gate 		(*array)[drv_majnum] = 1;
22057c478bd9Sstevel@tonic-gate 	}
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
22087c478bd9Sstevel@tonic-gate 	return (0);
22097c478bd9Sstevel@tonic-gate }
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate int
22137c478bd9Sstevel@tonic-gate do_the_update(char *driver_name, char *major_number)
22147c478bd9Sstevel@tonic-gate {
22157c478bd9Sstevel@tonic-gate 	return (append_to_file(driver_name, major_number, name_to_major,
2216f4da9be0Scth 	    ' ', " ", 0));
22177c478bd9Sstevel@tonic-gate }
22181ca93273Seota 
22191ca93273Seota /*
22201ca93273Seota  * is_blank() returns 1 (true) if a line specified is composed of
22211ca93273Seota  * whitespace characters only. otherwise, it returns 0 (false).
22221ca93273Seota  *
22231ca93273Seota  * Note. the argument (line) must be null-terminated.
22241ca93273Seota  */
22251ca93273Seota static int
22261ca93273Seota is_blank(char *line)
22271ca93273Seota {
22281ca93273Seota 	for (/* nothing */; *line != '\0'; line++)
22291ca93273Seota 		if (!isspace(*line))
22301ca93273Seota 			return (0);
22311ca93273Seota 	return (1);
22321ca93273Seota }
2233