setarch.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 (3.4 kb)"
  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. //config:
  19. //config:config LINUX32
  20. //config: bool "linux32 (3.2 kb)"
  21. //config: default y
  22. //config: select PLATFORM_LINUX
  23. //config: help
  24. //config: Alias to "setarch linux32".
  25. //config:
  26. //config:config LINUX64
  27. //config: bool "linux64 (3.2 kb)"
  28. //config: default y
  29. //config: select PLATFORM_LINUX
  30. //config: help
  31. //config: Alias to "setarch linux64".
  32. //applet:IF_SETARCH(APPLET_NOEXEC(setarch, setarch, BB_DIR_BIN, BB_SUID_DROP, setarch))
  33. // APPLET_NOEXEC:name main location suid_type help
  34. //applet:IF_LINUX32(APPLET_NOEXEC(linux32, setarch, BB_DIR_BIN, BB_SUID_DROP, linux32))
  35. //applet:IF_LINUX64(APPLET_NOEXEC(linux64, setarch, BB_DIR_BIN, BB_SUID_DROP, linux64))
  36. //kbuild:lib-$(CONFIG_SETARCH) += setarch.o
  37. //kbuild:lib-$(CONFIG_LINUX32) += setarch.o
  38. //kbuild:lib-$(CONFIG_LINUX64) += setarch.o
  39. //usage:#define setarch_trivial_usage
  40. //usage: "PERSONALITY [-R] PROG ARGS"
  41. //usage:#define setarch_full_usage "\n\n"
  42. //usage: "PERSONALITY may be:"
  43. //usage: "\n"" linux32 Set 32bit uname emulation"
  44. //usage: "\n"" linux64 Set 64bit uname emulation"
  45. //usage: "\n"
  46. //usage: "\n"" -R Disable address space randomization"
  47. //usage:
  48. //usage:#define linux32_trivial_usage NOUSAGE_STR
  49. //usage:#define linux32_full_usage ""
  50. //usage:
  51. //usage:#define linux64_trivial_usage NOUSAGE_STR
  52. //usage:#define linux64_full_usage ""
  53. #include "libbb.h"
  54. #include <sys/personality.h>
  55. #ifndef ADDR_NO_RANDOMIZE
  56. # define ADDR_NO_RANDOMIZE 0x0040000
  57. #endif
  58. int setarch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  59. int setarch_main(int argc UNUSED_PARAM, char **argv)
  60. {
  61. unsigned opts;
  62. unsigned long pers;
  63. /* Figure out what personality we are supposed to switch to ...
  64. * we can be invoked as either:
  65. * argv[0],argv[1] == "setarch","personality"
  66. * argv[0] == "personality"
  67. */
  68. if (ENABLE_SETARCH && applet_name[0] == 's'
  69. && argv[1] && is_prefixed_with(argv[1], "linux")
  70. ) {
  71. argv++;
  72. applet_name = argv[0];
  73. }
  74. if ((!ENABLE_SETARCH && !ENABLE_LINUX32) || applet_name[5] == '6')
  75. /* linux64 */
  76. pers = PER_LINUX;
  77. else
  78. if ((!ENABLE_SETARCH && !ENABLE_LINUX64) || applet_name[5] == '3')
  79. /* linux32 */
  80. pers = PER_LINUX32;
  81. else
  82. bb_show_usage();
  83. opts = getopt32(argv, "+R"); /* '+': stop at first non-option */
  84. if (opts)
  85. pers |= ADDR_NO_RANDOMIZE;
  86. /* Try to set personality */
  87. if (personality(pers) < 0)
  88. bb_perror_msg_and_die("personality(0x%lx)", pers);
  89. argv += optind;
  90. if (!argv[0])
  91. (--argv)[0] = (char*)"/bin/sh";
  92. /* Try to execute the program */
  93. BB_EXECVP_or_die(argv);
  94. }