I need to write a Linuxland daemon that talks to our custom hardware. I would like to use the /init process to start, and if necessary, restart it. I have made this entry into the init.<hardware>.rc file that looks like this:
service rsgd /system/bin/rsgd
class main
user system
Here is my daemon (just a skeleton now, but it runs):
#define DAEMONIZE
/* Fork off the parent process */
/* Exit the parent process. */
/* Change the file mode mask */
/* Create a new SID for the child process */
/* Change the current working directory */
/* Close out the standard file descriptors */
/* Daemon-specific initialization goes here */
/* Do some task here ... */
usleep(100000); /* wait 100 milliseconds */
Nothing fancy. When I build the system, the daemon gets created properly, and lives in /system/bin, as expected. When I boot the system, however, /init continually starts my daemon, many times, until the system eventually runs out of memory!
Is there something my daemon needs to do so /init doesn't think it has died and needs to restart?
Well, as I wrote this originally, I had an "Aha!" moment - /init must be acting on the exit of the parent process! That would explain why it starts another instance, even though the prior one is still around. So I got rid of the code with the DAEMONIZE conditionals, but that just hangs the system immediately after print this message:
Freeing init memory: 248K
So, what is the proper way to write a Linux daemon that /init can manage for me? Any ideas welcome!
Thanks!