process.cpp 10.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24
25
26
27
28
#include <migraphx/process.hpp>
#include <migraphx/errors.hpp>
#include <migraphx/env.hpp>
#include <functional>
#include <iostream>
29
30
31
32
33
34
#include <optional>

#ifdef _WIN32
// cppcheck-suppress definePrefix
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
35
#include <cstring>
36
#else
37
#include <unistd.h>
38
#endif
39
40
41
42
43
44

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TRACE_CMD_EXECUTE)

45
46
#ifndef _WIN32

47
48
49
50
51
std::function<void(const char*)> redirect_to(std::ostream& os)
{
    return [&](const char* x) { os << x; };
}

52
53
template <class F>
int exec(const std::string& cmd, const char* type, F f)
54
55
56
57
58
59
{
    int ec = 0;
    if(enabled(MIGRAPHX_TRACE_CMD_EXECUTE{}))
        std::cout << cmd << std::endl;
    auto closer = [&](FILE* stream) {
        auto status = pclose(stream);
60
        ec          = WIFEXITED(status) ? WEXITSTATUS(status) : 0; // NOLINT
61
62
63
    };
    {
        // TODO: Use execve instead of popen
64
        std::unique_ptr<FILE, decltype(closer)> pipe(popen(cmd.c_str(), type), closer); // NOLINT
65
        if(not pipe)
66
            MIGRAPHX_THROW("popen() failed: " + cmd);
67
        f(pipe.get());
68
69
70
71
    }
    return ec;
}

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
int exec(const std::string& cmd, const std::function<void(const char*)>& std_out)
{
    return exec(cmd, "r", [&](FILE* f) {
        std::array<char, 128> buffer;
        while(fgets(buffer.data(), buffer.size(), f) != nullptr)
            std_out(buffer.data());
    });
}

int exec(const std::string& cmd, std::function<void(process::writer)> std_in)
{
    return exec(cmd, "w", [&](FILE* f) {
        std_in([&](const char* buffer, std::size_t n) { std::fwrite(buffer, 1, n, f); });
    });
}

88
89
90
91
#else

constexpr std::size_t MIGRAPHX_PROCESS_BUFSIZE = 4096;

92
93
94
95
96
97
98
enum class direction
{
    input,
    output
};

template <direction dir>
99
100
101
class pipe
{
    public:
102
    explicit pipe()
103
104
105
    {
        SECURITY_ATTRIBUTES attrs;
        attrs.nLength              = sizeof(SECURITY_ATTRIBUTES);
106
        attrs.bInheritHandle       = TRUE;
107
108
109
110
111
        attrs.lpSecurityDescriptor = nullptr;

        if(CreatePipe(&m_read, &m_write, &attrs, 0) == FALSE)
            throw GetLastError();

112
113
114
115
116
117
118
119
120
121
122
123
        if(dir == direction::output)
        {
            // Do not inherit the read handle for the output pipe
            if(SetHandleInformation(m_read, HANDLE_FLAG_INHERIT, 0) == 0)
                throw GetLastError();
        }
        else
        {
            // Do not inherit the write handle for the input pipe
            if(SetHandleInformation(m_write, HANDLE_FLAG_INHERIT, 0) == 0)
                throw GetLastError();
        }
124
125
126
127
128
129
130
131
132
    }

    pipe(const pipe&)            = delete;
    pipe& operator=(const pipe&) = delete;

    pipe(pipe&&) = default;

    ~pipe()
    {
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
        if(m_write != nullptr)
        {
            CloseHandle(m_write);
        }
        if(m_read != nullptr)
        {
            CloseHandle(m_read);
        }
    }

    bool close_write_handle()
    {
        auto result = true;
        if(m_write != nullptr)
        {
            result  = CloseHandle(m_write) == TRUE;
            m_write = nullptr;
        }
        return result;
    }

    bool close_read_handle()
    {
        auto result = true;
        if(m_read != nullptr)
        {
            result = CloseHandle(m_read) == TRUE;
            m_read = nullptr;
        }
        return result;
163
164
    }

Artur Wojcik's avatar
Artur Wojcik committed
165
    std::pair<bool, DWORD> read(LPVOID buffer, DWORD length) const
166
167
    {
        DWORD bytes_read;
Artur Wojcik's avatar
Artur Wojcik committed
168
        if(ReadFile(m_read, buffer, length, &bytes_read, nullptr) == FALSE and GetLastError() == ERROR_MORE_DATA)
169
        {
Artur Wojcik's avatar
Artur Wojcik committed
170
            return {true, bytes_read};
171
        }
Artur Wojcik's avatar
Artur Wojcik committed
172
        return {false, bytes_read};
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
    }

    HANDLE get_read_handle() const { return m_read; }

    bool write(LPCVOID buffer, DWORD length) const
    {
        DWORD bytes_written;
        return WriteFile(m_write, buffer, length, &bytes_written, nullptr) == TRUE;
    }

    HANDLE get_write_handle() const { return m_write; }

    private:
    HANDLE m_write = nullptr, m_read = nullptr;
};

template <typename F>
190
int exec(const std::pair<std::string, std::string>& command, F f)
191
{
192
193
194
195
196
197
    auto& [cwd, cmd] = command;

    if((cmd.length() + 1) > MAX_PATH)
        MIGRAPHX_THROW("Command too long, required maximum " + std::to_string(MAX_PATH) +
                       " characters (including terminating null character)");

198
199
200
    try
    {
        if(enabled(MIGRAPHX_TRACE_CMD_EXECUTE{}))
201
            std::cout << "[cwd=" << cwd << "];  cmd='" << cmd << "'\n";
202
203
204
205

        STARTUPINFO info;
        PROCESS_INFORMATION process_info;

206
207
        pipe<direction::input> input{};
        pipe<direction::output> output{};
208
209
210

        ZeroMemory(&info, sizeof(STARTUPINFO));
        info.cb         = sizeof(STARTUPINFO);
211
212
213
        info.hStdError  = output.get_write_handle();
        info.hStdOutput = output.get_write_handle();
        info.hStdInput  = input.get_read_handle();
214
215
        info.dwFlags |= STARTF_USESTDHANDLES;

216
        TCHAR cmdline[MAX_PATH];
217
        std::strncpy(cmdline, cmd.c_str(), MAX_PATH);
218

219
        ZeroMemory(&process_info, sizeof(process_info));
220

221
        if(CreateProcess(nullptr,
222
                         cmdline,
223
224
225
226
227
                         nullptr,
                         nullptr,
                         TRUE,
                         0,
                         nullptr,
228
                         cwd.empty() ? nullptr : static_cast<LPCSTR>(cwd.c_str()),
229
230
231
                         &info,
                         &process_info) == FALSE)
        {
232
            MIGRAPHX_THROW("Error creating process (" + std::to_string(GetLastError()) + ")");
233
234
        }

235
236
237
238
239
240
241
242
243
244
245
246
247
        if(not output.close_write_handle())
            MIGRAPHX_THROW("Error closing STDOUT handle for writing (" +
                           std::to_string(GetLastError()) + ")");

        if(not input.close_read_handle())
            MIGRAPHX_THROW("Error closing STDIN handle for reading (" +
                           std::to_string(GetLastError()) + ")");

        f(input, output);

        if(not input.close_write_handle())
            MIGRAPHX_THROW("Error closing STDIN handle for writing (" +
                           std::to_string(GetLastError()) + ")");
248
249
250
251
252
253
254
255
256
257
258
259

        WaitForSingleObject(process_info.hProcess, INFINITE);

        DWORD status{};
        GetExitCodeProcess(process_info.hProcess, &status);

        CloseHandle(process_info.hProcess);
        CloseHandle(process_info.hThread);

        return static_cast<int>(status);
    }
    // cppcheck-suppress catchExceptionByValue
260
    catch(DWORD error)
261
    {
262
        MIGRAPHX_THROW("Error spawning process (" + std::to_string(error) + ")");
263
264
265
    }
}

266
int exec(const std::pair<std::string, std::string>& cmd)
267
268
269
270
271
{
    TCHAR buffer[MIGRAPHX_PROCESS_BUFSIZE];
    HANDLE std_out{GetStdHandle(STD_OUTPUT_HANDLE)};
    return (std_out == nullptr or std_out == INVALID_HANDLE_VALUE)
               ? GetLastError()
272
               : exec(cmd, [&](const pipe<direction::input>&, const pipe<direction::output>& out) {
273
274
                     for(;;)
                     {
Artur Wojcik's avatar
Artur Wojcik committed
275
276
277
278
279
280
                         auto [more_data, bytes_read] = out.read(buffer, MIGRAPHX_PROCESS_BUFSIZE);
                         if(not more_data or bytes_read == 0)
                             break;
                         DWORD written;
                         if(WriteFile(std_out, buffer, bytes_read, &written, nullptr) == FALSE)
                             break;
281
282
283
284
                     }
                 });
}

285
286
int exec(const std::pair<std::string, std::string>& cmd,
         std::function<void(process::writer)> std_in)
287
{
288
289
    return exec(cmd, [&](const pipe<direction::input>& input, const pipe<direction::output>&) {
        std_in([&](const char* buffer, std::size_t n) { input.write(buffer, n); });
290
291
292
293
294
    });
}

#endif

295
296
297
298
299
struct process_impl
{
    std::string command{};
    fs::path cwd{};

300
301
302
303
#ifdef _WIN32
    std::pair<std::string, std::string> get_params() const { return {cwd.string(), command}; }
#endif

304
305
306
307
308
309
310
311
    std::string get_command() const
    {
        std::string result;
        if(not cwd.empty())
            result += "cd " + cwd.string() + "; ";
        result += command;
        return result;
    }
312
313
314
315
316
317
318
319
320

    template <class... Ts>
    void check_exec(Ts&&... xs) const
    {
        int ec = migraphx::exec(std::forward<Ts>(xs)...);
        if(ec != 0)
            MIGRAPHX_THROW("Command " + get_command() + " exited with status " +
                           std::to_string(ec));
    }
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
};

process::process(const std::string& cmd) : impl(std::make_unique<process_impl>())
{
    impl->command = cmd;
}

process::process(process&&) noexcept = default;

process& process::operator=(process rhs)
{
    std::swap(impl, rhs.impl);
    return *this;
}

process::~process() noexcept = default;

process& process::cwd(const fs::path& p)
{
    impl->cwd = p;
    return *this;
}

344
345
346
347
348
void process::exec()
{
#ifndef _WIN32
    impl->check_exec(impl->get_command(), redirect_to(std::cout));
#else
349
    impl->check_exec(impl->get_params());
350
351
#endif
}
352
353

void process::write(std::function<void(process::writer)> pipe_in)
354
{
355
356
357
#ifndef _WIN32
    impl->check_exec(impl->get_command(), std::move(pipe_in));
#else
358
    impl->check_exec(impl->get_params(), std::move(pipe_in));
359
#endif
360
361
362
363
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx