简体   繁体   English

权限在C中使用popen时出现问题(linux)

[英]permission Problem while using popen in C (linux)

I am trying to access /proc/net/dev file with -r--r--r-- 1 root root 0 privileges using popen . 我正在尝试使用popen-r--r--r-- 1 root root 0特权访问/proc/net/dev文件。 The code is as fairly as: 该代码与:

main(){
FILE *f;
f=popen("/proc/net/dev","r");
pclose(f);}

logged in as a root,after compiling & running the program, I get this: 以根用户身份登录,编译并运行程序后,我得到以下信息:

sh: /proc/net/dev: Permission denied. sh:/ proc / net / dev:权限被拒绝。

Can you please help me through this problem? 您能帮我解决这个问题吗? thank you 谢谢

I think you're looking at this the wrong way, popen is to open a pipe from an application. 我认为您看错了方向,popen是从应用程序打开管道。 /proc/net/dev is not an application but is a file. /proc/net/dev不是应用程序,而是文件。 Try looking at fopen instead. 尝试查看fopen。

f=fopen("/proc/net/dev", "r");
fclose(f);

popen() is for opening a pipe to another process, the path provided must be to an executable file, not a device entry. popen()用于打开到另一个进程的管道,提供的路径必须是可执行文件,而不是设备条目。 You probably want open() or fopen() instead. 您可能需要改为open()fopen()

use read; 使用读取 for more info type man read in a terminal 有关更多信息,请在终端中输入man类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM