1 | /* $Id: win_exec_wrapper.c 3652 2024-11-03 03:04:15Z 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 <[email protected]>
|
---|
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 | #define MAX_EXEC_SIZE 512
|
---|
38 | WCHAR wszExec[MAX_EXEC_SIZE];
|
---|
39 | UINT cwcExec = GetModuleFileNameW(NULL, wszExec, MAX_EXEC_SIZE);
|
---|
40 | BOOL fExecOk = FALSE;
|
---|
41 | WCHAR const * const pwszCommandLine = GetCommandLineW();
|
---|
42 | STARTUPINFOW StartInfo = { sizeof(StartInfo), NULL, NULL, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL};
|
---|
43 | GetStartupInfoW(&StartInfo);
|
---|
44 |
|
---|
45 | /*
|
---|
46 | * Make sure we've got the standard handles.
|
---|
47 | */
|
---|
48 | StartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
|
---|
49 | StartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
|
---|
50 | StartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
|
---|
51 | StartInfo.dwFlags |= STARTF_USESTDHANDLES;
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * Construct the executable path.
|
---|
55 | */
|
---|
56 | if (cwcExec > 3+4)
|
---|
57 | {
|
---|
58 | /* Strip the filename. */
|
---|
59 | #define IS_SEP(a_wc) ( (a_wc) == '\\' || (a_wc) == ':' || (a_wc) == '\\' )
|
---|
60 | while (cwcExec > 3 && !IS_SEP(wszExec[cwcExec - 1]))
|
---|
61 | cwcExec--;
|
---|
62 | if (IS_SEP(wszExec[cwcExec - 1]))
|
---|
63 | {
|
---|
64 | /* Strip the separator. */
|
---|
65 | while (cwcExec > 3 && IS_SEP(wszExec[cwcExec - 1]))
|
---|
66 | cwcExec--;
|
---|
67 | if (!IS_SEP(wszExec[cwcExec - 1]))
|
---|
68 | {
|
---|
69 | /* Strip the path component: */
|
---|
70 | while (cwcExec > 3 && !IS_SEP(wszExec[cwcExec - 1]))
|
---|
71 | cwcExec--;
|
---|
72 | if (IS_SEP(wszExec[cwcExec - 1]))
|
---|
73 | {
|
---|
74 | /* Insert the target executable name: */
|
---|
75 | static char const s_szTargetName[] = TARGET_EXE_NAME;
|
---|
76 | unsigned off = 0;
|
---|
77 | while (off < sizeof(s_szTargetName))
|
---|
78 | wszExec[cwcExec++] = s_szTargetName[off++];
|
---|
79 | fExecOk = cwcExec <= MAX_EXEC_SIZE;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | if (fExecOk)
|
---|
85 | {
|
---|
86 | /*
|
---|
87 | * Create the real process.
|
---|
88 | */
|
---|
89 | if (CreateProcessW(wszExec, (WCHAR *)pwszCommandLine, NULL, NULL, TRUE /*bInheritHandles*/,
|
---|
90 | 0 /*fFlags*/, NULL /*pwszzEnv*/, NULL /*pwszCwd*/, &StartInfo, &ProcInfo))
|
---|
91 | {
|
---|
92 | /*
|
---|
93 | * Wait for it to complete.
|
---|
94 | */
|
---|
95 | CloseHandle(ProcInfo.hThread);
|
---|
96 | for (;;)
|
---|
97 | {
|
---|
98 | DWORD dwExitCode = 1;
|
---|
99 | WaitForSingleObject(ProcInfo.hProcess, INFINITE);
|
---|
100 | if (GetExitCodeProcess(ProcInfo.hProcess, &dwExitCode))
|
---|
101 | for (;;)
|
---|
102 | ExitProcess(dwExitCode);
|
---|
103 | Sleep(1);
|
---|
104 | }
|
---|
105 | }
|
---|
106 | else
|
---|
107 | {
|
---|
108 | static const char s_szMsg[] = "error: CreateProcessW failed for " TARGET_EXE_NAME "\r\n";
|
---|
109 | WriteFile(StartInfo.hStdError, s_szMsg, sizeof(s_szMsg) - 1, &dwIgnored, NULL);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | else
|
---|
113 | {
|
---|
114 | static const char s_szMsg[] = "error: path construction failed (" TARGET_EXE_NAME ")\r\n";
|
---|
115 | WriteFile(StartInfo.hStdError, s_szMsg, sizeof(s_szMsg) - 1, &dwIgnored, NULL);
|
---|
116 | }
|
---|
117 |
|
---|
118 | for (;;)
|
---|
119 | ExitProcess(31);
|
---|
120 | }
|
---|
121 |
|
---|