3
0

openvt.c 909 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. /* leave current vt */
  23. if (setsid() < 0) {
  24. bb_perror_msg_and_die("setsid");
  25. }
  26. close(0); /* so that new vt becomes stdin */
  27. /* and grab new one */
  28. fd = xopen(vtname, O_RDWR);
  29. /* Reassign stdout and sterr */
  30. dup2(fd, STDOUT_FILENO);
  31. dup2(fd, STDERR_FILENO);
  32. execvp(argv[2], &argv[2]);
  33. _exit(1);
  34. }
  35. return EXIT_SUCCESS;
  36. }