Debian Codegraph @

tools/win32/process.c

raw symbols SCIP
1 // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
2 // SPDX-License-Identifier: mit
3
4 #include <io.h>
5 #include <stdio.h>
6 #include <windows.h>
7
8 #include "common.h"
9 #include "process.h"
10
11 void die_windows_error(const char *fmt, ...) {
12 char *msg;
13
14 va_list ap;
15 va_start(ap, fmt);
16 vfprintf(stderr, fmt, ap);
17 va_end(ap);
18
19 if (!FormatMessage(
20 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL,
21 GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
22 (LPSTR)&msg, 0, NULL)) {
23 msg = "(failed to retrieve Windows error message)";
24 }
25
26 fprintf(stderr, ": %s\n", msg);
27 exit(1);
28 }
29
30 static char *make_command_line(const char *const *argv) {
31 int i;
32 size_t len = 1; /* initial quotes */
33 char *buf;
34 char *dest;
35
36 /* calculate the length of the required buffer, making worst
37 case assumptions for simplicity */
38 for (i = 0;;) {
39 /* each character could need escaping */
40 len += strlen(argv[i]) * 2;
41
42 if (!argv[++i]) {
43 break;
44 }
45
46 len += 3; /* quotes, space, quotes */
47 }
48
49 len += 2; /* final quotes and the terminating zero */
50
51 dest = buf = malloc(len);
52 if (!buf) {
53 die("allocating memory for subprocess command line");
54 }
55
56 /* Here we perform the inverse of the CommandLineToArgvW
57 function. Note that its rules are slightly crazy: A
58 sequence of backslashes only act to escape if followed by
59 double quotes. A sequence of backslashes not followed by
60 double quotes is untouched. */
61
62 for (i = 0;;) {
63 const char *src = argv[i];
64 int backslashes = 0;
65
66 *dest++ = '\"';
67
68 for (;;) {
69 switch (*src) {
70 case 0:
71 goto done;
72
73 case '\"':
74 for (; backslashes; backslashes--) {
75 *dest++ = '\\';
76 }
77
78 *dest++ = '\\';
79 *dest++ = '\"';
80 break;
81
82 case '\\':
83 backslashes++;
84 *dest++ = '\\';
85 break;
86
87 default:
88 backslashes = 0;
89 *dest++ = *src;
90 break;
91 }
92
93 src++;
94 }
95 done:
96 for (; backslashes; backslashes--) {
97 *dest++ = '\\';
98 }
99
100 *dest++ = '\"';
101
102 if (!argv[++i]) {
103 break;
104 }
105
106 *dest++ = ' ';
107 }
108
109 *dest++ = 0;
110 return buf;
111 }
112
113 void pipeline(const char *const *argv, struct pipeline *pl) {
114 HANDLE in_read_handle, in_write_handle;
115 SECURITY_ATTRIBUTES sec_attr;
116 PROCESS_INFORMATION proc_info;
117 STARTUPINFO start_info;
118 char *cmdline = make_command_line(argv);
119
120 sec_attr.nLength = sizeof sec_attr;
121 sec_attr.bInheritHandle = TRUE;
122 sec_attr.lpSecurityDescriptor = NULL;
123
124 if (!CreatePipe(&in_read_handle, &in_write_handle, &sec_attr, 0)) {
125 die_windows_error("CreatePipe");
126 }
127
128 if (!SetHandleInformation(in_write_handle, HANDLE_FLAG_INHERIT, 0)) {
129 die_windows_error("SetHandleInformation");
130 }
131
132 /* when in Rome... */
133 ZeroMemory(&proc_info, sizeof proc_info);
134 ZeroMemory(&start_info, sizeof start_info);
135
136 start_info.cb = sizeof start_info;
137 start_info.dwFlags |= STARTF_USESTDHANDLES;
138
139 if ((start_info.hStdError = GetStdHandle(STD_ERROR_HANDLE)) ==
140 INVALID_HANDLE_VALUE ||
141 (start_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE)) ==
142 INVALID_HANDLE_VALUE) {
143 die_windows_error("GetStdHandle");
144 }
145
146 start_info.hStdInput = in_read_handle;
147
148 if (!CreateProcess(NULL, cmdline, NULL, NULL, TRUE, 0, NULL, NULL,
149 &start_info, &proc_info)) {
150 die_windows_error("CreateProcess");
151 }
152
153 free(cmdline);
154
155 if (!CloseHandle(proc_info.hThread)) {
156 die_windows_error("CloseHandle for thread");
157 }
158 if (!CloseHandle(in_read_handle)) {
159 die_windows_error("CloseHandle");
160 }
161
162 pl->proc_handle = proc_info.hProcess;
163 pl->infd = _open_osfhandle((intptr_t)in_write_handle, 0);
164 }
165
166 int finish_pipeline(struct pipeline *pl) {
167 DWORD code;
168
169 if (close(pl->infd)) {
170 die_errno(errno, "close");
171 }
172
173 for (;;) {
174 if (!GetExitCodeProcess(pl->proc_handle, &code)) {
175 die_windows_error("GetExitCodeProcess");
176 }
177 if (code != STILL_ACTIVE) {
178 break;
179 }
180
181 if (WaitForSingleObject(pl->proc_handle, INFINITE) == WAIT_FAILED) {
182 die_windows_error("WaitForSingleObject");
183 }
184 }
185
186 if (!CloseHandle(pl->proc_handle)) {
187 die_windows_error("CloseHandle for process");
188 }
189
190 return code;
191 }