3
0

openvt.c 5.2 KB

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