1

I am using umask() system call to set file permission. I am using the following code :

umask(S_IWGRP | S_IWOTH);
printf("Enter file name that you want to create:");
scanf(" %s",file);
fd = open(file,O_CREAT|O_WRONLY);

But after executing it file is created with the given below permissions

--wS-----T 1 rvi rvi     0 Oct  2 15:20 test.txt

What is this S bit and T bit for ? and why it is modified automatically ?

5
  • S means the setuid bit is set without actual execute permission and T means the sticky bit is set without write permission for "others".
    – Joseph R.
    Commented Oct 2, 2013 at 10:33
  • Thank you for that.....but how it is automatically modified ?
    – Rex
    Commented Oct 2, 2013 at 10:54
  • No clue. That's why I went for a comment rather than an answer.
    – Joseph R.
    Commented Oct 2, 2013 at 10:56
  • ok...thanx..@JosephR.
    – Rex
    Commented Oct 2, 2013 at 11:23
  • btw, the third line shouldn' it be scanf("%s", &file); ? Commented Oct 2, 2013 at 11:34

1 Answer 1

1

When you call open with O_CREAT, it expects a third argument for the mode of the file. The umask is then masked out from the mode you request. So, for example:

umask(S_IWGRP | S_IWOTH /* == 0022 */);
fd = open(file, O_CREAT|O_WRONLY, 0777);

would create a file with flags 0777 & (~0022) = 0755, i.e., rwxr-xr-x, whereas:

umask(S_IWGRP | S_IWOTH);
fd = open(file, O_CREAT|O_WRONLY, 0666);

would create a file with flags 0666 & (~0022) = 0644, i.e., rw-r--r--. In your case, because you didn't supply a mode argument, the open function takes random data off the stack, so the only thing you know is that the bits masked off with umask will be off. Try supplying your desired mode to the open call; 0777 is a good choice for executable files and 0666 is a good one for non-executables.

2
  • It works....thanx..:)
    – Rex
    Commented Oct 2, 2013 at 12:51
  • a small note, files can be given max 0666 whereas folders 0777, the difference arises from execute flag. Commented Jun 25, 2018 at 22:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .