static int parse_febg_color(const char *arg) { int color; if (strcmp(arg, "black") == 0) return BLACK; else if (strcmp(arg, "red") == 0) return RED; else if (strcmp(arg, "green") == 0) return GREEN; else if (strcmp(arg, "yellow") == 0) return YELLOW; else if (strcmp(arg, "blue") == 0) return BLUE; else if (strcmp(arg, "magenta") == 0) return MAGENTA; else if (strcmp(arg, "cyan") == 0) return CYAN; else if (strcmp(arg, "white") == 0) return WHITE; else if (strcmp(arg, "default") == 0) return DEFAULT; else color = strtos32_or_err(arg, _("argument error")); if (color < BLACK || DEFAULT < color || color == GREY) errx(EXIT_FAILURE, _("argument error: %s"), arg); return color; }
static int parse_msglevel(const char *arg) { int ret; ret = strtos32_or_err(arg, _("argument error")); if (ret < CONSOLE_LEVEL_MIN || CONSOLE_LEVEL_MAX < ret) errx(EXIT_FAILURE, "%s: %s", _("argument error"), arg); return ret; }
static int parse_bfreq(char **av, char *oa, int *oi) { char *arg; arg = find_optional_arg(av, oa, oi); if (!arg) return 0; return strtos32_or_err(arg, _("argument error")); }
static void parse_tabs(char **argv, char *optarg, int *optind, int *tab_array) { int i = 0; if (optarg) { tab_array[i] = strtos32_or_err(optarg, _("argument error")); i++; } while (argv[*optind]) { if (TABS_MAX < i) errx(EXIT_FAILURE, _("too many tabs")); if (argv[*optind][0] == '-') break; tab_array[i] = strtos32_or_err(argv[*optind], _("argument error")); (*optind)++; i++; } tab_array[i] = -1; }
static void parse_tabs(char **av, char *oa, int *oi, int *tab_array) { int i = 0; if (oa) { tab_array[i] = strtos32_or_err(oa, _("argument error")); i++; } while (av[*oi]) { if (TABS_MAX < i) errx(EXIT_FAILURE, _("too many tabs")); if (av[*oi][0] == '-') break; tab_array[i] = strtos32_or_err(av[*oi], _("argument error")); (*oi)++; i++; } tab_array[i] = -1; }
static int parse_febg_color(const char *arg) { int color = parse_color(arg); if (color < 0) color = strtos32_or_err(arg, _("argument error")); if (!is_valid_color(color) || color == GREY) errx(EXIT_FAILURE, "%s: %s", _("argument error"), arg); return color; }
static int parse_regtabs(char **av, char *oa, int *oi) { int ret; char *arg; arg = find_optional_arg(av, oa, oi); if (!arg) return DEFAULT_TAB_LEN; ret = strtos32_or_err(arg, _("argument error")); if (ret < 1 || TABS_MAX < ret) errx(EXIT_FAILURE, "%s: %s", _("argument error"), arg); return ret; }
static int parse_blength(char **av, char *oa, int *oi) { int ret = -1; char *arg; arg = find_optional_arg(av, oa, oi); if (!arg) return 0; ret = strtos32_or_err(arg, _("argument error")); if (ret < 0 || BLENGTH_MAX < ret) errx(EXIT_FAILURE, "%s: %s", _("argument error"), arg); return ret; }
static int parse_snap(char **av, char *oa, int *oi) { int ret; char *arg; arg = find_optional_arg(av, oa, oi); if (!arg) return 0; ret = strtos32_or_err(arg, _("argument error")); if (ret < 1) errx(EXIT_FAILURE, "%s: %s", _("argument error"), arg); return ret; }
static int parse_iflag(char *str, int *set_iflag, int *clr_iflag) { int iflag; char *s; for (s = strtok(str, ","); s != NULL; s = strtok(NULL, ",")) { if (*s == '-') s++; if ((iflag = lookup_table(ld_iflags, s)) < 0) iflag = strtos32_or_err(s, _("invalid iflag")); if (s > str && *(s - 1) == '-') *clr_iflag |= iflag; else *set_iflag |= iflag; } dbg("iflag (set/clear): %d/%d", *set_iflag, *clr_iflag); return 0; }
static int parse_blank(char **av, char *oa, int *oi) { char *arg; arg = find_optional_arg(av, oa, oi); if (!arg) return BLANKEDSCREEN; if (!strcmp(arg, "force")) return BLANKSCREEN; else if (!strcmp(arg, "poke")) return UNBLANKSCREEN; else { int ret; ret = strtos32_or_err(arg, _("argument error")); if (ret < 0 || BLANK_MAX < ret) errx(EXIT_FAILURE, "%s: %s", _("argument error"), arg); return ret; } }
static int parse_ulhb_color(char **argv, int *optind) { char *color_name; int bright = 0; int color = -1; if (argv[*optind] && strcmp(argv[*optind - 1], "bright") == 0) { bright = 1; color_name = argv[*optind]; (*optind)++; } else color_name = argv[*optind - 1]; if (strcmp(color_name, "black") == 0) color = BLACK; else if (strcmp(color_name, "grey") == 0) color = GREY; else if (strcmp(color_name, "red") == 0) color = RED; else if (strcmp(color_name, "green") == 0) color = GREEN; else if (strcmp(color_name, "yellow") == 0) color = YELLOW; else if (strcmp(color_name, "blue") == 0) color = BLUE; else if (strcmp(color_name, "magenta") == 0) color = MAGENTA; else if (strcmp(color_name, "cyan") == 0) color = CYAN; else if (strcmp(color_name, "white") == 0) color = WHITE; else { color = strtos32_or_err(color_name, _("argument error")); if (color < BLACK || DEFAULT < color) errx(EXIT_FAILURE, _("argument error: %s"), color_name); } if (bright && (color == BLACK || color == GREY)) errx(EXIT_FAILURE, _("argument error: bright %s is not supported"), color_name); return color; }
static int parse_ulhb_color(char **av, int *oi) { char *color_name; int bright = 0; int color = -1; if (av[*oi] && strcmp(av[*oi - 1], "bright") == 0) { bright = 1; color_name = av[*oi]; (*oi)++; } else color_name = av[*oi - 1]; color = parse_color(color_name); if (color < 0) color = strtos32_or_err(color_name, _("argument error")); if (!is_valid_color(color)) errx(EXIT_FAILURE, "%s: %s", _("argument error"), color_name); if (bright && (color == BLACK || color == GREY)) errx(EXIT_FAILURE, _("argument error: bright %s is not supported"), color_name); return color; }
int main(int argc, char *argv[]) { struct libscols_table *tb; int c, notree = 0, nstart = -1, nend = -1; static const struct option longopts[] = { { "ascii", 0, 0, 'i' }, { "csv", 0, 0, 'c' }, { "list", 0, 0, 'l' }, { "noheadings", 0, 0, 'n' }, { "pairs", 0, 0, 'p' }, { "json", 0, 0, 'J' }, { "raw", 0, 0, 'r' }, { "range-start",1, 0, 'S' }, { "range-end", 1, 0, 'E' }, { NULL, 0, 0, 0 }, }; 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, "ciJlnprS:E:", longopts, NULL)) != -1) { switch(c) { case 'c': scols_table_set_column_separator(tb, ","); scols_table_enable_raw(tb, 1); notree = 1; break; case 'i': scols_table_enable_ascii(tb, 1); break; case 'J': scols_table_set_name(tb, "scolstest"); scols_table_enable_json(tb, 1); break; case 'l': notree = 1; break; case 'n': scols_table_enable_noheadings(tb, 1); break; case 'p': scols_table_enable_export(tb, 1); notree = 1; break; case 'r': scols_table_enable_raw(tb, 1); notree = 1; break; case 'S': nstart = strtos32_or_err(optarg, "failed to parse range start") - 1; break; case 'E': nend = strtos32_or_err(optarg, "failed to parse range end") - 1; break; default: usage(stderr); } } scols_table_enable_colors(tb, 1); setup_columns(tb, notree); if (optind == argc) add_lines(tb, "."); else while (optind < argc) add_lines(tb, argv[optind++]); if (nstart >= 0 || nend >= 0) { /* print subset */ struct libscols_line *start = NULL, *end = NULL; if (nstart >= 0) start = scols_table_get_line(tb, nstart); if (nend >= 0) end = scols_table_get_line(tb, nend); if (start || end) scols_table_print_range(tb, start, end); } else /* print all table */ scols_print_table(tb); scols_unref_table(tb); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { struct itimerval timeout, old_timer; int have_timeout = 0; int type = LOCK_EX; int block = 0; int open_flags = 0; int fd = -1; int opt, ix; int do_close = 0; int status; /* * The default exit code for lock conflict or timeout * is specified in man flock.1 */ int conflict_exit_code = 1; char **cmd_argv = NULL, *sh_c_argv[4]; const char *filename = NULL; struct sigaction old_sa; static const struct option long_options[] = { {"shared", no_argument, NULL, 's'}, {"exclusive", no_argument, NULL, 'x'}, {"unlock", no_argument, NULL, 'u'}, {"nonblocking", no_argument, NULL, 'n'}, {"nb", no_argument, NULL, 'n'}, {"timeout", required_argument, NULL, 'w'}, {"wait", required_argument, NULL, 'w'}, {"conflict-exit-code", required_argument, NULL, 'E'}, {"close", no_argument, NULL, 'o'}, {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'V'}, {NULL, 0, NULL, 0} }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); if (argc < 2) usage(EX_USAGE); memset(&timeout, 0, sizeof timeout); optopt = 0; while ((opt = getopt_long(argc, argv, "+sexnouw:E:hV?", long_options, &ix)) != EOF) { switch (opt) { case 's': type = LOCK_SH; break; case 'e': case 'x': type = LOCK_EX; break; case 'u': type = LOCK_UN; break; case 'o': do_close = 1; break; case 'n': block = LOCK_NB; break; case 'w': have_timeout = 1; strtotimeval_or_err(optarg, &timeout.it_value, _("invalid timeout value")); break; case 'E': conflict_exit_code = strtos32_or_err(optarg, _("invalid exit code")); break; case 'V': printf(UTIL_LINUX_VERSION); exit(EX_OK); default: /* optopt will be set if this was an unrecognized * option, i.e. *not* 'h' or '? */ usage(optopt ? EX_USAGE : 0); break; } } if (argc > optind + 1) { /* Run command */ if (!strcmp(argv[optind + 1], "-c") || !strcmp(argv[optind + 1], "--command")) { if (argc != optind + 3) errx(EX_USAGE, _("%s requires exactly one command argument"), argv[optind + 1]); cmd_argv = sh_c_argv; cmd_argv[0] = getenv("SHELL"); if (!cmd_argv[0] || !*cmd_argv[0]) cmd_argv[0] = _PATH_BSHELL; cmd_argv[1] = "-c"; cmd_argv[2] = argv[optind + 2]; cmd_argv[3] = 0; } else { cmd_argv = &argv[optind + 1]; } filename = argv[optind]; fd = open_file(filename, &open_flags); } else if (optind < argc) { /* Use provided file descriptor */ fd = strtos32_or_err(argv[optind], _("bad file descriptor")); } else { /* Bad options */ errx(EX_USAGE, _("requires file descriptor, file or directory")); } if (have_timeout) { if (timeout.it_value.tv_sec == 0 && timeout.it_value.tv_usec == 0) { /* -w 0 is equivalent to -n; this has to be * special-cased because setting an itimer to zero * means disabled! */ have_timeout = 0; block = LOCK_NB; } else setup_timer(&timeout, &old_timer, &old_sa, timeout_handler); } while (flock(fd, type | block)) { switch (errno) { case EWOULDBLOCK: /* -n option set and failed to lock. */ exit(conflict_exit_code); case EINTR: /* Signal received */ if (timeout_expired) /* -w option set and failed to lock. */ exit(conflict_exit_code); /* otherwise try again */ continue; case EIO: case EBADF: /* since Linux 3.4 (commit 55725513) */ /* Probably NFSv4 where flock() is emulated by fcntl(). * Let's try to reopen in read-write mode. */ if (!(open_flags & O_RDWR) && type != LOCK_SH && filename && access(filename, R_OK | W_OK) == 0) { close(fd); open_flags = O_RDWR; fd = open_file(filename, &open_flags); if (open_flags & O_RDWR) break; } /* go through */ default: /* Other errors */ if (filename) warn("%s", filename); else warn("%d", fd); exit((errno == ENOLCK || errno == ENOMEM) ? EX_OSERR : EX_DATAERR); } } if (have_timeout) cancel_timer(&old_timer, &old_sa); status = EX_OK; if (cmd_argv) { pid_t w, f; /* Clear any inherited settings */ signal(SIGCHLD, SIG_DFL); f = fork(); if (f < 0) { err(EX_OSERR, _("fork failed")); } else if (f == 0) { if (do_close) close(fd); execvp(cmd_argv[0], cmd_argv); /* execvp() failed */ warn(_("failed to execute %s"), cmd_argv[0]); _exit((errno == ENOMEM) ? EX_OSERR : EX_UNAVAILABLE); } else { do { w = waitpid(f, &status, 0); if (w == -1 && errno != EINTR) break; } while (w != f); if (w == -1) { status = EXIT_FAILURE; warn(_("waitpid failed")); } else if (WIFEXITED(status)) status = WEXITSTATUS(status); else if (WIFSIGNALED(status)) status = WTERMSIG(status) + 128; else /* WTF? */ status = EX_OSERR; } } return status; }
int main(int argc, char **argv) { int permission = 0644; int opt; size_t size = 0; int nsems = 0; int ask_shm = 0, ask_msg = 0, ask_sem = 0; static const struct option longopts[] = { {"shmem", required_argument, NULL, 'M'}, {"semaphore", required_argument, NULL, 'S'}, {"queue", no_argument, NULL, 'Q'}, {"mode", required_argument, NULL, 'p'}, {"version", no_argument, NULL, 'V'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0} }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while((opt = getopt_long(argc, argv, "hM:QS:p:Vh", longopts, NULL)) != -1) { switch(opt) { case 'M': size = strtosize_or_err(optarg, _("failed to parse size")); ask_shm = 1; break; case 'Q': ask_msg = 1; break; case 'S': nsems = strtos32_or_err(optarg, _("failed to parse elements")); ask_sem = 1; break; case 'p': permission = strtoul(optarg, NULL, 8); break; case 'h': usage(stdout); break; case 'V': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; default: errtryhelp(EXIT_FAILURE); } } if(!ask_shm && !ask_msg && !ask_sem) usage(stderr); if (ask_shm) { int shmid; if (-1 == (shmid = create_shm(size, permission))) err(EXIT_FAILURE, _("create share memory failed")); else printf(_("Shared memory id: %d\n"), shmid); } if (ask_msg) { int msgid; if (-1 == (msgid = create_msg(permission))) err(EXIT_FAILURE, _("create message queue failed")); else printf(_("Message queue id: %d\n"), msgid); } if (ask_sem) { int semid; if (-1 == (semid = create_sem(nsems, permission))) err(EXIT_FAILURE, _("create semaphore failed")); else printf(_("Semaphore id: %d\n"), semid); } return EXIT_SUCCESS; }
int main(int argc, char **argv) { int tty_fd; struct termios ts; int speed = 0, bits = '-', parity = '-', stop = '-'; int set_iflag = 0, clr_iflag = 0; int ldisc; int optc; char *dev; int intropause = 1; char *introparm = NULL; static const struct option opttbl[] = { {"speed", required_argument, NULL, 's'}, {"sevenbits", no_argument, NULL, '7'}, {"eightbits", no_argument, NULL, '8'}, {"noparity", no_argument, NULL, 'n'}, {"evenparity", no_argument, NULL, 'e'}, {"oddparity", no_argument, NULL, 'o'}, {"onestopbit", no_argument, NULL, '1'}, {"twostopbits", no_argument, NULL, '2'}, {"iflag", required_argument, NULL, 'i'}, {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'V'}, {"debug", no_argument, NULL, 'd'}, {"intro-command", no_argument, NULL, 'c'}, {"pause", no_argument, NULL, 'p'}, {NULL, 0, NULL, 0} }; signal(SIGKILL, handler); signal(SIGINT, handler); setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); /* parse options */ if (argc == 0) errx(EXIT_FAILURE, _("bad usage")); while ((optc = getopt_long(argc, argv, "dhV78neo12s:i:c:p:", opttbl, NULL)) >= 0) { switch (optc) { case 'd': debug = 1; break; case '1': case '2': stop = optc; break; case '7': case '8': bits = optc; break; case 'n': case 'e': case 'o': parity = optc; break; case 's': speed = strtos32_or_err(optarg, _("invalid speed argument")); break; case 'p': intropause = strtou32_or_err(optarg, _("invalid pause argument")); if (intropause > 10) errx(EXIT_FAILURE, "invalid pause: %s", optarg); break; case 'c': introparm = optarg; break; case 'i': parse_iflag(optarg, &set_iflag, &clr_iflag); break; case 'V': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case 'h': usage(); default: errtryhelp(EXIT_FAILURE); } } if (argc - optind != 2) { warnx(_("not enough arguments")); errtryhelp(EXIT_FAILURE); } /* parse line discipline specification */ ldisc = lookup_table(ld_discs, argv[optind]); if (ldisc < 0) ldisc = strtos32_or_err(argv[optind], _("invalid line discipline argument")); /* ldisc specific option settings */ if (ldisc == N_GIGASET_M101) { /* device specific defaults for line speed and data format */ if (speed == 0) speed = 115200; if (bits == '-') bits = '8'; if (parity == '-') parity = 'n'; if (stop == '-') stop = '1'; } /* open device */ dev = argv[optind + 1]; if ((tty_fd = open(dev, O_RDWR | O_NOCTTY)) < 0) err(EXIT_FAILURE, _("cannot open %s"), dev); if (!isatty(tty_fd)) errx(EXIT_FAILURE, _("%s is not a serial line"), dev); dbg("opened %s", dev); /* set line speed and format */ if (tcgetattr(tty_fd, &ts) < 0) err(EXIT_FAILURE, _("cannot get terminal attributes for %s"), dev); cfmakeraw(&ts); if (speed && my_cfsetspeed(&ts, speed) < 0) errx(EXIT_FAILURE, _("speed %d unsupported"), speed); switch (stop) { case '1': ts.c_cflag &= ~CSTOPB; break; case '2': ts.c_cflag |= CSTOPB; break; case '-': break; default: abort(); } switch (bits) { case '7': ts.c_cflag = (ts.c_cflag & ~CSIZE) | CS7; break; case '8': ts.c_cflag = (ts.c_cflag & ~CSIZE) | CS8; break; case '-': break; default: abort(); } switch (parity) { case 'n': ts.c_cflag &= ~(PARENB | PARODD); break; case 'e': ts.c_cflag |= PARENB; ts.c_cflag &= ~PARODD; break; case 'o': ts.c_cflag |= (PARENB | PARODD); break; case '-': break; default: abort(); } ts.c_cflag |= CREAD; /* just to be on the safe side */ ts.c_iflag |= set_iflag; ts.c_iflag &= ~clr_iflag; if (tcsetattr(tty_fd, TCSAFLUSH, &ts) < 0) err(EXIT_FAILURE, _("cannot set terminal attributes for %s"), dev); dbg("set to raw %d %c%c%c: cflag=0x%x", speed, bits, parity, stop, ts.c_cflag); if (introparm && *introparm) { dbg("intro command is '%s'", introparm); if (write_all(tty_fd, introparm, strlen(introparm)) != 0) err(EXIT_FAILURE, _("cannot write intro command to %s"), dev); if (intropause) { dbg("waiting for %d seconds", intropause); sleep(intropause); } } /* Attach the line discipline. */ if (ioctl(tty_fd, TIOCSETD, &ldisc) < 0) err(EXIT_FAILURE, _("cannot set line discipline")); dbg("line discipline set to %d", ldisc); /* ldisc specific post-attach actions */ if (ldisc == N_GSM0710) gsm0710_set_conf(tty_fd); /* Go into background if not in debug mode. */ if (!debug && daemon(0, 0) < 0) err(EXIT_FAILURE, _("cannot daemonize")); /* Sleep to keep the line discipline active. */ pause(); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { int i, policy = SCHED_RR, priority = 0, verbose = 0, policy_flag = 0, all_tasks = 0; struct sched_param sp; pid_t pid = -1; static const struct option longopts[] = { { "all-tasks", 0, NULL, 'a' }, { "batch", 0, NULL, 'b' }, { "fifo", 0, NULL, 'f' }, { "idle", 0, NULL, 'i' }, { "pid", 0, NULL, 'p' }, { "help", 0, NULL, 'h' }, { "max", 0, NULL, 'm' }, { "other", 0, NULL, 'o' }, { "rr", 0, NULL, 'r' }, { "reset-on-fork", 0, NULL, 'R' }, { "verbose", 0, NULL, 'v' }, { "version", 0, NULL, 'V' }, { NULL, 0, NULL, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while((i = getopt_long(argc, argv, "+abfiphmoRrvV", longopts, NULL)) != -1) { int ret = EXIT_FAILURE; switch (i) { case 'a': all_tasks = 1; break; case 'b': #ifdef SCHED_BATCH policy = SCHED_BATCH; #endif break; case 'f': policy = SCHED_FIFO; break; case 'R': #ifdef SCHED_RESET_ON_FORK policy_flag |= SCHED_RESET_ON_FORK; #endif break; case 'i': #ifdef SCHED_IDLE policy = SCHED_IDLE; #endif break; case 'm': show_min_max(); return EXIT_SUCCESS; case 'o': policy = SCHED_OTHER; break; case 'p': errno = 0; pid = strtos32_or_err(argv[argc - 1], _("invalid PID argument")); break; case 'r': policy = SCHED_RR; break; case 'v': verbose = 1; break; case 'V': printf("%s from %s\n", program_invocation_short_name, PACKAGE_STRING); return EXIT_SUCCESS; case 'h': ret = EXIT_SUCCESS; /* fallthrough */ default: show_usage(ret); } } if (((pid > -1) && argc - optind < 1) || ((pid == -1) && argc - optind < 2)) show_usage(EXIT_FAILURE); if ((pid > -1) && (verbose || argc - optind == 1)) { if (all_tasks) { pid_t tid; struct proc_tasks *ts = proc_open_tasks(pid); if (!ts) err(EXIT_FAILURE, _("cannot obtain the list of tasks")); while (!proc_next_tid(ts, &tid)) show_rt_info(tid, FALSE); proc_close_tasks(ts); } else show_rt_info(pid, FALSE); if (argc - optind == 1) return EXIT_SUCCESS; } errno = 0; priority = strtos32_or_err(argv[optind], _("invalid priority argument")); #ifdef SCHED_RESET_ON_FORK /* sanity check */ if ((policy_flag & SCHED_RESET_ON_FORK) && !(policy == SCHED_FIFO || policy == SCHED_RR)) errx(EXIT_FAILURE, _("SCHED_RESET_ON_FORK flag is supported for " "SCHED_FIFO and SCHED_RR policies only")); #endif policy |= policy_flag; if (pid == -1) pid = 0; sp.sched_priority = priority; if (all_tasks) { pid_t tid; struct proc_tasks *ts = proc_open_tasks(pid); if (!ts) err(EXIT_FAILURE, _("cannot obtain the list of tasks")); while (!proc_next_tid(ts, &tid)) if (sched_setscheduler(tid, policy, &sp) == -1) err(EXIT_FAILURE, _("failed to set tid %d's policy"), tid); proc_close_tasks(ts); } else if (sched_setscheduler(pid, policy, &sp) == -1) err(EXIT_FAILURE, _("failed to set pid %d's policy"), pid); if (verbose) show_rt_info(pid, TRUE); if (!pid) { argv += optind + 1; execvp(argv[0], argv); perror("execvp"); err(EXIT_FAILURE, _("failed to execute %s"), argv[0]); } return EXIT_SUCCESS; }
int main (int argc, char *argv[]) { int errors, numsig, pid; char *ep, *arg, *p; int do_pid, do_kill, check_all; int *pids, *ip; progname = argv[0]; if ((p = strrchr(progname, '/')) != NULL) progname = p+1; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); numsig = SIGTERM; do_pid = (! strcmp (progname, "pid")); /* Yecch */ do_kill = 0; check_all = 0; /* loop through the arguments. actually, -a is the only option can be used with other options. `kill' is basically a one-option-at-most program. */ for (argc--, argv++; argc > 0; argc--, argv++) { arg = *argv; if (*arg != '-') { break; } if (! strcmp (arg, "--")) { argc--, argv++; break; } if (! strcmp (arg, "-v") || ! strcmp (arg, "-V") || ! strcmp (arg, "--version")) { printf(UTIL_LINUX_VERSION); return 0; } if (! strcmp (arg, "-a")) { check_all++; continue; } if (! strcmp (arg, "-l")) { if (argc < 2) { printsignals (stdout); return 0; } if (argc > 2) { return usage (1); } /* argc == 2, accept "kill -l $?" */ arg = argv[1]; if ((numsig = arg_to_signum (arg, 1)) < 0) { fprintf (stderr, _("%s: unknown signal %s\n"), progname, arg); return 1; } printsig (numsig); return 0; } if (! strcmp (arg, "-p")) { do_pid++; if (do_kill) return usage (1); continue; } if (! strcmp (arg, "-s")) { if (argc < 2) { return usage (1); } do_kill++; if (do_pid) return usage (1); argc--, argv++; arg = *argv; if ((numsig = arg_to_signum (arg, 0)) < 0) { nosig (arg); return 1; } continue; } if (! strcmp (arg, "-q")) { if (argc < 2) return usage (1); argc--, argv++; arg = *argv; #ifdef HAVE_SIGQUEUE sigdata.sival_int = strtos32_or_err(arg, _("invalid sigval argument")); use_sigval = 1; #endif continue; } /* `arg' begins with a dash but is not a known option. so it's probably something like -HUP, or -1/-n try to deal with it. -n could be signal n, or pid -n (i.e. process group n). In case of doubt POSIX tells us to assume a signal. If a signal has been parsed, assume it's a pid, break */ if (do_kill) break; arg++; if ((numsig = arg_to_signum (arg, 0)) < 0) { return usage (1); } do_kill++; if (do_pid) return usage (1); continue; } if (! *argv) { return usage (1); } if (do_pid) { numsig = -1; } /* we're done with the options. the rest of the arguments should be process ids and names. kill them. */ for (errors = 0; (arg = *argv) != NULL; argv++) { pid = strtol (arg, &ep, 10); if (! *ep) errors += kill_verbose (arg, pid, numsig); else { pids = get_pids (arg, check_all); if (! pids) { errors++; fprintf (stderr, _("%s: can't find process \"%s\"\n"), progname, arg); continue; } for (ip = pids; *ip >= 0; ip++) errors += kill_verbose (arg, *ip, numsig); free (pids); } } return (errors); }
int main(int argc, char **argv) { struct stat statbuf; struct swap_header_v1_2 *hdr; int c; unsigned long long maxpages; unsigned long long goodpages; unsigned long long sz; off_t offset; int force = 0; int version = 1; char *block_count = 0; char *opt_label = NULL; unsigned char *uuid = NULL; #ifdef HAVE_LIBUUID const char *opt_uuid = NULL; uuid_t uuid_dat; #endif static const struct option longopts[] = { { "check", no_argument, 0, 'c' }, { "force", no_argument, 0, 'f' }, { "pagesize", required_argument, 0, 'p' }, { "label", required_argument, 0, 'L' }, { "swapversion", required_argument, 0, 'v' }, { "uuid", required_argument, 0, 'U' }, { "version", no_argument, 0, 'V' }, { "help", no_argument, 0, 'h' }, { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while((c = getopt_long(argc, argv, "cfp:L:v:U:Vh", longopts, NULL)) != -1) { switch (c) { case 'c': check=1; break; case 'f': force=1; break; case 'p': user_pagesize = strtou32_or_err(optarg, _("parsing page size failed")); break; case 'L': opt_label = optarg; break; case 'v': version = strtos32_or_err(optarg, _("parsing version number failed")); break; case 'U': #ifdef HAVE_LIBUUID opt_uuid = optarg; #else warnx(_("warning: ignoring -U (UUIDs are unsupported by %s)"), program_invocation_short_name); #endif break; case 'V': printf(UTIL_LINUX_VERSION); exit(EXIT_SUCCESS); case 'h': usage(stdout); default: usage(stderr); } } if (optind < argc) device_name = argv[optind++]; if (optind < argc) block_count = argv[optind++]; if (optind != argc) { warnx(_("only one device argument is currently supported")); usage(stderr); } if (version != 1) errx(EXIT_FAILURE, _("swapspace version %d is not supported"), version); #ifdef HAVE_LIBUUID if(opt_uuid) { if (uuid_parse(opt_uuid, uuid_dat) != 0) errx(EXIT_FAILURE, _("error: parsing UUID failed")); } else uuid_generate(uuid_dat); uuid = uuid_dat; #endif init_signature_page(); /* get pagesize */ if (!device_name) { warnx(_("error: Nowhere to set up swap on?")); usage(stderr); } if (block_count) { /* this silly user specified the number of blocks explicitly */ uint64_t blks = strtou64_or_err(block_count, _("invalid block count argument")); PAGES = blks / (pagesize / 1024); } sz = get_size(device_name); if (!PAGES) PAGES = sz; else if (PAGES > sz && !force) { errx(EXIT_FAILURE, _("error: " "size %llu KiB is larger than device size %llu KiB"), PAGES*(pagesize/1024), sz*(pagesize/1024)); } if (PAGES < MIN_GOODPAGES) { warnx(_("error: swap area needs to be at least %ld KiB"), (long)(MIN_GOODPAGES * pagesize/1024)); usage(stderr); } #ifdef __linux__ if (get_linux_version() >= KERNEL_VERSION(2,3,4)) maxpages = UINT_MAX + 1ULL; else if (get_linux_version() >= KERNEL_VERSION(2,2,1)) maxpages = V1_MAX_PAGES; else #endif maxpages = V1_OLD_MAX_PAGES; if (PAGES > maxpages) { PAGES = maxpages; warnx(_("warning: truncating swap area to %llu KiB"), PAGES * pagesize / 1024); } if (is_mounted(device_name)) errx(EXIT_FAILURE, _("error: " "%s is mounted; will not make swapspace"), device_name); if (stat(device_name, &statbuf) < 0) { perror(device_name); exit(EXIT_FAILURE); } if (S_ISBLK(statbuf.st_mode)) DEV = open(device_name, O_RDWR | O_EXCL); else DEV = open(device_name, O_RDWR); if (DEV < 0) { perror(device_name); exit(EXIT_FAILURE); } if (!S_ISBLK(statbuf.st_mode)) check=0; else if (blkdev_is_misaligned(DEV)) warnx(_("warning: %s is misaligned"), device_name); if (check) check_blocks(); wipe_device(DEV, device_name, force); hdr = (struct swap_header_v1_2 *) signature_page; hdr->version = 1; hdr->last_page = PAGES - 1; hdr->nr_badpages = badpages; if (badpages > PAGES - MIN_GOODPAGES) errx(EXIT_FAILURE, _("Unable to set up swap-space: unreadable")); goodpages = PAGES - badpages - 1; printf(_("Setting up swapspace version 1, size = %llu KiB\n"), goodpages * pagesize / 1024); write_signature("SWAPSPACE2"); write_uuid_and_label(uuid, opt_label); offset = 1024; if (lseek(DEV, offset, SEEK_SET) != offset) errx(EXIT_FAILURE, _("unable to rewind swap-device")); if (write_all(DEV, (char *) signature_page + offset, pagesize - offset) == -1) err(EXIT_FAILURE, _("%s: unable to write signature page"), device_name); #ifdef HAVE_LIBSELINUX if (S_ISREG(statbuf.st_mode) && is_selinux_enabled() > 0) { security_context_t context_string; security_context_t oldcontext; context_t newcontext; if (fgetfilecon(DEV, &oldcontext) < 0) { if (errno != ENODATA) err(EXIT_FAILURE, _("%s: unable to obtain selinux file label"), device_name); if (matchpathcon(device_name, statbuf.st_mode, &oldcontext)) errx(EXIT_FAILURE, _("unable to matchpathcon()")); } if (!(newcontext = context_new(oldcontext))) errx(EXIT_FAILURE, _("unable to create new selinux context")); if (context_type_set(newcontext, SELINUX_SWAPFILE_TYPE)) errx(EXIT_FAILURE, _("couldn't compute selinux context")); context_string = context_str(newcontext); if (strcmp(context_string, oldcontext)!=0) { if (fsetfilecon(DEV, context_string)) err(EXIT_FAILURE, _("unable to relabel %s to %s"), device_name, context_string); } context_free(newcontext); freecon(oldcontext); } #endif /* * A subsequent swapon() will fail if the signature * is not actually on disk. (This is a kernel bug.) * The fsync() in close_fd() will take care of writing. */ if (close_fd(DEV) != 0) err(EXIT_FAILURE, _("write failed")); return EXIT_SUCCESS; }
int main(int argc, char **argv) { struct mkswap_control ctl = { .fd = -1 }; int c; uint64_t sz; int version = SWAP_VERSION; char *block_count = NULL, *strsz = NULL; #ifdef HAVE_LIBUUID const char *opt_uuid = NULL; uuid_t uuid_dat; #endif static const struct option longopts[] = { { "check", no_argument, 0, 'c' }, { "force", no_argument, 0, 'f' }, { "pagesize", required_argument, 0, 'p' }, { "label", required_argument, 0, 'L' }, { "swapversion", required_argument, 0, 'v' }, { "uuid", required_argument, 0, 'U' }, { "version", no_argument, 0, 'V' }, { "help", no_argument, 0, 'h' }, { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while((c = getopt_long(argc, argv, "cfp:L:v:U:Vh", longopts, NULL)) != -1) { switch (c) { case 'c': ctl.check = 1; break; case 'f': ctl.force = 1; break; case 'p': ctl.user_pagesize = strtou32_or_err(optarg, _("parsing page size failed")); break; case 'L': ctl.opt_label = optarg; break; case 'v': version = strtos32_or_err(optarg, _("parsing version number failed")); if (version != SWAP_VERSION) errx(EXIT_FAILURE, _("swapspace version %d is not supported"), version); break; case 'U': #ifdef HAVE_LIBUUID opt_uuid = optarg; #else warnx(_("warning: ignoring -U (UUIDs are unsupported by %s)"), program_invocation_short_name); #endif break; case 'V': printf(UTIL_LINUX_VERSION); exit(EXIT_SUCCESS); case 'h': usage(stdout); default: usage(stderr); } } if (optind < argc) ctl.devname = argv[optind++]; if (optind < argc) block_count = argv[optind++]; if (optind != argc) { warnx(_("only one device argument is currently supported")); usage(stderr); } #ifdef HAVE_LIBUUID if(opt_uuid) { if (uuid_parse(opt_uuid, uuid_dat) != 0) errx(EXIT_FAILURE, _("error: parsing UUID failed")); } else uuid_generate(uuid_dat); ctl.uuid = uuid_dat; #endif init_signature_page(&ctl); /* get pagesize and allocate signature page */ if (!ctl.devname) { warnx(_("error: Nowhere to set up swap on?")); usage(stderr); } if (block_count) { /* this silly user specified the number of blocks explicitly */ uint64_t blks = strtou64_or_err(block_count, _("invalid block count argument")); ctl.npages = blks / (ctl.pagesize / 1024); } sz = get_size(&ctl); if (!ctl.npages) ctl.npages = sz; else if (ctl.npages > sz && !ctl.force) errx(EXIT_FAILURE, _("error: " "size %llu KiB is larger than device size %ju KiB"), ctl.npages * (ctl.pagesize / 1024), sz * (ctl.pagesize / 1024)); if (ctl.npages < MIN_GOODPAGES) errx(EXIT_FAILURE, _("error: swap area needs to be at least %ld KiB"), (long)(MIN_GOODPAGES * ctl.pagesize / 1024)); if (ctl.npages > UINT32_MAX) { /* true when swap is bigger than 17.59 terabytes */ ctl.npages = UINT32_MAX; warnx(_("warning: truncating swap area to %llu KiB"), ctl.npages * ctl.pagesize / 1024); } if (is_mounted(ctl.devname)) errx(EXIT_FAILURE, _("error: " "%s is mounted; will not make swapspace"), ctl.devname); open_device(&ctl); if (ctl.check) check_blocks(&ctl); wipe_device(&ctl); assert(ctl.hdr); ctl.hdr->version = version; ctl.hdr->last_page = ctl.npages - 1; ctl.hdr->nr_badpages = ctl.nbadpages; if ((ctl.npages - MIN_GOODPAGES) < ctl.nbadpages) errx(EXIT_FAILURE, _("Unable to set up swap-space: unreadable")); sz = (ctl.npages - ctl.nbadpages - 1) * ctl.pagesize; strsz = size_to_human_string(SIZE_SUFFIX_SPACE | SIZE_SUFFIX_3LETTER, sz); printf(_("Setting up swapspace version %d, size = %s (%ju bytes)\n"), version, strsz, sz); free(strsz); set_signature(&ctl); set_uuid_and_label(&ctl); write_header_to_device(&ctl); deinit_signature_page(&ctl); #ifdef HAVE_LIBSELINUX if (S_ISREG(ctl.devstat.st_mode) && is_selinux_enabled() > 0) { security_context_t context_string; security_context_t oldcontext; context_t newcontext; if (fgetfilecon(ctl.fd, &oldcontext) < 0) { if (errno != ENODATA) err(EXIT_FAILURE, _("%s: unable to obtain selinux file label"), ctl.devname); if (matchpathcon(ctl.devname, ctl.devstat.st_mode, &oldcontext)) errx(EXIT_FAILURE, _("unable to matchpathcon()")); } if (!(newcontext = context_new(oldcontext))) errx(EXIT_FAILURE, _("unable to create new selinux context")); if (context_type_set(newcontext, SELINUX_SWAPFILE_TYPE)) errx(EXIT_FAILURE, _("couldn't compute selinux context")); context_string = context_str(newcontext); if (strcmp(context_string, oldcontext)!=0) { if (fsetfilecon(ctl.fd, context_string)) err(EXIT_FAILURE, _("unable to relabel %s to %s"), ctl.devname, context_string); } context_free(newcontext); freecon(oldcontext); } #endif /* * A subsequent swapon() will fail if the signature * is not actually on disk. (This is a kernel bug.) * The fsync() in close_fd() will take care of writing. */ if (close_fd(ctl.fd) != 0) err(EXIT_FAILURE, _("write failed")); return EXIT_SUCCESS; }
static int set_sched_one(struct chrt_ctl *ctl, pid_t pid) { struct sched_param sp = { .sched_priority = ctl->priority }; int policy = ctl->policy; # ifdef SCHED_RESET_ON_FORK if (ctl->reset_on_fork) policy |= SCHED_RESET_ON_FORK; # endif return sched_setscheduler(pid, policy, &sp); } #else /* !HAVE_SCHED_SETATTR */ static int set_sched_one(struct chrt_ctl *ctl, pid_t pid) { /* use main() to check if the setting makes sense */ struct sched_attr sa = { .size = sizeof(struct sched_attr), .sched_policy = ctl->policy, .sched_priority = ctl->priority, .sched_runtime = ctl->runtime, .sched_period = ctl->period, .sched_deadline = ctl->deadline }; # ifdef SCHED_RESET_ON_FORK if (ctl->reset_on_fork) sa.sched_flags |= SCHED_RESET_ON_FORK; # endif return sched_setattr(pid, &sa, 0); } #endif /* HAVE_SCHED_SETATTR */ static void set_sched(struct chrt_ctl *ctl) { if (ctl->all_tasks) { pid_t tid; struct proc_tasks *ts = proc_open_tasks(ctl->pid); if (!ts) err(EXIT_FAILURE, _("cannot obtain the list of tasks")); while (!proc_next_tid(ts, &tid)) if (set_sched_one(ctl, tid) == -1) err(EXIT_FAILURE, _("failed to set tid %d's policy"), tid); proc_close_tasks(ts); } else if (set_sched_one(ctl, ctl->pid) == -1) err(EXIT_FAILURE, _("failed to set pid %d's policy"), ctl->pid); ctl->altered = 1; } int main(int argc, char **argv) { struct chrt_ctl _ctl = { .pid = -1 }, *ctl = &_ctl; int c; static const struct option longopts[] = { { "all-tasks", no_argument, NULL, 'a' }, { "batch", no_argument, NULL, 'b' }, { "deadline", no_argument, NULL, 'd' }, { "fifo", no_argument, NULL, 'f' }, { "idle", no_argument, NULL, 'i' }, { "pid", no_argument, NULL, 'p' }, { "help", no_argument, NULL, 'h' }, { "max", no_argument, NULL, 'm' }, { "other", no_argument, NULL, 'o' }, { "rr", no_argument, NULL, 'r' }, { "sched-runtime", required_argument, NULL, 'T' }, { "sched-period", required_argument, NULL, 'P' }, { "sched-deadline", required_argument, NULL, 'D' }, { "reset-on-fork", no_argument, NULL, 'R' }, { "verbose", no_argument, NULL, 'v' }, { "version", no_argument, NULL, 'V' }, { NULL, no_argument, NULL, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while((c = getopt_long(argc, argv, "+abdDfiphmoPTrRvV", longopts, NULL)) != -1) { int ret = EXIT_FAILURE; switch (c) { case 'a': ctl->all_tasks = 1; break; case 'b': #ifdef SCHED_BATCH ctl->policy = SCHED_BATCH; #endif break; case 'd': #ifdef SCHED_DEADLINE ctl->policy = SCHED_DEADLINE; #endif break; case 'f': ctl->policy = SCHED_FIFO; break; case 'R': ctl->reset_on_fork = 1; break; case 'i': #ifdef SCHED_IDLE ctl->policy = SCHED_IDLE; #endif break; case 'm': show_min_max(); return EXIT_SUCCESS; case 'o': ctl->policy = SCHED_OTHER; break; case 'p': errno = 0; ctl->pid = strtos32_or_err(argv[argc - 1], _("invalid PID argument")); break; case 'r': ctl->policy = SCHED_RR; break; case 'v': ctl->verbose = 1; break; case 'T': ctl->runtime = strtou64_or_err(optarg, _("invalid runtime argument")); break; case 'P': ctl->period = strtou64_or_err(optarg, _("invalid period argument")); break; case 'D': ctl->deadline = strtou64_or_err(optarg, _("invalid deadline argument")); break; case 'V': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case 'h': ret = EXIT_SUCCESS; /* fallthrough */ default: show_usage(ret); } } if (((ctl->pid > -1) && argc - optind < 1) || ((ctl->pid == -1) && argc - optind < 2)) show_usage(EXIT_FAILURE); if ((ctl->pid > -1) && (ctl->verbose || argc - optind == 1)) { show_sched_info(ctl); if (argc - optind == 1) return EXIT_SUCCESS; } errno = 0; ctl->priority = strtos32_or_err(argv[optind], _("invalid priority argument")); #ifdef SCHED_RESET_ON_FORK if (ctl->reset_on_fork && ctl->policy != SCHED_FIFO && ctl->policy != SCHED_RR) errx(EXIT_FAILURE, _("--reset-on-fork option is supported for " "SCHED_FIFO and SCHED_RR policies only")); #endif #ifdef SCHED_DEADLINE if ((ctl->runtime || ctl->deadline || ctl->period) && ctl->policy != SCHED_DEADLINE) errx(EXIT_FAILURE, _("--sched-{runtime,deadline,period} options " "are supported for SCHED_DEADLINE only")); if (ctl->policy == SCHED_DEADLINE) { /* The basic rule is runtime <= deadline <= period, so we can * make deadline and runtime optional on command line. Note we * don't check any values or set any defaults, it's kernel * responsibility. */ if (ctl->deadline == 0) ctl->deadline = ctl->period; if (ctl->runtime == 0) ctl->runtime = ctl->deadline; } #else if (ctl->runtime || ctl->deadline || ctl->period) errx(EXIT_FAILURE, _("SCHED_DEADLINE is unsupported")); #endif if (ctl->pid == -1) ctl->pid = 0; set_sched(ctl); if (ctl->verbose) show_sched_info(ctl); if (!ctl->pid) { argv += optind + 1; execvp(argv[0], argv); err(EXIT_FAILURE, _("failed to execute %s"), argv[0]); } return EXIT_SUCCESS; }
int main(int argc, char **argv) { pid_t pid = 0; int c, adj = 0, has_adj = 0; static const struct option longopts[] = { { "adjust", required_argument, NULL, 'n' }, { "pid", required_argument, NULL, 'p' }, { "help", no_argument, NULL, 'h' }, { "version", no_argument, NULL, 'V' }, { NULL, 0, NULL, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((c = getopt_long(argc, argv, "hn:p:V", longopts, NULL)) != -1) { switch (c) { case 'p': pid = strtos32_or_err(optarg, _("invalid PID argument")); break; case 'n': adj = strtos32_or_err(optarg, _("invalid adjust argument")); has_adj = 1; break; case 'V': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case 'h': usage(); default: errtryhelp(EXIT_FAILURE); } } if (optind < argc && pid) { warnx(_("invalid argument: %s"), argv[optind]); errtryhelp(EXIT_FAILURE); } if (!pid && argc - optind < 1) { warnx(_("no PID or COMMAND specified")); errtryhelp(EXIT_FAILURE); } if (optind < argc && !has_adj) { warnx(_("no OOM score adjust value specified")); errtryhelp(EXIT_FAILURE); } /* Show */ if (!has_adj) { printf(_("pid %d's current OOM score: %d\n"), pid, get_score(pid)); printf(_("pid %d's current OOM score adjust value: %d\n"), pid, get_score_adj(pid)); /* Change */ } else if (pid) { int old = get_score_adj(pid); if (set_score_adj(pid, adj)) err(EXIT_FAILURE, _("failed to set score adjust value")); printf(_("pid %d's OOM score adjust value changed from %d to %d\n"), pid, old, adj); /* Start new process */ } else { if (set_score_adj(getpid(), adj)) err(EXIT_FAILURE, _("failed to set score adjust value")); argv += optind; execvp(argv[0], argv); errexec(argv[0]); } return EXIT_SUCCESS; }
int main(int argc, char **argv) { int tty_fd; struct termios ts; int speed = 0, bits = '-', parity = '-', stop = '-'; int set_iflag = 0, clr_iflag = 0; int ldisc; int optc; char *dev; static const struct option opttbl[] = { {"speed", required_argument, NULL, 's'}, {"sevenbits", no_argument, NULL, '7'}, {"eightbits", no_argument, NULL, '8'}, {"noparity", no_argument, NULL, 'n'}, {"evenparity", no_argument, NULL, 'e'}, {"oddparity", no_argument, NULL, 'o'}, {"onestopbit", no_argument, NULL, '1'}, {"twostopbits", no_argument, NULL, '2'}, {"iflag", required_argument, NULL, 'i'}, {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'V'}, {"debug", no_argument, NULL, 'd'}, {NULL, 0, NULL, 0} }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); /* parse options */ progname = program_invocation_short_name; if (argc == 0) usage(EXIT_SUCCESS); while ((optc = getopt_long(argc, argv, "dhV78neo12s:i:", opttbl, NULL)) >= 0) { switch (optc) { case 'd': debug = 1; break; case '1': case '2': stop = optc; break; case '7': case '8': bits = optc; break; case 'n': case 'e': case 'o': parity = optc; break; case 's': speed = strtos32_or_err(optarg, _("invalid speed argument")); break; case 'i': parse_iflag(optarg, &set_iflag, &clr_iflag); break; case 'V': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case 'h': usage(EXIT_SUCCESS); default: warnx(_("invalid option")); usage(EXIT_FAILURE); } } if (argc - optind != 2) usage(EXIT_FAILURE); /* parse line discipline specification */ ldisc = lookup_table(ld_discs, argv[optind]); if (ldisc < 0) ldisc = strtos32_or_err(argv[optind], _("invalid line discipline argument")); /* open device */ dev = argv[optind + 1]; if ((tty_fd = open(dev, O_RDWR | O_NOCTTY)) < 0) err(EXIT_FAILURE, _("cannot open %s"), dev); if (!isatty(tty_fd)) errx(EXIT_FAILURE, _("%s is not a serial line"), dev); dbg("opened %s", dev); /* set line speed and format */ if (tcgetattr(tty_fd, &ts) < 0) err(EXIT_FAILURE, _("cannot get terminal attributes for %s"), dev); cfmakeraw(&ts); if (speed && my_cfsetspeed(&ts, speed) < 0) errx(EXIT_FAILURE, _("speed %d unsupported"), speed); switch (stop) { case '1': ts.c_cflag &= ~CSTOPB; break; case '2': ts.c_cflag |= CSTOPB; break; case '-': break; default: abort(); } switch (bits) { case '7': ts.c_cflag = (ts.c_cflag & ~CSIZE) | CS7; break; case '8': ts.c_cflag = (ts.c_cflag & ~CSIZE) | CS8; break; case '-': break; default: abort(); } switch (parity) { case 'n': ts.c_cflag &= ~(PARENB | PARODD); break; case 'e': ts.c_cflag |= PARENB; ts.c_cflag &= ~PARODD; break; case 'o': ts.c_cflag |= (PARENB | PARODD); break; case '-': break; default: abort(); } ts.c_cflag |= CREAD; /* just to be on the safe side */ ts.c_iflag |= set_iflag; ts.c_iflag &= ~clr_iflag; if (tcsetattr(tty_fd, TCSAFLUSH, &ts) < 0) err(EXIT_FAILURE, _("cannot set terminal attributes for %s"), dev); dbg("set to raw %d %c%c%c: cflag=0x%x", speed, bits, parity, stop, ts.c_cflag); /* Attach the line discpline. */ if (ioctl(tty_fd, TIOCSETD, &ldisc) < 0) err(EXIT_FAILURE, _("cannot set line discipline")); dbg("line discipline set to %d", ldisc); /* Go into background if not in debug mode. */ if (!debug && daemon(0, 0) < 0) err(EXIT_FAILURE, _("cannot daemonize")); /* Sleep to keep the line discipline active. */ pause(); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { int data = 4, set = 0, ioclass = IOPRIO_CLASS_BE, c; pid_t pid = 0; static const struct option longopts[] = { { "classdata", required_argument, NULL, 'n' }, { "class", required_argument, NULL, 'c' }, { "help", no_argument, NULL, 'h' }, { "ignore", no_argument, NULL, 't' }, { "pid", required_argument, NULL, 'p' }, { "version", no_argument, NULL, 'V' }, { NULL, 0, NULL, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((c = getopt_long(argc, argv, "+n:c:p:tVh", longopts, NULL)) != EOF) switch (c) { case 'n': data = strtos32_or_err(optarg, _("invalid class data argument")); set |= 1; break; case 'c': if (isdigit(*optarg)) ioclass = strtos32_or_err(optarg, _("invalid class argument")); else { ioclass = parse_ioclass(optarg); if (ioclass < 0) errx(EXIT_FAILURE, _("unknown scheduling class: '%s'"), optarg); } set |= 2; break; case 'p': pid = strtos32_or_err(optarg, _("invalid PID argument")); break; case 't': tolerant = 1; break; case 'V': printf(_("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING); return EXIT_SUCCESS; case 'h': usage(stdout); default: usage(stderr); } switch (ioclass) { case IOPRIO_CLASS_NONE: if ((set & 1) && !tolerant) warnx(_("ignoring given class data for none class")); data = 0; break; case IOPRIO_CLASS_RT: case IOPRIO_CLASS_BE: break; case IOPRIO_CLASS_IDLE: if ((set & 1) && !tolerant) warnx(_("ignoring given class data for idle class")); data = 7; break; default: if (!tolerant) warnx(_("unknown prio class %d"), ioclass); break; } if (!set && !pid && optind == argc) /* * ionice without options, print the current ioprio */ ioprio_print(0); else if (!set && pid) { /* * ionice -p PID [PID ...] */ ioprio_print(pid); for(; argv[optind]; ++optind) { pid = strtos32_or_err(argv[optind], _("invalid PID argument")); ioprio_print(pid); } } else if (set && pid) { /* * ionice -c CLASS -p PID [PID ...] */ ioprio_setpid(pid, ioclass, data); for(; argv[optind]; ++optind) { pid = strtos32_or_err(argv[optind], _("invalid PID argument")); ioprio_setpid(pid, ioclass, data); } } else if (argv[optind]) { /* * ionice [-c CLASS] COMMAND */ ioprio_setpid(0, ioclass, data); execvp(argv[optind], &argv[optind]); err(EXIT_FAILURE, _("failed to execute %s"), argv[optind]); } else usage(stderr); return EXIT_SUCCESS; }
int main(int argc, char **argv) { cpu_set_t *new_set; pid_t pid = 0; int c, all_tasks = 0; int ncpus; size_t new_setsize, nbits; struct taskset ts; static const struct option longopts[] = { { "all-tasks", 0, NULL, 'a' }, { "pid", 0, NULL, 'p' }, { "cpu-list", 0, NULL, 'c' }, { "help", 0, NULL, 'h' }, { "version", 0, NULL, 'V' }, { NULL, 0, NULL, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); memset(&ts, 0, sizeof(ts)); while ((c = getopt_long(argc, argv, "+apchV", longopts, NULL)) != -1) { switch (c) { case 'a': all_tasks = 1; break; case 'p': pid = strtos32_or_err(argv[argc - 1], _("invalid PID argument")); break; case 'c': ts.use_list = 1; break; case 'V': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case 'h': usage(stdout); break; default: errtryhelp(EXIT_FAILURE); } } if ((!pid && argc - optind < 2) || (pid && (argc - optind < 1 || argc - optind > 2))) usage(stderr); ncpus = get_max_number_of_cpus(); if (ncpus <= 0) errx(EXIT_FAILURE, _("cannot determine NR_CPUS; aborting")); /* * the ts->set is always used for the sched_getaffinity call * On the sched_getaffinity the kernel demands a user mask of * at least the size of its own cpumask_t. */ ts.set = cpuset_alloc(ncpus, &ts.setsize, &nbits); if (!ts.set) err(EXIT_FAILURE, _("cpuset_alloc failed")); /* buffer for conversion from mask to string */ ts.buflen = 7 * nbits; ts.buf = xmalloc(ts.buflen); /* * new_set is always used for the sched_setaffinity call * On the sched_setaffinity the kernel will zero-fill its * cpumask_t if the user's mask is shorter. */ new_set = cpuset_alloc(ncpus, &new_setsize, NULL); if (!new_set) err(EXIT_FAILURE, _("cpuset_alloc failed")); if (argc - optind == 1) ts.get_only = 1; else if (ts.use_list) { if (cpulist_parse(argv[optind], new_set, new_setsize, 0)) errx(EXIT_FAILURE, _("failed to parse CPU list: %s"), argv[optind]); } else if (cpumask_parse(argv[optind], new_set, new_setsize)) { errx(EXIT_FAILURE, _("failed to parse CPU mask: %s"), argv[optind]); } if (all_tasks && pid) { struct proc_tasks *tasks = proc_open_tasks(pid); while (!proc_next_tid(tasks, &ts.pid)) do_taskset(&ts, new_setsize, new_set); proc_close_tasks(tasks); } else { ts.pid = pid; do_taskset(&ts, new_setsize, new_set); } free(ts.buf); cpuset_free(ts.set); cpuset_free(new_set); if (!pid) { argv += optind + 1; execvp(argv[0], argv); err(EXIT_FAILURE, _("failed to execute %s"), argv[0]); } return EXIT_SUCCESS; }