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
51991Sheppo * Common Development and Distribution License (the "License").
61991Sheppo * 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 */
211991Sheppo
220Sstevel@tonic-gate /*
23*3941Svenki * Copyright 2007 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
44221Sla135387 static void mdesc_free(void *bufp, size_t size);
45*3941Svenki uint64_t *md_bufp;
46221Sla135387
470Sstevel@tonic-gate md_t *
mdesc_devinit(void)480Sstevel@tonic-gate mdesc_devinit(void)
490Sstevel@tonic-gate {
50*3941Svenki int fd;
510Sstevel@tonic-gate md_t *mdp;
52*3941Svenki size_t size;
530Sstevel@tonic-gate
54*3941Svenki /*
55*3941Svenki * We haven't finished using the previous MD/PRI info.
56*3941Svenki */
571991Sheppo if (md_bufp != NULL)
581991Sheppo return (NULL);
591991Sheppo
60*3941Svenki do {
61*3941Svenki if ((fd = open(MDESC_PATH, O_RDONLY, 0)) < 0)
62*3941Svenki break;
630Sstevel@tonic-gate
64*3941Svenki if (ioctl(fd, MDESCIOCGSZ, &size) < 0)
65*3941Svenki break;
66*3941Svenki if ((md_bufp = (uint64_t *)malloc(size)) == NULL) {
67*3941Svenki (void) close(fd);
68*3941Svenki break;
690Sstevel@tonic-gate }
700Sstevel@tonic-gate
71*3941Svenki /*
72*3941Svenki * A partial read is as bad as a failed read.
73*3941Svenki */
74*3941Svenki if (read(fd, md_bufp, size) != size) {
751991Sheppo free(md_bufp);
76*3941Svenki md_bufp = NULL;
770Sstevel@tonic-gate }
780Sstevel@tonic-gate
79*3941Svenki (void) close(fd);
80*3941Svenki /*LINTED: E_CONSTANT_CONDITION */
81*3941Svenki } while (0);
820Sstevel@tonic-gate
83*3941Svenki if (md_bufp) {
84*3941Svenki mdp = md_init_intern(md_bufp, malloc, mdesc_free);
85*3941Svenki if (mdp == NULL) {
86*3941Svenki free(md_bufp);
87*3941Svenki md_bufp = NULL;
88*3941Svenki }
89*3941Svenki } else
90*3941Svenki mdp = NULL;
910Sstevel@tonic-gate
920Sstevel@tonic-gate return (mdp);
930Sstevel@tonic-gate }
94221Sla135387
95221Sla135387 /*ARGSUSED*/
96221Sla135387 void
mdesc_free(void * bufp,size_t size)97221Sla135387 mdesc_free(void *bufp, size_t size)
98221Sla135387 {
991991Sheppo if (bufp)
1001991Sheppo free(bufp);
101221Sla135387 }
1021991Sheppo
1031991Sheppo void
mdesc_devfini(md_t * mdp)1041991Sheppo mdesc_devfini(md_t *mdp)
1051991Sheppo {
1061991Sheppo if (mdp)
1071991Sheppo (void) md_fini(mdp);
1081991Sheppo
1091991Sheppo if (md_bufp)
1101991Sheppo free(md_bufp);
1111991Sheppo md_bufp = NULL;
1121991Sheppo }
113