Access denied while writing file to "DiagnosticReports"

Viewed 52

Can anyone please explain to me why I cannot open file for writing (macOS) under /Library/Logs/DiagnosticReports (no matter the reason).

I know that this directory is owned by root, but when I'm using the terminal with the command touch foo, the file does get created without any problems (and I'm running as the same non-root user).

And when I'm trying to use the open syscall in my C program, I get error 2 (which is access denied).

That's the code that causes the error.

fd = open(path, O_RDWR);

Would like to know if there's a different between me running this program and me using the terminal to write to a file (again, same user)

1 Answers

The system does not permit you to open a directory as if it were a regular file.

After open (and many other system or library routines) fails, you can use perror (declared in <stdio.h>) to print a message about the error (based on the information set in errno). In this case, perror("open") prints “open: Is a directory” (or something similar).

Related