title: Controlling a C daemon from another program
body: <p>I'm trying to control a C daemon program from another userspace program.</p>
  
  <h2> <em>- Simple C daemon</em></h2>
  
  <p>This daemon is simply a C program which daemonize itself and log a message every second through syslog.</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;syslog.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;signal.h&gt;
  
  void bye();
  
  int main()
  {
    printf("Daemon starting ...\n");
    openlog("daemon-test", LOG_PID, LOG_DAEMON);
    signal(SIGTERM, bye);
  
    if(0 != daemon(0, 0))
    {
      syslog(LOG_ERR, "Can't daemonize\n");
      return EXIT_FAILURE;
    }
  
    syslog(LOG_INFO, "Daemon started !\n");
  
    while(1)
    {
      syslog(LOG_INFO, "Daemon alive\n");
      sleep(1);
    }
  
    return EXIT_SUCCESS;
  }
  
  void bye()
  {
    syslog(LOG_INFO, "Daemon killed !\n");
    exit(EXIT_SUCCESS);
  }
  </code></pre>
  
  <h2> <em>- Launching and killing the daemon from a C test program</em></h2>
  
  <p>For test purpose I have developped a minimal example. I'm using <code>popen</code> to launch the daemon because I want my program to continue its execution.</p>
  
  <p>After 5 seconds, the test program is supposed to kill the daemon.</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  
  #define DAEMON_NAME       "daemon-test"
  
  int main()
  {
      FILE* pipe = NULL;
      int i = 0;
  
      printf("Launching '%s' program\n", DAEMON_NAME);
      if(NULL == (pipe = popen(DAEMON_NAME, "re")))
      {
          printf("An error occured launching '%s': %m\n", DAEMON_NAME);
          return EXIT_FAILURE;
      }
      printf("Program '%s' launched\n", DAEMON_NAME);
  
      while(i&lt;5)
      {
          printf("Program alive !\n");
          sleep(1);
          i++;
      }
  
      if(NULL == (pipe = popen("killall " DAEMON_NAME, "re")))
      {
          printf("An error occured killing '%s' program: %m\n", DAEMON_NAME);
          return EXIT_FAILURE;
      }
      printf("Program '%s' killed\n", DAEMON_NAME);
  
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p><strong><em>Test program log:</em></strong></p>
  
  <pre><code>$ ./popenTest 
  Launching 'daemon-test' program
  Program 'daemon-test' launched
  Program alive !
  Program alive !
  Program alive !
  Program alive !
  Program alive !
  Program 'daemon-test' killed
  </code></pre>
  
  <p><strong><em>Syslog:</em></strong></p>
  
  <pre><code>Jun 25 13:58:15 PC325 daemon-test[4445]: Daemon started !
  Jun 25 13:58:15 PC325 daemon-test[4445]: Daemon alive
  Jun 25 13:58:16 PC325 daemon-test[4445]: Daemon alive
  Jun 25 13:58:17 PC325 daemon-test[4445]: Daemon alive
  Jun 25 13:58:18 PC325 daemon-test[4445]: Daemon alive
  Jun 25 13:58:19 PC325 daemon-test[4445]: Daemon alive
  Jun 25 13:58:20 PC325 daemon-test[4445]: Daemon alive
  Jun 25 13:58:20 PC325 daemon-test[4445]: Daemon killed !
  </code></pre>
  
  <p>So I'm able to launch and kill the daemon from my C program, however I would like to improve the behaviour in some specific cases.</p>
  
  <h2> <em>- Handling daemon crash</em></h2>
  
  <p>The daemon may fail at some point, in that case the control program should be notified so it can be relaunched. My problem is to detect that the daemon has been stopped.</p>
  
  <p>I have though about launching a thread waiting for daemon termination by a call to <code>pclose</code>, however it won't work as the daemonization has already closed file descriptors and detached the process.</p>
  
  <p>So I'm looking for the best way to have the program notified on daemon exit.</p>
  
  <p>I could poll using linux calls with <code>exec</code> family (such as <code>pgrep daemon-test</code> or <code>ps aux | grep daemon-test</code>) but I think there are more efficient way to achieve that.</p>
  
  <h2> <em>- Handling test program error</em></h2>
  
  <p>If the test program is killed or fails before it kills the daemon, at next execution, two instances of the daemon will run at the same time.</p>
  
  <p><strong><em>Test program log:</em></strong></p>
  
  <pre><code>$ ./popenTest 
  Launching 'daemon-test' program
  Program 'daemon-test' launched
  Program alive !
  Program alive !
  Program alive !
  ^C
  $ ./popenTest 
  Launching 'daemon-test' program
  Program 'daemon-test' launched
  Program alive !
  Program alive !
  Program alive !
  Program alive !
  Program alive !
  Program 'daemon-test' killed
  </code></pre>
  
  <p><strong><em>Syslog:</em></strong></p>
  
  <pre><code>Jun 25 14:17:25 PC325 daemon-test[4543]: Daemon started !
  Jun 25 14:17:25 PC325 daemon-test[4543]: Daemon alive
  Jun 25 14:17:26 PC325 daemon-test[4543]: Daemon alive
  Jun 25 14:17:27 PC325 daemon-test[4543]: Daemon alive
  Jun 25 14:17:28 PC325 daemon-test[4543]: Daemon alive
  Jun 25 14:17:29 PC325 daemon-test[4543]: Daemon alive
  Jun 25 14:17:29 PC325 daemon-test[4547]: Daemon started !
  Jun 25 14:17:29 PC325 daemon-test[4547]: Daemon alive
  Jun 25 14:17:30 PC325 daemon-test[4543]: Daemon alive
  Jun 25 14:17:30 PC325 daemon-test[4547]: Daemon alive
  Jun 25 14:17:31 PC325 daemon-test[4543]: Daemon alive
  Jun 25 14:17:31 PC325 daemon-test[4547]: Daemon alive
  Jun 25 14:17:32 PC325 daemon-test[4543]: Daemon alive
  Jun 25 14:17:32 PC325 daemon-test[4547]: Daemon alive
  Jun 25 14:17:33 PC325 daemon-test[4543]: Daemon alive
  Jun 25 14:17:33 PC325 daemon-test[4547]: Daemon alive
  Jun 25 14:17:34 PC325 daemon-test[4543]: Daemon alive
  Jun 25 14:17:34 PC325 daemon-test[4547]: Daemon alive
  Jun 25 14:17:34 PC325 daemon-test[4543]: Daemon killed !
  Jun 25 14:17:34 PC325 daemon-test[4547]: Daemon killed !
  </code></pre>
  
  <p>I want to avoid this situation by checking if there are already daemon instances running. If not I can launch the daemon from control program.</p>
  
  <p>Otherwise if one or several instances of the daemon are running I shall kill them before launching a new one.</p>
  
  <p>This could be achieved by calling <code>killall daemon-test</code> but calling this command at each execution doesn't satisfy me because it's useless most of the time.</p>
  
  <p>Moreover I would like to explicitly log the situation at each execution and though I want to know exactly how many instances were running in that case.</p>
  
  <p>Once again this can be resolved easilly using linux command calls, but I'm looking for the most efficient way to do it.</p>
  
  <p><strong>Does anybody knows how I could implement daemon process control without having to rely on linux command calls ?</strong></p>
  
  <hr>
  
  <p><strong><em>EDIT:</em></strong> <em>June 26th 2018</em></p>
  
  <p>I should have precised it from the beginning but my aim is to be able to monitor the daemon process <strong>without</strong> having to modify its code.</p>
  
  <p>So the daemon doesn't write its pid to a file and is always detached from its caller.</p>