How to add daemon stop, start and report function to this daemon code?
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
int main(void) {
/* Our process ID and Session ID */
pid_t pid, sid;
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0) {
exit(EXIT_SUCCESS);
}
/* Change the file mode mask */
umask(0);
/* Open any logs here */
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0) {
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Change the current working directory */
if ((chdir("/")) < 0) {
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
/* Daemon-specific initialization goes here */
/* The Big Loop */
while (1) {
/* Do some task here ... */
sleep(30); /* wait 30 seconds */
}
exit(EXIT_SUCCESS);
}
To create a daemon, you need a background process whose parent process is init. In the code above, _daemon creates a child process and then kills the parent process. In this case, your new process will be a subprocess of init and will continue to run in the background.
-m, --make-pidfile Used when starting a program that does not create its own pid file. This option will make start-stop-daemon create the file referenced with --pidfile and place the pid into it just before executing the process. Note, the file will only be removed when stopping the program if --remove-pidfile is used.
Daemon Thread Typically in C/C++ (Linux Environment) one would create a daemon using fork(). fork() creates a new process by duplicating the calling process. Here the parent process would exit leaving the the child process behind.
Issue the kill -15 command with the process identifier number to stop the daemons. For AIX® and Linux x86_64 GPFS™ file systems, issue the command dmkilld to stop the recall daemons. Verify that the daemons are no longer running.
/var/run/mydaemonname.pid
so that you can easily look up the pid later.There are some complications around stop/start, but if I'm understanding the question correctly, this should get you on the right track.
Edit: Added pid file as suggested by Dummy00001 in a comment below.
First, you probably don't have to do so much forking and house keeping yourself: http://linux.die.net/man/3/daemon
Next, remember that your daemon's interface to the world is probably through some sort of shell script that you also write located in /etc/init.d or whatever other distro-defined place.
So for the above answer, your shell script would send those signals to the pid of the process. There probably is a better way though. Signaling like above is a one way process, your controlling script has to jump through race-condition-prone and fragile hoops in order to confirm if the daemon successfully stopped or restarted. I would look for precedence and examples in /etc/init.d.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With