openvt.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /* _only_ O_NONBLOCK: ask for neither read nor write perms */
  56. /*FIXME: use? device_open(DEV_CONSOLE,0); */
  57. fd = open(DEV_CONSOLE, O_NONBLOCK);
  58. if (fd >= 0 && !not_vt_fd(fd))
  59. return fd;
  60. bb_error_msg_and_die("can't find open VT");
  61. }
  62. static int find_free_vtno(void)
  63. {
  64. int vtno;
  65. int fd = get_vt_fd();
  66. errno = 0;
  67. /*xfunc_error_retval = 3; - do we need compat? */
  68. if (ioctl(fd, VT_OPENQRY, &vtno) != 0 || vtno <= 0)
  69. bb_perror_msg_and_die("can't find open VT");
  70. // Not really needed, grep for DAEMON_ONLY_SANITIZE
  71. // if (fd > 2)
  72. // close(fd);
  73. return vtno;
  74. }
  75. /* vfork scares gcc, it generates bigger code.
  76. * Keep it away from main program.
  77. * TODO: move to libbb; or adapt existing libbb's spawn().
  78. */
  79. static NOINLINE void vfork_child(char **argv)
  80. {
  81. if (vfork() == 0) {
  82. /* CHILD */
  83. /* Try to make this VT our controlling tty */
  84. setsid(); /* lose old ctty */
  85. ioctl(STDIN_FILENO, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
  86. //bb_error_msg("our sid %d", getsid(0));
  87. //bb_error_msg("our pgrp %d", getpgrp());
  88. //bb_error_msg("VT's sid %d", tcgetsid(0));
  89. //bb_error_msg("VT's pgrp %d", tcgetpgrp(0));
  90. BB_EXECVP(argv[0], argv);
  91. bb_perror_msg_and_die("exec %s", argv[0]);
  92. }
  93. }
  94. int openvt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  95. int openvt_main(int argc ATTRIBUTE_UNUSED, char **argv)
  96. {
  97. char vtname[sizeof(VC_FORMAT) + sizeof(int)*3];
  98. struct vt_stat vtstat;
  99. char *str_c;
  100. int vtno;
  101. int flags;
  102. enum {
  103. OPT_c = (1 << 0),
  104. OPT_w = (1 << 1),
  105. OPT_s = (1 << 2),
  106. OPT_l = (1 << 3),
  107. OPT_f = (1 << 4),
  108. OPT_v = (1 << 5),
  109. };
  110. /* "+" - stop on first non-option */
  111. flags = getopt32(argv, "+c:wslfv", &str_c);
  112. argv += optind;
  113. if (flags & OPT_c) {
  114. /* Check for illegal vt number: < 1 or > 63 */
  115. vtno = xatou_range(str_c, 1, 63);
  116. } else {
  117. vtno = find_free_vtno();
  118. }
  119. /* Grab new VT */
  120. sprintf(vtname, VC_FORMAT, vtno);
  121. /* (Try to) clean up stray open fds above fd 2 */
  122. bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS | DAEMON_ONLY_SANITIZE, NULL);
  123. close(STDIN_FILENO);
  124. /*setsid(); - BAD IDEA: after we exit, child is SIGHUPed... */
  125. xopen(vtname, O_RDWR);
  126. xioctl(STDIN_FILENO, VT_GETSTATE, &vtstat);
  127. if (flags & OPT_s) {
  128. console_make_active(STDIN_FILENO, vtno);
  129. }
  130. if (!argv[0]) {
  131. argv--;
  132. argv[0] = getenv("SHELL");
  133. if (!argv[0])
  134. argv[0] = (char *) DEFAULT_SHELL;
  135. /*argv[1] = NULL; - already is */
  136. }
  137. xdup2(STDIN_FILENO, STDOUT_FILENO);
  138. xdup2(STDIN_FILENO, STDERR_FILENO);
  139. #ifdef BLOAT
  140. {
  141. /* Handle -l (login shell) option */
  142. const char *prog = argv[0];
  143. if (flags & OPT_l)
  144. argv[0] = xasprintf("-%s", argv[0]);
  145. }
  146. #endif
  147. vfork_child(argv);
  148. if (flags & OPT_w) {
  149. /* We have only one child, wait for it */
  150. safe_waitpid(-1, NULL, 0); /* loops on EINTR */
  151. if (flags & OPT_s) {
  152. console_make_active(STDIN_FILENO, vtstat.v_active);
  153. // Compat: even with -c N (try to) disallocate:
  154. // # /usr/app/kbd-1.12/bin/openvt -f -c 9 -ws sleep 5
  155. // openvt: could not deallocate console 9
  156. xioctl(STDIN_FILENO, VT_DISALLOCATE, (void*)(ptrdiff_t)vtno);
  157. }
  158. }
  159. return EXIT_SUCCESS;
  160. }