c++ - 升压进程运行 su
问题描述
我使用 boost 1_65_1 并尝试在 MacOS 上运行一个进程。我的主进程以 root 身份运行我需要以普通用户身份启动子启动。我的命令行:su user_name -c "full The path to the Executable file"。当我以 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++:
#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++ 风格结合起来。参见例如
推荐阅读
- python - 使用 Pandas 和 SQLAlchemy 将数据加载到 SQL Server 时出现问题
- mysql - 将备份转储还原到 MySql 数据库时出现问题
- angular - IONIC 4:任务“:app:transformDexArchiveWithExternalLibsDexMergerForDebug”执行失败
- android - PIP Activity 导致应用程序不响应 Android Q
- python - 在python中计算每个人在一个月内最后3天的支出
- javascript - 无需刷新整个页面即可更新网页中的图像
- python - Python 从 Elasticsearch 获取数据
- database - MongoDB如何回滚或销毁事务中新创建的集合
- php - 将普通的html表转换为数据表
- android - Webivew onNavigationStateChange 方法在本机反应中多次调用