Пример #1
0
/*
 * Create a service instance. returns 0 if successful.
 * If instance already exists enable it.
 */
int
smb_smf_instance_create(smb_scfhandle_t *handle, char *serv_prefix,
	char *inst_name)
{
	char *instance;
	int ret = SMBC_SMF_OK;
	int sz;

	if (handle == NULL) {
		return (SMBC_SMF_SYSTEM_ERR);
	}

	if (!serv_prefix || !inst_name) {
		return (SMBC_SMF_SYSTEM_ERR);
	}
	sz = strlen(serv_prefix) + strlen(inst_name) + 2;
	instance = malloc(sz);
	if (!instance) {
		return (SMBC_SMF_SYSTEM_ERR);
	}
	(void) snprintf(instance, sz, "%s:%s", serv_prefix, inst_name);
	handle->scf_instance = scf_instance_create(handle->scf_handle);
	if (scf_service_get_instance(handle->scf_service, inst_name,
	    handle->scf_instance) != SCF_SUCCESS) {
		if (scf_service_add_instance(handle->scf_service,
		    inst_name, handle->scf_instance) == SCF_SUCCESS) {
			if (smf_enable_instance(instance, 0))
				ret = SMBC_SMF_SYSTEM_ERR;
		} else {
			ret = SMBC_SMF_SYSTEM_ERR;
		}
	} else {
		if (smf_enable_instance(instance, 0))
			ret = SMBC_SMF_SYSTEM_ERR;
	}
	free(instance);
	return (ret);
}
Пример #2
0
static int
create_instance(scf_handle_t *handle, scf_service_t *svc,
    const char *instance_name, const char *kssl_entry, const char *command,
    const char *username, char *inaddr_any_name)
{
	int status = FAILURE;
	char *buf;
	boolean_t errflag = B_FALSE;
	ssize_t max_fmri_len;
	scf_instance_t *instance;

	instance = scf_instance_create(handle);
	if (instance == NULL) {
		errflag = B_TRUE;
		KSSL_DEBUG("scf_instance_create failed: %s\n",
		    scf_strerror(scf_error()));
		goto out;
	}
	KSSL_DEBUG("scf_instance_create succeeded\n");

	if (scf_service_get_instance(svc, inaddr_any_name, instance) == 0) {
		/* Let the caller deal with the duplicate instance */
		status = INSTANCE_ANY_EXISTS;
		goto out;
	}

	if (scf_service_add_instance(svc, instance_name, instance) != 0) {
		if (scf_error() == SCF_ERROR_EXISTS) {
			/* Let the caller deal with the duplicate instance */
			status = INSTANCE_OTHER_EXISTS;
			goto out;
		}

		errflag = B_TRUE;
		KSSL_DEBUG("scf_service_add_instance failed: %s\n",
		    scf_strerror(scf_error()));
		goto out;
	}
	KSSL_DEBUG("scf_service_add_instance succeeded\n");

	if ((add_pg_method(handle, instance, kssl_entry, "start",
		command, username) != SUCCESS) ||
	    (add_pg_method(handle, instance, kssl_entry, "refresh",
		command, username) != SUCCESS) ||
	    (add_pg_method(handle, instance, kssl_entry, "stop",
		"", username) != SUCCESS)) {
		scf_instance_destroy(instance);
		return (status);
	}

	/* enabling the instance */
	max_fmri_len = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
	if ((buf = malloc(max_fmri_len + 1)) == NULL)
		goto out;

	if (scf_instance_to_fmri(instance, buf, max_fmri_len + 1) > 0) {
		KSSL_DEBUG("instance_fmri=%s\n", buf);
		if (smf_enable_instance(buf, 0) != 0) {
			errflag = B_TRUE;
			KSSL_DEBUG(
			    "smf_enable_instance failed: %s\n",
			    scf_strerror(scf_error()));
			goto out;
		}
		status = SUCCESS;
	}

out:
	if (instance != NULL)
		scf_instance_destroy(instance);
	if (errflag)
		(void) fprintf(stderr, gettext(
		    "Unexpected fatal libscf error: %s. Exiting.\n"),
		    scf_strerror(scf_error()));
	return (status);
}