Difference between revisions of "Daemonise.c"
From Organic Design wiki
m |
m (Nad moved page Daemonise.h to Daemonise.c without leaving a redirect) |
||
(8 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | {{legacy}} | |
+ | <source lang="c"> | ||
+ | // [[[[http://www.organicdesign.co.nz/peerd|peerd]]]] - nodal p2p wiki daemon | ||
+ | // This article and all its includes are licenced under LGPL | ||
+ | // GPL: [[[[http://www.gnu.org/copyleft/lesser.html]]]] | ||
+ | // SRC: [[[[http://www.organicdesign.co.nz/daemonise.c]]]] | ||
+ | // included in [[[[http://www.organicdesign.co.nz/category:peerd/files/C|peerd]]]][[[[http://www.organicdesign.co.nz/peerd.c|/peerd.c]]]] | ||
− | + | int peerdInit(); | |
+ | |||
+ | |||
+ | // ----------------------------------------------------------------------------------------- // | ||
+ | // daemonise.c | ||
+ | |||
+ | int peerdInit() { | ||
// Fork so parent can exit and return control to what invoked it | // Fork so parent can exit and return control to what invoked it | ||
pid_t pid = fork(); | pid_t pid = fork(); | ||
if (pid > 0) exit(EXIT_SUCCESS); | if (pid > 0) exit(EXIT_SUCCESS); | ||
− | if (pid < 0) | + | if (pid < 0) { |
+ | logAdd("daemonise: First fork() failed!"); | ||
+ | exit(EXIT_FAILURE); | ||
+ | } | ||
// Become a new session leader with no controlling term | // Become a new session leader with no controlling term | ||
− | if (setsid() < 0) | + | 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 | // Fork again to be a non-session-leader which can't gain a controlling term | ||
pid = fork(); | pid = fork(); | ||
if (pid > 0) exit(EXIT_SUCCESS); | if (pid > 0) exit(EXIT_SUCCESS); | ||
− | if (pid < 0) | + | if (pid < 0) { |
+ | logAdd("daemonise: second fork() failed!"); | ||
+ | exit(EXIT_FAILURE); | ||
+ | } | ||
+ | errno = 0; | ||
// Should be /home/lc(peer) | // Should be /home/lc(peer) | ||
chdir("/home/peerd"); | chdir("/home/peerd"); | ||
Line 28: | Line 50: | ||
return errno; | return errno; | ||
} | } | ||
+ | </source> | ||
+ | [[Category:C]] |
Latest revision as of 13:11, 13 December 2019
// [[[[http://www.organicdesign.co.nz/peerd|peerd]]]] - nodal p2p wiki daemon
// This article and all its includes are licenced under LGPL
// GPL: [[[[http://www.gnu.org/copyleft/lesser.html]]]]
// SRC: [[[[http://www.organicdesign.co.nz/daemonise.c]]]]
// included in [[[[http://www.organicdesign.co.nz/category:peerd/files/C|peerd]]]][[[[http://www.organicdesign.co.nz/peerd.c|/peerd.c]]]]
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;
}