/* * smb_smf_scf_init() * * must be called before using any of the SCF functions. * Returns smb_scfhandle_t pointer if success. */ smb_scfhandle_t * smb_smf_scf_init(char *svc_name) { smb_scfhandle_t *handle; handle = malloc(sizeof (smb_scfhandle_t)); if (handle != NULL) { bzero((char *)handle, sizeof (smb_scfhandle_t)); handle->scf_state = SCH_STATE_INITIALIZING; handle->scf_handle = scf_handle_create(SCF_VERSION); if (handle->scf_handle != NULL) { if (scf_handle_bind(handle->scf_handle) == 0) { handle->scf_scope = scf_scope_create(handle->scf_handle); if (handle->scf_scope == NULL) goto err; if (scf_handle_get_local_scope( handle->scf_handle, handle->scf_scope) != 0) goto err; handle->scf_service = scf_service_create(handle->scf_handle); if (handle->scf_service == NULL) goto err; if (scf_scope_get_service(handle->scf_scope, svc_name, handle->scf_service) != SCF_SUCCESS) { goto err; } handle->scf_pg = scf_pg_create(handle->scf_handle); if (handle->scf_pg == NULL) goto err; handle->scf_state = SCH_STATE_INIT; } else { goto err; } } else { free(handle); handle = NULL; smb_smf_scf_log_error("Could not access SMF " "repository: %s\n"); } } return (handle); /* error handling/unwinding */ err: (void) smb_smf_scf_fini(handle); (void) smb_smf_scf_log_error("SMF initialization problem: %s\n"); return (NULL); }
fs_smfhandle_t * fs_smf_init(char *fmri, char *instance) { fs_smfhandle_t *handle = NULL; char *svcname, srv[MAXPATHLEN]; /* * svc name is of the form svc://network/fs/server:instance1 * FMRI portion is /network/fs/server */ snprintf(srv, MAXPATHLEN, "%s", fmri + strlen("svc:/")); svcname = strrchr(srv, ':'); if (svcname != NULL) *svcname = '\0'; svcname = srv; handle = calloc(1, sizeof (fs_smfhandle_t)); if (handle != NULL) { handle->fs_handle = scf_handle_create(SCF_VERSION); if (handle->fs_handle == NULL) goto out; if (scf_handle_bind(handle->fs_handle) != 0) goto out; handle->fs_service = scf_service_create(handle->fs_handle); handle->fs_scope = scf_scope_create(handle->fs_handle); if (scf_handle_get_local_scope(handle->fs_handle, handle->fs_scope) != 0) goto out; if (scf_scope_get_service(handle->fs_scope, svcname, handle->fs_service) != SCF_SUCCESS) { goto out; } handle->fs_pg = scf_pg_create(handle->fs_handle); handle->fs_instance = scf_instance_create(handle->fs_handle); handle->fs_property = scf_property_create(handle->fs_handle); handle->fs_value = scf_value_create(handle->fs_handle); } else { fprintf(stderr, gettext("Cannot access SMF repository: %s\n"), fmri); } return (handle); out: fs_smf_fini(handle); fprintf(stderr, gettext("SMF Initialization problems..%s\n"), fmri); return (NULL); }
targ_scf_t * mgmt_handle_init(void) { targ_scf_t *h; h = calloc(1, sizeof (targ_scf_t)); if (h == NULL) return (NULL); h->t_handle = scf_handle_create(SCF_VERSION); if (h->t_handle != NULL) { if (scf_handle_bind(h->t_handle) == 0) { h->t_scope = scf_scope_create(h->t_handle); h->t_service = scf_service_create(h->t_handle); h->t_pg = scf_pg_create(h->t_handle); h->t_instance = scf_instance_create(h->t_handle); if (scf_handle_get_scope(h->t_handle, SCF_SCOPE_LOCAL, h->t_scope) == 0) { if (scf_scope_get_service(h->t_scope, SA_TARGET_SVC_NAME, h->t_service) != 0) goto error; } else { syslog(LOG_ERR, "Got local scope which is wrong\n"); goto error; } } else goto error; } else { free(h); h = NULL; syslog(LOG_ERR, "iscsitgt could not access SMF repository: %s\n", scf_strerror(scf_error())); } return (h); error: mgmt_handle_fini(h); free(h); syslog(LOG_ERR, "iscsitgt SMF initialization problem: %s\n", scf_strerror(scf_error())); return (NULL); }
int delete_instance(const char *instance_name) { int status = FAILURE; char *buf; boolean_t errflag = B_FALSE; ssize_t max_fmri_len; scf_scope_t *scope; scf_service_t *svc; scf_handle_t *handle; scf_instance_t *instance; handle = scf_handle_create(SCF_VERSION); if (handle == NULL) { errflag = B_TRUE; KSSL_DEBUG("scf_handle_create failed: %s\n", scf_strerror(scf_error())); goto out1; } KSSL_DEBUG("scf_handle_create succeeded\n"); if (scf_handle_bind(handle) == -1) { errflag = B_TRUE; KSSL_DEBUG("scf_handle_bind failed: %s\n", scf_strerror(scf_error())); goto out1; } KSSL_DEBUG("scf_handle_bind succeeded\n"); if ((scope = scf_scope_create(handle)) == NULL) { errflag = B_TRUE; KSSL_DEBUG("scf_scope_create failed: %s\n", scf_strerror(scf_error())); goto out2; } KSSL_DEBUG("scf_scope_create succeeded\n"); if ((svc = scf_service_create(handle)) == NULL) { errflag = B_TRUE; KSSL_DEBUG("scf_service_create failed: %s\n", scf_strerror(scf_error())); goto out3; } KSSL_DEBUG("scf_service_create succeeded\n"); if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) == -1) { errflag = B_TRUE; KSSL_DEBUG("scf_handle_get_scope failed: %s\n", scf_strerror(scf_error())); goto out4; } KSSL_DEBUG("scf_handle_get_scope succeeded\n"); if (scf_scope_get_service(scope, SERVICE_NAME, svc) < 0) { scf_error_t scf_errnum = scf_error(); if (scf_errnum != SCF_ERROR_NOT_FOUND) { errflag = B_TRUE; KSSL_DEBUG( "ERROR scf_scope_get_service failed: %s\n", scf_strerror(scf_errnum)); } goto out4; } else { KSSL_DEBUG("scf_scope_get_service succeeded\n"); } instance = scf_instance_create(handle); if (instance == NULL) { errflag = B_TRUE; KSSL_DEBUG("scf_instance_create failed: %s\n", scf_strerror(scf_error())); goto out4; } if (scf_service_get_instance(svc, instance_name, instance) != 0) { scf_error_t scf_errnum = scf_error(); if (scf_errnum == SCF_ERROR_NOT_FOUND) { status = SUCCESS; } else { errflag = B_TRUE; KSSL_DEBUG( "ERROR scf_scope_get_service failed: %s\n", scf_strerror(scf_errnum)); } scf_instance_destroy(instance); goto out4; } max_fmri_len = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH); if ((buf = malloc(max_fmri_len + 1)) == NULL) goto out4; if (scf_instance_to_fmri(instance, buf, max_fmri_len + 1) > 0) { char *state; KSSL_DEBUG("instance_fmri=%s\n", buf); state = smf_get_state(buf); if (state) KSSL_DEBUG("state=%s\n", state); if (state && strcmp(state, "online") == 0) { if (smf_disable_instance(buf, 0) != 0) { errflag = B_TRUE; KSSL_DEBUG( "smf_disable_instance failed: %s\n", scf_strerror(scf_error())); } else { /* * Wait for some time till timeout to avoid * a race with scf_instance_delete() below. */ wait_till_to(buf); } } } if (scf_instance_delete(instance) != 0) { errflag = B_TRUE; KSSL_DEBUG( "ERROR scf_instance_delete failed: %s\n", scf_strerror(scf_error())); goto out4; } else { KSSL_DEBUG("deleted %s\n", instance_name); } status = SUCCESS; out4: scf_service_destroy(svc); out3: scf_scope_destroy(scope); out2: (void) scf_handle_unbind(handle); out1: if (handle != NULL) scf_handle_destroy(handle); if (errflag) (void) fprintf(stderr, gettext( "Unexpected fatal libscf error: %s. Exiting.\n"), scf_strerror(scf_error())); return (status); }