- const char *p;
- unsigned long options = 0;
- int verbose = 0;
- int c;
-
- /* Options --3gb and --4gb are for compatibitity with an old Debian setarch
- implementation. */
- static const struct option longopts[] =
- {
- { "help", 0, 0, 'h' },
- { "version", 0, 0, 'V' },
- { "verbose", 0, 0, 'v' },
- { "addr-no-randomize", 0, 0, 'R' },
- { "fdpic-funcptrs", 0, 0, 'F' },
- { "mmap-page-zero", 0, 0, 'Z' },
- { "addr-compat-layout", 0, 0, 'L' },
- { "read-implies-exec", 0, 0, 'X' },
- { "32bit", 0, 0, 'B' },
- { "short-inode", 0, 0, 'I' },
- { "whole-seconds", 0, 0, 'S' },
- { "sticky-timeouts", 0, 0, 'T' },
- { "3gb", 0, 0, '3' },
- { "4gb", 0, 0, OPT_4GB },
- { "uname-2.6", 0, 0, OPT_UNAME26 },
- { NULL, 0, 0, 0 }
- };
-
- setlocale(LC_ALL, "");
- bindtextdomain(PACKAGE, LOCALEDIR);
- textdomain(PACKAGE);
- atexit(close_stdout);
-
- if (argc < 1)
- show_usage(_("Not enough arguments"));
-
- p = program_invocation_short_name;
- if (!strcmp(p, "setarch")) {
- argc--;
- if (argc < 1)
- show_usage(_("Not enough arguments"));
- p = argv[1];
- argv[1] = argv[0]; /* for getopt_long() to get the program name */
- argv++;
- if (!strcmp(p, "-h") || !strcmp(p, "--help"))
- show_help();
- else if (!strcmp(p, "-V") || !strcmp(p, "--version"))
- show_version();
- else if (!strcmp(p, "--list")) {
- set_arch(argv[0], 0L, 1);
- return EXIT_SUCCESS;
- }
- }
- #if defined(__sparc64__) || defined(__sparc__)
- if (!strcmp(p, "sparc32bash")) {
- if (set_arch(p, 0L, 0))
- err(EXIT_FAILURE, _("Failed to set personality to %s"), p);
- execl("/bin/bash", NULL);
- err(EXIT_FAILURE, _("failed to execute %s"), "/bin/bash");
- }
- #endif
-
- while ((c = getopt_long(argc, argv, "+hVv3BFILRSTXZ", longopts, NULL)) != -1) {
- switch (c) {
- case 'h':
- show_help();
- break;
- case 'V':
- show_version();
- break;
- case 'v':
- verbose = 1;
- break;
- case 'R':
- turn_on(ADDR_NO_RANDOMIZE, options);
- break;
- case 'F':
- turn_on(FDPIC_FUNCPTRS, options);
- break;
- case 'Z':
- turn_on(MMAP_PAGE_ZERO, options);
- break;
- case 'L':
- turn_on(ADDR_COMPAT_LAYOUT, options);
- break;
- case 'X':
- turn_on(READ_IMPLIES_EXEC, options);
- break;
- case 'B':
- turn_on(ADDR_LIMIT_32BIT, options);
- break;
- case 'I':
- turn_on(SHORT_INODE, options);
- break;
- case 'S':
- turn_on(WHOLE_SECONDS, options);
- break;
- case 'T':
- turn_on(STICKY_TIMEOUTS, options);
- break;
- case '3':
- turn_on(ADDR_LIMIT_3GB, options);
- break;
- case OPT_4GB: /* just ignore this one */
- break;
- case OPT_UNAME26:
- turn_on(UNAME26, options);
- break;
- default:
- show_usage(NULL);
- }
- }
-
- argc -= optind;
- argv += optind;
-
- if (set_arch(p, options, 0))
- err(EXIT_FAILURE, _("Failed to set personality to %s"), p);
-
- if (!argc) {
- execl("/bin/sh", "-sh", NULL);
- err(EXIT_FAILURE, _("failed to execute %s"), "/bin/sh");
- }
-
- execvp(argv[0], argv);
- err(EXIT_FAILURE, "%s", argv[0]);
- return EXIT_FAILURE;
+ const char *arch = NULL;
+ unsigned long options = 0;
+ int verbose = 0;
+ int archwrapper;
+ int c;
+ struct arch_domain *doms, *target = NULL;
+ unsigned long pers_value = 0;
+ char *shell = NULL, *shell_arg = NULL;
+
+ /* Options without equivalent short options */
+ enum {
+ OPT_4GB = CHAR_MAX + 1,
+ OPT_UNAME26,
+ OPT_LIST
+ };
+
+ /* Options --3gb and --4gb are for compatibility with an old
+ * Debian setarch implementation. */
+ static const struct option longopts[] = {
+ {"help", no_argument, NULL, 'h'},
+ {"version", no_argument, NULL, 'V'},
+ {"verbose", no_argument, NULL, 'v'},
+ {"addr-no-randomize", no_argument, NULL, 'R'},
+ {"fdpic-funcptrs", no_argument, NULL, 'F'},
+ {"mmap-page-zero", no_argument, NULL, 'Z'},
+ {"addr-compat-layout", no_argument, NULL, 'L'},
+ {"read-implies-exec", no_argument, NULL, 'X'},
+ {"32bit", no_argument, NULL, 'B'},
+ {"short-inode", no_argument, NULL, 'I'},
+ {"whole-seconds", no_argument, NULL, 'S'},
+ {"sticky-timeouts", no_argument, NULL, 'T'},
+ {"3gb", no_argument, NULL, '3'},
+ {"4gb", no_argument, NULL, OPT_4GB},
+ {"uname-2.6", no_argument, NULL, OPT_UNAME26},
+ {"list", no_argument, NULL, OPT_LIST},
+ {NULL, 0, NULL, 0}
+ };
+
+ setlocale(LC_ALL, "");
+ bindtextdomain(PACKAGE, LOCALEDIR);
+ textdomain(PACKAGE);
+ close_stdout_atexit();
+
+ if (argc < 1) {
+ warnx(_("Not enough arguments"));
+ errtryhelp(EXIT_FAILURE);
+ }
+ archwrapper = strcmp(program_invocation_short_name, "setarch") != 0;
+ if (archwrapper) {
+ arch = program_invocation_short_name; /* symlinks to setarch */
+
+ /* Don't use ifdef sparc here, we get "Unrecognized architecture"
+ * error message later if necessary */
+ if (strcmp(arch, "sparc32bash") == 0) {
+ shell = "/bin/bash";
+ shell_arg = "";
+ goto set_arch;
+ }
+ } else {
+ if (1 < argc && *argv[1] != '-') {
+ arch = argv[1];
+ argv[1] = argv[0]; /* for getopt_long() to get the program name */
+ argv++;
+ argc--;
+ }
+ }
+
+ while ((c = getopt_long(argc, argv, "+hVv3BFILRSTXZ", longopts, NULL)) != -1) {
+ switch (c) {
+ case 'v':
+ verbose = 1;
+ break;
+ case 'R':
+ turn_on(ADDR_NO_RANDOMIZE, options);
+ break;
+ case 'F':
+ turn_on(FDPIC_FUNCPTRS, options);
+ break;
+ case 'Z':
+ turn_on(MMAP_PAGE_ZERO, options);
+ break;
+ case 'L':
+ turn_on(ADDR_COMPAT_LAYOUT, options);
+ break;
+ case 'X':
+ turn_on(READ_IMPLIES_EXEC, options);
+ break;
+ case 'B':
+ turn_on(ADDR_LIMIT_32BIT, options);
+ break;
+ case 'I':
+ turn_on(SHORT_INODE, options);
+ break;
+ case 'S':
+ turn_on(WHOLE_SECONDS, options);
+ break;
+ case 'T':
+ turn_on(STICKY_TIMEOUTS, options);
+ break;
+ case '3':
+ turn_on(ADDR_LIMIT_3GB, options);
+ break;
+ case OPT_4GB: /* just ignore this one */
+ break;
+ case OPT_UNAME26:
+ turn_on(UNAME26, options);
+ break;
+ case OPT_LIST:
+ if (!archwrapper) {
+ list_arch_domains(init_arch_domains());
+ return EXIT_SUCCESS;
+ } else
+ warnx(_("unrecognized option '--list'"));
+ /* fallthrough */
+
+ default:
+ errtryhelp(EXIT_FAILURE);
+ case 'h':
+ usage(archwrapper);
+ case 'V':
+ print_version(EXIT_SUCCESS);
+ }
+ }
+
+ if (!arch && !options)
+ errx(EXIT_FAILURE, _("no architecture argument or personality flags specified"));
+
+ argc -= optind;
+ argv += optind;
+
+set_arch:
+ /* get execution domain (architecture) */
+ if (arch) {
+ doms = init_arch_domains();
+ target = get_arch_domain(doms, arch);
+
+ if (!target)
+ errx(EXIT_FAILURE, _("%s: Unrecognized architecture"), arch);
+ pers_value = target->perval;
+ }
+
+ /* add personality flags */
+ pers_value |= options;
+
+ /* call kernel */
+ if (personality(pers_value) < 0) {
+ /*
+ * Depending on architecture and kernel version, personality
+ * syscall is either capable or incapable of returning an error.
+ * If the return value is not an error, then it's the previous
+ * personality value, which can be an arbitrary value
+ * undistinguishable from an error value.
+ * To make things clear, a second call is needed.
+ */
+ if (personality(pers_value) < 0)
+ err(EXIT_FAILURE, _("failed to set personality to %s"), arch);
+ }
+
+ /* make sure architecture is set as expected */
+ if (arch)
+ verify_arch_domain(target, arch);
+
+ if (!argc) {
+ shell = "/bin/sh";
+ shell_arg = "-sh";
+ }
+ if (verbose) {
+ printf(_("Execute command `%s'.\n"), shell ? shell : argv[0]);
+ /* flush all output streams before exec */
+ fflush(NULL);
+ }
+
+ /* Execute shell */
+ if (shell) {
+ execl(shell, shell_arg, NULL);
+ errexec(shell);
+ }
+
+ /* Execute on command line specified command */
+ execvp(argv[0], argv);
+ errexec(argv[0]);