123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- #include "libbb.h"
- #if !defined(__linux__) && !defined(TIOCGSERIAL) && !ENABLE_WERROR
- # warning cttyhack will not be able to detect a controlling tty on this system
- #endif
- struct vt_stat {
- unsigned short v_active;
- unsigned short v_signal;
- unsigned short v_state;
- };
- enum { VT_GETSTATE = 0x5603 };
- struct serial_struct {
- int type;
- int line;
- unsigned int port;
- int irq;
- int flags;
- int xmit_fifo_size;
- int custom_divisor;
- int baud_base;
- unsigned short close_delay;
- char io_type;
- char reserved_char[1];
- int hub6;
- unsigned short closing_wait;
- unsigned short closing_wait2;
- unsigned char *iomem_base;
- unsigned short iomem_reg_shift;
- unsigned int port_high;
- unsigned long iomap_base;
- int reserved[1];
- };
- int cttyhack_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int cttyhack_main(int argc UNUSED_PARAM, char **argv)
- {
- int fd;
- char console[sizeof(int)*3 + 16];
- union {
- struct vt_stat vt;
- struct serial_struct sr;
- char paranoia[sizeof(struct serial_struct) * 3];
- } u;
- strcpy(console, "/dev/tty");
- fd = open(console, O_RDWR);
- if (fd < 0) {
-
- do {
- #ifdef __linux__
-
- int s = open_read_close("/sys/class/tty/console/active",
- console + 5, sizeof(console) - 5);
- if (s > 0) {
- char *last;
-
- console[4 + s] = '\0';
-
- last = strrchr(console + 5, ' ');
- if (last)
- overlapping_strcpy(console + 5, last + 1);
- break;
- }
- if (ioctl(0, VT_GETSTATE, &u.vt) == 0) {
-
- sprintf(console + 8, "S%u" + 1, (int)u.vt.v_active);
- break;
- }
- #endif
- #ifdef TIOCGSERIAL
- if (ioctl(0, TIOCGSERIAL, &u.sr) == 0) {
-
- sprintf(console + 8, "S%u", (int)u.sr.line);
- break;
- }
- #endif
-
- console[0] = '\0';
- } while (0);
- }
- argv++;
- if (!argv[0]) {
- if (!console[0])
- return EXIT_FAILURE;
- puts(console);
- return EXIT_SUCCESS;
- }
- if (fd < 0) {
- fd = open_or_warn(console, O_RDWR);
- if (fd < 0)
- goto ret;
- }
-
- dup2(fd, 0);
- dup2(fd, 1);
- dup2(fd, 2);
- while (fd > 2)
- close(fd--);
-
- ioctl(0, TIOCSCTTY, 1);
- ret:
- BB_EXECVP_or_die(argv);
- }
|