Print this page
195 Need replacement for nfs/lockd+klm
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/fs.d/nfs/lib/smfcfg.c
+++ new/usr/src/cmd/fs.d/nfs/lib/smfcfg.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
↓ open down ↓ |
10 lines elided |
↑ open up ↑ |
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 +
21 22 /*
22 23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 + * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
23 25 */
24 26 #include <stdio.h>
25 27 #include <stdlib.h>
26 28 #include <syslog.h>
27 29 #include <stdarg.h>
28 30 #include "smfcfg.h"
29 31
30 32 fs_smfhandle_t *
31 33 fs_smf_init(char *fmri, char *instance)
32 34 {
33 35 fs_smfhandle_t *handle = NULL;
34 36 char *svcname, srv[MAXPATHLEN];
35 37
36 38 /*
37 39 * svc name is of the form svc://network/fs/server:instance1
38 40 * FMRI portion is /network/fs/server
39 41 */
40 42 snprintf(srv, MAXPATHLEN, "%s", fmri + strlen("svc:/"));
41 43 svcname = strrchr(srv, ':');
42 44 if (svcname != NULL)
43 45 *svcname = '\0';
44 46 svcname = srv;
45 47
46 48 handle = calloc(1, sizeof (fs_smfhandle_t));
47 49 if (handle != NULL) {
48 50 handle->fs_handle = scf_handle_create(SCF_VERSION);
49 51 if (handle->fs_handle == NULL)
50 52 goto out;
51 53 if (scf_handle_bind(handle->fs_handle) != 0)
52 54 goto out;
53 55 handle->fs_service =
54 56 scf_service_create(handle->fs_handle);
55 57 handle->fs_scope =
56 58 scf_scope_create(handle->fs_handle);
57 59 if (scf_handle_get_local_scope(handle->fs_handle,
58 60 handle->fs_scope) != 0)
59 61 goto out;
60 62 if (scf_scope_get_service(handle->fs_scope,
61 63 svcname, handle->fs_service) != SCF_SUCCESS) {
62 64 goto out;
63 65 }
64 66 handle->fs_pg =
65 67 scf_pg_create(handle->fs_handle);
66 68 handle->fs_instance =
67 69 scf_instance_create(handle->fs_handle);
68 70 handle->fs_property =
69 71 scf_property_create(handle->fs_handle);
70 72 handle->fs_value =
71 73 scf_value_create(handle->fs_handle);
72 74 } else {
73 75 fprintf(stderr,
74 76 gettext("Cannot access SMF repository: %s\n"), fmri);
75 77 }
76 78 return (handle);
77 79
78 80 out:
79 81 fs_smf_fini(handle);
80 82 fprintf(stderr, gettext("SMF Initialization problems..%s\n"), fmri);
81 83 return (NULL);
82 84 }
83 85
84 86
85 87 void
86 88 fs_smf_fini(fs_smfhandle_t *handle)
87 89 {
88 90 if (handle != NULL) {
89 91 scf_scope_destroy(handle->fs_scope);
90 92 scf_instance_destroy(handle->fs_instance);
91 93 scf_service_destroy(handle->fs_service);
92 94 scf_pg_destroy(handle->fs_pg);
93 95 scf_property_destroy(handle->fs_property);
94 96 scf_value_destroy(handle->fs_value);
95 97 if (handle->fs_handle != NULL) {
96 98 scf_handle_unbind(handle->fs_handle);
97 99 scf_handle_destroy(handle->fs_handle);
98 100 }
99 101 free(handle);
100 102 }
101 103 }
102 104
103 105 int
104 106 fs_smf_set_prop(smf_fstype_t fstype, char *prop_name, char *valbuf,
105 107 char *instance, scf_type_t sctype, char *fmri)
106 108 {
107 109 fs_smfhandle_t *phandle = NULL;
108 110 scf_handle_t *handle;
109 111 scf_propertygroup_t *pg;
110 112 scf_property_t *prop;
111 113 scf_transaction_t *tran = NULL;
112 114 scf_transaction_entry_t *entry = NULL;
113 115 scf_instance_t *inst;
114 116 scf_value_t *val;
115 117 int valint;
116 118 int index = 0;
117 119 int ret = 0;
118 120 char *p = NULL;
119 121 char *svcname, srv[MAXPATHLEN];
120 122 const char *pgname;
121 123
122 124 /*
123 125 * The SVC names we are using currently are already
124 126 * appended by default. Fix this for instances project.
125 127 */
126 128 snprintf(srv, MAXPATHLEN, "%s", fmri);
127 129 p = strstr(fmri, ":default");
128 130 if (p == NULL) {
129 131 strcat(srv, ":");
130 132 if (instance == NULL)
131 133 instance = "default";
132 134 if (strlen(srv) + strlen(instance) > MAXPATHLEN)
133 135 goto out;
134 136 strncat(srv, instance, strlen(instance));
135 137 }
136 138 svcname = srv;
137 139 phandle = fs_smf_init(fmri, instance);
138 140 if (phandle == NULL) {
139 141 return (SMF_SYSTEM_ERR);
140 142 }
141 143 handle = phandle->fs_handle;
142 144 pg = phandle->fs_pg;
143 145 prop = phandle->fs_property;
144 146 inst = phandle->fs_instance;
145 147 val = phandle->fs_value;
146 148 tran = scf_transaction_create(handle);
147 149 entry = scf_entry_create(handle);
148 150
149 151 if (handle == NULL || pg == NULL || prop == NULL ||
150 152 val == NULL|| tran == NULL || entry == NULL || inst == NULL) {
151 153 ret = SMF_SYSTEM_ERR;
152 154 goto out;
153 155 }
154 156
155 157 if (scf_handle_decode_fmri(handle, svcname, phandle->fs_scope,
156 158 phandle->fs_service, inst, NULL, NULL, 0) != 0) {
157 159 ret = scf_error();
158 160 goto out;
159 161 }
160 162 if (fstype == AUTOFS_SMF)
161 163 pgname = AUTOFS_PROPS_PGNAME;
162 164 else
163 165 pgname = NFS_PROPS_PGNAME;
164 166
165 167 if (scf_instance_get_pg(inst, pgname,
166 168 pg) != -1) {
167 169 uint8_t vint;
168 170 if (scf_transaction_start(tran, pg) == -1) {
169 171 ret = scf_error();
170 172 goto out;
171 173 }
172 174 switch (sctype) {
173 175 case SCF_TYPE_INTEGER:
174 176 errno = 0;
175 177 valint = strtoul(valbuf, NULL, 0);
176 178 if (errno != 0) {
177 179 ret = SMF_SYSTEM_ERR;
178 180 goto out;
179 181 }
180 182 if (scf_transaction_property_change(tran,
181 183 entry, prop_name, SCF_TYPE_INTEGER) == 0) {
182 184 scf_value_set_integer(val, valint);
183 185 if (scf_entry_add_value(entry, val) < 0) {
184 186 ret = scf_error();
185 187 goto out;
186 188 }
187 189 }
188 190 break;
189 191 case SCF_TYPE_ASTRING:
190 192 if (scf_transaction_property_change(tran, entry,
191 193 prop_name, SCF_TYPE_ASTRING) == 0) {
192 194 if (scf_value_set_astring(val,
193 195 valbuf) == 0) {
194 196 if (scf_entry_add_value(entry,
195 197 val) != 0) {
196 198 ret = scf_error();
197 199 goto out;
198 200 }
199 201 } else
200 202 ret = SMF_SYSTEM_ERR;
201 203 } else
202 204 ret = SMF_SYSTEM_ERR;
203 205 break;
204 206 case SCF_TYPE_BOOLEAN:
205 207 if (strcmp(valbuf, "1") == 0) {
206 208 vint = 1;
207 209 } else if (strcmp(valbuf, "0") == 0) {
208 210 vint = 0;
209 211 } else {
210 212 ret = SMF_SYSTEM_ERR;
211 213 break;
212 214 }
213 215 if (scf_transaction_property_change(tran, entry,
214 216 prop_name, SCF_TYPE_BOOLEAN) == 0) {
215 217 scf_value_set_boolean(val, (uint8_t)vint);
216 218 if (scf_entry_add_value(entry, val) != 0) {
217 219 ret = scf_error();
218 220 goto out;
219 221 }
220 222 } else {
221 223 ret = SMF_SYSTEM_ERR;
222 224 }
223 225 break;
224 226 }
225 227 if (ret != SMF_SYSTEM_ERR)
226 228 scf_transaction_commit(tran);
227 229 }
228 230 out:
229 231 if (tran != NULL)
230 232 scf_transaction_destroy(tran);
231 233 if (entry != NULL)
232 234 scf_entry_destroy(entry);
233 235 fs_smf_fini(phandle);
234 236 return (ret);
235 237 }
236 238
237 239 int
238 240 fs_smf_get_prop(smf_fstype_t fstype, char *prop_name, char *cbuf,
239 241 char *instance, scf_type_t sctype, char *fmri, int *bufsz)
240 242 {
241 243 fs_smfhandle_t *phandle = NULL;
242 244 scf_handle_t *handle;
243 245 scf_propertygroup_t *pg;
244 246 scf_property_t *prop;
245 247 scf_value_t *val;
246 248 scf_instance_t *inst;
247 249 int ret = 0, len = 0, length;
248 250 int64_t valint = 0;
249 251 char srv[MAXPATHLEN], *p, *svcname;
250 252 const char *pgname;
251 253 uint8_t bval;
252 254
253 255 /*
254 256 * The SVC names we are using currently are already
255 257 * appended by default. Fix this for instances project.
256 258 */
257 259 snprintf(srv, MAXPATHLEN, "%s", fmri);
258 260 p = strstr(fmri, ":default");
259 261 if (p == NULL) {
260 262 strcat(srv, ":");
261 263 if (instance == NULL)
262 264 instance = "default";
263 265 if (strlen(srv) + strlen(instance) > MAXPATHLEN)
264 266 goto out;
265 267 strncat(srv, instance, strlen(instance));
266 268 }
267 269 svcname = srv;
268 270 phandle = fs_smf_init(fmri, instance);
269 271 if (phandle == NULL)
270 272 return (SMF_SYSTEM_ERR);
271 273 handle = phandle->fs_handle;
272 274 pg = phandle->fs_pg;
273 275 inst = phandle->fs_instance;
274 276 prop = phandle->fs_property;
275 277 val = phandle->fs_value;
276 278
277 279 if (handle == NULL || pg == NULL || prop == NULL || val == NULL ||
278 280 inst == NULL) {
279 281 return (SMF_SYSTEM_ERR);
280 282 }
281 283
282 284
283 285 if (scf_handle_decode_fmri(handle, svcname, phandle->fs_scope,
284 286 phandle->fs_service, inst, NULL, NULL, 0) != 0) {
285 287 ret = scf_error();
286 288 goto out;
287 289 }
288 290
289 291 if (fstype == AUTOFS_SMF)
290 292 pgname = AUTOFS_PROPS_PGNAME;
291 293 else
292 294 pgname = NFS_PROPS_PGNAME;
293 295
294 296 if (scf_instance_get_pg(inst, pgname, pg) != -1) {
295 297 if (scf_pg_get_property(pg, prop_name,
296 298 prop) != SCF_SUCCESS) {
297 299 ret = scf_error();
298 300 goto out;
299 301 }
300 302 if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
301 303 ret = scf_error();
302 304 goto out;
303 305 }
304 306 switch (sctype) {
305 307 case SCF_TYPE_ASTRING:
306 308 len = scf_value_get_astring(val, cbuf, *bufsz);
307 309 if (len < 0 || len > *bufsz) {
308 310 ret = scf_error();
309 311 goto out;
310 312 }
311 313 ret = 0;
312 314 *bufsz = len;
313 315 break;
314 316 case SCF_TYPE_INTEGER:
315 317 if (scf_value_get_integer(val, &valint) != 0) {
316 318 ret = scf_error();
317 319 goto out;
318 320 }
319 321 length = snprintf(cbuf, *bufsz, "%lld", valint);
320 322 if (length < 0 || length > *bufsz) {
321 323 ret = SA_BAD_VALUE;
322 324 goto out;
323 325 }
324 326 ret = 0;
325 327 break;
326 328 case SCF_TYPE_BOOLEAN:
327 329 if (scf_value_get_boolean(val, &bval) != 0) {
328 330 ret = scf_error();
329 331 goto out;
330 332 }
331 333 if (bval == 1) {
332 334 length = snprintf(cbuf, *bufsz, "%s", "true");
333 335 } else {
334 336 length = snprintf(cbuf, *bufsz, "%s", "false");
335 337 }
336 338 if (length < 0 || length > *bufsz) {
337 339 ret = SA_BAD_VALUE;
338 340 goto out;
339 341 }
340 342 break;
341 343 }
342 344 } else {
343 345 ret = scf_error();
344 346 }
345 347 if ((ret != 0) && scf_error() != SCF_ERROR_NONE)
346 348 fprintf(stdout, gettext("%s\n"), scf_strerror(ret));
347 349 out:
348 350 fs_smf_fini(phandle);
349 351 return (ret);
350 352 }
↓ open down ↓ |
318 lines elided |
↑ open up ↑ |
351 353
352 354
353 355 int
354 356 nfs_smf_get_prop(char *prop_name, char *propbuf, char *instance,
355 357 scf_type_t sctype, char *svc_name, int *bufsz)
356 358 {
357 359 return (fs_smf_get_prop(NFS_SMF, prop_name, propbuf,
358 360 instance, sctype, svc_name, bufsz));
359 361 }
360 362
363 +/* Get an integer (base 10) property */
364 +int
365 +nfs_smf_get_iprop(char *prop_name, int *rvp, char *instance,
366 + scf_type_t sctype, char *svc_name)
367 +{
368 + char propbuf[32];
369 + int bufsz, rc, val;
370 +
371 + bufsz = sizeof (propbuf);
372 + rc = fs_smf_get_prop(NFS_SMF, prop_name, propbuf,
373 + instance, sctype, svc_name, &bufsz);
374 + if (rc != SA_OK)
375 + return (rc);
376 + errno = 0;
377 + val = strtol(propbuf, NULL, 10);
378 + if (errno != 0)
379 + return (SA_BAD_VALUE);
380 + *rvp = val;
381 + return (SA_OK);
382 +}
383 +
361 384 int
362 385 nfs_smf_set_prop(char *prop_name, char *value, char *instance,
363 386 scf_type_t type, char *svc_name)
364 387 {
365 388 return (fs_smf_set_prop(NFS_SMF, prop_name, value, instance,
366 389 type, svc_name));
367 390 }
368 391
369 392 int
370 393 autofs_smf_set_prop(char *prop_name, char *value, char *instance,
371 394 scf_type_t type, char *svc_name)
372 395 {
373 396 return (fs_smf_set_prop(AUTOFS_SMF, prop_name, value, instance,
374 397 type, svc_name));
375 398 }
376 399
377 400 int
378 401 autofs_smf_get_prop(char *prop_name, char *propbuf, char *instance,
379 402 scf_type_t sctype, char *svc_name, int *bufsz)
380 403 {
381 404 return (fs_smf_get_prop(AUTOFS_SMF, prop_name, propbuf,
382 405 instance, sctype, svc_name, bufsz));
383 406 }
384 407
385 408 boolean_t
386 409 string_to_boolean(const char *str)
387 410 {
388 411 if (strcasecmp(str, "true") == 0 || atoi(str) == 1 ||
389 412 strcasecmp(str, "on") == 0 || strcasecmp(str, "yes") == 0) {
390 413 return (B_TRUE);
391 414 } else
392 415 return (B_FALSE);
393 416 }
↓ open down ↓ |
23 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX