xref: /illumos-gate/usr/src/cmd/fm/schemes/mem/mem_util.c (revision 76716eaced8d7659d4594350eb3f343c31fe2806)
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 <mem.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <sys/mem.h>
34 #include <fm/fmd_fmri.h>
35 
36 void
37 mem_strarray_free(char **arr, size_t dim)
38 {
39 	int i;
40 
41 	for (i = 0; i < dim; i++) {
42 		if (arr[i] != NULL)
43 			fmd_fmri_strfree(arr[i]);
44 	}
45 	fmd_fmri_free(arr, sizeof (char *) * dim);
46 }
47 
48 int
49 mem_page_cmd(int cmd, nvlist_t *nvl)
50 {
51 	mem_page_t mpage;
52 	char *fmribuf;
53 	size_t fmrisz;
54 	int fd, rc, err;
55 
56 	if ((fd = open("/dev/mem", O_RDONLY)) < 0)
57 		return (-1); /* errno is set for us */
58 
59 	if ((errno = nvlist_size(nvl, &fmrisz, NV_ENCODE_NATIVE)) != 0 ||
60 	    fmrisz > MEM_FMRI_MAX_BUFSIZE ||
61 	    (fmribuf = fmd_fmri_alloc(fmrisz)) == NULL) {
62 		(void) close(fd);
63 		return (-1); /* errno is set for us */
64 	}
65 
66 	if ((errno = nvlist_pack(nvl, &fmribuf, &fmrisz,
67 	    NV_ENCODE_NATIVE, 0)) != 0) {
68 		fmd_fmri_free(fmribuf, fmrisz);
69 		(void) close(fd);
70 		return (-1); /* errno is set for us */
71 	}
72 
73 	mpage.m_fmri = fmribuf;
74 	mpage.m_fmrisz = fmrisz;
75 
76 	if ((rc = ioctl(fd, cmd, &mpage)) < 0)
77 		err = errno;
78 
79 	fmd_fmri_free(fmribuf, fmrisz);
80 
81 	(void) close(fd);
82 
83 	if (rc < 0) {
84 		errno = err;
85 		return (-1);
86 	}
87 
88 	return (0);
89 }
90