Ejemplo n.º 1
0
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);
}
Ejemplo n.º 2
0
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);
}