setarch.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * linux32/linux64 allows for changing uname emulation.
  4. *
  5. * Copyright 2002 Andi Kleen, SuSE Labs.
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config SETARCH
  10. //config: bool "setarch"
  11. //config: default y
  12. //config: select PLATFORM_LINUX
  13. //config: help
  14. //config: The linux32 utility is used to create a 32bit environment for the
  15. //config: specified program (usually a shell). It only makes sense to have
  16. //config: this util on a system that supports both 64bit and 32bit userland
  17. //config: (like amd64/x86, ppc64/ppc, sparc64/sparc, etc...).
  18. //applet:IF_SETARCH(APPLET(setarch, BB_DIR_BIN, BB_SUID_DROP))
  19. //applet:IF_SETARCH(APPLET_ODDNAME(linux32, setarch, BB_DIR_BIN, BB_SUID_DROP, linux32))
  20. //applet:IF_SETARCH(APPLET_ODDNAME(linux64, setarch, BB_DIR_BIN, BB_SUID_DROP, linux64))
  21. //kbuild:lib-$(CONFIG_SETARCH) += setarch.o
  22. //usage:#define setarch_trivial_usage
  23. //usage: "PERSONALITY [-R] PROG ARGS"
  24. //usage:#define setarch_full_usage "\n\n"
  25. //usage: "PERSONALITY may be:"
  26. //usage: "\n"" linux32 Set 32bit uname emulation"
  27. //usage: "\n"" linux64 Set 64bit uname emulation"
  28. //usage: "\n"
  29. //usage: "\n"" -R Disable address space randomization"
  30. //usage:
  31. //usage:#define linux32_trivial_usage NOUSAGE_STR
  32. //usage:#define linux32_full_usage ""
  33. //usage:
  34. //usage:#define linux64_trivial_usage NOUSAGE_STR
  35. //usage:#define linux64_full_usage ""
  36. #include "libbb.h"
  37. #include <sys/personality.h>
  38. #ifndef ADDR_NO_RANDOMIZE
  39. # define ADDR_NO_RANDOMIZE 0x0040000
  40. #endif
  41. int setarch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  42. int setarch_main(int argc UNUSED_PARAM, char **argv)
  43. {
  44. unsigned opts;
  45. unsigned long pers;
  46. /* Figure out what personality we are supposed to switch to ...
  47. * we can be invoked as either:
  48. * argv[0],argv[1] == "setarch","personality"
  49. * argv[0] == "personality"
  50. */
  51. if (ENABLE_SETARCH && applet_name[0] == 's'
  52. && argv[1] && is_prefixed_with(argv[1], "linux")
  53. ) {
  54. applet_name = argv[1];
  55. argv++;
  56. }
  57. if (applet_name[5] == '6') /* linux64 */
  58. pers = PER_LINUX;
  59. else if (applet_name[5] == '3') /* linux32 */
  60. pers = PER_LINUX32;
  61. else
  62. bb_show_usage();
  63. opts = getopt32(argv, "+R"); /* '+': stop at first non-option */
  64. if (opts)
  65. pers |= ADDR_NO_RANDOMIZE;
  66. /* Try to set personality */
  67. if (personality(pers) < 0)
  68. bb_perror_msg_and_die("personality(0x%lx)", pers);
  69. argv += optind;
  70. if (!argv[0])
  71. (--argv)[0] = (char*)"/bin/sh";
  72. /* Try to execute the program */
  73. BB_EXECVP_or_die(argv);
  74. }