openvt.c 4.5 KB

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