setarch.c 3.0 KB

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