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