1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
|
/* SPDX-License-Identifier: MIT */
/* SPDX-FileCopyrightText: (c) Copyright 2024,2025 Andrew Bower <andrew@bower.uk> */
#include <assert.h>
#include <ctype.h>
#include <getopt.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "options.h"
struct options opt;
const struct option_info options_info[] = {
{ C_R, OPT_SETUIDGID, 'u', NULL, required_argument, "set uid, gid and supplementary groups", "[:]USER[:GROUP]*", },
{ C_R, OPT_ENVUIDGID, 'U', NULL, required_argument, "set UID, GID and GIDLIST vars", "[:]USER[:GROUP]*", },
{ C_R, OPT_ARGV0, 'b', NULL, required_argument, "launch program with ARGV0 as the argv[0]", "ARGV0" },
{ C_R, OPT_ENVDIR, 'e', NULL, required_argument, "populate environment from directory", "DIR" },
{ C_R, OPT_CHROOT, '/', NULL, required_argument, "change root directory", "DIR" },
{ C_R, OPT_CHDIR, 'C', NULL, required_argument, "change directory", "DIR" },
{ C_R, OPT_NICE, 'n', NULL, required_argument, "adjust niceness", "INC" },
{ C_R, OPT_LOCK_WAIT, 'l', NULL, required_argument, "wait for lock", "FILE" },
{ C_R, OPT_LOCK, 'L', NULL, required_argument, "obtain lock; fail fast", "FILE" },
{ C_L, OPT_LOCKOPT_WAIT,'N', NULL, no_argument, "wait for lock (default)", NULL },
{ C_L, OPT_LOCKOPT_TRY, 'n', NULL, no_argument, "don't wait for lock", NULL },
{ C_L, OPT_LOCKOPT_NOISY,'X', NULL, no_argument, "fail noisily (default)", NULL },
{ C_L, OPT_LOCKOPT_QUIET,'x', NULL, no_argument, "fail silently", NULL },
{ C_RS,OPT_LIMIT_MEM, 'm', NULL, required_argument, "set soft DATA, STACK, MEMLOCK and AS limits", "BYTES" },
{ C_RS,OPT_RLIMIT_DATA, 'd', NULL, required_argument, "set RLIMIT_DATA", "BYTES" },
{ C_S, OPT_RLIMIT_MEMLOCK,'l',NULL, required_argument, "set RLIMIT_MEMLOCK", "BYTES" },
{ C_RS,OPT_RLIMIT_NOFILE,'o', NULL, required_argument, "set RLIMIT_NOFILE", "FILES" },
{ C_RS,OPT_RLIMIT_NPROC,'p', NULL, required_argument, "set RLIMIT_NPROC", "PROCS" },
{ C_RS,OPT_RLIMIT_FSIZE,'f', NULL, required_argument, "set RLIMIT_FSIZE", "BYTES" },
{ C_RS,OPT_RLIMIT_CORE, 'c', NULL, required_argument, "set RLIMIT_CORE", "BYTES" },
{ C_RS,OPT_RLIMIT_CPU, 't', NULL, required_argument, "set RLIMIT_CPU", "SECONDS" },
{ C_XS,OPT_RLIMIT_AS, 'a', "limit-as", required_argument, "set RLIMIT_AS", "BYTES" },
{ C_XS,OPT_RLIMIT_RSS, 'r', "limit-rss", required_argument, "set RLIMIT_RSS", "BYTES" },
{ C_XS,OPT_RLIMIT_STACK,'s', "limit-stack", required_argument, "set RLIMIT_STACK", "BYTES" },
{ C_X, OPT_RLIMIT_MEMLOCK, '\0', "limit-memlock", required_argument, "set RLIMIT_MEMLOCK", "BYTES" },
{ C_X, OPT_RLIMIT_MSGQUEUE, '\0', "limit-msgqueue", required_argument, "set RLIMIT_MSGQUEUE", "BYTES" },
{ C_X, OPT_RLIMIT_NICE, '\0', "limit-nice", required_argument, "set RLIMIT_NICE", "NICENESS" },
{ C_X, OPT_RLIMIT_RTPRIO, '\0', "limit-rtprio", required_argument, "set RLIMIT_RTPRIO", "PRIO" },
{ C_X, OPT_RLIMIT_RTTIME, '\0', "limit-rttime", required_argument, "set RLIMIT_RTTIME", "MS" },
{ C_X, OPT_RLIMIT_SIGPENDING,'\0', "limit-sigpending",required_argument, "set RLIMIT_SIGPENDING", "NUM" },
{ C_X, OPT_RLIMIT_LOCKS, '\0', "limit-locks", required_argument, "set RLIMIT_LOCKS", "NUM" },
{ C_ALL,OPT_VERBOSE, 'v', "verbose", no_argument, "be verbose", NULL },
{ C_R, OPT_VERSION, 'V', "version", no_argument, "show " NAME_STR " version", NULL },
{ C_R, OPT_PGRPHACK, 'P', NULL, no_argument, "run in new process group", NULL },
{ C_R, OPT_CLOSE_STDIN, '0', NULL, no_argument, "close stdin", NULL },
{ C_R, OPT_CLOSE_STDOUT,'1', NULL, no_argument, "close stdout", NULL },
{ C_R, OPT_CLOSE_STDERR,'2', NULL, no_argument, "close stderr", NULL },
{ C_X, OPT_LEGACY, '@', NULL, no_argument, "restricts following options to chpst(8) ones", NULL },
{ C_X, OPT_HELP, 'h', "help", no_argument, "show help", NULL },
{ C_X, OPT_FILE, '\0', "file", required_argument, "read options from file", "FILE" },
{ C_X, OPT_EXIT, '\0', "exit", optional_argument, "exit (with optional RETCODE)", "RETCODE" },
{ C_X, OPT_MOUNT_NS, '\0', "mount-ns", no_argument, "create mount namespace", NULL },
{ C_X, OPT_NET_NS, '\0', "net-ns", no_argument, "create net namespace", NULL },
{ C_X, OPT_USER_NS, '\0', "user-ns", no_argument, "create user namespace", NULL },
{ C_X, OPT_PID_NS, '\0', "pid-ns", no_argument, "create pid namespace", NULL },
{ C_X, OPT_UTS_NS, '\0', "uts-ns", no_argument, "create uts namespace", NULL },
{ C_X, OPT_NET_ADOPT, '\0', "adopt-net",required_argument, "adopt net namespace", "NS-PATH" },
{ C_X, OPT_PRIVATE_RUN, '\0', "private-run", no_argument, "create private /run", NULL },
{ C_X, OPT_PRIVATE_TMP, '\0', "private-tmp", no_argument, "create private /tmp", NULL },
{ C_X, OPT_PROTECT_HOME,'\0', "protect-home", no_argument, "hide home directories", NULL },
{ C_X, OPT_RO_HOME, '\0', "ro-home", no_argument, "make home directories read only", NULL },
{ C_X, OPT_RO_SYS, '\0', "ro-sys", no_argument, "create read only system", NULL },
{ C_X, OPT_RO_ETC, '\0', "ro-etc", no_argument, "make config files read only", NULL },
#ifndef NO_CAP
{ C_X, OPT_CAPBS_KEEP, '\0', "cap-bs-keep", required_argument, "restrict capabilities bounding set", "CAP[,...]" },
{ C_X, OPT_CAPBS_DROP, '\0', "cap-bs-drop", required_argument, "drop from capabilities bounding set", "CAP[,...]" },
{ C_X, OPT_CAPS_KEEP, '\0', "caps-keep", required_argument, "keep (only) these capabilities", "CAP[,...]" },
{ C_X, OPT_CAPS_DROP, '\0', "caps-drop", required_argument, "drop these capabilities", "CAP[,...]" },
#endif
{ C_X, OPT_FORK_JOIN, '\0', "fork-join", no_argument, "fork and wait for process", NULL },
{ C_X, OPT_NEW_ROOT, '\0', "new-root", no_argument, "create a new root fs", NULL },
{ C_X, OPT_NO_NEW_PRIVS,'\0', "no-new-privs", no_argument, "no new privileges", NULL },
{ C_X, OPT_CPUS, '\0', "cpus", required_argument, "set CPU affinity", "AFFINITY" },
{ C_X, OPT_CPU_SCHED, '\0', "cpu-scheduler",required_argument, "set CPU scheduler policy", "POLICY" },
{ C_X, OPT_IO_SCHED, '\0', "io-scheduler", required_argument, "set I/O scheduling class", "rt|best-effort|idle[:PRIORITY]"},
{ C_X, OPT_UMASK, '\0', "umask", required_argument,"set umask", "MODE" },
{ C_X, OPT_APP, '\0', "app", required_argument,"define application name", "NAME"},
{ C_X, OPT_RUN_DIR, '\0', "run-dir", no_argument, "create run dir", NULL },
{ C_X, OPT_STATE_DIR, '\0', "state-dir", no_argument, "create state dir", NULL },
{ C_X, OPT_CACHE_DIR, '\0', "cache-dir", no_argument, "create cache dir", NULL },
{ C_X, OPT_LOG_DIR, '\0', "log-dir", no_argument, "create log dir", NULL },
{ C_X, OPT_LOGIN, '\0', "login", no_argument, "simulate login environment", NULL },
{ C_X, OPT_OOM, '\0', "oom", required_argument,"set oom adjust value", "ADJ" },
{ C_X, OPT_HARDLIMIT, '\0', "hardlimit", no_argument, "set hard limits with soft limits", NULL },
{ C_A, OPT_UIDNUM, 'u', NULL, required_argument,"set numerical uid", "UID" },
{ C_A, OPT_GIDNUM, 'g', NULL, required_argument,"set numerical gid", "GID" },
{ C_A, OPT_GIDSNUM, 'G', NULL, required_argument,"set numerical supplementary groups", "GID*" },
{ C_A, OPT_UGFROMENV, 'U', NULL, no_argument,"set numerical uid, gid and supplementary groups from env", NULL },
{ C_X, OPT_UGFROMENV, '\0', "ugids-from-env", no_argument,"set numerical uid, gid and supplementary groups from env", NULL },
{ C_A, OPT_UGCLEARENV, 'z', NULL, no_argument,"clear numerical uid, gid and supplementary groups in env", NULL },
{ C_X, OPT_UGCLEARENV, '\0', "ugids-clear-env", no_argument,"clear numerical uid, gid and supplementary groups in env", NULL },
};
#define max_options ((ssize_t) ((sizeof options_info / sizeof *options_info)))
static struct option options[max_options + 1];
static char optstr[max_options * 2];
static void handle_option(enum compat_level *compat,
const struct option_info *optdef,
char *optarg);
void options_print(FILE *out) {
const struct option_info *optdef;
int i = 0;
for (optdef = options_info; optdef - options_info < max_options; optdef++) {
if (optdef->compat_level & opt.app->compat_level) {
if (i++ == 0)
fprintf(out, "\n OPTIONS\n");
fprintf(out, " %c%c%s%s%s%c%-*s %s\n",
optdef->short_name ? '-' : ' ',
optdef->short_name ? optdef->short_name : ' ',
opt.app->long_opts && optdef->long_name ? (optdef->short_name ? "," : " ") : "",
opt.app->long_opts && optdef->long_name ? " --" : "",
opt.app->long_opts && optdef->long_name ? optdef->long_name : "",
optdef->has_arg == optional_argument ? '=' : ' ',
opt.app->long_opts && optdef->long_name ? 20 - (int) strlen(optdef->long_name) : 24,
optdef->arg ? optdef->arg : " ",
optdef->help ? optdef->help : "");
}
}
}
void options_print_positional(FILE *out) {
int i;
for (i = 0; i < opt.app->takes_positional_opts; i++) {
const struct option_info *optdef;
enum opt option = opt.app->positional_opts[i];
/* Look up option definition */
for (optdef = options_info;
optdef - options_info < max_options && option != optdef->option;
optdef++);
if (optdef->has_arg != no_argument)
fprintf(out, " %s", optdef->arg);
}
}
void options_explain_positional(FILE *out) {
int i;
if (opt.app->takes_positional_opts)
fprintf(out, "\n");
for (i = 0; i < opt.app->takes_positional_opts; i++) {
const struct option_info *optdef;
enum opt option = opt.app->positional_opts[i];
/* Look up option definition */
for (optdef = options_info;
optdef - options_info < max_options && option != optdef->option;
optdef++);
fprintf(out, " %-10s %s\n",
optdef->has_arg != no_argument ? optdef->arg : "",
optdef->help);
}
}
bool options_init(void) {
const struct option_info *optdef;
size_t optstr_sz = 1;
size_t optstr_len;
int i = 0;
for (optdef = options_info; optdef - options_info < max_options; optdef++) {
if (optdef->compat_level & opt.app->compat_level) {
/* Populate long option list */
if (optdef->long_name) {
struct option option = {
.name = optdef->long_name,
.has_arg = optdef->has_arg,
.flag = 0,
.val = optdef->option
};
options[i++] = option;
}
/* Size up short option string */
if (optdef->short_name) {
switch (optdef->has_arg) {
case optional_argument:
optstr_sz++;
[[fallthrough]];
case required_argument:
optstr_sz++;
[[fallthrough]];
default:
optstr_sz++;
}
}
}
}
/* Populate short option string */
if (optstr_sz + 1 > sizeof optstr) {
fprintf(stderr, "short option string too long\n");
return false;
}
optstr_len = 0;
optstr[optstr_len++] = '+'; /* Stop accepting options after prog name */
for (optdef = options_info; optdef - options_info < max_options; optdef++) {
if (optdef->compat_level & opt.app->compat_level && optdef->short_name) {
optstr[optstr_len++] = optdef->short_name;
switch (optdef->has_arg) {
case optional_argument:
optstr[optstr_len++] = ':';
[[fallthrough]];
case required_argument:
optstr[optstr_len++] = ':';
default:
}
}
}
assert(optstr_len == optstr_sz);
optstr[optstr_len++] = '\0';
return true;
}
bool parse_limit(rlim_t *lim, const char *arg) {
int toks = 0;
long long val;
if (!strcmp(arg, "unlimited") ||
!strcmp(arg, "infinity") ||
!strcmp(arg, "=")) {
*lim = RLIM_INFINITY;
toks = 1;
} else {
toks = sscanf(arg, "%lld", &val);
if (toks == 1) {
if (val == -1LL) {
*lim = RLIM_INFINITY;
} else if (val >= 0LL) {
*lim = val;
} else {
fprintf(stderr, "invalid limit: %lld\n", val);
toks = 0;
}
}
}
return toks == 1 ? true : false;
}
/* SUPPORTED FORMATS
* soft (this differs from prlimit to match chpst)
* soft:
* soft:hard
* :hard
* +both (this is unique to xchpst)
*/
bool parse_limits(struct limit *limit, const char *arg) {
char *sep;
bool both = *arg == '+';
if (both)
arg++;
sep = strchr(arg, ':');
if (sep != NULL) {
*sep++ = '\0';
limit->hard_specified = parse_limit(&limit->limits.rlim_max, sep);
} else if (set(OPT_HARDLIMIT)) {
both = true;
}
if (arg != sep) {
limit->soft_specified = parse_limit(&limit->limits.rlim_cur, arg);
}
if (both) {
limit->limits.rlim_max = limit->limits.rlim_cur;
limit->hard_specified = limit->soft_specified;
}
return limit->soft_specified || limit->hard_specified;
}
#ifndef NO_CAP
bool parse_caps(cap_bits_t *caps, char *names) {
cap_bits_t set = 0;
cap_value_t val;
cap_value_t max_cap = cap_max_bits();
bool good = true;
char *tok;
int rc;
if (max_cap < 0) {
/* Record that we don't have capabilities but don't fail option */
runtime.absent.caps = true;
return true;
}
assert(8 * sizeof set >= (unsigned) max_cap);
while (names && *names && (tok = strsep(&names, ","))) {
rc = cap_from_name(tok, &val);
if (rc != 0) {
fprintf(stderr, "cannot interpret capability \"%s\"\n", tok);
good = false;
}
set |= (1ull << val);
}
*caps = set;
return good;
}
#endif
bool parse_cpu_range(const char *str, int range[3], int *max) {
int field = 0;
int val = 0;
char c = -1;
while (field < 3 && c) {
if (isdigit(c = *str++)) {
val = val * 10 + c - '0';
} else if ((c == '-' && field == 0) ||
(c == ':' && field == 1) ||
c == '\0') {
range[field++] = val;
val = 0;
} else {
return false;
}
}
/* Default stride is 1 */
if (field < 3)
range[2] = 1;
/* Singleton */
if (field < 2)
range[1] = range[0];
/* No CPUs listed */
if (field < 1)
return false;
if (max != NULL && range[1] > *max)
*max = range[1];
return true;
}
void parse_cpus(char *spec) {
char *rest = spec;
char *tok;
char *end = spec;
int range[3];
int cpu;
int max = 0;
while((tok = strsep(&rest, ","))) {
if (!parse_cpu_range(tok, range, &max))
goto fail;
end = tok;
}
opt.cpu_affinity.size = CPU_ALLOC_SIZE(max++);
opt.cpu_affinity.mask = CPU_ALLOC(max);
if (opt.cpu_affinity.mask == NULL) {
perror("CPU_ALLOC");
goto fail;
}
CPU_ZERO_S(opt.cpu_affinity.size, opt.cpu_affinity.mask);
for (tok = spec; tok <= end; tok += strlen(tok) + 1) {
parse_cpu_range(tok, range, NULL);
for (cpu = range[0]; cpu <= range[1]; cpu += range[2])
CPU_SET_S(cpu, opt.cpu_affinity.size, opt.cpu_affinity.mask);
}
return;
fail:
opt.error = true;
fprintf(stderr, "error in CPU list (at %s)\n", tok);
}
void parse_ionice(char *spec) {
const char *classes[] = {
"rt", "best-effort", "idle", NULL
};
long n;
int data;
char *s = strchrnul(spec, ':');
char *end;
for (n = 0; classes[n] && strncmp(spec, classes[n], s - spec); n++);
if (classes[n++] == NULL) {
n = strtol(spec, &end, 10);
if (end == spec || (*end != '\0' && *end != ':')) {
*end = '\0';
opt.error = true;
fprintf(stderr, "invalid ionice class: %s\n", spec);
}
}
if (*s)
s++;
data = strtol(s, NULL, 10);
opt.ionice_prio = IOPRIO_PRIO_VALUE(n, data);
}
int sched_policy_from_name(const char *name) {
if (!strcmp(name, "batch"))
return SCHED_BATCH;
else if (!strcmp(name, "idle"))
return SCHED_IDLE;
else if (strcmp(name, "other"))
fprintf(stderr, "ignoring unknown scheduler policy: %s\n", name);
return SCHED_OTHER;
}
const struct option_info *find_option(int by_code,
const char *by_name) {
const struct option_info *optdef;
const struct option_info *incompatible_option = NULL;
enum compat_level compat = opt.app->compat_level;
bool is_short = (by_code && isascii(by_code)) ||
(by_name && *by_name && by_name[1] == '\0');
/* Look up option definition */
for (optdef = options_info;
optdef - options_info < max_options;
optdef++) {
if ((by_code && by_code == (is_short ? optdef->short_name : (int) optdef->option)) ||
(by_name && (is_short ? *by_name == optdef->short_name : optdef->long_name && !strcmp(by_name, optdef->long_name)))) {
if ((optdef->compat_level & compat) == 0) {
/* Tentatively found an incompatible option but there may be
* another one that is compatible later in the list. */
incompatible_option = optdef;
} else {
incompatible_option = NULL;
break;
}
}
}
if (optdef - options_info == max_options)
return incompatible_option;
else
return optdef;
}
static void read_options_file(const char *path) {
struct options_file *opt_file;
struct stat statbuf;
int fd;
int rc;
off_t offset;
char *ptr;
char *option_name = NULL;
char *option_name_end = NULL;
char *option_value = NULL;
char *option_value_end = NULL;
ssize_t len;
const struct option_info *optdef;
enum compat_level compat = opt.app->compat_level;
enum {
S_LEADING_WSP,
S_COMMENT,
/* No states above are actionable on EOF */
/* All states below are actionable on EOF */
S_KEY,
S_DELIM_WSP,
S_VALUE,
S_POSSIBLY_TRAILING_WSP,
} state = S_LEADING_WSP;
fd = open(path, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "error opening options file %s: %s\n", path, strerror(errno));
goto fail;
}
rc = fstat(fd, &statbuf);
if (rc == -1) {
close(fd);
goto fail;
}
/* We allocate the space for the file permanently, just as the argv array
remains allocated. Option processing modifies argument values.
Add one for NUL-terminating last element if not a newline. */
opt_file = malloc(sizeof *opt_file + statbuf.st_size + 1);
if (opt_file == NULL) {
close(fd);
goto fail;
}
opt_file->next = opt.opt_files;
opt.opt_files = opt_file;
offset = 0;
do {
len = read(fd, opt_file->content + offset, statbuf.st_size - offset);
if (len == -1) {
close(fd);
goto fail;
}
offset += len;
} while (offset < statbuf.st_size);
close(fd);
ptr = opt_file->content;
for (ptr = opt_file->content; ptr - opt_file->content < statbuf.st_size; ptr++) {
char c = *ptr;
/* Whether a key and value are now completely specified */
bool action = false;
switch (state) {
case S_LEADING_WSP:
if (c == '#') {
state = S_COMMENT;
} else if (!isspace(c)) {
option_name = ptr;
state = S_KEY;
}
break;
case S_KEY:
if (c == '\n') {
action = true;
} else if (isspace(c)) {
state = S_DELIM_WSP;
}
option_name_end = ptr;
break;
case S_DELIM_WSP:
if (c == '\n') {
action = true;
} else if (!isspace(c)) {
option_value = ptr;
state = S_VALUE;
}
break;
case S_VALUE:
if (c == '\n') {
action = true;
} else if (isspace(c)) {
state = S_POSSIBLY_TRAILING_WSP;
}
option_value_end = ptr;
break;
case S_POSSIBLY_TRAILING_WSP:
if (c == '\n') {
action = true;
} else if (!isspace(c)) {
state = S_VALUE;
option_value_end = ptr;
}
break;
case S_COMMENT:
if (c == '\n') {
state = S_LEADING_WSP;
}
break;
}
if (action /* EOL */ ||
(ptr + 1 == opt_file->content + statbuf.st_size /* EOF */ &&
state >= S_KEY /* An actionable state */)) {
if (!action) {
/* If EOF then there is no EOL character to overwrite */
if (option_value)
option_value_end = ptr + 1;
else
option_name_end = ptr + 1;
}
*option_name_end = '\0';
if (option_value)
*option_value_end = '\0';
state = S_LEADING_WSP;
} else {
continue;
}
optdef = find_option(0, option_name);
if (optdef && (optdef->compat_level & compat) == 0) {
char short_name[2] = { optdef->short_name, 0};
fprintf(stderr, "illegal option (%s) at this compat level\n",
optdef->long_name ? optdef->long_name : short_name);
opt.error = true;
continue;
} else if (!optdef) {
fprintf(stderr, "unknown option in config file: %s\n", option_name);
opt.error = true;
continue;
}
if (is_verbose())
fprintf(stderr, "handling file option '%s' with value '%s'\n", option_name, option_value);
enable(optdef->option);
/* For now, throw away the value end pointer. In future we could use
this to enable arguments containing NUL bytes to be handled.
(Of course, this format cannot handle newline characters, which would
probably be more useful to embed!) */
handle_option(&compat, optdef,
optdef->has_arg == no_argument ? NULL : option_value);
option_value = NULL;
}
if (errno != 0) {
fprintf(stderr, "error reading options file %s: %s\n", path, strerror(errno));
goto fail;
}
return;
fail:
opt.error = true;
}
static void handle_option(enum compat_level *compat,
const struct option_info *optdef,
char *optarg) {
char *end;
switch (optdef->option) {
case OPT_LEGACY:
*compat = COMPAT_CHPST;
break;
case OPT_VERSION:
opt.version = true;
break;
case OPT_HELP:
opt.help = true;
break;
case OPT_FILE:
read_options_file(optarg);
break;
case OPT_EXIT:
opt.exit = true;
opt.retcode = optarg ? atoi(optarg) : CHPST_ERROR_EXIT;
break;
case OPT_VERBOSE:
opt.verbosity++;
break;
case OPT_ARGV0:
opt.argv0 = optarg;
break;
case OPT_ENVDIR:
opt.env_dir = optarg;
break;
case OPT_CHROOT:
opt.chroot = optarg;
break;
case OPT_CHDIR:
opt.chdir = optarg;
break;
case OPT_NICE:
opt.niceness = atoi(optarg);
break;
case OPT_LOCK_WAIT:
opt.lock_wait = true;
[[fallthrough]];
case OPT_LOCK:
opt.lock_file = optarg;
break;
case OPT_LOCKOPT_WAIT:
opt.lock_wait = true;
break;
case OPT_LOCKOPT_TRY:
opt.lock_wait = false;
opt.lock_nowait_override = true;
break;
case OPT_LOCKOPT_NOISY:
opt.lock_quiet = false;
break;
case OPT_LOCKOPT_QUIET:
opt.lock_quiet = true;
break;
case OPT_MOUNT_NS:
opt.new_ns |= CLONE_NEWNS;
break;
case OPT_NET_NS:
opt.new_ns |= CLONE_NEWNET;
break;
case OPT_PID_NS:
opt.new_ns |= CLONE_NEWPID;
break;
case OPT_USER_NS:
opt.new_ns |= CLONE_NEWUSER;
break;
case OPT_UTS_NS:
opt.new_ns |= CLONE_NEWUTS;
break;
case OPT_NET_ADOPT:
opt.net_adopt = optarg;
break;
case OPT_PRIVATE_RUN:
case OPT_PRIVATE_TMP:
case OPT_PROTECT_HOME:
case OPT_RO_HOME:
case OPT_RO_SYS:
case OPT_RO_ETC:
case OPT_PGRPHACK:
case OPT_FORK_JOIN:
case OPT_NEW_ROOT:
case OPT_NO_NEW_PRIVS:
case OPT_RUN_DIR:
case OPT_STATE_DIR:
case OPT_CACHE_DIR:
case OPT_LOG_DIR:
case OPT_LOGIN:
case OPT_HARDLIMIT:
case OPT_UGCLEARENV:
/* Boolean options needing no further option processing */
break;
#ifndef NO_CAP
case OPT_CAPBS_KEEP:
if (!parse_caps(&opt.cap_bounds, optarg))
opt.error = true;
opt.cap_bounds_op = CAP_OP_KEEP;
break;
case OPT_CAPBS_DROP:
if (!parse_caps(&opt.cap_bounds, optarg))
opt.error = true;
opt.cap_bounds_op = CAP_OP_DROP;
break;
case OPT_CAPS_KEEP:
if (!parse_caps(&opt.caps, optarg))
opt.error = true;
opt.caps_op = CAP_OP_KEEP;
break;
case OPT_CAPS_DROP:
if (!parse_caps(&opt.caps, optarg))
opt.error = true;
opt.caps_op = CAP_OP_DROP;
break;
#endif
case OPT_CPU_SCHED:
opt.sched_policy = sched_policy_from_name(optarg);
break;
case OPT_CPUS:
parse_cpus(optarg);
break;
case OPT_IO_SCHED:
parse_ionice(optarg);
break;
case OPT_UMASK:
if (sscanf(optarg, "%o", &opt.umask) != 1)
opt.error = true;
break;
case OPT_OOM:
opt.oom_adjust = strtol(optarg, &end, 10);
if (*optarg == '\0' || *end != '\0')
opt.error = true;
break;
case OPT_APP:
opt.app_name = optarg;
break;
case OPT_SETUIDGID:
if (usrgrp_parse(&opt.users_groups, optarg))
opt.error = true;
else if (usrgrp_resolve(&opt.users_groups))
opt.error = true;
if (opt.verbosity > 1)
usrgrp_print(stderr, "setuidgid", &opt.users_groups);
break;
case OPT_ENVUIDGID:
if (usrgrp_parse(&opt.env_users_groups, optarg))
opt.error = true;
else if (usrgrp_resolve(&opt.env_users_groups))
opt.error = true;
if (opt.verbosity > 1)
usrgrp_print(stderr, "envuidgid", &opt.env_users_groups);
break;
case OPT_UIDNUM:
if (usrgrp_from_env(&opt.users_groups, optarg, NULL, NULL))
opt.error = true;
else
enable(OPT_SETUIDGID);
break;
case OPT_GIDNUM:
if (usrgrp_from_env(&opt.users_groups, NULL, optarg, NULL))
opt.error = true;
else
enable(OPT_SETUIDGID);
break;
case OPT_GIDSNUM:
if (usrgrp_from_env(&opt.users_groups, NULL, NULL, optarg))
opt.error = true;
else
enable(OPT_SETUIDGID);
break;
case OPT_UGFROMENV:
if (usrgrp_from_env(&opt.users_groups,
getenv("UID"), getenv("GID"), getenv("GIDLIST")))
opt.error = true;
else
enable(OPT_SETUIDGID);
if (opt.verbosity > 1)
usrgrp_print(stderr, "ugfromenv", &opt.users_groups);
break;
case OPT_LIMIT_MEM:
if (!parse_limits(&opt.rlimit_memlock, optarg)) {
opt.error = true;
} else {
opt.rlimit_data = opt.rlimit_memlock;
opt.rlimit_stack = opt.rlimit_memlock;
opt.rlimit_as = opt.rlimit_as;
}
break;
case OPT_RLIMIT_DATA:
if (!parse_limits(&opt.rlimit_data, optarg))
opt.error = true;
break;
case OPT_RLIMIT_MEMLOCK:
if (!parse_limits(&opt.rlimit_memlock, optarg))
opt.error = true;
break;
case OPT_RLIMIT_AS:
if (!parse_limits(&opt.rlimit_as, optarg))
opt.error = true;
break;
case OPT_RLIMIT_STACK:
if (!parse_limits(&opt.rlimit_stack, optarg))
opt.error = true;
break;
case OPT_RLIMIT_NOFILE:
if (!parse_limits(&opt.rlimit_nofile, optarg))
opt.error = true;
break;
case OPT_RLIMIT_RSS:
if (!parse_limits(&opt.rlimit_rss, optarg))
opt.error = true;
break;
case OPT_RLIMIT_NPROC:
if (!parse_limits(&opt.rlimit_nproc, optarg))
opt.error = true;
break;
case OPT_RLIMIT_FSIZE:
if (!parse_limits(&opt.rlimit_fsize, optarg))
opt.error = true;
break;
case OPT_RLIMIT_CPU:
if (!parse_limits(&opt.rlimit_cpu, optarg))
opt.error = true;
break;
case OPT_RLIMIT_CORE:
if (!parse_limits(&opt.rlimit_core, optarg))
opt.error = true;
break;
case OPT_RLIMIT_MSGQUEUE:
if (!parse_limits(&opt.rlimit_msgqueue, optarg))
opt.error = true;
break;
case OPT_RLIMIT_NICE:
if (!parse_limits(&opt.rlimit_nice, optarg))
opt.error = true;
break;
case OPT_RLIMIT_RTPRIO:
if (!parse_limits(&opt.rlimit_rtprio, optarg))
opt.error = true;
break;
case OPT_RLIMIT_RTTIME:
if (!parse_limits(&opt.rlimit_rttime, optarg))
opt.error = true;
break;
case OPT_RLIMIT_SIGPENDING:
if (!parse_limits(&opt.rlimit_sigpending, optarg))
opt.error = true;
break;
case OPT_RLIMIT_LOCKS:
if (!parse_limits(&opt.rlimit_locks, optarg))
opt.error = true;
break;
case OPT_CLOSE_STDIN:
case OPT_CLOSE_STDOUT:
case OPT_CLOSE_STDERR:
opt.close_fds |= 1 << (optdef->option - OPT_CLOSE_STDIN + STDIN_FILENO);
break;
/* Avoid default case to get useful compiler warnings instead.
default:
fprintf(stderr, "-%c%s not yet implemented\n",
optdef->long_name ? '-' : optdef->short_name,
optdef->long_name ? optdef->long_name : "");
opt.error = true;
*/
}
}
int options_parse(int argc, char *argv[]) {
const struct option_info *optdef;
enum compat_level compat = opt.app->compat_level;
int c;
/* Process options */
while ((c = opt.app->long_opts ?
getopt_long(argc, argv, optstr, options, NULL) :
getopt(argc, argv, optstr)) != -1) {
optdef = find_option(c, NULL);
if (optdef && (optdef->compat_level & compat) == 0) {
char short_name[2] = { optdef->short_name, 0};
fprintf(stderr, "illegal option (%s) at this compat level\n",
optdef->long_name ? optdef->long_name : short_name);
opt.error = true;
} else if (optdef) {
/* Set bitfield before calling handler in case the handler wishes
* to reset the option based on argument value. */
enable(optdef->option);
handle_option(&compat, optdef, optarg);
} else {
opt.error = true;
}
}
/* Process positional arguments */
assert(opt.app->takes_positional_opts <= MAX_POSITIONAL_OPTS);
for (int i = 0; i < opt.app->takes_positional_opts; i++) {
enum opt option = opt.app->positional_opts[i];
if (optind == argc) {
opt.error = true;
break;
}
/* Look up option definition */
for (optdef = options_info;
optdef - options_info < max_options && option != optdef->option;
optdef++);
assert(optdef);
enable(optdef->option);
handle_option(&compat, optdef,
optdef->has_arg == no_argument ? NULL : argv[optind++]);
}
return optind;
}
void options_free(void) {
struct options_file *file, *next;
if (opt.cpu_affinity.size)
CPU_FREE(opt.cpu_affinity.mask);
usrgrp_free(&opt.users_groups);
usrgrp_free(&opt.env_users_groups);
for (file = opt.opt_files; file; file = next) {
next = file->next;
free(file);
}
}
|