-1

Based on my understanding 000 permission means no one is able to read or write to the file except the root user. Why does the following piece of code work?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

int main(void)
{
    char *message = "Hello world\n";

    int fd = open("greeting.txt", O_CREAT | O_WRONLY, 0000);
    write(fd, message, strlen(message));
    close(fd);
}

Also when I make a named semaphore using sem_open(sem_name, O_CREAT, 0000, 1); the parent and all of the child processes are able to modify it even though the file permission is set to 000.

My understanding of low-level C system calls and semaphores is limited so a thorough explanation would be appriciated.

Amirreza A.
  • 736
  • 4
  • 10
  • 1
    Did you read the docs? ;) – KamilCuk Apr 20 '23 at 15:04
  • 5
    From `man 2 open`: *Note that mode applies only to future accesses of the newly created file; the open() call that creates a read- only file may well return a read/write file descriptor.* – Eugene Sh. Apr 20 '23 at 15:05
  • 2
    @SteveSummit: Questions that can be answered by reading the manual for the function in question have been discouraged since before down-votes existed, even before [RTFM](https://en.wikipedia.org/wiki/RTFM) was created, and it is even the reason RTFM was created. Such questions fail the “do minimal research” threshold. – Eric Postpischil Apr 20 '23 at 15:28
  • @SteveSummit I did not know it as well, but right after reading this question I went to the `man` page and found out. This is something the OP is expected to do before the question is posted. It might be an interesting "fun fact", but a bad question. – Eugene Sh. Apr 20 '23 at 15:36
  • 3
    @Eric and Eugene: But by the same token, virtually every question can be answered by reading some documentation somewhere, so if we applied that rule religiously, there would be no valid questions at all. So I think there's a line — albeit a grey or blurry one — between questions anyone should be able to answer by themselves, versus those that might require some expert interpretation, perhaps from a helpful answerer on SO. Personally, I felt that this was such a question, as is [this one](https://stackoverflow.com/questions/76064808) (although there the prior really is a perfect dup). – Steve Summit Apr 20 '23 at 15:59
  • @SteveSummit: It is not read the documentation somewhere, it is read the documentation for the routine you are using. I can see asking about a question where the primary documentation for the routine being used does not answer the question or does not answer it clearly or it is quite long or it refers to concepts not defined at hand or there is some point to be clarified. I did not vote on this question, but it does not qualify on any of those points. It does not reach the threshold of having done minimal research. – Eric Postpischil Apr 20 '23 at 16:13

1 Answers1

0

From man open https://man7.org/linux/man-pages/man2/open.2.html :

          Note that mode applies only to future accesses of the
          newly created file; the open() call that creates a read-
          only file may well return a read/write file descriptor.
KamilCuk
  • 120,984
  • 8
  • 59
  • 111