Heres code I've been using.
Code:
#include <stdio.h>
int init_daemon();
int main()
{
int success_flag;
printf("Starting Daemon\n");
init_daemon();
/*code goes here*/
return (0);
}
int init_daemon()
{
pid_t pid;
if( ( pid=fork()) < 0)
return (-1); /*fork failed*/
else if(pid !=0)
exit(0); /*parent is destroyed at this point*/
setsid(); /*becomes the session leader*/
chdir("/"); /*changes current dir, to avoid blocking a device from being umounted*/
umask(0); /*clear out the file mode creation mask*/
return (0);
}
That code just makes your process a daemon, if will die right away cause it returns right after that. I got that from Advanced Programming in UNIX environment vy Stevens. Good book. Other implementations call for another fork, info on the net and in Stevens book as well. Maybe place it in init.d to get it launched on startup.