Daemonise.c

From Organic Design wiki
Revision as of 01:38, 26 July 2007 by Nad (talk | contribs) ({{C}}{{Category:Peerd}})

// [[[[1]]]] - nodal p2p wiki daemonTemplate:C

See also

// This article and all its includes are licenced under LGPL // GPL: [[[[2]]]] // SRC: [[[[3]]]] // included in [[[[4]]]][[[[5]]]]

int peerdInit();


// ----------------------------------------------------------------------------------------- // // daemonise.c

int peerdInit() {

// Fork so parent can exit and return control to what invoked it pid_t pid = fork(); if (pid > 0) exit(EXIT_SUCCESS); if (pid < 0) { logAdd("daemonise: First fork() failed!"); exit(EXIT_FAILURE); }

// Become a new session leader with no controlling term if (setsid() < 0) { logAdd("daemonise: setsid() failed!"); exit(EXIT_FAILURE); }

// Fork again to be a non-session-leader which can't gain a controlling term pid = fork(); if (pid > 0) exit(EXIT_SUCCESS); if (pid < 0) { logAdd("daemonise: second fork() failed!"); exit(EXIT_FAILURE); }

errno = 0; // Should be /home/lc(peer) chdir("/home/peerd");

// Don't inherit any file perms mask umask(0);

//close(STDIN_FILENO); //close(STDOUT_FILENO); //close(STDERR_FILENO);

return errno; }