static void _nss_mdns_get_svcstatetimestamp(struct timeval *ptv) { scf_handle_t *h; scf_simple_prop_t *sprop; int32_t nsec; (void) memset(ptv, 0, sizeof (struct timeval)); h = scf_handle_create(SCF_VERSION); if (h == NULL) return; if (scf_handle_bind(h) == -1) { scf_handle_destroy(h); return; } if ((sprop = scf_simple_prop_get(h, SMF_MDNS_FMRI, SCF_PG_RESTARTER, SCF_PROPERTY_STATE_TIMESTAMP)) != NULL) { ptv->tv_sec = *(time_t *)(scf_simple_prop_next_time(sprop, &nsec)); ptv->tv_usec = nsec / 1000; scf_simple_prop_free(sprop); } if (h != NULL) scf_handle_destroy(h); }
/* * 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); }
/* * Get FMRI for the named process. */ isc_result_t ns_smf_get_instance(char **ins_name, int debug, isc_mem_t *mctx) { scf_handle_t *h = NULL; int namelen; char *instance; REQUIRE(ins_name != NULL && *ins_name == NULL); if ((h = scf_handle_create(SCF_VERSION)) == NULL) { if (debug) UNEXPECTED_ERROR(__FILE__, __LINE__, "scf_handle_create() failed: %s", scf_strerror(scf_error())); return (ISC_R_FAILURE); } if (scf_handle_bind(h) == -1) { if (debug) UNEXPECTED_ERROR(__FILE__, __LINE__, "scf_handle_bind() failed: %s", scf_strerror(scf_error())); scf_handle_destroy(h); return (ISC_R_FAILURE); } if ((namelen = scf_myname(h, NULL, 0)) == -1) { if (debug) UNEXPECTED_ERROR(__FILE__, __LINE__, "scf_myname() failed: %s", scf_strerror(scf_error())); scf_handle_destroy(h); return (ISC_R_FAILURE); } if ((instance = isc_mem_allocate(mctx, namelen + 1)) == NULL) { UNEXPECTED_ERROR(__FILE__, __LINE__, "ns_smf_get_instance memory " "allocation failed: %s", isc_result_totext(ISC_R_NOMEMORY)); scf_handle_destroy(h); return (ISC_R_FAILURE); } if (scf_myname(h, instance, namelen + 1) == -1) { if (debug) UNEXPECTED_ERROR(__FILE__, __LINE__, "scf_myname() failed: %s", scf_strerror(scf_error())); scf_handle_destroy(h); isc_mem_free(mctx, instance); return (ISC_R_FAILURE); } scf_handle_destroy(h); *ins_name = instance; return (ISC_R_SUCCESS); }
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); }
static scf_handle_t * handle_create(void) { scf_handle_t *h = scf_handle_create(SCF_VERSION); if (h == NULL) return (NULL); if (scf_handle_bind(h) == -1) { scf_handle_destroy(h); return (NULL); } return (h); }
/* In Solaris 11 the audit daemon has been moved to SMF. In the process they simply dropped getacna() from the API, since it read from a now non-existent config file. This function re-implements getacna() to read from the SMF repository instead. */ int getacna(char *auditstring, int len) { scf_handle_t *handle = NULL; scf_property_t *property = NULL; scf_value_t *value = NULL; int ret = 0; handle = scf_handle_create(SCF_VERSION); if (handle == NULL) return -2; /* The man page for getacna on Solaris 10 states we should return -2 in case of error and set errno to indicate the error. We don't bother with errno here, though, since the only use of this function below doesn't check for errors anyway. */ ret = scf_handle_bind(handle); if (ret == -1) return -2; property = scf_property_create(handle); if (property == NULL) return -2; ret = scf_handle_decode_fmri(handle, "svc:/system/auditd:default/:properties/preselection/naflags", NULL, NULL, NULL, NULL, property, 0); if (ret == -1) return -2; value = scf_value_create(handle); if (value == NULL) return -2; ret = scf_property_get_value(property, value); if (ret == -1) return -2; ret = scf_value_get_astring(value, auditstring, len); if (ret == -1) return -2; scf_value_destroy(value); scf_property_destroy(property); scf_handle_destroy(handle); return 0; }
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 main() { if (daemonize_self() == 1) return (1); max_scf_fmri_size = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH) + 1; max_scf_name_size = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) + 1; assert(max_scf_fmri_size > 0); assert(max_scf_name_size > 0); if ((h = scf_handle_create(SCF_VERSION)) == NULL) { syslog(LOG_ERR | LOG_DAEMON, "scf_handle_create failed: %s\n", scf_strerror(scf_error())); return (1); } repository_rebind(h); scratch_fmri = umem_alloc(max_scf_fmri_size, UMEM_DEFAULT); scratch_name = umem_alloc(max_scf_name_size, UMEM_DEFAULT); if (scratch_fmri == NULL || scratch_name == NULL) { syslog(LOG_ERR | LOG_DAEMON, "Out of memory"); return (1); } inst = scf_instance_create(h); snap = scf_snapshot_create(h); scratch_pg = scf_pg_create(h); scratch_prop = scf_property_create(h); scratch_v = scf_value_create(h); if (inst == NULL || snap == NULL || scratch_pg == NULL || scratch_prop == NULL || scratch_v == NULL) { syslog(LOG_ERR | LOG_DAEMON, "Initialization failed: %s\n", scf_strerror(scf_error())); return (1); } return (repository_event_wait()); }
static void _nss_mdns_loadsmfcfg(mdns_backend_ptr_t be) { scf_handle_t *h; h = scf_handle_create(SCF_VERSION); if (h == NULL) return; if (scf_handle_bind(h) == -1) { scf_handle_destroy(h); return; } load_mdns_domaincfg(h, &(be->dmnsrchlist[0]), SMF_NSSMDNSCFG_SRCHPROP, NSSMDNS_MAXSRCHDMNS); load_mdns_domaincfg(h, &(be->validdmnlist[0]), SMF_NSSMDNSCFG_DMNPROP, NSSMDNS_MAXVALIDDMNS); if (h != NULL) scf_handle_destroy(h); }
int config_init(void) { if ((rep_handle = scf_handle_create(SCF_VERSION)) == NULL) { error_msg("%s: %s", gettext("Failed to create repository handle"), scf_strerror(scf_error())); return (-1); } else if (make_handle_bound(rep_handle) == -1) { /* let config_fini clean-up */ return (-1); } if ((proto_info_pool = uu_list_pool_create("proto_info_pool", sizeof (proto_info_t), offsetof(proto_info_t, link), proto_info_compare, UU_LIST_POOL_DEBUG)) == NULL) { error_msg(gettext("Failed to create uu list pool: %s"), uu_strerror(uu_error())); return (-1); } return (0); }
static void list_defaults() { scf_handle_t *h; scf_error_t err; int i; inetd_prop_t *proptable; size_t numprops; if (((h = scf_handle_create(SCF_VERSION)) == NULL) || (scf_handle_bind(h) == -1)) scfdie(); if ((proptable = read_default_props(h, &numprops, &err)) == NULL) { uu_die(gettext("Unexpected libscf error: %s. Exiting.\n"), scf_strerror(err)); } (void) printf("NAME=VALUE\n"); for (i = 0; i < numprops; i++) { if (!proptable[i].ip_default) continue; if (proptable[i].ip_error == IVE_UNSET) { (void) uu_warn(gettext("Error: Default property %s " "missing.\n"), proptable[i].ip_name); continue; } (void) printf("%s=", proptable[i].ip_name); print_prop_val(&proptable[i]); } free_instance_props(proptable); }
/* * 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); }
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); }
/* ARGSUSED0 */ static int list_props_cb(void *data, scf_walkinfo_t *wip) { int i; const char *instname = wip->fmri; scf_simple_prop_t *prop; inetd_prop_t *proplist; const char *restart_str; boolean_t is_rpc = B_FALSE; size_t numprops; scf_handle_t *h; scf_error_t err; if (((h = scf_handle_create(SCF_VERSION)) == NULL) || (scf_handle_bind(h) == -1)) scfdie(); /* * Get the property that holds the name of this instance's * restarter, and make sure that it is inetd. */ if ((prop = scf_simple_prop_get(h, instname, SCF_PG_GENERAL, SCF_PROPERTY_RESTARTER)) == NULL) { if (scf_error() == SCF_ERROR_NOT_FOUND) uu_die(gettext("Error: Specified service instance " "\"%s\" has no restarter property. inetd is not " "the delegated restarter of this instance.\n"), instname); if (scf_error() == SCF_ERROR_INVALID_ARGUMENT) uu_die(gettext("Error: \"%s\" is not a valid service " "instance.\n"), instname); scfdie(); } if (((restart_str = scf_simple_prop_next_ustring(prop)) == NULL) || (strstr(restart_str, INETADM_INETD_STR) == NULL)) { uu_die(gettext("Error: inetd is not the delegated restarter of " "specified service instance \"%s\".\n"), instname); } scf_simple_prop_free(prop); /* * This instance is controlled by inetd, so now we display all * of its properties. First the mandatory properties, and then * the properties that have default values, substituting the * default values inherited from inetd as necessary (this is done * for us by read_instance_props()). */ if ((proplist = read_instance_props(h, instname, &numprops, &err)) == NULL) { uu_die(gettext("Unexpected libscf error: %s. Exiting.\n"), scf_strerror(err)); } scf_handle_destroy(h); (void) printf("%-9s%s\n", "SCOPE", "NAME=VALUE"); for (i = 0; i < numprops; i++) { /* Skip rpc version properties if it's not an RPC service */ if ((strcmp(PR_RPC_LW_VER_NAME, proplist[i].ip_name) == 0) || (strcmp(PR_RPC_HI_VER_NAME, proplist[i].ip_name) == 0)) if (!is_rpc) continue; /* If it's not an unset property, print it out. */ if (proplist[i].ip_error != IVE_UNSET) { if (strcmp(PR_ISRPC_NAME, proplist[i].ip_name) == 0) is_rpc = proplist[i].ip_value.iv_boolean; (void) printf("%-9s%s=", proplist[i].from_inetd ? INETADM_DEFAULT_STR : "", proplist[i].ip_name); print_prop_val(&proplist[i]); continue; } /* arg0 is non-default, but also doesn't have to be set. */ if (i == PT_ARG0_INDEX) continue; /* all other properties should have values. */ if (proplist[i].ip_default) { (void) uu_warn(gettext("Error: Property %s is missing " "and has no defined default value.\n"), proplist[i].ip_name); } else { (void) uu_warn(gettext("Error: Required property %s is " "missing.\n"), proplist[i].ip_name); } } free_instance_props(proplist); return (0); }
/* * Returns a zone ID of Solaris when the TZ value is "localtime". * First, it tries scf. If scf fails, it looks for the same file as * /usr/share/lib/zoneinfo/localtime under /usr/share/lib/zoneinfo/. */ static char * getSolarisDefaultZoneID() { char *tz = NULL; struct stat statbuf; size_t size; char *buf; int fd; /* scf specific variables */ 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; 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 && scf_handle_decode_fmri(h, TIMEZONE_FMRI, NULL, NULL, inst, NULL, NULL, SCF_DECODE_FMRI_REQUIRE_INSTANCE) == 0 && scf_instance_get_snapshot(inst, "running", snap) == 0 && scf_instance_get_pg_composed(inst, snap, TIMEZONE_PG, pg) == 0 && scf_pg_get_property(pg, LOCALTIME_PROP, prop) == 0 && scf_property_get_value(prop, val) == 0) { ssize_t len; /* Gets the length of the zone ID string */ len = scf_value_get_astring(val, NULL, 0); if (len != -1) { tz = malloc(++len); /* +1 for a null byte */ if (tz != NULL && scf_value_get_astring(val, tz, len) != -1) { cleanupScf(h, snap, inst, pg, prop, val, NULL); return tz; } } } cleanupScf(h, snap, inst, pg, prop, val, tz); if (stat(DEFAULT_ZONEINFO_FILE, &statbuf) == -1) { return NULL; } size = (size_t) statbuf.st_size; buf = malloc(size); if (buf == NULL) { return NULL; } if ((fd = open(DEFAULT_ZONEINFO_FILE, O_RDONLY)) == -1) { free((void *) buf); return NULL; } if (read(fd, buf, size) != (ssize_t) size) { (void) close(fd); free((void *) buf); return NULL; } (void) close(fd); tz = findZoneinfoFile(buf, size, ZONEINFO_DIR); free((void *) buf); return tz; }
int main(int argc, char *argv[]) { int opt; uint_t lflag, eflag, dflag, pflag, mflag, Mflag; uint8_t enable; scf_error_t serr; int exit_status = 0; (void) setlocale(LC_ALL, ""); (void) textdomain(TEXT_DOMAIN); if ((h = scf_handle_create(SCF_VERSION)) == NULL) scfdie(); if (scf_handle_bind(h) == -1) uu_die(gettext("Error: Couldn't bind to svc.configd.\n")); if (argc == 1) { list_services(); goto out; } lflag = eflag = dflag = pflag = mflag = Mflag = 0; while ((opt = getopt(argc, argv, "ledpMm?")) != -1) { switch (opt) { case 'l': lflag = 1; break; case 'e': eflag = 1; break; case 'd': dflag = 1; break; case 'p': pflag = 1; break; case 'M': Mflag = 1; break; case 'm': mflag = 1; break; case '?': if (optopt == '?') { usage(B_TRUE); goto out; } else { usage(B_FALSE); } default: usage(B_FALSE); } } /* * All options are mutually exclusive, and we must have an option * if we reached here. */ if (lflag + eflag + dflag + pflag + mflag + Mflag != 1) usage(B_FALSE); argv += optind; argc -= optind; if ((pflag == 0) && (argc == 0)) usage(B_FALSE); serr = 0; if (lflag) { serr = scf_walk_fmri(h, argc, argv, 0, list_props_cb, NULL, &exit_status, uu_warn); } else if (dflag) { enable = 0; serr = scf_walk_fmri(h, argc, argv, 0, set_svc_enable_cb, &enable, &exit_status, uu_warn); } else if (eflag) { enable = 1; serr = scf_walk_fmri(h, argc, argv, 0, set_svc_enable_cb, &enable, &exit_status, uu_warn); } else if (mflag) { arglist_t args; char **cpp = argv; uint_t fmri_args = 0; /* count number of fmri arguments */ while ((fmri_args < argc) && (strchr(*cpp, '=') == NULL)) { fmri_args++; cpp++; } /* if no x=y args or no fmri, show usage */ if ((fmri_args == argc) || (fmri_args == 0)) usage(B_FALSE); /* setup args for modify_inst_props_cb */ args.argc = argc - fmri_args; args.argv = argv + fmri_args; serr = scf_walk_fmri(h, fmri_args, argv, 0, modify_inst_props_cb, &args, &exit_status, uu_warn); } else if (Mflag) { modify_defaults(argc, argv); } else if (pflag) { /* ensure there's no trailing garbage */ if (argc != 0) usage(B_FALSE); list_defaults(); } if (serr != 0) { uu_warn(gettext("failed to iterate over instances: %s"), scf_strerror(serr)); exit(UU_EXIT_FATAL); } out: (void) scf_handle_unbind(h); scf_handle_destroy(h); return (exit_status); }
/* * Inputs: * lpg is the property group to look up * lprop is the property within that group to look up * Outputs: * res is a pointer to an scf_resources_t. This is an internal * structure that holds all the handles needed to get a specific * property from the running snapshot; on a successful return it * contains the scf_value_t that should be passed to the desired * scf_value_get_foo() function, and must be freed after use by * calling release_scf_resources(). On a failure return, any * resources that may have been assigned to res are released, so * the caller does not need to do any cleanup in the failure case. * Returns: * 0 on success * -1 on failure */ static int get_property_value(const char *lpg, const char *lprop, scf_resources_t *res) { res->sr_inst = NULL; res->sr_snap = NULL; res->sr_pg = NULL; res->sr_prop = NULL; res->sr_val = NULL; if ((res->sr_handle = scf_handle_create(SCF_VERSION)) == NULL) { syslog(LOG_ERR, "scf_handle_create() failed: %s", scf_strerror(scf_error())); return (-1); } if (scf_handle_bind(res->sr_handle) != 0) { scf_handle_destroy(res->sr_handle); syslog(LOG_ERR, "scf_handle_destroy() failed: %s", scf_strerror(scf_error())); return (-1); } if ((res->sr_inst = scf_instance_create(res->sr_handle)) == NULL) { syslog(LOG_ERR, "scf_instance_create() failed: %s", scf_strerror(scf_error())); goto failure; } if (scf_handle_decode_fmri(res->sr_handle, OUR_FMRI, NULL, NULL, res->sr_inst, NULL, NULL, SCF_DECODE_FMRI_REQUIRE_INSTANCE) != 0) { syslog(LOG_ERR, "scf_handle_decode_fmri() failed: %s", scf_strerror(scf_error())); goto failure; } if ((res->sr_snap = scf_snapshot_create(res->sr_handle)) == NULL) { syslog(LOG_ERR, "scf_snapshot_create() failed: %s", scf_strerror(scf_error())); goto failure; } if (scf_instance_get_snapshot(res->sr_inst, "running", res->sr_snap) != 0) { syslog(LOG_ERR, "scf_instance_get_snapshot() failed: %s", scf_strerror(scf_error())); goto failure; } if ((res->sr_pg = scf_pg_create(res->sr_handle)) == NULL) { syslog(LOG_ERR, "scf_pg_create() failed: %s", scf_strerror(scf_error())); goto failure; } if (scf_instance_get_pg_composed(res->sr_inst, res->sr_snap, lpg, res->sr_pg) != 0) { syslog(LOG_ERR, "scf_instance_get_pg_composed(%s) failed: %s", lpg, scf_strerror(scf_error())); goto failure; } if ((res->sr_prop = scf_property_create(res->sr_handle)) == NULL) { syslog(LOG_ERR, "scf_property_create() failed: %s", scf_strerror(scf_error())); goto failure; } if (scf_pg_get_property(res->sr_pg, lprop, res->sr_prop) != 0) { syslog(LOG_ERR, "scf_pg_get_property(%s) failed: %s", lprop, scf_strerror(scf_error())); goto failure; } if ((res->sr_val = scf_value_create(res->sr_handle)) == NULL) { syslog(LOG_ERR, "scf_value_create() failed: %s", scf_strerror(scf_error())); goto failure; } if (scf_property_get_value(res->sr_prop, res->sr_val) != 0) { syslog(LOG_ERR, "scf_property_get_value() failed: %s", scf_strerror(scf_error())); goto failure; } return (0); failure: release_scf_resources(res); return (-1); }
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; }
static int create_service(const char *instance_name, const char *kssl_entry, const char *command, const char *username, char *inaddr_any_name) { int status = FAILURE; scf_scope_t *scope; scf_service_t *svc; scf_handle_t *handle; boolean_t errflag = B_TRUE; handle = scf_handle_create(SCF_VERSION); if (handle == NULL) { 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) { 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) { 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) { KSSL_DEBUG("scf_service_create failed: %s\n", scf_strerror(scf_error())); goto out3; } KSSL_DEBUG("scf_service_create succeeded\n"); if (scf_handle_decode_fmri(handle, SERVICE_NAME, NULL, svc, NULL, NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) { KSSL_DEBUG("scf_handle_decode_fmri failed: %s\n", scf_strerror(scf_error())); if (scf_error() == SCF_ERROR_NOT_FOUND) { (void) fprintf(stderr, gettext( "service %s not found in the repository." " Exiting.\n"), SERVICE_NAME); errflag = B_FALSE; } goto out4; } status = create_instance(handle, svc, instance_name, kssl_entry, command, username, inaddr_any_name); 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 (status != SUCCESS && status != INSTANCE_OTHER_EXISTS && status != INSTANCE_ANY_EXISTS && errflag) (void) fprintf(stderr, gettext( "Unexpected fatal libscf error: %s. Exiting.\n"), scf_strerror(scf_error())); return (status); }
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); }
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); }
int main(int argc, char *argv[]) { int c; scf_walk_callback callback; int flags; int err; (void) setlocale(LC_ALL, ""); (void) textdomain(TEXT_DOMAIN); return_code = UU_EXIT_OK; (void) uu_setpname(argv[0]); prop_pool = uu_list_pool_create("properties", sizeof (svcprop_prop_node_t), offsetof(svcprop_prop_node_t, spn_list_node), NULL, 0); if (prop_pool == NULL) uu_die("%s\n", uu_strerror(uu_error())); prop_list = uu_list_create(prop_pool, NULL, 0); hndl = scf_handle_create(SCF_VERSION); if (hndl == NULL) scfdie(); while ((c = getopt(argc, argv, "Ccfp:qs:tvwz:")) != -1) { switch (c) { case 'C': if (cflag || sflag || wait) usage(); /* Not with -c, -s or -w */ Cflag++; snapshot = NULL; break; case 'c': if (Cflag || sflag || wait) usage(); /* Not with -C, -s or -w */ cflag++; snapshot = NULL; break; case 'f': types = 1; fmris = 1; break; case 'p': add_prop(optarg); break; case 'q': quiet = 1; warn = quiet_warn; die = quiet_die; break; case 's': if (Cflag || cflag || wait) usage(); /* Not with -C, -c or -w */ snapshot = optarg; sflag++; break; case 't': types = 1; break; case 'v': verbose = 1; break; case 'w': if (Cflag || cflag || sflag) usage(); /* Not with -C, -c or -s */ wait = 1; break; case 'z': { scf_value_t *zone; scf_handle_t *h = hndl; if (getzoneid() != GLOBAL_ZONEID) uu_die(gettext("svcprop -z may only be used " "from the global zone\n")); if ((zone = scf_value_create(h)) == NULL) scfdie(); if (scf_value_set_astring(zone, optarg) != SCF_SUCCESS) scfdie(); if (scf_handle_decorate(h, "zone", zone) != SCF_SUCCESS) uu_die(gettext("invalid zone '%s'\n"), optarg); scf_value_destroy(zone); break; } case '?': switch (optopt) { case 'p': usage(); default: break; } /* FALLTHROUGH */ default: usage(); } } if (optind == argc) usage(); max_scf_name_length = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH); max_scf_value_length = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH); max_scf_fmri_length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH); if (max_scf_name_length == -1 || max_scf_value_length == -1 || max_scf_fmri_length == -1) scfdie(); if (scf_handle_bind(hndl) == -1) die(gettext("Could not connect to configuration repository: " "%s.\n"), scf_strerror(scf_error())); flags = SCF_WALK_PROPERTY | SCF_WALK_SERVICE | SCF_WALK_EXPLICIT; if (wait) { if (uu_list_numnodes(prop_list) > 1) usage(); if (argc - optind > 1) usage(); callback = do_wait; } else { callback = process_fmri; flags |= SCF_WALK_MULTIPLE; } if ((err = scf_walk_fmri(hndl, argc - optind, argv + optind, flags, callback, NULL, &return_code, warn)) != 0) { warn(gettext("failed to iterate over instances: %s\n"), scf_strerror(err)); return_code = UU_EXIT_FATAL; } scf_handle_destroy(hndl); return (return_code); }