【发布时间】:2023-04-08 19:04:01
【问题描述】:
我正在尝试提升我的程序的权限,将文件写入系统位置。我在 OSX 上的 C 中执行此操作,方法是派生一个使用 authopen 创建和写入文件的子进程。
我可以创建文件,但是我很难向其中写入字符串。从authopen 的手册页中,如果未声明-stdoutpipe,我可以使用-w 将stdin 指向文件。我不想从stdin 读取数据,但我想将一个常量 str 写入文件。
我发现手册页上对-stdoutpipe 的描述令人困惑,网上也没有关于如何使用此标志的示例。谁能提供任何建议如何做到这一点?
我的代码:
pid_t processId = fork();
if (processId == 0) {
//in child process
const char * authopenPath = "/usr/libexec/authopen";
//Create the file fromProg if it does not exist. This works OK.
execl(authopenPath,
authopenPath,
"-c",
"/etc/fromProg",
NULL);
//This is where I need help.
execl(authopenPath,
authopenPath,
"-stdoutpipe", //<- Not sure how to write a string to file using this
//-w -a", //<- Or this
"/etc/fromProg",
NULL);
exit(0);
}
【问题讨论】:
标签: c pipe exec fork osx-mavericks