components/visual-panels/smf/src/cmd/rad/mod/smf/datatype.c
changeset 901 19b502ccabc8
parent 827 0944d8c0158b
equal deleted inserted replaced
900:63d3bf696d85 901:19b502ccabc8
    49 /*
    49 /*
    50  * Routines for creating ADR data objects from SCF types or objects.
    50  * Routines for creating ADR data objects from SCF types or objects.
    51  * Consumed by the API entry points.
    51  * Consumed by the API entry points.
    52  */
    52  */
    53 
    53 
    54 data_t *
    54 adr_data_t *
    55 create_SMFState(const char *state)
    55 create_SMFState(const char *state)
    56 {
    56 {
    57 	if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0) {
    57 	if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0) {
    58 		return (&e__SMFState_UNINIT);
    58 		return (&e__SMFState_UNINIT);
    59 	} else if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
    59 	} else if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
    60 		return (&e__SMFState_MAINT);
    60 		return (&e__SMFState_MAINT);
    61 	} else if (strcmp(state, SCF_STATE_STRING_LEGACY) == 0) {
    61 	} else if (strcmp(state, SCF_STATE_STRING_LEGACY) == 0) {
    62 		return (&e__SMFState_LEGACY);
    62 		return (&e__SMFState_LEGACY);
    63 	} else {
    63 	} else {
    64 		return (data_new_enum_byname(&t__SMFState, state));
    64 		return (adr_data_new_enum_byname(&t__SMFState, state));
    65 	}
    65 	}
    66 }
    66 }
    67 
    67 
    68 static data_t *
    68 static adr_data_t *
    69 create_DepGrouping(const char *grouping)
    69 create_DepGrouping(const char *grouping)
    70 {
    70 {
    71 	if (strcmp(grouping, SCF_DEP_REQUIRE_ALL) == 0)
    71 	if (strcmp(grouping, SCF_DEP_REQUIRE_ALL) == 0)
    72 		return (&e__DepGrouping_REQUIRE_ALL);
    72 		return (&e__DepGrouping_REQUIRE_ALL);
    73 	else if (strcmp(grouping, SCF_DEP_REQUIRE_ANY) == 0)
    73 	else if (strcmp(grouping, SCF_DEP_REQUIRE_ANY) == 0)
    77 	else if (strcmp(grouping, SCF_DEP_OPTIONAL_ALL) == 0)
    77 	else if (strcmp(grouping, SCF_DEP_OPTIONAL_ALL) == 0)
    78 		return (&e__DepGrouping_OPTIONAL_ALL);
    78 		return (&e__DepGrouping_OPTIONAL_ALL);
    79 	return (NULL);
    79 	return (NULL);
    80 }
    80 }
    81 
    81 
    82 static data_t *
    82 static adr_data_t *
    83 create_DepRestart(const char *restarton)
    83 create_DepRestart(const char *restarton)
    84 {
    84 {
    85 	if (strcmp(restarton, SCF_DEP_RESET_ON_ERROR) == 0)
    85 	if (strcmp(restarton, SCF_DEP_RESET_ON_ERROR) == 0)
    86 		return (&e__DepRestart_ERROR);
    86 		return (&e__DepRestart_ERROR);
    87 	if (strcmp(restarton, SCF_DEP_RESET_ON_RESTART) == 0)
    87 	if (strcmp(restarton, SCF_DEP_RESET_ON_RESTART) == 0)
    91 	if (strcmp(restarton, SCF_DEP_RESET_ON_NONE) == 0)
    91 	if (strcmp(restarton, SCF_DEP_RESET_ON_NONE) == 0)
    92 		return (&e__DepRestart_NONE);
    92 		return (&e__DepRestart_NONE);
    93 	return (NULL);
    93 	return (NULL);
    94 }
    94 }
    95 
    95 
    96 static data_t *
    96 static adr_data_t *
    97 create_PropertyType(scf_type_t type)
    97 create_PropertyType(scf_type_t type)
    98 {
    98 {
    99 	data_t *result = data_new_enum(&t__PropertyType, type);
    99 	adr_data_t *result = adr_data_new_enum(&t__PropertyType, type);
   100 	assert(result != NULL);
   100 	assert(result != NULL);
   101 	return (result);
   101 	return (result);
   102 }
   102 }
   103 
   103 
   104 scf_type_t
   104 scf_type_t
   105 from_PropertyType(data_t *type)
   105 from_PropertyType(adr_data_t *type)
   106 {
   106 {
   107 	return (enum_tovalue(type));
   107 	return (adr_enum_tovalue(type));
   108 }
   108 }
   109 
   109 
   110 static data_t *
   110 static adr_data_t *
   111 create_PropertyVisibility(uint8_t visibility)
   111 create_PropertyVisibility(uint8_t visibility)
   112 {
   112 {
   113 	switch (visibility) {
   113 	switch (visibility) {
   114 	case SCF_TMPL_VISIBILITY_HIDDEN:
   114 	case SCF_TMPL_VISIBILITY_HIDDEN:
   115 		return (&e__PropertyVisibility_HIDDEN);
   115 		return (&e__PropertyVisibility_HIDDEN);
   127  * A return value of SE_NOTFOUND implies that the property group wasn't
   127  * A return value of SE_NOTFOUND implies that the property group wasn't
   128  * a dependency and should be skipped.
   128  * a dependency and should be skipped.
   129  */
   129  */
   130 svcerr_t
   130 svcerr_t
   131 create_Dependency(scf_handle_t *scfhandle, scf_propertygroup_t *pg,
   131 create_Dependency(scf_handle_t *scfhandle, scf_propertygroup_t *pg,
   132     data_t **ret)
   132     adr_data_t **ret)
   133 {
   133 {
   134 	svcerr_t se = SE_OK;
   134 	svcerr_t se = SE_OK;
   135 	char type[max_pgtype + 1];
   135 	char type[max_pgtype + 1];
   136 	char grouping[max_value + 1];
   136 	char grouping[max_value + 1];
   137 	char restarton[max_value + 1];
   137 	char restarton[max_value + 1];
   157 	    scf_iter_property_values(iter, prop) != 0) {
   157 	    scf_iter_property_values(iter, prop) != 0) {
   158 		se = smfu_maperr(scf_error());
   158 		se = smfu_maperr(scf_error());
   159 		goto out;
   159 		goto out;
   160 	}
   160 	}
   161 
   161 
   162 	data_t *group = create_DepGrouping(grouping);
   162 	adr_data_t *group = create_DepGrouping(grouping);
   163 	data_t *restart = create_DepRestart(restarton);
   163 	adr_data_t *restart = create_DepRestart(restarton);
   164 	if (group == NULL || restart == NULL) {
   164 	if (group == NULL || restart == NULL) {
   165 		se = SE_NOTFOUND;
   165 		se = SE_NOTFOUND;
   166 		goto out;
   166 		goto out;
   167 	}
   167 	}
   168 
   168 
   169 	data_t *result = data_new_struct(&t__Dependency);
   169 	adr_data_t *result = adr_data_new_struct(&t__Dependency);
   170 	struct_set(result, "name", data_new_string(pgname, lt_copy));
   170 	adr_struct_set(result, "name", adr_data_new_string(pgname, LT_COPY));
   171 	struct_set(result, "grouping", group);
   171 	adr_struct_set(result, "grouping", group);
   172 	struct_set(result, "restartOn", restart);
   172 	adr_struct_set(result, "restartOn", restart);
   173 	data_t *array = data_new_array(&t_array_string, 5);
   173 	adr_data_t *array = adr_data_new_array(&adr_t_array_string, 5);
   174 	struct_set(result, "target", array);
   174 	adr_struct_set(result, "target", array);
   175 
   175 
   176 	while (scf_iter_next_value(iter, val) > 0) {
   176 	while (scf_iter_next_value(iter, val) > 0) {
   177 		if (scf_value_get_as_string(val, type, sizeof (type)) == -1) {
   177 		if (scf_value_get_as_string(val, type, sizeof (type)) == -1) {
   178 			se = smfu_maperr(scf_error());
   178 			se = smfu_maperr(scf_error());
   179 			data_free(result);
   179 			adr_data_free(result);
   180 			goto out;
   180 			goto out;
   181 		}
   181 		}
   182 		(void) array_add(array, data_new_string(type, lt_copy));
   182 		(void) adr_array_add(array, adr_data_new_string(type, LT_COPY));
   183 	}
   183 	}
   184 	if (!data_verify(result, NULL, B_TRUE)) {
   184 	if (!adr_data_verify(result, NULL, B_TRUE)) {
   185 		se = SE_FATAL;
   185 		se = SE_FATAL;
   186 		data_free(result);
   186 		adr_data_free(result);
   187 		goto out;
   187 		goto out;
   188 	}
   188 	}
   189 
   189 
   190 	*ret = result;
   190 	*ret = result;
   191 out:
   191 out:
   200  *
   200  *
   201  * A return value of SE_NOTFOUND implies that the property group wasn't
   201  * A return value of SE_NOTFOUND implies that the property group wasn't
   202  * a man page and should be skipped.
   202  * a man page and should be skipped.
   203  */
   203  */
   204 svcerr_t
   204 svcerr_t
   205 create_Manpage(scf_handle_t *scfhandle, scf_propertygroup_t *pg, data_t **ret)
   205 create_Manpage(scf_handle_t *scfhandle, scf_propertygroup_t *pg,
       
   206     adr_data_t **ret)
   206 {
   207 {
   207 	char manpath[max_value + 1];
   208 	char manpath[max_value + 1];
   208 	char section[max_value + 1];
   209 	char section[max_value + 1];
   209 	char title[max_name + 1];
   210 	char title[max_name + 1];
   210 	char pgname[max_name + 1];
   211 	char pgname[max_name + 1];
   237 	    scf_value_get_as_string(val, section, sizeof (section)) == -1) {
   238 	    scf_value_get_as_string(val, section, sizeof (section)) == -1) {
   238 		se = smfu_maperr(scf_error());
   239 		se = smfu_maperr(scf_error());
   239 		goto out;
   240 		goto out;
   240 	}
   241 	}
   241 
   242 
   242 	data_t *result = data_new_struct(&t__Manpage);
   243 	adr_data_t *result = adr_data_new_struct(&t__Manpage);
   243 	struct_set(result, "title", data_new_string(title, lt_copy));
   244 	adr_struct_set(result, "title", adr_data_new_string(title, LT_COPY));
   244 	struct_set(result, "section", data_new_string(section, lt_copy));
   245 	adr_struct_set(result, "section",
       
   246 	    adr_data_new_string(section, LT_COPY));
   245 	if (strcmp(manpath, ":default") != 0) {
   247 	if (strcmp(manpath, ":default") != 0) {
   246 		data_t *mp = data_new_string(manpath, lt_copy);
   248 		adr_data_t *mp = adr_data_new_string(manpath, LT_COPY);
   247 		if (mp == NULL) {
   249 		if (mp == NULL) {
   248 			se = SE_FATAL;
   250 			se = SE_FATAL;
   249 			data_free(result);
   251 			adr_data_free(result);
   250 			goto out;
   252 			goto out;
   251 		}
   253 		}
   252 		struct_set(result, "path", data_new_string(manpath, lt_copy));
   254 		adr_struct_set(result, "path",
   253 	}
   255 		    adr_data_new_string(manpath, LT_COPY));
   254 
   256 	}
   255 	if (!data_verify(result, NULL, B_TRUE)) {
   257 
       
   258 	if (!adr_data_verify(result, NULL, B_TRUE)) {
   256 		se = SE_FATAL;
   259 		se = SE_FATAL;
   257 		data_free(result);
   260 		adr_data_free(result);
   258 		goto out;
   261 		goto out;
   259 	}
   262 	}
   260 
   263 
   261 	*ret = result;
   264 	*ret = result;
   262 out:
   265 out:
   270  *
   273  *
   271  * A return value of SE_NOTFOUND implies that the property group wasn't
   274  * A return value of SE_NOTFOUND implies that the property group wasn't
   272  * a doclink and should be skipped.
   275  * a doclink and should be skipped.
   273  */
   276  */
   274 svcerr_t
   277 svcerr_t
   275 create_Doclink(scf_handle_t *scfhandle, scf_propertygroup_t *pg, data_t **ret)
   278 create_Doclink(scf_handle_t *scfhandle, scf_propertygroup_t *pg,
       
   279     adr_data_t **ret)
   276 {
   280 {
   277 	char name[max_value + 1];
   281 	char name[max_value + 1];
   278 	char uri[max_value + 1];
   282 	char uri[max_value + 1];
   279 	char pgname[max_name + 1];
   283 	char pgname[max_name + 1];
   280 	svcerr_t se = SE_OK;
   284 	svcerr_t se = SE_OK;
   302 	    scf_value_get_as_string(val, uri, sizeof (uri)) == -1) {
   306 	    scf_value_get_as_string(val, uri, sizeof (uri)) == -1) {
   303 		se = smfu_maperr(scf_error());
   307 		se = smfu_maperr(scf_error());
   304 		goto out;
   308 		goto out;
   305 	}
   309 	}
   306 
   310 
   307 	data_t *result = data_new_struct(&t__Doclink);
   311 	adr_data_t *result = adr_data_new_struct(&t__Doclink);
   308 	struct_set(result, "name", data_new_string(name, lt_copy));
   312 	adr_struct_set(result, "name", adr_data_new_string(name, LT_COPY));
   309 	struct_set(result, "uri", data_new_string(uri, lt_copy));
   313 	adr_struct_set(result, "uri", adr_data_new_string(uri, LT_COPY));
   310 
   314 
   311 	if (!data_verify(result, NULL, B_TRUE)) {
   315 	if (!adr_data_verify(result, NULL, B_TRUE)) {
   312 		se = SE_FATAL;
   316 		se = SE_FATAL;
   313 		data_free(result);
   317 		adr_data_free(result);
   314 		goto out;
   318 		goto out;
   315 	}
   319 	}
   316 
   320 
   317 	*ret = result;
   321 	*ret = result;
   318 out:
   322 out:
   320 	scf_property_destroy(prop);
   324 	scf_property_destroy(prop);
   321 	return (se);
   325 	return (se);
   322 }
   326 }
   323 
   327 
   324 svcerr_t
   328 svcerr_t
   325 create_PropertyGroup(scf_propertygroup_t *pg, data_t **ret)
   329 create_PropertyGroup(scf_propertygroup_t *pg, adr_data_t **ret)
   326 {
   330 {
   327 	char name[max_value + 1];
   331 	char name[max_value + 1];
   328 	char type[max_pgtype + 1];
   332 	char type[max_pgtype + 1];
   329 	uint32_t flags;
   333 	uint32_t flags;
   330 	svcerr_t se = SE_OK;
   334 	svcerr_t se = SE_OK;
   334 	    scf_pg_get_flags(pg, &flags) == -1) {
   338 	    scf_pg_get_flags(pg, &flags) == -1) {
   335 		se = smfu_maperr(scf_error());
   339 		se = smfu_maperr(scf_error());
   336 		goto out;
   340 		goto out;
   337 	}
   341 	}
   338 
   342 
   339 	data_t *result = data_new_struct(&t__PropertyGroup);
   343 	adr_data_t *result = adr_data_new_struct(&t__PropertyGroup);
   340 	struct_set(result, "name", data_new_string(name, lt_copy));
   344 	adr_struct_set(result, "name", adr_data_new_string(name, LT_COPY));
   341 	struct_set(result, "type", data_new_string(type, lt_copy));
   345 	adr_struct_set(result, "type", adr_data_new_string(type, LT_COPY));
   342 	struct_set(result, "flags", data_new_uinteger(flags));
   346 	adr_struct_set(result, "flags", adr_data_new_uinteger(flags));
   343 
   347 
   344 	if (!data_verify(result, NULL, B_TRUE)) {
   348 	if (!adr_data_verify(result, NULL, B_TRUE)) {
   345 		se = SE_FATAL;
   349 		se = SE_FATAL;
   346 		data_free(result);
   350 		adr_data_free(result);
   347 		goto out;
   351 		goto out;
   348 	}
   352 	}
   349 
   353 
   350 	*ret = result;
   354 	*ret = result;
   351 out:
   355 out:
   352 	return (se);
   356 	return (se);
   353 }
   357 }
   354 
   358 
   355 svcerr_t
   359 svcerr_t
   356 create_Property(scf_property_t *prop, scf_iter_t *iter, scf_value_t *value,
   360 create_Property(scf_property_t *prop, scf_iter_t *iter, scf_value_t *value,
   357     data_t **ret, data_t **error)
   361     adr_data_t **ret, adr_data_t **error)
   358 {
   362 {
   359 	char name[max_name + 1];
   363 	char name[max_name + 1];
   360 	scf_type_t type;
   364 	scf_type_t type;
   361 
   365 
   362 	if (scf_property_get_name(prop, name, sizeof (name)) == -1 ||
   366 	if (scf_property_get_name(prop, name, sizeof (name)) == -1 ||
   363 	    scf_property_type(prop, &type) == -1 ||
   367 	    scf_property_type(prop, &type) == -1 ||
   364 	    scf_iter_property_values(iter, prop))
   368 	    scf_iter_property_values(iter, prop))
   365 		return (smfu_maperr(scf_error()));
   369 		return (smfu_maperr(scf_error()));
   366 
   370 
   367 	data_t *values = data_new_array(&t_array_string, 5);
   371 	adr_data_t *values = adr_data_new_array(&adr_t_array_string, 5);
   368 	int err;
   372 	int err;
   369 	while ((err = scf_iter_next_value(iter, value)) > 0) {
   373 	while ((err = scf_iter_next_value(iter, value)) > 0) {
   370 		char *valstr;
   374 		char *valstr;
   371 		if (smfu_value_get_string(value, &valstr) != 0) {
   375 		if (smfu_value_get_string(value, &valstr) != 0) {
   372 			data_free(values);
   376 			adr_data_free(values);
   373 			return (SE_FATAL);
   377 			return (SE_FATAL);
   374 		}
   378 		}
   375 		(void) array_add(values, data_new_string(valstr, lt_free));
   379 		(void) adr_array_add(values,
       
   380 		    adr_data_new_string(valstr, LT_FREE));
   376 	}
   381 	}
   377 	if (err != 0) {
   382 	if (err != 0) {
   378 		if (scf_error() == SCF_ERROR_PERMISSION_DENIED)
   383 		if (scf_error() == SCF_ERROR_PERMISSION_DENIED)
   379 			return (error_scf(error, &e__ErrorCode_DENIED, NULL,
   384 			return (error_scf(error, &e__ErrorCode_DENIED, NULL,
   380 			    NULL, NULL));
   385 			    NULL, NULL));
   381 		else
   386 		else
   382 			return (smfu_maperr(scf_error()));
   387 			return (smfu_maperr(scf_error()));
   383 	}
   388 	}
   384 
   389 
   385 	data_t *result = data_new_struct(&t__Property);
   390 	adr_data_t *result = adr_data_new_struct(&t__Property);
   386 	struct_set(result, "name", data_new_string(name, lt_copy));
   391 	adr_struct_set(result, "name", adr_data_new_string(name, LT_COPY));
   387 	struct_set(result, "type", create_PropertyType(type));
   392 	adr_struct_set(result, "type", create_PropertyType(type));
   388 	struct_set(result, "values", values);
   393 	adr_struct_set(result, "values", values);
   389 	*ret = result;
   394 	*ret = result;
   390 
   395 
   391 	return (SE_OK);
   396 	return (SE_OK);
   392 }
   397 }
   393 
   398 
   394 svcerr_t
   399 svcerr_t
   395 create_Properties(scf_handle_t *scfhandle, scf_propertygroup_t *pg,
   400 create_Properties(scf_handle_t *scfhandle, scf_propertygroup_t *pg,
   396     data_t **ret, data_t **error)
   401     adr_data_t **ret, adr_data_t **error)
   397 {
   402 {
   398 	svcerr_t se = SE_OK;
   403 	svcerr_t se = SE_OK;
   399 	scf_iter_t *piter = scf_iter_create(scfhandle);
   404 	scf_iter_t *piter = scf_iter_create(scfhandle);
   400 	scf_iter_t *viter = scf_iter_create(scfhandle);
   405 	scf_iter_t *viter = scf_iter_create(scfhandle);
   401 	scf_value_t *value = scf_value_create(scfhandle);
   406 	scf_value_t *value = scf_value_create(scfhandle);
   402 	scf_property_t *prop = scf_property_create(scfhandle);
   407 	scf_property_t *prop = scf_property_create(scfhandle);
   403 	data_t *result = data_new_array(&t_array__Property, 5);
   408 	adr_data_t *result = adr_data_new_array(&t_array__Property, 5);
   404 
   409 
   405 	if (piter == NULL || viter == NULL || value == NULL || prop == NULL) {
   410 	if (piter == NULL || viter == NULL || value == NULL || prop == NULL) {
   406 		se = SE_FATAL;
   411 		se = SE_FATAL;
   407 		goto out;
   412 		goto out;
   408 	}
   413 	}
   412 		goto out;
   417 		goto out;
   413 	}
   418 	}
   414 
   419 
   415 	int err;
   420 	int err;
   416 	while ((err = scf_iter_next_property(piter, prop)) > 0) {
   421 	while ((err = scf_iter_next_property(piter, prop)) > 0) {
   417 		data_t *propdata = NULL;
   422 		adr_data_t *propdata = NULL;
   418 		if ((se = create_Property(prop, viter, value, &propdata,
   423 		if ((se = create_Property(prop, viter, value, &propdata,
   419 		    error)) != SE_OK)
   424 		    error)) != SE_OK)
   420 			goto out;
   425 			goto out;
   421 		(void) array_add(result, propdata);
   426 		(void) adr_array_add(result, propdata);
   422 	}
   427 	}
   423 	if (err != 0)
   428 	if (err != 0)
   424 		se = smfu_maperr(scf_error());
   429 		se = smfu_maperr(scf_error());
   425 
   430 
   426 out:
   431 out:
   427 	if (se != SE_OK)
   432 	if (se != SE_OK)
   428 		data_free(result);
   433 		adr_data_free(result);
   429 	else
   434 	else
   430 		*ret = result;
   435 		*ret = result;
   431 
   436 
   432 	scf_iter_destroy(piter);
   437 	scf_iter_destroy(piter);
   433 	scf_iter_destroy(viter);
   438 	scf_iter_destroy(viter);
   437 	return (se);
   442 	return (se);
   438 }
   443 }
   439 
   444 
   440 svcerr_t
   445 svcerr_t
   441 create_ExtendedState(scf_handle_t *scfhandle, scf_instance_t *instance,
   446 create_ExtendedState(scf_handle_t *scfhandle, scf_instance_t *instance,
   442     data_t **ret)
   447     adr_data_t **ret)
   443 {
   448 {
   444 	scf_propertygroup_t *pg = scf_pg_create(scfhandle);
   449 	scf_propertygroup_t *pg = scf_pg_create(scfhandle);
   445 	scf_property_t *prop = scf_property_create(scfhandle);
   450 	scf_property_t *prop = scf_property_create(scfhandle);
   446 	scf_value_t *value = scf_value_create(scfhandle);
   451 	scf_value_t *value = scf_value_create(scfhandle);
   447 	data_t *result = data_new_struct(&t__ExtendedState);
   452 	adr_data_t *result = adr_data_new_struct(&t__ExtendedState);
   448 	svcerr_t se = SE_FATAL;
   453 	svcerr_t se = SE_FATAL;
   449 	int serr;
   454 	int serr;
   450 	data_t *dstr;
   455 	adr_data_t *dstr;
   451 	char *str;
   456 	char *str;
   452 	int64_t sec;
   457 	int64_t sec;
   453 	int32_t nsec;
   458 	int32_t nsec;
   454 	uint64_t ctid;
   459 	uint64_t ctid;
   455 	uint8_t bool;
   460 	uint8_t bool;
   463 	if (scf_pg_get_property(pg, SCF_PROPERTY_STATE, prop) == -1 ||
   468 	if (scf_pg_get_property(pg, SCF_PROPERTY_STATE, prop) == -1 ||
   464 	    scf_property_get_value(prop, value) == -1)
   469 	    scf_property_get_value(prop, value) == -1)
   465 		goto scferror;
   470 		goto scferror;
   466 	if (smfu_value_get_string(value, &str) != 0)
   471 	if (smfu_value_get_string(value, &str) != 0)
   467 		goto error;
   472 		goto error;
   468 	struct_set(result, "state", create_SMFState(str));
   473 	adr_struct_set(result, "state", create_SMFState(str));
   469 	free(str);
   474 	free(str);
   470 
   475 
   471 	if (scf_pg_get_property(pg, SCF_PROPERTY_NEXT_STATE, prop) == -1 ||
   476 	if (scf_pg_get_property(pg, SCF_PROPERTY_NEXT_STATE, prop) == -1 ||
   472 	    scf_property_get_value(prop, value) == -1)
   477 	    scf_property_get_value(prop, value) == -1)
   473 		goto scferror;
   478 		goto scferror;
   474 	if (smfu_value_get_string(value, &str) != 0)
   479 	if (smfu_value_get_string(value, &str) != 0)
   475 		goto error;
   480 		goto error;
   476 	struct_set(result, "nextState", create_SMFState(str));
   481 	adr_struct_set(result, "nextState", create_SMFState(str));
   477 	free(str);
   482 	free(str);
   478 
   483 
   479 	if (scf_pg_get_property(pg, SCF_PROPERTY_AUX_STATE, prop) == -1 ||
   484 	if (scf_pg_get_property(pg, SCF_PROPERTY_AUX_STATE, prop) == -1 ||
   480 	    scf_property_get_value(prop, value) == -1)
   485 	    scf_property_get_value(prop, value) == -1)
   481 		goto scferror;
   486 		goto scferror;
   482 	if (smfu_value_get_string(value, &str) != 0)
   487 	if (smfu_value_get_string(value, &str) != 0)
   483 		goto error;
   488 		goto error;
   484 	if ((dstr = data_new_string(str, lt_free)) == NULL)
   489 	if ((dstr = adr_data_new_string(str, LT_FREE)) == NULL)
   485 		goto error;
   490 		goto error;
   486 	struct_set(result, "auxstate", dstr);
   491 	adr_struct_set(result, "auxstate", dstr);
   487 
   492 
   488 	if (scf_pg_get_property(pg, SCF_PROPERTY_STATE_TIMESTAMP, prop) == -1 ||
   493 	if (scf_pg_get_property(pg, SCF_PROPERTY_STATE_TIMESTAMP, prop) == -1 ||
   489 	    scf_property_get_value(prop, value) == -1 ||
   494 	    scf_property_get_value(prop, value) == -1 ||
   490 	    scf_value_get_time(value, &sec, &nsec) == -1)
   495 	    scf_value_get_time(value, &sec, &nsec) == -1)
   491 		goto scferror;
   496 		goto scferror;
   492 	struct_set(result, "stime", data_new_time(sec, nsec));
   497 	adr_struct_set(result, "stime", adr_data_new_time(sec, nsec));
   493 
   498 
   494 	if (scf_pg_get_property(pg, SCF_PROPERTY_CONTRACT, prop) == -1 ||
   499 	if (scf_pg_get_property(pg, SCF_PROPERTY_CONTRACT, prop) == -1 ||
   495 	    scf_property_get_value(prop, value) == -1) {
   500 	    scf_property_get_value(prop, value) == -1) {
   496 		if (scf_error() == SCF_ERROR_NOT_FOUND)
   501 		if (scf_error() == SCF_ERROR_NOT_FOUND)
   497 			struct_set(result, "contractid", data_new_integer(-1));
   502 			adr_struct_set(result, "contractid",
       
   503 			    adr_data_new_integer(-1));
   498 		else
   504 		else
   499 			goto scferror;
   505 			goto scferror;
   500 	} else if (scf_value_get_count(value, &ctid) == -1) {
   506 	} else if (scf_value_get_count(value, &ctid) == -1) {
   501 		goto scferror;
   507 		goto scferror;
   502 	} else {
   508 	} else {
   503 		struct_set(result, "contractid",
   509 		adr_struct_set(result, "contractid",
   504 		    data_new_integer(ctid <= INT_MAX ? ctid : -1));
   510 		    adr_data_new_integer(ctid <= INT_MAX ? ctid : -1));
   505 	}
   511 	}
   506 
   512 
   507 	if ((serr = smfu_read_enabled(instance, pg, prop, value, SCF_PG_GENERAL,
   513 	if ((serr = smfu_read_enabled(instance, pg, prop, value, SCF_PG_GENERAL,
   508 	    &bool)) != 0) {
   514 	    &bool)) != 0) {
   509 		se = smfu_maperr(serr);
   515 		se = smfu_maperr(serr);
   510 		goto error;
   516 		goto error;
   511 	}
   517 	}
   512 	data_t *persist = data_new_boolean(bool > 0);
   518 	adr_data_t *persist = adr_data_new_boolean(bool > 0);
   513 	struct_set(result, "enabled", persist);
   519 	adr_struct_set(result, "enabled", persist);
   514 
   520 
   515 	if ((serr = smfu_read_enabled(instance, pg, prop, value,
   521 	if ((serr = smfu_read_enabled(instance, pg, prop, value,
   516 	    SCF_PG_GENERAL_OVR, &bool)) == 0) {
   522 	    SCF_PG_GENERAL_OVR, &bool)) == 0) {
   517 		struct_set(result, "enabled_temp", data_new_boolean(bool > 0));
   523 		adr_struct_set(result, "enabled_temp",
       
   524 		    adr_data_new_boolean(bool > 0));
   518 	} else if (serr == SCF_ERROR_NOT_FOUND) {
   525 	} else if (serr == SCF_ERROR_NOT_FOUND) {
   519 		struct_set(result, "enabled_temp", data_ref(persist));
   526 		adr_struct_set(result, "enabled_temp", adr_data_ref(persist));
   520 	} else {
   527 	} else {
   521 		se = smfu_maperr(serr);
   528 		se = smfu_maperr(serr);
   522 		goto error;
   529 		goto error;
   523 	}
   530 	}
   524 
   531 
   527 	return (SE_OK);
   534 	return (SE_OK);
   528 
   535 
   529 scferror:
   536 scferror:
   530 	se = smfu_maperr(scf_error());
   537 	se = smfu_maperr(scf_error());
   531 error:
   538 error:
   532 	data_free(result);
   539 	adr_data_free(result);
   533 	return (se);
   540 	return (se);
   534 }
   541 }
   535 
   542 
   536 svcerr_t
   543 svcerr_t
   537 create_PGTemplate(scf_pg_tmpl_t *pgtmpl, const char *locale, data_t **ret)
   544 create_PGTemplate(scf_pg_tmpl_t *pgtmpl, const char *locale, adr_data_t **ret)
   538 {
   545 {
   539 	data_t *pt = data_new_struct(&t__PGTemplate);
   546 	adr_data_t *pt = adr_data_new_struct(&t__PGTemplate);
   540 	data_t *dstr;
   547 	adr_data_t *dstr;
   541 	char *str;
   548 	char *str;
   542 	svcerr_t se = SE_FATAL;
   549 	svcerr_t se = SE_FATAL;
   543 
   550 
   544 	if (scf_tmpl_pg_name(pgtmpl, &str) == -1)
   551 	if (scf_tmpl_pg_name(pgtmpl, &str) == -1)
   545 		goto scferror;
   552 		goto scferror;
   546 	struct_set(pt, "pgname", data_new_string(str, lt_free));
   553 	adr_struct_set(pt, "pgname", adr_data_new_string(str, LT_FREE));
   547 
   554 
   548 	if (scf_tmpl_pg_type(pgtmpl, &str) == -1)
   555 	if (scf_tmpl_pg_type(pgtmpl, &str) == -1)
   549 		goto scferror;
   556 		goto scferror;
   550 	struct_set(pt, "pgtype", data_new_string(str, lt_free));
   557 	adr_struct_set(pt, "pgtype", adr_data_new_string(str, LT_FREE));
   551 
   558 
   552 	if (scf_tmpl_pg_common_name(pgtmpl, locale, &str) == -1) {
   559 	if (scf_tmpl_pg_common_name(pgtmpl, locale, &str) == -1) {
   553 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   560 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   554 			goto scferror;
   561 			goto scferror;
   555 	} else {
   562 	} else {
   556 		if ((dstr = data_new_string(str, lt_free)) == NULL)
   563 		if ((dstr = adr_data_new_string(str, LT_FREE)) == NULL)
   557 			goto error;
   564 			goto error;
   558 		struct_set(pt, "name", dstr);
   565 		adr_struct_set(pt, "name", dstr);
   559 	}
   566 	}
   560 
   567 
   561 	if (scf_tmpl_pg_description(pgtmpl, locale, &str) == -1) {
   568 	if (scf_tmpl_pg_description(pgtmpl, locale, &str) == -1) {
   562 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   569 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   563 			goto scferror;
   570 			goto scferror;
   564 	} else {
   571 	} else {
   565 		if ((dstr = data_new_string(str, lt_free)) == NULL)
   572 		if ((dstr = adr_data_new_string(str, LT_FREE)) == NULL)
   566 			goto error;
   573 			goto error;
   567 		struct_set(pt, "description", dstr);
   574 		adr_struct_set(pt, "description", dstr);
   568 	}
   575 	}
   569 
   576 
   570 	uint8_t req;
   577 	uint8_t req;
   571 	if (scf_tmpl_pg_required(pgtmpl, &req) == -1) {
   578 	if (scf_tmpl_pg_required(pgtmpl, &req) == -1) {
   572 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   579 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   573 			goto scferror;
   580 			goto scferror;
   574 		req = 0;
   581 		req = 0;
   575 	}
   582 	}
   576 	struct_set(pt, "required", data_new_boolean(req > 0));
   583 	adr_struct_set(pt, "required", adr_data_new_boolean(req > 0));
   577 
   584 
   578 	*ret = pt;
   585 	*ret = pt;
   579 	return (SE_OK);
   586 	return (SE_OK);
   580 
   587 
   581 scferror:
   588 scferror:
   582 	se = smfu_maperr(scf_error());
   589 	se = smfu_maperr(scf_error());
   583 error:
   590 error:
   584 	data_free(pt);
   591 	adr_data_free(pt);
   585 	return (se);
   592 	return (se);
   586 }
   593 }
   587 
   594 
   588 svcerr_t
   595 svcerr_t
   589 create_PropTemplate(scf_prop_tmpl_t *proptmpl, const char *locale, data_t **ret)
   596 create_PropTemplate(scf_prop_tmpl_t *proptmpl, const char *locale,
   590 {
   597     adr_data_t **ret)
   591 	data_t *pt = data_new_struct(&t__PropTemplate);
   598 {
   592 	data_t *dstr;
   599 	adr_data_t *pt = adr_data_new_struct(&t__PropTemplate);
       
   600 	adr_data_t *dstr;
   593 	char *str;
   601 	char *str;
   594 	svcerr_t se = SE_FATAL;
   602 	svcerr_t se = SE_FATAL;
   595 
   603 
   596 	if (scf_tmpl_prop_name(proptmpl, &str) == -1)
   604 	if (scf_tmpl_prop_name(proptmpl, &str) == -1)
   597 		goto error;
   605 		goto error;
   598 	struct_set(pt, "propname", data_new_string(str, lt_free));
   606 	adr_struct_set(pt, "propname", adr_data_new_string(str, LT_FREE));
   599 
   607 
   600 	scf_type_t type;
   608 	scf_type_t type;
   601 	if (scf_tmpl_prop_type(proptmpl, &type) == -1) {
   609 	if (scf_tmpl_prop_type(proptmpl, &type) == -1) {
   602 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   610 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   603 			goto scferror;
   611 			goto scferror;
   604 		type = SCF_TYPE_INVALID;
   612 		type = SCF_TYPE_INVALID;
   605 	}
   613 	}
   606 	struct_set(pt, "proptype", create_PropertyType(type));
   614 	adr_struct_set(pt, "proptype", create_PropertyType(type));
   607 
   615 
   608 	if (scf_tmpl_prop_common_name(proptmpl, locale, &str) == -1) {
   616 	if (scf_tmpl_prop_common_name(proptmpl, locale, &str) == -1) {
   609 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   617 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   610 			goto scferror;
   618 			goto scferror;
   611 	} else {
   619 	} else {
   612 		if ((dstr = data_new_string(str, lt_free)) == NULL)
   620 		if ((dstr = adr_data_new_string(str, LT_FREE)) == NULL)
   613 			goto error;
   621 			goto error;
   614 		struct_set(pt, "name", dstr);
   622 		adr_struct_set(pt, "name", dstr);
   615 	}
   623 	}
   616 
   624 
   617 	if (scf_tmpl_prop_description(proptmpl, locale, &str) == -1) {
   625 	if (scf_tmpl_prop_description(proptmpl, locale, &str) == -1) {
   618 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   626 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   619 			goto scferror;
   627 			goto scferror;
   620 	} else {
   628 	} else {
   621 		if ((dstr = data_new_string(str, lt_free)) == NULL)
   629 		if ((dstr = adr_data_new_string(str, LT_FREE)) == NULL)
   622 			goto error;
   630 			goto error;
   623 		struct_set(pt, "description", dstr);
   631 		adr_struct_set(pt, "description", dstr);
   624 	}
   632 	}
   625 
   633 
   626 	if (scf_tmpl_prop_units(proptmpl, locale, &str) == -1) {
   634 	if (scf_tmpl_prop_units(proptmpl, locale, &str) == -1) {
   627 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   635 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   628 			goto scferror;
   636 			goto scferror;
   629 	} else {
   637 	} else {
   630 		if ((dstr = data_new_string(str, lt_free)) == NULL)
   638 		if ((dstr = adr_data_new_string(str, LT_FREE)) == NULL)
   631 			goto error;
   639 			goto error;
   632 		struct_set(pt, "units", dstr);
   640 		adr_struct_set(pt, "units", dstr);
   633 	}
   641 	}
   634 
   642 
   635 	uint8_t req;
   643 	uint8_t req;
   636 	if (scf_tmpl_prop_required(proptmpl, &req) == -1) {
   644 	if (scf_tmpl_prop_required(proptmpl, &req) == -1) {
   637 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   645 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   638 			goto scferror;
   646 			goto scferror;
   639 		req = 0;
   647 		req = 0;
   640 	}
   648 	}
   641 	struct_set(pt, "required", data_new_boolean(req > 0));
   649 	adr_struct_set(pt, "required", adr_data_new_boolean(req > 0));
   642 
   650 
   643 	uint64_t min, max;
   651 	uint64_t min, max;
   644 	if (scf_tmpl_prop_cardinality(proptmpl, &min, &max) == -1) {
   652 	if (scf_tmpl_prop_cardinality(proptmpl, &min, &max) == -1) {
   645 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   653 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   646 			goto scferror;
   654 			goto scferror;
   647 		min = max = 0;
   655 		min = max = 0;
   648 	}
   656 	}
   649 	struct_set(pt, "cardinality_min", data_new_ulong(min));
   657 	adr_struct_set(pt, "cardinality_min", adr_data_new_ulong(min));
   650 	struct_set(pt, "cardinality_max", data_new_ulong(max));
   658 	adr_struct_set(pt, "cardinality_max", adr_data_new_ulong(max));
   651 
   659 
   652 	uint8_t vis;
   660 	uint8_t vis;
   653 	if (scf_tmpl_prop_visibility(proptmpl, &vis) == -1) {
   661 	if (scf_tmpl_prop_visibility(proptmpl, &vis) == -1) {
   654 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   662 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   655 			goto scferror;
   663 			goto scferror;
   656 		vis = SCF_TMPL_VISIBILITY_READWRITE;
   664 		vis = SCF_TMPL_VISIBILITY_READWRITE;
   657 	}
   665 	}
   658 	struct_set(pt, "visibility", create_PropertyVisibility(vis));
   666 	adr_struct_set(pt, "visibility", create_PropertyVisibility(vis));
   659 
   667 
   660 	scf_values_t values;
   668 	scf_values_t values;
   661 	if (scf_tmpl_prop_internal_seps(proptmpl, &values) == 0) {
   669 	if (scf_tmpl_prop_internal_seps(proptmpl, &values) == 0) {
   662 		data_t *array =
   670 		adr_data_t *array =
   663 		    data_new_array(&t_array_string, values.value_count);
   671 		    adr_data_new_array(&adr_t_array_string, values.value_count);
   664 		for (int i = 0; i < values.value_count; i++)
   672 		for (int i = 0; i < values.value_count; i++)
   665 			(void) array_add(array, data_new_string(
   673 			(void) adr_array_add(array, adr_data_new_string(
   666 			    values.values_as_strings[i], lt_copy));
   674 			    values.values_as_strings[i], LT_COPY));
   667 		if (!data_verify(array, NULL, B_TRUE)) {
   675 		if (!adr_data_verify(array, NULL, B_TRUE)) {
   668 			data_free(array);
   676 			adr_data_free(array);
   669 			goto error;
   677 			goto error;
   670 		}
   678 		}
   671 		struct_set(pt, "separators", array);
   679 		adr_struct_set(pt, "separators", array);
   672 		scf_values_destroy(&values);
   680 		scf_values_destroy(&values);
   673 	} else {
   681 	} else {
   674 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   682 		if (scf_error() != SCF_ERROR_NOT_FOUND)
   675 			goto scferror;
   683 			goto scferror;
   676 	}
   684 	}
   679 	return (SE_OK);
   687 	return (SE_OK);
   680 
   688 
   681 scferror:
   689 scferror:
   682 	se = smfu_maperr(scf_error());
   690 	se = smfu_maperr(scf_error());
   683 error:
   691 error:
   684 	data_free(pt);
   692 	adr_data_free(pt);
   685 	return (se);
   693 	return (se);
   686 }
   694 }
   687 
   695 
   688 
   696 
   689 svcerr_t
   697 svcerr_t
   690 create_Instance(scf_instance_t *instance, const char *sname, const char *iname,
   698 create_Instance(scf_instance_t *instance, const char *sname, const char *iname,
   691     data_t **data, scf_propertygroup_t *pg, scf_property_t *prop,
   699     adr_data_t **data, scf_propertygroup_t *pg, scf_property_t *prop,
   692     scf_value_t *val)
   700     scf_value_t *val)
   693 {
   701 {
   694 	char statestr[MAX_SCF_STATE_STRING_SZ];
   702 	char statestr[MAX_SCF_STATE_STRING_SZ];
   695 	ssize_t len;
   703 	ssize_t len;
   696 	int64_t seconds;
   704 	int64_t seconds;
   711 	if (scf_pg_get_property(pg, SCF_PROPERTY_STATE_TIMESTAMP, prop) != 0 ||
   719 	if (scf_pg_get_property(pg, SCF_PROPERTY_STATE_TIMESTAMP, prop) != 0 ||
   712 	    scf_property_get_value(prop, val) != 0 ||
   720 	    scf_property_get_value(prop, val) != 0 ||
   713 	    scf_value_get_time(val, &seconds, &nsec) != 0)
   721 	    scf_value_get_time(val, &seconds, &nsec) != 0)
   714 		return (smfu_maperr(scf_error()));
   722 		return (smfu_maperr(scf_error()));
   715 
   723 
   716 	data_t *inst = data_new_struct(&t__Instance);
   724 	adr_data_t *inst = adr_data_new_struct(&t__Instance);
   717 	struct_set(inst, "fmri",
   725 	adr_struct_set(inst, "fmri",
   718 	    data_new_string(smfu_fmri_alloc(sname, iname), lt_free));
   726 	    adr_data_new_string(smfu_fmri_alloc(sname, iname), LT_FREE));
   719 	struct_set(inst, "objectName",
   727 	adr_struct_set(inst, "objectName",
   720 	    data_new_name(smfu_name_alloc(sname, iname)));
   728 	    adr_data_new_name(smfu_name_alloc(sname, iname)));
   721 	struct_set(inst, "stime", data_new_time(seconds, nsec));
   729 	adr_struct_set(inst, "stime", adr_data_new_time(seconds, nsec));
   722 	struct_set(inst, "state", create_SMFState(statestr));
   730 	adr_struct_set(inst, "state", create_SMFState(statestr));
   723 
   731 
   724 	if (!data_verify(inst, NULL, B_TRUE)) {
   732 	if (!adr_data_verify(inst, NULL, B_TRUE)) {
   725 		data_free(inst);
   733 		adr_data_free(inst);
   726 		return (SE_FATAL);
   734 		return (SE_FATAL);
   727 	}
   735 	}
   728 
   736 
   729 	*data = inst;
   737 	*data = inst;
   730 	return (SE_OK);
   738 	return (SE_OK);