【发布时间】:2017-02-25 04:00:04
【问题描述】:
我当前的 gksudo 命令适用于 Process.spawn_async_with_pipes。但是,如果我用 pkexec 切换 gksudo,它不会显示 pkexec 窗口,而是在没有提示的情况下直接完成命令并且什么也不返回。
当我将 Process.spawn_command_line_sync 与相同的 pkexec 命令一起使用时,它会弹出询问密码的提示,并且命令执行良好并返回结果。
我使用 pkexec 的关键原因是使用 polkit 而不会提示用户后续使用需要 root 权限的命令。
我的 Process.spawn_async_with_pipes 代码的方法如下所示。
我需要有关如何使 pkexec 作为后台进程工作的帮助,即提示应该阻止 gui,但是一旦用户提供密码,它应该将控制权返回给 gui 并继续在后台执行。这正是 gksudo 发生的情况。
提前致谢
这是异步方法
public int execute_sync_multiarg_command_pipes(string[] spawn_args) {
debug("Starting to execute async command: "+string.joinv(" ", spawn_args));
spawn_async_with_pipes_output.erase(0, -1); //clear the output buffer
MainLoop loop = new MainLoop ();
try {
string[] spawn_env = Environ.get();
Pid child_pid;
int standard_input;
int standard_output;
int standard_error;
Process.spawn_async_with_pipes ("/",
spawn_args,
spawn_env,
SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
null,
out child_pid,
out standard_input,
out standard_output,
out standard_error);
// capture stdout:
IOChannel output = new IOChannel.unix_new (standard_output);
output.add_watch (IOCondition.IN | IOCondition.HUP, (channel, condition) => {
return process_line (channel, condition, "stdout");
});
// capture stderr:
IOChannel error = new IOChannel.unix_new (standard_error);
error.add_watch (IOCondition.IN | IOCondition.HUP, (channel, condition) => {
return process_line (channel, condition, "stderr");
});
ChildWatch.add (child_pid, (pid, status) => {
// Triggered when the child indicated by child_pid exits
Process.close_pid (pid);
loop.quit ();
});
loop.run ();
} catch(SpawnError e) {
warning("Failure in executing async command ["+string.joinv(" ", spawn_args)+"] : "+e.message);
spawn_async_with_pipes_output.append(e.message);
}
debug("Completed executing async command["+string.joinv(" ", spawn_args)+"]...");
return 0;
}`
它是这样称呼的:
execute_sync_multiarg_command_pipes({"pkexec", COMMAND_USING_SUDO[6]});
【问题讨论】:
-
一些事情。您使用的是 Linux 还是 Windows?您可能需要检查您的链接。它完全对我有用。第三,当您引用的文档是针对
spawn_async时,您为什么要使用spawn_async_with_pipes?只是好奇。不过你问的问题很好。 -
@oldtechaa 是否还有适用于 Windows 的策略工具包端口?
-
请提供MVCE(即我们可以查看的最小代码示例)。
-
@Jens,说得好。不知道我为什么这么问。
-
@JensMühlenhoff 我提供了我的异步方法以及它的调用方式。
标签: process glib stdio vala gio