diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-03-27 16:26:35 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-03-27 16:26:35 +0000 |
commit | 95891fc016c9911406cb826540873f0894bcbe8b (patch) | |
tree | 6bd21013d32cd589946742000c021ad95de61634 /console-tools/openvt.c | |
parent | 72b3442aeb948a9f27822d8888df765393b9ecd4 (diff) | |
download | busybox-w32-95891fc016c9911406cb826540873f0894bcbe8b.tar.gz busybox-w32-95891fc016c9911406cb826540873f0894bcbe8b.tar.bz2 busybox-w32-95891fc016c9911406cb826540873f0894bcbe8b.zip |
openvt: fix gross mismatch between us and "standard" openvt.
standard one even has different syntax!
std: "openvt -c 12", we: "openvt 12"
std: "openvt top", we: complain that "top" is not a number.
openvt: implement -c -w -s (-l -f -v are also accepted but ingnored)
openvt_main 188 343 +155
vfork_child - 67 +67
not_vt_fd - 23 +23
packed_usage 23932 23952 +20
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 2/0 up/down: 265/0) Total: 265 bytes
text data bss dec hex filename
801344 641 7380 809365 c5995 busybox_old
801617 641 7380 809638 c5aa6 busybox_unstripped
Diffstat (limited to 'console-tools/openvt.c')
-rw-r--r-- | console-tools/openvt.c | 161 |
1 files changed, 148 insertions, 13 deletions
diff --git a/console-tools/openvt.c b/console-tools/openvt.c index 39b985995..6f2aa19c4 100644 --- a/console-tools/openvt.c +++ b/console-tools/openvt.c | |||
@@ -8,29 +8,164 @@ | |||
8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
9 | */ | 9 | */ |
10 | 10 | ||
11 | /* getopt not needed */ | 11 | #include <linux/vt.h> |
12 | |||
13 | #include "libbb.h" | 12 | #include "libbb.h" |
14 | 13 | ||
14 | /* "Standard" openvt's man page (we do not support all of this): | ||
15 | |||
16 | openvt [-c NUM] [-fsulv] [--] [command [args]] | ||
17 | |||
18 | Find the first available VT, and run command on it. Stdio is directed | ||
19 | to that VT. If no command is specified then $SHELL is used. | ||
20 | |||
21 | -c NUM | ||
22 | Use the given VT number, not the first free one. | ||
23 | -f | ||
24 | Force opening a VT: don't try to check if VT is already in use. | ||
25 | -s | ||
26 | Switch to the new VT when starting the command. | ||
27 | The VT of the new command will be made the new current VT. | ||
28 | -u | ||
29 | Figure out the owner of the current VT, and run login as that user. | ||
30 | Suitable to be called by init. Shouldn't be used with -c or -l. | ||
31 | -l | ||
32 | Make the command a login shell: a "-" is prepended to the argv[0] | ||
33 | when command is executed. | ||
34 | -v | ||
35 | Verbose. | ||
36 | -w | ||
37 | Wait for command to complete. If -w and -s are used together, | ||
38 | switch back to the controlling terminal when the command completes. | ||
39 | |||
40 | bbox: | ||
41 | -u: not implemented | ||
42 | -f: always in effect | ||
43 | -l: not implemented, ignored | ||
44 | -v: ignored | ||
45 | -ws: does NOT switch back | ||
46 | */ | ||
47 | |||
48 | /* Helper: does this fd understand VT_xxx? */ | ||
49 | static int not_vt_fd(int fd) | ||
50 | { | ||
51 | struct vt_stat vtstat; | ||
52 | return ioctl(fd, VT_GETSTATE, &vtstat); /* !0: error, it's not VT fd */ | ||
53 | } | ||
54 | |||
55 | /* Helper: get a fd suitable for VT_xxx */ | ||
56 | static int get_vt_fd(void) | ||
57 | { | ||
58 | int fd; | ||
59 | |||
60 | /* Do we, by chance, already have it? */ | ||
61 | for (fd = 0; fd < 3; fd++) | ||
62 | if (!not_vt_fd(fd)) | ||
63 | return fd; | ||
64 | /* _only_ O_NONBLOCK: ask for neither read not write perms */ | ||
65 | fd = open(DEV_CONSOLE, O_NONBLOCK); | ||
66 | if (fd >= 0 && !not_vt_fd(fd)) | ||
67 | return fd; | ||
68 | bb_error_msg_and_die("can't find open VT"); | ||
69 | } | ||
70 | |||
71 | static int find_free_vtno(void) | ||
72 | { | ||
73 | int vtno; | ||
74 | int fd = get_vt_fd(); | ||
75 | |||
76 | errno = 0; | ||
77 | /*xfunc_error_retval = 3; - do we need compat? */ | ||
78 | if (ioctl(fd, VT_OPENQRY, &vtno) != 0 || vtno <= 0) | ||
79 | bb_perror_msg_and_die("can't find open VT"); | ||
80 | // Not really needed, grep for DAEMON_ONLY_SANITIZE | ||
81 | // if (fd > 2) | ||
82 | // close(fd); | ||
83 | return vtno; | ||
84 | } | ||
85 | |||
86 | /* vfork scares gcc, it generates bigger code. | ||
87 | * Keep it away from main program. | ||
88 | * TODO: move to libbb; or adapt existing libbb's spawn(). | ||
89 | */ | ||
90 | static NOINLINE void vfork_child(char **argv) | ||
91 | { | ||
92 | if (vfork() == 0) { | ||
93 | /* CHILD */ | ||
94 | /* Try to make this VT our controlling tty */ | ||
95 | setsid(); /* lose old ctty */ | ||
96 | ioctl(0, TIOCSCTTY, 0 /* 0: don't forcibly steal */); | ||
97 | //bb_error_msg("our sid %d", getsid(0)); | ||
98 | //bb_error_msg("our pgrp %d", getpgrp()); | ||
99 | //bb_error_msg("VT's sid %d", tcgetsid(0)); | ||
100 | //bb_error_msg("VT's pgrp %d", tcgetpgrp(0)); | ||
101 | BB_EXECVP(argv[0], argv); | ||
102 | bb_perror_msg_and_die("exec %s", argv[0]); | ||
103 | } | ||
104 | } | ||
105 | |||
15 | int openvt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 106 | int openvt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
16 | int openvt_main(int argc, char **argv) | 107 | int openvt_main(int argc ATTRIBUTE_UNUSED, char **argv) |
17 | { | 108 | { |
18 | char vtname[sizeof(VC_FORMAT) + 2]; | 109 | char vtname[sizeof(VC_FORMAT) + sizeof(int)*3]; |
110 | char *str_c; | ||
111 | int vtno; | ||
112 | int flags; | ||
113 | enum { | ||
114 | OPT_c = (1 << 0), | ||
115 | OPT_w = (1 << 1), | ||
116 | OPT_s = (1 << 2), | ||
117 | OPT_l = (1 << 3), | ||
118 | OPT_f = (1 << 4), | ||
119 | OPT_v = (1 << 5), | ||
120 | }; | ||
19 | 121 | ||
20 | if (argc < 3) | 122 | /* "+" - stop on first non-option */ |
21 | bb_show_usage(); | 123 | flags = getopt32(argv, "+c:wslfv", &str_c); |
124 | argv += optind; | ||
22 | 125 | ||
23 | /* check for illegal vt number: < 1 or > 63 */ | 126 | if (flags & OPT_c) { |
24 | sprintf(vtname, VC_FORMAT, (int)xatou_range(argv[1], 1, 63)); | 127 | /* Check for illegal vt number: < 1 or > 63 */ |
128 | vtno = xatou_range(str_c, 1, 63); | ||
129 | } else { | ||
130 | vtno = find_free_vtno(); | ||
131 | } | ||
25 | 132 | ||
26 | bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv); | 133 | /* Grab new VT */ |
27 | /* grab new one */ | 134 | sprintf(vtname, VC_FORMAT, vtno); |
135 | /* (Try to) clean up stray open fds above fd 2 */ | ||
136 | bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS | DAEMON_ONLY_SANITIZE, NULL); | ||
28 | close(0); | 137 | close(0); |
138 | /*setsid(); - BAD IDEA: after we exit, child is SIGHUPed... */ | ||
29 | xopen(vtname, O_RDWR); | 139 | xopen(vtname, O_RDWR); |
140 | |||
141 | if (flags & OPT_s) { | ||
142 | xioctl(0, VT_ACTIVATE, (void*)(ptrdiff_t)vtno); | ||
143 | xioctl(0, VT_WAITACTIVE, (void*)(ptrdiff_t)vtno); | ||
144 | } | ||
145 | |||
146 | if (!argv[0]) { | ||
147 | argv--; | ||
148 | argv[0] = getenv("SHELL"); | ||
149 | if (!argv[0]) | ||
150 | argv[0] = (char *) DEFAULT_SHELL; | ||
151 | /*argv[1] = NULL; - already is */ | ||
152 | } | ||
153 | |||
30 | xdup2(0, STDOUT_FILENO); | 154 | xdup2(0, STDOUT_FILENO); |
31 | xdup2(0, STDERR_FILENO); | 155 | xdup2(0, STDERR_FILENO); |
32 | 156 | ||
33 | argv += 2; | 157 | #ifdef BLOAT |
34 | BB_EXECVP(argv[0], argv); | 158 | /* Handle -l (login shell) option */ |
35 | _exit(1); | 159 | const char *prog = argv[0]; |
160 | if (flags & OPT_l) | ||
161 | argv[0] = xasprintf("-%s", argv[0]); | ||
162 | #endif | ||
163 | |||
164 | vfork_child(argv); | ||
165 | if (flags & OPT_w) { | ||
166 | wait(NULL); | ||
167 | // TODO: -ws handling should be here | ||
168 | } | ||
169 | |||
170 | return EXIT_SUCCESS; | ||
36 | } | 171 | } |