comparison toys/pending/openvt.c @ 1358:310165c2f451 draft

Move deallocvt.c into openvt.c, cleanup both.
author Rob Landley <rob@landley.net>
date Tue, 24 Jun 2014 06:42:08 -0500
parents 85f297591693
children
comparison
equal deleted inserted replaced
1357:e01ae62fcac5 1358:310165c2f451
3 * Copyright 2014 Vivek Kumar Bhagat <vivek.bhagat89@gmail.com> 3 * Copyright 2014 Vivek Kumar Bhagat <vivek.bhagat89@gmail.com>
4 * 4 *
5 * No Standard 5 * No Standard
6 6
7 USE_OPENVT(NEWTOY(openvt, "c#<1>63sw", TOYFLAG_BIN|TOYFLAG_NEEDROOT)) 7 USE_OPENVT(NEWTOY(openvt, "c#<1>63sw", TOYFLAG_BIN|TOYFLAG_NEEDROOT))
8 USE_DEALLOCVT(NEWTOY(deallocvt, ">1", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_NEEDROOT))
8 9
9 config OPENVT 10 config OPENVT
10 bool "openvt" 11 bool "openvt"
11 default n 12 default n
12 help 13 help
13 usage: openvt [-c N] [-s] [-w] [--] [command [command_options]] 14 usage: openvt [-c N] [-sw] [command [command_options]]
14 15
15 start a program on a new virtual terminal (VT) 16 start a program on a new virtual terminal (VT)
16 17
17 -c N Use VT N 18 -c N Use VT N
18 -s Switch to new VT 19 -s Switch to new VT
19 -w Wait for command to exit 20 -w Wait for command to exit
20 if -s and -w option used together, switch back 21
21 to originating VT when command completes 22 if -sw used together, switch back to originating VT when command completes
23
24 config DEALLOCVT
25 bool "deallocvt"
26 default n
27 help
28 usage: deallocvt [N]
29
30 Deallocate unused virtual terminal /dev/ttyN, or all unused consoles.
22 */ 31 */
23 32
24 #define FOR_openvt 33 #define FOR_openvt
25 #include "toys.h" 34 #include "toys.h"
26 #include <linux/vt.h> 35 #include <linux/vt.h>
28 37
29 GLOBALS( 38 GLOBALS(
30 unsigned long vt_num; 39 unsigned long vt_num;
31 ) 40 )
32 41
33 int find_console_fd(void) 42 int open_console(void)
34 { 43 {
35 char *console_name[] = {"/dev/tty", "/dev/tty0", "/dev/console"}; 44 char arg, *console_name[] = {"/dev/tty", "/dev/tty0", "/dev/console"};
36 int i; 45 int i, fd;
37 int fd;
38 char arg;
39 46
40 for (i = 0; i < 3; i++) { 47 for (i = 0; i < ARRAY_LEN(console_name); i++) {
41 fd = open(console_name[i], O_RDONLY); 48 fd = open(console_name[i], O_RDWR);
42 if (fd < 0 && errno == EACCES)
43 fd = open(console_name[i], O_WRONLY);
44
45 if (fd >= 0) { 49 if (fd >= 0) {
46 arg = 0; 50 arg = 0;
47 if (0 == ioctl(fd, KDGKBTYPE, &arg)) 51 if (!ioctl(fd, KDGKBTYPE, &arg)) return fd;
48 return fd; 52 close(fd);
49 else
50 close(fd);
51 } 53 }
52 } 54 }
53 55
54 /* check std fd 0, 1 and 2 */ 56 /* check std fd 0, 1 and 2 */
55 for (fd = 0; fd < 3; fd++) { 57 for (fd = 0; fd < 3; fd++) {
56 arg = 0; 58 arg = 0;
57 if (0 == ioctl(fd, KDGKBTYPE, &arg)) 59 if (0 == ioctl(fd, KDGKBTYPE, &arg)) return fd;
58 return fd;
59 } 60 }
60 61
61 return -1; 62 return -1;
62 } 63 }
63 64
71 return TT.vt_num; 72 return TT.vt_num;
72 } 73 }
73 74
74 void openvt_main(void) 75 void openvt_main(void)
75 { 76 {
76 int fd = -1, vt_fd = -1, pid, ret = 0; 77 int fd, vt_fd, ret = 0;
77 struct vt_stat vstate; 78 struct vt_stat vstate;
79 pid_t pid;
78 80
79 if (!(toys.optflags & FLAG_c)) { 81 if (!(toys.optflags & FLAG_c)) {
80 // check if fd 0,1 or 2 is already opened 82 // check if fd 0,1 or 2 is already opened
81 for (fd = 0; fd < 3; fd++) 83 for (fd = 0; fd < 3; fd++)
82 if (!ioctl(fd, VT_GETSTATE, &vstate)) { 84 if (!ioctl(fd, VT_GETSTATE, &vstate)) {
91 xvtnum(fd); 93 xvtnum(fd);
92 } 94 }
93 } 95 }
94 96
95 sprintf(toybuf, "/dev/tty%lu", TT.vt_num); 97 sprintf(toybuf, "/dev/tty%lu", TT.vt_num);
96 fd = find_console_fd(); 98 fd = open_console();
97 xioctl(fd, VT_GETSTATE, &vstate); 99 xioctl(fd, VT_GETSTATE, &vstate);
98 100
99 close(0); //new vt becomes stdin 101 close(0); //new vt becomes stdin
100 vt_fd = xopen(toybuf, O_RDWR); 102 vt_fd = xopen(toybuf, O_RDWR);
101 if (toys.optflags & FLAG_s) { 103 if (toys.optflags & FLAG_s) {
126 //check why deallocate isn't working here 128 //check why deallocate isn't working here
127 xioctl(fd, VT_DISALLOCATE, (void *)(ptrdiff_t)TT.vt_num); 129 xioctl(fd, VT_DISALLOCATE, (void *)(ptrdiff_t)TT.vt_num);
128 } 130 }
129 } 131 }
130 } 132 }
133
134 void deallocvt_main(void)
135 {
136 long vt_num = 0; // 0 deallocates all unused consoles
137 int fd;
138
139 if (*toys.optargs) vt_num = atolx_range(*toys.optargs, 1, 63);
140
141 if ((fd = open_console()) < 0) error_exit("can't open console");
142 xioctl(fd, VT_DISALLOCATE, (void *)vt_num);
143 if (CFG_TOYBOX_FREE) close(fd);
144 }