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 withumask 0
beforehand, this code should work as expected.