Negative return value in open system call for file created in /proc file system

StackOverflow https://stackoverflow.com/questions/17825959

  •  04-06-2022
  •  | 
  •  

سؤال

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.

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top