Reputation: 28860
I have the following snippet of code running inside a massive Linux daemon.
I'm trying to debug to a log file, but when the log file exists, the fopen
fails
if ( ( debugFILE = fopen( "/home/lala/debug.log", "a" ) ) == NULL )
{
perror("error: ");
}
The error I get is: "Permission denied".
This is a the output of ls of the specific file:
----rw---- 1 lala lala 0 Mar 11 18:26 debug.log
First, why the file was created in the firstplace with these permissions. Second, why the fopen succeeds when creating, but not when opening ?
Upvotes: 3
Views: 4915
Reputation: 3117
fopen
creates files with permission0666
modified by the process'sumask
.
So if you do not manually change the files permissions within your program or after your program has finished.
The process will most likely have a wrong umask.
Do you set the umask
within your programm or within the context of the calling process? Your umask -S
output actually looks fine (looks like umask 002
).
Upvotes: 4
Reputation: 8195
The "a" option will always create a file if it doesn't exist, and if successful return a valid pointer. It's created according to the umask setting of the process - and in your case the process is creating a file without proper permissions, hence the next time fopen
fails. If you don't want to mess with the umask, just call this before and after the fopen
:
chmod("/home/lala/debug.log", 0644);
It's fine to call chmod
in this way if the file doesn't exist, it'll just do nothing (except for setting errno
appropriately).
Upvotes: 4