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.
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.
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]
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
34 fs_smf_init(char *fmri
, char *instance
)
36 fs_smfhandle_t
*handle
= NULL
;
37 char *svcname
, srv
[MAXPATHLEN
];
40 * svc name is of the form svc://network/fs/server:instance1
41 * FMRI portion is /network/fs/server
43 snprintf(srv
, MAXPATHLEN
, "%s", fmri
+ strlen("svc:/"));
44 svcname
= strrchr(srv
, ':');
49 handle
= calloc(1, sizeof (fs_smfhandle_t
));
51 handle
->fs_handle
= scf_handle_create(SCF_VERSION
);
52 if (handle
->fs_handle
== NULL
)
54 if (scf_handle_bind(handle
->fs_handle
) != 0)
57 scf_service_create(handle
->fs_handle
);
59 scf_scope_create(handle
->fs_handle
);
60 if (scf_handle_get_local_scope(handle
->fs_handle
,
61 handle
->fs_scope
) != 0)
63 if (scf_scope_get_service(handle
->fs_scope
,
64 svcname
, handle
->fs_service
) != SCF_SUCCESS
) {
68 scf_pg_create(handle
->fs_handle
);
70 scf_instance_create(handle
->fs_handle
);
72 scf_property_create(handle
->fs_handle
);
74 scf_value_create(handle
->fs_handle
);
77 gettext("Cannot access SMF repository: %s\n"), fmri
);
83 fprintf(stderr
, gettext("SMF Initialization problems..%s\n"), fmri
);
89 fs_smf_fini(fs_smfhandle_t
*handle
)
92 scf_scope_destroy(handle
->fs_scope
);
93 scf_instance_destroy(handle
->fs_instance
);
94 scf_service_destroy(handle
->fs_service
);
95 scf_pg_destroy(handle
->fs_pg
);
96 scf_property_destroy(handle
->fs_property
);
97 scf_value_destroy(handle
->fs_value
);
98 if (handle
->fs_handle
!= NULL
) {
99 scf_handle_unbind(handle
->fs_handle
);
100 scf_handle_destroy(handle
->fs_handle
);
107 fs_smf_set_prop(smf_fstype_t fstype
, char *prop_name
, char *valbuf
,
108 char *instance
, scf_type_t sctype
, char *fmri
)
110 fs_smfhandle_t
*phandle
= NULL
;
111 scf_handle_t
*handle
;
112 scf_propertygroup_t
*pg
;
113 scf_property_t
*prop
;
114 scf_transaction_t
*tran
= NULL
;
115 scf_transaction_entry_t
*entry
= NULL
;
116 scf_instance_t
*inst
;
121 char *svcname
, srv
[MAXPATHLEN
];
125 * The SVC names we are using currently are already
126 * appended by default. Fix this for instances project.
128 snprintf(srv
, MAXPATHLEN
, "%s", fmri
);
129 p
= strstr(fmri
, ":default");
132 if (instance
== NULL
)
133 instance
= "default";
134 if (strlen(srv
) + strlen(instance
) > MAXPATHLEN
)
136 strncat(srv
, instance
, strlen(instance
));
139 phandle
= fs_smf_init(fmri
, instance
);
140 if (phandle
== NULL
) {
141 return (SMF_SYSTEM_ERR
);
143 handle
= phandle
->fs_handle
;
145 prop
= phandle
->fs_property
;
146 inst
= phandle
->fs_instance
;
147 val
= phandle
->fs_value
;
148 tran
= scf_transaction_create(handle
);
149 entry
= scf_entry_create(handle
);
151 if (handle
== NULL
|| pg
== NULL
|| prop
== NULL
||
152 val
== NULL
|| tran
== NULL
|| entry
== NULL
|| inst
== NULL
) {
153 ret
= SMF_SYSTEM_ERR
;
157 if (scf_handle_decode_fmri(handle
, svcname
, phandle
->fs_scope
,
158 phandle
->fs_service
, inst
, NULL
, NULL
, 0) != 0) {
162 if (fstype
== AUTOFS_SMF
)
163 pgname
= AUTOFS_PROPS_PGNAME
;
165 pgname
= NFS_PROPS_PGNAME
;
167 if (scf_instance_get_pg(inst
, pgname
,
170 if (scf_transaction_start(tran
, pg
) == -1) {
175 case SCF_TYPE_INTEGER
:
177 valint
= strtoul(valbuf
, NULL
, 0);
179 ret
= SMF_SYSTEM_ERR
;
182 if (scf_transaction_property_change(tran
,
183 entry
, prop_name
, SCF_TYPE_INTEGER
) == 0) {
184 scf_value_set_integer(val
, valint
);
185 if (scf_entry_add_value(entry
, val
) < 0) {
191 case SCF_TYPE_ASTRING
:
192 if (scf_transaction_property_change(tran
, entry
,
193 prop_name
, SCF_TYPE_ASTRING
) == 0) {
194 if (scf_value_set_astring(val
,
196 if (scf_entry_add_value(entry
,
202 ret
= SMF_SYSTEM_ERR
;
204 ret
= SMF_SYSTEM_ERR
;
206 case SCF_TYPE_BOOLEAN
:
207 if (strcmp(valbuf
, "1") == 0) {
209 } else if (strcmp(valbuf
, "0") == 0) {
212 ret
= SMF_SYSTEM_ERR
;
215 if (scf_transaction_property_change(tran
, entry
,
216 prop_name
, SCF_TYPE_BOOLEAN
) == 0) {
217 scf_value_set_boolean(val
, (uint8_t)vint
);
218 if (scf_entry_add_value(entry
, val
) != 0) {
223 ret
= SMF_SYSTEM_ERR
;
227 if (ret
!= SMF_SYSTEM_ERR
)
228 scf_transaction_commit(tran
);
232 scf_transaction_destroy(tran
);
234 scf_entry_destroy(entry
);
235 fs_smf_fini(phandle
);
240 fs_smf_get_prop(smf_fstype_t fstype
, char *prop_name
, char *cbuf
,
241 char *instance
, scf_type_t sctype
, char *fmri
, int *bufsz
)
243 fs_smfhandle_t
*phandle
= NULL
;
244 scf_handle_t
*handle
;
245 scf_propertygroup_t
*pg
;
246 scf_property_t
*prop
;
248 scf_instance_t
*inst
;
249 int ret
= 0, len
= 0, length
;
251 char srv
[MAXPATHLEN
], *p
, *svcname
;
256 * The SVC names we are using currently are already
257 * appended by default. Fix this for instances project.
259 snprintf(srv
, MAXPATHLEN
, "%s", fmri
);
260 p
= strstr(fmri
, ":default");
263 if (instance
== NULL
)
264 instance
= "default";
265 if (strlen(srv
) + strlen(instance
) > MAXPATHLEN
)
267 strncat(srv
, instance
, strlen(instance
));
270 phandle
= fs_smf_init(fmri
, instance
);
272 return (SMF_SYSTEM_ERR
);
273 handle
= phandle
->fs_handle
;
275 inst
= phandle
->fs_instance
;
276 prop
= phandle
->fs_property
;
277 val
= phandle
->fs_value
;
279 if (handle
== NULL
|| pg
== NULL
|| prop
== NULL
|| val
== NULL
||
281 return (SMF_SYSTEM_ERR
);
285 if (scf_handle_decode_fmri(handle
, svcname
, phandle
->fs_scope
,
286 phandle
->fs_service
, inst
, NULL
, NULL
, 0) != 0) {
291 if (fstype
== AUTOFS_SMF
)
292 pgname
= AUTOFS_PROPS_PGNAME
;
294 pgname
= NFS_PROPS_PGNAME
;
296 if (scf_instance_get_pg(inst
, pgname
, pg
) != -1) {
297 if (scf_pg_get_property(pg
, prop_name
,
298 prop
) != SCF_SUCCESS
) {
302 if (scf_property_get_value(prop
, val
) != SCF_SUCCESS
) {
307 case SCF_TYPE_ASTRING
:
308 len
= scf_value_get_astring(val
, cbuf
, *bufsz
);
309 if (len
< 0 || len
> *bufsz
) {
316 case SCF_TYPE_INTEGER
:
317 if (scf_value_get_integer(val
, &valint
) != 0) {
321 length
= snprintf(cbuf
, *bufsz
, "%lld", valint
);
322 if (length
< 0 || length
> *bufsz
) {
328 case SCF_TYPE_BOOLEAN
:
329 if (scf_value_get_boolean(val
, &bval
) != 0) {
334 length
= snprintf(cbuf
, *bufsz
, "%s", "true");
336 length
= snprintf(cbuf
, *bufsz
, "%s", "false");
338 if (length
< 0 || length
> *bufsz
) {
347 if ((ret
!= 0) && scf_error() != SCF_ERROR_NONE
)
348 fprintf(stdout
, gettext("%s\n"), scf_strerror(ret
));
350 fs_smf_fini(phandle
);
356 nfs_smf_get_prop(char *prop_name
, char *propbuf
, char *instance
,
357 scf_type_t sctype
, char *svc_name
, int *bufsz
)
359 return (fs_smf_get_prop(NFS_SMF
, prop_name
, propbuf
,
360 instance
, sctype
, svc_name
, bufsz
));
363 /* Get an integer (base 10) property */
365 nfs_smf_get_iprop(char *prop_name
, int *rvp
, char *instance
,
366 scf_type_t sctype
, char *svc_name
)
371 bufsz
= sizeof (propbuf
);
372 rc
= fs_smf_get_prop(NFS_SMF
, prop_name
, propbuf
,
373 instance
, sctype
, svc_name
, &bufsz
);
377 val
= strtol(propbuf
, NULL
, 10);
379 return (SA_BAD_VALUE
);
385 nfs_smf_set_prop(char *prop_name
, char *value
, char *instance
,
386 scf_type_t type
, char *svc_name
)
388 return (fs_smf_set_prop(NFS_SMF
, prop_name
, value
, instance
,
393 autofs_smf_set_prop(char *prop_name
, char *value
, char *instance
,
394 scf_type_t type
, char *svc_name
)
396 return (fs_smf_set_prop(AUTOFS_SMF
, prop_name
, value
, instance
,
401 autofs_smf_get_prop(char *prop_name
, char *propbuf
, char *instance
,
402 scf_type_t sctype
, char *svc_name
, int *bufsz
)
404 return (fs_smf_get_prop(AUTOFS_SMF
, prop_name
, propbuf
,
405 instance
, sctype
, svc_name
, bufsz
));
409 string_to_boolean(const char *str
)
411 if (strcasecmp(str
, "true") == 0 || atoi(str
) == 1 ||
412 strcasecmp(str
, "on") == 0 || strcasecmp(str
, "yes") == 0) {