LCOV - code coverage report
Current view: top level - lib - daemonize.c (source / functions) Hit Total Coverage
Test: smtpd.info Lines: 15 19 78.9 %
Date: 2015-11-25 19:06:20 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* This file is part of the cmail project (http://cmail.rocks/)
       2             :  * (c) 2015 Fabian "cbdev" Stumpf
       3             :  * License: Simplified BSD (2-Clause)
       4             :  * For further information, consult LICENSE.txt
       5             :  */
       6             : 
       7             : #include <unistd.h>
       8             : 
       9           1 : int daemonize(LOGGER log, FILE* pidfile_handle){
      10           1 :         int pid = fork();
      11           2 :         if(pid < 0){
      12           0 :                 logprintf(log, LOG_ERROR, "Failed to fork\n");
      13           0 :                 if(pidfile_handle){
      14           0 :                         fclose(pidfile_handle);
      15             :                 }
      16           0 :                 return -1;
      17             :         }
      18             : 
      19           2 :         if(pid == 0){
      20           1 :                 close(0);
      21           1 :                 close(1);
      22           1 :                 close(2);
      23           1 :                 setsid();
      24           1 :                 if(pidfile_handle){
      25           1 :                         fclose(pidfile_handle);
      26             :                 }
      27           1 :                 return 0;
      28             :         }
      29             : 
      30             :         else{
      31           1 :                 if(pidfile_handle){
      32           1 :                         fprintf(pidfile_handle, "%d\n", pid);
      33           1 :                         fclose(pidfile_handle);
      34             :                 }
      35           1 :                 return 1;
      36             :         }
      37             : }

Generated by: LCOV version 1.11