123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- #include <linux/vt.h>
- #include "libbb.h"
- static int not_vt_fd(int fd)
- {
- struct vt_stat vtstat;
- return ioctl(fd, VT_GETSTATE, &vtstat);
- }
- static int get_vt_fd(void)
- {
- int fd;
-
- for (fd = 0; fd < 3; fd++)
- if (!not_vt_fd(fd))
- return fd;
- fd = open(DEV_CONSOLE, O_RDONLY | O_NONBLOCK);
- if (fd >= 0 && !not_vt_fd(fd))
- return fd;
- bb_error_msg_and_die("can't find open VT");
- }
- static int find_free_vtno(void)
- {
- int vtno;
- int fd = get_vt_fd();
- errno = 0;
-
- if (ioctl(fd, VT_OPENQRY, &vtno) != 0 || vtno <= 0)
- bb_perror_msg_and_die("can't find open VT");
- return vtno;
- }
- static NOINLINE void vfork_child(char **argv)
- {
- if (vfork() == 0) {
-
-
- setsid();
- ioctl(STDIN_FILENO, TIOCSCTTY, 0 );
-
-
-
-
- BB_EXECVP_or_die(argv);
- }
- }
- int openvt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int openvt_main(int argc UNUSED_PARAM, char **argv)
- {
- char vtname[sizeof(VC_FORMAT) + sizeof(int)*3];
- struct vt_stat vtstat;
- char *str_c;
- int vtno;
- int flags;
- enum {
- OPT_c = (1 << 0),
- OPT_w = (1 << 1),
- OPT_s = (1 << 2),
- OPT_l = (1 << 3),
- OPT_f = (1 << 4),
- OPT_v = (1 << 5),
- };
-
- flags = getopt32(argv, "+c:wslfv", &str_c);
- argv += optind;
- if (flags & OPT_c) {
-
- vtno = xatou_range(str_c, 1, 63);
- } else {
- vtno = find_free_vtno();
- }
-
- sprintf(vtname, VC_FORMAT, vtno);
-
- bb_daemon_helper(DAEMON_CLOSE_EXTRA_FDS);
- close(STDIN_FILENO);
-
- xopen(vtname, O_RDWR);
- xioctl(STDIN_FILENO, VT_GETSTATE, &vtstat);
- if (flags & OPT_s) {
- console_make_active(STDIN_FILENO, vtno);
- }
- if (!argv[0]) {
- argv--;
- argv[0] = (char *) get_shell_name();
-
- }
- xdup2(STDIN_FILENO, STDOUT_FILENO);
- xdup2(STDIN_FILENO, STDERR_FILENO);
- #ifdef BLOAT
- {
-
- const char *prog = argv[0];
- if (flags & OPT_l)
- argv[0] = xasprintf("-%s", argv[0]);
- }
- #endif
- vfork_child(argv);
- if (flags & OPT_w) {
-
- safe_waitpid(-1, NULL, 0);
- if (flags & OPT_s) {
- console_make_active(STDIN_FILENO, vtstat.v_active);
-
-
-
- xioctl(STDIN_FILENO, VT_DISALLOCATE, (void*)(ptrdiff_t)vtno);
- }
- }
- return EXIT_SUCCESS;
- }
|