Newer
Older
/*
* BRLTTY - A background process providing access to the console screen (when in
* text mode) for a blind person using a refreshable braille display.
*
* Copyright (C) 1995-2018 by The BRLTTY Developers.
*
* BRLTTY comes with ABSOLUTELY NO WARRANTY.
*
* This is free software, placed under the terms of the
* GNU Lesser General Public License, as published by the Free Software
* Foundation; either version 2.1 of the License, or (at your option) any
* later version. Please see the file LICENSE-LGPL for details.
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
*
* This software is maintained by Dave Mielke <dave@mielke.cc>.
*/
#include "prologue.h"
#include <io.h>
#include <fcntl.h>
#include "log.h"
#include "system_windows.h"
#include "hostcmd_windows.h"
#include "hostcmd_internal.h"
typedef struct {
DWORD identifier;
HANDLE *field;
} HandleEntry;
static void
closeHandle (HANDLE *handle) {
if (*handle != INVALID_HANDLE_VALUE) {
if (*handle) {
CloseHandle(*handle);
}
*handle = INVALID_HANDLE_VALUE;
}
}
static HANDLE *
getParentHandle (HostCommandStream *hcs) {
return hcs->isInput? &hcs->package.outputHandle: &hcs->package.inputHandle;
}
static HANDLE *
getChildHandle (HostCommandStream *hcs) {
return hcs->isInput? &hcs->package.inputHandle: &hcs->package.outputHandle;
}
int
constructHostCommandPackageData (HostCommandPackageData *pkg) {
pkg->inputHandle = INVALID_HANDLE_VALUE;
pkg->outputHandle = INVALID_HANDLE_VALUE;
return 1;
}
void
destructHostCommandPackageData (HostCommandPackageData *pkg) {
closeHandle(&pkg->inputHandle);
closeHandle(&pkg->outputHandle);
}
int
prepareHostCommandStream (HostCommandStream *hcs, void *data) {
SECURITY_ATTRIBUTES attributes;
ZeroMemory(&attributes, sizeof(attributes));
attributes.nLength = sizeof(attributes);
attributes.bInheritHandle = TRUE;
attributes.lpSecurityDescriptor = NULL;
if (CreatePipe(&hcs->package.inputHandle, &hcs->package.outputHandle, &attributes, 0)) {
if (SetHandleInformation(*getParentHandle(hcs), HANDLE_FLAG_INHERIT, 0)) {
return 1;
} else {
logWindowsSystemError("SetHandleInformation");
}
} else {
logWindowsSystemError("CreatePipe");
}
return 0;
}
typedef struct {
const HandleEntry *const handleTable;
} SetChildHandleData;
static int
setChildHandle (HostCommandStream *hcs, void *data) {
SetChildHandleData *sch = data;
*sch->handleTable[hcs->fileDescriptor].field = *getChildHandle(hcs);
return 1;
}
static int
finishParentHostCommandStream (HostCommandStream *hcs, void *data) {
{
HANDLE *handle = getParentHandle(hcs);
int mode = hcs->isInput? O_WRONLY: O_RDONLY;
int fileDescriptor;
if ((fileDescriptor = _open_osfhandle((intptr_t)*handle, mode)) == -1) {
logSystemError("_open_osfhandle");
return 0;
}
*handle = INVALID_HANDLE_VALUE;
if (!finishHostCommandStream(hcs, fileDescriptor)) {
_close(fileDescriptor);
return 0;
}
}
closeHandle(getChildHandle(hcs));
return 1;
}
int
runCommand (
int *result,
const char *const *command,
HostCommandStream *streams,
int asynchronous
) {
int ok = 0;
char *line = makeWindowsCommandLine(command);
if (line) {
STARTUPINFO startup;
const HandleEntry handleTable[] = {
[0] = {.identifier=STD_INPUT_HANDLE , .field=&startup.hStdInput },
[1] = {.identifier=STD_OUTPUT_HANDLE, .field=&startup.hStdOutput},
[2] = {.identifier=STD_ERROR_HANDLE , .field=&startup.hStdError },
};
const unsigned int handleCount = ARRAY_COUNT(handleTable);
const HandleEntry *const handleEnd = handleTable + handleCount;
SetChildHandleData sch = {
.handleTable = handleTable
};
logMessage(LOG_DEBUG, "host command: %s", line);
ZeroMemory(&startup, sizeof(startup));
startup.cb = sizeof(startup);
startup.dwFlags = STARTF_USESTDHANDLES;
{
const HandleEntry *hdl = handleTable;
while (hdl < handleEnd) {
if ((*hdl->field = GetStdHandle(hdl->identifier)) == INVALID_HANDLE_VALUE) {
logWindowsSystemError("GetStdHandle");
return 0;
}
hdl += 1;
}
}
if (processHostCommandStreams(streams, setChildHandle, &sch)) {
PROCESS_INFORMATION info;
ZeroMemory(&info, sizeof(info));
if (CreateProcess(NULL, line, NULL, NULL, TRUE,
CREATE_NEW_PROCESS_GROUP,
NULL, NULL, &startup, &info)) {
if (processHostCommandStreams(streams, finishParentHostCommandStream, NULL)) {
ok = 1;
if (asynchronous) {
*result = 0;
} else {
DWORD waitResult;
*result = 0XFF;
while ((waitResult = WaitForSingleObject(info.hProcess, INFINITE)) == WAIT_TIMEOUT);
if (waitResult == WAIT_OBJECT_0) {
DWORD exitCode;
if (GetExitCodeProcess(info.hProcess, &exitCode)) {
*result = exitCode;
} else {
logWindowsSystemError("GetExitCodeProcess");
}
} else {
logWindowsSystemError("WaitForSingleObject");
}
}
}
CloseHandle(info.hProcess);
CloseHandle(info.hThread);
} else {
logWindowsSystemError("CreateProcess");
}
}
free(line);
} else {
logMallocError();
}
return ok;
}