openvt.c 5.1 KB

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