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