Background
On Linux systems, Application Logs exist in subdirectories of /var/log
, which is owned by root/root
and has 755 permissions on my system. For example, I see /var/log/mysql
and /var/log/samba
.
Question
If I want a myapp to be able to write into a /var/log/myapp
, what is the canonical way of accomplishing this in C/C++?
Thoughts
Do I have to do something crazy like setuid root
if I don't want to sudo a_setup_script.sh
? Note that I am aware of the syslog
routines, but they are insufficient for my needs (I need to log much more information, separated into different files, hence the need for the subdirectory).
Do I need to look into a combination of Ubuntu packaging (to set up the directory) and direct file IO into the subdirectory (by myapp)?
I would like to follow standards as much as possible.
Addendum
I forgot to mention, myapp is actually a daemon processes (a server that listens to clients) so it wouldn't be so bad to have a myapp_user which actually runs/starts the process.
ANSWER
For Ubuntu, the best solution appears to be rsyslog
, a powerful, modern replacement for syslog
. It will generate files/directories as necessary, it has a built-in language for flexible routing of syslog
entries, and it uses the simple, old syslog
API at the C/C++ level. To store routing information, you can define your own encoding of the text message in C/C++, in conjunction with a proper rsyslog.conf
to handle the decoding.
No, no no no. No suid for such stuff. These logs are managed by a process known as "syslog" and there is an API to send messages to this logger:
void openlog(const char *ident, int option, int facility);
void syslog(int priority, const char *format, ...);
void closelog(void);
Or you can type 'man syslog' on the command line and get all the info :-)
Update: you will need permissions to edit syslog's configuration file to send message to a separate log file, otherwise they will end up in the default location (probably /var/log/syslog).