1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <sys/types.h>
33 #include <alloca.h>
34 #include <sys/stat.h>
35 #include <malloc.h>
36 #include <fcntl.h>
37 #include <syslog.h>
38 #include <mdesc.h>
39 #include <string.h>
40 #include <errno.h>
41 
42 #define	MDESC_PATH	"/devices/pseudo/mdesc@0:mdesc"
43 #define	SIZE	8192
44 
45 md_t *
46 mdesc_devinit(void)
47 {
48 	int fh;
49 	uint8_t *bufp = NULL;
50 	int res;
51 	int size;
52 	int offset;
53 	md_t *mdp;
54 
55 	fh = open(MDESC_PATH, O_RDONLY, 0);
56 	if (fh < 0) {
57 		return (NULL);
58 	}
59 
60 	size = SIZE;	/* initial size */
61 	offset = 0;
62 
63 	bufp = malloc(size);
64 	if (NULL == bufp) {
65 		return (NULL);
66 	}
67 
68 		/* OK read until we get a EOF */
69 
70 	do {
71 		int len;
72 
73 		len = size - offset;
74 
75 		while (len < SIZE) {
76 			size += SIZE;
77 			bufp = realloc(bufp, size);
78 			if (NULL == bufp)
79 				return (NULL);
80 			len = size - offset;
81 		}
82 
83 		do {
84 			res = read(fh, bufp+offset, len);
85 		} while ((res < 0) && (errno == EAGAIN));
86 
87 		if (res < 0) {
88 			free(bufp);
89 			return (NULL);
90 		}
91 
92 		offset += res;
93 	} while (res > 0);
94 
95 	(void) close(fh);
96 
97 	bufp = realloc(bufp, offset);
98 	if (NULL == bufp)
99 		return (NULL);
100 
101 	mdp = md_init_intern((uint64_t *)bufp, malloc, free);
102 	if (NULL == mdp) {
103 		free(bufp);
104 		return (NULL);
105 	}
106 
107 	return (mdp);
108 }
109