static gid_t get_group_gid(const char *optarg) { struct group *gr; gid_t gid; if ((gr = getgrnam(optarg))) return gr->gr_gid; gid = strtou32_or_err(optarg, _("invalid group argument")); if (!getgrgid(gid)) errx(EXIT_FAILURE, _("%s: unknown gid"), optarg); return gid; }
int main(int argc, char **argv) { int c, fd, partno; const char *wholedisk; uint64_t start; static const struct option longopts[] = { {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {NULL, no_argument, 0, '0'}, }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((c = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1) switch (c) { case 'V': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case 'h': usage(stdout); default: errtryhelp(EXIT_FAILURE); } if (argc != 4) usage(stderr); wholedisk = argv[1]; partno = strtou32_or_err(argv[2], _("invalid partition number argument")); if ((fd = open(wholedisk, O_RDONLY)) < 0) err(EXIT_FAILURE, _("cannot open %s"), wholedisk); if (get_partition_start(fd, partno, &start)) err(EXIT_FAILURE, _("%s: failed to get start of the partition number %s"), wholedisk, argv[2]); if (partx_resize_partition(fd, partno, start, strtou64_or_err(argv[3], _("invalid length argument")))) err(EXIT_FAILURE, _("failed to resize partition")); if (close_fd(fd) != 0) err(EXIT_FAILURE, _("write failed")); return 0; }
int main(int argc, char **argv) { int c, fd; static const struct option longopts[] = { {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {NULL, no_argument, 0, '0'}, }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); while ((c = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1) switch (c) { case 'V': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case 'h': usage(stdout); default: usage(stderr); } if (argc != 5) usage(stderr); if ((fd = open(argv[1], O_RDONLY)) < 0) err(EXIT_FAILURE, _("%s: open failed"), argv[1]); if (partx_add_partition(fd, strtou32_or_err(argv[2], _("invalid partition number argument")), strtou64_or_err(argv[3], _("invalid start argument")), strtou64_or_err(argv[4], _("invalid length argument")))) err(EXIT_FAILURE, _("failed to add partition")); return EXIT_SUCCESS; }
int main(int argc, char **argv) { int ch; struct iovec iov; struct utmpx *utmpptr; char *p; char line[sizeof(utmpptr->ut_line) + 1]; int print_banner = TRUE; struct group_workspace *group_buf = NULL; char *mbuf, *fname = NULL; size_t mbufsize; unsigned timeout = WRITE_TIME_OUT; char **mvec = NULL; int mvecsz = 0; static const struct option longopts[] = { { "nobanner", no_argument, NULL, 'n' }, { "timeout", required_argument, NULL, 't' }, { "group", required_argument, NULL, 'g' }, { "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 ((ch = getopt_long(argc, argv, "nt:g:Vh", longopts, NULL)) != -1) { switch (ch) { case 'n': if (geteuid() == 0) print_banner = FALSE; else warnx(_("--nobanner is available only for root")); break; case 't': timeout = strtou32_or_err(optarg, _("invalid timeout argument")); if (timeout < 1) errx(EXIT_FAILURE, _("invalid timeout argument: %s"), optarg); break; case 'g': group_buf = init_group_workspace(optarg); break; case 'V': printf(UTIL_LINUX_VERSION); exit(EXIT_SUCCESS); case 'h': usage(); default: errtryhelp(EXIT_FAILURE); } } argc -= optind; argv += optind; if (argc == 1 && access(argv[0], F_OK) == 0) fname = argv[0]; else if (argc >= 1) { mvec = argv; mvecsz = argc; } mbuf = makemsg(fname, mvec, mvecsz, &mbufsize, print_banner); iov.iov_base = mbuf; iov.iov_len = mbufsize; while((utmpptr = getutxent())) { if (!utmpptr->ut_user[0]) continue; #ifdef USER_PROCESS if (utmpptr->ut_type != USER_PROCESS) continue; #endif /* Joey Hess reports that use-sessreg in /etc/X11/wdm/ produces ut_line entries like :0, and a write to /dev/:0 fails. */ if (utmpptr->ut_line[0] == ':') continue; if (group_buf && !is_gr_member(utmpptr->ut_user, group_buf)) continue; mem2strcpy(line, utmpptr->ut_line, sizeof(utmpptr->ut_line), sizeof(line)); if ((p = ttymsg(&iov, 1, line, timeout)) != NULL) warnx("%s", p); } endutxent(); free(mbuf); free_group_workspace(group_buf); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { int c; /* for getopt */ int start = 0; size_t length = 0; static const struct option longopts[] = { {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"blocksize", required_argument, 0, 'b'}, {"extract", optional_argument, 0, 'x'}, {NULL, no_argument, 0, '0'}, }; setlocale(LC_MESSAGES, ""); setlocale(LC_CTYPE, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); /* command line options */ while ((c = getopt_long(argc, argv, "ayvVhb:", longopts, NULL)) != EOF) switch (c) { case 'a': /* ignore */ case 'y': break; case 'h': usage(FSCK_EX_OK); break; case 'V': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case 'x': opt_extract = 1; if(optarg) extract_dir = optarg; break; case 'v': opt_verbose++; break; case 'b': blksize = strtou32_or_err(optarg, _("invalid blocksize argument")); break; default: usage(FSCK_EX_USAGE); } if ((argc - optind) != 1) usage(FSCK_EX_USAGE); filename = argv[optind]; test_super(&start, &length); test_crc(start); if(opt_extract) { if (blksize == 0) blksize = getpagesize(); outbuffer = xmalloc(blksize * 2); test_fs(start); } if (opt_verbose) printf(_("%s: OK\n"), filename); exit(FSCK_EX_OK); }
int main(int argc, char **argv) { char *tty = NULL; char *p; struct passwd *pwd; int c, fd = -1; int opt_e = 0; pid_t pid, pgrp, ppgrp, ttypgrp; struct sigaction saved_sighup; static const struct option longopts[] = { { "login-shell", 0, 0, 'p' }, { "timeout", 1, 0, 't' }, { "force", 0, 0, 'e' }, { "help", 0, 0, 'h' }, { "version", 0, 0, 'V' }, { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); /* * See if we have a timeout flag. */ while ((c = getopt_long(argc, argv, "ehpt:V", longopts, NULL)) != -1) { switch(c) { case 't': timeout = strtou32_or_err(optarg, _("invalid timeout argument")); break; case 'p': profile = 1; break; case 'e': opt_e = 1; break; case 'V': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case 'h': usage(stdout); return EXIT_SUCCESS; default: usage(stderr); /* Do not exit! */ break; } } if (geteuid() != 0) errx(EXIT_FAILURE, _("only root can run this program.")); /* * See if we need to open an other tty device. */ mask_signal(SIGQUIT, SIG_IGN, &saved_sigquit); mask_signal(SIGTSTP, SIG_IGN, &saved_sigtstp); mask_signal(SIGINT, SIG_IGN, &saved_sigint); if (optind < argc) tty = argv[optind]; if (tty || (tty = getenv("CONSOLE"))) { if ((fd = open(tty, O_RDWR)) < 0) { warn(_("cannot open %s"), tty); fd = dup(0); } if (!isatty(fd)) { warn(_("%s: not a tty"), tty); close(fd); } else { /* * Only go through this trouble if the new tty doesn't * fall in this process group. */ pid = getpid(); pgrp = getpgid(0); ppgrp = getpgid(getppid()); ttypgrp = tcgetpgrp(fd); if (pgrp != ttypgrp && ppgrp != ttypgrp) { if (pid != getsid(0)) { if (pid == getpgid(0)) setpgid(0, getpgid(getppid())); setsid(); } sigaction(SIGHUP, NULL, &saved_sighup); if (ttypgrp > 0) ioctl(0, TIOCNOTTY, (char *)1); sigaction(SIGHUP, &saved_sighup, NULL); close(0); close(1); close(2); if (fd > 2) close(fd); if ((fd = open(tty, O_RDWR|O_NOCTTY)) < 0) warn(_("cannot open %s"), tty); else { ioctl(0, TIOCSCTTY, (char *)1); tcsetpgrp(fd, ppgrp); dup2(fd, 0); dup2(fd, 1); dup2(fd, 2); if (fd > 2) close(fd); } } else if (fd > 2) close(fd); } } else if (getpid() == 1) { /* We are init. We hence need to set a session anyway */ setsid(); if (ioctl(0, TIOCSCTTY, (char *)1)) warn(_("TIOCSCTTY: ioctl failed")); } fixtty(); /* * Get the root password. */ if ((pwd = getrootpwent(opt_e)) == NULL) { warnx(_("cannot open password database.")); sleep(2); } /* * Ask for the password. */ while (pwd) { if ((p = getpasswd(pwd->pw_passwd)) == NULL) break; if (pwd->pw_passwd[0] == 0 || strcmp(crypt(p, pwd->pw_passwd), pwd->pw_passwd) == 0) sushell(pwd); mask_signal(SIGQUIT, SIG_IGN, &saved_sigquit); mask_signal(SIGTSTP, SIG_IGN, &saved_sigtstp); mask_signal(SIGINT, SIG_IGN, &saved_sigint); fprintf(stderr, _("Login incorrect\n\n")); } /* * User pressed Control-D. */ return EXIT_SUCCESS; }
int main(int argc, char **argv) { const char *socket_path = UUIDD_SOCKET_PATH; const char *pidfile_path = NULL; const char *pidfile_path_param = NULL; const char *err_context; char buf[1024], *cp; char str[UUID_STR_LEN]; uuid_t uu; int i, c, ret; int do_type = 0, do_kill = 0, num = 0; int no_pid = 0; int s_flag = 0; struct uuidd_cxt_t uuidd_cxt = { .timeout = 0 }; static const struct option longopts[] = { {"pid", required_argument, NULL, 'p'}, {"socket", required_argument, NULL, 's'}, {"timeout", required_argument, NULL, 'T'}, {"kill", no_argument, NULL, 'k'}, {"random", no_argument, NULL, 'r'}, {"time", no_argument, NULL, 't'}, {"uuids", required_argument, NULL, 'n'}, {"no-pid", no_argument, NULL, 'P'}, {"no-fork", no_argument, NULL, 'F'}, {"socket-activation", no_argument, NULL, 'S'}, {"debug", no_argument, NULL, 'd'}, {"quiet", no_argument, NULL, 'q'}, {"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 ((c = getopt_long(argc, argv, "p:s:T:krtn:PFSdqVh", longopts, NULL)) != -1) { switch (c) { case 'd': uuidd_cxt.debug = 1; break; case 'k': do_kill++; break; case 'n': num = strtou32_or_err(optarg, _("failed to parse --uuids")); break; case 'p': pidfile_path_param = optarg; break; case 'P': no_pid = 1; break; case 'F': uuidd_cxt.no_fork = 1; break; case 'S': #ifdef USE_SOCKET_ACTIVATION uuidd_cxt.no_sock = 1; uuidd_cxt.no_fork = 1; no_pid = 1; #else errx(EXIT_FAILURE, _("uuidd has been built without " "support for socket activation")); #endif break; case 'q': uuidd_cxt.quiet = 1; break; case 'r': do_type = UUIDD_OP_RANDOM_UUID; break; case 's': socket_path = optarg; s_flag = 1; break; case 't': do_type = UUIDD_OP_TIME_UUID; break; case 'T': uuidd_cxt.timeout = strtou32_or_err(optarg, _("failed to parse --timeout")); break; case 'V': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case 'h': usage(stdout); default: usage(stderr); } } if (no_pid && pidfile_path_param && !uuidd_cxt.quiet) warnx(_("Both --pid and --no-pid specified. Ignoring --no-pid.")); if (!no_pid && !pidfile_path_param) pidfile_path = UUIDD_PIDFILE_PATH; else if (pidfile_path_param) pidfile_path = pidfile_path_param; /* custom socket path and socket-activation make no sense */ if (s_flag && uuidd_cxt.no_sock && !uuidd_cxt.quiet) warnx(_("Both --socket-activation and --socket specified. " "Ignoring --socket.")); if (num && do_type) { ret = call_daemon(socket_path, do_type + 2, buf, sizeof(buf), &num, &err_context); if (ret < 0) err(EXIT_FAILURE, _("error calling uuidd daemon (%s)"), err_context); if (do_type == UUIDD_OP_TIME_UUID) { if (ret != sizeof(uu) + sizeof(num)) unexpected_size(ret); uuid_unparse((unsigned char *) buf, str); printf(P_("%s and %d subsequent UUID\n", "%s and %d subsequent UUIDs\n", num - 1), str, num - 1); } else { printf(_("List of UUIDs:\n")); cp = buf + 4; if (ret != (int) (sizeof(num) + num * sizeof(uu))) unexpected_size(ret); for (i = 0; i < num; i++, cp += UUID_LEN) { uuid_unparse((unsigned char *) cp, str); printf("\t%s\n", str); } } return EXIT_SUCCESS; } if (do_type) { ret = call_daemon(socket_path, do_type, (char *) &uu, sizeof(uu), 0, &err_context); if (ret < 0) err(EXIT_FAILURE, _("error calling uuidd daemon (%s)"), err_context); if (ret != sizeof(uu)) unexpected_size(ret); uuid_unparse(uu, str); printf("%s\n", str); return EXIT_SUCCESS; } if (do_kill) { ret = call_daemon(socket_path, UUIDD_OP_GETPID, buf, sizeof(buf), 0, NULL); if ((ret > 0) && ((do_kill = atoi((char *) buf)) > 0)) { ret = kill(do_kill, SIGTERM); if (ret < 0) { if (!uuidd_cxt.quiet) warn(_("couldn't kill uuidd running " "at pid %d"), do_kill); return EXIT_FAILURE; } if (!uuidd_cxt.quiet) printf(_("Killed uuidd running at pid %d.\n"), do_kill); } return EXIT_SUCCESS; } server_loop(socket_path, pidfile_path, &uuidd_cxt); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { struct libscols_table *tb; struct libscols_line *ln; /* any line */ struct libscols_line *g1, *g2, *g3; /* groups */ struct libscols_line *p1; /* parents */ int c; static const struct option longopts[] = { { "maxout", 0, NULL, 'm' }, { "width", 1, NULL, 'w' }, { "help", 1, NULL, 'h' }, { NULL, 0, NULL, 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, "hmw:", longopts, NULL)) != -1) { switch(c) { case 'h': printf("%s [--help | --maxout | --width <num>]\n", program_invocation_short_name); break; case 'm': scols_table_enable_maxout(tb, TRUE); 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; } } scols_table_enable_colors(tb, isatty(STDOUT_FILENO)); setup_columns(tb); add_line(tb, NULL, "Alone", "bla bla bla"); p1 = add_line(tb, NULL, "A", "bla bla bla"); add_line(tb, p1, "A:B", "bla bla bla"); add_line(tb, p1, "A:C", "bla bla bla"); g1 = add_line(tb, NULL, "B", "bla bla bla"); g2 = add_line(tb, NULL, "C", "bla bla bla"); ln = add_line(tb, NULL, "D", "bla bla bla"); scols_table_group_lines(tb, g2, ln, 0); ln = add_line(tb, NULL, "G2:A", "alb alb alb"); scols_line_link_group(ln, g2, 0); ln = add_line(tb, NULL, "E", "bla bla bla"); scols_table_group_lines(tb, g1, ln, 0); ln = add_line(tb, NULL, "G1:A", "alb alb alb"); scols_line_link_group(ln, g1, 0); add_line(tb, NULL, "G", "bla bla bla"); g3 = ln = add_line(tb, NULL, "G1:B", "alb alb alb"); scols_line_link_group(ln, g1, 0); ln = add_line(tb, NULL, "F", "bla bla bla"); scols_table_group_lines(tb, g3, ln, 0); ln = add_line(tb, NULL, "G3:A", "alb alb alb"); scols_line_link_group(ln, g3, 0); add_line(tb, NULL, "foo", "bla bla bla"); add_line(tb, NULL, "bar", "bla bla bla"); scols_print_table(tb); scols_unref_table(tb); 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) { 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; }
int main(int argc, char **argv) { struct loopdev_cxt lc; int act = 0, flags = 0, passfd = -1, c; char *file = NULL, *encryption = 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 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); loopcxt_init(&lc, 0); loopcxt_enable_debug(&lc, getenv("LOOPDEV_DEBUG") ? TRUE : FALSE); while ((c = getopt_long(argc, argv, "ac:d:De:E:fhj:o:p:PrvV", longopts, NULL)) != -1) { if (act && strchr("acdDfj", c)) errx(EXIT_FAILURE, _("the options %s are mutually exclusive"), "--{all,associated,set-capacity,detach,detach-all,find}"); switch (c) { case 'a': act = A_SHOW; break; case 'c': act = A_SET_CAPACITY; loopcxt_set_device(&lc, optarg); break; case 'r': lo_flags |= LO_FLAGS_READ_ONLY; break; case 'd': act = A_DELETE; loopcxt_set_device(&lc, optarg); break; case 'D': act = A_DELETE_ALL; break; case 'E': case 'e': encryption = optarg; 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': passfd = strtou32_or_err(optarg, _("invalid passphrase file descriptor")); 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; loopcxt_set_device(&lc, argv[optind++]); } if (!act) { /* * losetup <loopdev> <backing_file> */ act = A_CREATE; if (optind >= argc) errx(EXIT_FAILURE, _("no loop device specified")); loopcxt_set_device(&lc, argv[optind++]); if (optind >= argc) errx(EXIT_FAILURE, _("no file specified")); file = argv[optind++]; } if (act != A_CREATE && (encryption || sizelimit || passfd != -1 || lo_flags || showdev)) errx(EXIT_FAILURE, _("the options %s are allowed to loop device setup only"), "--{encryption,sizelimit,pass-fd,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: { char *pass = NULL; int hasdev = loopcxt_has_device(&lc); if (encryption) { #ifdef MCL_FUTURE if(mlockall(MCL_CURRENT | MCL_FUTURE)) err(EXIT_FAILURE, _("couldn't lock into memory")); #endif pass = xgetpass(passfd, _("Password: "******"not found unused device")); break; } if (encryption && pass) loopcxt_set_encryption(&lc, encryption, pass); 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); free(pass); if (showdev && res == 0) printf("%s\n", loopcxt_get_device(&lc)); break; } case A_DELETE: res = delete_loop(&lc); while (optind < argc) { loopcxt_set_device(&lc, argv[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; }
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; }
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; }
int main(int argc, char **argv) { int ch; struct iovec iov; struct utmp *utmpptr; char *p; char line[sizeof(utmpptr->ut_line) + 1]; int print_banner = TRUE; char *mbuf; size_t mbufsize; unsigned timeout = WRITE_TIME_OUT; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); static const struct option longopts[] = { { "nobanner", no_argument, 0, 'n' }, { "timeout", required_argument, 0, 't' }, { "version", no_argument, 0, 'V' }, { "help", no_argument, 0, 'h' }, { NULL, 0, 0, 0 } }; while ((ch = getopt_long(argc, argv, "nt:Vh", longopts, NULL)) != -1) { switch (ch) { case 'n': if (geteuid() == 0) print_banner = FALSE; else warnx(_("--nobanner is available only for root")); break; case 't': timeout = strtou32_or_err(optarg, _("invalid timeout argument")); if (timeout < 1) errx(EXIT_FAILURE, _("invalid timeout argument: %s"), optarg); break; case 'V': printf(_("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING); exit(EXIT_SUCCESS); case 'h': usage(stdout); default: usage(stderr); } } argc -= optind; argv += optind; if (argc > 1) usage(stderr); mbuf = makemsg(*argv, &mbufsize, print_banner); iov.iov_base = mbuf; iov.iov_len = mbufsize; while((utmpptr = getutent())) { if (!utmpptr->ut_name[0] || !strncmp(utmpptr->ut_name, IGNOREUSER, sizeof(utmpptr->ut_name))) continue; #ifdef USER_PROCESS if (utmpptr->ut_type != USER_PROCESS) continue; #endif /* Joey Hess reports that use-sessreg in /etc/X11/wdm/ produces ut_line entries like :0, and a write to /dev/:0 fails. */ if (utmpptr->ut_line[0] == ':') continue; xstrncpy(line, utmpptr->ut_line, sizeof(utmpptr->ut_line)); if ((p = ttymsg(&iov, 1, line, timeout)) != NULL) warnx("%s", p); } endutent(); free(mbuf); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { int i, c, act = ACT_FDISK; int colormode = UL_COLORMODE_AUTO; struct fdisk_context *cxt; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); fdisk_init_debug(0); cxt = fdisk_new_context(); if (!cxt) err(EXIT_FAILURE, _("failed to allocate libfdisk context")); fdisk_context_set_ask(cxt, ask_callback, NULL); while ((c = getopt(argc, argv, "b:c::C:hH:lL::sS:t:u::vV")) != -1) { switch (c) { case 'b': { size_t sz = strtou32_or_err(optarg, _("invalid sector size argument")); if (sz != 512 && sz != 1024 && sz != 2048 && sz != 4096) usage(stderr); fdisk_save_user_sector_size(cxt, sz, sz); break; } case 'C': fdisk_save_user_geometry(cxt, strtou32_or_err(optarg, _("invalid cylinders argument")), 0, 0); break; case 'c': if (optarg) { /* this setting is independent on the current * actively used label */ struct fdisk_label *lb = fdisk_context_get_label(cxt, "dos"); if (!lb) err(EXIT_FAILURE, _("not found DOS label driver")); if (strcmp(optarg, "=dos") == 0) fdisk_dos_enable_compatible(lb, TRUE); else if (strcmp(optarg, "=nondos") == 0) fdisk_dos_enable_compatible(lb, FALSE); else usage(stderr); } /* use default if no optarg specified */ break; case 'H': fdisk_save_user_geometry(cxt, 0, strtou32_or_err(optarg, _("invalid heads argument")), 0); break; case 'S': fdisk_save_user_geometry(cxt, 0, 0, strtou32_or_err(optarg, _("invalid sectors argument"))); break; case 'l': act = ACT_LIST; break; case 'L': if (optarg) colormode = colormode_or_err(optarg, _("unsupported color mode")); break; case 's': act = ACT_SHOWSIZE; break; case 't': { struct fdisk_label *lb = NULL; while (fdisk_context_next_label(cxt, &lb) == 0) fdisk_label_set_disabled(lb, 1); lb = fdisk_context_get_label(cxt, optarg); if (!lb) errx(EXIT_FAILURE, _("unsupported disklabel: %s"), optarg); fdisk_label_set_disabled(lb, 0); } case 'u': if (optarg && *optarg == '=') optarg++; if (fdisk_context_set_unit(cxt, optarg) != 0) usage(stderr); break; case 'V': case 'v': printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case 'h': usage(stdout); default: usage(stderr); } } if (argc-optind != 1 && fdisk_has_user_device_properties(cxt)) warnx(_("The device properties (sector size and geometry) should" " be used with one specified device only.")); colors_init(colormode); switch (act) { case ACT_LIST: fdisk_context_enable_listonly(cxt, 1); if (argc > optind) { int k; for (k = optind; k < argc; k++) print_device_pt(cxt, argv[k]); } else print_all_devices_pt(cxt); break; case ACT_SHOWSIZE: /* deprecated */ if (argc - optind <= 0) usage(stderr); for (i = optind; i < argc; i++) { if (argc - optind == 1) printf("%llu\n", get_dev_blocks(argv[i])); else printf("%s: %llu\n", argv[i], get_dev_blocks(argv[i])); } break; case ACT_FDISK: if (argc-optind != 1) usage(stderr); if (fdisk_context_assign_device(cxt, argv[optind], 0) != 0) err(EXIT_FAILURE, _("cannot open %s"), argv[optind]); /* Here starts interactive mode, use fdisk_{warn,info,..} functions */ color_enable(UL_COLOR_GREEN); fdisk_info(cxt, _("Welcome to fdisk (%s)."), PACKAGE_STRING); color_disable(); fdisk_info(cxt, _("Changes will remain in memory only, until you decide to write them.\n" "Be careful before using the write command.\n")); fflush(stdout); if (!fdisk_dev_has_disklabel(cxt)) { fdisk_warnx(cxt, _("Device does not contain a recognized partition table.")); fdisk_create_disklabel(cxt, NULL); } while (1) process_fdisk_menu(&cxt); } fdisk_free_context(cxt); return EXIT_SUCCESS; }
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 libscols_table *tb; struct libscols_symbols *sy; struct libscols_cell *title; int c; static const struct option longopts[] = { { "maxout", 0, NULL, 'm' }, { "width", 1, NULL, 'w' }, { NULL, 0, NULL, 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, "mw:", longopts, NULL)) != -1) { switch(c) { case 'm': scols_table_enable_maxout(tb, TRUE); 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; } } scols_table_enable_colors(tb, isatty(STDOUT_FILENO)); setup_columns(tb); add_line(tb, "foo", "bla bla bla"); add_line(tb, "bar", "alb alb alb"); title = scols_table_get_title(tb); /* right */ scols_cell_set_data(title, "This is right title"); scols_cell_set_color(title, "red"); scols_cell_set_flags(title, SCOLS_CELL_FL_RIGHT); scols_print_table(tb); /* center */ sy = scols_new_symbols(); if (!sy) err_oom(); scols_table_set_symbols(tb, sy); scols_symbols_set_title_padding(sy, "="); scols_cell_set_data(title, "This is center title (with padding)"); scols_cell_set_color(title, "green"); scols_cell_set_flags(title, SCOLS_CELL_FL_CENTER); scols_print_table(tb); /* left */ scols_symbols_set_title_padding(sy, "-"); scols_cell_set_data(title, "This is left title (with padding)"); scols_cell_set_color(title, "blue"); scols_cell_set_flags(title, SCOLS_CELL_FL_LEFT); scols_print_table(tb); scols_unref_table(tb); return EXIT_SUCCESS; }