summaryrefslogtreecommitdiff
path: root/usr/src/lib/varpd/libvarpd/common/libvarpd_prop.c
blob: 4d50fb372dbbb4952cf16822defdd5b17cf31339 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2015 Joyent, Inc.
 */

/*
 * varpd property management
 */

#include <libvarpd_impl.h>
#include <errno.h>
#include <strings.h>
#include <sys/mac.h>
#include <umem.h>

typedef struct varpd_prop_info {
	varpd_impl_t		*vprop_vip;
	varpd_instance_t	*vprop_instance;
	uint_t			vprop_type;
	uint_t			vprop_prot;
	uint32_t		vprop_defsize;
	uint32_t		vprop_psize;
	char			vprop_name[LIBVARPD_PROP_NAMELEN];
	uint8_t			vprop_default[LIBVARPD_PROP_SIZEMAX];
	uint8_t			vprop_poss[LIBVARPD_PROP_SIZEMAX];
} varpd_prop_info_t;

void
libvarpd_prop_set_name(varpd_prop_handle_t *phdl, const char *name)
{
	varpd_prop_info_t *infop = (varpd_prop_info_t *)phdl;
	(void) strlcpy(infop->vprop_name, name, OVERLAY_PROP_NAMELEN);
}

void
libvarpd_prop_set_prot(varpd_prop_handle_t *phdl, overlay_prop_prot_t perm)
{
	varpd_prop_info_t *infop = (varpd_prop_info_t *)phdl;
	infop->vprop_prot = perm;
}

void
libvarpd_prop_set_type(varpd_prop_handle_t *phdl, overlay_prop_type_t type)
{
	varpd_prop_info_t *infop = (varpd_prop_info_t *)phdl;
	infop->vprop_type = type;
}

int
libvarpd_prop_set_default(varpd_prop_handle_t *phdl, void *buf, ssize_t len)
{
	varpd_prop_info_t *infop = (varpd_prop_info_t *)phdl;

	if (len > LIBVARPD_PROP_SIZEMAX)
		return (E2BIG);

	if (len < 0)
		return (EOVERFLOW);

	bcopy(buf, infop->vprop_default, len);
	infop->vprop_defsize = len;
	return (0);
}

void
libvarpd_prop_set_nodefault(varpd_prop_handle_t *phdl)
{
	varpd_prop_info_t *infop = (varpd_prop_info_t *)phdl;

	infop->vprop_default[0] = '\0';
	infop->vprop_defsize = 0;
}

void
libvarpd_prop_set_range_uint32(varpd_prop_handle_t *phdl, uint32_t min,
    uint32_t max)
{
	varpd_prop_info_t *infop = (varpd_prop_info_t *)phdl;
	mac_propval_range_t *rangep = (mac_propval_range_t *)infop->vprop_poss;

	if (rangep->mpr_count != 0 && rangep->mpr_type != MAC_PROPVAL_UINT32)
		return;

	if (infop->vprop_psize + sizeof (mac_propval_uint32_range_t) >
	    sizeof (infop->vprop_poss))
		return;

	infop->vprop_psize += sizeof (mac_propval_uint32_range_t);
	rangep->mpr_count++;
	rangep->mpr_type = MAC_PROPVAL_UINT32;
	rangep->u.mpr_uint32[rangep->mpr_count-1].mpur_min = min;
	rangep->u.mpr_uint32[rangep->mpr_count-1].mpur_max = max;
}

void
libvarpd_prop_set_range_str(varpd_prop_handle_t *phdl, const char *str)
{
	varpd_prop_info_t *infop = (varpd_prop_info_t *)phdl;
	size_t len = strlen(str) + 1; /* Account for a null terminator */
	mac_propval_range_t *rangep = (mac_propval_range_t *)infop->vprop_poss;
	mac_propval_str_range_t *pstr = &rangep->u.mpr_str;

	if (rangep->mpr_count != 0 && rangep->mpr_type != MAC_PROPVAL_STR)
		return;

	if (infop->vprop_psize + len > sizeof (infop->vprop_poss))
		return;

	rangep->mpr_count++;
	rangep->mpr_type = MAC_PROPVAL_STR;
	(void) strlcpy((char *)&pstr->mpur_data[pstr->mpur_nextbyte], str,
	    sizeof (infop->vprop_poss) - infop->vprop_psize);
	pstr->mpur_nextbyte += len;
	infop->vprop_psize += len;
}

int
libvarpd_prop_handle_alloc(varpd_handle_t *vph, varpd_instance_handle_t *inst,
    varpd_prop_handle_t **phdlp)
{
	varpd_prop_info_t *infop;

	infop = umem_alloc(sizeof (varpd_prop_info_t), UMEM_DEFAULT);
	if (infop == NULL)
		return (ENOMEM);

	bzero(infop, sizeof (varpd_prop_info_t));
	infop->vprop_vip = (varpd_impl_t *)vph;
	infop->vprop_instance = (varpd_instance_t *)inst;

	*phdlp = (varpd_prop_handle_t *)infop;
	return (0);
}

void
libvarpd_prop_handle_free(varpd_prop_handle_t *phdl)
{
	umem_free(phdl, sizeof (varpd_prop_info_t));
}

int
libvarpd_prop_nprops(varpd_instance_handle_t *ihdl, uint_t *np)
{
	varpd_instance_t *instp = (varpd_instance_t *)ihdl;

	return (instp->vri_plugin->vpp_ops->vpo_nprops(instp->vri_private, np));
}

int
libvarpd_prop_info_fill(varpd_prop_handle_t *phdl, uint_t propid)
{
	varpd_prop_info_t *infop = (varpd_prop_info_t *)phdl;
	varpd_instance_t *instp = infop->vprop_instance;
	mac_propval_range_t *rangep = (mac_propval_range_t *)infop->vprop_poss;

	infop->vprop_psize = sizeof (mac_propval_range_t);
	bzero(rangep, sizeof (mac_propval_range_t));
	return (instp->vri_plugin->vpp_ops->vpo_propinfo(instp->vri_private,
	    propid, phdl));
}

int
libvarpd_prop_info(varpd_prop_handle_t *phdl, const char **namep,
    uint_t *typep, uint_t *protp, const void **defp, uint32_t *sizep,
    const mac_propval_range_t **possp)
{
	varpd_prop_info_t *infop = (varpd_prop_info_t *)phdl;
	if (namep != NULL)
		*namep = infop->vprop_name;
	if (typep != NULL)
		*typep = infop->vprop_type;
	if (protp != NULL)
		*protp = infop->vprop_prot;
	if (defp != NULL)
		*defp = infop->vprop_default;
	if (sizep != NULL)
		*sizep = infop->vprop_psize;
	if (possp != NULL)
		*possp = (mac_propval_range_t *)infop->vprop_poss;
	return (0);
}

int
libvarpd_prop_get(varpd_prop_handle_t *phdl, void *buf, uint32_t *sizep)
{
	varpd_prop_info_t *infop = (varpd_prop_info_t *)phdl;
	varpd_instance_t *instp = infop->vprop_instance;

	if (infop->vprop_name[0] == '\0')
		return (EINVAL);

	return (instp->vri_plugin->vpp_ops->vpo_getprop(instp->vri_private,
	    infop->vprop_name, buf, sizep));
}

int
libvarpd_prop_set(varpd_prop_handle_t *phdl, const void *buf, uint32_t size)
{
	varpd_prop_info_t *infop = (varpd_prop_info_t *)phdl;
	varpd_instance_t *instp = infop->vprop_instance;

	if (infop->vprop_name[0] == '\0')
		return (EINVAL);

	return (instp->vri_plugin->vpp_ops->vpo_setprop(instp->vri_private,
	    infop->vprop_name, buf, size));
}

void
libvarpd_prop_door_convert(const varpd_prop_handle_t *phdl,
    varpd_client_propinfo_arg_t *vcfap)
{
	const varpd_prop_info_t *infop = (const varpd_prop_info_t *)phdl;

	vcfap->vcfa_type = infop->vprop_type;
	vcfap->vcfa_prot = infop->vprop_prot;
	vcfap->vcfa_defsize = infop->vprop_defsize;
	vcfap->vcfa_psize = infop->vprop_psize;
	bcopy(infop->vprop_name, vcfap->vcfa_name, LIBVARPD_PROP_NAMELEN);
	bcopy(infop->vprop_default, vcfap->vcfa_default, LIBVARPD_PROP_SIZEMAX);
	bcopy(infop->vprop_poss, vcfap->vcfa_poss, LIBVARPD_PROP_SIZEMAX);
}