#include <locale.h>
#include <libintl.h>
#include <libscf.h>
#include <libscf_priv.h>
#include <libuutil.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <sys/stat.h>
extern scf_handle_t *h;
extern ssize_t max_scf_fmri_sz;
extern void do_scfdie(int) __NORETURN;
extern int inst_get_state(scf_instance_t *, char *, const char *,
scf_propertygroup_t **);
extern ssize_t get_astring_prop(const scf_propertygroup_t *, const char *,
scf_property_t *, scf_value_t *, char *, size_t);
extern int get_bool_prop(scf_propertygroup_t *, const char *, uint8_t *);
#define scfdie() do_scfdie(__LINE__)
int has_potential(scf_instance_t *, int);
int
is_enabled(scf_instance_t *inst)
{
scf_propertygroup_t *pg;
uint8_t bp;
if ((pg = scf_pg_create(h)) == NULL)
scfdie();
if (scf_instance_get_pg(inst, SCF_PG_GENERAL_OVR, pg) == 0 &&
get_bool_prop(pg, SCF_PROPERTY_ENABLED, &bp) == 0) {
scf_pg_destroy(pg);
return (bp);
}
if (scf_instance_get_pg(inst, SCF_PG_GENERAL, pg) == 0 &&
get_bool_prop(pg, SCF_PROPERTY_ENABLED, &bp) == 0) {
scf_pg_destroy(pg);
return (bp);
}
scf_pg_destroy(pg);
return (B_FALSE);
}
static char *
read_astring_prop(scf_propertygroup_t *pg, scf_value_t *val,
scf_property_t *prop, const char *name)
{
char *value;
size_t value_sz;
if (scf_pg_get_property(pg, name, prop) == -1) {
switch (scf_error()) {
case SCF_ERROR_NOT_FOUND:
case SCF_ERROR_DELETED:
return (NULL);
default:
scfdie();
}
}
if (scf_property_get_value(prop, val) != 0) {
switch (scf_error()) {
case SCF_ERROR_DELETED:
case SCF_ERROR_NOT_FOUND:
case SCF_ERROR_CONSTRAINT_VIOLATED:
return (NULL);
default:
scfdie();
}
}
value_sz = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
if ((value = malloc(value_sz)) == NULL)
scfdie();
if (scf_value_get_astring(val, value, value_sz) <= 0) {
free(value);
return (NULL);
}
return (value);
}
static scf_iter_t *
prop_walk_init(scf_propertygroup_t *pg, const char *name)
{
scf_iter_t *iter;
scf_property_t *prop;
if ((iter = scf_iter_create(h)) == NULL ||
(prop = scf_property_create(h)) == NULL)
scfdie();
if (scf_pg_get_property(pg, name, prop) != 0) {
switch (scf_error()) {
case SCF_ERROR_NOT_FOUND:
case SCF_ERROR_DELETED:
goto error;
default:
scfdie();
}
}
if (scf_iter_property_values(iter, prop) != 0) {
if (scf_error() != SCF_ERROR_DELETED)
scfdie();
goto error;
}
scf_property_destroy(prop);
return (iter);
error:
scf_property_destroy(prop);
scf_iter_destroy(iter);
return (NULL);
}
static int
prop_walk_step(scf_iter_t *iter, char *fmri, size_t len)
{
int r;
scf_value_t *val;
if ((val = scf_value_create(h)) == NULL)
scfdie();
r = scf_iter_next_value(iter, val);
if (r == 0)
goto out;
if (r == -1) {
if (scf_error() != SCF_ERROR_DELETED)
scfdie();
goto out;
}
if (scf_value_get_astring(val, fmri, len) <= 0) {
r = -1;
goto out;
}
out:
scf_value_destroy(val);
return (r);
}
static int
file_has_potential(char *fmri, int exclude)
{
const char *path;
struct stat st;
int good = exclude ? B_FALSE : B_TRUE;
if (scf_parse_file_fmri(fmri, NULL, &path) != 0)
return (good);
if (stat(path, &st) == 0)
return (good);
if (errno == EACCES) {
uu_warn(gettext("Unable to access \"%s\".\n"), path);
return (B_FALSE);
}
return (!good);
}
static int
inst_has_potential(scf_instance_t *inst, int enabled, int optional, int exclude)
{
char state[MAX_SCF_STATE_STRING_SZ];
if (!enabled)
return ((optional || exclude) ? 2 : 0);
if (inst_get_state(inst, state, NULL, NULL) != 0)
return (0);
if (optional && strcmp(state, SCF_STATE_STRING_OFFLINE) == 0)
return (2);
if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
return ((optional || exclude) ? 2 : 0);
}
if (exclude)
return (0);
if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0 ||
strcmp(state, SCF_STATE_STRING_DEGRADED) == 0)
return (1);
return (has_potential(inst, B_FALSE));
}
static int
fmri_has_potential(char *fmri, int isfile, int optional, int exclude,
int restarter)
{
scf_instance_t *inst;
scf_service_t *svc;
scf_iter_t *iter;
int good = exclude ? B_FALSE : B_TRUE;
int enabled;
int r, result;
int optbad;
assert(!optional || !exclude);
if (isfile)
return (file_has_potential(fmri, exclude));
if ((inst = scf_instance_create(h)) == NULL ||
(svc = scf_service_create(h)) == NULL ||
(iter = scf_iter_create(h)) == NULL)
scfdie();
if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL, NULL,
SCF_DECODE_FMRI_EXACT) == 0) {
enabled = is_enabled(inst);
result =
(inst_has_potential(inst, enabled, optional, exclude) != 0);
goto out;
}
if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL,
SCF_DECODE_FMRI_EXACT) != 0) {
result = restarter ? B_FALSE : good;
goto out;
}
if (scf_iter_service_instances(iter, svc) != 0) {
if (scf_error() != SCF_ERROR_DELETED)
scfdie();
result = good;
goto out;
}
optbad = 0;
for (;;) {
r = scf_iter_next_instance(iter, inst);
if (r == 0) {
result = exclude || (optional && !optbad);
goto out;
}
if (r == -1) {
if (scf_error() != SCF_ERROR_DELETED)
scfdie();
result = good;
goto out;
}
enabled = is_enabled(inst);
r = inst_has_potential(inst, enabled, optional, exclude);
if (exclude)
r = (r == 0);
if (r == 1) {
result = good;
goto out;
}
if (optional && r == 0)
optbad = 1;
}
out:
scf_instance_destroy(inst);
scf_service_destroy(svc);
scf_iter_destroy(iter);
return (result);
}
static int
eval_require_any(scf_iter_t *iter, char *value, size_t value_sz, int isfile)
{
int r, empty = B_TRUE;
for (;;) {
if ((r = prop_walk_step(iter, value, value_sz)) <= 0)
return ((r == 0 && empty) ? B_TRUE : r);
if (fmri_has_potential(value, isfile, B_FALSE, B_FALSE,
B_FALSE))
return (1);
empty = B_FALSE;
}
}
static int
eval_all(scf_iter_t *iter, char *value, size_t value_sz,
int isfile, int optional, int exclude)
{
int r;
for (;;) {
if ((r = prop_walk_step(iter, value, value_sz)) <= 0)
return ((r == 0) ? 1 : r);
if (!fmri_has_potential(value, isfile, optional, exclude,
B_FALSE))
return (0);
}
}
static int
eval_require_all(scf_iter_t *iter, char *value, size_t value_sz, int isfile)
{
return (eval_all(iter, value, value_sz, isfile, B_FALSE, B_FALSE));
}
static int
eval_optional_all(scf_iter_t *iter, char *value, size_t value_sz, int isfile)
{
return (eval_all(iter, value, value_sz, isfile, B_TRUE, B_FALSE));
}
static int
eval_exclude_all(scf_iter_t *iter, char *value, size_t value_sz, int isfile)
{
return (eval_all(iter, value, value_sz, isfile, B_FALSE, B_TRUE));
}
int
has_potential(scf_instance_t *inst, int restarter_only)
{
scf_snapshot_t *snap;
scf_iter_t *iter, *viter = NULL;
scf_propertygroup_t *pg;
scf_property_t *prop;
scf_value_t *val;
char *type = NULL, *grouping = NULL;
char *value;
size_t value_sz;
int result = B_TRUE, r;
int isfile;
value_sz = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
if ((iter = scf_iter_create(h)) == NULL ||
(snap = scf_snapshot_create(h)) == NULL ||
(pg = scf_pg_create(h)) == NULL ||
(val = scf_value_create(h)) == NULL ||
(prop = scf_property_create(h)) == NULL ||
(value = malloc(value_sz)) == NULL)
scfdie();
if (scf_instance_get_pg_composed(inst, NULL, SCF_PG_GENERAL, pg) != 0)
scfdie();
r = get_astring_prop(pg, SCF_PROPERTY_RESTARTER, prop, val, value,
value_sz);
if (r == -ENOENT) {
(void) strlcpy(value, SCF_SERVICE_STARTD, value_sz);
} else if (r < 0 || r > max_scf_fmri_sz) {
result = B_FALSE;
goto out;
}
if (!fmri_has_potential(value, B_FALSE, B_FALSE, B_FALSE, B_TRUE)) {
result = B_FALSE;
goto out;
}
if (restarter_only)
goto out;
if (scf_instance_get_snapshot(inst, "running", snap) != 0) {
if (scf_error() != SCF_ERROR_NOT_FOUND)
scfdie();
scf_snapshot_destroy(snap);
snap = NULL;
}
if (scf_iter_instance_pgs_typed_composed(iter, inst, snap,
SCF_GROUP_DEPENDENCY) != 0) {
if (scf_error() != SCF_ERROR_DELETED)
scfdie();
goto out;
}
for (;;) {
r = scf_iter_next_pg(iter, pg);
if (r == 0)
break;
if (r == -1) {
if (scf_error() != SCF_ERROR_DELETED)
scfdie();
goto out;
}
if ((grouping = read_astring_prop(pg, val, prop,
SCF_PROPERTY_GROUPING)) == NULL)
goto out;
if ((type = read_astring_prop(pg, val, prop,
SCF_PROPERTY_TYPE)) == NULL)
goto out;
if (strcmp(type, "path") == 0) {
isfile = B_TRUE;
} else if (strcmp(type, "service") == 0) {
isfile = B_FALSE;
} else {
free(type);
goto out;
}
free(type);
if ((viter = prop_walk_init(pg, SCF_PROPERTY_ENTITIES)) == NULL)
goto out;
if (strcmp(grouping, SCF_DEP_REQUIRE_ALL) == 0) {
r = eval_require_all(viter, value, value_sz, isfile);
} else if (strcmp(grouping, SCF_DEP_REQUIRE_ANY) == 0) {
r = eval_require_any(viter, value, value_sz, isfile);
} else if (strcmp(grouping, SCF_DEP_EXCLUDE_ALL) == 0) {
r = eval_exclude_all(viter, value, value_sz, isfile);
} else if (strcmp(grouping, SCF_DEP_OPTIONAL_ALL) == 0) {
r = eval_optional_all(viter, value, value_sz, isfile);
} else {
scf_iter_destroy(viter);
free(grouping);
grouping = NULL;
goto out;
}
scf_iter_destroy(viter);
free(grouping);
grouping = NULL;
if (r == 0) {
result = B_FALSE;
goto out;
} else if (r == -1) {
goto out;
}
}
out:
free(value);
scf_property_destroy(prop);
scf_value_destroy(val);
scf_pg_destroy(pg);
if (snap != NULL)
scf_snapshot_destroy(snap);
if (grouping != NULL)
free(grouping);
scf_iter_destroy(iter);
return (result);
}