File: 0004-New-option-to-change-VT.patch

package info (click to toggle)
tuxonice-userui 1.1%2Bdfsg1.gc3bdd83-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 636 kB
  • ctags: 1,853
  • sloc: ansic: 5,788; makefile: 91; sh: 63; python: 46
file content (280 lines) | stat: -rw-r--r-- 7,914 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
From: Julien Muchembled <jm@jmuchemb.eu>
Date: Mon, 23 Dec 2013 06:30:06 +0100
Subject: New option to change VT

When invoked with -vN (or --chvt=N) option, userui makes /dev/ttyN the
foreground terminal before doing anything else, and restore previous VT at exit.
If /dev/ttyN is already the foreground terminal, the new option does nothing.
There's also no change in behaviour if this new option is not passed.

TOI UI needs its own VT and without such option, it would be required
to work around this in every power management hibernation.
---
 fbsplash/cmd.c                  | 11 ++++----
 fbsplash/common.c               |  1 -
 fbsplash/splash.h               |  1 -
 fbsplash/userui_fbsplash_core.c | 22 ----------------
 userui.h                        |  1 +
 userui_core.c                   | 56 +++++++++++++++++++++++++++++++++++++----
 userui_text.c                   |  2 --
 7 files changed, 58 insertions(+), 36 deletions(-)

diff --git a/fbsplash/cmd.c b/fbsplash/cmd.c
index 2672d69..0a418df 100644
--- a/fbsplash/cmd.c
+++ b/fbsplash/cmd.c
@@ -25,6 +25,7 @@
 #include <errno.h>
 #include <linux/fb.h>
 #include "splash.h"
+#include "../userui.h"
 
 #ifdef CONFIG_FBSPLASH
 #include <linux/console_splash.h>
@@ -32,7 +33,7 @@
 void cmd_setstate(unsigned int state, unsigned char origin)
 {
 	struct fb_splash_iowrapper wrapper = {
-		.vc = arg_vc,
+		.vc = suspend_vt,
 		.origin = origin,
 		.data = &state,
 	};
@@ -46,7 +47,7 @@ void cmd_setstate(unsigned int state, unsigned char origin)
 void cmd_setpic(struct fb_image *img, unsigned char origin)
 {
 	struct fb_splash_iowrapper wrapper = {
-		.vc = arg_vc,
+		.vc = suspend_vt,
 		.origin = origin,
 		.data = img,
 	};
@@ -60,7 +61,7 @@ void cmd_setcfg(unsigned char origin)
 {
 	struct vc_splash vc_cfg;
 	struct fb_splash_iowrapper wrapper = {
-		.vc = arg_vc,
+		.vc = suspend_vt,
 		.origin = origin,
 		.data = &vc_cfg,
 	};
@@ -82,7 +83,7 @@ void cmd_getcfg()
 {
 	struct vc_splash vc_cfg;
 	struct fb_splash_iowrapper wrapper = {
-		.vc = arg_vc,
+		.vc = suspend_vt,
 		.origin = FB_SPLASH_IO_ORIG_USER,
 		.data = &vc_cfg,
 	};
@@ -97,7 +98,7 @@ void cmd_getcfg()
 		strcpy(vc_cfg.theme, "<none>");
 	} 
 		
-	printf("Splash config on console %d:\n", arg_vc);
+	printf("Splash config on console %d:\n", suspend_vt);
 	printf("tx:       %d\n", vc_cfg.tx);
 	printf("ty:	  %d\n", vc_cfg.ty);
 	printf("twidth:	  %d\n", vc_cfg.twidth);
diff --git a/fbsplash/common.c b/fbsplash/common.c
index fb34c7c..370007d 100644
--- a/fbsplash/common.c
+++ b/fbsplash/common.c
@@ -37,7 +37,6 @@ char *config_file = NULL;
 
 enum TASK arg_task = none; 
 int arg_fb = 0;
-int arg_vc = 0;
 char arg_mode = 'v';
 char *arg_theme = NULL;
 u16 arg_progress = 0;
diff --git a/fbsplash/splash.h b/fbsplash/splash.h
index 4b37ff1..bd0d100 100644
--- a/fbsplash/splash.h
+++ b/fbsplash/splash.h
@@ -265,7 +265,6 @@ extern struct fb_fix_screeninfo   fb_fix;
 extern enum ENDIANESS endianess;
 extern enum TASK arg_task;
 extern int arg_fb;
-extern int arg_vc;
 extern char *arg_theme;
 extern char arg_mode;
 extern u16 arg_progress;
diff --git a/fbsplash/userui_fbsplash_core.c b/fbsplash/userui_fbsplash_core.c
index 2a4099b..09f1504 100644
--- a/fbsplash/userui_fbsplash_core.c
+++ b/fbsplash/userui_fbsplash_core.c
@@ -67,27 +67,6 @@ static void silent_off() {
 	show_cursor();
 }
 
-static int get_active_vt() {
-	int vt, fd;
-	struct vt_stat vt_stat;
-
-	vt = 62; /* default */
-
-	if ((fd = open("/dev/tty0", O_RDONLY)) == -1)
-		goto out;
-
-	if (ioctl(fd, VT_GETSTATE, &vt_stat) == -1)
-		goto out;
-
-	vt = vt_stat.v_active - 1;
-
-out:
-	if (fd >= 0)
-		close(fd);
-
-	return vt;
-}
-
 static int fbsplash_load() {
 	fb_fd = -1;
 	last_pos = 0;
@@ -103,7 +82,6 @@ static int fbsplash_load() {
 		return 1;
 	}
 
-	arg_vc = get_active_vt();
 	arg_mode = 's';
 
 	/* Read theme config file */
diff --git a/userui.h b/userui.h
index ca59826..920f704 100644
--- a/userui.h
+++ b/userui.h
@@ -53,6 +53,7 @@ void printk(char *msg, ...);
 
 extern char software_suspend_version[32];
 extern int can_use_escape;
+extern int suspend_vt;
 extern volatile __uint32_t console_loglevel;
 extern volatile __uint32_t suspend_action;
 extern volatile __uint32_t suspend_debug;
diff --git a/userui_core.c b/userui_core.c
index 74d7aa6..5e3f713 100644
--- a/userui_core.c
+++ b/userui_core.c
@@ -96,6 +96,8 @@ static void might_switch_ops(void)
 }
 
 static int netlink_socket_num = 0;
+static int saved_vt = 0;
+int suspend_vt = 0;
 
 static char* descriptions[] = {
 	"General",
@@ -373,7 +375,7 @@ int common_keypress_handler(int key)
 }
 
 static void handle_params(int argc, char **argv) {
-	static char global_optstring[] = "htc:fu";
+	static char global_optstring[] = "htc:fuv:";
 	static struct option global_longopts[] = {
 		{"help", 0, 0, 'h'},
 		{"test", 0, 0, 't'},
@@ -382,6 +384,7 @@ static void handle_params(int argc, char **argv) {
 #ifdef USE_USPLASH
 		{"usplash", 0, 0, 'u'},
 #endif
+		{"chvt", 1, 0, 'v'},
 		{NULL, 0, 0, 0},
 	};
 
@@ -437,6 +440,9 @@ static void handle_params(int argc, char **argv) {
 			case 't':
 				test_run++;
 				break;
+			case 'v':
+				suspend_vt = atoi(optarg);
+				break;
 			case 'h':
 				fprintf(stderr,
 "Usage: %s [options]\n"
@@ -445,6 +451,9 @@ static void handle_params(int argc, char **argv) {
 "     Specifying -t once will give an demo of this module.\n"
 "     Specifying -t twice will make the demo run as fast as it can.\n"
 "     (useful for performance testing).\n"
+"  -v, --chvt=N\n"
+"     Makes /dev/ttyN the foreground terminal before doing anything else\n"
+"     and restore previous VT at the end.\n"
 #ifdef USE_USPLASH
 "  -u\n"
 "     Use usersplash interface by default.\n"
@@ -603,6 +612,27 @@ static void enforce_lifesavers() {
 	setrlimit(RLIMIT_CORE, &r);
 }
 
+static int chvt(int vt) {
+	return ioctl(console_fd, VT_ACTIVATE, vt)
+	    || ioctl(console_fd, VT_WAITACTIVE, vt)
+	    || close(console_fd)
+	    || (console_fd = open("/dev/console", O_RDWR)) == -1;
+}
+
+static void restore_vt() {
+	struct rlimit r;
+	r.rlim_cur = r.rlim_max = console_fd + 1;
+	setrlimit(RLIMIT_NOFILE, &r);
+	close(STDOUT_FILENO);
+	close(STDERR_FILENO);
+	/* It does not matter whether other files are still opened or not.
+	 * RLIMIT_NOFILE only prevents files from being opened with a fd greater
+	 * or equal than the limit. chvt should reopen console with fd=0
+	 */
+	chvt(saved_vt);
+	ioctl(console_fd, VT_DISALLOCATE, suspend_vt);
+}
+
 static void restore_console() {
 	int result;
 	ioctl(console_fd, KDSKBMODE, K_XLATE);
@@ -726,14 +756,30 @@ static void setup_signal_handlers() {
 }
 
 static void open_console() {
+	struct vt_stat vtstat;
+
 	if ((console_fd = open("/dev/console", O_RDWR)) == -1)
 		bail_err("open(\"/dev/console\", O_RDWR)");
 
-	if (dup2(console_fd, STDIN_FILENO) == -1)
-		bail_err("dup2(fd, STDIN_FILENO)");
-	if (!test_run && dup2(console_fd, STDOUT_FILENO) == -1)
+	if (ioctl(console_fd, VT_GETSTATE, &vtstat))
+		bail_err("VT_GETSTATE");
+	if (!suspend_vt)
+		suspend_vt = vtstat.v_active;
+	else if (suspend_vt != vtstat.v_active) {
+		if (chvt(suspend_vt))
+			bail_err("failed to change VT");
+		saved_vt = vtstat.v_active;
+		atexit(restore_vt);
+	}
+
+	if (console_fd != STDIN_FILENO) {
+		if (dup2(console_fd, STDIN_FILENO) == -1 || close(console_fd))
+			bail_err("dup2(fd, STDIN_FILENO)");
+		console_fd = STDIN_FILENO;
+	}
+	if (dup2(console_fd, STDOUT_FILENO) == -1)
 		bail_err("dup2(fd, STDOUT_FILENO)");
-	if (!test_run && dup2(console_fd, STDERR_FILENO) == -1)
+	if (dup2(console_fd, STDERR_FILENO) == -1)
 		bail_err("dup2(fd, STDERR_FILENO)");
 }
 
diff --git a/userui_text.c b/userui_text.c
index 0b9ff2a..5502f4b 100644
--- a/userui_text.c
+++ b/userui_text.c
@@ -362,8 +362,6 @@ static void text_cleanup()
 		close(vcsa_fd);
 
 	ioctl(STDOUT_FILENO, TCSETSF, (long)&termios);
-
-	/* chvt back? */
 }
 
 static void text_redraw()