I have a question about open() system call. I am writing a C code, in which I need to open all files from a provided directory in read only mode. Before the open call I check if the user has read permission to this file. Although when the provided directory is /
, a file there swapfile
cannot open and system call fails, while user has read permission.
Can someone tell me why this happens or how to correct it?
As you can see below user has read access, so the file should open.
-rw------- 1 root root 2147483648 Οκτ 22 18:58 swapfile
The code which is responsible for the error:
int fd = open(path, O_RDONLY);
if (fd == -1)
{
perror("open");
exit(1);
}
The error:
open: Permission denied
ls -l
output.