Exemplo n.º 1
0
static void
cleanupScf(scf_handle_t *h,
           scf_snapshot_t *snap,
           scf_instance_t *inst,
           scf_propertygroup_t *pg,
           scf_property_t *prop,
           scf_value_t *val,
           char *buf) {
    if (buf != NULL) {
        free(buf);
    }
    if (snap != NULL) {
        scf_snapshot_destroy(snap);
    }
    if (val != NULL) {
        scf_value_destroy(val);
    }
    if (prop != NULL) {
        scf_property_destroy(prop);
    }
    if (pg != NULL) {
        scf_pg_destroy(pg);
    }
    if (inst != NULL) {
        scf_instance_destroy(inst);
    }
    if (h != NULL) {
        scf_handle_destroy(h);
    }
}
Exemplo n.º 2
0
static scf_propertygroup_t *
add_property_group_to_instance(scf_handle_t *handle, scf_instance_t *instance,
    const char *pg_name, const char *pg_type)
{
	scf_propertygroup_t *pg;

	pg = scf_pg_create(handle);
	if (pg == NULL) {
		KSSL_DEBUG("scf_pg_create failed: %s\n",
		    scf_strerror(scf_error()));
		(void) fprintf(stderr, gettext(
		    "Unexpected fatal libscf error: %s. Exiting.\n"),
		    scf_strerror(scf_error()));
		return (NULL);
	}

	if (scf_instance_add_pg(instance, pg_name, pg_type, 0, pg) != 0) {
		KSSL_DEBUG("ERROR: scf_instance_add_pg failed: %s\n",
		    scf_strerror(scf_error()));
		if (scf_error() == SCF_ERROR_EXISTS)
			(void) fprintf(stderr, gettext(
			    "Error: another process is modifying this instance."
			    " Exiting.\n"));
		else
			(void) fprintf(stderr, gettext(
			    "Unexpected fatal libscf error: %s. Exiting.\n"),
			    scf_strerror(scf_error()));
		scf_pg_destroy(pg);
		return (NULL);
	} else {
		KSSL_DEBUG("property group created\n");
	}

	return (pg);
}
Exemplo n.º 3
0
/*
 * smb_smf_scf_fini(handle)
 *
 * must be called when done. Called with the handle allocated in
 * smb_smf_scf_init(), it cleans up the state and frees any SCF resources
 * still in use.
 */
void
smb_smf_scf_fini(smb_scfhandle_t *handle)
{
	if (handle != NULL) {
		int unbind = 0;
		scf_iter_destroy(handle->scf_pg_iter);
		handle->scf_pg_iter = NULL;

		scf_iter_destroy(handle->scf_inst_iter);
		handle->scf_inst_iter = NULL;

		unbind = 1;
		scf_scope_destroy(handle->scf_scope);
		handle->scf_scope = NULL;

		scf_instance_destroy(handle->scf_instance);
		handle->scf_instance = NULL;

		scf_service_destroy(handle->scf_service);
		handle->scf_service = NULL;

		scf_pg_destroy(handle->scf_pg);
		handle->scf_pg = NULL;

		handle->scf_state = SCH_STATE_UNINIT;
		if (unbind)
			(void) scf_handle_unbind(handle->scf_handle);
		scf_handle_destroy(handle->scf_handle);
		handle->scf_handle = NULL;

		free(handle);
	}
}
Exemplo n.º 4
0
void
mgmt_handle_fini(targ_scf_t *h)
{
	if (h != NULL) {
		int	unbind = 0;
		if (h->t_scope != NULL) {
			unbind = 1;
			scf_scope_destroy(h->t_scope);
			h->t_scope = NULL;
		}
		if (h->t_instance != NULL) {
			scf_instance_destroy(h->t_instance);
			h->t_instance = NULL;
		}
		if (h->t_service != NULL) {
			scf_service_destroy(h->t_service);
			h->t_service = NULL;
		}
		if (h->t_pg != NULL) {
			scf_pg_destroy(h->t_pg);
			h->t_pg = NULL;
		}
		if (h->t_handle != NULL) {
			if (unbind)
				(void) scf_handle_unbind(h->t_handle);
			scf_handle_destroy(h->t_handle);
			h->t_handle = NULL;
		}
		free(h);
		h = NULL;
	}
}
Exemplo n.º 5
0
static void
delete_prop(const scf_instance_t *inst, const char *pg, const char *prop)
{
	scf_transaction_t		*tx;
	scf_transaction_entry_t		*ent;
	scf_propertygroup_t		*gpg;
	scf_property_t			*eprop;
	int				ret;

	if ((gpg = scf_pg_create(h)) == NULL ||
	    (eprop = scf_property_create(h)) == NULL ||
	    (tx = scf_transaction_create(h)) == NULL ||
	    (ent = scf_entry_create(h)) == NULL)
		scfdie();

	if (scf_instance_get_pg(inst, pg, gpg) != SCF_SUCCESS) {
		if (scf_error() != SCF_ERROR_NOT_FOUND)
			scfdie();

		uu_die(gettext("Error: \"%s\" property group missing.\n"), pg);
	}

	do {
		if (scf_transaction_start(tx, gpg) != SCF_SUCCESS) {
			if (scf_error() != SCF_ERROR_PERMISSION_DENIED)
				scfdie();

			uu_die(gettext("Error: Permission denied.\n"));
		}

		if (scf_transaction_property_delete(tx, ent,
		    prop) != SCF_SUCCESS) {
			if (scf_error() != SCF_ERROR_NOT_FOUND)
				scfdie();

			uu_die(
			    gettext("Error: \"%s\" property does not exist.\n"),
			    prop);
		}

		ret = scf_transaction_commit(tx);
		if (ret < 0) {
			if (scf_error() != SCF_ERROR_PERMISSION_DENIED)
				scfdie();

			uu_die(gettext("Error: Permission denied.\n"));
		}
		if (ret == 0) {
			scf_transaction_reset(tx);
			if (scf_pg_update(gpg) == -1)
				scfdie();
		}
	} while (ret == 0);

	(void) scf_entry_destroy(ent);
	scf_transaction_destroy(tx);
	scf_property_destroy(eprop);
	scf_pg_destroy(gpg);
}
Exemplo n.º 6
0
conerr_t
smfu_get_property(char *fmri, char *pgname, char *propname, char *value,
    size_t n)
{
	conerr_t err = ce_ok;
	scf_handle_t *scfhandle = handle_create();
	scf_service_t *service = scf_service_create(scfhandle);
	scf_instance_t *instance = scf_instance_create(scfhandle);
	scf_propertygroup_t *pg = scf_pg_create(scfhandle);
	scf_property_t *prop = scf_property_create(scfhandle);
	scf_iter_t *iter = scf_iter_create(scfhandle);
	scf_value_t *val = scf_value_create(scfhandle);

	if (scfhandle == NULL || service == NULL || instance == NULL ||
	    pg == NULL || prop == NULL || iter == NULL || val == NULL) {
		err = ce_nomem;
		goto out;
	}

	if (scf_handle_decode_fmri(scfhandle, fmri, NULL, service, instance,
	    NULL, NULL, 0) != SCF_SUCCESS) {
		rad_log(RL_ERROR, "couldn't decode '%s': %s\n", fmri,
		    scf_strerror(scf_error()));
		err = maperr(scf_error());
		goto out;
	}

	if (scf_instance_get_pg(instance, pgname, pg) != 0 ||
	    scf_pg_get_property(pg, propname, prop) != 0 ||
	    scf_iter_property_values(iter, prop) != 0) {
		rad_log(RL_ERROR, "couldn't get property: '%s/%s/%s': %s\n",
		    fmri, pgname, propname, scf_strerror(scf_error()));
		err = maperr(scf_error());
		goto out;
	}

	if (scf_iter_next_value(iter, val) > 0 &&
	    scf_value_get_as_string(val, value, n) == -1) {
		rad_log(RL_ERROR,
		    "couldn't get property value: '%s/%s/%s': %s\n", fmri,
		    pgname, propname, scf_strerror(scf_error()));
		err = maperr(scf_error());
		goto out;
	}
out:
	scf_value_destroy(val);
	scf_property_destroy(prop);
	scf_pg_destroy(pg);
	scf_iter_destroy(iter);
	scf_instance_destroy(instance);
	scf_service_destroy(service);
	scf_handle_destroy(scfhandle);

	return (err);
}
Exemplo n.º 7
0
/*
 * Inputs:
 *   res is a pointer to the scf_resources_t to be released.
 */
static void
release_scf_resources(scf_resources_t *res)
{
	scf_value_destroy(res->sr_val);
	scf_property_destroy(res->sr_prop);
	scf_pg_destroy(res->sr_pg);
	scf_snapshot_destroy(res->sr_snap);
	scf_instance_destroy(res->sr_inst);
	(void) scf_handle_unbind(res->sr_handle);
	scf_handle_destroy(res->sr_handle);
}
Exemplo n.º 8
0
void
fs_smf_fini(fs_smfhandle_t *handle)
{
	if (handle != NULL) {
		scf_scope_destroy(handle->fs_scope);
		scf_instance_destroy(handle->fs_instance);
		scf_service_destroy(handle->fs_service);
		scf_pg_destroy(handle->fs_pg);
		scf_property_destroy(handle->fs_property);
		scf_value_destroy(handle->fs_value);
		if (handle->fs_handle != NULL) {
			scf_handle_unbind(handle->fs_handle);
			scf_handle_destroy(handle->fs_handle);
		}
		free(handle);
	}
}
Exemplo n.º 9
0
void
load_fini(void)
{
	scf_iter_destroy(load_propiter);
	load_propiter = NULL;
	scf_iter_destroy(load_valiter);
	load_valiter = NULL;
	scf_iter_destroy(load_pgiter);
	load_pgiter = NULL;
	scf_pg_destroy(load_pgroup);
	load_pgroup = NULL;
	scf_value_destroy(load_val);
	load_val = NULL;
	scf_property_destroy(load_prop);
	load_prop = NULL;
	free(loadbuf);
	loadbuf = NULL;
}
Status osDaemonIsEnabled( int inQuiet )
{
	Status			isEnabled = sStatusNotEnabled;
	scf_handle_t *	theHandle = scf_handle_create( SCF_VERSION );
	if (inQuiet == 0) { writeInitialMessage( sOpIsEnabled ); }
	if ( theHandle != 0 )
	{
		if ( scf_handle_bind( theHandle ) == 0 )
		{
			scf_instance_t * theInstance = scf_instance_create( theHandle );
			if ( theInstance != 0 )
			{
				if ( scf_handle_decode_fmri(
						theHandle,
						sInstanceName,
						0,
						0,
						theInstance,
						0,
						0,
						SCF_DECODE_FMRI_EXACT ) != -1 )
				{
					scf_handle_t * theInstanceHandle =
						scf_instance_handle( theInstance );
					if ( theInstanceHandle != 0 )
					{
						uint8_t					theEnabled;
						scf_propertygroup_t *	theGroup	=
							scf_pg_create( theInstanceHandle );
						scf_property_t *		theProp		=
							scf_property_create( theInstanceHandle );
						scf_value_t *			theValue	=
							scf_value_create( theInstanceHandle );
						if ( theGroup != 0 && theProp != 0 && theValue != 0 )
						{
							if ( scf_instance_get_pg( theInstance, 
							                          SCF_PG_GENERAL,
							                          theGroup ) == 0		&&
							     scf_pg_get_property( theGroup,
							                          SCF_PROPERTY_ENABLED,
							                          theProp ) == 0		&&
							     scf_property_get_value( theProp,
							                             theValue ) == 0	&&
							     scf_value_get_boolean( theValue,	
							                            &theEnabled ) == 0 )
								{
							     	isEnabled = theEnabled == 1 ?
										sStatusEnabled : sStatusNotEnabled;	
								}
						}
						scf_pg_destroy( theGroup );
						scf_property_destroy( theProp );
						scf_value_destroy( theValue );
					}
				}
				scf_instance_destroy( theInstance );
			}
		}
		scf_handle_destroy( theHandle );
	}
	if (inQuiet == 0) { writeFinalMessage( sOpIsEnabled, isEnabled ); }
	return isEnabled;
}
Exemplo n.º 11
0
static int
add_pg_method(scf_handle_t *handle, scf_instance_t *instance,
    const char *kssl_entry, const char *pg_name, const char *flags,
    const char *value_str)
{
	int len, rv;
	char *command;
	const char *base_command;
	int status = FAILURE;
	boolean_t errflag = B_FALSE;
	scf_transaction_t *tran;
	scf_propertygroup_t *pg;

	pg = add_property_group_to_instance(handle, instance,
	    pg_name, SCF_GROUP_METHOD);
	if (pg == NULL) {
		/* flag is false to suppress duplicate error messages */
		errflag = B_FALSE;
		goto out0;
	}
	KSSL_DEBUG("%s method added\n", pg_name);

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

	do {
		if (scf_transaction_start(tran, pg) != 0) {
			KSSL_DEBUG("scf_transaction_start failed: %s\n",
			    scf_strerror(scf_error()));
			if (scf_error() == SCF_ERROR_PERMISSION_DENIED) {
				(void) fprintf(stderr, gettext(
				    "Error: Permission denied.\n"));
				errflag = B_FALSE;
			} else if (scf_error() ==  SCF_ERROR_DELETED) {
				(void) fprintf(stderr, gettext(
				    "Error: property group %s has"
				    " been deleted.\n"), pg_name);
				errflag = B_FALSE;
			} else
				errflag = B_TRUE;
			goto out1;
		}
		KSSL_DEBUG("scf_transaction_start succeeded\n");

		if (strcmp(pg_name, "stop") == 0)
			base_command = "/usr/lib/kssladm delete";
		else
			base_command = "/usr/lib/kssladm create";

		len = strlen(base_command) + strlen(flags) +
		    strlen(kssl_entry) + 3;

		command = malloc(len);
		if (command == NULL) {
			goto out2;
		}

		(void) snprintf(command, len, "%s %s %s",
		    base_command, flags, kssl_entry);
		KSSL_DEBUG("command=%s\n", command);

		if (add_new_property(handle, SCF_PROPERTY_EXEC,
		    SCF_TYPE_ASTRING, command, tran) != SUCCESS) {
			free(command);
			goto out2;
		}
		free(command);

		if (add_new_property(handle, SCF_PROPERTY_TIMEOUT,
		    SCF_TYPE_COUNT, "60", tran) != SUCCESS)
			goto out2;

		if (set_method_context(handle, tran, value_str) != SUCCESS)
			goto out2;

		rv = scf_transaction_commit(tran);
		switch (rv) {
		case 1:
			KSSL_DEBUG("scf_transaction_commit succeeded\n");
			status = SUCCESS;
			goto out2;
		case 0:
			scf_transaction_reset(tran);
			if (scf_pg_update(pg) == -1) {
				goto out2;
			}
			break;
		case -1:
		default:
			KSSL_DEBUG("ERROR: scf_transaction_commit failed: %s\n",
			    scf_strerror(scf_error()));
			if (scf_error() == SCF_ERROR_PERMISSION_DENIED) {
				(void) fprintf(stderr, gettext(
				    "Error: Permission denied.\n"));
				errflag = B_FALSE;
			} else {
				errflag = B_TRUE;
			}
			goto out2;
		}
	} while (rv == 0);

out2:
	scf_transaction_reset(tran);
out1:
	scf_transaction_destroy_children(tran);
	scf_transaction_destroy(tran);
out0:
	if (pg != NULL)
		scf_pg_destroy(pg);
	if (errflag)
		(void) fprintf(stderr, gettext(
		    "Unexpected fatal libscf error: %s. Exiting.\n"),
		    scf_strerror(scf_error()));
	return (status);
}
Exemplo n.º 12
0
static void
modify_prop(const scf_instance_t *inst, const char *pg, const char *prop,
    scf_type_t type, void *value)
{
	scf_transaction_t		*tx;
	scf_transaction_entry_t		*ent;
	scf_propertygroup_t		*gpg;
	scf_property_t			*eprop;
	scf_value_t			*v;
	int				ret, create = 0;
	char				*fmri;
	ssize_t				max_fmri_len;

	if ((gpg = scf_pg_create(h)) == NULL ||
	    (eprop = scf_property_create(h)) == NULL ||
	    (v = scf_value_create(h)) == NULL)
		scfdie();

	/* Get the property group or create it if it is missing. */
	if (scf_instance_get_pg(inst, pg, gpg) == -1) {
		if (scf_error() != SCF_ERROR_NOT_FOUND)
			scfdie();

		max_fmri_len = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
		if ((fmri = malloc(max_fmri_len + 1)) == NULL)
			uu_die(gettext("Error: Out of memory.\n"));

		if (scf_instance_to_fmri(inst, fmri, max_fmri_len + 1) < 0)
			scfdie();

		syslog(LOG_NOTICE, "inetadm: Property group \"%s\" missing "
		    "from \"%s\", attempting to add it.\n", pg, fmri);
		free(fmri);

		if (scf_instance_add_pg(inst, pg, SCF_GROUP_FRAMEWORK, 0,
		    gpg) == -1) {
			switch (scf_error()) {
			case SCF_ERROR_EXISTS:
				break;
			case SCF_ERROR_PERMISSION_DENIED:
				uu_die(gettext("Error: Permission denied.\n"));
			default:
				scfdie();
			}
		}
	}

	if (scf_pg_get_property(gpg, prop, eprop) == -1) {
		if (scf_error() != SCF_ERROR_NOT_FOUND)
			scfdie();

		create = 1;
	}

	if ((tx = scf_transaction_create(h)) == NULL ||
	    (ent = scf_entry_create(h)) == NULL)
		scfdie();

	do {
		uu_list_t	*sv_list;

		if (scf_transaction_start(tx, gpg) == -1) {
			if (scf_error() != SCF_ERROR_PERMISSION_DENIED)
				scfdie();

			uu_die(gettext("Error: Permission denied.\n"));
		}

		/* Modify the property or create it if it is missing */
		if (create)
			ret = scf_transaction_property_new(tx, ent, prop, type);
		else
			ret = scf_transaction_property_change_type(tx, ent,
			    prop, type);
		if (ret == -1)
			scfdie();

		switch (type) {
		case SCF_TYPE_BOOLEAN:
			scf_value_set_boolean(v, *(uint8_t *)value);
			break;
		case SCF_TYPE_INTEGER:
			scf_value_set_integer(v, *(int64_t *)value);
			break;
		case SCF_TYPE_ASTRING:
			if (strcmp(prop, PR_PROTO_NAME) == 0) {
				add_proto_list(ent, h, (char **)value,
				    &sv_list);
			} else if (scf_value_set_astring(v, value) == -1) {
				scfdie();
			}
			break;
		default:
			uu_die(gettext("Error: Internal inetadm error"));
		}

		if ((strcmp(prop, PR_PROTO_NAME) != 0) &&
		    (scf_entry_add_value(ent, v) == -1))
			scfdie();

		ret = scf_transaction_commit(tx);
		if (ret == -1) {
			if (scf_error() != SCF_ERROR_PERMISSION_DENIED)
				scfdie();

			uu_die(gettext("Error: Permission denied.\n"));
		}

		scf_transaction_reset(tx);

		if (ret == 0) {
			if (scf_pg_update(gpg) == -1)
				scfdie();
		}

		if (strcmp(prop, PR_PROTO_NAME) == 0)
			remove_proto_list(ent, sv_list);

	} while (ret == 0);

	scf_value_destroy(v);
	scf_entry_destroy(ent);
	scf_transaction_destroy(tx);
	scf_property_destroy(eprop);
	scf_pg_destroy(gpg);
}
Exemplo n.º 13
0
/* ARGSUSED */
static int
do_wait(void *unused, scf_walkinfo_t *wip)
{
	scf_property_t *prop;
	scf_propertygroup_t *lpg, *pg;
	const char *propname;
	svcprop_prop_node_t *p;

	const char *emsg_not_found = gettext("Not found.\n");

	if ((lpg = scf_pg_create(hndl)) == NULL ||
	    (prop = scf_property_create(hndl)) == NULL)
		scfdie();

	if (wip->prop != NULL) {
		if (uu_list_numnodes(prop_list) > 0)
			uu_xdie(UU_EXIT_USAGE, gettext("-p cannot be used with "
			    "property FMRIs.\n"));
		pg = wip->pg;

		assert(strrchr(wip->fmri, '/') != NULL);
		propname = strrchr(wip->fmri, '/') + 1;

	} else if (wip->pg != NULL) {
		p = uu_list_first(prop_list);

		if (p != NULL) {
			if (p->spn_comp2 != NULL)
				uu_xdie(UU_EXIT_USAGE, gettext("-p argument "
				    "\"%s/%s\" has too many components for "
				    "property group %s.\n"),
				    p->spn_comp1, p->spn_comp2, wip->fmri);

			propname = p->spn_comp1;

			if (scf_pg_get_property(wip->pg, propname, prop) !=
			    SCF_SUCCESS) {
				switch (scf_error()) {
				case SCF_ERROR_INVALID_ARGUMENT:
					uu_xdie(UU_EXIT_USAGE,
					    gettext("Invalid property name "
					    "\"%s\".\n"), propname);

					/* NOTREACHED */

				case SCF_ERROR_NOT_FOUND:
					die(emsg_not_found);

					/* NOTREACHED */

				default:
					scfdie();
				}
			}
		} else {
			propname = NULL;
		}

		pg = wip->pg;

	} else if (wip->inst != NULL) {

		p = uu_list_first(prop_list);
		if (p == NULL)
			uu_xdie(UU_EXIT_USAGE,
			    gettext("Cannot wait for an instance.\n"));

		if (scf_instance_get_pg(wip->inst, p->spn_comp1, lpg) !=
		    SCF_SUCCESS) {
			switch (scf_error()) {
			case SCF_ERROR_INVALID_ARGUMENT:
				uu_xdie(UU_EXIT_USAGE, gettext("Invalid "
				    "property group name \"%s\".\n"),
				    p->spn_comp1);

			case SCF_ERROR_NOT_FOUND:
				die(emsg_not_found);

				/* NOTREACHED */

			default:
				scfdie();
			}
		}

		propname = p->spn_comp2;

		if (propname != NULL) {
			if (scf_pg_get_property(lpg, propname, prop) !=
			    SCF_SUCCESS) {
				switch (scf_error()) {
				case SCF_ERROR_INVALID_ARGUMENT:
					uu_xdie(UU_EXIT_USAGE,
					    gettext("Invalid property name "
					    "\"%s\".\n"), propname);

				case SCF_ERROR_NOT_FOUND:
					die(emsg_not_found);

					/* NOTREACHED */

				default:
					scfdie();
				}
			}
		}

		pg = lpg;

	} else if (wip->svc != NULL) {

		p = uu_list_first(prop_list);
		if (p == NULL)
			uu_xdie(UU_EXIT_USAGE,
			    gettext("Cannot wait for a service.\n"));

		if (scf_service_get_pg(wip->svc, p->spn_comp1, lpg) !=
		    SCF_SUCCESS) {
			switch (scf_error()) {
			case SCF_ERROR_INVALID_ARGUMENT:
				uu_xdie(UU_EXIT_USAGE, gettext("Invalid "
				    "property group name \"%s\".\n"),
				    p->spn_comp1);

			case SCF_ERROR_NOT_FOUND:
				die(emsg_not_found);

			default:
				scfdie();
			}
		}

		propname = p->spn_comp2;

		if (propname != NULL) {
			if (scf_pg_get_property(lpg, propname, prop) !=
			    SCF_SUCCESS) {
				switch (scf_error()) {
				case SCF_ERROR_INVALID_ARGUMENT:
					uu_xdie(UU_EXIT_USAGE,
					    gettext("Invalid property name "
					    "\"%s\".\n"), propname);

					/* NOTREACHED */

				case SCF_ERROR_NOT_FOUND:
					die(emsg_not_found);

					/* NOTREACHED */

				default:
					scfdie();
				}
			}
		}

		pg = lpg;

	} else {
		uu_xdie(UU_EXIT_USAGE, gettext("FMRI must specify an entity, "
		    "property group, or property.\n"));
	}

	for (;;) {
		int ret;

		ret = _scf_pg_wait(pg, -1);
		if (ret != SCF_SUCCESS)
			scfdie();

		ret = scf_pg_update(pg);
		if (ret < 0) {
			if (scf_error() != SCF_ERROR_DELETED)
				scfdie();

			die(emsg_not_found);
		}
		if (ret == SCF_COMPLETE)
			break;
	}

	if (propname != NULL) {
		if (scf_pg_get_property(pg, propname, prop) == SCF_SUCCESS) {
			if (!quiet)
				display_prop(pg, prop);
		} else {
			if (scf_error() != SCF_ERROR_NOT_FOUND)
				scfdie();

			if (PRINT_NOPROP_ERRORS)
				uu_warn(emsg_not_found);

			return_code = UU_EXIT_FATAL;
		}
	} else {
		if (!quiet)
			display_pg(pg);
	}

	scf_property_destroy(prop);
	scf_pg_destroy(lpg);

	return (0);
}
Exemplo n.º 14
0
/*
 * Entity (service or instance): If there are -p options,
 * display_{pg,prop}() the named property groups and/or properties.  Otherwise
 * display_pg() all property groups.
 */
static void
process_ent(scf_entityp_t ent)
{
	scf_snapshot_t *snap = NULL;
	scf_propertygroup_t *pg;
	scf_property_t *prop;
	scf_iter_t *iter;
	svcprop_prop_node_t *spn;
	int ret, err;

	if (uu_list_numnodes(prop_list) == 0) {
		if (quiet)
			return;

		if ((pg = scf_pg_create(hndl)) == NULL ||
		    (iter = scf_iter_create(hndl)) == NULL)
			scfdie();

		if (cflag || Cflag || ent.type != ENT_INSTANCE) {
			if (scf_iter_entity_pgs(iter, ent) == -1)
				scfdie();
		} else {
			if (snapshot != NULL)
				snap = get_snapshot(ent.u.inst, snapshot);

			if (scf_iter_instance_pgs_composed(iter, ent.u.inst,
			    snap) == -1)
				scfdie();
			if (snap)
				scf_snapshot_destroy(snap);
		}

		while ((ret = scf_iter_next_pg(iter, pg)) == 1)
			display_pg(pg);
		if (ret == -1)
			scfdie();

		/*
		 * In normal usage, i.e. against the running snapshot,
		 * we must iterate over the current non-persistent
		 * pg's.
		 */
		if (sflag == 0 && snap != NULL) {
			scf_iter_reset(iter);
			if (scf_iter_instance_pgs_composed(iter, ent.u.inst,
			    NULL) == -1)
				scfdie();
			while ((ret = scf_iter_next_pg(iter, pg)) == 1) {
				uint32_t flags;

				if (scf_pg_get_flags(pg, &flags) == -1)
					scfdie();
				if (flags & SCF_PG_FLAG_NONPERSISTENT)
					display_pg(pg);
			}
		}
		if (ret == -1)
			scfdie();

		scf_iter_destroy(iter);
		scf_pg_destroy(pg);

		return;
	}

	if ((pg = scf_pg_create(hndl)) == NULL ||
	    (prop = scf_property_create(hndl)) == NULL)
		scfdie();

	if (ent.type == ENT_INSTANCE && snapshot != NULL)
		snap = get_snapshot(ent.u.inst, snapshot);

	for (spn = uu_list_first(prop_list);
	    spn != NULL;
	    spn = uu_list_next(prop_list, spn)) {
		if (ent.type == ENT_INSTANCE) {
			if (Cflag)
				ret = scf_instance_get_pg(ent.u.inst,
				    spn->spn_comp1, pg);
			else
				ret = scf_instance_get_pg_composed(ent.u.inst,
				    snap, spn->spn_comp1, pg);
			err = scf_error();

			/*
			 * If we didn't find it in the specified snapshot, use
			 * the current values if the pg is nonpersistent.
			 */
			if (ret == -1 && !Cflag &&snap != NULL && err ==
			    SCF_ERROR_NOT_FOUND) {
				ret = scf_instance_get_pg_composed(
				    ent.u.inst, NULL, spn->spn_comp1,
				    pg);

				if (ret == 0) {
					uint32_t flags;

					if (scf_pg_get_flags(pg, &flags) == -1)
						scfdie();
					if ((flags & SCF_PG_FLAG_NONPERSISTENT)
					    == 0) {
						ret = -1;
					}
				}
			}
		} else {
			/*
			 * If we are displaying properties for a service,
			 * treat it as though it were a composed, current
			 * lookup. (implicit cflag) However, if a snapshot
			 * was specified, fail.
			 */
			if (sflag)
				die(gettext("Only instances have "
				    "snapshots.\n"));
			ret = scf_entity_get_pg(ent, spn->spn_comp1, pg);
			err = scf_error();
		}
		if (ret == -1) {
			if (err != SCF_ERROR_NOT_FOUND)
				scfdie();

			if (PRINT_NOPROP_ERRORS) {
				char *buf;

				buf = safe_malloc(max_scf_fmri_length + 1);
				if (scf_entity_to_fmri(ent, buf,
				    max_scf_fmri_length + 1) == -1)
					scfdie();

				uu_warn(gettext("Couldn't find property group "
				    "`%s' for %s `%s'.\n"), spn->spn_comp1,
				    SCF_ENTITY_TYPE_NAME(ent), buf);

				free(buf);
			}

			noprop_common_action();

			continue;
		}

		if (spn->spn_comp2 == NULL) {
			if (!quiet)
				display_pg(pg);
			continue;
		}

		if (scf_pg_get_property(pg, spn->spn_comp2, prop) == -1) {
			if (scf_error() != SCF_ERROR_NOT_FOUND)
				scfdie();

			if (PRINT_NOPROP_ERRORS) {
				char *buf;

				buf = safe_malloc(max_scf_fmri_length + 1);
				if (scf_entity_to_fmri(ent, buf,
				    max_scf_fmri_length + 1) == -1)
					scfdie();

				/* FMRI syntax knowledge */
				uu_warn(gettext("Couldn't find property "
				    "`%s/%s' for %s `%s'.\n"), spn->spn_comp1,
				    spn->spn_comp2, SCF_ENTITY_TYPE_NAME(ent),
				    buf);

				free(buf);
			}

			noprop_common_action();

			continue;
		}

		if (!quiet)
			display_prop(pg, prop);
	}

	scf_property_destroy(prop);
	scf_pg_destroy(pg);
	if (snap)
		scf_snapshot_destroy(snap);
}
Exemplo n.º 15
0
conerr_t
smfu_set_property(char *fmri, char *pgname, char *propname, char *value)
{
	conerr_t err = ce_ok;
	scf_handle_t *scfhandle = handle_create();
	scf_service_t *service = scf_service_create(scfhandle);
	scf_instance_t *instance = scf_instance_create(scfhandle);
	scf_propertygroup_t *pg = scf_pg_create(scfhandle);
	scf_property_t *prop = scf_property_create(scfhandle);
	scf_value_t *val = scf_value_create(scfhandle);
	scf_transaction_t *tx = scf_transaction_create(scfhandle);
	scf_transaction_entry_t *ent = scf_entry_create(scfhandle);
	scf_type_t type;

	if (scfhandle == NULL || service == NULL || instance == NULL ||
	    pg == NULL || prop == NULL || tx == NULL || ent == NULL ||
	    val == NULL) {
		err = ce_nomem;
		goto out;
	}

	if (scf_handle_decode_fmri(scfhandle, fmri, NULL, service, instance,
	    NULL, NULL, 0) != SCF_SUCCESS) {
		rad_log(RL_ERROR, "couldn't decode '%s': %s\n", fmri,
		    scf_strerror(scf_error()));
		err = maperr(scf_error());
		goto out;
	}

	if (scf_instance_get_pg(instance, pgname, pg) != 0 ||
	    scf_pg_get_property(pg, propname, prop) != 0 ||
	    scf_property_type(prop, &type) != 0) {
		rad_log(RL_ERROR, "couldn't get property: '%s/%s/%s': %s\n",
		    fmri, pgname, propname, scf_strerror(scf_error()));
		err = maperr(scf_error());
		goto out;
	}

top:
	if (scf_transaction_start(tx, pg) == -1 ||
	    scf_transaction_property_change(tx, ent, propname, type) != 0 ||
	    scf_value_set_from_string(val, type, value) != 0 ||
	    scf_entry_add_value(ent, val) != 0) {
		rad_log(RL_ERROR, "couldn't set property: '%s/%s/%s': %s\n",
		    fmri, pgname, propname, scf_strerror(scf_error()));
		err = maperr(scf_error());
		goto out;
	}

	switch (scf_transaction_commit(tx)) {
	/* success */
	case 1:
		if (smf_refresh_instance(fmri) != 0) {
			err = maperr(scf_error());
			goto out;
		}
		break;
	/* retry */
	case 0:
		if (scf_pg_update(pg) != 0) {
			err = maperr(scf_error());
			goto out;
		}
		scf_transaction_reset(tx);
		goto top;
	default:
		err = maperr(scf_error());
		goto out;
	}
out:
	scf_entry_destroy(ent);
	scf_transaction_destroy(tx);
	scf_value_destroy(val);
	scf_property_destroy(prop);
	scf_pg_destroy(pg);
	scf_instance_destroy(instance);
	scf_service_destroy(service);
	scf_handle_destroy(scfhandle);

	return (err);
}
Exemplo n.º 16
0
static void
kbd_defaults(int kbd)
{
    scf_handle_t *h = NULL;
    scf_snapshot_t *snap = NULL;
    scf_instance_t *inst = NULL;
    scf_propertygroup_t *pg = NULL;
    scf_property_t *prop = NULL;
    scf_value_t *val = NULL;

    int layout_num;
    char *val_layout = NULL, *val_abort = NULL;
    uint8_t val_click;
    int64_t val_delay, val_rate;
    int64_t val_kbd_beeper, val_console_beeper;

    if ((h = scf_handle_create(SCF_VERSION)) == NULL ||
            scf_handle_bind(h) != 0 ||
            (inst = scf_instance_create(h)) == NULL ||
            (snap = scf_snapshot_create(h)) == NULL ||
            (pg = scf_pg_create(h)) == NULL ||
            (prop = scf_property_create(h)) == NULL ||
            (val = scf_value_create(h)) == NULL) {
        goto out;
    }

    if (scf_handle_decode_fmri(h, KBD_FMRI, NULL, NULL, inst,
                               NULL, NULL, SCF_DECODE_FMRI_REQUIRE_INSTANCE) != 0) {
        goto out;
    }

    if (scf_instance_get_snapshot(inst, "running", snap) != 0) {
        scf_snapshot_destroy(snap);
        snap = NULL;
    }

    if (scf_instance_get_pg_composed(inst, snap, KBD_PG, pg) != 0) {
        goto out;
    }

    if ((val_abort = malloc(KBD_MAX_NAME_LEN)) == NULL) {
        (void) fprintf(stderr,
                       "Can not alloc memory for keyboard properties\n");
        goto out;
    }

    if ((val_layout = malloc(KBD_MAX_NAME_LEN)) == NULL) {
        (void) fprintf(stderr,
                       "Can not alloc memory for keyboard properties\n");
        goto out;
    }

    if (scf_pg_get_property(pg, KBD_PROP_KEYCLICK, prop) != 0 ||
            scf_property_get_value(prop, val) != 0 ||
            scf_value_get_boolean(val, &val_click) == -1) {
        (void) fprintf(stderr, "Can not get KEYCLICK\n");
    }

    if (val_click == 1)
        (void) click("on", kbd);
    else if (val_click == 0)
        (void) click("off", kbd);
    else
        (void) fprintf(stderr,
                       BAD_DEFAULT_INT, KBD_PROP_KEYCLICK, val_click);

    if (scf_pg_get_property(pg, KBD_PROP_KEYBOARD_ABORT, prop) != 0 ||
            scf_property_get_value(prop, val) != 0 ||
            scf_value_get_astring(val, val_abort, KBD_MAX_NAME_LEN) == -1) {
        (void) fprintf(stderr, "Can not get KEYBOARD_ABORT\n");
    }

    if (*val_abort != '\0') {
        /*
         * ABORT must equal "enable", "disable" or "alternate"
         */
        if ((strcmp(val_abort, "enable") == 0) ||
                (strcmp(val_abort, "alternate") == 0) ||
                (strcmp(val_abort, "disable") == 0))
            (void) abort_enable(val_abort, kbd);
        else
            (void) fprintf(stderr, BAD_DEFAULT_STR,
                           KBD_PROP_KEYBOARD_ABORT, val_abort);
    }

    if (scf_pg_get_property(pg, KBD_PROP_RPTDELAY, prop) != 0 ||
            scf_property_get_value(prop, val) != 0 ||
            scf_value_get_integer(val, &val_delay) == -1) {
        (void) fprintf(stderr, "Can not get RPTDELAY\n");
    }

    if (val_delay > 0)
        (void) set_rptdelay(val_delay, kbd);
    else
        (void) fprintf(stderr,
                       BAD_DEFAULT_LLINT, KBD_PROP_RPTDELAY, val_delay);

    if (scf_pg_get_property(pg, KBD_PROP_RPTRATE, prop) != 0 ||
            scf_property_get_value(prop, val) != 0 ||
            scf_value_get_integer(val, &val_rate) == -1) {
        (void) fprintf(stderr, "Can not get RPTRATE\n");
    }

    if (val_rate > 0)
        (void) set_rptrate(val_rate, kbd);
    else
        (void) fprintf(stderr,
                       BAD_DEFAULT_LLINT, KBD_PROP_RPTRATE, val_rate);

    if (scf_pg_get_property(pg, KBD_PROP_LAYOUT, prop) != 0 ||
            scf_property_get_value(prop, val) != 0 ||
            scf_value_get_astring(val, val_layout, KBD_MAX_NAME_LEN) == -1) {
        (void) fprintf(stderr, "Can not get LAYOUT\n");
    }

    if (*val_layout != '\0') {
        /*
         * LAYOUT must be one of the layouts supported in kbd_layouts
         */
        if (get_layouts() != 0)
            goto out;

        if ((layout_num = get_layout_number(val_layout)) == -1) {
            (void) fprintf(stderr,
                           BAD_DEFAULT_STR, KBD_PROP_LAYOUT, val_layout);
            goto out;
        }

        (void) set_layout(kbd, layout_num);
    }

    if (scf_pg_get_property(pg, KBD_PROP_FREQ, prop) != 0 ||
            scf_property_get_value(prop, val) != 0 ||
            scf_value_get_integer(val, &val_kbd_beeper) == -1) {
        (void) fprintf(stderr, "Can not get FREQ\n");
    }

    if (val_kbd_beeper >= 0 && val_kbd_beeper <= INT16_MAX)
        (void) set_beep_freq(kbd, "keyboard", val_kbd_beeper);
    else
        (void) fprintf(stderr,
                       BAD_DEFAULT_LLINT, KBD_PROP_FREQ, val_kbd_beeper);

    if (scf_pg_get_property(pg, KBD_PROP_CONSFREQ, prop) != 0 ||
            scf_property_get_value(prop, val) != 0 ||
            scf_value_get_integer(val, &val_console_beeper) == -1) {
        (void) fprintf(stderr, "Can not get CONSFREQ\n");
    }

    if (val_console_beeper >= 0 && val_console_beeper <= INT16_MAX)
        (void) set_beep_freq(kbd, "console", val_console_beeper);
    else
        (void) fprintf(stderr,
                       BAD_DEFAULT_LLINT, KBD_PROP_CONSFREQ, val_console_beeper);

out:
    if (val_layout != NULL)
        free(val_layout);
    if (val_abort != NULL)
        free(val_abort);
    if (snap != NULL)
        scf_snapshot_destroy(snap);
    scf_value_destroy(val);
    scf_property_destroy(prop);
    scf_pg_destroy(pg);
    scf_instance_destroy(inst);
    scf_handle_destroy(h);
}
Exemplo n.º 17
0
/*
 * store_inetd_hash stores the string hash in inetd's configuration file hash
 * in the repository. On success, SCF_ERROR_NONE is returned. Otherwise, the
 * scf_error value is returned.
 */
scf_error_t
store_inetd_hash(const char *hash)
{
	int ret;
	scf_error_t rval = SCF_ERROR_NONE;
	scf_handle_t *h;
	scf_propertygroup_t *pg = NULL;
	scf_instance_t *inst = NULL;
	scf_transaction_t *tx = NULL;
	scf_transaction_entry_t *txent = NULL;
	scf_property_t *prop = NULL;
	scf_value_t *val = NULL;

	if ((h = scf_handle_create(SCF_VERSION)) == NULL ||
	    scf_handle_bind(h) == -1)
		goto error;

	if ((pg = scf_pg_create(h)) == NULL ||
	    (inst = scf_instance_create(h)) == NULL ||
	    scf_handle_decode_fmri(h, INETD_INSTANCE_FMRI, NULL, NULL, inst,
	    NULL, NULL, SCF_DECODE_FMRI_EXACT) == -1)
		goto error;

	if (scf_instance_get_pg(inst, HASH_PG, pg) == -1) {
		if (scf_error() != SCF_ERROR_NOT_FOUND ||
		    scf_instance_add_pg(inst, HASH_PG, SCF_GROUP_APPLICATION,
		    0, pg) == -1)
			goto error;
	}

	if ((tx = scf_transaction_create(h)) == NULL ||
	    (txent = scf_entry_create(h)) == NULL ||
	    (prop = scf_property_create(h)) == NULL ||
	    (val = scf_value_create(h)) == NULL)
		goto error;

	do {
		if (scf_transaction_start(tx, pg) == -1)
			goto error;

		if (scf_transaction_property_new(tx, txent, HASH_PROP,
		    SCF_TYPE_ASTRING) == -1 &&
		    scf_transaction_property_change_type(tx, txent, HASH_PROP,
		    SCF_TYPE_ASTRING) == -1)
			goto error;

		if (scf_value_set_astring(val, hash) == -1 ||
		    scf_entry_add_value(txent, val) == -1)
			goto error;

		if ((ret = scf_transaction_commit(tx)) == -1)
			goto error;

		if (ret == 0) {
			scf_transaction_reset(tx);
			if (scf_pg_update(pg) == -1)
				goto error;
		}
	} while (ret == 0);

	goto success;

error:
	rval = scf_error();

success:
	scf_value_destroy(val);
	scf_property_destroy(prop);
	scf_entry_destroy(txent);
	scf_transaction_destroy(tx);
	scf_instance_destroy(inst);
	scf_pg_destroy(pg);
	scf_handle_destroy(h);
	return (rval);
}
Exemplo n.º 18
0
void
read_scf_proto_cfg(const char *proto, scf_cfg_t *cfg)
{
	scf_handle_t *handle = NULL;
	scf_scope_t *sc = NULL;
	scf_service_t *svc = NULL;
	scf_propertygroup_t *pg = NULL;
	scf_property_t *prop = NULL;
	scf_value_t *value = NULL;
	scf_iter_t *value_iter = NULL;
	uint64_t val;
	char *str;
	size_t slen;
	int i;

	handle = scf_handle_create(SCF_VERSION);
	sc = scf_scope_create(handle);
	svc = scf_service_create(handle);
	pg = scf_pg_create(handle);
	prop = scf_property_create(handle);
	value = scf_value_create(handle);
	value_iter = scf_iter_create(handle);

	if (handle == NULL || sc == NULL || svc == NULL || pg == NULL ||
	    prop == NULL || value == NULL || value_iter == NULL) {
		DMSG(D_OP, "%s: unable to create smf(5) handles.", proto);
		goto done;
	}

	if (scf_handle_bind(handle) != 0) {
		DMSG(D_OP, "%s: unable to bind smf(5) handle: %s", proto,
		    scf_strerror(scf_error()));
		goto done;
	}

	if (scf_handle_decode_fmri(handle, fmri, sc, svc, NULL, NULL, NULL,
	    0) != 0) {
		DMSG(D_OP, "%s: unable to decode fmri '%s': %s", fmri,
		    scf_strerror(scf_error()));
		goto done;
	}

	if (scf_service_get_pg(svc, proto, pg) != 0 &&
	    scf_error() != SCF_ERROR_NOT_FOUND) {
		DMSG(D_OP, "%s: unable to read '%s' property group: %s",
		    proto, proto, scf_strerror(scf_error()));
		goto done;
	}

	for (i = 0; cfg[i].name != NULL; i++) {
		scf_cfg_t *c = &cfg[i];

		if (scf_pg_get_property(pg, c->name, prop) != 0) {
			if (scf_error() != SCF_ERROR_NOT_FOUND)
				DMSG(D_OP, "%s: unable to read %s/%s from "
				    "smf: %s", proto, proto, c->name,
				    scf_strerror(scf_error()));

			continue;
		}

		if (scf_property_is_type(prop, c->type) != 0) {
			scf_type_t type;

			if (scf_error() != SCF_ERROR_TYPE_MISMATCH) {
				DMSG(D_OP, "%s: unable to validate "
				    "type of '%s/%s' smf property: %s",
				    proto, proto, c->name,
				    scf_strerror(scf_error()));
				continue;
			}

			if (scf_property_type(prop, &type) != 0) {
				DMSG(D_OP, "%s: unable to obtain "
				    "type of '%s/%s' smf property: %s",
				    proto, proto, c->name,
				    scf_strerror(scf_error()));
				continue;
			}

			DMSG(D_OP, "%s: property '%s/%s' has an unexpected "
			    "type:\n"
			    "   expected type: %s\n"
			    "     actual type: %s\n",
			    proto, proto, c->name,
			    scf_type_to_string(c->type),
			    scf_type_to_string(type));
			continue;
		}

		if (scf_property_get_value(prop, value) != 0) {
			if (scf_error() != SCF_ERROR_NOT_SET)
				DMSG(D_OP, "%s: unable to get value of "
				    "'%s/%s' smf property: %s", proto,
				    proto, c->name, scf_strerror(scf_error()));

			continue;
		}

		switch (c->type) {
		case SCF_TYPE_COUNT:
			if (scf_value_get_count(value, &val) != 0) {
				DMSG(D_OP, "%s: unable to read value of "
				    "'%s/%s' smf property: %s", proto, proto,
				    c->name, scf_strerror(scf_error()));
				continue;
			}

			if (val > c->max) {
				DMSG(D_OP, "%s: value of '%s/%s' smf property "
				    "(%'llu) is out of range (0 - %'zu).",
				    proto, proto, c->name, val, c->max);
				continue;
			}
			*((uint32_t *)c->val) = (uint32_t)val;
			break;
		case SCF_TYPE_ASTRING:
		{
			char **valp = (char **)c->val;
			ssize_t len;

			slen = c->max + 1;
			if ((str = malloc(slen)) == NULL) {
				/* XXX message */
				continue;
			}
			if ((len = scf_value_get_astring(value, str,
			    slen)) >= slen)
				DMSG(D_OP, "%s: length of '%s/%s' "
				    "(%'zd bytes) exceeds maximum "
				    "allowable length (%zu bytes).  The string"
				    " will be truncated.", proto, proto,
				    c->name, len, c->max);

			free(*valp);
			*valp = str;
			break;
		}
		default:
			VERIFY(0);
		}
	}

done:
	scf_iter_destroy(value_iter);
	scf_value_destroy(value);
	scf_property_destroy(prop);
	scf_pg_destroy(pg);
	scf_service_destroy(svc);
	scf_scope_destroy(sc);
	scf_handle_destroy(handle);
}