When you want to write a daemon - a program that runs in background and serves during all the time that machine run(or almost all the time) you there are few things that's better to remember.
First of all the daemon should release controlling terminal. If daemon holds controlling terminal SIGHUP is sent when controlling terminal is lost. This happens if CLOCAL flag on the terminal is not set. That's not a good idea to handle these signals as a workaround to keep process running if controlling terminal is lost.
In linux(and maybe in some other OSes) lately CLOCAL is usually set but the developer shouldn't rely on this.
There are few ways to detach from the controlling terminal.
The easy way is to request TIOCNOTTY from the tty device if it supports it:
int tty = open("/dev/tty", O_RDWR); if (tty == -1) return -1; if (ioctl(tty, TIOCNOTTY, (char *)0) == -1) return -1; close(tty);This might not work for in some OSes. There is another opportunity.
Daemon has to create a new session using setsid. The caller of setsid becomes a process leader without controlling terminal. There is one thing with setsid - it creates a new session if caller is not a process group leader.
To do so first of all the daemon should call setsid from its child. The child will become a process leader and will get closer to be a real daemon and the parent will exit taking controlling tty with itself:
#include <unistd.h> int main(int argc, char **argv) { if (fork() == 0) { setsid(); while (1) { /* Daemons like to sleep */ sleep(1); } } return 0; }Next thing is to release current working directory. It's not good if daemon starts in some directory that is placed on filesystem that might be unmounted during the daemon's work. You won't be able to unmount the filesystem in that case. Simply chdir to '/' is a good choice.
Also it'd be nice to close stdin/stdout/stderr file descriptors since the daemon doesn't need them and some system resources could be saved. If the daemon is going to produce some output that's bad idea to use inherited stdout and stderr since you don't know where they might be redirected. It's better to provide new open descriptors(stdout and/or stderr might point to some log file):
#include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> int main(int argc, char **argv) { if (fork() == 0) { int log; setsid(); log = open("/tmp/test.log", O_CREAT|O_APPEND|O_RDWR, 0755); close(0); close(2); dup2(log, 1); printf("Daemon is started\n"); while (1) { /* Daemons like to sleep */ sleep(1); printf("Daemon is sleeping\n"); fflush(NULL); } } return 0; }The output will go to "/tmp/test.log":
*./test *tail /tmp/test.log -f Daemon is started Daemon is sleeping Daemon is sleeping Daemon is sleeping Daemon is sleeping Daemon is sleeping