cttyhack.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* This code is adapted from busybox project
  2. *
  3. * Licensed under GPLv2
  4. */
  5. #include "libbb.h"
  6. /* From <linux/vt.h> */
  7. struct vt_stat {
  8. unsigned short v_active; /* active vt */
  9. unsigned short v_signal; /* signal to send */
  10. unsigned short v_state; /* vt bitmask */
  11. };
  12. enum { VT_GETSTATE = 0x5603 }; /* get global vt state info */
  13. /* From <linux/serial.h> */
  14. struct serial_struct {
  15. int type;
  16. int line;
  17. unsigned int port;
  18. int irq;
  19. int flags;
  20. int xmit_fifo_size;
  21. int custom_divisor;
  22. int baud_base;
  23. unsigned short close_delay;
  24. char io_type;
  25. char reserved_char[1];
  26. int hub6;
  27. unsigned short closing_wait; /* time to wait before closing */
  28. unsigned short closing_wait2; /* no longer used... */
  29. unsigned char *iomem_base;
  30. unsigned short iomem_reg_shift;
  31. unsigned int port_high;
  32. unsigned long iomap_base; /* cookie passed into ioremap */
  33. int reserved[1];
  34. };
  35. int cttyhack_main(int argc, char **argv) ATTRIBUTE_NORETURN;
  36. int cttyhack_main(int argc, char **argv)
  37. {
  38. int fd;
  39. char console[sizeof(int)*3 + 16];
  40. union {
  41. struct vt_stat vt;
  42. struct serial_struct sr;
  43. char paranoia[sizeof(struct serial_struct) * 3];
  44. } u;
  45. if (!*++argv) {
  46. bb_show_usage();
  47. }
  48. strcpy(console, "/dev/tty");
  49. if (ioctl(0, TIOCGSERIAL, &u.sr) == 0) {
  50. /* this is a serial console */
  51. sprintf(console + 8, "S%d", u.sr.line);
  52. } else if (ioctl(0, VT_GETSTATE, &u.vt) == 0) {
  53. /* this is linux virtual tty */
  54. sprintf(console + 8, "S%d" + 1, u.vt.v_active);
  55. }
  56. if (console[8]) {
  57. fd = xopen(console, O_RDWR);
  58. //bb_error_msg("switching to '%s'", console);
  59. dup2(fd, 0);
  60. dup2(fd, 1);
  61. dup2(fd, 2);
  62. while (fd > 2) close(fd--);
  63. }
  64. execvp(argv[0], argv);
  65. bb_perror_msg_and_die("cannot exec '%s'", argv[0]);
  66. }