Difference between revisions of "Daemonise.c"

From Organic Design wiki
m
m (Nad moved page Daemonise.h to Daemonise.c without leaving a redirect)
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
// Set name as seen in ps list
+
{{legacy}}
int i;
+
<source lang="c">
char *tmp = *argv;
+
// [[[[http://www.organicdesign.co.nz/peerd|peerd]]]] - nodal p2p wiki daemon
for (i=0; i<30; i++) tmp[i] = 'x';
+
// This article and all its includes are licenced under LGPL
sprintf(*argv,"peerd: %s (http%d)",peer,port);
+
// GPL: [[[[http://www.gnu.org/copyleft/lesser.html]]]]
printf("argv[0] = \"%s\"\n",*argv);
+
// SRC: [[[[http://www.organicdesign.co.nz/daemonise.c]]]]
// Fork so parent can exit and return control to what invoked it
+
// included in [[[[http://www.organicdesign.co.nz/category:peerd/files/C|peerd]]]][[[[http://www.organicdesign.co.nz/peerd.c|/peerd.c]]]]
pid_t pid = fork();
 
if (pid > 0) exit(EXIT_SUCCESS);
 
if (pid < 0) exit(logErr("First fork() failed!"));
 
  
// Become a new session leader with no controlling term
+
int peerdInit();
if (setsid() < 0) exit(logErr("setsid() failed!"));
 
  
// 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) exit(logErr("Second fork() failed!"));
 
  
chdir("/"); // Don't keep any dir in use
+
// ----------------------------------------------------------------------------------------- //
umask(0);   // Don't inherit any file perms mask
+
// daemonise.c
//close(STDIN_FILENO);
+
 
//close(STDOUT_FILENO);
+
int peerdInit() {
//close(STDERR_FILENO);
+
 
 +
// 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;
 +
}
 +
</source>
 +
[[Category:C]]

Latest revision as of 13:11, 13 December 2019

Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, now this page is for historic record only.
// [[[[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;
	}