|
0 |
/*
|
|
1 |
* Copyright (C) 2016 Red Hat, Inc.
|
|
2 |
*
|
|
3 |
* This program is free software; you can redistribute it and/or modify
|
|
4 |
* it under the terms of the GNU General Public License as published by
|
|
5 |
* the Free Software Foundation; either version 2 of the License, or
|
|
6 |
* (at your option) any later version.
|
|
7 |
*
|
|
8 |
* This program is distributed in the hope that it will be useful,
|
|
9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
* GNU General Public License for more details.
|
|
12 |
*
|
|
13 |
* You should have received a copy of the GNU General Public License
|
|
14 |
* along with this program; if not, write to the Free Software
|
|
15 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
*/
|
|
17 |
|
|
18 |
/* This small program test redirection inside Remote Viewer
|
|
19 |
*/
|
|
20 |
|
|
21 |
#include <config.h>
|
|
22 |
|
|
23 |
/* force assert to be compiler, on Windows are usually disabled */
|
|
24 |
#undef NDEBUG
|
|
25 |
#undef DEBUG
|
|
26 |
#define DEBUG 1
|
|
27 |
|
|
28 |
#include <windows.h>
|
|
29 |
#include <stdio.h>
|
|
30 |
#include <stdlib.h>
|
|
31 |
#include <assert.h>
|
|
32 |
#include <io.h>
|
|
33 |
#include <glib.h>
|
|
34 |
|
|
35 |
static BOOL has_console = FALSE;
|
|
36 |
static HANDLE h_console = INVALID_HANDLE_VALUE;
|
|
37 |
static FILE *log_f = NULL;
|
|
38 |
static int num_test = 0;
|
|
39 |
|
|
40 |
static int
|
|
41 |
has_inherit(HANDLE h)
|
|
42 |
{
|
|
43 |
DWORD flags;
|
|
44 |
if (GetHandleInformation(h, &flags))
|
|
45 |
return flags & HANDLE_FLAG_INHERIT;
|
|
46 |
return 0;
|
|
47 |
}
|
|
48 |
|
|
49 |
static BOOL is_handle_valid(HANDLE h)
|
|
50 |
{
|
|
51 |
if (h == INVALID_HANDLE_VALUE || h == NULL)
|
|
52 |
return FALSE;
|
|
53 |
|
|
54 |
DWORD flags;
|
|
55 |
return GetHandleInformation(h, &flags);
|
|
56 |
}
|
|
57 |
|
|
58 |
static int
|
|
59 |
called_test(int argc G_GNUC_UNUSED, char **argv)
|
|
60 |
{
|
|
61 |
STARTUPINFO si;
|
|
62 |
memset(&si, 0, sizeof(si));
|
|
63 |
si.cb = sizeof(si);
|
|
64 |
si.dwFlags = STARTF_USESTDHANDLES;
|
|
65 |
GetStartupInfo(&si);
|
|
66 |
fprintf(log_f, "handles %p %p - %p %p %d %d\n", si.hStdOutput, si.hStdError,
|
|
67 |
GetStdHandle(STD_OUTPUT_HANDLE), GetStdHandle(STD_ERROR_HANDLE),
|
|
68 |
has_inherit(GetStdHandle(STD_OUTPUT_HANDLE)), has_inherit(GetStdHandle(STD_ERROR_HANDLE)));
|
|
69 |
|
|
70 |
/* Get redirection from parent */
|
|
71 |
BOOL out_valid = is_handle_valid(GetStdHandle(STD_OUTPUT_HANDLE));
|
|
72 |
BOOL err_valid = is_handle_valid(GetStdHandle(STD_ERROR_HANDLE));
|
|
73 |
|
|
74 |
/*
|
|
75 |
* If not all output are redirected try to redirect to parent console.
|
|
76 |
* If parent has no console (for instance as launched from GUI) just
|
|
77 |
* rely on default (no output).
|
|
78 |
*/
|
|
79 |
if ((!out_valid || !err_valid) && AttachConsole(ATTACH_PARENT_PROCESS)) {
|
|
80 |
fprintf(log_f, "attached\n");
|
|
81 |
if (!out_valid) {
|
|
82 |
freopen("CONOUT$", "w", stdout);
|
|
83 |
dup2(fileno(stdout), STDOUT_FILENO);
|
|
84 |
}
|
|
85 |
if (!err_valid) {
|
|
86 |
freopen("CONOUT$", "w", stderr);
|
|
87 |
dup2(fileno(stderr), STDERR_FILENO);
|
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
printf("stdout %s line\n", argv[0]);
|
|
92 |
fflush(stdout);
|
|
93 |
fprintf(stderr, "stderr %s line\n", argv[0]);
|
|
94 |
fflush(stderr);
|
|
95 |
return 0;
|
|
96 |
}
|
|
97 |
|
|
98 |
static enum {
|
|
99 |
METHOD_CREATEPROCESS,
|
|
100 |
METHOD_STDHANDLE
|
|
101 |
} redir_method = METHOD_CREATEPROCESS;
|
|
102 |
|
|
103 |
static void
|
|
104 |
exec(HANDLE redir_out, HANDLE redir_err)
|
|
105 |
{
|
|
106 |
char program[MAX_PATH+128];
|
|
107 |
GetModuleFileName(NULL, program+1, MAX_PATH);
|
|
108 |
program[0] = '\"';
|
|
109 |
sprintf(strchr(program, 0) , "\" %d", num_test);
|
|
110 |
|
|
111 |
HANDLE old_out = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
112 |
HANDLE old_err = GetStdHandle(STD_ERROR_HANDLE);
|
|
113 |
|
|
114 |
BOOL inherit = FALSE;
|
|
115 |
STARTUPINFO si;
|
|
116 |
memset(&si, 0, sizeof(si));
|
|
117 |
si.cb = sizeof(si);
|
|
118 |
si.dwFlags = 0;
|
|
119 |
si.hStdOutput = si.hStdError = si.hStdInput = INVALID_HANDLE_VALUE;
|
|
120 |
if (redir_out != INVALID_HANDLE_VALUE || redir_err != INVALID_HANDLE_VALUE) {
|
|
121 |
if (redir_method == METHOD_CREATEPROCESS)
|
|
122 |
si.dwFlags |= STARTF_USESTDHANDLES;
|
|
123 |
inherit = TRUE;
|
|
124 |
if (redir_out != INVALID_HANDLE_VALUE) {
|
|
125 |
SetHandleInformation(redir_out, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
|
|
126 |
if (redir_method == METHOD_CREATEPROCESS) {
|
|
127 |
si.hStdOutput = redir_out;
|
|
128 |
} else {
|
|
129 |
SetStdHandle(STD_OUTPUT_HANDLE, redir_out);
|
|
130 |
}
|
|
131 |
}
|
|
132 |
if (redir_err != INVALID_HANDLE_VALUE) {
|
|
133 |
SetHandleInformation(redir_err, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
|
|
134 |
if (redir_method == METHOD_CREATEPROCESS) {
|
|
135 |
si.hStdError = redir_err;
|
|
136 |
} else {
|
|
137 |
SetStdHandle(STD_ERROR_HANDLE, redir_err);
|
|
138 |
}
|
|
139 |
}
|
|
140 |
}
|
|
141 |
|
|
142 |
fprintf(log_f, "handles %p %p - %p %p %d %d\n", si.hStdOutput, si.hStdError,
|
|
143 |
GetStdHandle(STD_OUTPUT_HANDLE), GetStdHandle(STD_ERROR_HANDLE),
|
|
144 |
has_inherit(GetStdHandle(STD_OUTPUT_HANDLE)), has_inherit(GetStdHandle(STD_ERROR_HANDLE)));
|
|
145 |
|
|
146 |
fprintf(log_f, "sub command ---->\n");
|
|
147 |
fclose(log_f);
|
|
148 |
log_f = NULL;
|
|
149 |
|
|
150 |
PROCESS_INFORMATION pi = { 0, };
|
|
151 |
assert(CreateProcess(NULL, program, NULL, NULL, inherit, 0, NULL, NULL, &si, &pi));
|
|
152 |
CloseHandle(pi.hThread);
|
|
153 |
WaitForSingleObject(pi.hProcess, INFINITE);
|
|
154 |
CloseHandle(pi.hProcess);
|
|
155 |
|
|
156 |
log_f = fopen("log.txt", "a");
|
|
157 |
assert(log_f);
|
|
158 |
setbuf(log_f, NULL);
|
|
159 |
fprintf(log_f, "<---- sub command\n");
|
|
160 |
|
|
161 |
SetStdHandle(STD_OUTPUT_HANDLE, old_out);
|
|
162 |
SetStdHandle(STD_ERROR_HANDLE, old_err);
|
|
163 |
}
|
|
164 |
|
|
165 |
// simple dirty function to read first line in a file
|
|
166 |
static char *
|
|
167 |
read_file(const char *fn)
|
|
168 |
{
|
|
169 |
FILE *f = fopen(fn, "r");
|
|
170 |
assert(f);
|
|
171 |
|
|
172 |
// dirty but fast
|
|
173 |
static char buf[1024];
|
|
174 |
|
|
175 |
memset(buf, 0, sizeof(buf));
|
|
176 |
if (!fgets(buf, sizeof(buf), f))
|
|
177 |
memset(buf, 0, sizeof(buf));
|
|
178 |
|
|
179 |
fclose(f);
|
|
180 |
return buf;
|
|
181 |
}
|
|
182 |
|
|
183 |
static char *
|
|
184 |
read_console(void)
|
|
185 |
{
|
|
186 |
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
|
|
187 |
assert(GetConsoleScreenBufferInfo(h_console, &csbiInfo));
|
|
188 |
|
|
189 |
fprintf(log_f, "size %d %d\n", csbiInfo.dwSize.X, csbiInfo.dwSize.Y);
|
|
190 |
fprintf(log_f, "win %d %d %d %d\n", csbiInfo.srWindow.Left, csbiInfo.srWindow.Top, csbiInfo.srWindow.Right, csbiInfo.srWindow.Bottom);
|
|
191 |
|
|
192 |
COORD size;
|
|
193 |
size.Y = csbiInfo.srWindow.Bottom + 1;
|
|
194 |
size.X = csbiInfo.srWindow.Right + 1;
|
|
195 |
|
|
196 |
COORD coord = { 0, 0 };
|
|
197 |
|
|
198 |
CHAR_INFO *buf = calloc(size.Y * size.X, sizeof(CHAR_INFO));
|
|
199 |
assert(buf);
|
|
200 |
|
|
201 |
SMALL_RECT rect;
|
|
202 |
rect.Top = 0;
|
|
203 |
rect.Left = 0;
|
|
204 |
rect.Bottom = size.Y - 1;
|
|
205 |
rect.Right = size.X - 1;
|
|
206 |
|
|
207 |
// read console content
|
|
208 |
assert(ReadConsoleOutput(h_console, buf, size, coord, &rect));
|
|
209 |
|
|
210 |
// convert to just text
|
|
211 |
unsigned n;
|
|
212 |
char *char_buf = (char *) buf;
|
|
213 |
for (n = 0; n < size.X * size.Y; ++n)
|
|
214 |
char_buf[n] = buf[n].Char.AsciiChar;
|
|
215 |
char_buf[n] = 0;
|
|
216 |
|
|
217 |
// remove additional spaces
|
|
218 |
char *p;
|
|
219 |
while ((p=strstr(char_buf, " ")) != NULL)
|
|
220 |
memmove(p, p+1, strlen(p));
|
|
221 |
|
|
222 |
return char_buf;
|
|
223 |
}
|
|
224 |
|
|
225 |
static void
|
|
226 |
check(BOOL redir_out, BOOL redir_err)
|
|
227 |
{
|
|
228 |
++num_test;
|
|
229 |
fprintf(log_f, "check method %d console %d redir_out %d redir_err %d\n",
|
|
230 |
(int) redir_method, has_console, redir_out, redir_err);
|
|
231 |
|
|
232 |
char stdout_line[64], stderr_line[64];
|
|
233 |
|
|
234 |
sprintf(stdout_line, "stdout %d line", num_test);
|
|
235 |
sprintf(stderr_line, "stderr %d line", num_test);
|
|
236 |
|
|
237 |
DeleteFile("stdout.txt");
|
|
238 |
DeleteFile("stderr.txt");
|
|
239 |
|
|
240 |
HANDLE out = CreateFile("stdout.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
|
|
241 |
assert(out != INVALID_HANDLE_VALUE);
|
|
242 |
HANDLE err = CreateFile("stderr.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
|
|
243 |
assert(err != INVALID_HANDLE_VALUE);
|
|
244 |
|
|
245 |
exec(redir_out ? out : INVALID_HANDLE_VALUE,
|
|
246 |
redir_err ? err : INVALID_HANDLE_VALUE);
|
|
247 |
|
|
248 |
CloseHandle(out);
|
|
249 |
CloseHandle(err);
|
|
250 |
|
|
251 |
// check file output
|
|
252 |
if (redir_out) {
|
|
253 |
char *data = read_file("stdout.txt");
|
|
254 |
assert(strstr(data, stdout_line) != NULL);
|
|
255 |
}
|
|
256 |
if (redir_err) {
|
|
257 |
char *data = read_file("stderr.txt");
|
|
258 |
assert(strstr(data, stderr_line) != NULL);
|
|
259 |
}
|
|
260 |
|
|
261 |
DeleteFile("stdout.txt");
|
|
262 |
DeleteFile("stderr.txt");
|
|
263 |
|
|
264 |
// check console output
|
|
265 |
if (!has_console)
|
|
266 |
return;
|
|
267 |
|
|
268 |
char *data = read_console();
|
|
269 |
fprintf(log_f, "\nconsole: %s\nstdout expected: %s\nstderr expected: %s\n", data, stdout_line, stderr_line);
|
|
270 |
if (!redir_out)
|
|
271 |
assert(strstr(data, stdout_line) != NULL);
|
|
272 |
|
|
273 |
if (!redir_err)
|
|
274 |
assert(strstr(data, stderr_line) != NULL);
|
|
275 |
free(data);
|
|
276 |
}
|
|
277 |
|
|
278 |
static void
|
|
279 |
all_checks(void)
|
|
280 |
{
|
|
281 |
check(TRUE, FALSE);
|
|
282 |
check(FALSE, TRUE);
|
|
283 |
check(TRUE, TRUE);
|
|
284 |
|
|
285 |
assert(AllocConsole());
|
|
286 |
has_console = TRUE;
|
|
287 |
h_console = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
288 |
fprintf(log_f, "got console handles %p %p\n", h_console, GetStdHandle(STD_ERROR_HANDLE));
|
|
289 |
|
|
290 |
check(FALSE, FALSE);
|
|
291 |
check(TRUE, FALSE);
|
|
292 |
check(FALSE, TRUE);
|
|
293 |
check(TRUE, TRUE);
|
|
294 |
|
|
295 |
assert(FreeConsole());
|
|
296 |
has_console = FALSE;
|
|
297 |
h_console = INVALID_HANDLE_VALUE;
|
|
298 |
}
|
|
299 |
|
|
300 |
int WINAPI WinMain(HINSTANCE hInstance G_GNUC_UNUSED, HINSTANCE hPrevInstance G_GNUC_UNUSED,
|
|
301 |
LPSTR lpCmdLine, int nShowCmd G_GNUC_UNUSED)
|
|
302 |
{
|
|
303 |
static const char cmd_seps[] = " \t\r\n\v";
|
|
304 |
|
|
305 |
char *argv[10], *p;
|
|
306 |
int argc = 0;
|
|
307 |
|
|
308 |
// parse arguments
|
|
309 |
for (p = strtok(lpCmdLine, cmd_seps); p && argc < 10; p = strtok(NULL, cmd_seps))
|
|
310 |
argv[argc++] = p;
|
|
311 |
argv[argc] = NULL;
|
|
312 |
|
|
313 |
log_f = fopen("log.txt", argc >= 1 ? "a" : "w");
|
|
314 |
assert(log_f);
|
|
315 |
setbuf(log_f, NULL);
|
|
316 |
|
|
317 |
fprintf(log_f, "argc %d argv[0] %s \n", argc, argv[0]);
|
|
318 |
|
|
319 |
if (argc >= 1) {
|
|
320 |
return called_test(argc, argv);
|
|
321 |
}
|
|
322 |
|
|
323 |
// main program, call ourself with different arguments and settings
|
|
324 |
redir_method = METHOD_CREATEPROCESS;
|
|
325 |
all_checks();
|
|
326 |
|
|
327 |
redir_method = METHOD_STDHANDLE;
|
|
328 |
all_checks();
|
|
329 |
|
|
330 |
fprintf(log_f, "everything ok\n");
|
|
331 |
|
|
332 |
fclose(log_f);
|
|
333 |
return 0;
|
|
334 |
}
|