C编程-使用sudo特权执行另一个程序 [英] C programming - execute another program with sudo privileges

查看:205
本文介绍了C编程-使用sudo特权执行另一个程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C程序,可以打开mp3并将jpg图稿提取到同一文件夹中.如果我没有root权限执行此程序,则会崩溃.如果我用sudo执行它,则正常工作.

i have a C program that opens an mp3 and extract the jpg artwork in the same folder. If i execute this program with no root privileges i get a crash. If i execute it with sudo it works normally.

现在,我需要谁发动以前的程序中另一个C程序时,它需要对所选择的MP3 JPG格式的作品.

Now, i need another C programs who launch the previous program when it needs a jpg artwork for the selected mp3.

我试图通过甚至在命令中使用sudo调用它们来调用popen("./firstProgram test.mp3","r")函数或system("/(绝对路径)/firstProgram test.mp3")函数.否,无论是相对还是绝对路径.但是似乎没有任何版本可以使用.

I tried to call popen("./firstProgram test.mp3" , "r") function or system("/(absolute path)/firstProgram test.mp3") function by calling them even with sudo in the command or not and either with relative or absolute paths. But no version seems to work.

如何成功地从第二个程序启动第一个程序?

How can i launch the first program from the second one with success?

谢谢!

推荐答案

Unix(Linux)系统中可能永远包含有C编程手册.查看第2节系统调用".

Unix (Linux) systems have contained a C Programming Manual in them since possibly forever. Look in Section 2, "System Calls".

此Wikipedia页面介绍了Unix手册的部分"

This Wikipedia Page explains the Unix Manual "sections"

这是本手册的第2节,您可以阅读有关系统调用"的信息

It is section 2 of the manual you can read about "System Calls"

尝试命令:man 2 setuid

Try the command: man 2 setuid

这将为您提供setuid()系统调用的手册,我认为这是您想要的.

This will give you the manual for the setuid() system call which I think is what you want.

该手册页还将列出您可能想要的对其他相关系统调用的引用.

That manual page will also list references to other related system calls that may be what you want.

在编译C程序并使用进行低级硬件访问的系统调用时,请记住对gcc使用-O2或-O3选项.手册中有提及.

Remember when compiling C programs and using system calls that do low-level hardware access, to use the -O2, or -O3 option to gcc. There is a mention of it in the manual.

最终,setuid()系统调用使一个用户启动的正在运行的进程将该正在运行的进程的UID更改为以其他用户身份运行. (例如,即使Apache是​​由root启动的,您也可能会看到它以"apache"的身份运行).

Ultimately the setuid() system call makes a running process started by one user change the UID of that running process to be running as some other user. (For example, you may see the Apache running as "apache", even though it was started by root).

setuid(0)让您成为root.

setuid(0) lets you be root.

这篇关于C编程-使用sudo特权执行另一个程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆