Esempio n. 1
0
/* main */
int
main(int argc, char * argv[])
{
    printf("# 01a-cc\n");
    printf("1..16\n"); /* tests */
    create_exe();  /* 1-2 make testexe.c and testexe.exe */
    run_exe();     /* 2-5 run testexe.exe */
    remove_exe();  /* 6-7 remove testexe.c and testexe.exe */
    create_lib();  /* 8-9 make testlib.c and testlib.so */
    load_lib();    /* 10-14 load and call testfunction */
    remove_lib();  /* 15-17 remove testexe.c .o and .so */
    return 0;
}
Esempio n. 2
0
static int CreateChildProcess(T waiter, std::string const& executable,
                              std::vector<std::string> const& args,
                              std::string const& logger, std::string const& input,
                              bool secure)
{
    auto outPipe = create_pipe();
    auto errPipe = create_pipe();

    Optional<file_descriptor_source> inputSource;

    if (!secure)
    {
        TC_LOG_TRACE(logger, "Starting process \"%s\" with arguments: \"%s\".",
                executable.c_str(), boost::algorithm::join(args, " ").c_str());
    }

    // Start the child process
    child c = [&]
    {
        if (!input.empty())
        {
            inputSource = file_descriptor_source(input);

            // With binding stdin
            return execute(run_exe(boost::filesystem::absolute(executable)),
                set_args(args),
                inherit_env(),
                bind_stdin(*inputSource),
                bind_stdout(file_descriptor_sink(outPipe.sink, close_handle)),
                bind_stderr(file_descriptor_sink(errPipe.sink, close_handle)));
        }
        else
        {
            // Without binding stdin
            return execute(run_exe(boost::filesystem::absolute(executable)),
                set_args(args),
                inherit_env(),
                bind_stdout(file_descriptor_sink(outPipe.sink, close_handle)),
                bind_stderr(file_descriptor_sink(errPipe.sink, close_handle)));
        }
    }();

    file_descriptor_source outFd(outPipe.source, close_handle);
    file_descriptor_source errFd(errPipe.source, close_handle);

    auto outInfo = MakeTCLogSink([&](std::string msg)
    {
        TC_LOG_INFO(logger, "%s", msg.c_str());
    });

    auto outError = MakeTCLogSink([&](std::string msg)
    {
        TC_LOG_ERROR(logger, "%s", msg.c_str());
    });

    copy(outFd, outInfo);
    copy(errFd, outError);

    // Call the waiter in the current scope to prevent
    // the streams from closing too early on leaving the scope.
    int const result = waiter(c);

    if (!secure)
    {
        TC_LOG_TRACE(logger, ">> Process \"%s\" finished with return value %i.",
                executable.c_str(), result);
    }

    if (inputSource)
        inputSource->close();

    return result;
}