|
Lines 103-109
static void daemonize(int lock_fd)
Link Here
|
| 103 |
if (rv < 0 || rv >= PATH_MAX) |
103 |
if (rv < 0 || rv >= PATH_MAX) |
| 104 |
abort(); |
104 |
abort(); |
| 105 |
|
105 |
|
| 106 |
fd = open(pidfile, O_WRONLY|O_CREAT|O_EXCL); |
106 |
fd = open(pidfile, O_WRONLY|O_CREAT|O_EXCL, 0644); |
| 107 |
if (fd < 0) { |
107 |
if (fd < 0) { |
| 108 |
if (errno == EEXIST) |
108 |
if (errno == EEXIST) |
| 109 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "pidfile %s exists, aborting...%d %s", pidfile, errno, strerror(errno)); |
109 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "pidfile %s exists, aborting...%d %s", pidfile, errno, strerror(errno)); |
|
Lines 149-177
static void daemonize(int lock_fd)
Link Here
|
| 149 |
rv = snprintf(buf, sizeof buf, "%d", pid); |
149 |
rv = snprintf(buf, sizeof buf, "%d", pid); |
| 150 |
if (rv < 0 || rv >= sizeof buf) |
150 |
if (rv < 0 || rv >= sizeof buf) |
| 151 |
abort(); |
151 |
abort(); |
| 152 |
write(fd, buf, rv); |
152 |
rv = write(fd, buf, rv); |
|
|
153 |
if (rv) univention_debug(UV_DEBUG_LDAP, UV_DEBUG_WARN, "Failed to write %s: %s", pidfile, strerror(errno)); |
| 153 |
} |
154 |
} |
| 154 |
close(fd); |
155 |
rv = close(fd); |
|
|
156 |
if (rv) univention_debug(UV_DEBUG_LDAP, UV_DEBUG_WARN, "Failed to close %s: %s", pidfile, strerror(errno)); |
| 155 |
|
157 |
|
| 156 |
// Set new file permissions |
158 |
// Set new file permissions |
| 157 |
umask(0); |
159 |
umask(0); |
| 158 |
|
160 |
|
| 159 |
// Change the working directory to the root directory |
161 |
// Change the working directory to the root directory |
| 160 |
chdir("/"); |
162 |
rv = chdir("/"); |
|
|
163 |
if (rv) univention_debug(UV_DEBUG_LDAP, UV_DEBUG_WARN, "Failed to change directory: %s", strerror(errno)); |
| 161 |
|
164 |
|
| 162 |
if ((null = open("/dev/null", O_RDWR)) == -1) { |
165 |
if ((null = open("/dev/null", O_RDWR)) == -1) { |
| 163 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "could not open /dev/null"); |
166 |
univention_debug(UV_DEBUG_LISTENER, UV_DEBUG_ERROR, "could not open /dev/null: %s", strerror(errno)); |
| 164 |
exit(EXIT_FAILURE); |
167 |
exit(EXIT_FAILURE); |
| 165 |
} |
168 |
} |
| 166 |
dup2(null, STDIN_FILENO); |
169 |
dup2(null, STDIN_FILENO); |
| 167 |
dup2(null, STDOUT_FILENO); |
170 |
dup2(null, STDOUT_FILENO); |
| 168 |
if ((log = open("/var/log/univention/listener.log", O_WRONLY | O_CREAT | O_APPEND)) >= 0) { |
171 |
if ((log = open("/var/log/univention/listener.log", O_WRONLY | O_CREAT | O_APPEND, 0640)) >= 0) { |
| 169 |
dup2(log, STDERR_FILENO); |
172 |
dup2(log, STDERR_FILENO); |
| 170 |
close(log); |
173 |
rv = close(log); |
|
|
174 |
if (rv) univention_debug(UV_DEBUG_LDAP, UV_DEBUG_WARN, "Failed to close /var/log/univention/listener.log: %s", strerror(errno)); |
| 171 |
} else { |
175 |
} else { |
| 172 |
dup2(null, STDERR_FILENO); |
176 |
dup2(null, STDERR_FILENO); |
| 173 |
} |
177 |
} |
| 174 |
close(null); |
178 |
rv = close(null); |
|
|
179 |
if (rv) univention_debug(UV_DEBUG_LDAP, UV_DEBUG_WARN, "Failed to close /dev/null: %s", strerror(errno)); |
| 175 |
|
180 |
|
| 176 |
// Close all open file descriptors |
181 |
// Close all open file descriptors |
| 177 |
for (fd = sysconf(_SC_OPEN_MAX); fd > STDERR_FILENO; fd--) |
182 |
for (fd = sysconf(_SC_OPEN_MAX); fd > STDERR_FILENO; fd--) |