title: Creating a daemon with stop, start functionality in C
body: <p>How to add daemon stop, start and report function to this daemon code?</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;syslog.h&gt;
  #include &lt;string.h&gt;
  
  int main(void) {
  
          /* Our process ID and Session ID */
          pid_t pid, sid;
  
          /* Fork off the parent process */
          pid = fork();
          if (pid &lt; 0) {
                  exit(EXIT_FAILURE);
          }
          /* If we got a good PID, then
             we can exit the parent process. */
          if (pid &gt; 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 &lt; 0) {
                  /* Log the failure */
                  exit(EXIT_FAILURE);
          }
  
  
  
          /* Change the current working directory */
          if ((chdir("/")) &lt; 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);
  }
  </code></pre>