xref: /titanic_41/usr/src/cmd/fm/schemes/mem/mem_util.c (revision b7d3956b92a285d8dac2c7f5f7e28d2ef5347ef8)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <mem.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <sys/mem.h>
33 #include <fm/fmd_fmri.h>
34 
35 void
36 mem_strarray_free(char **arr, size_t dim)
37 {
38 	int i;
39 
40 	for (i = 0; i < dim; i++) {
41 		if (arr[i] != NULL)
42 			fmd_fmri_strfree(arr[i]);
43 	}
44 	fmd_fmri_free(arr, sizeof (char *) * dim);
45 }
46 
47 int
48 mem_page_cmd(int cmd, nvlist_t *nvl)
49 {
50 	mem_page_t mpage;
51 	char *fmribuf;
52 	size_t fmrisz;
53 	int fd, rc, err;
54 
55 	if ((fd = open("/dev/mem", O_RDONLY)) < 0)
56 		return (-1); /* errno is set for us */
57 
58 	if ((errno = nvlist_size(nvl, &fmrisz, NV_ENCODE_NATIVE)) != 0 ||
59 	    fmrisz > MEM_FMRI_MAX_BUFSIZE ||
60 	    (fmribuf = fmd_fmri_alloc(fmrisz)) == NULL) {
61 		(void) close(fd);
62 		return (-1); /* errno is set for us */
63 	}
64 
65 	if ((errno = nvlist_pack(nvl, &fmribuf, &fmrisz,
66 	    NV_ENCODE_NATIVE, 0)) != 0) {
67 		fmd_fmri_free(fmribuf, fmrisz);
68 		(void) close(fd);
69 		return (-1); /* errno is set for us */
70 	}
71 
72 	mpage.m_fmri = fmribuf;
73 	mpage.m_fmrisz = fmrisz;
74 
75 	if ((rc = ioctl(fd, cmd, &mpage)) < 0)
76 		err = errno;
77 
78 	fmd_fmri_free(fmribuf, fmrisz);
79 
80 	(void) close(fd);
81 
82 	if (rc < 0) {
83 		errno = err;
84 		return (-1);
85 	}
86 
87 	return (0);
88 }
89