diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/system/daemon.cpp | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/system/daemon.cpp')
-rw-r--r-- | util/system/daemon.cpp | 126 |
1 files changed, 63 insertions, 63 deletions
diff --git a/util/system/daemon.cpp b/util/system/daemon.cpp index 130e6c8f45..e05091b26e 100644 --- a/util/system/daemon.cpp +++ b/util/system/daemon.cpp @@ -1,26 +1,26 @@ -#include <util/generic/yexception.h> - +#include <util/generic/yexception.h> + #include <cerrno> #include <cstdlib> #include <util/system/info.h> - + #if defined(_win_) - #include <io.h> + #include <io.h> #else - #include <sys/wait.h> - #include <unistd.h> - #include <fcntl.h> + #include <sys/wait.h> + #include <unistd.h> + #include <fcntl.h> #endif -#include "daemon.h" +#include "daemon.h" #ifdef _unix_ -using namespace NDaemonMaker; +using namespace NDaemonMaker; static bool Fork(EParent parent) { pid_t pid = fork(); - if (pid > 0) { + if (pid > 0) { int status = 0; while (waitpid(pid, &status, 0) < 0 && errno == EINTR) { } @@ -29,83 +29,83 @@ static bool Fork(EParent parent) { } else { return true; } - } else if (pid < 0) { + } else if (pid < 0) { ythrow TSystemError() << "Cannot fork"; - } + } - if (setsid() < 0) { + if (setsid() < 0) { ythrow TSystemError() << "Cannot setsid"; - } + } - pid = fork(); + pid = fork(); - if (pid > 0) { + if (pid > 0) { _exit(0); - } else if (pid < 0) { + } else if (pid < 0) { ythrow TSystemError() << "Cannot second fork"; - } + } return false; -} +} #endif -static void CloseFromToExcept(int from, int to, const int* except) { - (void)from; - (void)to; - (void)except; - +static void CloseFromToExcept(int from, int to, const int* except) { + (void)from; + (void)to; + (void)except; + #ifdef _unix_ int mfd = NSystemInfo::MaxOpenFiles(); for (int s = from; s < mfd && (to == -1 || s < to); s++) { - for (const int* ex = except; *ex >= 0; ++ex) { - if (s == *ex) { + for (const int* ex = except; *ex >= 0; ++ex) { + if (s == *ex) { goto dontclose; - } - } + } + } while (close(s) == -1) { - if (errno == EBADF) { + if (errno == EBADF) { break; - } - if (errno != EINTR) { - ythrow TSystemError() << "close(" << s << ") failed"; - } + } + if (errno != EINTR) { + ythrow TSystemError() << "close(" << s << ") failed"; + } } - dontclose:; + dontclose:; } #endif /* _unix_ */ } bool NDaemonMaker::MakeMeDaemon(ECloseDescriptors cd, EStdIoDescriptors iod, EChDir chd, EParent parent) { - (void)cd; - (void)iod; - (void)chd; - + (void)cd; + (void)iod; + (void)chd; + #ifdef _unix_ if (Fork(parent)) { return true; } if (chd == chdirRoot) { - if (chdir("/")) { + if (chdir("/")) { ythrow TSystemError() << "chdir(\"/\") failed"; - } + } } - int fd[4] = {-1, -1, -1, -1}; - switch (iod) { + int fd[4] = {-1, -1, -1, -1}; + switch (iod) { case openYandexStd: fd[0] = open("yandex.stdin", O_RDONLY); - if (fd[0] < 0) { + if (fd[0] < 0) { ythrow TSystemError() << "Cannot open 'yandex.stdin'"; - } + } fd[1] = open("yandex.stdout", O_WRONLY | O_APPEND | O_CREAT, 660); - if (fd[1] < 0) { + if (fd[1] < 0) { ythrow TSystemError() << "Cannot open 'yandex.stdout'"; - } + } fd[2] = open("yandex.stderr", O_WRONLY | O_APPEND | O_CREAT, 660); - if (fd[2] < 0) { + if (fd[2] < 0) { ythrow TSystemError() << "Cannot open 'yandex.stderr'"; - } + } break; case openDevNull: fd[0] = open("/dev/null", O_RDWR, 0); @@ -120,38 +120,38 @@ bool NDaemonMaker::MakeMeDaemon(ECloseDescriptors cd, EStdIoDescriptors iod, ECh fd[0], fd[1], fd[2], - -1}; - if (closeAll == cd) { + -1}; + if (closeAll == cd) { CloseFromToExcept(0, -1, except); - } else if (closeStdIoOnly == cd) { + } else if (closeStdIoOnly == cd) { CloseFromToExcept(0, 3, except); - } else { + } else { ythrow yexception() << "Unknown close descriptors mode: " << (int)cd; - } + } - switch (iod) { + switch (iod) { case openYandexStd: /* Assuming that open(2) acquires fds in order. */ dup2(fd[0], STDIN_FILENO); - if (fd[0] > 2) { + if (fd[0] > 2) { close(fd[0]); - } + } dup2(fd[1], STDOUT_FILENO); - if (fd[1] > 2) { + if (fd[1] > 2) { close(fd[1]); - } + } dup2(fd[2], STDERR_FILENO); - if (fd[2] > 2) { + if (fd[2] > 2) { close(fd[2]); - } + } break; case openDevNull: dup2(fd[0], STDIN_FILENO); dup2(fd[0], STDOUT_FILENO); dup2(fd[0], STDERR_FILENO); - if (fd[0] > 2) { + if (fd[0] > 2) { close(fd[0]); - } + } break; default: break; @@ -163,6 +163,6 @@ bool NDaemonMaker::MakeMeDaemon(ECloseDescriptors cd, EStdIoDescriptors iod, ECh } void NDaemonMaker::CloseFrom(int fd) { - static const int except[1] = {-1}; + static const int except[1] = {-1}; CloseFromToExcept(fd, -1, except); } |