MCPcopy Create free account
hub / github.com/SNAS/openbmp / daemonize

Function daemonize

Server/src/openbmp.cpp:94–136  ·  view source on GitHub ↗

* Daemonize the program */

Source from the content-addressed store, hash-verified

92 * Daemonize the program
93 */
94void daemonize() {
95 pid_t pid, sid;
96
97 pid = fork();
98
99 if (pid < 0) // Error forking
100 _exit(EXIT_FAILURE);
101
102 if (pid > 0) {
103 _exit(EXIT_SUCCESS);
104
105 } else {
106 sid = setsid();
107 if (sid < 0)
108 exit(EXIT_FAILURE);
109 }
110
111 //Change File Mask
112 umask(0);
113
114 //Change Directory
115 if ((chdir("/")) < 0)
116 exit(EXIT_FAILURE);
117
118 //Close Standard File Descriptors
119 close(STDIN_FILENO);
120 close(STDOUT_FILENO);
121 close(STDERR_FILENO);
122
123 // Write PID to PID file if requested
124 if (pid_filename != NULL) {
125 pid_t pid = getpid();
126 ofstream pfile(pid_filename);
127
128 if (pfile.is_open()) {
129 pfile << pid << endl;
130 pfile.close();
131 } else {
132 LOG_ERR("Failed to write PID to %s", pid_filename);
133 exit(EXIT_FAILURE);
134 }
135 }
136}
137
138/**
139 * Signal handler

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected