/* */ #include "daemon.h" #include #include #include namespace aria2 { int daemon(int nochdir, int noclose) { #ifdef HAVE_WORKING_FORK pid_t pid; pid = fork(); if(pid == -1) { return -1; } else if(pid > 0) { _exit(EXIT_SUCCESS); } if(setsid() == -1) { return -1; } pid = fork(); if(pid == -1) { return -1; } else if(pid > 0) { _exit(EXIT_SUCCESS); } if(nochdir == 0) { if(chdir("/") == -1) { return -1; } } if(noclose == 0) { if(freopen("/dev/null", "r", stdin) == 0) { return -1; } if(freopen("/dev/null", "w", stdout) == 0) { return -1; } if(freopen("/dev/null", "w", stderr) == 0) { return -1; } } #endif // HAVE_WORKING_FORK return 0; } } // namespace aria2