setarch.c 3.0 KB

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