1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 30*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 31*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 32*7c478bd9Sstevel@tonic-gate #include <sys/openpromio.h> 33*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 34*7c478bd9Sstevel@tonic-gate #include <string.h> 35*7c478bd9Sstevel@tonic-gate #include <stdio.h> /* sprintf() */ 36*7c478bd9Sstevel@tonic-gate #include <unistd.h> 37*7c478bd9Sstevel@tonic-gate 38*7c478bd9Sstevel@tonic-gate /* 39*7c478bd9Sstevel@tonic-gate * opp_zalloc(): allocates and initializes a struct openpromio 40*7c478bd9Sstevel@tonic-gate * 41*7c478bd9Sstevel@tonic-gate * input: size_t: the size of the variable-length part of the openpromio 42*7c478bd9Sstevel@tonic-gate * const char *: an initial value for oprom_array, if non-NULL 43*7c478bd9Sstevel@tonic-gate * output: struct openpromio: the allocated, initialized openpromio 44*7c478bd9Sstevel@tonic-gate */ 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate static struct openpromio * 47*7c478bd9Sstevel@tonic-gate opp_zalloc(size_t size, const char *prop) 48*7c478bd9Sstevel@tonic-gate { 49*7c478bd9Sstevel@tonic-gate struct openpromio *opp = malloc(sizeof (struct openpromio) + size); 50*7c478bd9Sstevel@tonic-gate 51*7c478bd9Sstevel@tonic-gate if (opp != NULL) { 52*7c478bd9Sstevel@tonic-gate (void) memset(opp, 0, sizeof (struct openpromio) + size); 53*7c478bd9Sstevel@tonic-gate opp->oprom_size = size; 54*7c478bd9Sstevel@tonic-gate if (prop != NULL) 55*7c478bd9Sstevel@tonic-gate (void) strcpy(opp->oprom_array, prop); 56*7c478bd9Sstevel@tonic-gate } 57*7c478bd9Sstevel@tonic-gate return (opp); 58*7c478bd9Sstevel@tonic-gate } 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate /* 61*7c478bd9Sstevel@tonic-gate * goto_rootnode(): moves to the root of the devinfo tree 62*7c478bd9Sstevel@tonic-gate * 63*7c478bd9Sstevel@tonic-gate * input: int: an open descriptor to /dev/openprom 64*7c478bd9Sstevel@tonic-gate * output: int: nonzero on success 65*7c478bd9Sstevel@tonic-gate */ 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate static int 68*7c478bd9Sstevel@tonic-gate goto_rootnode(int prom_fd) 69*7c478bd9Sstevel@tonic-gate { 70*7c478bd9Sstevel@tonic-gate struct openpromio op = { sizeof (int), 0 }; 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate /* zero it explicitly since a union is involved */ 73*7c478bd9Sstevel@tonic-gate op.oprom_node = 0; 74*7c478bd9Sstevel@tonic-gate return (ioctl(prom_fd, OPROMNEXT, &op) == 0); 75*7c478bd9Sstevel@tonic-gate } 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate /* 78*7c478bd9Sstevel@tonic-gate * return_property(): returns the value of a given property 79*7c478bd9Sstevel@tonic-gate * 80*7c478bd9Sstevel@tonic-gate * input: int: an open descriptor to /dev/openprom 81*7c478bd9Sstevel@tonic-gate * const char *: the property to look for in the current devinfo node 82*7c478bd9Sstevel@tonic-gate * output: the value of that property (dynamically allocated) 83*7c478bd9Sstevel@tonic-gate */ 84*7c478bd9Sstevel@tonic-gate 85*7c478bd9Sstevel@tonic-gate static char * 86*7c478bd9Sstevel@tonic-gate return_property(int prom_fd, const char *prop) 87*7c478bd9Sstevel@tonic-gate { 88*7c478bd9Sstevel@tonic-gate int proplen; 89*7c478bd9Sstevel@tonic-gate char *result; 90*7c478bd9Sstevel@tonic-gate struct openpromio *opp = opp_zalloc(strlen(prop) + 1, prop); 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate if (opp == NULL) 93*7c478bd9Sstevel@tonic-gate return (NULL); 94*7c478bd9Sstevel@tonic-gate 95*7c478bd9Sstevel@tonic-gate if (ioctl(prom_fd, OPROMGETPROPLEN, opp) == -1) { 96*7c478bd9Sstevel@tonic-gate free(opp); 97*7c478bd9Sstevel@tonic-gate return (NULL); 98*7c478bd9Sstevel@tonic-gate } 99*7c478bd9Sstevel@tonic-gate 100*7c478bd9Sstevel@tonic-gate proplen = opp->oprom_len; 101*7c478bd9Sstevel@tonic-gate if (proplen > (strlen(prop) + 1)) { 102*7c478bd9Sstevel@tonic-gate free(opp); 103*7c478bd9Sstevel@tonic-gate opp = opp_zalloc(proplen, prop); 104*7c478bd9Sstevel@tonic-gate if (opp == NULL) 105*7c478bd9Sstevel@tonic-gate return (NULL); 106*7c478bd9Sstevel@tonic-gate } 107*7c478bd9Sstevel@tonic-gate 108*7c478bd9Sstevel@tonic-gate if (ioctl(prom_fd, OPROMGETPROP, opp) == -1) { 109*7c478bd9Sstevel@tonic-gate free(opp); 110*7c478bd9Sstevel@tonic-gate return (NULL); 111*7c478bd9Sstevel@tonic-gate } 112*7c478bd9Sstevel@tonic-gate 113*7c478bd9Sstevel@tonic-gate result = strdup(opp->oprom_array); 114*7c478bd9Sstevel@tonic-gate free(opp); 115*7c478bd9Sstevel@tonic-gate return (result); 116*7c478bd9Sstevel@tonic-gate } 117*7c478bd9Sstevel@tonic-gate 118*7c478bd9Sstevel@tonic-gate /* 119*7c478bd9Sstevel@tonic-gate * sanitize_class_id(): translates the class id into a canonical format, 120*7c478bd9Sstevel@tonic-gate * so that it can be used easily with dhcptab(4). 121*7c478bd9Sstevel@tonic-gate * 122*7c478bd9Sstevel@tonic-gate * input: char *: the class id to canonicalize 123*7c478bd9Sstevel@tonic-gate * output: void 124*7c478bd9Sstevel@tonic-gate */ 125*7c478bd9Sstevel@tonic-gate 126*7c478bd9Sstevel@tonic-gate static void 127*7c478bd9Sstevel@tonic-gate sanitize_class_id(char *src_ptr) 128*7c478bd9Sstevel@tonic-gate { 129*7c478bd9Sstevel@tonic-gate char *dst_ptr = src_ptr; 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate /* remove all spaces and change all commas to periods */ 132*7c478bd9Sstevel@tonic-gate while (*src_ptr != '\0') { 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate switch (*src_ptr) { 135*7c478bd9Sstevel@tonic-gate 136*7c478bd9Sstevel@tonic-gate case ' ': 137*7c478bd9Sstevel@tonic-gate break; 138*7c478bd9Sstevel@tonic-gate 139*7c478bd9Sstevel@tonic-gate case ',': 140*7c478bd9Sstevel@tonic-gate *dst_ptr++ = '.'; 141*7c478bd9Sstevel@tonic-gate break; 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate default: 144*7c478bd9Sstevel@tonic-gate *dst_ptr++ = *src_ptr; 145*7c478bd9Sstevel@tonic-gate break; 146*7c478bd9Sstevel@tonic-gate } 147*7c478bd9Sstevel@tonic-gate src_ptr++; 148*7c478bd9Sstevel@tonic-gate } 149*7c478bd9Sstevel@tonic-gate *dst_ptr = '\0'; 150*7c478bd9Sstevel@tonic-gate } 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate /* 153*7c478bd9Sstevel@tonic-gate * get_class_id(): retrieves the class id from the prom, then canonicalizes it 154*7c478bd9Sstevel@tonic-gate * 155*7c478bd9Sstevel@tonic-gate * input: void 156*7c478bd9Sstevel@tonic-gate * output: char *: the class id (dynamically allocated and sanitized) 157*7c478bd9Sstevel@tonic-gate */ 158*7c478bd9Sstevel@tonic-gate 159*7c478bd9Sstevel@tonic-gate char * 160*7c478bd9Sstevel@tonic-gate get_class_id(void) 161*7c478bd9Sstevel@tonic-gate { 162*7c478bd9Sstevel@tonic-gate int prom_fd; 163*7c478bd9Sstevel@tonic-gate char *name, *class_id = NULL; 164*7c478bd9Sstevel@tonic-gate size_t len; 165*7c478bd9Sstevel@tonic-gate 166*7c478bd9Sstevel@tonic-gate prom_fd = open("/dev/openprom", O_RDONLY); 167*7c478bd9Sstevel@tonic-gate if (prom_fd == -1) 168*7c478bd9Sstevel@tonic-gate return (NULL); 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate if (goto_rootnode(prom_fd) == 0) { 171*7c478bd9Sstevel@tonic-gate (void) close(prom_fd); 172*7c478bd9Sstevel@tonic-gate return (NULL); 173*7c478bd9Sstevel@tonic-gate } 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate /* 176*7c478bd9Sstevel@tonic-gate * the `name' property is the same as the result of `uname -i', modulo 177*7c478bd9Sstevel@tonic-gate * some stylistic issues we fix up via sanitize_class_id() below. 178*7c478bd9Sstevel@tonic-gate */ 179*7c478bd9Sstevel@tonic-gate 180*7c478bd9Sstevel@tonic-gate name = return_property(prom_fd, "name"); 181*7c478bd9Sstevel@tonic-gate (void) close(prom_fd); 182*7c478bd9Sstevel@tonic-gate if (name == NULL) 183*7c478bd9Sstevel@tonic-gate return (NULL); 184*7c478bd9Sstevel@tonic-gate 185*7c478bd9Sstevel@tonic-gate /* 186*7c478bd9Sstevel@tonic-gate * if the name is not prefixed with a vendor name, add "SUNW," to make 187*7c478bd9Sstevel@tonic-gate * it more likely to be globally unique; see PSARC/2004/674. 188*7c478bd9Sstevel@tonic-gate */ 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate if (strchr(name, ',') == NULL) { 191*7c478bd9Sstevel@tonic-gate len = strlen(name) + sizeof ("SUNW,"); 192*7c478bd9Sstevel@tonic-gate class_id = malloc(len); 193*7c478bd9Sstevel@tonic-gate if (class_id == NULL) { 194*7c478bd9Sstevel@tonic-gate free(name); 195*7c478bd9Sstevel@tonic-gate return (NULL); 196*7c478bd9Sstevel@tonic-gate } 197*7c478bd9Sstevel@tonic-gate (void) snprintf(class_id, len, "SUNW,%s", name); 198*7c478bd9Sstevel@tonic-gate free(name); 199*7c478bd9Sstevel@tonic-gate } else { 200*7c478bd9Sstevel@tonic-gate class_id = name; 201*7c478bd9Sstevel@tonic-gate } 202*7c478bd9Sstevel@tonic-gate 203*7c478bd9Sstevel@tonic-gate sanitize_class_id(class_id); 204*7c478bd9Sstevel@tonic-gate return (class_id); 205*7c478bd9Sstevel@tonic-gate } 206