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 5*1ca93273Seota * Common Development and Distribution License (the "License"). 6*1ca93273Seota * 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*1ca93273Seota * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <stdio.h> 297c478bd9Sstevel@tonic-gate #include <stdlib.h> 30*1ca93273Seota #include <ctype.h> 317c478bd9Sstevel@tonic-gate #include <unistd.h> 327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 337c478bd9Sstevel@tonic-gate #include <libintl.h> 347c478bd9Sstevel@tonic-gate #include <wait.h> 357c478bd9Sstevel@tonic-gate #include <string.h> 367c478bd9Sstevel@tonic-gate #include <errno.h> 377c478bd9Sstevel@tonic-gate #include <fcntl.h> 387c478bd9Sstevel@tonic-gate #include <signal.h> 397c478bd9Sstevel@tonic-gate #include <sys/buf.h> 407c478bd9Sstevel@tonic-gate #include <sys/stat.h> 417c478bd9Sstevel@tonic-gate #include <grp.h> 427c478bd9Sstevel@tonic-gate #include "addrem.h" 437c478bd9Sstevel@tonic-gate #include "errmsg.h" 447c478bd9Sstevel@tonic-gate #include "plcysubr.h" 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate static char *add_rem_lock; /* lock file */ 477c478bd9Sstevel@tonic-gate static char *tmphold; /* temperary file for updating */ 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 53*1ca93273Seota static int is_blank(char *); 54*1ca93273Seota 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, 1087c478bd9Sstevel@tonic-gate char *entry_separator) 1097c478bd9Sstevel@tonic-gate { 1107c478bd9Sstevel@tonic-gate int i, len; 1117c478bd9Sstevel@tonic-gate int fpint; 1127c478bd9Sstevel@tonic-gate char *current_head, *previous_head; 1137c478bd9Sstevel@tonic-gate char *line, *one_entry; 1147c478bd9Sstevel@tonic-gate FILE *fp; 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate if ((fp = fopen(filename, "a")) == NULL) { 1177c478bd9Sstevel@tonic-gate perror(NULL); 1187c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), 1197c478bd9Sstevel@tonic-gate filename); 1207c478bd9Sstevel@tonic-gate return (ERROR); 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate len = strlen(entry_list); 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate one_entry = calloc(len + 1, 1); 1267c478bd9Sstevel@tonic-gate if (one_entry == NULL) { 1277c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_UPDATE), filename); 1287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_MEM)); 1297c478bd9Sstevel@tonic-gate (void) fclose(fp); 1307c478bd9Sstevel@tonic-gate return (ERROR); 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate previous_head = entry_list; 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate line = calloc(strlen(driver_name) + len + 4, 1); 1367c478bd9Sstevel@tonic-gate if (line == NULL) { 1377c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_MEM)); 1387c478bd9Sstevel@tonic-gate (void) fclose(fp); 1397c478bd9Sstevel@tonic-gate err_exit(); 1407c478bd9Sstevel@tonic-gate } 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate /* 1437c478bd9Sstevel@tonic-gate * get one entry at a time from list and append to <filename> file 1447c478bd9Sstevel@tonic-gate */ 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate do { 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate for (i = 0; i <= len; i++) 1497c478bd9Sstevel@tonic-gate one_entry[i] = 0; 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate for (i = 0; i <= (int)strlen(line); i++) 1527c478bd9Sstevel@tonic-gate line[i] = 0; 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate current_head = get_entry(previous_head, one_entry, 1557c478bd9Sstevel@tonic-gate list_separator); 1567c478bd9Sstevel@tonic-gate previous_head = current_head; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate (void) strcpy(line, driver_name); 1597c478bd9Sstevel@tonic-gate (void) strcat(line, entry_separator); 1607c478bd9Sstevel@tonic-gate (void) strcat(line, one_entry); 1617c478bd9Sstevel@tonic-gate (void) strcat(line, "\n"); 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate if ((fputs(line, fp)) == EOF) { 1647c478bd9Sstevel@tonic-gate perror(NULL); 1657c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_UPDATE), 1667c478bd9Sstevel@tonic-gate filename); 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate } while (*current_head != '\0'); 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate (void) fflush(fp); 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate fpint = fileno(fp); 1757c478bd9Sstevel@tonic-gate (void) fsync(fpint); 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate (void) fclose(fp); 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate free(one_entry); 1807c478bd9Sstevel@tonic-gate free(line); 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate return (NOERR); 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate /* 1877c478bd9Sstevel@tonic-gate * open file 1887c478bd9Sstevel@tonic-gate * read thru file, deleting all entries if first 1897c478bd9Sstevel@tonic-gate * entry = driver_name 1907c478bd9Sstevel@tonic-gate * close 1917c478bd9Sstevel@tonic-gate * if error, leave original file intact with message 1927c478bd9Sstevel@tonic-gate * assumption : drvconfig has been modified to work with clone 1937c478bd9Sstevel@tonic-gate * entries in /etc/minor_perm as driver:mummble NOT 1947c478bd9Sstevel@tonic-gate * clone:driver mummble 1957c478bd9Sstevel@tonic-gate * this implementation will NOT find clone entries 1967c478bd9Sstevel@tonic-gate * clone:driver mummble 1977c478bd9Sstevel@tonic-gate * match: 1987c478bd9Sstevel@tonic-gate * delete just the matching entry 1997c478bd9Sstevel@tonic-gate * 2007c478bd9Sstevel@tonic-gate */ 2017c478bd9Sstevel@tonic-gate int 2027c478bd9Sstevel@tonic-gate delete_entry( 2037c478bd9Sstevel@tonic-gate char *oldfile, 2047c478bd9Sstevel@tonic-gate char *driver_name, 2057c478bd9Sstevel@tonic-gate char *marker, 2067c478bd9Sstevel@tonic-gate char *match) 2077c478bd9Sstevel@tonic-gate { 2087c478bd9Sstevel@tonic-gate int rv, i; 2097c478bd9Sstevel@tonic-gate int status = NOERR; 2107c478bd9Sstevel@tonic-gate int drvr_found = 0; 2117c478bd9Sstevel@tonic-gate boolean_t nomatch = B_TRUE; 212*1ca93273Seota char *newfile, *tptr, *cp, *dup; 2137c478bd9Sstevel@tonic-gate char line[MAX_DBFILE_ENTRY], drv[FILENAME_MAX + 1]; 2147c478bd9Sstevel@tonic-gate FILE *fp, *newfp; 2157c478bd9Sstevel@tonic-gate struct group *sysgrp; 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate /* 2187c478bd9Sstevel@tonic-gate * check if match is specified and if it equals " " 2197c478bd9Sstevel@tonic-gate * this is a special case handling as we do a strstr(3STRING) 2207c478bd9Sstevel@tonic-gate * to match an entry. By default all entries are space separated 2217c478bd9Sstevel@tonic-gate * and without this check all entries of the file could get deleted. 2227c478bd9Sstevel@tonic-gate */ 2237c478bd9Sstevel@tonic-gate if (match && (*match == ' ' && strlen(match) == 1)) { 2247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_INT_UPDATE), oldfile); 2257c478bd9Sstevel@tonic-gate return (ERROR); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate if ((fp = fopen(oldfile, "r")) == NULL) { 2297c478bd9Sstevel@tonic-gate perror(NULL); 2307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), oldfile); 2317c478bd9Sstevel@tonic-gate return (ERROR); 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate /* 2357c478bd9Sstevel@tonic-gate * Build filename for temporary file 2367c478bd9Sstevel@tonic-gate */ 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate if ((tptr = calloc(strlen(oldfile) + strlen(XEND) + 1, 1)) == NULL) { 2397c478bd9Sstevel@tonic-gate perror(NULL); 2407c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_MEM)); 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate (void) strcpy(tptr, oldfile); 2447c478bd9Sstevel@tonic-gate (void) strcat(tptr, XEND); 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate /* 2477c478bd9Sstevel@tonic-gate * Set gid so we preserve group attribute. Ideally we wouldn't 2487c478bd9Sstevel@tonic-gate * assume a gid of "sys" but we can't undo the damage on already 2497c478bd9Sstevel@tonic-gate * installed systems unless we force the issue. 2507c478bd9Sstevel@tonic-gate */ 2517c478bd9Sstevel@tonic-gate if ((sysgrp = getgrnam("sys")) != NULL) { 2527c478bd9Sstevel@tonic-gate (void) setgid(sysgrp->gr_gid); 2537c478bd9Sstevel@tonic-gate } 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate newfile = mktemp(tptr); 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate if ((newfp = fopen(newfile, "w")) == NULL) { 2587c478bd9Sstevel@tonic-gate perror(NULL); 2597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), 2607c478bd9Sstevel@tonic-gate newfile); 2617c478bd9Sstevel@tonic-gate return (ERROR); 2627c478bd9Sstevel@tonic-gate } 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate while ((fgets(line, sizeof (line), fp) != NULL) && status == NOERR) { 265*1ca93273Seota /* copy the whole line into dup */ 266*1ca93273Seota if ((dup = strdup(line)) == NULL) { 267*1ca93273Seota perror(NULL); 268*1ca93273Seota (void) fprintf(stderr, gettext(ERR_NO_MEM)); 269*1ca93273Seota status = ERROR; 270*1ca93273Seota break; 271*1ca93273Seota } 272*1ca93273Seota /* cut off comments starting with '#' */ 273*1ca93273Seota if ((cp = strchr(dup, '#')) != NULL) 274*1ca93273Seota *cp = '\0'; 275*1ca93273Seota /* ignore comment or blank lines */ 276*1ca93273Seota if (is_blank(dup)) { 277*1ca93273Seota if (fputs(line, newfp) == EOF) { 2787c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_UPDATE), 2797c478bd9Sstevel@tonic-gate oldfile); 2807c478bd9Sstevel@tonic-gate status = ERROR; 2817c478bd9Sstevel@tonic-gate } 282*1ca93273Seota free(dup); 2837c478bd9Sstevel@tonic-gate continue; 2847c478bd9Sstevel@tonic-gate } 285*1ca93273Seota 286*1ca93273Seota /* get the driver name */ 287*1ca93273Seota if (sscanf(dup, "%s", drv) != 1) { 2887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_BAD_LINE), 2897c478bd9Sstevel@tonic-gate oldfile, line); 2907c478bd9Sstevel@tonic-gate status = ERROR; 291*1ca93273Seota free(dup); 292*1ca93273Seota break; 2937c478bd9Sstevel@tonic-gate } 294*1ca93273Seota free(dup); 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate for (i = strcspn(drv, marker); i < FILENAME_MAX; i++) { 2977c478bd9Sstevel@tonic-gate drv[i] = '\0'; 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate if (strcmp(driver_name, drv) != 0) { 3017c478bd9Sstevel@tonic-gate if ((fputs(line, newfp)) == EOF) { 3027c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_UPDATE), 3037c478bd9Sstevel@tonic-gate oldfile); 3047c478bd9Sstevel@tonic-gate status = ERROR; 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate } else { 3077c478bd9Sstevel@tonic-gate drvr_found++; 3087c478bd9Sstevel@tonic-gate if (match) { /* Just delete one entry */ 3097c478bd9Sstevel@tonic-gate /* for now delete just minor_perm and aliases */ 3107c478bd9Sstevel@tonic-gate if ((strcmp(oldfile, minor_perm) == 0) || 3117c478bd9Sstevel@tonic-gate (strcmp(oldfile, extra_privs) == 0) || 3127c478bd9Sstevel@tonic-gate (strcmp(oldfile, driver_aliases) == 0)) { 3137c478bd9Sstevel@tonic-gate if (strstr(line, match)) { 3147c478bd9Sstevel@tonic-gate nomatch = B_FALSE; 3157c478bd9Sstevel@tonic-gate } else { 3167c478bd9Sstevel@tonic-gate if ((fputs(line, newfp)) == 3177c478bd9Sstevel@tonic-gate EOF) { 3187c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3197c478bd9Sstevel@tonic-gate gettext(ERR_UPDATE), 3207c478bd9Sstevel@tonic-gate oldfile); 3217c478bd9Sstevel@tonic-gate status = ERROR; 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate if (nomatch != B_FALSE) 3247c478bd9Sstevel@tonic-gate nomatch = B_TRUE; 3257c478bd9Sstevel@tonic-gate } 3267c478bd9Sstevel@tonic-gate } 3277c478bd9Sstevel@tonic-gate } 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate } /* end of else */ 3307c478bd9Sstevel@tonic-gate } /* end of while */ 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate (void) fclose(fp); 3337c478bd9Sstevel@tonic-gate 3347c478bd9Sstevel@tonic-gate /* Make sure that the file is on disk */ 3357c478bd9Sstevel@tonic-gate if (fflush(newfp) != 0 || fsync(fileno(newfp)) != 0) 3367c478bd9Sstevel@tonic-gate status = ERROR; 3377c478bd9Sstevel@tonic-gate else 3387c478bd9Sstevel@tonic-gate rv = NOERR; 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate (void) fclose(newfp); 3417c478bd9Sstevel@tonic-gate 3427c478bd9Sstevel@tonic-gate /* no matching driver found */ 3437c478bd9Sstevel@tonic-gate rv = NOERR; 3447c478bd9Sstevel@tonic-gate if (!drvr_found || 3457c478bd9Sstevel@tonic-gate (nomatch == B_TRUE)) { 3467c478bd9Sstevel@tonic-gate rv = NONE_FOUND; 3477c478bd9Sstevel@tonic-gate } 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate /* 3507c478bd9Sstevel@tonic-gate * if error, leave original file, delete new file 3517c478bd9Sstevel@tonic-gate * if noerr, replace original file with new file 3527c478bd9Sstevel@tonic-gate */ 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate if (status == NOERR) { 3557c478bd9Sstevel@tonic-gate if (rename(oldfile, tmphold) == -1) { 3567c478bd9Sstevel@tonic-gate perror(NULL); 3577c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_UPDATE), oldfile); 3587c478bd9Sstevel@tonic-gate (void) unlink(newfile); 3597c478bd9Sstevel@tonic-gate return (ERROR); 3607c478bd9Sstevel@tonic-gate } else if (rename(newfile, oldfile) == -1) { 3617c478bd9Sstevel@tonic-gate perror(NULL); 3627c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_UPDATE), oldfile); 3637c478bd9Sstevel@tonic-gate (void) unlink(oldfile); 3647c478bd9Sstevel@tonic-gate (void) unlink(newfile); 3657c478bd9Sstevel@tonic-gate if (link(tmphold, oldfile) == -1) { 3667c478bd9Sstevel@tonic-gate perror(NULL); 3677c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_BAD_LINK), 3687c478bd9Sstevel@tonic-gate oldfile, tmphold); 3697c478bd9Sstevel@tonic-gate } 3707c478bd9Sstevel@tonic-gate return (ERROR); 3717c478bd9Sstevel@tonic-gate } 3727c478bd9Sstevel@tonic-gate (void) unlink(tmphold); 3737c478bd9Sstevel@tonic-gate } else { 3747c478bd9Sstevel@tonic-gate /* 3757c478bd9Sstevel@tonic-gate * since there's an error, leave file alone; remove 3767c478bd9Sstevel@tonic-gate * new file 3777c478bd9Sstevel@tonic-gate */ 3787c478bd9Sstevel@tonic-gate if (unlink(newfile) == -1) { 3797c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_CANT_RM), newfile); 3807c478bd9Sstevel@tonic-gate } 3817c478bd9Sstevel@tonic-gate return (ERROR); 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate return (rv); 3857c478bd9Sstevel@tonic-gate } 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate /* 3897c478bd9Sstevel@tonic-gate * wrapper for call to get_name_to_major_entry(): given driver name, 3907c478bd9Sstevel@tonic-gate * retrieve major number. 3917c478bd9Sstevel@tonic-gate */ 3927c478bd9Sstevel@tonic-gate int 3937c478bd9Sstevel@tonic-gate get_major_no(char *driver_name, char *file_name) 3947c478bd9Sstevel@tonic-gate { 3957c478bd9Sstevel@tonic-gate int major = UNIQUE; 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate if (get_name_to_major_entry(&major, driver_name, file_name) == ERROR) 3987c478bd9Sstevel@tonic-gate return (ERROR); 3997c478bd9Sstevel@tonic-gate else 4007c478bd9Sstevel@tonic-gate return (major); 4017c478bd9Sstevel@tonic-gate } 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate /* 4047c478bd9Sstevel@tonic-gate * wrapper for call to get_name_to_major_entry(): given major number, 4057c478bd9Sstevel@tonic-gate * retrieve driver name. 4067c478bd9Sstevel@tonic-gate */ 4077c478bd9Sstevel@tonic-gate int 4087c478bd9Sstevel@tonic-gate get_driver_name(int major, char *file_name, char *buf) 4097c478bd9Sstevel@tonic-gate { 4107c478bd9Sstevel@tonic-gate if (major < 0) 4117c478bd9Sstevel@tonic-gate return (ERROR); 4127c478bd9Sstevel@tonic-gate return (get_name_to_major_entry(&major, buf, file_name)); 4137c478bd9Sstevel@tonic-gate } 4147c478bd9Sstevel@tonic-gate 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate /* 4177c478bd9Sstevel@tonic-gate * return pointer to cached name_to_major file - reads file into 4187c478bd9Sstevel@tonic-gate * cache if this has not already been done. Since there may be 4197c478bd9Sstevel@tonic-gate * requests for multiple name_to_major files (rem_name_to_major, 4207c478bd9Sstevel@tonic-gate * name_to_major), this routine keeps a list of cached files. 4217c478bd9Sstevel@tonic-gate */ 4227c478bd9Sstevel@tonic-gate static int 4237c478bd9Sstevel@tonic-gate get_cached_n_to_m_file(char *filename, char ***cache) 4247c478bd9Sstevel@tonic-gate { 4257c478bd9Sstevel@tonic-gate struct n_to_m_cache { 4267c478bd9Sstevel@tonic-gate char *file; 4277c478bd9Sstevel@tonic-gate char **cached_file; 4287c478bd9Sstevel@tonic-gate int size; 4297c478bd9Sstevel@tonic-gate struct n_to_m_cache *next; 4307c478bd9Sstevel@tonic-gate }; 4317c478bd9Sstevel@tonic-gate static struct n_to_m_cache *head = NULL; 4327c478bd9Sstevel@tonic-gate struct n_to_m_cache *ptr; 4337c478bd9Sstevel@tonic-gate FILE *fp; 4347c478bd9Sstevel@tonic-gate char drv[FILENAME_MAX + 1]; 4357c478bd9Sstevel@tonic-gate char entry[FILENAME_MAX + 1]; 436*1ca93273Seota char line[MAX_N2M_ALIAS_LINE], *cp; 4377c478bd9Sstevel@tonic-gate int maj; 4387c478bd9Sstevel@tonic-gate int size = 0; 4397c478bd9Sstevel@tonic-gate 4407c478bd9Sstevel@tonic-gate 4417c478bd9Sstevel@tonic-gate /* 4427c478bd9Sstevel@tonic-gate * see if the file is already cached - either 4437c478bd9Sstevel@tonic-gate * rem_name_to_major or name_to_major 4447c478bd9Sstevel@tonic-gate */ 4457c478bd9Sstevel@tonic-gate ptr = head; 4467c478bd9Sstevel@tonic-gate while (ptr != NULL) { 4477c478bd9Sstevel@tonic-gate if (strcmp(ptr->file, filename) == 0) 4487c478bd9Sstevel@tonic-gate break; 4497c478bd9Sstevel@tonic-gate ptr = ptr->next; 4507c478bd9Sstevel@tonic-gate } 4517c478bd9Sstevel@tonic-gate 4527c478bd9Sstevel@tonic-gate if (ptr == NULL) { /* we need to cache the contents */ 4537c478bd9Sstevel@tonic-gate if ((fp = fopen(filename, "r")) == NULL) { 4547c478bd9Sstevel@tonic-gate perror(NULL); 4557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_CANT_OPEN), 4567c478bd9Sstevel@tonic-gate filename); 4577c478bd9Sstevel@tonic-gate return (ERROR); 4587c478bd9Sstevel@tonic-gate } 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line), fp) != NULL) { 461*1ca93273Seota /* cut off comments starting with '#' */ 462*1ca93273Seota if ((cp = strchr(line, '#')) != NULL) 463*1ca93273Seota *cp = '\0'; 464*1ca93273Seota /* ignore comment or blank lines */ 465*1ca93273Seota if (is_blank(line)) 466*1ca93273Seota continue; 467*1ca93273Seota /* sanity-check */ 4687c478bd9Sstevel@tonic-gate if (sscanf(line, "%s%s", drv, entry) != 2) { 4697c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_BAD_LINE), 4707c478bd9Sstevel@tonic-gate filename, line); 4717c478bd9Sstevel@tonic-gate continue; 4727c478bd9Sstevel@tonic-gate } 4737c478bd9Sstevel@tonic-gate maj = atoi(entry); 4747c478bd9Sstevel@tonic-gate if (maj > size) 4757c478bd9Sstevel@tonic-gate size = maj; 4767c478bd9Sstevel@tonic-gate } 4777c478bd9Sstevel@tonic-gate 4787c478bd9Sstevel@tonic-gate /* allocate struct to cache the file */ 4797c478bd9Sstevel@tonic-gate ptr = (struct n_to_m_cache *)calloc(1, 4807c478bd9Sstevel@tonic-gate sizeof (struct n_to_m_cache)); 4817c478bd9Sstevel@tonic-gate if (ptr == NULL) { 4827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_MEM)); 4837c478bd9Sstevel@tonic-gate return (ERROR); 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate ptr->size = size + 1; 4867c478bd9Sstevel@tonic-gate /* allocate space to cache contents of file */ 4877c478bd9Sstevel@tonic-gate ptr->cached_file = (char **)calloc(ptr->size, sizeof (char *)); 4887c478bd9Sstevel@tonic-gate if (ptr->cached_file == NULL) { 4897c478bd9Sstevel@tonic-gate free(ptr); 4907c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_MEM)); 4917c478bd9Sstevel@tonic-gate return (ERROR); 4927c478bd9Sstevel@tonic-gate } 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate rewind(fp); 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate /* 4977c478bd9Sstevel@tonic-gate * now fill the cache 4987c478bd9Sstevel@tonic-gate * the cache is an array of char pointers indexed by major 4997c478bd9Sstevel@tonic-gate * number 5007c478bd9Sstevel@tonic-gate */ 5017c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line), fp) != NULL) { 502*1ca93273Seota /* cut off comments starting with '#' */ 503*1ca93273Seota if ((cp = strchr(line, '#')) != NULL) 504*1ca93273Seota *cp = '\0'; 505*1ca93273Seota /* ignore comment or blank lines */ 506*1ca93273Seota if (is_blank(line)) 507*1ca93273Seota continue; 508*1ca93273Seota /* sanity-check */ 5097c478bd9Sstevel@tonic-gate if (sscanf(line, "%s%s", drv, entry) != 2) { 5107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_BAD_LINE), 5117c478bd9Sstevel@tonic-gate filename, line); 5127c478bd9Sstevel@tonic-gate continue; 5137c478bd9Sstevel@tonic-gate } 5147c478bd9Sstevel@tonic-gate maj = atoi(entry); 5157c478bd9Sstevel@tonic-gate if ((ptr->cached_file[maj] = strdup(drv)) == NULL) { 5167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_MEM)); 5177c478bd9Sstevel@tonic-gate free(ptr->cached_file); 5187c478bd9Sstevel@tonic-gate free(ptr); 5197c478bd9Sstevel@tonic-gate return (ERROR); 5207c478bd9Sstevel@tonic-gate } 5217c478bd9Sstevel@tonic-gate (void) strcpy(ptr->cached_file[maj], drv); 5227c478bd9Sstevel@tonic-gate } 5237c478bd9Sstevel@tonic-gate (void) fclose(fp); 5247c478bd9Sstevel@tonic-gate /* link the cache struct into the list of cached files */ 5257c478bd9Sstevel@tonic-gate ptr->file = strdup(filename); 5267c478bd9Sstevel@tonic-gate if (ptr->file == NULL) { 5277c478bd9Sstevel@tonic-gate for (maj = 0; maj <= ptr->size; maj++) 5287c478bd9Sstevel@tonic-gate free(ptr->cached_file[maj]); 5297c478bd9Sstevel@tonic-gate free(ptr->cached_file); 5307c478bd9Sstevel@tonic-gate free(ptr); 5317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_MEM)); 5327c478bd9Sstevel@tonic-gate return (ERROR); 5337c478bd9Sstevel@tonic-gate } 5347c478bd9Sstevel@tonic-gate ptr->next = head; 5357c478bd9Sstevel@tonic-gate head = ptr; 5367c478bd9Sstevel@tonic-gate } 5377c478bd9Sstevel@tonic-gate /* return value pointer to contents of file */ 5387c478bd9Sstevel@tonic-gate *cache = ptr->cached_file; 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate /* return size */ 5417c478bd9Sstevel@tonic-gate return (ptr->size); 5427c478bd9Sstevel@tonic-gate } 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate 5457c478bd9Sstevel@tonic-gate /* 5467c478bd9Sstevel@tonic-gate * Using get_cached_n_to_m_file(), retrieve maximum major number 5477c478bd9Sstevel@tonic-gate * found in the specificed file (name_to_major/rem_name_to_major). 5487c478bd9Sstevel@tonic-gate * 5497c478bd9Sstevel@tonic-gate * The return value is actually the size of the internal cache including 0. 5507c478bd9Sstevel@tonic-gate */ 5517c478bd9Sstevel@tonic-gate int 5527c478bd9Sstevel@tonic-gate get_max_major(char *file_name) 5537c478bd9Sstevel@tonic-gate { 5547c478bd9Sstevel@tonic-gate char **n_to_m_cache = NULL; 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate return (get_cached_n_to_m_file(file_name, &n_to_m_cache)); 5577c478bd9Sstevel@tonic-gate } 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate 5607c478bd9Sstevel@tonic-gate /* 5617c478bd9Sstevel@tonic-gate * searching name_to_major: if major_no == UNIQUE then the caller wants to 5627c478bd9Sstevel@tonic-gate * use the driver name as the key. Otherwise, the caller wants to use 5637c478bd9Sstevel@tonic-gate * the major number as a key. 5647c478bd9Sstevel@tonic-gate * 5657c478bd9Sstevel@tonic-gate * This routine caches the contents of the name_to_major file on 5667c478bd9Sstevel@tonic-gate * first call. And it could be generalized to deal with other 5677c478bd9Sstevel@tonic-gate * config files if necessary. 5687c478bd9Sstevel@tonic-gate */ 5697c478bd9Sstevel@tonic-gate static int 5707c478bd9Sstevel@tonic-gate get_name_to_major_entry(int *major_no, char *driver_name, char *file_name) 5717c478bd9Sstevel@tonic-gate { 5727c478bd9Sstevel@tonic-gate int maj; 5737c478bd9Sstevel@tonic-gate char **n_to_m_cache = NULL; 5747c478bd9Sstevel@tonic-gate int size = 0; 5757c478bd9Sstevel@tonic-gate 5767c478bd9Sstevel@tonic-gate int ret = NOT_UNIQUE; 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate /* 5797c478bd9Sstevel@tonic-gate * read the file in - we cache it in case caller wants to 5807c478bd9Sstevel@tonic-gate * do multiple lookups 5817c478bd9Sstevel@tonic-gate */ 5827c478bd9Sstevel@tonic-gate size = get_cached_n_to_m_file(file_name, &n_to_m_cache); 5837c478bd9Sstevel@tonic-gate 5847c478bd9Sstevel@tonic-gate if (size == ERROR) 5857c478bd9Sstevel@tonic-gate return (ERROR); 5867c478bd9Sstevel@tonic-gate 5877c478bd9Sstevel@tonic-gate /* search with driver name as key */ 5887c478bd9Sstevel@tonic-gate if (*major_no == UNIQUE) { 5897c478bd9Sstevel@tonic-gate for (maj = 0; maj < size; maj++) { 5907c478bd9Sstevel@tonic-gate if ((n_to_m_cache[maj] != NULL) && 5917c478bd9Sstevel@tonic-gate (strcmp(driver_name, n_to_m_cache[maj]) == 0)) { 5927c478bd9Sstevel@tonic-gate *major_no = maj; 5937c478bd9Sstevel@tonic-gate break; 5947c478bd9Sstevel@tonic-gate } 5957c478bd9Sstevel@tonic-gate } 5967c478bd9Sstevel@tonic-gate if (maj >= size) 5977c478bd9Sstevel@tonic-gate ret = UNIQUE; 5987c478bd9Sstevel@tonic-gate /* search with major number as key */ 5997c478bd9Sstevel@tonic-gate } else { 6007c478bd9Sstevel@tonic-gate /* 6017c478bd9Sstevel@tonic-gate * Bugid 1254588, drvconfig dump core after loading driver 6027c478bd9Sstevel@tonic-gate * with major number bigger than entries defined in 6037c478bd9Sstevel@tonic-gate * /etc/name_to_major. 6047c478bd9Sstevel@tonic-gate */ 6057c478bd9Sstevel@tonic-gate if (*major_no >= size) 6067c478bd9Sstevel@tonic-gate return (UNIQUE); 6077c478bd9Sstevel@tonic-gate 6087c478bd9Sstevel@tonic-gate if (n_to_m_cache[*major_no] != NULL) { 6097c478bd9Sstevel@tonic-gate (void) strcpy(driver_name, n_to_m_cache[*major_no]); 6107c478bd9Sstevel@tonic-gate } else 6117c478bd9Sstevel@tonic-gate ret = UNIQUE; 6127c478bd9Sstevel@tonic-gate } 6137c478bd9Sstevel@tonic-gate return (ret); 6147c478bd9Sstevel@tonic-gate } 6157c478bd9Sstevel@tonic-gate 6167c478bd9Sstevel@tonic-gate /* 6177c478bd9Sstevel@tonic-gate * given pointer to member n in space separated list, return pointer 6187c478bd9Sstevel@tonic-gate * to member n+1, return member n 6197c478bd9Sstevel@tonic-gate */ 6207c478bd9Sstevel@tonic-gate char * 6217c478bd9Sstevel@tonic-gate get_entry( 6227c478bd9Sstevel@tonic-gate char *prev_member, 6237c478bd9Sstevel@tonic-gate char *current_entry, 6247c478bd9Sstevel@tonic-gate char separator) 6257c478bd9Sstevel@tonic-gate { 6267c478bd9Sstevel@tonic-gate char *ptr; 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate ptr = prev_member; 6297c478bd9Sstevel@tonic-gate 6307c478bd9Sstevel@tonic-gate /* skip white space */ 6317c478bd9Sstevel@tonic-gate while (*ptr == '\t' || *ptr == ' ') 6327c478bd9Sstevel@tonic-gate ptr++; 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate /* read thru the current entry */ 6357c478bd9Sstevel@tonic-gate while (*ptr != separator && *ptr != '\0') { 6367c478bd9Sstevel@tonic-gate *current_entry++ = *ptr++; 6377c478bd9Sstevel@tonic-gate } 6387c478bd9Sstevel@tonic-gate *current_entry = '\0'; 6397c478bd9Sstevel@tonic-gate 6407c478bd9Sstevel@tonic-gate if ((separator == ',') && (*ptr == separator)) 6417c478bd9Sstevel@tonic-gate ptr++; /* skip over comma */ 6427c478bd9Sstevel@tonic-gate 6437c478bd9Sstevel@tonic-gate /* skip white space */ 6447c478bd9Sstevel@tonic-gate while (*ptr == '\t' || *ptr == ' ') { 6457c478bd9Sstevel@tonic-gate ptr++; 6467c478bd9Sstevel@tonic-gate } 6477c478bd9Sstevel@tonic-gate 6487c478bd9Sstevel@tonic-gate return (ptr); 6497c478bd9Sstevel@tonic-gate } 6507c478bd9Sstevel@tonic-gate 6517c478bd9Sstevel@tonic-gate /*ARGSUSED0*/ 6527c478bd9Sstevel@tonic-gate static void 6537c478bd9Sstevel@tonic-gate signal_rtn(int sig) 6547c478bd9Sstevel@tonic-gate { 6557c478bd9Sstevel@tonic-gate exit_unlock(); 6567c478bd9Sstevel@tonic-gate } 6577c478bd9Sstevel@tonic-gate 6587c478bd9Sstevel@tonic-gate void 6597c478bd9Sstevel@tonic-gate enter_lock(void) 6607c478bd9Sstevel@tonic-gate { 6617c478bd9Sstevel@tonic-gate int fd; 6627c478bd9Sstevel@tonic-gate 6637c478bd9Sstevel@tonic-gate /* 6647c478bd9Sstevel@tonic-gate * Setup handler to clean lock file in case user terminates 6657c478bd9Sstevel@tonic-gate * the command. 6667c478bd9Sstevel@tonic-gate */ 6677c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, signal_rtn); 6687c478bd9Sstevel@tonic-gate (void) sigset(SIGHUP, signal_rtn); 6697c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, signal_rtn); 6707c478bd9Sstevel@tonic-gate 6717c478bd9Sstevel@tonic-gate /* 6727c478bd9Sstevel@tonic-gate * attempt to create the lock file 6737c478bd9Sstevel@tonic-gate */ 6747c478bd9Sstevel@tonic-gate if ((fd = open(add_rem_lock, O_CREAT | O_EXCL | O_WRONLY, 6757c478bd9Sstevel@tonic-gate S_IRUSR | S_IWUSR)) == -1) { 6767c478bd9Sstevel@tonic-gate if (errno == EEXIST) { 6777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_PROG_IN_USE)); 6787c478bd9Sstevel@tonic-gate } else { 6797c478bd9Sstevel@tonic-gate perror(gettext(ERR_LOCKFILE)); 6807c478bd9Sstevel@tonic-gate } 6817c478bd9Sstevel@tonic-gate exit(1); 6827c478bd9Sstevel@tonic-gate } 6837c478bd9Sstevel@tonic-gate (void) close(fd); 6847c478bd9Sstevel@tonic-gate } 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate void 6877c478bd9Sstevel@tonic-gate err_exit(void) 6887c478bd9Sstevel@tonic-gate { 6897c478bd9Sstevel@tonic-gate /* release memory allocated for moddir */ 6907c478bd9Sstevel@tonic-gate cleanup_moddir(); 6917c478bd9Sstevel@tonic-gate /* remove add_drv/rem_drv lock */ 6927c478bd9Sstevel@tonic-gate exit_unlock(); 6937c478bd9Sstevel@tonic-gate exit(1); 6947c478bd9Sstevel@tonic-gate } 6957c478bd9Sstevel@tonic-gate 6967c478bd9Sstevel@tonic-gate void 6977c478bd9Sstevel@tonic-gate cleanup_moddir(void) 6987c478bd9Sstevel@tonic-gate { 6997c478bd9Sstevel@tonic-gate struct drvmod_dir *walk_ptr; 7007c478bd9Sstevel@tonic-gate struct drvmod_dir *free_ptr = moddir; 7017c478bd9Sstevel@tonic-gate 7027c478bd9Sstevel@tonic-gate while (free_ptr != NULL) { 7037c478bd9Sstevel@tonic-gate walk_ptr = free_ptr->next; 7047c478bd9Sstevel@tonic-gate free(free_ptr); 7057c478bd9Sstevel@tonic-gate free_ptr = walk_ptr; 7067c478bd9Sstevel@tonic-gate } 7077c478bd9Sstevel@tonic-gate } 7087c478bd9Sstevel@tonic-gate 7097c478bd9Sstevel@tonic-gate void 7107c478bd9Sstevel@tonic-gate exit_unlock(void) 7117c478bd9Sstevel@tonic-gate { 7127c478bd9Sstevel@tonic-gate struct stat buf; 7137c478bd9Sstevel@tonic-gate 7147c478bd9Sstevel@tonic-gate if (stat(add_rem_lock, &buf) == NOERR) { 7157c478bd9Sstevel@tonic-gate if (unlink(add_rem_lock) == -1) { 7167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_REM_LOCK), 7177c478bd9Sstevel@tonic-gate add_rem_lock); 7187c478bd9Sstevel@tonic-gate } 7197c478bd9Sstevel@tonic-gate } 7207c478bd9Sstevel@tonic-gate } 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate /* 7237c478bd9Sstevel@tonic-gate * error adding driver; need to back out any changes to files. 7247c478bd9Sstevel@tonic-gate * check flag to see which files need entries removed 7257c478bd9Sstevel@tonic-gate * entry removal based on driver name 7267c478bd9Sstevel@tonic-gate */ 7277c478bd9Sstevel@tonic-gate void 7287c478bd9Sstevel@tonic-gate remove_entry( 7297c478bd9Sstevel@tonic-gate int c_flag, 7307c478bd9Sstevel@tonic-gate char *driver_name) 7317c478bd9Sstevel@tonic-gate { 7327c478bd9Sstevel@tonic-gate 7337c478bd9Sstevel@tonic-gate if (c_flag & CLEAN_NAM_MAJ) { 7347c478bd9Sstevel@tonic-gate if (delete_entry(name_to_major, driver_name, " ", 7357c478bd9Sstevel@tonic-gate NULL) == ERROR) { 7367c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_CLEAN), 7377c478bd9Sstevel@tonic-gate name_to_major, driver_name); 7387c478bd9Sstevel@tonic-gate } 7397c478bd9Sstevel@tonic-gate } 7407c478bd9Sstevel@tonic-gate 7417c478bd9Sstevel@tonic-gate if (c_flag & CLEAN_DRV_ALIAS) { 7427c478bd9Sstevel@tonic-gate if (delete_entry(driver_aliases, driver_name, " ", 7437c478bd9Sstevel@tonic-gate NULL) == ERROR) { 7447c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_DEL_ENTRY), 7457c478bd9Sstevel@tonic-gate driver_name, driver_aliases); 7467c478bd9Sstevel@tonic-gate } 7477c478bd9Sstevel@tonic-gate } 7487c478bd9Sstevel@tonic-gate 7497c478bd9Sstevel@tonic-gate if (c_flag & CLEAN_DRV_CLASSES) { 7507c478bd9Sstevel@tonic-gate if (delete_entry(driver_classes, driver_name, "\t", NULL) == 7517c478bd9Sstevel@tonic-gate ERROR) { 7527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_DEL_ENTRY), 7537c478bd9Sstevel@tonic-gate driver_name, driver_classes); 7547c478bd9Sstevel@tonic-gate } 7557c478bd9Sstevel@tonic-gate } 7567c478bd9Sstevel@tonic-gate 7577c478bd9Sstevel@tonic-gate if (c_flag & CLEAN_MINOR_PERM) { 7587c478bd9Sstevel@tonic-gate if (delete_entry(minor_perm, driver_name, ":", NULL) == ERROR) { 7597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_DEL_ENTRY), 7607c478bd9Sstevel@tonic-gate driver_name, minor_perm); 7617c478bd9Sstevel@tonic-gate } 7627c478bd9Sstevel@tonic-gate } 7637c478bd9Sstevel@tonic-gate /* 7647c478bd9Sstevel@tonic-gate * There's no point in removing entries from files that don't 7657c478bd9Sstevel@tonic-gate * exist. Prevent error messages by checking for file existence 7667c478bd9Sstevel@tonic-gate * first. 7677c478bd9Sstevel@tonic-gate */ 7687c478bd9Sstevel@tonic-gate if ((c_flag & CLEAN_DEV_POLICY) != 0 && 7697c478bd9Sstevel@tonic-gate access(device_policy, F_OK) == 0) { 7707c478bd9Sstevel@tonic-gate if (delete_plcy_entry(device_policy, driver_name) == ERROR) { 7717c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_DEL_ENTRY), 7727c478bd9Sstevel@tonic-gate driver_name, device_policy); 7737c478bd9Sstevel@tonic-gate } 7747c478bd9Sstevel@tonic-gate } 7757c478bd9Sstevel@tonic-gate if ((c_flag & CLEAN_DRV_PRIV) != 0 && 7767c478bd9Sstevel@tonic-gate access(extra_privs, F_OK) == 0) { 7777c478bd9Sstevel@tonic-gate if (delete_entry(extra_privs, driver_name, ":", NULL) == 7787c478bd9Sstevel@tonic-gate ERROR) { 7797c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_DEL_ENTRY), 7807c478bd9Sstevel@tonic-gate driver_name, extra_privs); 7817c478bd9Sstevel@tonic-gate } 7827c478bd9Sstevel@tonic-gate } 7837c478bd9Sstevel@tonic-gate } 7847c478bd9Sstevel@tonic-gate 7857c478bd9Sstevel@tonic-gate int 7867c478bd9Sstevel@tonic-gate check_perms_aliases( 7877c478bd9Sstevel@tonic-gate int m_flag, 7887c478bd9Sstevel@tonic-gate int i_flag) 7897c478bd9Sstevel@tonic-gate { 7907c478bd9Sstevel@tonic-gate /* 7917c478bd9Sstevel@tonic-gate * If neither i_flag nor m_flag are specified no need to check the 7927c478bd9Sstevel@tonic-gate * files for access permissions 7937c478bd9Sstevel@tonic-gate */ 7947c478bd9Sstevel@tonic-gate if (!m_flag && !i_flag) 7957c478bd9Sstevel@tonic-gate return (NOERR); 7967c478bd9Sstevel@tonic-gate 7977c478bd9Sstevel@tonic-gate /* check minor_perm file : exits and is writable */ 7987c478bd9Sstevel@tonic-gate if (m_flag) { 7997c478bd9Sstevel@tonic-gate if (access(minor_perm, R_OK | W_OK)) { 8007c478bd9Sstevel@tonic-gate perror(NULL); 8017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), 8027c478bd9Sstevel@tonic-gate minor_perm); 8037c478bd9Sstevel@tonic-gate return (ERROR); 8047c478bd9Sstevel@tonic-gate } 8057c478bd9Sstevel@tonic-gate } 8067c478bd9Sstevel@tonic-gate 8077c478bd9Sstevel@tonic-gate /* check driver_aliases file : exits and is writable */ 8087c478bd9Sstevel@tonic-gate if (i_flag) { 8097c478bd9Sstevel@tonic-gate if (access(driver_aliases, R_OK | W_OK)) { 8107c478bd9Sstevel@tonic-gate perror(NULL); 8117c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), 8127c478bd9Sstevel@tonic-gate driver_aliases); 8137c478bd9Sstevel@tonic-gate return (ERROR); 8147c478bd9Sstevel@tonic-gate } 8157c478bd9Sstevel@tonic-gate } 8167c478bd9Sstevel@tonic-gate 8177c478bd9Sstevel@tonic-gate return (NOERR); 8187c478bd9Sstevel@tonic-gate } 8197c478bd9Sstevel@tonic-gate 8207c478bd9Sstevel@tonic-gate 8217c478bd9Sstevel@tonic-gate int 8227c478bd9Sstevel@tonic-gate check_name_to_major(int mode) 8237c478bd9Sstevel@tonic-gate { 8247c478bd9Sstevel@tonic-gate /* check name_to_major file : exists and is writable */ 8257c478bd9Sstevel@tonic-gate if (access(name_to_major, mode)) { 8267c478bd9Sstevel@tonic-gate perror(NULL); 8277c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), 8287c478bd9Sstevel@tonic-gate name_to_major); 8297c478bd9Sstevel@tonic-gate return (ERROR); 8307c478bd9Sstevel@tonic-gate } 8317c478bd9Sstevel@tonic-gate 8327c478bd9Sstevel@tonic-gate return (NOERR); 8337c478bd9Sstevel@tonic-gate } 8347c478bd9Sstevel@tonic-gate 8357c478bd9Sstevel@tonic-gate 8367c478bd9Sstevel@tonic-gate /* 8377c478bd9Sstevel@tonic-gate * All this stuff is to support a server installing 8387c478bd9Sstevel@tonic-gate * drivers on diskless clients. When on the server 8397c478bd9Sstevel@tonic-gate * need to prepend the basedir 8407c478bd9Sstevel@tonic-gate */ 8417c478bd9Sstevel@tonic-gate int 8427c478bd9Sstevel@tonic-gate build_filenames(char *basedir) 8437c478bd9Sstevel@tonic-gate { 8447c478bd9Sstevel@tonic-gate int len; 8457c478bd9Sstevel@tonic-gate 8467c478bd9Sstevel@tonic-gate if (basedir == NULL) { 8477c478bd9Sstevel@tonic-gate driver_aliases = DRIVER_ALIAS; 8487c478bd9Sstevel@tonic-gate driver_classes = DRIVER_CLASSES; 8497c478bd9Sstevel@tonic-gate minor_perm = MINOR_PERM; 8507c478bd9Sstevel@tonic-gate name_to_major = NAM_TO_MAJ; 8517c478bd9Sstevel@tonic-gate rem_name_to_major = REM_NAM_TO_MAJ; 8527c478bd9Sstevel@tonic-gate add_rem_lock = ADD_REM_LOCK; 8537c478bd9Sstevel@tonic-gate tmphold = TMPHOLD; 8547c478bd9Sstevel@tonic-gate devfs_root = DEVFS_ROOT; 8557c478bd9Sstevel@tonic-gate device_policy = DEV_POLICY; 8567c478bd9Sstevel@tonic-gate extra_privs = EXTRA_PRIVS; 8577c478bd9Sstevel@tonic-gate 8587c478bd9Sstevel@tonic-gate } else { 8597c478bd9Sstevel@tonic-gate len = strlen(basedir); 8607c478bd9Sstevel@tonic-gate 8617c478bd9Sstevel@tonic-gate driver_aliases = malloc(len + sizeof (DRIVER_ALIAS)); 8627c478bd9Sstevel@tonic-gate driver_classes = malloc(len + sizeof (DRIVER_CLASSES)); 8637c478bd9Sstevel@tonic-gate minor_perm = malloc(len + sizeof (MINOR_PERM)); 8647c478bd9Sstevel@tonic-gate name_to_major = malloc(len + sizeof (NAM_TO_MAJ)); 8657c478bd9Sstevel@tonic-gate rem_name_to_major = malloc(len + sizeof (REM_NAM_TO_MAJ)); 8667c478bd9Sstevel@tonic-gate add_rem_lock = malloc(len + sizeof (ADD_REM_LOCK)); 8677c478bd9Sstevel@tonic-gate tmphold = malloc(len + sizeof (TMPHOLD)); 8687c478bd9Sstevel@tonic-gate devfs_root = malloc(len + sizeof (DEVFS_ROOT)); 8697c478bd9Sstevel@tonic-gate device_policy = malloc(len + sizeof (DEV_POLICY)); 8707c478bd9Sstevel@tonic-gate extra_privs = malloc(len + sizeof (EXTRA_PRIVS)); 8717c478bd9Sstevel@tonic-gate 8727c478bd9Sstevel@tonic-gate 8737c478bd9Sstevel@tonic-gate if ((driver_aliases == NULL) || 8747c478bd9Sstevel@tonic-gate (driver_classes == NULL) || 8757c478bd9Sstevel@tonic-gate (minor_perm == NULL) || 8767c478bd9Sstevel@tonic-gate (name_to_major == NULL) || 8777c478bd9Sstevel@tonic-gate (rem_name_to_major == NULL) || 8787c478bd9Sstevel@tonic-gate (add_rem_lock == NULL) || 8797c478bd9Sstevel@tonic-gate (tmphold == NULL) || 8807c478bd9Sstevel@tonic-gate (devfs_root == NULL) || 8817c478bd9Sstevel@tonic-gate (device_policy == NULL) || 8827c478bd9Sstevel@tonic-gate (extra_privs == NULL)) { 8837c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_MEM)); 8847c478bd9Sstevel@tonic-gate return (ERROR); 8857c478bd9Sstevel@tonic-gate } 8867c478bd9Sstevel@tonic-gate 8877c478bd9Sstevel@tonic-gate (void) sprintf(driver_aliases, "%s%s", basedir, DRIVER_ALIAS); 8887c478bd9Sstevel@tonic-gate (void) sprintf(driver_classes, "%s%s", basedir, DRIVER_CLASSES); 8897c478bd9Sstevel@tonic-gate (void) sprintf(minor_perm, "%s%s", basedir, MINOR_PERM); 8907c478bd9Sstevel@tonic-gate (void) sprintf(name_to_major, "%s%s", basedir, NAM_TO_MAJ); 8917c478bd9Sstevel@tonic-gate (void) sprintf(rem_name_to_major, "%s%s", basedir, 8927c478bd9Sstevel@tonic-gate REM_NAM_TO_MAJ); 8937c478bd9Sstevel@tonic-gate (void) sprintf(add_rem_lock, "%s%s", basedir, ADD_REM_LOCK); 8947c478bd9Sstevel@tonic-gate (void) sprintf(tmphold, "%s%s", basedir, TMPHOLD); 8957c478bd9Sstevel@tonic-gate (void) sprintf(devfs_root, "%s%s", basedir, DEVFS_ROOT); 8967c478bd9Sstevel@tonic-gate (void) sprintf(device_policy, "%s%s", basedir, DEV_POLICY); 8977c478bd9Sstevel@tonic-gate (void) sprintf(extra_privs, "%s%s", basedir, EXTRA_PRIVS); 8987c478bd9Sstevel@tonic-gate } 8997c478bd9Sstevel@tonic-gate 9007c478bd9Sstevel@tonic-gate return (NOERR); 9017c478bd9Sstevel@tonic-gate } 9027c478bd9Sstevel@tonic-gate 9037c478bd9Sstevel@tonic-gate static int 9047c478bd9Sstevel@tonic-gate exec_command(char *path, char *cmdline[MAX_CMD_LINE]) 9057c478bd9Sstevel@tonic-gate { 9067c478bd9Sstevel@tonic-gate pid_t pid; 9077c478bd9Sstevel@tonic-gate uint_t stat_loc; 9087c478bd9Sstevel@tonic-gate int waitstat; 9097c478bd9Sstevel@tonic-gate int exit_status; 9107c478bd9Sstevel@tonic-gate 9117c478bd9Sstevel@tonic-gate /* child */ 9127c478bd9Sstevel@tonic-gate if ((pid = fork()) == 0) { 9137c478bd9Sstevel@tonic-gate (void) execv(path, cmdline); 9147c478bd9Sstevel@tonic-gate perror(NULL); 9157c478bd9Sstevel@tonic-gate return (ERROR); 9167c478bd9Sstevel@tonic-gate } else if (pid == -1) { 9177c478bd9Sstevel@tonic-gate /* fork failed */ 9187c478bd9Sstevel@tonic-gate perror(NULL); 9197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_FORK_FAIL), cmdline); 9207c478bd9Sstevel@tonic-gate return (ERROR); 9217c478bd9Sstevel@tonic-gate } else { 9227c478bd9Sstevel@tonic-gate /* parent */ 9237c478bd9Sstevel@tonic-gate do { 9247c478bd9Sstevel@tonic-gate waitstat = waitpid(pid, (int *)&stat_loc, 0); 9257c478bd9Sstevel@tonic-gate 9267c478bd9Sstevel@tonic-gate } while ((!WIFEXITED(stat_loc) && 9277c478bd9Sstevel@tonic-gate !WIFSIGNALED(stat_loc)) || (waitstat == 0)); 9287c478bd9Sstevel@tonic-gate 9297c478bd9Sstevel@tonic-gate exit_status = WEXITSTATUS(stat_loc); 9307c478bd9Sstevel@tonic-gate 9317c478bd9Sstevel@tonic-gate return (exit_status); 9327c478bd9Sstevel@tonic-gate } 9337c478bd9Sstevel@tonic-gate } 9347c478bd9Sstevel@tonic-gate 9357c478bd9Sstevel@tonic-gate /* 9367c478bd9Sstevel@tonic-gate * check that major_num doesn't exceed maximum on this machine 9377c478bd9Sstevel@tonic-gate * do this here to support add_drv on server for diskless clients 9387c478bd9Sstevel@tonic-gate */ 9397c478bd9Sstevel@tonic-gate int 9407c478bd9Sstevel@tonic-gate config_driver( 9417c478bd9Sstevel@tonic-gate char *driver_name, 9427c478bd9Sstevel@tonic-gate major_t major_num, 9437c478bd9Sstevel@tonic-gate char *aliases, 9447c478bd9Sstevel@tonic-gate char *classes, 9457c478bd9Sstevel@tonic-gate int cleanup_flag, 9467c478bd9Sstevel@tonic-gate int verbose_flag) 9477c478bd9Sstevel@tonic-gate { 9487c478bd9Sstevel@tonic-gate int max_dev; 9497c478bd9Sstevel@tonic-gate int n = 0; 9507c478bd9Sstevel@tonic-gate char *cmdline[MAX_CMD_LINE]; 9517c478bd9Sstevel@tonic-gate char maj_num[128]; 9527c478bd9Sstevel@tonic-gate char *previous; 9537c478bd9Sstevel@tonic-gate char *current; 9547c478bd9Sstevel@tonic-gate int exec_status; 9557c478bd9Sstevel@tonic-gate int len; 9567c478bd9Sstevel@tonic-gate 9577c478bd9Sstevel@tonic-gate if (modctl(MODRESERVED, NULL, &max_dev) < 0) { 9587c478bd9Sstevel@tonic-gate perror(NULL); 9597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_MAX_MAJOR)); 9607c478bd9Sstevel@tonic-gate return (ERROR); 9617c478bd9Sstevel@tonic-gate } 9627c478bd9Sstevel@tonic-gate 9637c478bd9Sstevel@tonic-gate if (major_num >= max_dev) { 9647c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_MAX_EXCEEDS), 9657c478bd9Sstevel@tonic-gate major_num, max_dev); 9667c478bd9Sstevel@tonic-gate return (ERROR); 9677c478bd9Sstevel@tonic-gate } 9687c478bd9Sstevel@tonic-gate 9697c478bd9Sstevel@tonic-gate /* bind major number and driver name */ 9707c478bd9Sstevel@tonic-gate 9717c478bd9Sstevel@tonic-gate /* build command line */ 9727c478bd9Sstevel@tonic-gate cmdline[n++] = DRVCONFIG; 9737c478bd9Sstevel@tonic-gate if (verbose_flag) { 9747c478bd9Sstevel@tonic-gate cmdline[n++] = "-v"; 9757c478bd9Sstevel@tonic-gate } 9767c478bd9Sstevel@tonic-gate cmdline[n++] = "-b"; 9777c478bd9Sstevel@tonic-gate if (classes) { 9787c478bd9Sstevel@tonic-gate cmdline[n++] = "-c"; 9797c478bd9Sstevel@tonic-gate cmdline[n++] = classes; 9807c478bd9Sstevel@tonic-gate } 9817c478bd9Sstevel@tonic-gate cmdline[n++] = "-i"; 9827c478bd9Sstevel@tonic-gate cmdline[n++] = driver_name; 9837c478bd9Sstevel@tonic-gate cmdline[n++] = "-m"; 9847c478bd9Sstevel@tonic-gate (void) sprintf(maj_num, "%lu", major_num); 9857c478bd9Sstevel@tonic-gate cmdline[n++] = maj_num; 9867c478bd9Sstevel@tonic-gate 9877c478bd9Sstevel@tonic-gate if (aliases != NULL) { 9887c478bd9Sstevel@tonic-gate len = strlen(aliases); 9897c478bd9Sstevel@tonic-gate previous = aliases; 9907c478bd9Sstevel@tonic-gate do { 9917c478bd9Sstevel@tonic-gate cmdline[n++] = "-a"; 9927c478bd9Sstevel@tonic-gate cmdline[n] = calloc(len + 1, 1); 9937c478bd9Sstevel@tonic-gate if (cmdline[n] == NULL) { 9947c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9957c478bd9Sstevel@tonic-gate gettext(ERR_NO_MEM)); 9967c478bd9Sstevel@tonic-gate return (ERROR); 9977c478bd9Sstevel@tonic-gate } 9987c478bd9Sstevel@tonic-gate current = get_entry(previous, 9997c478bd9Sstevel@tonic-gate cmdline[n++], ' '); 10007c478bd9Sstevel@tonic-gate previous = current; 10017c478bd9Sstevel@tonic-gate 10027c478bd9Sstevel@tonic-gate } while (*current != '\0'); 10037c478bd9Sstevel@tonic-gate 10047c478bd9Sstevel@tonic-gate } 10057c478bd9Sstevel@tonic-gate cmdline[n] = (char *)0; 10067c478bd9Sstevel@tonic-gate 10077c478bd9Sstevel@tonic-gate exec_status = exec_command(DRVCONFIG_PATH, cmdline); 10087c478bd9Sstevel@tonic-gate 10097c478bd9Sstevel@tonic-gate if (exec_status == NOERR) 10107c478bd9Sstevel@tonic-gate return (NOERR); 10117c478bd9Sstevel@tonic-gate perror(NULL); 10127c478bd9Sstevel@tonic-gate remove_entry(cleanup_flag, driver_name); 10137c478bd9Sstevel@tonic-gate return (ERROR); 10147c478bd9Sstevel@tonic-gate } 10157c478bd9Sstevel@tonic-gate 10167c478bd9Sstevel@tonic-gate void 10177c478bd9Sstevel@tonic-gate load_driver(char *driver_name, int verbose_flag) 10187c478bd9Sstevel@tonic-gate { 10197c478bd9Sstevel@tonic-gate int n = 0; 10207c478bd9Sstevel@tonic-gate char *cmdline[MAX_CMD_LINE]; 10217c478bd9Sstevel@tonic-gate int exec_status; 10227c478bd9Sstevel@tonic-gate 10237c478bd9Sstevel@tonic-gate /* build command line */ 10247c478bd9Sstevel@tonic-gate cmdline[n++] = DEVFSADM; 10257c478bd9Sstevel@tonic-gate if (verbose_flag) { 10267c478bd9Sstevel@tonic-gate cmdline[n++] = "-v"; 10277c478bd9Sstevel@tonic-gate } 10287c478bd9Sstevel@tonic-gate cmdline[n++] = "-i"; 10297c478bd9Sstevel@tonic-gate cmdline[n++] = driver_name; 10307c478bd9Sstevel@tonic-gate cmdline[n] = (char *)0; 10317c478bd9Sstevel@tonic-gate 10327c478bd9Sstevel@tonic-gate exec_status = exec_command(DEVFSADM_PATH, cmdline); 10337c478bd9Sstevel@tonic-gate 10347c478bd9Sstevel@tonic-gate if (exec_status != NOERR) { 10357c478bd9Sstevel@tonic-gate /* no clean : name and major number are bound */ 10367c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_CONFIG), 10377c478bd9Sstevel@tonic-gate driver_name); 10387c478bd9Sstevel@tonic-gate } 10397c478bd9Sstevel@tonic-gate } 10407c478bd9Sstevel@tonic-gate 10417c478bd9Sstevel@tonic-gate void 10427c478bd9Sstevel@tonic-gate get_modid(char *driver_name, int *mod) 10437c478bd9Sstevel@tonic-gate { 10447c478bd9Sstevel@tonic-gate struct modinfo modinfo; 10457c478bd9Sstevel@tonic-gate 10467c478bd9Sstevel@tonic-gate modinfo.mi_id = -1; 10477c478bd9Sstevel@tonic-gate modinfo.mi_info = MI_INFO_ALL; 10487c478bd9Sstevel@tonic-gate do { 10497c478bd9Sstevel@tonic-gate /* 10507c478bd9Sstevel@tonic-gate * If we are at the end of the list of loaded modules 10517c478bd9Sstevel@tonic-gate * then set *mod = -1 and return 10527c478bd9Sstevel@tonic-gate */ 10537c478bd9Sstevel@tonic-gate if (modctl(MODINFO, 0, &modinfo) < 0) { 10547c478bd9Sstevel@tonic-gate *mod = -1; 10557c478bd9Sstevel@tonic-gate return; 10567c478bd9Sstevel@tonic-gate } 10577c478bd9Sstevel@tonic-gate 10587c478bd9Sstevel@tonic-gate *mod = modinfo.mi_id; 10597c478bd9Sstevel@tonic-gate } while (strcmp(driver_name, modinfo.mi_name) != 0); 10607c478bd9Sstevel@tonic-gate } 10617c478bd9Sstevel@tonic-gate 10627c478bd9Sstevel@tonic-gate int 10637c478bd9Sstevel@tonic-gate create_reconfig(char *basedir) 10647c478bd9Sstevel@tonic-gate { 10657c478bd9Sstevel@tonic-gate char reconfig_file[MAXPATHLEN + FILENAME_MAX + 1]; 10667c478bd9Sstevel@tonic-gate FILE *reconfig_fp; 10677c478bd9Sstevel@tonic-gate 10687c478bd9Sstevel@tonic-gate if (basedir != NULL) { 10697c478bd9Sstevel@tonic-gate (void) strcpy(reconfig_file, basedir); 10707c478bd9Sstevel@tonic-gate (void) strcat(reconfig_file, RECONFIGURE); 10717c478bd9Sstevel@tonic-gate } else { 10727c478bd9Sstevel@tonic-gate (void) strcpy(reconfig_file, RECONFIGURE); 10737c478bd9Sstevel@tonic-gate } 10747c478bd9Sstevel@tonic-gate if ((reconfig_fp = fopen(reconfig_file, "a")) == NULL) 10757c478bd9Sstevel@tonic-gate return (ERROR); 10767c478bd9Sstevel@tonic-gate 10777c478bd9Sstevel@tonic-gate (void) fclose(reconfig_fp); 10787c478bd9Sstevel@tonic-gate return (NOERR); 10797c478bd9Sstevel@tonic-gate } 10807c478bd9Sstevel@tonic-gate 10817c478bd9Sstevel@tonic-gate 10827c478bd9Sstevel@tonic-gate /* 10837c478bd9Sstevel@tonic-gate * update_minor_entry: 10847c478bd9Sstevel@tonic-gate * open file 10857c478bd9Sstevel@tonic-gate * for each entry in list 10867c478bd9Sstevel@tonic-gate * where list entries are separated by <list_separator> 10877c478bd9Sstevel@tonic-gate * modify entry : driver_name <entry_separator> entry 10887c478bd9Sstevel@tonic-gate * close file 10897c478bd9Sstevel@tonic-gate * 10907c478bd9Sstevel@tonic-gate * return error/noerr 10917c478bd9Sstevel@tonic-gate */ 10927c478bd9Sstevel@tonic-gate int 10937c478bd9Sstevel@tonic-gate update_minor_entry(char *driver_name, char *perm_list) 10947c478bd9Sstevel@tonic-gate { 10957c478bd9Sstevel@tonic-gate FILE *fp; 10967c478bd9Sstevel@tonic-gate FILE *newfp; 10977c478bd9Sstevel@tonic-gate struct group *sysgrp; 10987c478bd9Sstevel@tonic-gate int match = 0; 1099*1ca93273Seota char line[MAX_DBFILE_ENTRY], *cp, *dup; 1100*1ca93273Seota char drv[FILENAME_MAX + 1], *drv_minor; 11017c478bd9Sstevel@tonic-gate char minor[FILENAME_MAX + 1], perm[OPT_LEN + 1]; 11027c478bd9Sstevel@tonic-gate char own[OPT_LEN + 1], grp[OPT_LEN + 1]; 11037c478bd9Sstevel@tonic-gate int status = NOERR, i; 11047c478bd9Sstevel@tonic-gate char *newfile, *tptr; 11057c478bd9Sstevel@tonic-gate extern void bzero(); 11067c478bd9Sstevel@tonic-gate 11077c478bd9Sstevel@tonic-gate if ((fp = fopen(minor_perm, "r")) == NULL) { 11087c478bd9Sstevel@tonic-gate perror(NULL); 11097c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), 11107c478bd9Sstevel@tonic-gate minor_perm); 11117c478bd9Sstevel@tonic-gate 11127c478bd9Sstevel@tonic-gate return (ERROR); 11137c478bd9Sstevel@tonic-gate } 11147c478bd9Sstevel@tonic-gate 11157c478bd9Sstevel@tonic-gate /* 11167c478bd9Sstevel@tonic-gate * Build filename for temporary file 11177c478bd9Sstevel@tonic-gate */ 11187c478bd9Sstevel@tonic-gate if ((tptr = calloc(strlen(minor_perm) + strlen(XEND) + 1, 1)) == NULL) { 11197c478bd9Sstevel@tonic-gate perror(NULL); 11207c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_MEM)); 11217c478bd9Sstevel@tonic-gate } 11227c478bd9Sstevel@tonic-gate (void) strcpy(tptr, minor_perm); 11237c478bd9Sstevel@tonic-gate (void) strcat(tptr, XEND); 11247c478bd9Sstevel@tonic-gate 11257c478bd9Sstevel@tonic-gate /* 11267c478bd9Sstevel@tonic-gate * Set gid so we preserve group attribute. Ideally we wouldn't 11277c478bd9Sstevel@tonic-gate * assume a gid of "sys" but we can't undo the damage on already 11287c478bd9Sstevel@tonic-gate * installed systems unless we force the issue. 11297c478bd9Sstevel@tonic-gate */ 11307c478bd9Sstevel@tonic-gate if ((sysgrp = getgrnam("sys")) != NULL) { 11317c478bd9Sstevel@tonic-gate (void) setgid(sysgrp->gr_gid); 11327c478bd9Sstevel@tonic-gate } 11337c478bd9Sstevel@tonic-gate 11347c478bd9Sstevel@tonic-gate newfile = mktemp(tptr); 11357c478bd9Sstevel@tonic-gate if ((newfp = fopen(newfile, "w")) == NULL) { 11367c478bd9Sstevel@tonic-gate perror(NULL); 11377c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), 11387c478bd9Sstevel@tonic-gate newfile); 11397c478bd9Sstevel@tonic-gate return (ERROR); 11407c478bd9Sstevel@tonic-gate } 11417c478bd9Sstevel@tonic-gate 11427c478bd9Sstevel@tonic-gate if (sscanf(perm_list, "%s%s%s%s", minor, perm, own, grp) != 4) { 11437c478bd9Sstevel@tonic-gate status = ERROR; 11447c478bd9Sstevel@tonic-gate } 11457c478bd9Sstevel@tonic-gate 11467c478bd9Sstevel@tonic-gate while ((fgets(line, sizeof (line), fp) != NULL) && status == NOERR) { 1147*1ca93273Seota /* copy the whole line into dup */ 1148*1ca93273Seota if ((dup = strdup(line)) == NULL) { 1149*1ca93273Seota perror(NULL); 1150*1ca93273Seota (void) fprintf(stderr, gettext(ERR_NO_MEM)); 1151*1ca93273Seota status = ERROR; 1152*1ca93273Seota break; 1153*1ca93273Seota } 1154*1ca93273Seota /* cut off comments starting with '#' */ 1155*1ca93273Seota if ((cp = strchr(dup, '#')) != NULL) 1156*1ca93273Seota *cp = '\0'; 1157*1ca93273Seota /* ignore comment or blank lines */ 1158*1ca93273Seota if (is_blank(dup)) { 1159*1ca93273Seota if (fputs(line, newfp) == EOF) { 11607c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_UPDATE), 11617c478bd9Sstevel@tonic-gate minor_perm); 11627c478bd9Sstevel@tonic-gate status = ERROR; 11637c478bd9Sstevel@tonic-gate } 1164*1ca93273Seota free(dup); 11657c478bd9Sstevel@tonic-gate continue; 11667c478bd9Sstevel@tonic-gate } 11677c478bd9Sstevel@tonic-gate 1168*1ca93273Seota /* get the driver name */ 1169*1ca93273Seota if (sscanf(dup, "%s", drv) != 1) { 11707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_BAD_LINE), 11717c478bd9Sstevel@tonic-gate minor_perm, line); 11727c478bd9Sstevel@tonic-gate status = ERROR; 1173*1ca93273Seota free(dup); 1174*1ca93273Seota break; 11757c478bd9Sstevel@tonic-gate } 11767c478bd9Sstevel@tonic-gate 1177*1ca93273Seota /* 1178*1ca93273Seota * get the minor name; place the NULL character at the 1179*1ca93273Seota * end of the driver name, then make the drv_minor 1180*1ca93273Seota * point to the first character of the minor name. 1181*1ca93273Seota * the line missing ':' must be treated as a broken one. 1182*1ca93273Seota */ 1183*1ca93273Seota i = strcspn(drv, ":"); 1184*1ca93273Seota if (i == strlen(drv)) { 11857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_BAD_LINE), 11867c478bd9Sstevel@tonic-gate minor_perm, line); 11877c478bd9Sstevel@tonic-gate status = ERROR; 1188*1ca93273Seota free(dup); 1189*1ca93273Seota break; 11907c478bd9Sstevel@tonic-gate } 1191*1ca93273Seota drv[i] = '\0'; 1192*1ca93273Seota drv_minor = &drv[strlen(drv) + 1]; 11937c478bd9Sstevel@tonic-gate 1194*1ca93273Seota /* 1195*1ca93273Seota * compare both of the driver name and the minor name. 1196*1ca93273Seota * then the new line should be written to the file if 1197*1ca93273Seota * both of them match 1198*1ca93273Seota */ 11997c478bd9Sstevel@tonic-gate if ((strcmp(drv, driver_name) == 0) && 12007c478bd9Sstevel@tonic-gate (strcmp(minor, drv_minor) == 0)) { 1201*1ca93273Seota /* if it has a comment, keep it */ 1202*1ca93273Seota if (cp != NULL) { 1203*1ca93273Seota cp++; /* skip a terminator */ 1204*1ca93273Seota (void) sprintf(line, "%s:%s %s %s %s #%s\n", 1205*1ca93273Seota drv, minor, perm, own, grp, cp); 1206*1ca93273Seota } else { 1207*1ca93273Seota (void) sprintf(line, "%s:%s %s %s %s\n", 1208*1ca93273Seota drv, minor, perm, own, grp); 1209*1ca93273Seota } 12107c478bd9Sstevel@tonic-gate match = 1; 12117c478bd9Sstevel@tonic-gate } 1212*1ca93273Seota free(dup); 12137c478bd9Sstevel@tonic-gate 1214*1ca93273Seota /* update the file */ 12157c478bd9Sstevel@tonic-gate if ((fputs(line, newfp)) == EOF) { 12167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_UPDATE), 12177c478bd9Sstevel@tonic-gate minor_perm); 12187c478bd9Sstevel@tonic-gate status = ERROR; 12197c478bd9Sstevel@tonic-gate } 12207c478bd9Sstevel@tonic-gate } 12217c478bd9Sstevel@tonic-gate 12227c478bd9Sstevel@tonic-gate if (!match) { 12237c478bd9Sstevel@tonic-gate (void) bzero(line, sizeof (&line[0])); 12247c478bd9Sstevel@tonic-gate (void) sprintf(line, "%s:%s %s %s %s\n", 12257c478bd9Sstevel@tonic-gate driver_name, minor, perm, own, grp); 12267c478bd9Sstevel@tonic-gate 12277c478bd9Sstevel@tonic-gate /* add the new entry */ 12287c478bd9Sstevel@tonic-gate if ((fputs(line, newfp)) == EOF) { 12297c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_UPDATE), minor_perm); 12307c478bd9Sstevel@tonic-gate status = ERROR; 12317c478bd9Sstevel@tonic-gate } 12327c478bd9Sstevel@tonic-gate } 12337c478bd9Sstevel@tonic-gate 12347c478bd9Sstevel@tonic-gate (void) fclose(fp); 12357c478bd9Sstevel@tonic-gate 12367c478bd9Sstevel@tonic-gate if (fflush(newfp) != 0 || fsync(fileno(newfp)) != 0) 12377c478bd9Sstevel@tonic-gate status = ERROR; 12387c478bd9Sstevel@tonic-gate 12397c478bd9Sstevel@tonic-gate (void) fclose(newfp); 12407c478bd9Sstevel@tonic-gate 12417c478bd9Sstevel@tonic-gate /* 12427c478bd9Sstevel@tonic-gate * if error, leave original file, delete new file 12437c478bd9Sstevel@tonic-gate * if noerr, replace original file with new file 12447c478bd9Sstevel@tonic-gate */ 12457c478bd9Sstevel@tonic-gate if (status == NOERR) { 12467c478bd9Sstevel@tonic-gate if (rename(minor_perm, tmphold) == -1) { 12477c478bd9Sstevel@tonic-gate perror(NULL); 12487c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_UPDATE), minor_perm); 12497c478bd9Sstevel@tonic-gate (void) unlink(newfile); 12507c478bd9Sstevel@tonic-gate return (ERROR); 12517c478bd9Sstevel@tonic-gate } else if (rename(newfile, minor_perm) == -1) { 12527c478bd9Sstevel@tonic-gate perror(NULL); 12537c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_UPDATE), minor_perm); 12547c478bd9Sstevel@tonic-gate (void) unlink(minor_perm); 12557c478bd9Sstevel@tonic-gate (void) unlink(newfile); 12567c478bd9Sstevel@tonic-gate if (link(tmphold, minor_perm) == -1) { 12577c478bd9Sstevel@tonic-gate perror(NULL); 12587c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_BAD_LINK), 12597c478bd9Sstevel@tonic-gate minor_perm, tmphold); 12607c478bd9Sstevel@tonic-gate } 12617c478bd9Sstevel@tonic-gate return (ERROR); 12627c478bd9Sstevel@tonic-gate } 12637c478bd9Sstevel@tonic-gate (void) unlink(tmphold); 12647c478bd9Sstevel@tonic-gate } else { 12657c478bd9Sstevel@tonic-gate /* 12667c478bd9Sstevel@tonic-gate * since there's an error, leave file alone; remove 12677c478bd9Sstevel@tonic-gate * new file 12687c478bd9Sstevel@tonic-gate */ 12697c478bd9Sstevel@tonic-gate if (unlink(newfile) == -1) { 12707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_CANT_RM), newfile); 12717c478bd9Sstevel@tonic-gate } 12727c478bd9Sstevel@tonic-gate return (ERROR); 12737c478bd9Sstevel@tonic-gate } 12747c478bd9Sstevel@tonic-gate 12757c478bd9Sstevel@tonic-gate return (NOERR); 12767c478bd9Sstevel@tonic-gate 12777c478bd9Sstevel@tonic-gate } 12787c478bd9Sstevel@tonic-gate 12797c478bd9Sstevel@tonic-gate 12807c478bd9Sstevel@tonic-gate /* 12817c478bd9Sstevel@tonic-gate * list_entry: 12827c478bd9Sstevel@tonic-gate * open file 12837c478bd9Sstevel@tonic-gate * read thru file, listing all entries if first entry = driver_name 12847c478bd9Sstevel@tonic-gate * close 12857c478bd9Sstevel@tonic-gate */ 12867c478bd9Sstevel@tonic-gate void 12877c478bd9Sstevel@tonic-gate list_entry( 12887c478bd9Sstevel@tonic-gate char *oldfile, 12897c478bd9Sstevel@tonic-gate char *driver_name, 12907c478bd9Sstevel@tonic-gate char *marker) 12917c478bd9Sstevel@tonic-gate { 12927c478bd9Sstevel@tonic-gate FILE *fp; 12937c478bd9Sstevel@tonic-gate int i; 1294*1ca93273Seota char line[MAX_DBFILE_ENTRY], *cp; 12957c478bd9Sstevel@tonic-gate char drv[FILENAME_MAX + 1]; 12967c478bd9Sstevel@tonic-gate 12977c478bd9Sstevel@tonic-gate if ((fp = fopen(oldfile, "r")) == NULL) { 12987c478bd9Sstevel@tonic-gate perror(NULL); 12997c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), oldfile); 13007c478bd9Sstevel@tonic-gate 13017c478bd9Sstevel@tonic-gate return; 13027c478bd9Sstevel@tonic-gate } 13037c478bd9Sstevel@tonic-gate 13047c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line), fp) != NULL) { 1305*1ca93273Seota /* cut off comments starting with '#' */ 1306*1ca93273Seota if ((cp = strchr(line, '#')) != NULL) 1307*1ca93273Seota *cp = '\0'; 1308*1ca93273Seota /* ignore comment or blank lines */ 1309*1ca93273Seota if (is_blank(line)) 13107c478bd9Sstevel@tonic-gate continue; 1311*1ca93273Seota /* sanity-check */ 13127c478bd9Sstevel@tonic-gate if (sscanf(line, "%s", drv) != 1) { 13137c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_BAD_LINE), 13147c478bd9Sstevel@tonic-gate oldfile, line); 13157c478bd9Sstevel@tonic-gate } 13167c478bd9Sstevel@tonic-gate 13177c478bd9Sstevel@tonic-gate for (i = strcspn(drv, marker); i < FILENAME_MAX; i++) { 13187c478bd9Sstevel@tonic-gate drv[i] = '\0'; 13197c478bd9Sstevel@tonic-gate } 13207c478bd9Sstevel@tonic-gate 13217c478bd9Sstevel@tonic-gate if (strcmp(driver_name, drv) == 0) { 13227c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%s", line); 13237c478bd9Sstevel@tonic-gate } 13247c478bd9Sstevel@tonic-gate } 13257c478bd9Sstevel@tonic-gate 13267c478bd9Sstevel@tonic-gate (void) fclose(fp); 13277c478bd9Sstevel@tonic-gate } 13287c478bd9Sstevel@tonic-gate 1329*1ca93273Seota static boolean_t 1330*1ca93273Seota is_token(char *tok) 1331*1ca93273Seota { 1332*1ca93273Seota /* 1333*1ca93273Seota * Check the token here. According to IEEE1275 Open Firmware Boot 1334*1ca93273Seota * Standard, the name is composed of 1 to 31 letters, 1335*1ca93273Seota * digits and punctuation characters from the set ",._+-", and 1336*1ca93273Seota * uppercase and lowercase characters are considered distinct. 1337*1ca93273Seota * (ie. token := [a-zA-Z0-9,._+-]+, length(token) <= 31) 1338*1ca93273Seota * However, since either the definition of driver or aliase names is 1339*1ca93273Seota * not known well, only '#' is avoided explicitly. (the kernel lexical 1340*1ca93273Seota * analyzer treats it as a start of a comment) 1341*1ca93273Seota */ 1342*1ca93273Seota for (/* nothing */; *tok != '\0'; tok++) 1343*1ca93273Seota if (*tok == '#' || iscntrl(*tok)) 1344*1ca93273Seota return (B_FALSE); 1345*1ca93273Seota 1346*1ca93273Seota return (B_TRUE); 1347*1ca93273Seota } 13487c478bd9Sstevel@tonic-gate 13497c478bd9Sstevel@tonic-gate /* 13507c478bd9Sstevel@tonic-gate * check each entry in perm_list for: 13517c478bd9Sstevel@tonic-gate * 4 arguments 13527c478bd9Sstevel@tonic-gate * permission arg is in valid range 13537c478bd9Sstevel@tonic-gate * permlist entries separated by comma 13547c478bd9Sstevel@tonic-gate * return ERROR/NOERR 13557c478bd9Sstevel@tonic-gate */ 13567c478bd9Sstevel@tonic-gate int 13577c478bd9Sstevel@tonic-gate check_perm_opts(char *perm_list) 13587c478bd9Sstevel@tonic-gate { 13597c478bd9Sstevel@tonic-gate char *current_head; 13607c478bd9Sstevel@tonic-gate char *previous_head; 13617c478bd9Sstevel@tonic-gate char *one_entry; 13627c478bd9Sstevel@tonic-gate int i, len, scan_stat; 13637c478bd9Sstevel@tonic-gate char minor[FILENAME_MAX + 1]; 13647c478bd9Sstevel@tonic-gate char perm[OPT_LEN + 1]; 13657c478bd9Sstevel@tonic-gate char own[OPT_LEN + 1]; 13667c478bd9Sstevel@tonic-gate char grp[OPT_LEN + 1]; 13677c478bd9Sstevel@tonic-gate char dumb[OPT_LEN + 1]; 13687c478bd9Sstevel@tonic-gate int status = NOERR; 13697c478bd9Sstevel@tonic-gate int intperm; 13707c478bd9Sstevel@tonic-gate 13717c478bd9Sstevel@tonic-gate len = strlen(perm_list); 13727c478bd9Sstevel@tonic-gate 13737c478bd9Sstevel@tonic-gate if (len == 0) { 13747c478bd9Sstevel@tonic-gate return (ERROR); 13757c478bd9Sstevel@tonic-gate } 13767c478bd9Sstevel@tonic-gate 13777c478bd9Sstevel@tonic-gate one_entry = calloc(len + 1, 1); 13787c478bd9Sstevel@tonic-gate if (one_entry == NULL) { 13797c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_MEM)); 13807c478bd9Sstevel@tonic-gate return (ERROR); 13817c478bd9Sstevel@tonic-gate } 13827c478bd9Sstevel@tonic-gate 13837c478bd9Sstevel@tonic-gate previous_head = perm_list; 13847c478bd9Sstevel@tonic-gate current_head = perm_list; 13857c478bd9Sstevel@tonic-gate 13867c478bd9Sstevel@tonic-gate while (*current_head != '\0') { 13877c478bd9Sstevel@tonic-gate 13887c478bd9Sstevel@tonic-gate for (i = 0; i <= len; i++) 13897c478bd9Sstevel@tonic-gate one_entry[i] = 0; 13907c478bd9Sstevel@tonic-gate 13917c478bd9Sstevel@tonic-gate current_head = get_entry(previous_head, one_entry, ','); 13927c478bd9Sstevel@tonic-gate 13937c478bd9Sstevel@tonic-gate previous_head = current_head; 13947c478bd9Sstevel@tonic-gate scan_stat = sscanf(one_entry, "%s%s%s%s%s", minor, perm, own, 13957c478bd9Sstevel@tonic-gate grp, dumb); 13967c478bd9Sstevel@tonic-gate 13977c478bd9Sstevel@tonic-gate if (scan_stat < 4) { 13987c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_MIS_TOK), 13997c478bd9Sstevel@tonic-gate "-m", one_entry); 14007c478bd9Sstevel@tonic-gate status = ERROR; 14017c478bd9Sstevel@tonic-gate } 14027c478bd9Sstevel@tonic-gate if (scan_stat > 4) { 14037c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_TOO_MANY_ARGS), 14047c478bd9Sstevel@tonic-gate "-m", one_entry); 14057c478bd9Sstevel@tonic-gate status = ERROR; 14067c478bd9Sstevel@tonic-gate } 14077c478bd9Sstevel@tonic-gate 14087c478bd9Sstevel@tonic-gate intperm = atoi(perm); 14097c478bd9Sstevel@tonic-gate if (intperm < 0000 || intperm > 4777) { 14107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_BAD_MODE), perm); 14117c478bd9Sstevel@tonic-gate status = ERROR; 14127c478bd9Sstevel@tonic-gate } 14137c478bd9Sstevel@tonic-gate } 14147c478bd9Sstevel@tonic-gate 14157c478bd9Sstevel@tonic-gate free(one_entry); 14167c478bd9Sstevel@tonic-gate return (status); 14177c478bd9Sstevel@tonic-gate } 14187c478bd9Sstevel@tonic-gate 14197c478bd9Sstevel@tonic-gate 14207c478bd9Sstevel@tonic-gate /* 14217c478bd9Sstevel@tonic-gate * check each alias : 14227c478bd9Sstevel@tonic-gate * alias list members separated by white space 14237c478bd9Sstevel@tonic-gate * cannot exist as driver name in /etc/name_to_major 14247c478bd9Sstevel@tonic-gate * cannot exist as driver or alias name in /etc/driver_aliases 14257c478bd9Sstevel@tonic-gate */ 14267c478bd9Sstevel@tonic-gate int 14277c478bd9Sstevel@tonic-gate aliases_unique(char *aliases) 14287c478bd9Sstevel@tonic-gate { 14297c478bd9Sstevel@tonic-gate char *current_head; 14307c478bd9Sstevel@tonic-gate char *previous_head; 14317c478bd9Sstevel@tonic-gate char *one_entry; 14327c478bd9Sstevel@tonic-gate int i, len; 14337c478bd9Sstevel@tonic-gate int is_unique; 14347c478bd9Sstevel@tonic-gate 14357c478bd9Sstevel@tonic-gate len = strlen(aliases); 14367c478bd9Sstevel@tonic-gate 14377c478bd9Sstevel@tonic-gate one_entry = calloc(len + 1, 1); 14387c478bd9Sstevel@tonic-gate if (one_entry == NULL) { 14397c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_MEM)); 14407c478bd9Sstevel@tonic-gate return (ERROR); 14417c478bd9Sstevel@tonic-gate } 14427c478bd9Sstevel@tonic-gate 14437c478bd9Sstevel@tonic-gate previous_head = aliases; 14447c478bd9Sstevel@tonic-gate 14457c478bd9Sstevel@tonic-gate do { 14467c478bd9Sstevel@tonic-gate for (i = 0; i <= len; i++) 14477c478bd9Sstevel@tonic-gate one_entry[i] = 0; 14487c478bd9Sstevel@tonic-gate 14497c478bd9Sstevel@tonic-gate current_head = get_entry(previous_head, one_entry, ' '); 14507c478bd9Sstevel@tonic-gate previous_head = current_head; 14517c478bd9Sstevel@tonic-gate 14527c478bd9Sstevel@tonic-gate if ((unique_driver_name(one_entry, name_to_major, 14537c478bd9Sstevel@tonic-gate &is_unique)) == ERROR) { 14547c478bd9Sstevel@tonic-gate free(one_entry); 14557c478bd9Sstevel@tonic-gate return (ERROR); 14567c478bd9Sstevel@tonic-gate } 14577c478bd9Sstevel@tonic-gate 14587c478bd9Sstevel@tonic-gate if (is_unique != UNIQUE) { 14597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_ALIAS_IN_NAM_MAJ), 14607c478bd9Sstevel@tonic-gate one_entry); 14617c478bd9Sstevel@tonic-gate free(one_entry); 14627c478bd9Sstevel@tonic-gate return (ERROR); 14637c478bd9Sstevel@tonic-gate } 14647c478bd9Sstevel@tonic-gate 14657c478bd9Sstevel@tonic-gate if (unique_drv_alias(one_entry) != NOERR) { 14667c478bd9Sstevel@tonic-gate free(one_entry); 14677c478bd9Sstevel@tonic-gate return (ERROR); 14687c478bd9Sstevel@tonic-gate } 14697c478bd9Sstevel@tonic-gate 1470*1ca93273Seota if (!is_token(one_entry)) { 1471*1ca93273Seota (void) fprintf(stderr, gettext(ERR_BAD_TOK), 1472*1ca93273Seota "-i", one_entry); 1473*1ca93273Seota free(one_entry); 1474*1ca93273Seota return (ERROR); 1475*1ca93273Seota } 1476*1ca93273Seota 14777c478bd9Sstevel@tonic-gate } while (*current_head != '\0'); 14787c478bd9Sstevel@tonic-gate 14797c478bd9Sstevel@tonic-gate free(one_entry); 14807c478bd9Sstevel@tonic-gate 14817c478bd9Sstevel@tonic-gate return (NOERR); 14827c478bd9Sstevel@tonic-gate 14837c478bd9Sstevel@tonic-gate } 14847c478bd9Sstevel@tonic-gate 14857c478bd9Sstevel@tonic-gate 14867c478bd9Sstevel@tonic-gate int 14877c478bd9Sstevel@tonic-gate update_driver_aliases( 14887c478bd9Sstevel@tonic-gate char *driver_name, 14897c478bd9Sstevel@tonic-gate char *aliases) 14907c478bd9Sstevel@tonic-gate { 14917c478bd9Sstevel@tonic-gate /* make call to update the aliases file */ 14927c478bd9Sstevel@tonic-gate return (append_to_file(driver_name, aliases, driver_aliases, ' ', " ")); 14937c478bd9Sstevel@tonic-gate 14947c478bd9Sstevel@tonic-gate } 14957c478bd9Sstevel@tonic-gate 14967c478bd9Sstevel@tonic-gate 14977c478bd9Sstevel@tonic-gate int 14987c478bd9Sstevel@tonic-gate unique_drv_alias(char *drv_alias) 14997c478bd9Sstevel@tonic-gate { 15007c478bd9Sstevel@tonic-gate FILE *fp; 15017c478bd9Sstevel@tonic-gate char drv[FILENAME_MAX + 1]; 1502*1ca93273Seota char line[MAX_N2M_ALIAS_LINE + 1], *cp; 15037c478bd9Sstevel@tonic-gate char alias[FILENAME_MAX + 1]; 15047c478bd9Sstevel@tonic-gate int status = NOERR; 15057c478bd9Sstevel@tonic-gate 15067c478bd9Sstevel@tonic-gate fp = fopen(driver_aliases, "r"); 15077c478bd9Sstevel@tonic-gate 15087c478bd9Sstevel@tonic-gate if (fp != NULL) { 15097c478bd9Sstevel@tonic-gate while ((fgets(line, sizeof (line), fp) != 0) && 15107c478bd9Sstevel@tonic-gate status != ERROR) { 1511*1ca93273Seota /* cut off comments starting with '#' */ 1512*1ca93273Seota if ((cp = strchr(line, '#')) != NULL) 1513*1ca93273Seota *cp = '\0'; 1514*1ca93273Seota /* ignore comment or blank lines */ 1515*1ca93273Seota if (is_blank(line)) 1516*1ca93273Seota continue; 1517*1ca93273Seota /* sanity-check */ 15187c478bd9Sstevel@tonic-gate if (sscanf(line, "%s %s", drv, alias) != 2) 15197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_BAD_LINE), 15207c478bd9Sstevel@tonic-gate driver_aliases, line); 15217c478bd9Sstevel@tonic-gate 15227c478bd9Sstevel@tonic-gate if ((strcmp(drv_alias, drv) == 0) || 15237c478bd9Sstevel@tonic-gate (strcmp(drv_alias, alias) == 0)) { 15247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 15257c478bd9Sstevel@tonic-gate gettext(ERR_ALIAS_IN_USE), 15267c478bd9Sstevel@tonic-gate drv_alias); 15277c478bd9Sstevel@tonic-gate status = ERROR; 15287c478bd9Sstevel@tonic-gate } 15297c478bd9Sstevel@tonic-gate } 15307c478bd9Sstevel@tonic-gate (void) fclose(fp); 15317c478bd9Sstevel@tonic-gate return (status); 15327c478bd9Sstevel@tonic-gate } else { 15337c478bd9Sstevel@tonic-gate perror(NULL); 15347c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_CANT_OPEN), driver_aliases); 15357c478bd9Sstevel@tonic-gate return (ERROR); 15367c478bd9Sstevel@tonic-gate } 15377c478bd9Sstevel@tonic-gate 15387c478bd9Sstevel@tonic-gate } 15397c478bd9Sstevel@tonic-gate 15407c478bd9Sstevel@tonic-gate 15417c478bd9Sstevel@tonic-gate /* 15427c478bd9Sstevel@tonic-gate * search for driver_name in first field of file file_name 15437c478bd9Sstevel@tonic-gate * searching name_to_major and driver_aliases: name separated from rest of 15447c478bd9Sstevel@tonic-gate * line by blank 15457c478bd9Sstevel@tonic-gate * if there return 15467c478bd9Sstevel@tonic-gate * else return 15477c478bd9Sstevel@tonic-gate */ 15487c478bd9Sstevel@tonic-gate int 15497c478bd9Sstevel@tonic-gate unique_driver_name(char *driver_name, char *file_name, 15507c478bd9Sstevel@tonic-gate int *is_unique) 15517c478bd9Sstevel@tonic-gate { 15527c478bd9Sstevel@tonic-gate int ret; 15537c478bd9Sstevel@tonic-gate 15547c478bd9Sstevel@tonic-gate if ((ret = get_major_no(driver_name, file_name)) == ERROR) { 15557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), 15567c478bd9Sstevel@tonic-gate file_name); 15577c478bd9Sstevel@tonic-gate } else { 15587c478bd9Sstevel@tonic-gate /* XXX */ 15597c478bd9Sstevel@tonic-gate /* check alias file for name collision */ 15607c478bd9Sstevel@tonic-gate if (unique_drv_alias(driver_name) == ERROR) { 15617c478bd9Sstevel@tonic-gate ret = ERROR; 15627c478bd9Sstevel@tonic-gate } else { 15637c478bd9Sstevel@tonic-gate if (ret != UNIQUE) 15647c478bd9Sstevel@tonic-gate *is_unique = NOT_UNIQUE; 15657c478bd9Sstevel@tonic-gate else 15667c478bd9Sstevel@tonic-gate *is_unique = ret; 15677c478bd9Sstevel@tonic-gate ret = NOERR; 15687c478bd9Sstevel@tonic-gate } 15697c478bd9Sstevel@tonic-gate } 15707c478bd9Sstevel@tonic-gate return (ret); 15717c478bd9Sstevel@tonic-gate } 15727c478bd9Sstevel@tonic-gate 15737c478bd9Sstevel@tonic-gate 15747c478bd9Sstevel@tonic-gate int 15757c478bd9Sstevel@tonic-gate check_space_within_quote(char *str) 15767c478bd9Sstevel@tonic-gate { 15777c478bd9Sstevel@tonic-gate register int i; 15787c478bd9Sstevel@tonic-gate register int len; 15797c478bd9Sstevel@tonic-gate int quoted = 0; 15807c478bd9Sstevel@tonic-gate 15817c478bd9Sstevel@tonic-gate len = strlen(str); 15827c478bd9Sstevel@tonic-gate for (i = 0; i < len; i++, str++) { 15837c478bd9Sstevel@tonic-gate if (*str == '"') { 15847c478bd9Sstevel@tonic-gate if (quoted == 0) 15857c478bd9Sstevel@tonic-gate quoted++; 15867c478bd9Sstevel@tonic-gate else 15877c478bd9Sstevel@tonic-gate quoted--; 15887c478bd9Sstevel@tonic-gate } else if (*str == ' ' && quoted) 15897c478bd9Sstevel@tonic-gate return (ERROR); 15907c478bd9Sstevel@tonic-gate } 15917c478bd9Sstevel@tonic-gate 15927c478bd9Sstevel@tonic-gate return (0); 15937c478bd9Sstevel@tonic-gate } 15947c478bd9Sstevel@tonic-gate 15957c478bd9Sstevel@tonic-gate 15967c478bd9Sstevel@tonic-gate /* 15977c478bd9Sstevel@tonic-gate * get major number 15987c478bd9Sstevel@tonic-gate * write driver_name major_num to name_to_major file 15997c478bd9Sstevel@tonic-gate * major_num returned in major_num 16007c478bd9Sstevel@tonic-gate * return success/failure 16017c478bd9Sstevel@tonic-gate */ 16027c478bd9Sstevel@tonic-gate int 16037c478bd9Sstevel@tonic-gate update_name_to_major(char *driver_name, major_t *major_num, int server) 16047c478bd9Sstevel@tonic-gate { 16057c478bd9Sstevel@tonic-gate char major[MAX_STR_MAJOR + 1]; 16067c478bd9Sstevel@tonic-gate struct stat buf; 16077c478bd9Sstevel@tonic-gate char *num_list; 16087c478bd9Sstevel@tonic-gate char drv_majnum_str[MAX_STR_MAJOR + 1]; 16097c478bd9Sstevel@tonic-gate int new_maj = -1; 16107c478bd9Sstevel@tonic-gate int i, tmp = 0, is_unique, have_rem_n2m = 0; 16117c478bd9Sstevel@tonic-gate int max_dev = 0; 16127c478bd9Sstevel@tonic-gate 16137c478bd9Sstevel@tonic-gate /* 16147c478bd9Sstevel@tonic-gate * if driver_name already in rem_name_to_major 16157c478bd9Sstevel@tonic-gate * delete entry from rem_nam_to_major 16167c478bd9Sstevel@tonic-gate * put entry into name_to_major 16177c478bd9Sstevel@tonic-gate */ 16187c478bd9Sstevel@tonic-gate 16197c478bd9Sstevel@tonic-gate if (stat(rem_name_to_major, &buf) == 0) { 16207c478bd9Sstevel@tonic-gate have_rem_n2m = 1; 16217c478bd9Sstevel@tonic-gate } 16227c478bd9Sstevel@tonic-gate 16237c478bd9Sstevel@tonic-gate if (have_rem_n2m) { 16247c478bd9Sstevel@tonic-gate if ((is_unique = get_major_no(driver_name, rem_name_to_major)) 16257c478bd9Sstevel@tonic-gate == ERROR) 16267c478bd9Sstevel@tonic-gate return (ERROR); 16277c478bd9Sstevel@tonic-gate 16287c478bd9Sstevel@tonic-gate /* 16297c478bd9Sstevel@tonic-gate * found a match in rem_name_to_major 16307c478bd9Sstevel@tonic-gate */ 16317c478bd9Sstevel@tonic-gate if (is_unique != UNIQUE) { 16327c478bd9Sstevel@tonic-gate char scratch[FILENAME_MAX]; 16337c478bd9Sstevel@tonic-gate 16347c478bd9Sstevel@tonic-gate /* 16357c478bd9Sstevel@tonic-gate * If there is a match in /etc/rem_name_to_major then 16367c478bd9Sstevel@tonic-gate * be paranoid: is that major number already in 16377c478bd9Sstevel@tonic-gate * /etc/name_to_major (potentially under another name)? 16387c478bd9Sstevel@tonic-gate */ 16397c478bd9Sstevel@tonic-gate if (get_driver_name(is_unique, name_to_major, 16407c478bd9Sstevel@tonic-gate scratch) != UNIQUE) { 16417c478bd9Sstevel@tonic-gate /* 16427c478bd9Sstevel@tonic-gate * nuke the rem_name_to_major entry-- it 16437c478bd9Sstevel@tonic-gate * isn't helpful. 16447c478bd9Sstevel@tonic-gate */ 16457c478bd9Sstevel@tonic-gate (void) delete_entry(rem_name_to_major, 16467c478bd9Sstevel@tonic-gate driver_name, " ", NULL); 16477c478bd9Sstevel@tonic-gate } else { 16487c478bd9Sstevel@tonic-gate (void) snprintf(major, sizeof (major), 16497c478bd9Sstevel@tonic-gate "%d", is_unique); 16507c478bd9Sstevel@tonic-gate 16517c478bd9Sstevel@tonic-gate if (append_to_file(driver_name, major, 16527c478bd9Sstevel@tonic-gate name_to_major, ' ', " ") == ERROR) { 16537c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 16547c478bd9Sstevel@tonic-gate gettext(ERR_NO_UPDATE), 16557c478bd9Sstevel@tonic-gate name_to_major); 16567c478bd9Sstevel@tonic-gate return (ERROR); 16577c478bd9Sstevel@tonic-gate } 16587c478bd9Sstevel@tonic-gate 16597c478bd9Sstevel@tonic-gate if (delete_entry(rem_name_to_major, 16607c478bd9Sstevel@tonic-gate driver_name, " ", NULL) == ERROR) { 16617c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 16627c478bd9Sstevel@tonic-gate gettext(ERR_DEL_ENTRY), driver_name, 16637c478bd9Sstevel@tonic-gate rem_name_to_major); 16647c478bd9Sstevel@tonic-gate return (ERROR); 16657c478bd9Sstevel@tonic-gate } 16667c478bd9Sstevel@tonic-gate 16677c478bd9Sstevel@tonic-gate /* found matching entry : no errors */ 16687c478bd9Sstevel@tonic-gate *major_num = is_unique; 16697c478bd9Sstevel@tonic-gate return (NOERR); 16707c478bd9Sstevel@tonic-gate } 16717c478bd9Sstevel@tonic-gate } 16727c478bd9Sstevel@tonic-gate } 16737c478bd9Sstevel@tonic-gate 16747c478bd9Sstevel@tonic-gate /* 16757c478bd9Sstevel@tonic-gate * Bugid: 1264079 16767c478bd9Sstevel@tonic-gate * In a server case (with -b option), we can't use modctl() to find 16777c478bd9Sstevel@tonic-gate * the maximum major number, we need to dig thru client's 16787c478bd9Sstevel@tonic-gate * /etc/name_to_major and /etc/rem_name_to_major for the max_dev. 16797c478bd9Sstevel@tonic-gate * 16807c478bd9Sstevel@tonic-gate * if (server) 16817c478bd9Sstevel@tonic-gate * get maximum major number thru (rem_)name_to_major file on client 16827c478bd9Sstevel@tonic-gate * else 16837c478bd9Sstevel@tonic-gate * get maximum major number allowable on current system using modctl 16847c478bd9Sstevel@tonic-gate */ 16857c478bd9Sstevel@tonic-gate if (server) { 16867c478bd9Sstevel@tonic-gate max_dev = 0; 16877c478bd9Sstevel@tonic-gate tmp = 0; 16887c478bd9Sstevel@tonic-gate 16897c478bd9Sstevel@tonic-gate max_dev = get_max_major(name_to_major); 16907c478bd9Sstevel@tonic-gate 16917c478bd9Sstevel@tonic-gate /* If rem_name_to_major exists, we need to check it too */ 16927c478bd9Sstevel@tonic-gate if (have_rem_n2m) { 16937c478bd9Sstevel@tonic-gate tmp = get_max_major(rem_name_to_major); 16947c478bd9Sstevel@tonic-gate 16957c478bd9Sstevel@tonic-gate /* 16967c478bd9Sstevel@tonic-gate * If name_to_major is missing, we can get max_dev from 16977c478bd9Sstevel@tonic-gate * /etc/rem_name_to_major. If both missing, bail out! 16987c478bd9Sstevel@tonic-gate */ 16997c478bd9Sstevel@tonic-gate if ((max_dev == ERROR) && (tmp == ERROR)) { 17007c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 17017c478bd9Sstevel@tonic-gate gettext(ERR_CANT_ACCESS_FILE), 17027c478bd9Sstevel@tonic-gate name_to_major); 17037c478bd9Sstevel@tonic-gate return (ERROR); 17047c478bd9Sstevel@tonic-gate } 17057c478bd9Sstevel@tonic-gate 17067c478bd9Sstevel@tonic-gate /* guard against bigger maj_num in rem_name_to_major */ 17077c478bd9Sstevel@tonic-gate if (tmp > max_dev) 17087c478bd9Sstevel@tonic-gate max_dev = tmp; 17097c478bd9Sstevel@tonic-gate } else { 17107c478bd9Sstevel@tonic-gate /* 17117c478bd9Sstevel@tonic-gate * If we can't get major from name_to_major file 17127c478bd9Sstevel@tonic-gate * and there is no /etc/rem_name_to_major file, 17137c478bd9Sstevel@tonic-gate * then we don't have a max_dev, bail out quick! 17147c478bd9Sstevel@tonic-gate */ 17157c478bd9Sstevel@tonic-gate if (max_dev == ERROR) 17167c478bd9Sstevel@tonic-gate return (ERROR); 17177c478bd9Sstevel@tonic-gate } 17187c478bd9Sstevel@tonic-gate 17197c478bd9Sstevel@tonic-gate /* 17207c478bd9Sstevel@tonic-gate * In case there is no more slack in current name_to_major 17217c478bd9Sstevel@tonic-gate * table, provide at least 1 extra entry so the add_drv can 17227c478bd9Sstevel@tonic-gate * succeed. Since only one add_drv process is allowed at one 17237c478bd9Sstevel@tonic-gate * time, and hence max_dev will be re-calculated each time 17247c478bd9Sstevel@tonic-gate * add_drv is ran, we don't need to worry about adding more 17257c478bd9Sstevel@tonic-gate * than 1 extra slot for max_dev. 17267c478bd9Sstevel@tonic-gate */ 17277c478bd9Sstevel@tonic-gate max_dev++; 17287c478bd9Sstevel@tonic-gate 17297c478bd9Sstevel@tonic-gate } else { 17307c478bd9Sstevel@tonic-gate if (modctl(MODRESERVED, NULL, &max_dev) < 0) { 17317c478bd9Sstevel@tonic-gate perror(NULL); 17327c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_MAX_MAJOR)); 17337c478bd9Sstevel@tonic-gate return (ERROR); 17347c478bd9Sstevel@tonic-gate } 17357c478bd9Sstevel@tonic-gate } 17367c478bd9Sstevel@tonic-gate 17377c478bd9Sstevel@tonic-gate /* 17387c478bd9Sstevel@tonic-gate * max_dev is really how many slots the kernel has allocated for 17397c478bd9Sstevel@tonic-gate * devices... [0 , maxdev-1], not the largest available device num. 17407c478bd9Sstevel@tonic-gate */ 17417c478bd9Sstevel@tonic-gate if ((num_list = calloc(max_dev, 1)) == NULL) { 17427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_MEM)); 17437c478bd9Sstevel@tonic-gate return (ERROR); 17447c478bd9Sstevel@tonic-gate } 17457c478bd9Sstevel@tonic-gate 17467c478bd9Sstevel@tonic-gate /* 17477c478bd9Sstevel@tonic-gate * Populate the num_list array 17487c478bd9Sstevel@tonic-gate */ 17497c478bd9Sstevel@tonic-gate if (fill_n2m_array(name_to_major, &num_list, &max_dev) != 0) { 17507c478bd9Sstevel@tonic-gate return (ERROR); 17517c478bd9Sstevel@tonic-gate } 17527c478bd9Sstevel@tonic-gate if (have_rem_n2m) { 17537c478bd9Sstevel@tonic-gate if (fill_n2m_array(rem_name_to_major, &num_list, &max_dev) != 0) 17547c478bd9Sstevel@tonic-gate return (ERROR); 17557c478bd9Sstevel@tonic-gate } 17567c478bd9Sstevel@tonic-gate 17577c478bd9Sstevel@tonic-gate /* find first free major number */ 17587c478bd9Sstevel@tonic-gate for (i = 0; i < max_dev; i++) { 17597c478bd9Sstevel@tonic-gate if (num_list[i] != 1) { 17607c478bd9Sstevel@tonic-gate new_maj = i; 17617c478bd9Sstevel@tonic-gate break; 17627c478bd9Sstevel@tonic-gate } 17637c478bd9Sstevel@tonic-gate } 17647c478bd9Sstevel@tonic-gate 17657c478bd9Sstevel@tonic-gate if (new_maj == -1) { 17667c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_FREE_MAJOR)); 17677c478bd9Sstevel@tonic-gate return (ERROR); 17687c478bd9Sstevel@tonic-gate } 17697c478bd9Sstevel@tonic-gate 17707c478bd9Sstevel@tonic-gate (void) sprintf(drv_majnum_str, "%d", new_maj); 17717c478bd9Sstevel@tonic-gate if (do_the_update(driver_name, drv_majnum_str) == ERROR) { 17727c478bd9Sstevel@tonic-gate return (ERROR); 17737c478bd9Sstevel@tonic-gate } 17747c478bd9Sstevel@tonic-gate 17757c478bd9Sstevel@tonic-gate *major_num = new_maj; 17767c478bd9Sstevel@tonic-gate return (NOERR); 17777c478bd9Sstevel@tonic-gate } 17787c478bd9Sstevel@tonic-gate 17797c478bd9Sstevel@tonic-gate 17807c478bd9Sstevel@tonic-gate int 17817c478bd9Sstevel@tonic-gate fill_n2m_array(char *filename, char **array, int *nelems) 17827c478bd9Sstevel@tonic-gate { 17837c478bd9Sstevel@tonic-gate FILE *fp; 1784*1ca93273Seota char line[MAX_N2M_ALIAS_LINE + 1], *cp; 17857c478bd9Sstevel@tonic-gate char drv[FILENAME_MAX + 1]; 17867c478bd9Sstevel@tonic-gate u_longlong_t dnum; 17877c478bd9Sstevel@tonic-gate major_t drv_majnum; 17887c478bd9Sstevel@tonic-gate 17897c478bd9Sstevel@tonic-gate /* 17907c478bd9Sstevel@tonic-gate * Read through the file, marking each major number found 17917c478bd9Sstevel@tonic-gate * order is not relevant 17927c478bd9Sstevel@tonic-gate */ 17937c478bd9Sstevel@tonic-gate if ((fp = fopen(filename, "r")) == NULL) { 17947c478bd9Sstevel@tonic-gate perror(NULL); 17957c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_CANT_ACCESS_FILE), filename); 17967c478bd9Sstevel@tonic-gate return (ERROR); 17977c478bd9Sstevel@tonic-gate } 17987c478bd9Sstevel@tonic-gate 17997c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line), fp) != 0) { 1800*1ca93273Seota /* cut off comments starting with '#' */ 1801*1ca93273Seota if ((cp = strchr(line, '#')) != NULL) 1802*1ca93273Seota *cp = '\0'; 1803*1ca93273Seota /* ignore comment or blank lines */ 1804*1ca93273Seota if (is_blank(line)) 1805*1ca93273Seota continue; 1806*1ca93273Seota /* sanity-check */ 18077c478bd9Sstevel@tonic-gate if (sscanf(line, "%s %llu", drv, &dnum) != 2) { 18087c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_BAD_LINE), 18097c478bd9Sstevel@tonic-gate filename, line); 18107c478bd9Sstevel@tonic-gate (void) fclose(fp); 18117c478bd9Sstevel@tonic-gate return (ERROR); 18127c478bd9Sstevel@tonic-gate } 18137c478bd9Sstevel@tonic-gate 18147c478bd9Sstevel@tonic-gate if (dnum > L_MAXMAJ32) { 18157c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_MAJ_TOOBIG), drv, 18167c478bd9Sstevel@tonic-gate dnum, filename, L_MAXMAJ32); 18177c478bd9Sstevel@tonic-gate continue; 18187c478bd9Sstevel@tonic-gate } 18197c478bd9Sstevel@tonic-gate /* 18207c478bd9Sstevel@tonic-gate * cast down to a major_t; we can be sure this is safe because 18217c478bd9Sstevel@tonic-gate * of the above range-check. 18227c478bd9Sstevel@tonic-gate */ 18237c478bd9Sstevel@tonic-gate drv_majnum = (major_t)dnum; 18247c478bd9Sstevel@tonic-gate 18257c478bd9Sstevel@tonic-gate if (drv_majnum >= *nelems) { 18267c478bd9Sstevel@tonic-gate /* 18277c478bd9Sstevel@tonic-gate * Allocate some more space, up to drv_majnum + 1 so 18287c478bd9Sstevel@tonic-gate * we can accomodate 0 through drv_majnum. 18297c478bd9Sstevel@tonic-gate * 18307c478bd9Sstevel@tonic-gate * Note that in the failure case, we leak all of the 18317c478bd9Sstevel@tonic-gate * old contents of array. It's ok, since we just 18327c478bd9Sstevel@tonic-gate * wind up exiting immediately anyway. 18337c478bd9Sstevel@tonic-gate */ 18347c478bd9Sstevel@tonic-gate *nelems = drv_majnum + 1; 18357c478bd9Sstevel@tonic-gate *array = realloc(*array, *nelems); 18367c478bd9Sstevel@tonic-gate if (*array == NULL) { 18377c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_MEM)); 18387c478bd9Sstevel@tonic-gate return (ERROR); 18397c478bd9Sstevel@tonic-gate } 18407c478bd9Sstevel@tonic-gate } 18417c478bd9Sstevel@tonic-gate (*array)[drv_majnum] = 1; 18427c478bd9Sstevel@tonic-gate } 18437c478bd9Sstevel@tonic-gate 18447c478bd9Sstevel@tonic-gate (void) fclose(fp); 18457c478bd9Sstevel@tonic-gate return (0); 18467c478bd9Sstevel@tonic-gate } 18477c478bd9Sstevel@tonic-gate 18487c478bd9Sstevel@tonic-gate 18497c478bd9Sstevel@tonic-gate int 18507c478bd9Sstevel@tonic-gate do_the_update(char *driver_name, char *major_number) 18517c478bd9Sstevel@tonic-gate { 18527c478bd9Sstevel@tonic-gate return (append_to_file(driver_name, major_number, name_to_major, 18537c478bd9Sstevel@tonic-gate ' ', " ")); 18547c478bd9Sstevel@tonic-gate } 1855*1ca93273Seota 1856*1ca93273Seota /* 1857*1ca93273Seota * is_blank() returns 1 (true) if a line specified is composed of 1858*1ca93273Seota * whitespace characters only. otherwise, it returns 0 (false). 1859*1ca93273Seota * 1860*1ca93273Seota * Note. the argument (line) must be null-terminated. 1861*1ca93273Seota */ 1862*1ca93273Seota static int 1863*1ca93273Seota is_blank(char *line) 1864*1ca93273Seota { 1865*1ca93273Seota for (/* nothing */; *line != '\0'; line++) 1866*1ca93273Seota if (!isspace(*line)) 1867*1ca93273Seota return (0); 1868*1ca93273Seota return (1); 1869*1ca93273Seota } 1870