1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #include "daemonize.h"
- #include "logging.h"
- #include <stdlib.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <unistd.h>
- void daemonize(const char *name)
- {
-
- pid_t pid = fork();
- if (pid < 0) {
- FATAL_ERRORNO("Failed to fork");
- }
- if (pid != 0) {
- LOG_MSG("Daemon launched as %s...\n", name);
- exit(EXIT_SUCCESS);
- }
-
- pid = fork();
- if (pid < 0) {
- FATAL_ERRORNO("Failed to fork");
- } else if (pid > 0) {
- exit(EXIT_SUCCESS);
- }
-
- umask(0);
- if (setsid() < 0) {
- FATAL_ERRORNO("Failed to create process group\n");
- }
- if (chdir("/") < 0) {
- FATAL_ERRORNO("Failed to change to root directory\n");
- }
- close(STDIN_FILENO);
- close(STDOUT_FILENO);
- close(STDERR_FILENO);
- }
|