Esempio n. 1
0
inline PROCESS_INFORMATION win32_start(const Executable &exe, const Arguments &args, const environment &env, stream_info &infoin, stream_info &infoout, stream_info &infoerr, const win32_setup &setup) 
{ 
    file_handle chin, chout, cherr; 

    BOOST_ASSERT(setup.startupinfo->cb >= sizeof(STARTUPINFOA)); 
    BOOST_ASSERT(!(setup.startupinfo->dwFlags & STARTF_USESTDHANDLES)); 

    boost::scoped_ptr<STARTUPINFOA> si(new STARTUPINFOA); 
    ::CopyMemory(si.get(), setup.startupinfo, sizeof(*setup.startupinfo)); 
    si->dwFlags |= STARTF_USESTDHANDLES; 

    switch (infoin.type_) 
    { 
    case stream_info::close: 
        { 
            break; 
        } 
    case stream_info::inherit: 
        { 
            chin = file_handle::win32_std(STD_INPUT_HANDLE, true); 
            break; 
        } 
    case stream_info::use_file: 
        { 
            HANDLE h = ::CreateFileA(infoin.file_.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 
            if (h == INVALID_HANDLE_VALUE) 
                boost::throw_exception(boost::system::system_error(boost::system::error_code(::GetLastError(), boost::system::get_system_category()), "boost::process::detail::win32_start: CreateFile failed")); 
            chin = file_handle(h); 
            break; 
        } 
    case stream_info::use_handle: 
        { 
            chin = infoin.handle_; 
            chin.win32_set_inheritable(true); 
            break; 
        } 
    case stream_info::use_pipe: 
        { 
            infoin.pipe_->rend().win32_set_inheritable(true); 
            chin = infoin.pipe_->rend(); 
            break; 
        } 
    default: 
        { 
            BOOST_ASSERT(false); 
            break; 
        } 
    } 

    si->hStdInput = chin.valid() ? chin.get() : INVALID_HANDLE_VALUE; 

    switch (infoout.type_) 
    { 
    case stream_info::close: 
        { 
            break; 
        } 
    case stream_info::inherit: 
        { 
            chout = file_handle::win32_std(STD_OUTPUT_HANDLE, true); 
            break; 
        } 
    case stream_info::use_file: 
        { 
            HANDLE h = ::CreateFileA(infoout.file_.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 
            if (h == INVALID_HANDLE_VALUE) 
                boost::throw_exception(boost::system::system_error(boost::system::error_code(::GetLastError(), boost::system::get_system_category()), "boost::process::detail::win32_start: CreateFile failed")); 
            chout = file_handle(h); 
            break; 
        } 
    case stream_info::use_handle: 
        { 
            chout = infoout.handle_; 
            chout.win32_set_inheritable(true); 
            break; 
        } 
    case stream_info::use_pipe: 
        { 
            infoout.pipe_->wend().win32_set_inheritable(true); 
            chout = infoout.pipe_->wend(); 
            break; 
        } 
    default: 
        { 
            BOOST_ASSERT(false); 
            break; 
        } 
    } 

    si->hStdOutput = chout.valid() ? chout.get() : INVALID_HANDLE_VALUE; 

    switch (infoerr.type_) 
    { 
    case stream_info::close: 
        { 
            break; 
        } 
    case stream_info::inherit: 
        { 
            cherr = file_handle::win32_std(STD_ERROR_HANDLE, true); 
            break; 
        } 
    case stream_info::redirect: 
        { 
            BOOST_ASSERT(infoerr.desc_to_ == 1); 
            BOOST_ASSERT(chout.valid()); 
            cherr = file_handle::win32_dup(chout.get(), true); 
            break; 
        } 
    case stream_info::use_file: 
        { 
            HANDLE h = ::CreateFileA(infoerr.file_.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 
            if (h == INVALID_HANDLE_VALUE) 
                boost::throw_exception(boost::system::system_error(boost::system::error_code(::GetLastError(), boost::system::get_system_category()), "boost::process::detail::win32_start: CreateFile failed")); 
            cherr = file_handle(h); 
            break; 
        } 
    case stream_info::use_handle: 
        { 
            cherr = infoerr.handle_; 
            cherr.win32_set_inheritable(true); 
            break; 
        } 
    case stream_info::use_pipe: 
        { 
            infoerr.pipe_->wend().win32_set_inheritable(true); 
            cherr = infoerr.pipe_->wend(); 
            break; 
        } 
    default: 
        { 
            BOOST_ASSERT(false); 
            break; 
        } 
    } 

    si->hStdError = cherr.valid() ? cherr.get() : INVALID_HANDLE_VALUE; 

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

    boost::shared_array<char> cmdline;

    boost::scoped_array<char> executable;
    // exe could be "", in which case call CreateProcess with NULL first argument
    if (exe.size()) {
        executable.reset(new char[exe.size() + 1]);
#if defined(__CYGWIN__) || defined(_SCL_SECURE_NO_DEPRECATE)
        ::strcpy(executable.get(), exe.c_str());
#else
        ::strcpy_s(executable.get(), exe.size() + 1, exe.c_str());
#endif
        cmdline = collection_to_win32_cmdline(args);
    }
    else if (args.size() == 1) {
        // No exe and just a single argument. Dont escape anything - pass it through unchanged.
        std::string const &c = args.front();
        cmdline.reset(new char[c.size() + 1]);
#if defined(__CYGWIN__) || defined(_SCL_SECURE_NO_DEPRECATE) 
        ::strcpy(cmdline.get(), c.c_str());
#else 
        ::strcpy_s(cmdline.get(), c.size() + 1, c.c_str());
#endif 
    }
    else {
        cmdline = collection_to_win32_cmdline(args);
    }

    boost::scoped_array<char> workdir(new char[setup.work_directory.size() + 1]); 
#if defined(__CYGWIN__) || defined(_SCL_SECURE_NO_DEPRECATE) 
    ::strcpy(workdir.get(), setup.work_directory.c_str()); 
#else 
    ::strcpy_s(workdir.get(), setup.work_directory.size() + 1, setup.work_directory.c_str()); 
#endif 

    boost::shared_array<char> envstrs = environment_to_win32_strings(env); 

    if (!::CreateProcessA(executable.get(), cmdline.get(), NULL, NULL, TRUE, 0, envstrs.get(), workdir.get(), si.get(), &pi)) 
        boost::throw_exception(boost::system::system_error(boost::system::error_code(::GetLastError(), boost::system::get_system_category()), "boost::process::detail::win32_start: CreateProcess failed")); 

    return pi; 
} 
Esempio n. 2
0
inline
pid_t
posix_start(const Executable& exe,
            const Arguments& args,
            const environment& env,
            info_map& infoin,
            info_map& infoout,
            const posix_setup& setup)
{
    pid_t pid = ::fork();
    if (pid == -1) {
        boost::throw_exception
            (system_error("boost::process::detail::posix_start",
                          "fork(2) failed", errno));
    } else if (pid == 0) {
#if defined(F_MAXFD)
        int maxdescs = std::max(::fcntl(0, F_MAXFD), 128); // XXX
#else
        int maxdescs = 128; // XXX
#endif
        try {
            boost::scoped_array< bool > closeflags(new bool[maxdescs]);
            for (int i = 0; i < maxdescs; i++)
                closeflags.get()[i] = true;

            setup_input(infoin, closeflags.get(), maxdescs);
            setup_output(infoout, closeflags.get(), maxdescs);

            for (int i = 0; i < maxdescs; i++)
                if (closeflags.get()[i])
                    ::close(i);

            setup();
        } catch (const system_error& e) {
            ::write(STDERR_FILENO, e.what(), std::strlen(e.what()));
            ::write(STDERR_FILENO, "\n", 1);
            ::exit(EXIT_FAILURE);
        }

        std::pair< std::size_t, char** > argcv =
            collection_to_posix_argv(args);
        char** envp = environment_to_envp(env);
        ::execve(exe.c_str(), argcv.second, envp);
        system_error e("boost::process::detail::posix_start",
                       "execve(2) failed", errno);

        for (std::size_t i = 0; i < argcv.first; i++)
            delete [] argcv.second[i];
        delete [] argcv.second;

        for (std::size_t i = 0; i < env.size(); i++)
            delete [] envp[i];
        delete [] envp;

        ::write(STDERR_FILENO, e.what(), std::strlen(e.what()));
        ::write(STDERR_FILENO, "\n", 1);
        ::exit(EXIT_FAILURE);
    }

    BOOST_ASSERT(pid > 0);

    for (info_map::iterator iter = infoin.begin();
         iter != infoin.end(); iter++) {
        stream_info& si = (*iter).second;

        if (si.m_type == stream_info::use_pipe)
            si.m_pipe->rend().close();
    }

    for (info_map::iterator iter = infoout.begin();
         iter != infoout.end(); iter++) {
        stream_info& si = (*iter).second;

        if (si.m_type == stream_info::use_pipe)
            si.m_pipe->wend().close();
    }

    return pid;
}