void daemonize2()

in sample_app/onvif_camera_mock/artifacts/onvif_srvd/src/daemon.c [263:301]


void daemonize2(void (*optional_init)(void *), void *data)
{
    if( !daemon_info.no_fork )
        do_fork();


    // Reset the file mode mask
    umask(0);


    // Create a new process group(session) (SID) for the child process
    // call setsid() only if fork is done
    if( !daemon_info.no_fork && (setsid() == -1) )
        daemon_error_exit("Can't setsid: %m\n");


    // Change the current working directory to "/"
    // This prevents the current directory from locked
    // The demon must always change the directory to "/"
    if( !daemon_info.no_chdir && (chdir("/") != 0) )
        daemon_error_exit("Can't chdir: %m\n");


    if( daemon_info.pid_file && (create_pid_file(daemon_info.pid_file) == -1) )
        daemon_error_exit("Can't create pid file: %s: %m\n", daemon_info.pid_file);


    // call user functions for the optional initialization
    // before closing the standardIO (STDIN, STDOUT, STDERR)
    if( optional_init )
        optional_init(data);


    if( !daemon_info.no_close_stdio && (redirect_stdio_to_devnull() != 0) )
        daemon_error_exit("Can't redirect stdio to /dev/null: %m\n");


    daemon_info.daemonized = 1; //good job
}