3
0

openvt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * openvt.c - open a vt to run a command.
  4. *
  5. * busyboxed by Quy Tonthat <quy@signal3.com>
  6. * hacked by Tito <farmatito@tiscali.it>
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. //usage:#define openvt_trivial_usage
  11. //usage: "[-c N] [-sw] [PROG ARGS]"
  12. //usage:#define openvt_full_usage "\n\n"
  13. //usage: "Start PROG on a new virtual terminal\n"
  14. //usage: "\n -c N Use specified VT"
  15. //usage: "\n -s Switch to the VT"
  16. /* //usage: "\n -l Run PROG as login shell (by prepending '-')" */
  17. //usage: "\n -w Wait for PROG to exit"
  18. //usage:
  19. //usage:#define openvt_example_usage
  20. //usage: "openvt 2 /bin/ash\n"
  21. #include <linux/vt.h>
  22. #include "libbb.h"
  23. /* "Standard" openvt's man page (we do not support all of this):
  24. openvt [-c NUM] [-fsulv] [--] [command [args]]
  25. Find the first available VT, and run command on it. Stdio is directed
  26. to that VT. If no command is specified then $SHELL is used.
  27. -c NUM
  28. Use the given VT number, not the first free one.
  29. -f
  30. Force opening a VT: don't try to check if VT is already in use.
  31. -s
  32. Switch to the new VT when starting the command.
  33. The VT of the new command will be made the new current VT.
  34. -u
  35. Figure out the owner of the current VT, and run login as that user.
  36. Suitable to be called by init. Shouldn't be used with -c or -l.
  37. -l
  38. Make the command a login shell: a "-" is prepended to the argv[0]
  39. when command is executed.
  40. -v
  41. Verbose.
  42. -w
  43. Wait for command to complete. If -w and -s are used together,
  44. switch back to the controlling terminal when the command completes.
  45. bbox:
  46. -u: not implemented
  47. -f: always in effect
  48. -l: not implemented, ignored
  49. -v: ignored
  50. -ws: does NOT switch back
  51. */
  52. /* Helper: does this fd understand VT_xxx? */
  53. static int not_vt_fd(int fd)
  54. {
  55. struct vt_stat vtstat;
  56. return ioctl(fd, VT_GETSTATE, &vtstat); /* !0: error, it's not VT fd */
  57. }
  58. /* Helper: get a fd suitable for VT_xxx */
  59. static int get_vt_fd(void)
  60. {
  61. int fd;
  62. /* Do we, by chance, already have it? */
  63. for (fd = 0; fd < 3; fd++)
  64. if (!not_vt_fd(fd))
  65. return fd;
  66. fd = open(DEV_CONSOLE, O_RDONLY | O_NONBLOCK);
  67. if (fd >= 0 && !not_vt_fd(fd))
  68. return fd;
  69. bb_error_msg_and_die("can't find open VT");
  70. }
  71. static int find_free_vtno(void)
  72. {
  73. int vtno;
  74. int fd = get_vt_fd();
  75. errno = 0;
  76. /*xfunc_error_retval = 3; - do we need compat? */
  77. if (ioctl(fd, VT_OPENQRY, &vtno) != 0 || vtno <= 0)
  78. bb_perror_msg_and_die("can't find open VT");
  79. // Not really needed, grep for DAEMON_ONLY_SANITIZE
  80. // if (fd > 2)
  81. // close(fd);
  82. return vtno;
  83. }
  84. /* vfork scares gcc, it generates bigger code.
  85. * Keep it away from main program.
  86. * TODO: move to libbb; or adapt existing libbb's spawn().
  87. */
  88. static NOINLINE void vfork_child(char **argv)
  89. {
  90. if (vfork() == 0) {
  91. /* CHILD */
  92. /* Try to make this VT our controlling tty */
  93. setsid(); /* lose old ctty */
  94. ioctl(STDIN_FILENO, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
  95. //bb_error_msg("our sid %d", getsid(0));
  96. //bb_error_msg("our pgrp %d", getpgrp());
  97. //bb_error_msg("VT's sid %d", tcgetsid(0));
  98. //bb_error_msg("VT's pgrp %d", tcgetpgrp(0));
  99. BB_EXECVP_or_die(argv);
  100. }
  101. }
  102. int openvt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  103. int openvt_main(int argc UNUSED_PARAM, char **argv)
  104. {
  105. char vtname[sizeof(VC_FORMAT) + sizeof(int)*3];
  106. struct vt_stat vtstat;
  107. char *str_c;
  108. int vtno;
  109. int flags;
  110. enum {
  111. OPT_c = (1 << 0),
  112. OPT_w = (1 << 1),
  113. OPT_s = (1 << 2),
  114. OPT_l = (1 << 3),
  115. OPT_f = (1 << 4),
  116. OPT_v = (1 << 5),
  117. };
  118. /* "+" - stop on first non-option */
  119. flags = getopt32(argv, "+c:wslfv", &str_c);
  120. argv += optind;
  121. if (flags & OPT_c) {
  122. /* Check for illegal vt number: < 1 or > 63 */
  123. vtno = xatou_range(str_c, 1, 63);
  124. } else {
  125. vtno = find_free_vtno();
  126. }
  127. /* Grab new VT */
  128. sprintf(vtname, VC_FORMAT, vtno);
  129. /* (Try to) clean up stray open fds above fd 2 */
  130. bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS | DAEMON_ONLY_SANITIZE, NULL);
  131. close(STDIN_FILENO);
  132. /*setsid(); - BAD IDEA: after we exit, child is SIGHUPed... */
  133. xopen(vtname, O_RDWR);
  134. xioctl(STDIN_FILENO, VT_GETSTATE, &vtstat);
  135. if (flags & OPT_s) {
  136. console_make_active(STDIN_FILENO, vtno);
  137. }
  138. if (!argv[0]) {
  139. argv--;
  140. argv[0] = (char *) get_shell_name();
  141. /*argv[1] = NULL; - already is */
  142. }
  143. xdup2(STDIN_FILENO, STDOUT_FILENO);
  144. xdup2(STDIN_FILENO, STDERR_FILENO);
  145. #ifdef BLOAT
  146. {
  147. /* Handle -l (login shell) option */
  148. const char *prog = argv[0];
  149. if (flags & OPT_l)
  150. argv[0] = xasprintf("-%s", argv[0]);
  151. }
  152. #endif
  153. vfork_child(argv);
  154. if (flags & OPT_w) {
  155. /* We have only one child, wait for it */
  156. safe_waitpid(-1, NULL, 0); /* loops on EINTR */
  157. if (flags & OPT_s) {
  158. console_make_active(STDIN_FILENO, vtstat.v_active);
  159. // Compat: even with -c N (try to) disallocate:
  160. // # /usr/app/kbd-1.12/bin/openvt -f -c 9 -ws sleep 5
  161. // openvt: could not deallocate console 9
  162. xioctl(STDIN_FILENO, VT_DISALLOCATE, (void*)(ptrdiff_t)vtno);
  163. }
  164. }
  165. return EXIT_SUCCESS;
  166. }