xref: /illumos-gate/usr/src/lib/libcontract/common/device.c (revision 1fceb383a3f0b59711832b9dc4e8329d7f216604)
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 2007 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 <sys/ctfs.h>
29 #include <sys/contract.h>
30 #include <sys/contract/device.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <libnvpair.h>
35 #include <limits.h>
36 #include <sys/stat.h>
37 #include <libcontract.h>
38 #include "libcontract_impl.h"
39 
40 /*
41  * Device contract template routines
42  */
43 
44 int
45 ct_dev_tmpl_set_minor(int fd, char *minor)
46 {
47 	return (ct_tmpl_set_internal(fd, CTDP_MINOR, (uintptr_t)minor));
48 }
49 
50 int
51 ct_dev_tmpl_set_aset(int fd, uint_t aset)
52 {
53 	return (ct_tmpl_set_internal(fd, CTDP_ACCEPT, aset));
54 }
55 
56 int
57 ct_dev_tmpl_set_noneg(int fd)
58 {
59 	return (ct_tmpl_set_internal(fd, CTDP_NONEG, CTDP_NONEG_SET));
60 }
61 
62 int
63 ct_dev_tmpl_clear_noneg(int fd)
64 {
65 	return (ct_tmpl_set_internal(fd, CTDP_NONEG, CTDP_NONEG_CLEAR));
66 }
67 
68 int
69 ct_dev_tmpl_get_minor(int fd, char *buf, size_t *buflenp)
70 {
71 	char	path[PATH_MAX];
72 	int	error;
73 	size_t	len;
74 
75 	error = ct_tmpl_get_internal_string(fd, CTDP_MINOR, path);
76 	if (error) {
77 		return (error);
78 	}
79 
80 	len = strlcpy(buf, path, *buflenp);
81 	if (len >= *buflenp) {
82 		*buflenp = len + 1;
83 		return (EOVERFLOW);
84 	}
85 
86 	return (0);
87 }
88 
89 int
90 ct_dev_tmpl_get_aset(int fd, uint_t *aset)
91 {
92 	return (ct_tmpl_get_internal(fd, CTDP_ACCEPT, aset));
93 }
94 
95 int
96 ct_dev_tmpl_get_noneg(int fd, uint_t *negp)
97 {
98 	return (ct_tmpl_get_internal(fd, CTDP_NONEG, negp));
99 }
100 
101 /*
102  * Device contract event routines
103  */
104 
105 /*
106  * No device contract specific event routines
107  */
108 
109 
110 /*
111  * Device contract status routines
112  */
113 
114 int
115 ct_dev_status_get_aset(ct_stathdl_t stathdl, uint_t *aset)
116 {
117 	struct ctlib_status_info *info = stathdl;
118 
119 	if (info->status.ctst_type != CTT_DEVICE)
120 		return (EINVAL);
121 
122 	if (info->nvl == NULL)
123 		return (ENOENT);
124 
125 	return (nvlist_lookup_uint32(info->nvl, CTDS_ASET, aset));
126 }
127 
128 int
129 ct_dev_status_get_noneg(ct_stathdl_t stathdl, uint_t *negp)
130 {
131 	struct ctlib_status_info *info = stathdl;
132 
133 	if (info->status.ctst_type != CTT_DEVICE)
134 		return (EINVAL);
135 
136 	if (info->nvl == NULL)
137 		return (ENOENT);
138 
139 	return (nvlist_lookup_uint32(info->nvl, CTDS_NONEG, negp));
140 }
141 
142 int
143 ct_dev_status_get_dev_state(ct_stathdl_t stathdl, uint_t *statep)
144 {
145 	struct ctlib_status_info *info = stathdl;
146 
147 	if (info->status.ctst_type != CTT_DEVICE)
148 		return (EINVAL);
149 
150 	if (info->nvl == NULL)
151 		return (ENOENT);
152 
153 	return (nvlist_lookup_uint32(info->nvl, CTDS_STATE, statep));
154 }
155 
156 int
157 ct_dev_status_get_minor(ct_stathdl_t stathdl, char **bufp)
158 {
159 	int error;
160 	struct ctlib_status_info *info = stathdl;
161 
162 	if (bufp == NULL)
163 		return (EINVAL);
164 
165 	if (info->status.ctst_type != CTT_DEVICE)
166 		return (EINVAL);
167 
168 	if (info->nvl == NULL)
169 		return (ENOENT);
170 
171 	error = nvlist_lookup_string(info->nvl, CTDS_MINOR, bufp);
172 	if (error != 0) {
173 		return (error);
174 	}
175 
176 	return (0);
177 }
178