mirror of
https://github.com/optim-enterprises-bv/nDPId.git
synced 2025-10-30 01:42:22 +00:00
Apple/BSD port (#30)
* Add MacOS to Github CI builds. * Fixed libnDPI-4.8 CI build. * Fixed missing include for `struct sockaddr*`. * Reworked IPv4 address and netmask retrieval. Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
This commit is contained in:
48
utils.c
48
utils.c
@@ -141,6 +141,7 @@ int is_path_absolute(char const * const prefix, char const * const path)
|
||||
int daemonize_with_pidfile(char const * const pidfile)
|
||||
{
|
||||
pid_str ps = {};
|
||||
int nullfd;
|
||||
|
||||
if (daemonize != 0)
|
||||
{
|
||||
@@ -156,12 +157,44 @@ int daemonize_with_pidfile(char const * const pidfile)
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (daemon(0, 0) != 0)
|
||||
nullfd = open("/dev/null", O_NONBLOCK, O_WRONLY);
|
||||
if (nullfd < 0 || dup2(nullfd, STDIN_FILENO) < 0 || dup2(nullfd, STDOUT_FILENO) < 0 ||
|
||||
dup2(nullfd, STDERR_FILENO) < 0)
|
||||
{
|
||||
logger_early(1, "daemon: %s", strerror(errno));
|
||||
logger_early(1, "Opening /dev/null or replacing stdin/stdout/stderr failed: %s", strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// For compatiblity reasons, we use the UNIX double fork() technique.
|
||||
|
||||
switch (fork())
|
||||
{
|
||||
case 0:
|
||||
break;
|
||||
case -1:
|
||||
logger_early(1, "Could not fork (first time): %s", strerror(errno));
|
||||
return 1;
|
||||
default:
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (chdir("/") < 0 || setsid() < 0)
|
||||
{
|
||||
logger_early(1, "chdir() / setsid() failed: %s", strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
switch (fork())
|
||||
{
|
||||
case 0:
|
||||
break;
|
||||
case -1:
|
||||
logger_early(1, "Could not fork (second time): %s", strerror(errno));
|
||||
return 1;
|
||||
default:
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (create_pidfile(pidfile) != 0)
|
||||
{
|
||||
return 1;
|
||||
@@ -401,6 +434,17 @@ __attribute__((format(printf, 2, 3))) void logger_early(int is_error, char const
|
||||
log_to_console = old_log_to_console;
|
||||
}
|
||||
|
||||
int set_fd_cloexec(int fd)
|
||||
{
|
||||
int flags = fcntl(fd, F_GETFD, 0);
|
||||
|
||||
if (flags < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return fcntl(fd, F_SETFD, FD_CLOEXEC);
|
||||
}
|
||||
|
||||
char const * get_nDPId_version(void)
|
||||
{
|
||||
return "nDPId version "
|
||||
|
||||
Reference in New Issue
Block a user