int set_proc_title( const char* format, ... );
int daemonize( void );
-int daemonize_write_pid( FILE* pidfile );
void* safe_malloc(int size);
void* safe_calloc(int size);
the terminal, and redirects the standard streams (stdin, stdout, stderr) to /dev/null.
*/
int daemonize( void ) {
- return daemonize_write_pid( NULL );
-}
-/**
- @brief Become a proper daemon, and report the childs process ID.
- @return 0 if successful, or -1 if not.
-
- Call fork(). If pidfile is not NULL, the parent writes the process ID of the child
- process to the specified file. Then it exits. The child moves to the root
- directory, detaches from the terminal, and redirects the standard streams (stdin,
- stdout, stderr) to /dev/null.
- */
-int daemonize_write_pid( FILE* pidfile ) {
pid_t f = fork();
if (f == -1) {
return 0;
} else { // We're in the parent...
- if( pidfile ) {
- fprintf( pidfile, "%ld\n", (long) f );
- fclose( pidfile );
- }
_exit(0);
}
}