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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <stdio.h> 300Sstevel@tonic-gate #include <unistd.h> 310Sstevel@tonic-gate #include <stdlib.h> 320Sstevel@tonic-gate #include <sys/types.h> 330Sstevel@tonic-gate #include <alloca.h> 340Sstevel@tonic-gate #include <sys/stat.h> 350Sstevel@tonic-gate #include <malloc.h> 360Sstevel@tonic-gate #include <fcntl.h> 370Sstevel@tonic-gate #include <syslog.h> 380Sstevel@tonic-gate #include <mdesc.h> 390Sstevel@tonic-gate #include <string.h> 400Sstevel@tonic-gate #include <errno.h> 410Sstevel@tonic-gate 420Sstevel@tonic-gate #define MDESC_PATH "/devices/pseudo/mdesc@0:mdesc" 430Sstevel@tonic-gate #define SIZE 8192 440Sstevel@tonic-gate 45*221Sla135387 static void mdesc_free(void *bufp, size_t size); 46*221Sla135387 470Sstevel@tonic-gate md_t * 480Sstevel@tonic-gate mdesc_devinit(void) 490Sstevel@tonic-gate { 500Sstevel@tonic-gate int fh; 510Sstevel@tonic-gate uint8_t *bufp = NULL; 520Sstevel@tonic-gate int res; 530Sstevel@tonic-gate int size; 540Sstevel@tonic-gate int offset; 550Sstevel@tonic-gate md_t *mdp; 560Sstevel@tonic-gate 570Sstevel@tonic-gate fh = open(MDESC_PATH, O_RDONLY, 0); 580Sstevel@tonic-gate if (fh < 0) { 590Sstevel@tonic-gate return (NULL); 600Sstevel@tonic-gate } 610Sstevel@tonic-gate 620Sstevel@tonic-gate size = SIZE; /* initial size */ 630Sstevel@tonic-gate offset = 0; 640Sstevel@tonic-gate 650Sstevel@tonic-gate bufp = malloc(size); 660Sstevel@tonic-gate if (NULL == bufp) { 670Sstevel@tonic-gate return (NULL); 680Sstevel@tonic-gate } 690Sstevel@tonic-gate 700Sstevel@tonic-gate /* OK read until we get a EOF */ 710Sstevel@tonic-gate 720Sstevel@tonic-gate do { 730Sstevel@tonic-gate int len; 740Sstevel@tonic-gate 750Sstevel@tonic-gate len = size - offset; 760Sstevel@tonic-gate 770Sstevel@tonic-gate while (len < SIZE) { 780Sstevel@tonic-gate size += SIZE; 790Sstevel@tonic-gate bufp = realloc(bufp, size); 800Sstevel@tonic-gate if (NULL == bufp) 810Sstevel@tonic-gate return (NULL); 820Sstevel@tonic-gate len = size - offset; 830Sstevel@tonic-gate } 840Sstevel@tonic-gate 850Sstevel@tonic-gate do { 860Sstevel@tonic-gate res = read(fh, bufp+offset, len); 870Sstevel@tonic-gate } while ((res < 0) && (errno == EAGAIN)); 880Sstevel@tonic-gate 890Sstevel@tonic-gate if (res < 0) { 900Sstevel@tonic-gate free(bufp); 910Sstevel@tonic-gate return (NULL); 920Sstevel@tonic-gate } 930Sstevel@tonic-gate 940Sstevel@tonic-gate offset += res; 950Sstevel@tonic-gate } while (res > 0); 960Sstevel@tonic-gate 970Sstevel@tonic-gate (void) close(fh); 980Sstevel@tonic-gate 990Sstevel@tonic-gate bufp = realloc(bufp, offset); 1000Sstevel@tonic-gate if (NULL == bufp) 1010Sstevel@tonic-gate return (NULL); 1020Sstevel@tonic-gate 103*221Sla135387 mdp = md_init_intern((uint64_t *)bufp, malloc, mdesc_free); 1040Sstevel@tonic-gate if (NULL == mdp) { 1050Sstevel@tonic-gate free(bufp); 1060Sstevel@tonic-gate return (NULL); 1070Sstevel@tonic-gate } 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate return (mdp); 1100Sstevel@tonic-gate } 111*221Sla135387 112*221Sla135387 /*ARGSUSED*/ 113*221Sla135387 void 114*221Sla135387 mdesc_free(void *bufp, size_t size) 115*221Sla135387 { 116*221Sla135387 free(bufp); 117*221Sla135387 } 118