| 1 | /* $Id: win_exec_wrapper.c 3528 2021-12-19 16:32:38Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * win_exec_wrapper - Stub for exec'ing a kmk_xxx program.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (c) 2021 knut st. osmundsen <bird-kBuild-spamixx@anduin.net>
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of kBuild.
|
|---|
| 10 | *
|
|---|
| 11 | * kBuild is free software; you can redistribute it and/or modify
|
|---|
| 12 | * it under the terms of the GNU General Public License as published by
|
|---|
| 13 | * the Free Software Foundation; either version 3 of the License, or
|
|---|
| 14 | * (at your option) any later version.
|
|---|
| 15 | *
|
|---|
| 16 | * kBuild is distributed in the hope that it will be useful,
|
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 19 | * GNU General Public License for more details.
|
|---|
| 20 | *
|
|---|
| 21 | * You should have received a copy of the GNU General Public License
|
|---|
| 22 | * along with kBuild. If not, see <http://www.gnu.org/licenses/>
|
|---|
| 23 | *
|
|---|
| 24 | */
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 | /*********************************************************************************************************************************
|
|---|
| 28 | * Header Files *
|
|---|
| 29 | *********************************************************************************************************************************/
|
|---|
| 30 | #include <windows.h>
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | VOID __stdcall BareBoneStart(VOID)
|
|---|
| 34 | {
|
|---|
| 35 | DWORD dwIgnored;
|
|---|
| 36 | PROCESS_INFORMATION ProcInfo = { NULL, NULL, 0, 0 };
|
|---|
| 37 | WCHAR wszExec[260];
|
|---|
| 38 | UINT cwcExec = GetModuleFileNameW(NULL, wszExec, 512);
|
|---|
| 39 | BOOL fExecOk = FALSE;
|
|---|
| 40 | WCHAR const * const pwszCommandLine = GetCommandLineW();
|
|---|
| 41 | STARTUPINFOW StartInfo = { sizeof(StartInfo), NULL, NULL, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL};
|
|---|
| 42 | GetStartupInfoW(&StartInfo);
|
|---|
| 43 |
|
|---|
| 44 | /*
|
|---|
| 45 | * Make sure we've got the standard handles.
|
|---|
| 46 | */
|
|---|
| 47 | StartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
|
|---|
| 48 | StartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
|
|---|
| 49 | StartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
|
|---|
| 50 | StartInfo.dwFlags |= STARTF_USESTDHANDLES;
|
|---|
| 51 |
|
|---|
| 52 | /*
|
|---|
| 53 | * Construct the executable path.
|
|---|
| 54 | */
|
|---|
| 55 | if (cwcExec > 10)
|
|---|
| 56 | {
|
|---|
| 57 | /* Strip the filename. */
|
|---|
| 58 | #define IS_SEP(a_wc) ( (a_wc) == '\\' || (a_wc) == ':' || (a_wc) == '\\' )
|
|---|
| 59 | while (cwcExec > 3 && !IS_SEP(wszExec[cwcExec - 1]))
|
|---|
| 60 | cwcExec--;
|
|---|
| 61 | if (IS_SEP(wszExec[cwcExec - 1]))
|
|---|
| 62 | {
|
|---|
| 63 | /* Strip the separator. */
|
|---|
| 64 | while (cwcExec > 3 && IS_SEP(wszExec[cwcExec - 1]))
|
|---|
| 65 | cwcExec--;
|
|---|
| 66 | if (!IS_SEP(wszExec[cwcExec - 1]))
|
|---|
| 67 | {
|
|---|
| 68 | /* Strip the path component: */
|
|---|
| 69 | while (cwcExec > 3 && !IS_SEP(wszExec[cwcExec - 1]))
|
|---|
| 70 | cwcExec--;
|
|---|
| 71 | if (IS_SEP(wszExec[cwcExec - 1]))
|
|---|
| 72 | {
|
|---|
| 73 | /* Insert the target executable name: */
|
|---|
| 74 | static char const s_szTargetName[] = TARGET_EXE_NAME;
|
|---|
| 75 | unsigned off = 0;
|
|---|
| 76 | while (off < sizeof(s_szTargetName))
|
|---|
| 77 | wszExec[cwcExec++] = s_szTargetName[off++];
|
|---|
| 78 | fExecOk = cwcExec <= 260;
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 | if (fExecOk)
|
|---|
| 84 | {
|
|---|
| 85 | /*
|
|---|
| 86 | * Create the real process.
|
|---|
| 87 | */
|
|---|
| 88 | if (CreateProcessW(wszExec, (WCHAR *)pwszCommandLine, NULL, NULL, TRUE /*bInheritHandles*/,
|
|---|
| 89 | 0 /*fFlags*/, NULL /*pwszzEnv*/, NULL /*pwszCwd*/, &StartInfo, &ProcInfo))
|
|---|
| 90 | {
|
|---|
| 91 | /*
|
|---|
| 92 | * Wait for it to complete.
|
|---|
| 93 | */
|
|---|
| 94 | CloseHandle(ProcInfo.hThread);
|
|---|
| 95 | for (;;)
|
|---|
| 96 | {
|
|---|
| 97 | DWORD dwExitCode = 1;
|
|---|
| 98 | WaitForSingleObject(ProcInfo.hProcess, INFINITE);
|
|---|
| 99 | if (GetExitCodeProcess(ProcInfo.hProcess, &dwExitCode))
|
|---|
| 100 | for (;;)
|
|---|
| 101 | ExitProcess(dwExitCode);
|
|---|
| 102 | Sleep(1);
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 | else
|
|---|
| 106 | {
|
|---|
| 107 | static const char s_szMsg[] = "error: CreateProcessW failed for " TARGET_EXE_NAME "\r\n";
|
|---|
| 108 | WriteFile(StartInfo.hStdError, s_szMsg, sizeof(s_szMsg) - 1, &dwIgnored, NULL);
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 | else
|
|---|
| 112 | {
|
|---|
| 113 | static const char s_szMsg[] = "error: path construction failed (" TARGET_EXE_NAME ")\r\n";
|
|---|
| 114 | WriteFile(StartInfo.hStdError, s_szMsg, sizeof(s_szMsg) - 1, &dwIgnored, NULL);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | for (;;)
|
|---|
| 118 | ExitProcess(31);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|