File: HostApp.cpp

package info (click to toggle)
pd-vstplugin 0.6.1-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 2,008 kB
  • sloc: cpp: 22,783; lisp: 2,860; makefile: 37; sh: 26
file content (628 lines) | stat: -rw-r--r-- 20,646 bytes parent folder | download | duplicates (2)
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#include "HostApp.h"
#include "MiscUtils.h"
#include "CpuArch.h"
#include "FileUtils.h"
#include "MiscUtils.h"
#include "Log.h"

#include <mutex>
#include <cassert>

#ifndef _WIN32
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>
#endif

namespace vst {

std::string getHostAppName(CpuArch arch){
    if (arch == getHostCpuArchitecture()){
    #ifdef _WIN32
        return "host.exe";
    #else
        return "host";
    #endif
    } else {
        std::string host = std::string("host_") + cpuArchToString(arch);
    #if defined(_WIN32)
        host += ".exe";
    #endif
        return host;
    }
}

//----------------------- ProcessHandle -----------------------------//

#ifdef _WIN32

int ProcessHandle::pid() const {
    return pi_.dwProcessId;
}

bool ProcessHandle::valid() const {
    return pi_.dwProcessId > 0;
}

int ProcessHandle::wait() {
    auto [done, code] = tryWait(-1);
    assert(done);
    return code;
}

std::pair<bool, int> ProcessHandle::tryWait(double timeout) {
    // don't remove the DWORD cast!
    const DWORD timeoutms = (timeout >= 0) ? static_cast<DWORD>(timeout * 1000.f) : INFINITE;
    auto res = WaitForSingleObject(pi_.hProcess, timeoutms);
    if (res == WAIT_TIMEOUT){
        return { false, -1 };
    } else if (res == WAIT_OBJECT_0){
        DWORD code;
        if (!GetExitCodeProcess(pi_.hProcess, &code)) {
            throw Error(Error::SystemError, "couldn't retrieve exit code for subprocess!");
        }
        close();
        return { true, code };
    } else {
        throw Error(Error::SystemError, "WaitForSingleObject() failed: " + errorMessage(GetLastError()));
    }
}

bool ProcessHandle::checkIfRunning() {
    DWORD res = WaitForSingleObject(pi_.hProcess, 0);
    if (res == WAIT_TIMEOUT){
        return true; // still running
    } else if (res == WAIT_OBJECT_0){
        DWORD code = 0;
        if (GetExitCodeProcess(pi_.hProcess, &code)){
            if (code == EXIT_SUCCESS){
                LOG_DEBUG("Watchdog: subprocess exited successfully");
            } else if (code == EXIT_FAILURE){
                // LATER get the actual Error from the child process.
                LOG_WARNING("Watchdog: subprocess exited with failure");
            } else {
                LOG_WARNING("Watchdog: subprocess crashed!");
            }
        } else {
            LOG_ERROR("Watchdog: couldn't retrieve exit code for subprocess!");
        }
        close();
        return false;
    } else {
        // error (should we rather throw?)
        LOG_ERROR("Watchdog: WaitForSingleObject() failed: " << errorMessage(GetLastError()));
        return false;
    }
}

bool ProcessHandle::terminate() {
    if (TerminateProcess(pi_.hProcess, EXIT_FAILURE)) {
        close();
        return true;
    } else {
        LOG_ERROR("VSTPlugin: couldn't terminate subprocess: " << errorMessage(GetLastError()));
        return false;
    }
}

void ProcessHandle::close() {
    if (valid()) {
        CloseHandle(pi_.hProcess);
        CloseHandle(pi_.hThread);
        ZeroMemory(&pi_, sizeof(pi_));
    }
}

#else

int ProcessHandle::pid() const {
    return pid_;
}

bool ProcessHandle::valid() const {
    return pid_ >= 0;
}

int ProcessHandle::wait() {
    int status = 0;
    if (waitpid(pid_, &status, 0) == pid_) {
        pid_ = -1; // sentinel
        return parseStatus(status);
    } else {
        std::stringstream msg;
        msg << "waitpid() failed: " << errorMessage(errno);
        throw Error(Error::SystemError, msg.str());
    }
}

std::pair<bool, int> ProcessHandle::tryWait(double timeout) {
    int status = 0;
    if (timeout < 0) {
        return { true, wait() };
    } else if (timeout == 0) {
        auto ret = waitpid(pid_, &status, WNOHANG);
        if (ret == 0) {
            return { false, -1 }; // still running
        } else if (ret == pid_) {
            pid_ = -1; // sentinel
            return { true, parseStatus(status) }; // finished
        } else {
            std::stringstream msg;
            msg << "waitpid() failed: " << errorMessage(errno);
            throw Error(Error::SystemError, msg.str());
        }
    } else {
        // HACK: poll in a loop with exponential back-off
        int sleepmicros = 1000;
        const int maxsleep = 100000; // 100 ms
        auto start = std::chrono::system_clock::now();
        for (;;) {
            auto ret = waitpid(pid_, &status, WNOHANG);
            if (ret == pid_) {
                break;
            } else if (ret == 0) {
                auto now = std::chrono::system_clock::now();
                auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - start).count();
                if (elapsed >= timeout) {
                    return { false, -1 }; // time out
                } else {
                    usleep(sleepmicros);
                    sleepmicros *= 2;
                    if (sleepmicros > maxsleep) {
                        sleepmicros = maxsleep;
                    }
                }
            } else if (ret < 0) {
                std::stringstream msg;
                msg << "waitpid() failed " << errorMessage(errno);
                throw Error(Error::SystemError, msg.str());
            }
        }
        // finished
        pid_ = -1; // sentinel
        return { true, parseStatus(status) };
    }
}

bool ProcessHandle::checkIfRunning() {
    int status = 0;
    auto ret = waitpid(pid_, &status, WNOHANG);
    if (ret == 0){
        return true; // still running
    } else if (ret == pid_) {
        // subprocess changed state
        try {
            int code = parseStatus(status);
            if (code == EXIT_SUCCESS){
                LOG_DEBUG("Watchdog: subprocess exited successfully");
            } else if (code == EXIT_FAILURE){
                // LATER get the actual Error from the child process.
                LOG_WARNING("Watchdog: subprocess exited with failure");
            } else {
                LOG_WARNING("Watchdog: subprocess crashed!");
            }
        } catch (const Error& e) {
            LOG_WARNING("WatchDog: " << e.what());
        }
        // finished
        pid_ = -1; // sentinel
        return false;
    } else {
        // error (should we rather throw?)
        LOG_ERROR("Watchdog: waitpid() failed: " << errorMessage(errno));
        return false;
    }
}

bool ProcessHandle::terminate() {
    if (kill(pid_, SIGTERM) == 0) {
        pid_ = -1; // sentinel
        return true;
    } else {
        LOG_ERROR("VSTPlugin: couldn't terminate subprocess: " << errorMessage(errno));
        return false;
    }
}

int ProcessHandle::parseStatus(int status) {
    if (WIFEXITED(status)) {
        return WEXITSTATUS(status);
    } else if (WIFSIGNALED(status)){
        auto sig = WTERMSIG(status);
        std::stringstream msg;
        msg << "subprocess was terminated with signal "
           << sig << " (" << strsignal(sig) << ")";
        throw Error(Error::SystemError, msg.str());
    } else if (WIFSTOPPED(status)){
        auto sig = WSTOPSIG(status);
        std::stringstream msg;
        msg << "subprocess was stopped with signal "
           << sig << " (" << strsignal(sig) << ")";
        throw Error(Error::SystemError, msg.str());
    } else if (WIFCONTINUED(status)){
        // FIXME what should be do here?
        throw Error(Error::SystemError, "subprocess continued");
    } else {
        std::stringstream msg;
        msg << "unknown exit status (" << status << ")";
        throw Error(Error::SystemError, msg.str());
    }
}

#endif // _WIN32

//--------------------------- HostApp ------------------------------//

// should host.exe inherit file handles and print to stdout/stderr?
#ifndef PROBE_LOG
#define PROBE_LOG 0
#endif

// redirect stdout and stderr from child process to parent.
// use this if you want to see debug output from the actual VST plugins.
// NOTE: this doesn't affect log functions like LOG_ERROR because
// they go to a dedicated log pipe.
#ifndef BRIDGE_LOG
#define BRIDGE_LOG 0
#endif

class HostApp : public IHostApp {
public:
    HostApp(CpuArch arch, const std::string& path)
        : arch_(arch), path_(path) {}

    CpuArch arch() const override {
        return arch_;
    }

    const std::string& path() const override {
        return path_;
    }

    ProcessHandle probe(const std::string& path, int id,
                        const std::string& tmpPath) const override;

    ProcessHandle bridge(const std::string& shmPath, intptr_t logPipe) const override;

    virtual bool test() const {
        return doTest(path_);
    }
protected:
    CpuArch arch_;
    std::string path_;

    bool doTest(const std::string& cmd, const std::string& args = "") const;

#ifdef _WIN32
    ProcessHandle createProcess(const std::string& cmdline, bool log) const;
#else
    template<bool log, typename... T>
    static ProcessHandle createProcess(const char *cmd, T&&... args);
#endif
};

bool HostApp::doTest(const std::string& cmd, const std::string& args) const {
    std::stringstream ss;
    ss << args << " test " << getVersionString();
    try {
        int exitCode = runCommand(cmd, ss.str());
        if (exitCode == EXIT_SUCCESS){
            return true; // success
        } else if (exitCode == kHostAppVersionMismatch) {
            LOG_ERROR("VSTPlugin: host app '" << path_ << "' failed (version mismatch)");
        } else {
            LOG_ERROR("VSTPlugin: host app '" << path_ << "' failed with exit code " << exitCode);
        }
    } catch (const Error& e) {
        LOG_ERROR("VSTPlugin: failed to execute host app '" << path_ << "': " << e.what());
    }
    return false;
}

#ifdef _WIN32

ProcessHandle HostApp::probe(const std::string &pluginPath, int id,
                             const std::string &tmpPath) const {
    // turn id into hex string
    char idString[12];
    if (id >= 0){
        snprintf(idString, sizeof(idString), "0x%X", id);
    } else {
        sprintf(idString, "_");
    }
    /// LOG_DEBUG("host path: " << path_);
    /// LOG_DEBUG("temp path: " << tmpPath);
    // arguments: host.exe probe <plugin_path> <plugin_id> <file_path>
    // NOTE: we need to quote string arguments (in case they contain spaces)
    std::stringstream cmdline;
    cmdline << fileName(path_) << " probe "
            << "\"" << pluginPath << "\" " << idString
            << " \"" << tmpPath + "\"";

    return createProcess(cmdline.str(), PROBE_LOG);
}

ProcessHandle HostApp::bridge(const std::string &shmPath, intptr_t logPipe) const {
    /// LOG_DEBUG("host path: " << shorten(hostPath));
    // arguments: host.exe bridge <parent_pid> <shm_path> <log_pipe>
    // NOTE: we need to quote string arguments (in case they contain spaces)
    // NOTE: Win32 handles can be safely cast to DWORD!
    std::stringstream cmdline;
    cmdline << fileName(path_) << " bridge " << getCurrentProcessId()
            << " \"" << shmPath << "\" " << (DWORD)logPipe;

    return createProcess(cmdline.str(), BRIDGE_LOG);
}

ProcessHandle HostApp::createProcess(const std::string &cmdline, bool log) const {
    // LOG_DEBUG(path_ << " " << cmdline);

    PROCESS_INFORMATION pi;
    ZeroMemory(&pi, sizeof(pi));

    STARTUPINFOW si;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);

    if (!CreateProcessW(widen(path_).c_str(), &widen(cmdline)[0], NULL, NULL, 0,
                        log ? CREATE_NEW_CONSOLE : DETACHED_PROCESS,
                        NULL, NULL, &si, &pi)){
        auto err = GetLastError();
        std::stringstream ss;
        ss << "couldn't open host process " << path_ << " (" << errorMessage(err) << ")";
        throw Error(Error::SystemError, ss.str());
    }

    return ProcessHandle(pi);
}

#else

template<bool log, typename... T>
ProcessHandle HostApp::createProcess(const char *cmd, T&&... args) {
    if (!log) {
        // flush before fork() to avoid duplicate printouts!
        fflush(stdout);
        fflush(stderr);
    }
    auto pid = fork();
    if (pid == -1) {
        throw Error(Error::SystemError, "fork() failed!");
    } else if (pid == 0) {
        // child process
        if (!log) {
            // disable stdout and stderr
            auto nullOut = fopen("/dev/null", "w");
            dup2(fileno(nullOut), STDOUT_FILENO);
            dup2(fileno(nullOut), STDERR_FILENO);
        }
        // NOTE: we must not quote arguments to exec!
        // NOTE: use PATH for "arch", "wine", etc.
        if (execlp(cmd, args..., nullptr) < 0) {
            LOG_ERROR("VSTPlugin: execl() failed: " << errorMessage(errno));
        }
        std::exit(EXIT_FAILURE);
    }
    return ProcessHandle(pid);
}

ProcessHandle HostApp::probe(const std::string &pluginPath, int id,
                             const std::string &tmpPath) const {
    // turn id into hex string
    char idString[12];
    if (id >= 0){
        snprintf(idString, sizeof(idString), "0x%X", id);
    } else {
        sprintf(idString, "_");
    }
    // arguments: host probe <plugin_path> <plugin_id> <file_path>
    return createProcess<PROBE_LOG>(path_.c_str(), fileName(path_).c_str(), "probe",
                                    pluginPath.c_str(), idString, tmpPath.c_str());
}

ProcessHandle HostApp::bridge(const std::string &shmPath, intptr_t logPipe) const {
    auto parent = std::to_string(getpid());
    auto pipe = std::to_string(static_cast<int>(logPipe));
    // arguments: host bridge <parent_pid> <shm_path> <log_pipe>
    return createProcess<BRIDGE_LOG>(path_.c_str(), fileName(path_).c_str(), "bridge",
                                     parent.c_str(), shmPath.c_str(), pipe.c_str());
}

#endif

#ifdef __APPLE__
const char * archOption(CpuArch arch) {
    if (arch == CpuArch::aarch64) {
        return "-arm64";
    } else if (arch == CpuArch::amd64) {
        return "-x86_64";
    } else if (arch == CpuArch::i386) {
        return "-i386";
    } else {
        std::stringstream ss;
        ss << "unsupported CPU architecture " << cpuArchToString(arch);
        throw Error(Error::ModuleError, ss.str());
    }
}

class UniversalHostApp : public HostApp {
public:
    using HostApp::HostApp;

    ProcessHandle probe(const std::string& pluginPath, int id,
                        const std::string& tmpPath) const override {
        // turn id into hex string
        char idString[12];
        if (id >= 0) {
            snprintf(idString, sizeof(idString), "0x%X", id);
        } else {
            sprintf(idString, "_");
        }
        // arguments: arch -<arch> <host_path> probe <plugin_path> <plugin_id> <file_path>
        return createProcess<PROBE_LOG>("arch", "arch", archOption(arch_), path_.c_str(), "probe",
                                        pluginPath.c_str(), idString, tmpPath.c_str());
    }

    ProcessHandle bridge(const std::string& shmPath, intptr_t logPipe) const override {
        auto parent = std::to_string(getpid());
        auto pipe = std::to_string(static_cast<int>(logPipe));
        // arguments: arch -<arch> <host_path> bridge <parent_pid> <shm_path> <log_pipe>
        return createProcess<BRIDGE_LOG>("arch", "arch", archOption(arch_), path_.c_str(), "bridge",
                                         parent.c_str(), shmPath.c_str(), pipe.c_str());
    }

    bool test() const override {
        std::stringstream ss;
        ss << archOption(arch_) << " \"" << path_ << "\"";
        return doTest("arch", ss.str());
    }
};
#endif

#if USE_WINE
class WineHostApp : public HostApp {
    using HostApp::HostApp;

    ProcessHandle probe(const std::string& pluginPath, int id,
                        const std::string& tmpPath) const override {
        auto wine = wineCmd();
        // turn id into hex string
        char idString[12];
        if (id >= 0){
            snprintf(idString, sizeof(idString), "0x%X", id);
        } else {
            sprintf(idString, "_");
        }
        // arguments: wine <host_path> probe <plugin_path> <plugin_id> <file_path>
        return createProcess<PROBE_LOG>(wine, wine, path_.c_str(), "probe",
                                        pluginPath.c_str(), idString, tmpPath.c_str());
    }

    ProcessHandle bridge(const std::string& shmPath, intptr_t logPipe) const override {
        auto wine = wineCmd();
        auto parent = std::to_string(getpid());
        auto pipe = std::to_string(static_cast<int>(logPipe));
        // arguments: wine <host_path> bridge <parent_pid> <shm_path>
        return createProcess<BRIDGE_LOG>(wine, wine, path_.c_str(), "bridge",
                                         parent.c_str(), shmPath.c_str(), pipe.c_str());
    }

    bool test() const override {
        std::stringstream ss;
        ss << "\"" << path_ << "\"";
        return doTest(wineCmd(), ss.str());
    }

    const char * wineCmd() const {
        if (arch_ == CpuArch::pe_amd64) {
            return getWine64Command();
        } else {
            return getWineCommand();
        }
    }
};
#endif

std::mutex gHostAppMutex;

std::unordered_map<CpuArch, std::unique_ptr<IHostApp>> gHostAppDict;

// Generally, we can bridge between any kinds of CPU architectures,
// as long as the they are supported by the platform in question.
//
// We use the following naming scheme for the plugin bridge app:
// host_<cpu_arch>[extension]
// Examples: "host_i386", "host_amd64.exe", etc.
//
// We can selectively enable/disable CPU architectures simply by
// including resp. omitting the corresponding app.
// Note that we always ship a version of the *same* CPU architecture
// called "host" resp. "host.exe" to support plugin sandboxing.
//
// Bridging between i386 and amd64 is typically employed on Windows,
// but also possible on Linux and macOS (before 10.15).
// On the upcoming ARM MacBooks, we can also bridge between amd64 and aarch64.
// NOTE: We ship 64-bit Intel builds on Linux without "host_i386" and
// ask people to compile it themselves if they need it.
//
// On macOS and Linux we can also use the plugin bridge to run Windows plugins
// via Wine. The apps are called "host_pe_i386.exe" and "host_pe_amd64.exe".

// we only have to protect against concurrent insertion to avoid race conditions
// during the lookup process. The actual IHostApp pointer will never be invalidated.
IHostApp* IHostApp::get(CpuArch arch) {
    std::lock_guard lock(gHostAppMutex);
    auto it = gHostAppDict.find(arch);
    if (it != gHostAppDict.end()){
        return it->second.get();
    }

#if USE_WINE
    bool isWine = false;
    if (arch == CpuArch::pe_amd64) {
        // check if the 'wine64' command can be found and works.
        if (!haveWine64()){
            gHostAppDict[arch] = nullptr;
            return nullptr;
        }
        isWine = true;
    } else if (arch == CpuArch::pe_i386) {
        // check if the 'wine' command can be found and works.
        if (!haveWine()){
            gHostAppDict[arch] = nullptr;
            return nullptr;
        }
        isWine = true;
    }
#endif // USE_WINE

#ifdef _WIN32
    auto path = getModuleDirectory() + "\\" + getHostAppName(arch);
#else
    auto path = getModuleDirectory() + "/" + getHostAppName(arch);
#endif

    // check if host app exists and works
    if (pathExists(path)) {
        std::unique_ptr<HostApp> app;
    #if USE_WINE
        if (isWine) {
            app = std::make_unique<WineHostApp>(arch, path);
        }
    #endif
        if (!app) {
            app = std::make_unique<HostApp>(arch, path);
        }
        if (app->test()) {
            LOG_DEBUG("host app '" << path << "' is working");
            auto [result, _] = gHostAppDict.emplace(arch, std::move(app));
            return result->second.get();
        }
    } else {
    #ifdef __APPLE__
        // check if "host" is a universal binary that contains the desired architecture
        path = getModuleDirectory() + "/host";
        for (auto& a : getFileCpuArchitectures(path)) {
            if (a == arch) {
                auto app = std::make_unique<UniversalHostApp>(arch, path);
                if (app->test()) {
                    LOG_DEBUG("host app '" << path << "' ("
                              << cpuArchToString(arch) << ") is working");
                    auto [result, _] = gHostAppDict.emplace(arch, std::move(app));
                    return result->second.get();
                } else {
                    break;
                }
            }
        }
    #endif // __APPLE__
    }
    // failure
    LOG_INFO("VSTPlugin: no appropriate host app for CPU architecture " << cpuArchToString(arch));
    gHostAppDict[arch] = nullptr;
    return nullptr;
}

} // vst