gpt4 book ai didi

c++ - boost 进程运行 su

转载 作者:行者123 更新时间:2023-11-30 04:55:53 48 4
gpt4 key购买 nike

我使用 boost 1_65_1 并尝试在 MacOS 上运行一个进程。我的主要进程以 root 身份运行我需要以普通用户身份启动子启动。我的命令行:su user_name -c "full 可执行文件的路径"。当我以 root 身份在终端中运行命令行时,它正常工作。

当我尝试使用 Boost Process 运行它时,进程“su”正在运行但立即退出并出现错误 2。

我运行流程的代码:

void RunProcess(std::string a_PathToExe, std::vector<std::string> a_Args, io_servicePtr a_pIoServicePtr,
ProcessExitCallBack a_pProcessExitCallBack) {

Error l_Error;
std::stringstream l_ShellCommandLine;
ProcessHandlePtr l_pProcess = NULL;
boost::process::group l_Group;
boost::shared_ptr<boost::process::async_pipe> l_PipeOutputStream =
boost::shared_ptr<boost::process::async_pipe>(new boost::process::async_pipe(*(a_pIoServicePtr.get())));
boost::shared_ptr<boost::process::async_pipe> l_PipeError =
boost::shared_ptr<boost::process::async_pipe>(new boost::process::async_pipe(*(a_pIoServicePtr.get())));

try {

l_ShellCommandLine << "\"" << a_PathToExe << "\""
<< " ";
for (size_t i = 0; i < a_Args.size(); ++i) {
l_ShellCommandLine << a_Args.at(i) << " ";
}

l_pProcess = ProcessHandlePtr(new boost::process::child(
l_ShellCommandLine.str(), boost::process::std_in.close(),
boost::process::std_out > *(l_PipeOutputStream.get()), boost::process::std_err > *(l_PipeError.get()),
*(a_pIoServicePtr.get()), l_Group, boost::process::shell,
boost::process::on_exit = [](int exit, const std::error_code &ec_in) {
WRITE_INFO_LOG("The process exit with error %d %s", exit, ec_in.message().c_str());
}));

l_PID = l_pProcess->id();
WaiteForProcessToExit(l_pProcess, l_PipeOutputStream, l_PipeError, a_pProcessExitCallBack);
}

catch (std::exception &e) {

} catch (...) {
}
return;
}

void WaiteForProcessToExit(ProcessHandlePtr a_pProcessHandle,
boost::shared_ptr<boost::process::async_pipe> a_PipeOutputStream,
boost::shared_ptr<boost::process::async_pipe> a_PipeError,
ProcessExitCallBack a_pProcessExitCallBack) {

std::array<char, 1024> buffer = std::array<char, 1024>{};
a_PipeOutputStream->async_read_some(
boost::process::buffer(buffer), [=](boost::system::error_code ec, size_t transferre) {
if (transferre > 0) {
std::string Line = std::string(buffer.data());
WRITE_INFO_LOG("%s", Line.c_str());
}

if (ec != 0) {

WRITE_INFO_LOG("The Process %d has exit with error code %d", a_pProcessHandle->id(), ec);
if (a_pProcessExitCallBack != NULL) {
a_pProcessExitCallBack(a_pProcessHandle->id(), Error());
}
} else {
WaiteForProcessToExit(a_pProcessHandle, a_PipeOutputStream, a_PipeError, a_pProcessExitCallBack);
}
});
}

为什么子进程没有运行?提前致谢

最佳答案

首先,您没有显示足够的代码。

其次,如果你使用 su,你不应该要求 bp::shell

第三,该代码看起来像一个 C 编译器跑掉了。为什么这么复杂?为什么全是动态内存管理?为什么要异步,如果您所做的只是等待完成?

这是我的简化版本,它对我有用。或许您可以找到您之前所做的不同的取景器细节:

同步,C++:

Live On Coliru

#include <iostream>
#include <boost/asio.hpp>
#include <boost/process.hpp>
#include <boost/process/async.hpp>

namespace bp = boost::process;

struct ProcessResult {
std::string out, err;
int exitcode;
};

ProcessResult RunProcess(std::string exe, std::vector<std::string> args, boost::asio::io_service& io) {
std::future<std::string> out, err;
bp::group group;

bp::child child(
bp::search_path(exe), args, bp::std_in.close(), bp::std_out > out, bp::std_err > err, io, group
);

child.wait();
return { out.get(), err.get(), child.exit_code() };
}

int main() {
boost::asio::io_service io;

// keep work to avoid io.run to complete early
auto work = boost::asio::make_work_guard(io);
std::thread io_thread([&io] { io.run(); });

auto pstree = RunProcess("su", { "sehe", "-c", "/usr/bin/free" }, io);

std::cout << "Exitcode: " << pstree.exitcode << "\n";
std::cout << "Output: " << pstree.out << "\n";
std::cout << "Error: " << pstree.err << "\n";

work.reset(); // allow service to complete
io_thread.join();
}

打印(以 root 身份运行时):

Exitcode: 0
Output: total used free shared buff/cache available
Mem: 32832516 21846896 2486924 2963636 8498696 5595284
Swap: 0 0 0

Error:

当不以 root 身份运行时:

Exitcode: 1
Output:
Error: su: must be run from a terminal

异步

您仍然可以将异步与 C++ 风格结合起来。参见例如

关于c++ - boost 进程运行 su,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52858314/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com