summaryrefslogtreecommitdiffstats
path: root/src/misc/win_exec_wrapper.c
blob: 0e1e851bb4e47caa576717ab13bbb2aed15f0078 (plain)
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
/* $Id: win_exec_wrapper.c 3528 2021-12-19 16:32:38Z bird $ */
/** @file
 * win_exec_wrapper - Stub for exec'ing a kmk_xxx program.
 */

/*
 * Copyright (c) 2021 knut st. osmundsen <bird-kBuild-spamixx@anduin.net>
 *
 * This file is part of kBuild.
 *
 * kBuild is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * kBuild is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with kBuild.  If not, see <http://www.gnu.org/licenses/>
 *
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <windows.h>


VOID __stdcall BareBoneStart(VOID)
{
    DWORD               dwIgnored;
    PROCESS_INFORMATION ProcInfo        = { NULL, NULL, 0, 0 };
    WCHAR               wszExec[260];
    UINT                cwcExec         = GetModuleFileNameW(NULL, wszExec, 512);
    BOOL                fExecOk         = FALSE;
    WCHAR const * const pwszCommandLine = GetCommandLineW();
    STARTUPINFOW        StartInfo       = { sizeof(StartInfo), NULL, NULL, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL};
    GetStartupInfoW(&StartInfo);

    /*
     * Make sure we've got the standard handles.
     */
    StartInfo.hStdInput  = GetStdHandle(STD_INPUT_HANDLE);
    StartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    StartInfo.hStdError  = GetStdHandle(STD_ERROR_HANDLE);
    StartInfo.dwFlags   |= STARTF_USESTDHANDLES;

    /*
     * Construct the executable path.
     */
    if (cwcExec > 10)
    {
        /* Strip the filename. */
#define IS_SEP(a_wc) ( (a_wc) == '\\' || (a_wc) == ':' || (a_wc) == '\\' )
        while (cwcExec > 3 && !IS_SEP(wszExec[cwcExec - 1]))
            cwcExec--;
        if (IS_SEP(wszExec[cwcExec - 1]))
        {
            /* Strip the separator. */
            while (cwcExec > 3 && IS_SEP(wszExec[cwcExec - 1]))
                cwcExec--;
            if (!IS_SEP(wszExec[cwcExec - 1]))
            {
                /* Strip the path component: */
                while (cwcExec > 3 && !IS_SEP(wszExec[cwcExec - 1]))
                    cwcExec--;
                if (IS_SEP(wszExec[cwcExec - 1]))
                {
                    /* Insert the target executable name: */
                    static char const s_szTargetName[] = TARGET_EXE_NAME;
                    unsigned off = 0;
                    while (off < sizeof(s_szTargetName))
                        wszExec[cwcExec++] = s_szTargetName[off++];
                    fExecOk = cwcExec <= 260;
                }
            }
        }
    }
    if (fExecOk)
    {
        /*
         * Create the real process.
         */
        if (CreateProcessW(wszExec, (WCHAR *)pwszCommandLine, NULL, NULL, TRUE /*bInheritHandles*/,
                           0 /*fFlags*/, NULL /*pwszzEnv*/, NULL /*pwszCwd*/, &StartInfo, &ProcInfo))
        {
            /*
             * Wait for it to complete.
             */
            CloseHandle(ProcInfo.hThread);
            for (;;)
            {
                DWORD dwExitCode = 1;
                WaitForSingleObject(ProcInfo.hProcess, INFINITE);
                if (GetExitCodeProcess(ProcInfo.hProcess, &dwExitCode))
                    for (;;)
                        ExitProcess(dwExitCode);
                Sleep(1);
            }
        }
        else
        {
            static const char s_szMsg[] = "error: CreateProcessW failed for " TARGET_EXE_NAME "\r\n";
            WriteFile(StartInfo.hStdError, s_szMsg, sizeof(s_szMsg) - 1, &dwIgnored, NULL);
        }
    }
    else
    {
        static const char s_szMsg[] = "error: path construction failed (" TARGET_EXE_NAME ")\r\n";
        WriteFile(StartInfo.hStdError, s_szMsg, sizeof(s_szMsg) - 1, &dwIgnored, NULL);
    }

    for (;;)
        ExitProcess(31);
}