Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 690c21246b9fc5f8afdc61c536c28d1ff5744e93 (plain) (blame)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*******************************************************************************
 * Copyright (c) 2002, 2015 QNX Software Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     QNX Software Systems - initial API and implementation
 *     Wind River Systems, Inc.
 *
 *  starter.cpp
 *
 *  This is a small utility for windows spawner
 *******************************************************************************/

#define STRICT
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <process.h>
#include <tchar.h>
#include <stdio.h>
#include <psapi.h>
#include <stdbool.h>

#include "util.h"

#define MAX_CMD_LINE_LENGTH (2049)
#define PIPE_NAME_LENGTH 100

int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace);
void DisplayErrorMessage();

// BOOL KillProcessEx(DWORD dwProcessId);  // Handle of the process

///////////////////////////////////////////////////////////////////////////////
BOOL WINAPI HandlerRoutine(DWORD dwCtrlType) { //  control signal type
    BOOL ret = TRUE;
    switch (dwCtrlType) {
    case CTRL_C_EVENT:
        break;
    case CTRL_BREAK_EVENT:
        break;
    case CTRL_CLOSE_EVENT:
        ret = FALSE;
        break;
    case CTRL_LOGOFF_EVENT:
        ret = FALSE;
        break;
    case CTRL_SHUTDOWN_EVENT:
        ret = FALSE;
        break;
    default:
        break;
    }
    return ret;
}

// The default here means we haven't checked yet
// i.e. cygwin is true but the bin dir hasn't been captured
wchar_t *cygwinBin = NULL;
bool _isCygwin = true;

bool isCygwin(HANDLE process) {
    // Have we checked before?
    if (cygwinBin || !_isCygwin) {
        return _isCygwin;
    }

    // See if this process loaded cygwin, need a different SIGINT for them
    HMODULE mods[1024];
    DWORD needed;
    if (EnumProcessModules(process, mods, sizeof(mods), &needed)) {
        int i;
        needed /= sizeof(HMODULE);
        for (i = 0; i < needed; ++i) {
            wchar_t modName[MAX_PATH];
            if (GetModuleFileNameEx(process, mods[i], modName, MAX_PATH)) {
                wchar_t *p = wcsrchr(modName, L'\\');
                if (p) {
                    *p = 0; // Null terminate there for future reference
                    if (!wcscmp(++p, L"cygwin1.dll")) {
                        _isCygwin = true;
                        // Store away the bind dir
                        cygwinBin = wcsdup(modName);
                        return _isCygwin;
                    }
                }
            }
        }
    }

    _isCygwin = false;
    return _isCygwin;
}

bool runCygwinCommand(wchar_t *command) {
    wchar_t cygcmd[1024];
    swprintf(cygcmd, sizeof(cygcmd) / sizeof(cygcmd[0]), L"%s\\%s", cygwinBin, command);

    STARTUPINFO si;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    PROCESS_INFORMATION pi;
    ZeroMemory(&pi, sizeof(pi));
    if (CreateProcess(NULL, cygcmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
        WaitForSingleObject(pi.hProcess, INFINITE);
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
        return true;
    } else if (CreateProcess(NULL, command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
        WaitForSingleObject(pi.hProcess, INFINITE);
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
        return true;
    }
    return false;
}

static bool openNamedPipeAsStdHandle(HANDLE *handle, DWORD stdHandle, int parentPid, int counter,
                                     SECURITY_ATTRIBUTES *sa) {
    wchar_t pipeName[PIPE_NAME_LENGTH];
    DWORD dwDesiredAccess;
    DWORD dwShareMode;

    switch (stdHandle) {
    case STD_INPUT_HANDLE:
        BUILD_PIPE_NAME(pipeName, L"stdin", parentPid, counter);
        dwDesiredAccess = GENERIC_READ;
        dwShareMode = FILE_SHARE_READ;
        break;
    case STD_OUTPUT_HANDLE:
        BUILD_PIPE_NAME(pipeName, L"stdout", parentPid, counter);
        dwDesiredAccess = GENERIC_WRITE;
        dwShareMode = FILE_SHARE_WRITE;
        break;
    case STD_ERROR_HANDLE:
        BUILD_PIPE_NAME(pipeName, L"stderr", parentPid, counter);
        dwDesiredAccess = GENERIC_WRITE;
        dwShareMode = FILE_SHARE_WRITE;
        break;
    default:
        if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
            cdtTrace(L"Invalid STD handle given %i", stdHandle);
        }
        return false;
    }

    *handle = CreateFileW(pipeName, dwDesiredAccess, dwShareMode, NULL, OPEN_EXISTING, 0, sa);
    if (INVALID_HANDLE_VALUE == *handle) {
        if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
            cdtTrace(L"Failed to open pipe: %s -> %p\n", pipeName, handle);
        }
        return false;
    }

    SetHandleInformation(*handle, HANDLE_FLAG_INHERIT, TRUE);

    if (!SetStdHandle(stdHandle, *handle)) {
        if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
            cdtTrace(L"Failed to reassign standard stream to pipe %s: %i\n", pipeName, GetLastError());
        }
        return false;
    }

    if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
        cdtTrace(L"Successfully assigned pipe %s -> %p\n", pipeName, *handle);
    }

    return true;
}

bool createCommandLine(int argc, wchar_t **argv, wchar_t **cmdLine) {
    int size = MAX_CMD_LINE_LENGTH;
    wchar_t *buffer = (wchar_t *)malloc(size * sizeof(wchar_t));

    if (!buffer) {
        // malloc failed
        cdtTrace(L"Not enough memory to build cmd line!\n");
        return false;
    }

    int nPos = 0;
    for (int i = 0; i < argc; ++i) {
        wchar_t *str = *(argv + i);
        int len = wcslen(str);
        if (str) {
            int required = nPos + len + 2; // 2 => space + \0
            if (required > 32 * 1024) {
                free(buffer);
                if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
                    cdtTrace(L"Command line too long!\n");
                }
                return false;
            }

            while (1) {
                // Ensure enough space in buffer
                if (required > size) {
                    size *= 2;
                    if (size < required) {
                        size = required;
                    }

                    wchar_t *tmp = (wchar_t *)realloc(buffer, size * sizeof(wchar_t));
                    if (tmp) {
                        // realloc successful
                        buffer = tmp;
                    } else {
                        // Failed to realloc memory
                        free(buffer);
                        if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
                            cdtTrace(L"Not enough memory to build cmd line!\n");
                        }
                        return false;
                    }
                }

                int nCpyLen = copyTo(buffer + nPos, (const wchar_t *)str, len, size - nPos);
                if (nCpyLen < 0) { // Buffer too small
                    // Do a real count of number of chars required
                    required = nPos + copyTo(NULL, (const wchar_t *)str, len, INT_MAX) + 2; // 2 => space + \0
                    continue;
                }

                // Buffer was big enough.
                nPos += nCpyLen;
                break;
            }

            buffer[nPos++] = _T(' ');
            buffer[nPos] = _T('\0');
        } else {
            free(buffer);
            if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
                cdtTrace(L"Invalid argument!\n");
            }
            return false;
        }
    }

    *cmdLine = buffer;
    return true;
}

void raiseSignal(HANDLE h, int pid, const wchar_t *signal) {
    if (isCygwin(h)) {
        // Need to issue a kill command
        wchar_t kill[1024];
        swprintf(kill, sizeof(kill) / sizeof(kill[0]), L"kill -%s %d", signal, pid);
        if (!runCygwinCommand(kill)) {
            // fall back to console event
            GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
        }
    } else {
        GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
    }
}

int main() {

    int argc;
    wchar_t **argv = CommandLineToArgvW(GetCommandLine(), &argc);

    // Make sure that we've been passed the right number of arguments
    if (argc < 9) {
        wprintf(
            L"Usage: %s (parent pid) (counter) (four inheritable event handles) (trace enable) (CommandLineToSpawn)\n",
            argv[0]);
        return 0;
    }

    // Activate tracing...
    if (wcstol(argv[8], NULL, 2)) {
        for (int i = 0; i < sizeof(ALL_TRACE_KINDS) / sizeof(ALL_TRACE_KINDS[0]); i++) {
            enableTraceFor(ALL_TRACE_KINDS[i]);
        }
    }

    STARTUPINFOW si = {sizeof(si)};
    PROCESS_INFORMATION pi = {0};
    DWORD dwExitCode = 0;

    BOOL exitProc = FALSE;
    HANDLE waitEvent = OpenEventW(EVENT_ALL_ACCESS, TRUE, argv[4]);
    HANDLE h[5];
    h[0] = OpenEventW(EVENT_ALL_ACCESS, TRUE, argv[3]); // simulated SIGINT (CTRL-C or Cygwin 'kill -SIGINT')
                                                        //  h[1] we reserve for the process handle
    h[2] = OpenEventW(EVENT_ALL_ACCESS, TRUE, argv[5]); // simulated SIGTERM
    h[3] = OpenEventW(EVENT_ALL_ACCESS, TRUE, argv[6]); // simulated SIGKILL
    h[4] = OpenEventW(EVENT_ALL_ACCESS, TRUE, argv[7]); // CTRL-C, in all cases

    SetConsoleCtrlHandler(HandlerRoutine, TRUE);

    int parentPid = wcstol(argv[1], NULL, 10);
    int nCounter = wcstol(argv[2], NULL, 10);

    HANDLE stdHandles[] = {
        INVALID_HANDLE_VALUE, // STDIN
        INVALID_HANDLE_VALUE, // STDOUT
        INVALID_HANDLE_VALUE  // STDERR
    };

    SECURITY_ATTRIBUTES sa;
    sa.nLength = sizeof(SECURITY_ATTRIBUTES);
    sa.bInheritHandle = TRUE;
    sa.lpSecurityDescriptor = NULL;

    if (!openNamedPipeAsStdHandle(&stdHandles[0], STD_INPUT_HANDLE, parentPid, nCounter, &sa) ||
        !openNamedPipeAsStdHandle(&stdHandles[1], STD_OUTPUT_HANDLE, parentPid, nCounter, &sa) ||
        !openNamedPipeAsStdHandle(&stdHandles[2], STD_ERROR_HANDLE, parentPid, nCounter, &sa)) {
        CLOSE_HANDLES(stdHandles);
        return -1;
    }

    if (isTraceEnabled(CDT_TRACE_SPAWNER_DETAILS)) {
        wchar_t *lpvEnv = GetEnvironmentStringsW();

        if (lpvEnv) {
            // Variable strings are separated by NULL byte, and the block is
            // terminated by a NULL byte.

            cdtTrace(L"Starter: Environment\n");
            for (wchar_t *lpszVariable = lpvEnv; *lpszVariable; lpszVariable += wcslen(lpszVariable) + 1) {
                cdtTrace(L"%s\n", lpszVariable);
            }

            FreeEnvironmentStringsW(lpvEnv);
        } else {
            // If the returned pointer is NULL, exit.
            cdtTrace(L"Cannot Read Environment\n");
        }
    }

    // Create job object
    HANDLE hJob = CreateJobObject(NULL, NULL);
    if (hJob) {
        // Configure job to
        // - terminate all associated processes when the last handle to it is closed
        // - allow child processes to break away from the job.
        JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobInfo;
        ZeroMemory(&jobInfo, sizeof(jobInfo));
        jobInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_BREAKAWAY_OK;
        if (!SetInformationJobObject(hJob, JobObjectExtendedLimitInformation, &jobInfo, sizeof(jobInfo))) {
            if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
                cdtTrace(L"Cannot set job information\n");
                DisplayErrorMessage();
            }
        }
    } else if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
        cdtTrace(L"Cannot create job object\n");
        DisplayErrorMessage();
    }

    // Construct the full command line
    wchar_t *cmdLine = NULL;
    if (!createCommandLine(argc - 9, &argv[9], &cmdLine)) {
        return 0;
    }

    if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
        cdtTrace(L"Starting: %s\n", cmdLine);
    }

    // Spawn the other processes as part of this Process Group
    // If this process is already part of a job, the flag CREATE_BREAKAWAY_FROM_JOB
    // makes the child process detach from the job, such that we can assign it
    // to our own job object.
    BOOL f = CreateProcessW(NULL, cmdLine, NULL, NULL, TRUE, CREATE_BREAKAWAY_FROM_JOB, NULL, NULL, &si, &pi);
    // If breaking away from job is not permitted, retry without breakaway flag
    if (!f) {
        f = CreateProcessW(NULL, cmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
    }

    // We don't need them any more
    CLOSE_HANDLES(stdHandles);

    if (f) {
        free(cmdLine);
        cmdLine = NULL;

        if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
            cdtTrace(L"Process %i started\n", pi.dwProcessId);
        }
        SetEvent(waitEvent); // Means that process has been spawned
        CloseHandle(pi.hThread);
        h[1] = pi.hProcess;

        if (hJob) {
            if (!AssignProcessToJobObject(hJob, pi.hProcess)) {
                if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
                    cdtTrace(L"Cannot assign process %i to a job\n", pi.dwProcessId);
                    DisplayErrorMessage();
                }
            }
        }

#define SIGINT_EVENT (WAIT_OBJECT_0 + 0)
#define EXIT_EVENT (WAIT_OBJECT_0 + 1)
#define SIGTERM_EVENT (WAIT_OBJECT_0 + 2)
#define SIGKILL_EVENT (WAIT_OBJECT_0 + 3)
#define CTRLC_EVENT (WAIT_OBJECT_0 + 4)
        while (!exitProc) {
            // Wait for the spawned-process to die or for the event
            // indicating that the processes should be forcibly killed.
            DWORD event = WaitForMultipleObjects(5, h, FALSE, INFINITE);
            switch (event) {
            case SIGINT_EVENT:
            case CTRLC_EVENT:
                if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
                    cdtTrace(L"starter (PID %i) received CTRL-C event\n", GetCurrentProcessId());
                }
                if (event == SIGINT_EVENT) {
                    raiseSignal(h[1], pi.dwProcessId, L"SIGINT");
                } else {
                    GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
                }

                SetEvent(waitEvent);
                break;

            case EXIT_EVENT: // App terminated normally
                             // Make it's exit code our exit code
                if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
                    cdtTrace(L"starter: launched process has been terminated(PID %i)\n", pi.dwProcessId);
                }
                GetExitCodeProcess(pi.hProcess, &dwExitCode);
                exitProc = TRUE;
                break;

            case SIGTERM_EVENT:
                if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
                    cdtTrace(L"starter received TERM event (PID %i)\n", GetCurrentProcessId());
                }

                raiseSignal(h[1], pi.dwProcessId, L"TERM");
                SetEvent(waitEvent);

                // Note that we keep trucking until the child process terminates (case EXIT_EVENT)
                break;

            case SIGKILL_EVENT:
                if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
                    cdtTrace(L"starter received KILL event (PID %i)\n", GetCurrentProcessId());
                }

                raiseSignal(h[1], pi.dwProcessId, L"KILL");
                SetEvent(waitEvent);

                if (hJob && !TerminateJobObject(hJob, (DWORD)-1)) {
                    if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
                        cdtTrace(L"Cannot terminate job\n");
                        DisplayErrorMessage();
                    }
                }

                // Note that we keep trucking until the child process terminates (case EXIT_EVENT)
                break;

            default:
                // Unexpected code
                if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
                    DisplayErrorMessage();
                }
                exitProc = TRUE;
                break;
            }
        }
    } else if (isTraceEnabled(CDT_TRACE_SPAWNER)) {
        cdtTrace(L"Cannot start: %s\n", cmdLine);
        free(cmdLine);

        DisplayErrorMessage();
    }

    CloseHandle(waitEvent);
    CLOSE_HANDLES(h);

    return dwExitCode;
}

void DisplayErrorMessage() {
    wchar_t *lpMsgBuf = formatWinErrorCode(GetLastError());
    OutputDebugStringW(lpMsgBuf);
    // Free the buffer.
    free(lpMsgBuf);
}
//////////////////////////////// End of File //////////////////////////////////

Back to the top