Difference between revisions of "Daemonise.c"

From Organic Design wiki
m
m
Line 3: Line 3:
 
if (pid > 0) exit(EXIT_SUCCESS);
 
if (pid > 0) exit(EXIT_SUCCESS);
 
if (pid < 0) exit(logErr("First fork() failed!"));
 
if (pid < 0) exit(logErr("First fork() failed!"));
 
printf("test5\n");
 
 
 
if (setsid() < 0) exit(logErr("setsid() failed!"));
 
if (setsid() < 0) exit(logErr("setsid() failed!"));
 +
printf("test6\n");
 
pid = fork();
 
pid = fork();
 +
printf("test7\n");
 
if (pid > 0) exit(EXIT_SUCCESS);
 
if (pid > 0) exit(EXIT_SUCCESS);
 +
printf("test8\n");
 
if (pid < 0) exit(logErr("Second fork() failed!"));
 
if (pid < 0) exit(logErr("Second fork() failed!"));
 +
printf("test9\n");
 
chdir("/");
 
chdir("/");
 
umask(0);
 
umask(0);

Revision as of 11:34, 21 July 2006

// Convert running program into a system daemon pid_t pid = fork(); if (pid > 0) exit(EXIT_SUCCESS); if (pid < 0) exit(logErr("First fork() failed!")); if (setsid() < 0) exit(logErr("setsid() failed!")); printf("test6\n"); pid = fork(); printf("test7\n"); if (pid > 0) exit(EXIT_SUCCESS); printf("test8\n"); if (pid < 0) exit(logErr("Second fork() failed!")); printf("test9\n"); chdir("/"); umask(0); close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO);