Вопрос

I'm currently working through "The Linux programming interface" and in chapter 4 there is an exercise where we have to rewrite the "tee" command. I've done this, but no matter what I do my file permissions (held in variable of type mode_t) aren't being set correctly.

i.e, I have this code:

filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; // filePerms is mode_t
foutputFd = open((argc == 2) ? argv[1] : argv[2], flags, filePerms); // don't worry about the ternary here
if (foutputFd == -1)
  errExit("opening file %s", (argc == 2) ? argv[1] : argv[2]); // ...or here.

After all is said and done, the file in the directory will have permissions rw-r--r-- instead of rw-rw-rw specified by the above flags

Now, I did some research and it looks like the mode argument passed to open() specifies the maximum number of allowed permissions and then it gets changed later anyway (somehow). If this is the case, why is it like this? Why let me specify permissions in the open call anyway?

Any help would be appreciated, I'd like to keep going with this book but this issue is a bit frustrating lol (and I haven't found it explained in the book at all).

Это было полезно?

Решение

This change is generally brought on by the umask setting. If you adjusted it in your shell with umask 0 beforehand, this code should work as expected.

Другие советы

First of all check what you are passing in flags argument. Any file permissions will have some result ONLY when O_CREAT flag is passed.

What is more the effective file permissions is result of flags provided and value of umask as

mode & ~umask.

Check what is umask in your case set to and you will have probably answer for your question. What is more, you can change umask by using umask() syscall.

For more info you can read for example open man page.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top