openvt.c 902 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. /* getopt not needed */
  11. #include "busybox.h"
  12. int openvt_main(int argc, char **argv)
  13. {
  14. int fd;
  15. char vtname[sizeof(VC_FORMAT) + 2];
  16. if (argc < 3) {
  17. bb_show_usage();
  18. }
  19. /* check for illegal vt number: < 1 or > 63 */
  20. sprintf(vtname, VC_FORMAT, (int)xatoul_range(argv[1], 1, 63));
  21. if (fork() == 0) {
  22. /* child */
  23. /* leave current vt (controlling tty) */
  24. setsid();
  25. /* and grab new one */
  26. fd = xopen(vtname, O_RDWR);
  27. /* Reassign stdin, stdout and sterr */
  28. dup2(fd, STDIN_FILENO);
  29. dup2(fd, STDOUT_FILENO);
  30. dup2(fd, STDERR_FILENO);
  31. while (fd > 2) close(fd--);
  32. execvp(argv[2], &argv[2]);
  33. _exit(1);
  34. }
  35. return EXIT_SUCCESS;
  36. }