10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52805Seota * Common Development and Distribution License (the "License"). 62805Seota * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 222805Seota * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <stdio.h> 300Sstevel@tonic-gate #include <ctype.h> 310Sstevel@tonic-gate #include <sys/types.h> 320Sstevel@tonic-gate #include <sys/mkdev.h> 330Sstevel@tonic-gate #include <sys/stat.h> 340Sstevel@tonic-gate #include <unistd.h> 350Sstevel@tonic-gate #include <dirent.h> 360Sstevel@tonic-gate #include <limits.h> 370Sstevel@tonic-gate #include <string.h> 380Sstevel@tonic-gate #include <libsvm.h> 390Sstevel@tonic-gate #include <svm.h> 400Sstevel@tonic-gate #include <errno.h> 410Sstevel@tonic-gate 420Sstevel@tonic-gate 430Sstevel@tonic-gate #define VERSION "1.0" 440Sstevel@tonic-gate #define DISK_DIR "/dev/rdsk" 450Sstevel@tonic-gate 460Sstevel@tonic-gate extern int _map_to_effective_dev(); 470Sstevel@tonic-gate 482805Seota static int is_blank(char *); 492805Seota 502805Seota /* 512805Seota * is_blank() returns 1 (true) if a line specified is composed of 522805Seota * whitespace characters only. otherwise, it returns 0 (false). 532805Seota * 542805Seota * Note. the argument (line) must be null-terminated. 552805Seota */ 562805Seota static int 572805Seota is_blank(char *line) 580Sstevel@tonic-gate { 592805Seota for (/* nothing */; *line != '\0'; line++) 602805Seota if (!isspace(*line)) 610Sstevel@tonic-gate return (0); 620Sstevel@tonic-gate return (1); 630Sstevel@tonic-gate } 640Sstevel@tonic-gate 650Sstevel@tonic-gate /* 660Sstevel@tonic-gate * FUNCTION: write_targ_nm_table 670Sstevel@tonic-gate * creates a tuple table of <driver name, major number > in md.conf 680Sstevel@tonic-gate * INPUT: rootpath 690Sstevel@tonic-gate * 700Sstevel@tonic-gate * RETURN VALUES: 710Sstevel@tonic-gate * RET_SUCCESS 720Sstevel@tonic-gate * RET_ERROR 730Sstevel@tonic-gate */ 740Sstevel@tonic-gate 750Sstevel@tonic-gate int 760Sstevel@tonic-gate write_targ_nm_table(char *path) 770Sstevel@tonic-gate { 780Sstevel@tonic-gate FILE *targfp = NULL; 790Sstevel@tonic-gate FILE *mdfp = NULL; 800Sstevel@tonic-gate char buf[PATH_MAX], *cp; 810Sstevel@tonic-gate int retval = RET_SUCCESS; 820Sstevel@tonic-gate int first_entry = 1; 830Sstevel@tonic-gate 840Sstevel@tonic-gate if ((mdfp = fopen(MD_CONF, "a")) == NULL) 850Sstevel@tonic-gate return (RET_ERROR); 860Sstevel@tonic-gate 870Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s%s", path, NAME_TO_MAJOR); 880Sstevel@tonic-gate 890Sstevel@tonic-gate if ((targfp = fopen(buf, "r")) == NULL) { 900Sstevel@tonic-gate (void) fclose(mdfp); 910Sstevel@tonic-gate return (RET_ERROR); 920Sstevel@tonic-gate } 930Sstevel@tonic-gate 940Sstevel@tonic-gate while (fgets(buf, PATH_MAX, targfp) != NULL && 950Sstevel@tonic-gate (retval == RET_SUCCESS)) { 96*3036Seota /* remove a new-line character for md_targ_nm_table */ 97*3036Seota if ((cp = strchr(buf, '\n')) != NULL) 98*3036Seota *cp = 0; 992805Seota /* cut off comments starting with '#' */ 1002805Seota if ((cp = strchr(buf, '#')) != NULL) 1012805Seota *cp = 0; 1022805Seota /* ignore comment or blank lines */ 1032805Seota if (is_blank(buf)) 1040Sstevel@tonic-gate continue; 1050Sstevel@tonic-gate if (first_entry) { 1060Sstevel@tonic-gate if (fprintf(mdfp, "md_targ_nm_table=\"%s\"", buf) < 0) 1070Sstevel@tonic-gate retval = RET_ERROR; 1080Sstevel@tonic-gate first_entry = 0; 109*3036Seota } else { 110*3036Seota if (fprintf(mdfp, ",\"%s\"", buf) < 0) 111*3036Seota retval = RET_ERROR; 1120Sstevel@tonic-gate } 1130Sstevel@tonic-gate } 1140Sstevel@tonic-gate if (!first_entry) 1150Sstevel@tonic-gate if (fprintf(mdfp, ";\n") < 0) 1160Sstevel@tonic-gate retval = RET_ERROR; 1170Sstevel@tonic-gate (void) fclose(mdfp); 1180Sstevel@tonic-gate (void) fclose(targfp); 1190Sstevel@tonic-gate return (retval); 1200Sstevel@tonic-gate } 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate /* 1230Sstevel@tonic-gate * FUNCTION: write_xlate_to_mdconf 1240Sstevel@tonic-gate * creates a tuple table of <miniroot devt, target devt> in md.conf 1250Sstevel@tonic-gate * INPUT: rootpath 1260Sstevel@tonic-gate * 1270Sstevel@tonic-gate * RETURN VALUES: 1280Sstevel@tonic-gate * RET_SUCCESS 1290Sstevel@tonic-gate * RET_ERROR 1300Sstevel@tonic-gate */ 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate int 1330Sstevel@tonic-gate write_xlate_to_mdconf(char *path) 1340Sstevel@tonic-gate { 1350Sstevel@tonic-gate FILE *fptr = NULL; 1360Sstevel@tonic-gate struct dirent *dp; 1370Sstevel@tonic-gate DIR *dirp; 1380Sstevel@tonic-gate struct stat statb_dev; 1390Sstevel@tonic-gate struct stat statb_edev; 1400Sstevel@tonic-gate char *devname; 1410Sstevel@tonic-gate char edevname[PATH_MAX]; 1420Sstevel@tonic-gate char targname[PATH_MAX]; 1430Sstevel@tonic-gate char diskdir[PATH_MAX]; 1440Sstevel@tonic-gate int first_devid = 1; 1450Sstevel@tonic-gate int ret = RET_SUCCESS; 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate if ((fptr = fopen(MD_CONF, "a")) == NULL) { 1480Sstevel@tonic-gate return (RET_ERROR); 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate (void) snprintf(diskdir, sizeof (diskdir), "%s%s", path, DISK_DIR); 1530Sstevel@tonic-gate if ((dirp = opendir(diskdir)) == NULL) { 1540Sstevel@tonic-gate (void) fclose(fptr); 1550Sstevel@tonic-gate return (RET_ERROR); 1560Sstevel@tonic-gate } 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate /* special case to write the first tuple in the table */ 1590Sstevel@tonic-gate while (((dp = readdir(dirp)) != (struct dirent *)0) && 1600Sstevel@tonic-gate (ret != RET_ERROR)) { 1610Sstevel@tonic-gate if ((strcmp(dp->d_name, ".") == 0) || 1620Sstevel@tonic-gate (strcmp(dp->d_name, "..") == 0)) 1630Sstevel@tonic-gate continue; 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate if ((strlen(diskdir) + strlen(dp->d_name) + 2) > PATH_MAX) { 1660Sstevel@tonic-gate continue; 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate (void) snprintf(targname, sizeof (targname), "%s/%s", 1700Sstevel@tonic-gate diskdir, dp->d_name); 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate if (stat(targname, &statb_dev) != 0) { 1730Sstevel@tonic-gate continue; 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate if ((devname = strstr(targname, DISK_DIR)) == NULL) { 1770Sstevel@tonic-gate continue; 1780Sstevel@tonic-gate } 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate if (_map_to_effective_dev((char *)devname, (char *)&edevname) 1810Sstevel@tonic-gate != 0) { 1820Sstevel@tonic-gate continue; 1830Sstevel@tonic-gate } 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate if (stat(edevname, &statb_edev) != 0) { 1860Sstevel@tonic-gate continue; 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate if (first_devid) { 1900Sstevel@tonic-gate if (fprintf(fptr, "md_xlate_ver=\"%s\";\n" 1910Sstevel@tonic-gate "md_xlate=%lu,%lu", VERSION, 1920Sstevel@tonic-gate statb_edev.st_rdev, statb_dev.st_rdev) < 0) 1930Sstevel@tonic-gate ret = RET_ERROR; 1940Sstevel@tonic-gate first_devid = 0; 1950Sstevel@tonic-gate } 1960Sstevel@tonic-gate if (fprintf(fptr, ",%lu,%lu", statb_edev.st_rdev, 1970Sstevel@tonic-gate statb_dev.st_rdev) < 0) 1980Sstevel@tonic-gate ret = RET_ERROR; 1990Sstevel@tonic-gate } /* end while */ 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate if (!first_devid) 2020Sstevel@tonic-gate if (fprintf(fptr, ";\n") < 0) 2030Sstevel@tonic-gate ret = RET_ERROR; 2040Sstevel@tonic-gate (void) fclose(fptr); 2050Sstevel@tonic-gate (void) closedir(dirp); 2060Sstevel@tonic-gate return (ret); 2070Sstevel@tonic-gate } 208