Search code examples
clinuxmkdir

Creating a directory in "/var/log/" using C in Linux


I need to create a directory in /var/log using a C program which does NOT run with superuser rights. This linked question didn't help me!

I understand that the file permissions of /var/log does not allow us to write in it nor do I want to change it. I use mkdir() to create the directory which fails for obvious reasons.

So, is it possible for a normal (with no root rights) C program to create a directory in /var/log?

My goal: To create a directory using a C program in /var/log without changing the parent's file permission. Should I run my program as root? But I would prefer not to.

Kindly help. TIA


Solution

  • You cannot programmatically overturn the filesystem's access control.

    What you could try to do instead is to use an existing logging mechanism that is provided by the system. For example, journald that comes with systemd allows for per-user logging.

    If your program runs as a user, it should only use the user's home directory to store files, wether it being configuration or logging. Think about it this way: What happens if several users want to use your program at the same time?

    If your program really is a system daemon, have a look at other software that runs under their own user. They could either have their own logging directory be prepared by the init script that calles them (running the daemon itself as a different user), or they purge their priviledges during startup. An example is httpd, which needs root priviledges to listen on port 80.