Question

I have created a file in /proc named "test" (it was created in kernel). The file exists. When I want to open it in user level it returns negative.

int fd;
if((fd=open("/proc/test","O_RDONLY"))<0){ 
      perror("open"); 
}

The error that I see is open: File exists. I have seen this question but it is not my case.

Was it helpful?

Solution

You need parentheses in there (now fixed in the question), and the second argument to open() is not a string:

#include <fcntl.h>

int fd;
if ((fd = open("/proc/test", O_RDONLY)) < 0)
    perror("open");

I'm not convinced it was a good idea to create a file of any sort in the /proc file system. In fact, I'm a bit surprised you were allowed to. If you are learning to program as root, I hope you have good backups.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top