コード例 #1
0
ファイル: wipefs.c プロジェクト: CSRedRat/util-linux
int
main(int argc, char **argv)
{
	struct wipe_desc *wp0 = NULL, *wp;
	int c, all = 0, has_offset = 0, noact = 0, quiet = 0;
	int mode = WP_MODE_PRETTY;

	static const struct option longopts[] = {
	    { "all",       0, 0, 'a' },
	    { "help",      0, 0, 'h' },
	    { "no-act",    0, 0, 'n' },
	    { "offset",    1, 0, 'o' },
	    { "parsable",  0, 0, 'p' },
	    { "quiet",     0, 0, 'q' },
	    { "types",     1, 0, 't' },
	    { "version",   0, 0, 'V' },
	    { NULL,        0, 0, 0 }
	};

	static const ul_excl_t excl[] = {       /* rows and cols in in ASCII order */
		{ 'a','o' },
		{ 0 }
	};
	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	atexit(close_stdout);

	while ((c = getopt_long(argc, argv, "ahno:pqt:V", longopts, NULL)) != -1) {

		err_exclusive_options(c, longopts, excl, excl_st);

		switch(c) {
		case 'a':
			all++;
			break;
		case 'h':
			usage(stdout);
			break;
		case 'n':
			noact++;
			break;
		case 'o':
			wp0 = add_offset(wp0, strtosize_or_err(optarg,
					 _("invalid offset argument")), 1);
			has_offset++;
			break;
		case 'p':
			mode = WP_MODE_PARSABLE;
			break;
		case 'q':
			quiet++;
			break;
		case 't':
			type_pattern = optarg;
			break;
		case 'V':
			printf(_("%s from %s\n"), program_invocation_short_name,
				PACKAGE_STRING);
			return EXIT_SUCCESS;
		default:
			usage(stderr);
			break;
		}
	}

	if (optind == argc)
		usage(stderr);

	if (!all && !has_offset) {
		/*
		 * Print only
		 */
		while (optind < argc) {
			wp0 = read_offsets(NULL, argv[optind++]);
			if (wp0)
				print_all(wp0, mode);
			free_wipe(wp0);
		}
	} else {
		/*
		 * Erase
		 */
		while (optind < argc) {
			wp = clone_offset(wp0);
			wp = do_wipe(wp, argv[optind++], noact, all, quiet);
			free_wipe(wp);
		}
	}

	return EXIT_SUCCESS;
}
コード例 #2
0
int main(int argc, char **argv)
{
	int	c;
	int	fd;
	int	mode = 0;
	int	dig = 0;
	loff_t	length = -2LL;
	loff_t	offset = 0;

	static const struct option longopts[] = {
	    { "help",           0, 0, 'h' },
	    { "version",        0, 0, 'V' },
	    { "keep-size",      0, 0, 'n' },
	    { "punch-hole",     0, 0, 'p' },
	    { "collapse-range", 0, 0, 'c' },
	    { "dig-holes",      0, 0, 'd' },
	    { "insert-range",   0, 0, 'i' },
	    { "zero-range",     0, 0, 'z' },
	    { "offset",         1, 0, 'o' },
	    { "length",         1, 0, 'l' },
	    { "verbose",        0, 0, 'v' },
	    { NULL,             0, 0, 0 }
	};

	static const ul_excl_t excl[] = {	/* rows and cols in in ASCII order */
		{ 'c', 'd', 'p', 'z' },
		{ 'c', 'n' },
		{ 0 }
	};
	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	atexit(close_stdout);

	while ((c = getopt_long(argc, argv, "hvVncpdizl:o:", longopts, NULL))
			!= -1) {

		err_exclusive_options(c, longopts, excl, excl_st);

		switch(c) {
		case 'h':
			usage(stdout);
			break;
		case 'c':
			mode |= FALLOC_FL_COLLAPSE_RANGE;
			break;
		case 'd':
			dig = 1;
			break;
		case 'i':
			mode |= FALLOC_FL_INSERT_RANGE;
			break;
		case 'l':
			length = cvtnum(optarg);
			break;
		case 'n':
			mode |= FALLOC_FL_KEEP_SIZE;
			break;
		case 'o':
			offset = cvtnum(optarg);
			break;
		case 'p':
			mode |= FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
			break;
		case 'z':
			mode |= FALLOC_FL_ZERO_RANGE;
			break;
		case 'v':
			verbose++;
			break;
		case 'V':
			printf(UTIL_LINUX_VERSION);
			return EXIT_SUCCESS;
		default:
			usage(stderr);
			break;
		}
	}

	if (optind == argc)
		errx(EXIT_FAILURE, _("no filename specified"));

	filename = argv[optind++];

	if (optind != argc)
		errx(EXIT_FAILURE, _("unexpected number of arguments"));

	if (dig) {
		/* for --dig-holes the default is analyze all file */
		if (length == -2LL)
			length = 0;
		if (length < 0)
			errx(EXIT_FAILURE, _("invalid length value specified"));
	} else {
		/* it's safer to require the range specification (--length --offset) */
		if (length == -2LL)
			errx(EXIT_FAILURE, _("no length argument specified"));
		if (length <= 0)
			errx(EXIT_FAILURE, _("invalid length value specified"));
	}
	if (offset < 0)
		errx(EXIT_FAILURE, _("invalid offset value specified"));

	/* O_CREAT makes sense only for the default fallocate(2) behavior
	 * when mode is no specified and new space is allocated */
	fd = open(filename, O_RDWR | (!dig && !mode ? O_CREAT : 0),
		  S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
	if (fd < 0)
		err(EXIT_FAILURE, _("cannot open %s"), filename);

	if (dig)
		dig_holes(fd, offset, length);
	else
		xfallocate(fd, mode, offset, length);

	if (close_fd(fd) != 0)
		err(EXIT_FAILURE, _("write failed: %s"), filename);

	return EXIT_SUCCESS;
}
コード例 #3
0
ファイル: chcpu.c プロジェクト: Yowiee/util-linux
int main(int argc, char *argv[])
{
	cpu_set_t *cpu_set;
	size_t setsize;
	int cmd = -1;
	int c;

	static const struct option longopts[] = {
		{ "configure",	required_argument, 0, 'c' },
		{ "deconfigure",required_argument, 0, 'g' },
		{ "disable",	required_argument, 0, 'd' },
		{ "dispatch",	required_argument, 0, 'p' },
		{ "enable",	required_argument, 0, 'e' },
		{ "help",	no_argument,       0, 'h' },
		{ "rescan",	no_argument,       0, 'r' },
		{ "version",	no_argument,       0, 'V' },
		{ NULL,		0, 0, 0 }
	};

	static const ul_excl_t excl[] = {       /* rows and cols in in ASCII order */
		{ 'c','d','e','g','p' },
		{ 0 }
	};
	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	atexit(close_stdout);

	maxcpus = get_max_number_of_cpus();
	if (maxcpus < 1)
		errx(EXIT_FAILURE, _("cannot determine NR_CPUS; aborting"));
	if (path_exist(_PATH_SYS_CPU_ONLINE))
		onlinecpus = path_read_cpulist(maxcpus, _PATH_SYS_CPU_ONLINE);
	setsize = CPU_ALLOC_SIZE(maxcpus);
	cpu_set = CPU_ALLOC(maxcpus);
	if (!cpu_set)
		err(EXIT_FAILURE, _("cpuset_alloc failed"));

	while ((c = getopt_long(argc, argv, "c:d:e:g:hp:rV", longopts, NULL)) != -1) {

		err_exclusive_options(c, longopts, excl, excl_st);

		switch (c) {
		case 'c':
			cmd = CMD_CPU_CONFIGURE;
			cpu_parse(argv[optind - 1], cpu_set, setsize);
			break;
		case 'd':
			cmd = CMD_CPU_DISABLE;
			cpu_parse(argv[optind - 1], cpu_set, setsize);
			break;
		case 'e':
			cmd = CMD_CPU_ENABLE;
			cpu_parse(argv[optind - 1], cpu_set, setsize);
			break;
		case 'g':
			cmd = CMD_CPU_DECONFIGURE;
			cpu_parse(argv[optind - 1], cpu_set, setsize);
			break;
		case 'h':
			usage(stdout);
		case 'p':
			if (strcmp("horizontal", argv[optind - 1]) == 0)
				cmd = CMD_CPU_DISPATCH_HORIZONTAL;
			else if (strcmp("vertical", argv[optind - 1]) == 0)
				cmd = CMD_CPU_DISPATCH_VERTICAL;
			else
				errx(EXIT_FAILURE, _("unsupported argument: %s"),
				     argv[optind -1 ]);
			break;
		case 'r':
			cmd = CMD_CPU_RESCAN;
			break;
		case 'V':
			printf(_("%s from %s\n"), program_invocation_short_name,
			       PACKAGE_STRING);
			return EXIT_SUCCESS;
		default:
			usage(stderr);
		}
	}

	if ((argc == 1) || (argc != optind))
		usage(stderr);

	switch (cmd) {
	case CMD_CPU_ENABLE:
		return cpu_enable(cpu_set, maxcpus, 1);
	case CMD_CPU_DISABLE:
		return cpu_enable(cpu_set, maxcpus, 0);
	case CMD_CPU_CONFIGURE:
		return cpu_configure(cpu_set, maxcpus, 1);
	case CMD_CPU_DECONFIGURE:
		return cpu_configure(cpu_set, maxcpus, 0);
	case CMD_CPU_RESCAN:
		return cpu_rescan();
	case CMD_CPU_DISPATCH_HORIZONTAL:
		return cpu_set_dispatch(0);
	case CMD_CPU_DISPATCH_VERTICAL:
		return cpu_set_dispatch(1);
	}
	return EXIT_SUCCESS;
}
コード例 #4
0
ファイル: setpriv.c プロジェクト: hubert-he/util-linux
int main(int argc, char **argv)
{
    enum {
        NNP = CHAR_MAX + 1,
        RUID,
        EUID,
        RGID,
        EGID,
        REUID,
        REGID,
        CLEAR_GROUPS,
        KEEP_GROUPS,
        GROUPS,
        INHCAPS,
        LISTCAPS,
        CAPBSET,
        SECUREBITS,
        SELINUX_LABEL,
        APPARMOR_PROFILE
    };

    static const struct option longopts[] = {
        {"dump", no_argument, 0, 'd'},
        {"nnp", no_argument, 0, NNP},
        {"no-new-privs", no_argument, 0, NNP},
        {"inh-caps", required_argument, 0, INHCAPS},
        {"list-caps", no_argument, 0, LISTCAPS},
        {"ruid", required_argument, 0, RUID},
        {"euid", required_argument, 0, EUID},
        {"rgid", required_argument, 0, RGID},
        {"egid", required_argument, 0, EGID},
        {"reuid", required_argument, 0, REUID},
        {"regid", required_argument, 0, REGID},
        {"clear-groups", no_argument, 0, CLEAR_GROUPS},
        {"keep-groups", no_argument, 0, KEEP_GROUPS},
        {"groups", required_argument, 0, GROUPS},
        {"bounding-set", required_argument, 0, CAPBSET},
        {"securebits", required_argument, 0, SECUREBITS},
        {"selinux-label", required_argument, 0, SELINUX_LABEL},
        {"apparmor-profile", required_argument, 0, APPARMOR_PROFILE},
        {"help", no_argument, 0, 'h'},
        {"version", no_argument, 0, 'V'},
        {NULL, 0, 0, 0}
    };

    static const ul_excl_t excl[] = {
        /* keep in same order with enum definitions */
        {CLEAR_GROUPS, KEEP_GROUPS, GROUPS},
        {0}
    };
    int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;

    int c;
    struct privctx opts;
    int dumplevel = 0;
    int total_opts = 0;
    int list_caps = 0;

    setlocale(LC_ALL, "");
    bindtextdomain(PACKAGE, LOCALEDIR);
    textdomain(PACKAGE);
    atexit(close_stdout);

    memset(&opts, 0, sizeof(opts));

    while ((c = getopt_long(argc, argv, "+dhV", longopts, NULL)) != -1) {
        err_exclusive_options(c, longopts, excl, excl_st);
        total_opts++;
        switch (c) {
        case 'd':
            dumplevel++;
            break;
        case NNP:
            if (opts.nnp)
                errx(EXIT_FAILURE,
                     _("duplicate --no-new-privs option"));
            opts.nnp = 1;
            break;
        case RUID:
            if (opts.have_ruid)
                errx(EXIT_FAILURE, _("duplicate ruid"));
            opts.have_ruid = 1;
            opts.ruid = get_user(optarg, _("failed to parse ruid"));
            break;
        case EUID:
            if (opts.have_euid)
                errx(EXIT_FAILURE, _("duplicate euid"));
            opts.have_euid = 1;
            opts.euid = get_user(optarg, _("failed to parse euid"));
            break;
        case REUID:
            if (opts.have_ruid || opts.have_euid)
                errx(EXIT_FAILURE, _("duplicate ruid or euid"));
            opts.have_ruid = opts.have_euid = 1;
            opts.ruid = opts.euid = get_user(optarg, _("failed to parse reuid"));
            break;
        case RGID:
            if (opts.have_rgid)
                errx(EXIT_FAILURE, _("duplicate rgid"));
            opts.have_rgid = 1;
            opts.rgid = get_group(optarg, _("failed to parse rgid"));
            break;
        case EGID:
            if (opts.have_egid)
                errx(EXIT_FAILURE, _("duplicate egid"));
            opts.have_egid = 1;
            opts.egid = get_group(optarg, _("failed to parse egid"));
            break;
        case REGID:
            if (opts.have_rgid || opts.have_egid)
                errx(EXIT_FAILURE, _("duplicate rgid or egid"));
            opts.have_rgid = opts.have_egid = 1;
            opts.rgid = opts.egid = get_group(optarg, _("failed to parse regid"));
            break;
        case CLEAR_GROUPS:
            if (opts.clear_groups)
                errx(EXIT_FAILURE,
                     _("duplicate --clear-groups option"));
            opts.clear_groups = 1;
            break;
        case KEEP_GROUPS:
            if (opts.keep_groups)
                errx(EXIT_FAILURE,
                     _("duplicate --keep-groups option"));
            opts.keep_groups = 1;
            break;
        case GROUPS:
            if (opts.have_groups)
                errx(EXIT_FAILURE,
                     _("duplicate --groups option"));
            parse_groups(&opts, optarg);
            break;
        case LISTCAPS:
            list_caps = 1;
            break;
        case INHCAPS:
            if (opts.caps_to_inherit)
                errx(EXIT_FAILURE,
                     _("duplicate --inh-caps option"));
            opts.caps_to_inherit = optarg;
            break;
        case CAPBSET:
            if (opts.bounding_set)
                errx(EXIT_FAILURE,
                     _("duplicate --bounding-set option"));
            opts.bounding_set = optarg;
            break;
        case SECUREBITS:
            if (opts.have_securebits)
                errx(EXIT_FAILURE,
                     _("duplicate --securebits option"));
            parse_securebits(&opts, optarg);
            break;
        case SELINUX_LABEL:
            if (opts.selinux_label)
                errx(EXIT_FAILURE,
                     _("duplicate --selinux-label option"));
            opts.selinux_label = optarg;
            break;
        case APPARMOR_PROFILE:
            if (opts.apparmor_profile)
                errx(EXIT_FAILURE,
                     _("duplicate --apparmor-profile option"));
            opts.apparmor_profile = optarg;
            break;
        case 'h':
            usage(stdout);
        case 'V':
            printf(UTIL_LINUX_VERSION);
            return EXIT_SUCCESS;
        case '?':
            usage(stderr);
        default:
            errx(EXIT_FAILURE, _("unrecognized option '%c'"), c);
        }
    }

    if (dumplevel) {
        if (total_opts != dumplevel || optind < argc)
            errx(EXIT_FAILURE,
                 _("--dump is incompatible with all other options"));
        dump(dumplevel);
        return EXIT_SUCCESS;
    }

    if (list_caps) {
        if (total_opts != 1 || optind < argc)
            errx(EXIT_FAILURE,
                 _("--list-caps must be specified alone"));
        list_known_caps();
        return EXIT_SUCCESS;
    }

    if (argc <= optind)
        errx(EXIT_FAILURE, _("No program specified"));

    if ((opts.have_rgid || opts.have_egid)
            && !opts.keep_groups && !opts.clear_groups && !opts.have_groups)
        errx(EXIT_FAILURE,
             _("--[re]gid requires --keep-groups, --clear-groups, or --groups"));

    if (opts.nnp)
        if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
            err(EXIT_FAILURE, _("disallow granting new privileges failed"));

    if (opts.selinux_label)
        do_selinux_label(opts.selinux_label);
    if (opts.apparmor_profile)
        do_apparmor_profile(opts.apparmor_profile);

    if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1)
        err(EXIT_FAILURE, _("keep process capabilities failed"));

    /* We're going to want CAP_SETPCAP, CAP_SETUID, and CAP_SETGID if
     * possible.  */
    bump_cap(CAP_SETPCAP);
    bump_cap(CAP_SETUID);
    bump_cap(CAP_SETGID);
    if (capng_apply(CAPNG_SELECT_CAPS) != 0)
        err(SETPRIV_EXIT_PRIVERR, _("activate capabilities"));

    if (opts.have_ruid || opts.have_euid) {
        do_setresuid(&opts);
        /* KEEPCAPS doesn't work for the effective mask. */
        if (capng_apply(CAPNG_SELECT_CAPS) != 0)
            err(SETPRIV_EXIT_PRIVERR, _("reactivate capabilities"));
    }

    if (opts.have_rgid || opts.have_egid)
        do_setresgid(&opts);

    if (opts.have_groups) {
        if (setgroups(opts.num_groups, opts.groups) != 0)
            err(SETPRIV_EXIT_PRIVERR, _("setgroups failed"));
    } else if (opts.clear_groups) {
        gid_t x = 0;
        if (setgroups(0, &x) != 0)
            err(SETPRIV_EXIT_PRIVERR, _("setgroups failed"));
    }

    if (opts.have_securebits)
        if (prctl(PR_SET_SECUREBITS, opts.securebits, 0, 0, 0) != 0)
            err(SETPRIV_EXIT_PRIVERR, _("set process securebits failed"));

    if (opts.bounding_set) {
        do_caps(CAPNG_BOUNDING_SET, opts.bounding_set);
        errno = EPERM;	/* capng doesn't set errno if we're missing CAP_SETPCAP */
        if (capng_apply(CAPNG_SELECT_BOUNDS) != 0)
            err(SETPRIV_EXIT_PRIVERR, _("apply bounding set"));
    }

    if (opts.caps_to_inherit) {
        do_caps(CAPNG_INHERITABLE, opts.caps_to_inherit);
        if (capng_apply(CAPNG_SELECT_CAPS) != 0)
            err(SETPRIV_EXIT_PRIVERR, _("apply capabilities"));
    }

    execvp(argv[optind], argv + optind);

    err(EXIT_FAILURE, _("cannot execute: %s"), argv[optind]);
}
コード例 #5
0
ファイル: setterm.c プロジェクト: Webster-WXH/util-linux
static void parse_option(struct setterm_control *ctl, int ac, char **av)
{
	int c;
	enum {
		OPT_TERM = CHAR_MAX + 1,
		OPT_RESET,
		OPT_RESIZE,
		OPT_INITIALIZE,
		OPT_CURSOR,
		OPT_REPEAT,
		OPT_APPCURSORKEYS,
		OPT_LINEWRAP,
		OPT_DEFAULT,
		OPT_FOREGROUND,
		OPT_BACKGROUND,
		OPT_ULCOLOR,
		OPT_HBCOLOR,
		OPT_INVERSESCREEN,
		OPT_BOLD,
		OPT_HALF_BRIGHT,
		OPT_BLINK,
		OPT_REVERSE,
		OPT_UNDERLINE,
		OPT_STORE,
		OPT_CLEAR,
		OPT_TABS,
		OPT_CLRTABS,
		OPT_REGTABS,
		OPT_BLANK,
		OPT_DUMP,
		OPT_APPEND,
		OPT_FILE,
		OPT_MSG,
		OPT_MSGLEVEL,
		OPT_POWERSAVE,
		OPT_POWERDOWN,
		OPT_BLENGTH,
		OPT_BFREQ,
		OPT_VERSION,
		OPT_HELP
	};
	static const struct option longopts[] = {
		{"term", required_argument, NULL, OPT_TERM},
		{"reset", no_argument, NULL, OPT_RESET},
		{"resize", no_argument, NULL, OPT_RESIZE},
		{"initialize", no_argument, NULL, OPT_INITIALIZE},
		{"cursor", required_argument, NULL, OPT_CURSOR},
		{"repeat", required_argument, NULL, OPT_REPEAT},
		{"appcursorkeys", required_argument, NULL, OPT_APPCURSORKEYS},
		{"linewrap", required_argument, NULL, OPT_LINEWRAP},
		{"default", no_argument, NULL, OPT_DEFAULT},
		{"foreground", required_argument, NULL, OPT_FOREGROUND},
		{"background", required_argument, NULL, OPT_BACKGROUND},
		{"ulcolor", required_argument, NULL, OPT_ULCOLOR},
		{"hbcolor", required_argument, NULL, OPT_HBCOLOR},
		{"inversescreen", required_argument, NULL, OPT_INVERSESCREEN},
		{"bold", required_argument, NULL, OPT_BOLD},
		{"half-bright", required_argument, NULL, OPT_HALF_BRIGHT},
		{"blink", required_argument, NULL, OPT_BLINK},
		{"reverse", required_argument, NULL, OPT_REVERSE},
		{"underline", required_argument, NULL, OPT_UNDERLINE},
		{"store", no_argument, NULL, OPT_STORE},
		{"clear", required_argument, NULL, OPT_CLEAR},
		{"tabs", optional_argument, NULL, OPT_TABS},
		{"clrtabs", optional_argument, NULL, OPT_CLRTABS},
		{"regtabs", optional_argument, NULL, OPT_REGTABS},
		{"blank", optional_argument, NULL, OPT_BLANK},
		{"dump", optional_argument, NULL, OPT_DUMP},
		{"append", required_argument, NULL, OPT_APPEND},
		{"file", required_argument, NULL, OPT_FILE},
		{"msg", required_argument, NULL, OPT_MSG},
		{"msglevel", required_argument, NULL, OPT_MSGLEVEL},
		{"powersave", required_argument, NULL, OPT_POWERSAVE},
		{"powerdown", optional_argument, NULL, OPT_POWERDOWN},
		{"blength", optional_argument, NULL, OPT_BLENGTH},
		{"bfreq", optional_argument, NULL, OPT_BFREQ},
		{"version", no_argument, NULL, OPT_VERSION},
		{"help", no_argument, NULL, OPT_HELP},
		{NULL, 0, NULL, 0}
	};
	static const ul_excl_t excl[] = {
		{ OPT_DEFAULT, OPT_STORE },
		{ OPT_TABS, OPT_CLRTABS, OPT_REGTABS },
		{ OPT_MSG, OPT_MSGLEVEL },
		{ 0 }
	};
	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;

	while ((c = getopt_long_only(ac, av, "", longopts, NULL)) != -1) {
		err_exclusive_options(c, longopts, excl, excl_st);
		switch (c) {
		case OPT_TERM:
			ctl->opt_term = set_opt_flag(ctl->opt_term);
			ctl->opt_te_terminal_name = optarg;
			break;
		case OPT_RESET:
			ctl->opt_reset = set_opt_flag(ctl->opt_reset);
			break;
		case OPT_RESIZE:
			ctl->opt_resize = set_opt_flag(ctl->opt_resize);
			break;
		case OPT_INITIALIZE:
			ctl->opt_initialize = set_opt_flag(ctl->opt_initialize);
			break;
		case OPT_CURSOR:
			ctl->opt_cursor = set_opt_flag(ctl->opt_cursor);
			ctl->opt_cu_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_REPEAT:
			ctl->opt_repeat = set_opt_flag(ctl->opt_repeat);
			ctl->opt_rep_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_APPCURSORKEYS:
			ctl->opt_appcursorkeys = set_opt_flag(ctl->opt_appcursorkeys);
			ctl->opt_appck_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_LINEWRAP:
			ctl->opt_linewrap = set_opt_flag(ctl->opt_linewrap);
			ctl->opt_li_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_DEFAULT:
			ctl->opt_default = set_opt_flag(ctl->opt_default);
			break;
		case OPT_FOREGROUND:
			ctl->opt_foreground = set_opt_flag(ctl->opt_foreground);
			ctl->opt_fo_color = parse_febg_color(optarg);
			break;
		case OPT_BACKGROUND:
			ctl->opt_background = set_opt_flag(ctl->opt_background);
			ctl->opt_ba_color = parse_febg_color(optarg);
			break;
		case OPT_ULCOLOR:
			ctl->opt_ulcolor = set_opt_flag(ctl->opt_ulcolor);
			ctl->opt_ul_color = parse_ulhb_color(av, &optind);
			break;
		case OPT_HBCOLOR:
			ctl->opt_hbcolor = set_opt_flag(ctl->opt_hbcolor);
			ctl->opt_hb_color = parse_ulhb_color(av, &optind);
			break;
		case OPT_INVERSESCREEN:
			ctl->opt_inversescreen = set_opt_flag(ctl->opt_inversescreen);
			ctl->opt_invsc_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_BOLD:
			ctl->opt_bold = set_opt_flag(ctl->opt_bold);
			ctl->opt_bo_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_HALF_BRIGHT:
			ctl->opt_halfbright = set_opt_flag(ctl->opt_halfbright);
			ctl->opt_hb_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_BLINK:
			ctl->opt_blink = set_opt_flag(ctl->opt_blink);
			ctl->opt_bl_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_REVERSE:
			ctl->opt_reverse = set_opt_flag(ctl->opt_reverse);
			ctl->opt_re_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_UNDERLINE:
			ctl->opt_underline = set_opt_flag(ctl->opt_underline);
			ctl->opt_un_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_STORE:
			ctl->opt_store = set_opt_flag(ctl->opt_store);
			break;
		case OPT_CLEAR:
			ctl->opt_clear = set_opt_flag(ctl->opt_clear);
			ctl->opt_cl_all = parse_switch(optarg, _("argument error"),
						"all", "reset", NULL);
			break;
		case OPT_TABS:
			ctl->opt_tabs = set_opt_flag(ctl->opt_tabs);
			parse_tabs(av, optarg, &optind, ctl->opt_tb_array);
			break;
		case OPT_CLRTABS:
			ctl->opt_clrtabs = set_opt_flag(ctl->opt_clrtabs);
			parse_tabs(av, optarg, &optind, ctl->opt_tb_array);
			break;
		case OPT_REGTABS:
			ctl->opt_regtabs = set_opt_flag(ctl->opt_regtabs);
			ctl->opt_rt_len = parse_regtabs(av, optarg, &optind);
			break;
		case OPT_BLANK:
			ctl->opt_blank = set_opt_flag(ctl->opt_blank);
			ctl->opt_bl_min = parse_blank(av, optarg, &optind);
			break;
		case OPT_DUMP:
			ctl->opt_snap = set_opt_flag(ctl->opt_snap);
			ctl->opt_sn_num = parse_snap(av, optarg, &optind);
			break;
		case OPT_APPEND:
			ctl->opt_append = set_opt_flag(ctl->opt_append);
			ctl->opt_sn_num = parse_snap(av, optarg, &optind);
			break;
		case OPT_FILE:
			ctl->opt_snapfile = set_opt_flag(ctl->opt_snapfile);
			ctl->opt_sn_name = optarg;
			break;
		case OPT_MSG:
			ctl->opt_msg = set_opt_flag(ctl->opt_msg);
			ctl->opt_msg_on = parse_switch(optarg, _("argument error"),
						"on", "off", NULL);
			break;
		case OPT_MSGLEVEL:
			ctl->opt_msglevel = set_opt_flag(ctl->opt_msglevel);
			ctl->opt_msglevel_num = parse_msglevel(optarg);
			if (ctl->opt_msglevel_num == 0) {
				ctl->opt_msg = set_opt_flag(ctl->opt_msg);
				ctl->opt_msg_on |= 1;
			}
			break;
		case OPT_POWERSAVE:
			ctl->opt_powersave = set_opt_flag(ctl->opt_powersave);
			ctl->opt_ps_mode = parse_powersave(optarg);
			break;
		case OPT_POWERDOWN:
			ctl->opt_powerdown = set_opt_flag(ctl->opt_powerdown);
			ctl->opt_pd_min = parse_blank(av, optarg, &optind);
			break;
		case OPT_BLENGTH:
			ctl->opt_blength = set_opt_flag(ctl->opt_blength);
			ctl->opt_blength_l = parse_blength(av, optarg, &optind);
			break;
		case OPT_BFREQ:
			ctl->opt_bfreq = set_opt_flag(ctl->opt_bfreq);
			ctl->opt_bfreq_f = parse_bfreq(av, optarg, &optind);
			break;
		case OPT_VERSION:
			printf(UTIL_LINUX_VERSION);
			exit(EXIT_SUCCESS);
		case OPT_HELP:
			usage();
		default:
			errtryhelp(EXIT_FAILURE);
		}
	}
}
コード例 #6
0
ファイル: blkid.c プロジェクト: Romutk/lab3.2n
int main(int argc, char **argv)
{
	blkid_cache cache = NULL;
	char **devices = NULL;
	char *show[128] = { NULL, };
	char *search_type = NULL, *search_value = NULL;
	char *read = NULL;
	int fltr_usage = 0;
	char **fltr_type = NULL;
	int fltr_flag = BLKID_FLTR_ONLYIN;
	unsigned int numdev = 0, numtag = 0;
	int version = 0;
	int err = BLKID_EXIT_OTHER;
	unsigned int i;
	int output_format = 0;
	int lookup = 0, gc = 0, lowprobe = 0, eval = 0;
	int c;
	uintmax_t offset = 0, size = 0;

	static const ul_excl_t excl[] = {       /* rows and cols in in ASCII order */
		{ 'n','u' },
		{ 0 }
	};
	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;

	show[0] = NULL;
	atexit(close_stdout);

	while ((c = getopt (argc, argv,
			    "c:df:ghilL:n:ko:O:ps:S:t:u:U:w:Vv")) != EOF) {

		err_exclusive_options(c, NULL, excl, excl_st);

		switch (c) {
		case 'c':
			if (optarg && !*optarg)
				read = NULL;
			else
				read = optarg;
			break;
		case 'd':
			raw_chars = 1;
			break;
		case 'L':
			eval++;
			search_value = xstrdup(optarg);
			search_type = xstrdup("LABEL");
			break;
		case 'n':
			fltr_type = list_to_types(optarg, &fltr_flag);
			break;
		case 'u':
			fltr_usage = list_to_usage(optarg, &fltr_flag);
			break;
		case 'U':
			eval++;
			search_value = xstrdup(optarg);
			search_type = xstrdup("UUID");
			break;
		case 'i':
			lowprobe |= LOWPROBE_TOPOLOGY;
			break;
		case 'l':
			lookup++;
			break;
		case 'g':
			gc = 1;
			break;
		case 'k':
		{
			size_t idx = 0;
			const char *name = NULL;

			while (blkid_superblocks_get_name(idx++, &name, NULL) == 0)
				printf("%s\n", name);
			exit(EXIT_SUCCESS);
		}
		case 'o':
			if (!strcmp(optarg, "value"))
				output_format = OUTPUT_VALUE_ONLY;
			else if (!strcmp(optarg, "device"))
				output_format = OUTPUT_DEVICE_ONLY;
			else if (!strcmp(optarg, "list"))
				output_format = OUTPUT_PRETTY_LIST;	/* deprecated */
			else if (!strcmp(optarg, "udev"))
				output_format = OUTPUT_UDEV_LIST;
			else if (!strcmp(optarg, "export"))
				output_format = OUTPUT_EXPORT_LIST;
			else if (!strcmp(optarg, "full"))
				output_format = 0;
			else {
				fprintf(stderr, "Invalid output format %s. "
					"Choose from value,\n\t"
					"device, list, udev or full\n", optarg);
				exit(BLKID_EXIT_OTHER);
			}
			break;
		case 'O':
			offset = strtosize_or_err(optarg, "invalid offset argument");
			break;
		case 'p':
			lowprobe |= LOWPROBE_SUPERBLOCKS;
			break;
		case 's':
			if (numtag + 1 >= sizeof(show) / sizeof(*show)) {
				fprintf(stderr, "Too many tags specified\n");
				usage(err);
			}
			show[numtag++] = optarg;
			show[numtag] = NULL;
			break;
		case 'S':
			size = strtosize_or_err(optarg, "invalid size argument");
			break;
		case 't':
			if (search_type) {
				fprintf(stderr, "Can only search for "
						"one NAME=value pair\n");
				usage(err);
			}
			if (blkid_parse_tag_string(optarg,
						   &search_type,
						   &search_value)) {
				fprintf(stderr, "-t needs NAME=value pair\n");
				usage(err);
			}
			break;
		case 'V':
		case 'v':
			version = 1;
			break;
		case 'w':
			/* ignore - backward compatibility */
			break;
		case 'h':
			err = 0;
			/* fallthrough */
		default:
			usage(err);
		}
	}


	/* The rest of the args are device names */
	if (optind < argc) {
		devices = xcalloc(argc - optind, sizeof(char *));
		while (optind < argc)
			devices[numdev++] = argv[optind++];
	}

	if (version) {
		print_version(stdout);
		goto exit;
	}

	/* convert LABEL/UUID lookup to evaluate request */
	if (lookup && output_format == OUTPUT_DEVICE_ONLY && search_type &&
	    (!strcmp(search_type, "LABEL") || !strcmp(search_type, "UUID"))) {
		eval++;
		lookup = 0;
	}

	if (!lowprobe && !eval && blkid_get_cache(&cache, read) < 0)
		goto exit;

	if (gc) {
		blkid_gc_cache(cache);
		err = 0;
		goto exit;
	}
	err = BLKID_EXIT_NOTFOUND;

	if (eval == 0 && (output_format & OUTPUT_PRETTY_LIST)) {
		if (lowprobe) {
			fprintf(stderr, "The low-level probing mode does not "
					"support 'list' output format\n");
			exit(BLKID_EXIT_OTHER);
		}
		pretty_print_dev(NULL);
	}

	if (lowprobe) {
		/*
		 * Low-level API
		 */
		blkid_probe pr;

		if (!numdev) {
			fprintf(stderr, "The low-level probing mode "
					"requires a device\n");
			exit(BLKID_EXIT_OTHER);
		}

		/* automatically enable 'export' format for I/O Limits */
		if (!output_format  && (lowprobe & LOWPROBE_TOPOLOGY))
			output_format = OUTPUT_EXPORT_LIST;

		pr = blkid_new_probe();
		if (!pr)
			goto exit;

		if (lowprobe & LOWPROBE_SUPERBLOCKS) {
			blkid_probe_set_superblocks_flags(pr,
				BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
				BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE |
				BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION);

			if (fltr_usage && blkid_probe_filter_superblocks_usage(
						pr, fltr_flag, fltr_usage))
				goto exit;

			else if (fltr_type && blkid_probe_filter_superblocks_type(
						pr, fltr_flag, fltr_type))
				goto exit;
		}

		for (i = 0; i < numdev; i++) {
			err = lowprobe_device(pr, devices[i], lowprobe, show,
					output_format,
					(blkid_loff_t) offset,
					(blkid_loff_t) size);
			if (err)
				break;
		}
		blkid_free_probe(pr);
	} else if (eval) {
		/*
		 * Evaluate API
		 */
		char *res = blkid_evaluate_tag(search_type, search_value, NULL);
		if (res) {
			err = 0;
			printf("%s\n", res);
		}
	} else if (lookup) {
		/*
		 * Classic (cache based) API
		 */
		blkid_dev dev;

		if (!search_type) {
			fprintf(stderr, "The lookup option requires a "
				"search type specified using -t\n");
			exit(BLKID_EXIT_OTHER);
		}
		/* Load any additional devices not in the cache */
		for (i = 0; i < numdev; i++)
			blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL);

		if ((dev = blkid_find_dev_with_tag(cache, search_type,
						   search_value))) {
			print_tags(dev, show, output_format);
			err = 0;
		}
	/* If we didn't specify a single device, show all available devices */
	} else if (!numdev) {
		blkid_dev_iterate	iter;
		blkid_dev		dev;

		blkid_probe_all(cache);

		iter = blkid_dev_iterate_begin(cache);
		blkid_dev_set_search(iter, search_type, search_value);
		while (blkid_dev_next(iter, &dev) == 0) {
			dev = blkid_verify(cache, dev);
			if (!dev)
				continue;
			print_tags(dev, show, output_format);
			err = 0;
		}
		blkid_dev_iterate_end(iter);
	/* Add all specified devices to cache (optionally display tags) */
	} else for (i = 0; i < numdev; i++) {
		blkid_dev dev = blkid_get_dev(cache, devices[i],
						  BLKID_DEV_NORMAL);

		if (dev) {
			if (search_type &&
			    !blkid_dev_has_tag(dev, search_type,
					       search_value))
				continue;
			print_tags(dev, show, output_format);
			err = 0;
		}
	}

exit:
	free(search_type);
	free(search_value);
	free_types_list(fltr_type);
	if (!lowprobe && !eval)
		blkid_put_cache(cache);
	free(devices);
	return err;
}
コード例 #7
0
ファイル: wipefs.c プロジェクト: justinc1985/IntelRangeley
int
main(int argc, char **argv)
{
	struct wipe_desc *wp0 = NULL, *wp;
	int c, has_offset = 0, flags = 0;
	int mode = WP_MODE_PRETTY;

	static const struct option longopts[] = {
	    { "all",       0, 0, 'a' },
	    { "backup",    0, 0, 'b' },
	    { "force",     0, 0, 'f' },
	    { "help",      0, 0, 'h' },
	    { "no-act",    0, 0, 'n' },
	    { "offset",    1, 0, 'o' },
	    { "parsable",  0, 0, 'p' },
	    { "quiet",     0, 0, 'q' },
	    { "types",     1, 0, 't' },
	    { "version",   0, 0, 'V' },
	    { NULL,        0, 0, 0 }
	};

	static const ul_excl_t excl[] = {       /* rows and cols in in ASCII order */
		{ 'a','o' },
		{ 0 }
	};
	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	atexit(close_stdout);

	while ((c = getopt_long(argc, argv, "afhno:pqt:V", longopts, NULL)) != -1) {

		err_exclusive_options(c, longopts, excl, excl_st);

		switch(c) {
		case 'a':
			flags |= WP_FL_ALL;
			break;
		case 'b':
			flags |= WP_FL_BACKUP;
			break;
		case 'f':
			flags |= WP_FL_FORCE;
			break;
		case 'h':
			usage(stdout);
			break;
		case 'n':
			flags |= WP_FL_NOACT;
			break;
		case 'o':
			wp0 = add_offset(wp0, strtosize_or_err(optarg,
					 _("invalid offset argument")), 1);
			has_offset++;
			break;
		case 'p':
			mode = WP_MODE_PARSABLE;
			break;
		case 'q':
			flags |= WP_FL_QUIET;
			break;
		case 't':
			type_pattern = optarg;
			break;
		case 'V':
			printf(UTIL_LINUX_VERSION);
			return EXIT_SUCCESS;
		default:
			usage(stderr);
			break;
		}
	}

	if (optind == argc)
		usage(stderr);

	if ((flags & WP_FL_BACKUP) && !((flags & WP_FL_ALL) || has_offset))
		warnx(_("The --backup option is meaningless in this context"));

	if (!(flags & WP_FL_ALL) && !has_offset) {
		/*
		 * Print only
		 */
		while (optind < argc) {
			wp0 = read_offsets(NULL, argv[optind++]);
			if (wp0)
				print_all(wp0, mode);
			free_wipe(wp0);
		}
	} else {
		/*
		 * Erase
		 */
		while (optind < argc) {
			wp = clone_offset(wp0);
			wp = do_wipe(wp, argv[optind++], flags);
			free_wipe(wp);
		}
	}

	return EXIT_SUCCESS;
}
コード例 #8
0
ファイル: rtcwake.c プロジェクト: 0xAX/util-linux
int main(int argc, char **argv)
{
    struct rtcwake_control ctl = {
        .mode_str = "suspend",		/* default mode */
        .adjfile = _PATH_ADJTIME,
        .clock_mode = CM_AUTO
    };
    char *devname = DEFAULT_RTC_DEVICE;
    unsigned seconds = 0;
    int suspend = SYSFS_MODE;
    int rc = EXIT_SUCCESS;
    int t;
    int fd;
    time_t alarm = 0;
    enum {
        OPT_DATE = CHAR_MAX + 1,
        OPT_LIST
    };
    static const struct option long_options[] = {
        {"adjfile",     required_argument,      0, 'A'},
        {"auto",	no_argument,		0, 'a'},
        {"dry-run",	no_argument,		0, 'n'},
        {"local",	no_argument,		0, 'l'},
        {"utc",		no_argument,		0, 'u'},
        {"verbose",	no_argument,		0, 'v'},
        {"version",	no_argument,		0, 'V'},
        {"help",	no_argument,		0, 'h'},
        {"mode",	required_argument,	0, 'm'},
        {"device",	required_argument,	0, 'd'},
        {"seconds",	required_argument,	0, 's'},
        {"time",	required_argument,	0, 't'},
        {"date",	required_argument,	0, OPT_DATE},
        {"list-modes",	no_argument,		0, OPT_LIST},
        {0,		0,			0, 0  }
    };
    static const ul_excl_t excl[] = {
        { 'a', 'l', 'u' },
        { 's', 't', OPT_DATE },
    };
    int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;

    setlocale(LC_ALL, "");
    bindtextdomain(PACKAGE, LOCALEDIR);
    textdomain(PACKAGE);
    atexit(close_stdout);

    while ((t = getopt_long(argc, argv, "A:ahd:lm:ns:t:uVv",
                            long_options, NULL)) != EOF) {
        err_exclusive_options(t, long_options, excl, excl_st);
        switch (t) {
        case 'A':
            /* for better compatibility with hwclock */
            ctl.adjfile = optarg;
            break;
        case 'a':
            ctl.clock_mode = CM_AUTO;
            break;
        case 'd':
            devname = optarg;
            break;
        case 'l':
            ctl.clock_mode = CM_LOCAL;
            break;

        case OPT_LIST:
            list_modes(&ctl);
            return EXIT_SUCCESS;

        case 'm':
            if ((suspend = get_rtc_mode(&ctl, optarg)) < 0)
                errx(EXIT_FAILURE, _("unrecognized suspend state '%s'"), optarg);
            ctl.mode_str = optarg;
            break;
        case 'n':
            ctl.dryrun = 1;
            break;
        case 's':
            /* alarm time, seconds-to-sleep (relative) */
            seconds = strtou32_or_err(optarg, _("invalid seconds argument"));
            break;
        case 't':
            /* alarm time, time_t (absolute, seconds since epoch) */
            alarm = strtou32_or_err(optarg, _("invalid time argument"));
            break;
        case OPT_DATE:
        {   /* alarm time, see timestamp format from manual */
            usec_t p;
            if (parse_timestamp(optarg, &p) < 0)
                errx(EXIT_FAILURE, _("invalid time value \"%s\""), optarg);
            alarm = (time_t) (p / 1000000);
            break;
        }
        case 'u':
            ctl.clock_mode = CM_UTC;
            break;
        case 'v':
            ctl.verbose = 1;
            break;
        case 'V':
            printf(UTIL_LINUX_VERSION);
            exit(EXIT_SUCCESS);
        case 'h':
            usage(stdout);
        default:
            usage(stderr);
        }
    }

    if (ctl.clock_mode == CM_AUTO) {
        if (read_clock_mode(&ctl) < 0) {
            printf(_("%s: assuming RTC uses UTC ...\n"),
                   program_invocation_short_name);
            ctl.clock_mode = CM_UTC;
        }
    }

    if (ctl.verbose)
        printf("%s",  ctl.clock_mode == CM_UTC ? _("Using UTC time.\n") :
               _("Using local time.\n"));

    if (!alarm && !seconds && suspend != DISABLE_MODE && suspend != SHOW_MODE)
        errx(EXIT_FAILURE, _("must provide wake time (see --seconds, --time and --date options)"));

    /* device must exist and (if we'll sleep) be wakeup-enabled */
    fd = open_dev_rtc(devname);

    if (suspend != ON_MODE && suspend != NO_MODE && !is_wakeup_enabled(devname))
        errx(EXIT_FAILURE, _("%s not enabled for wakeup events"), devname);

    /* relative or absolute alarm time, normalized to time_t */
    if (get_basetimes(&ctl, fd) < 0)
        exit(EXIT_FAILURE);

    if (ctl.verbose)
        printf(_("alarm %ld, sys_time %ld, rtc_time %ld, seconds %u\n"),
               alarm, ctl.sys_time, ctl.rtc_time, seconds);

    if (suspend != DISABLE_MODE && suspend != SHOW_MODE) {
        /* perform alarm setup when the show or disable modes are not set */
        if (alarm) {
            if (alarm < ctl.sys_time)
                errx(EXIT_FAILURE, _("time doesn't go backward to %s"),
                     ctime(&alarm));
            alarm += ctl.sys_time - ctl.rtc_time;
        } else
            alarm = ctl.rtc_time + seconds + 1;

        if (setup_alarm(&ctl, fd, &alarm) < 0)
            exit(EXIT_FAILURE);

        if (suspend == NO_MODE || suspend == ON_MODE)
            printf(_("%s: wakeup using %s at %s"),
                   program_invocation_short_name, devname,
                   ctime(&alarm));
        else
            printf(_("%s: wakeup from \"%s\" using %s at %s"),
                   program_invocation_short_name, ctl.mode_str, devname,
                   ctime(&alarm));
        fflush(stdout);
        xusleep(10 * 1000);
    }

    switch (suspend) {
    case NO_MODE:
        if (ctl.verbose)
            printf(_("suspend mode: no; leaving\n"));
        ctl.dryrun = 1;	/* to skip disabling alarm at the end */
        break;
    case OFF_MODE:
    {
        char *arg[5];
        int i = 0;

        if (ctl.verbose)
            printf(_("suspend mode: off; executing %s\n"),
                   _PATH_SHUTDOWN);
        arg[i++] = _PATH_SHUTDOWN;
        arg[i++] = "-h";
        arg[i++] = "-P";
        arg[i++] = "now";
        arg[i]   = NULL;
        if (!ctl.dryrun) {
            execv(arg[0], arg);
            warn(_("failed to execute %s"), _PATH_SHUTDOWN);
            rc = EXIT_FAILURE;
        }
        break;
    }
    case ON_MODE:
    {
        unsigned long data;

        if (ctl.verbose)
            printf(_("suspend mode: on; reading rtc\n"));
        if (!ctl.dryrun) {
            do {
                t = read(fd, &data, sizeof data);
                if (t < 0) {
                    warn(_("rtc read failed"));
                    break;
                }
                if (ctl.verbose)
                    printf("... %s: %03lx\n", devname, data);
            } while (!(data & RTC_AF));
        }
        break;
    }
    case DISABLE_MODE:
        /* just break, alarm gets disabled in the end */
        if (ctl.verbose)
            printf(_("suspend mode: disable; disabling alarm\n"));
        break;
    case SHOW_MODE:
        if (ctl.verbose)
            printf(_("suspend mode: show; printing alarm info\n"));
        if (print_alarm(&ctl, fd))
            rc = EXIT_FAILURE;
        ctl.dryrun = 1;	/* don't really disable alarm in the end, just show */
        break;
    default:
        if (ctl.verbose)
            printf(_("suspend mode: %s; suspending system\n"), ctl.mode_str);
        sync();
        suspend_system(&ctl);
    }

    if (!ctl.dryrun) {
        struct rtc_wkalrm wake;

        if (ioctl(fd, RTC_WKALM_RD, &wake) < 0) {
            warn(_("read rtc alarm failed"));
            rc = EXIT_FAILURE;
        } else {
            wake.enabled = 0;
            if (ioctl(fd, RTC_WKALM_SET, &wake) < 0) {
                warn(_("disable rtc alarm interrupt failed"));
                rc = EXIT_FAILURE;
            }
        }
    }

    close(fd);
    return rc;
}
コード例 #9
0
ファイル: fsfreeze.c プロジェクト: ArakniD/util-linux
int main(int argc, char **argv)
{
	int fd = -1, c;
	int action = NOOP, rc = EXIT_FAILURE;
	char *path;
	struct stat sb;

	static const struct option longopts[] = {
	    { "help",      0, 0, 'h' },
	    { "freeze",    0, 0, 'f' },
	    { "unfreeze",  0, 0, 'u' },
	    { "version",   0, 0, 'V' },
	    { NULL,        0, 0, 0 }
	};

	static const ul_excl_t excl[] = {       /* rows and cols in in ASCII order */
		{ 'f','u' },			/* freeze, unfreeze */
		{ 0 }
	};
	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	atexit(close_stdout);

	while ((c = getopt_long(argc, argv, "hfuV", longopts, NULL)) != -1) {

		err_exclusive_options(c, longopts, excl, excl_st);

		switch(c) {
		case 'h':
			usage(stdout);
			break;
		case 'f':
			action = FREEZE;
			break;
		case 'u':
			action = UNFREEZE;
			break;
		case 'V':
			printf(UTIL_LINUX_VERSION);
			exit(EXIT_SUCCESS);
		default:
			usage(stderr);
			break;
		}
	}

	if (action == NOOP)
		errx(EXIT_FAILURE, _("neither --freeze or --unfreeze specified"));
	if (optind == argc)
		errx(EXIT_FAILURE, _("no filename specified"));
	path = argv[optind++];

	if (optind != argc) {
		warnx(_("unexpected number of arguments"));
		usage(stderr);
	}

	fd = open(path, O_RDONLY);
	if (fd < 0)
		err(EXIT_FAILURE, _("cannot open %s"), path);

	if (fstat(fd, &sb) == -1) {
		warn(_("stat of %s failed"), path);
		goto done;
	}

	if (!S_ISDIR(sb.st_mode)) {
		warnx(_("%s: is not a directory"), path);
		goto done;
	}

	switch (action) {
	case FREEZE:
		if (freeze_f(fd)) {
			warn(_("%s: freeze failed"), path);
			goto done;
		}
		break;
	case UNFREEZE:
		if (unfreeze_f(fd)) {
			warn(_("%s: unfreeze failed"), path);
			goto done;
		}
		break;
	default:
		abort();
	}

	rc = EXIT_SUCCESS;
done:
	if (fd >= 0)
		close(fd);
	return rc;
}
コード例 #10
0
ファイル: fromfile.c プロジェクト: Webster-WXH/util-linux
int main(int argc, char *argv[])
{
	struct libscols_table *tb;
	int c, n, nlines = 0;
	int parent_col = -1, id_col = -1;

	static const struct option longopts[] = {
		{ "maxout", 0, NULL, 'm' },
		{ "column", 1, NULL, 'c' },
		{ "nlines", 1, NULL, 'n' },
		{ "width",  1, NULL, 'w' },
		{ "tree-parent-column", 1, NULL, 'p' },
		{ "tree-id-column",	1, NULL, 'i' },
		{ "json",   0, NULL, 'J' },
		{ "raw",    0, NULL, 'r' },
		{ "export", 0, NULL, 'E' },
		{ "colsep",  1, NULL, 'C' },
		{ "help",   0, NULL, 'h' },
		{ NULL, 0, NULL, 0 },
	};

	static const ul_excl_t excl[] = {       /* rows and cols in ASCII order */
		{ 'E', 'J', 'r' },
		{ 0 }
	};
	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;

	setlocale(LC_ALL, "");	/* just to have enable UTF8 chars */
	scols_init_debug(0);

	tb = scols_new_table();
	if (!tb)
		err(EXIT_FAILURE, "failed to create output table");

	while((c = getopt_long(argc, argv, "hCc:Ei:Jmn:p:rw:", longopts, NULL)) != -1) {

		err_exclusive_options(c, longopts, excl, excl_st);

		switch(c) {
		case 'c': /* add column from file */
		{
			struct libscols_column *cl;
			FILE *f = fopen(optarg, "r");

			if (!f)
				err(EXIT_FAILURE, "%s: open failed", optarg);
			cl = parse_column(f);
			if (cl && scols_table_add_column(tb, cl))
				err(EXIT_FAILURE, "%s: failed to add column", optarg);
			scols_unref_column(cl);
			fclose(f);
			break;
		}
		case 'p':
			parent_col = strtou32_or_err(optarg, "failed to parse tree PARENT column");
			break;
		case 'i':
			id_col = strtou32_or_err(optarg, "failed to parse tree ID column");
			break;
		case 'J':
			scols_table_enable_json(tb, 1);
			scols_table_set_name(tb, "testtable");
			break;
		case 'm':
			scols_table_enable_maxout(tb, TRUE);
			break;
		case 'r':
			scols_table_enable_raw(tb, TRUE);
			break;
		case 'E':
			scols_table_enable_export(tb, TRUE);
			break;
		case 'C':
			scols_table_set_column_separator(tb, optarg);
			break;
		case 'n':
			nlines = strtou32_or_err(optarg, "failed to parse number of lines");
			break;
		case 'w':
			scols_table_set_termforce(tb, SCOLS_TERMFORCE_ALWAYS);
			scols_table_set_termwidth(tb, strtou32_or_err(optarg, "failed to parse terminal width"));
			break;
		case 'h':
			usage();
		default:
			errtryhelp(EXIT_FAILURE);
		}
	}

	if (nlines <= 0)
		errx(EXIT_FAILURE, "--nlines not set");

	for (n = 0; n < nlines; n++) {
		struct libscols_line *ln = scols_new_line();

		if (!ln || scols_table_add_line(tb, ln))
			err(EXIT_FAILURE, "failed to add a new line");

		scols_unref_line(ln);
	}

	n = 0;

	while (optind < argc) {
		FILE *f = fopen(argv[optind], "r");

		if (!f)
			err(EXIT_FAILURE, "%s: open failed", argv[optind]);

		parse_column_data(f, tb, n);
		optind++;
		n++;
	}

	if (scols_table_is_tree(tb) && parent_col >= 0 && id_col >= 0)
		compose_tree(tb, parent_col, id_col);

	scols_table_enable_colors(tb, isatty(STDOUT_FILENO));

	scols_print_table(tb);
	scols_unref_table(tb);
	return EXIT_SUCCESS;
}
コード例 #11
0
ファイル: losetup.c プロジェクト: knot/util-linux
int main(int argc, char **argv)
{
	struct loopdev_cxt lc;
	int act = 0, flags = 0, c;
	char *file = NULL;
	uint64_t offset = 0, sizelimit = 0;
	int res = 0, showdev = 0, lo_flags = 0;

	enum {
		OPT_SIZELIMIT = CHAR_MAX + 1,
		OPT_SHOW
	};
	static const struct option longopts[] = {
		{ "all", 0, 0, 'a' },
		{ "set-capacity", 1, 0, 'c' },
		{ "detach", 1, 0, 'd' },
		{ "detach-all", 0, 0, 'D' },
		{ "encryption", 1, 0, 'e' },
		{ "find", 0, 0, 'f' },
		{ "help", 0, 0, 'h' },
		{ "associated", 1, 0, 'j' },
		{ "offset", 1, 0, 'o' },
		{ "sizelimit", 1, 0, OPT_SIZELIMIT },
		{ "pass-fd", 1, 0, 'p' },
		{ "partscan", 0, 0, 'P' },
		{ "read-only", 0, 0, 'r' },
	        { "show", 0, 0, OPT_SHOW },
		{ "verbose", 0, 0, 'v' },
		{ "version", 0, 0, 'V' },
		{ NULL, 0, 0, 0 }
	};

	static const ul_excl_t excl[] = {	/* rows and cols in ASCII order */
		{ 'D','a','c','d','f','j' },
		{ 0 }
	};
	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	atexit(close_stdout);

	if (loopcxt_init(&lc, 0))
		err(EXIT_FAILURE, _("failed to initialize loopcxt"));

	while ((c = getopt_long(argc, argv, "ac:d:De:E:fhj:o:p:PrvV",
				longopts, NULL)) != -1) {

		err_exclusive_options(c, longopts, excl, excl_st);

		switch (c) {
		case 'a':
			act = A_SHOW;
			break;
		case 'c':
			act = A_SET_CAPACITY;
			if (loopcxt_set_device(&lc, optarg))
				err(EXIT_FAILURE, _("%s: failed to use device"),
						optarg);
			break;
		case 'r':
			lo_flags |= LO_FLAGS_READ_ONLY;
			break;
		case 'd':
			act = A_DELETE;
			if (loopcxt_set_device(&lc, optarg))
				err(EXIT_FAILURE, _("%s: failed to use device"),
						optarg);
			break;
		case 'D':
			act = A_DELETE_ALL;
			break;
		case 'E':
		case 'e':
			errx(EXIT_FAILURE, _("encryption not supported, use cryptsetup(8) instead"));
			break;
		case 'f':
			act = A_FIND_FREE;
			break;
		case 'h':
			usage(stdout);
			break;
		case 'j':
			act = A_SHOW;
			file = optarg;
			break;
		case 'o':
			offset = strtosize_or_err(optarg, _("failed to parse offset"));
			flags |= LOOPDEV_FL_OFFSET;
			break;
		case 'p':
                        warn(_("--pass-fd is no longer supported"));
			break;
		case 'P':
			lo_flags |= LO_FLAGS_PARTSCAN;
			break;
		case OPT_SHOW:
			showdev = 1;
			break;
		case 'v':
			verbose = 1;
			break;
		case 'V':
			printf(UTIL_LINUX_VERSION);
			return EXIT_SUCCESS;
		case OPT_SIZELIMIT:			/* --sizelimit */
			sizelimit = strtosize_or_err(optarg, _("failed to parse size"));
			flags |= LOOPDEV_FL_SIZELIMIT;
                        break;
		default:
			usage(stderr);
		}
	}

	if (argc == 1)
		usage(stderr);

	if (act == A_FIND_FREE && optind < argc) {
		/*
		 * losetup -f <backing_file>
		 */
		act = A_CREATE;
		file = argv[optind++];
	}
	if (!act && optind + 1 == argc) {
		/*
		 * losetup <device>
		 */
		act = A_SHOW_ONE;
		if (loopcxt_set_device(&lc, argv[optind]))
			err(EXIT_FAILURE, _("%s: failed to use device"),
					argv[optind]);
		optind++;
	}
	if (!act) {
		/*
		 * losetup <loopdev> <backing_file>
		 */
		act = A_CREATE;

		if (optind >= argc)
			errx(EXIT_FAILURE, _("no loop device specified"));
		if (loopcxt_set_device(&lc, argv[optind]))
			err(EXIT_FAILURE, _("%s: failed to use device"),
					argv[optind]);
		optind++;

		if (optind >= argc)
			errx(EXIT_FAILURE, _("no file specified"));
		file = argv[optind++];
	}

	if (act != A_CREATE &&
	    (sizelimit || lo_flags || showdev))
		errx(EXIT_FAILURE,
			_("the options %s are allowed to loop device setup only"),
			"--{sizelimit,read-only,show}");

	if ((flags & LOOPDEV_FL_OFFSET) &&
	    act != A_CREATE && (act != A_SHOW || !file))
		errx(EXIT_FAILURE, _("the option --offset is not allowed in this context."));

	switch (act) {
	case A_CREATE:
	{
		int hasdev = loopcxt_has_device(&lc);

		do {
			/* Note that loopcxt_{find_unused,set_device}() resets
			 * loopcxt struct.
			 */
			if (!hasdev && (res = loopcxt_find_unused(&lc))) {
				warnx(_("not found unused device"));
				break;
			}
			if (flags & LOOPDEV_FL_OFFSET)
				loopcxt_set_offset(&lc, offset);
			if (flags & LOOPDEV_FL_SIZELIMIT)
				loopcxt_set_sizelimit(&lc, sizelimit);
			if (lo_flags)
				loopcxt_set_flags(&lc, lo_flags);
			if ((res = loopcxt_set_backing_file(&lc, file))) {
				warn(_("%s: failed to use backing file"), file);
				break;
			}
			errno = 0;
			res = loopcxt_setup_device(&lc);
			if (res == 0)
				break;			/* success */
			if (errno != EBUSY) {
				warn(_("%s: failed to setup loop device"),
					hasdev && loopcxt_get_fd(&lc) < 0 ?
					    loopcxt_get_device(&lc) : file);
				break;
			}
		} while (hasdev == 0);

		if (res == 0) {
			if (showdev)
				printf("%s\n", loopcxt_get_device(&lc));
			warn_size(file, sizelimit);
		}
		break;
	}
	case A_DELETE:
		res = delete_loop(&lc);
		while (optind < argc) {
			if (loopcxt_set_device(&lc, argv[optind]))
				warn(_("%s: failed to use device"),
						argv[optind]);
			optind++;
			res += delete_loop(&lc);
		}
		break;
	case A_DELETE_ALL:
		res = delete_all_loops(&lc);
		break;
	case A_FIND_FREE:
		if (loopcxt_find_unused(&lc))
			warn(_("find unused loop device failed"));
		else
			printf("%s\n", loopcxt_get_device(&lc));
		break;
	case A_SHOW:
		res = show_all_loops(&lc, file, offset, flags);
		break;
	case A_SHOW_ONE:
		res = printf_loopdev(&lc);
		if (res)
			warn(_("%s"), loopcxt_get_device(&lc));
		break;
	case A_SET_CAPACITY:
		res = set_capacity(&lc);
		break;
	default:
		usage(stderr);
		break;
	}

	loopcxt_deinit(&lc);
	return res ? EXIT_FAILURE : EXIT_SUCCESS;
}