diff options
author | dvshkurko <dvshkurko@yandex-team.ru> | 2022-02-10 16:45:52 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:52 +0300 |
commit | c768a99151e47c3a4bb7b92c514d256abd301c4d (patch) | |
tree | 1a2c5ffcf89eb53ecd79dbc9bc0a195c27404d0c /util/system | |
parent | 321ee9bce31ec6e238be26dbcbe539cffa2c3309 (diff) | |
download | ydb-c768a99151e47c3a4bb7b92c514d256abd301c4d.tar.gz |
Restoring authorship annotation for <dvshkurko@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'util/system')
-rw-r--r-- | util/system/daemon.cpp | 32 | ||||
-rw-r--r-- | util/system/daemon.h | 12 | ||||
-rw-r--r-- | util/system/daemon_ut.cpp | 184 | ||||
-rw-r--r-- | util/system/file.cpp | 6 | ||||
-rw-r--r-- | util/system/file_lock.cpp | 2 | ||||
-rw-r--r-- | util/system/flock.cpp | 2 | ||||
-rw-r--r-- | util/system/pipe.cpp | 44 | ||||
-rw-r--r-- | util/system/pipe.h | 10 | ||||
-rw-r--r-- | util/system/shellcommand.cpp | 16 | ||||
-rw-r--r-- | util/system/ut/ya.make | 2 |
10 files changed, 155 insertions, 155 deletions
diff --git a/util/system/daemon.cpp b/util/system/daemon.cpp index 5020720206..130e6c8f45 100644 --- a/util/system/daemon.cpp +++ b/util/system/daemon.cpp @@ -17,18 +17,18 @@ #ifdef _unix_ using namespace NDaemonMaker; -static bool Fork(EParent parent) { +static bool Fork(EParent parent) { pid_t pid = fork(); if (pid > 0) { - int status = 0; - while (waitpid(pid, &status, 0) < 0 && errno == EINTR) { - } - if (parent == callExitFromParent) { + int status = 0; + while (waitpid(pid, &status, 0) < 0 && errno == EINTR) { + } + if (parent == callExitFromParent) { _exit(0); - } else { - return true; - } + } else { + return true; + } } else if (pid < 0) { ythrow TSystemError() << "Cannot fork"; } @@ -44,7 +44,7 @@ static bool Fork(EParent parent) { } else if (pid < 0) { ythrow TSystemError() << "Cannot second fork"; } - return false; + return false; } #endif @@ -75,15 +75,15 @@ static void CloseFromToExcept(int from, int to, const int* except) { #endif /* _unix_ */ } -bool NDaemonMaker::MakeMeDaemon(ECloseDescriptors cd, EStdIoDescriptors iod, EChDir chd, EParent parent) { +bool NDaemonMaker::MakeMeDaemon(ECloseDescriptors cd, EStdIoDescriptors iod, EChDir chd, EParent parent) { (void)cd; (void)iod; (void)chd; #ifdef _unix_ - if (Fork(parent)) { - return true; - } + if (Fork(parent)) { + return true; + } if (chd == chdirRoot) { if (chdir("/")) { @@ -156,9 +156,9 @@ bool NDaemonMaker::MakeMeDaemon(ECloseDescriptors cd, EStdIoDescriptors iod, ECh default: break; } - return false; -#else - return true; + return false; +#else + return true; #endif } diff --git a/util/system/daemon.h b/util/system/daemon.h index c9cc73fc44..b00793b9c9 100644 --- a/util/system/daemon.h +++ b/util/system/daemon.h @@ -17,11 +17,11 @@ namespace NDaemonMaker { chdirRoot }; - enum EParent { - callExitFromParent = 0, - returnFromParent - }; - - bool MakeMeDaemon(ECloseDescriptors cd = closeAll, EStdIoDescriptors iod = openDevNull, EChDir chd = chdirRoot, EParent parent = callExitFromParent); + enum EParent { + callExitFromParent = 0, + returnFromParent + }; + + bool MakeMeDaemon(ECloseDescriptors cd = closeAll, EStdIoDescriptors iod = openDevNull, EChDir chd = chdirRoot, EParent parent = callExitFromParent); void CloseFrom(int fd); } diff --git a/util/system/daemon_ut.cpp b/util/system/daemon_ut.cpp index fd80f3ba8c..f93b9a9645 100644 --- a/util/system/daemon_ut.cpp +++ b/util/system/daemon_ut.cpp @@ -1,94 +1,94 @@ -#include "daemon.h" - +#include "daemon.h" + #include <library/cpp/testing/unittest/registar.h> - -#include <util/network/pair.h> -#include <util/network/socket.h> -#include <util/system/pipe.h> - -Y_UNIT_TEST_SUITE(TDaemonTest) { -#ifdef _unix_ - template <typename Func> + +#include <util/network/pair.h> +#include <util/network/socket.h> +#include <util/system/pipe.h> + +Y_UNIT_TEST_SUITE(TDaemonTest) { +#ifdef _unix_ + template <typename Func> static bool ProcessBuffer(Func&& func, void* bufin, size_t size) { - char* buf = (char*)bufin; - do { - const ssize_t bytesDone = func(buf, size); - if (bytesDone == 0) { - return false; - } - - if (bytesDone < 0) { - if (errno == EAGAIN || errno == EINTR) { - continue; - } else { - return false; - } - } - - buf += bytesDone; - size -= bytesDone; - } while (size != 0); - - return true; - } - - const int size = 1024 * 4; - const int pagesSize = sizeof(int) * size; - - Y_UNIT_TEST(WaitForMessageSocket) { - using namespace NDaemonMaker; - SOCKET sockets[2]; - SocketPair(sockets, false, true); - TSocket sender(sockets[0]); - TSocket receiver(sockets[1]); - - int status = -1; - int* pages = new int[size]; - - memset(pages, 0, pagesSize); - if (MakeMeDaemon(closeStdIoOnly, openDevNull, chdirNone, returnFromParent)) { - sender.Close(); - UNIT_ASSERT(ProcessBuffer([&receiver](char* ptr, size_t sz) -> size_t { return receiver.Recv(ptr, sz); }, &status, sizeof(status))); - UNIT_ASSERT(ProcessBuffer([&receiver](char* ptr, size_t sz) -> size_t { return receiver.Recv(ptr, sz); }, pages, pagesSize)); - UNIT_ASSERT(memchr(pages, 0, pagesSize) == nullptr); - } else { - receiver.Close(); - status = 0; - UNIT_ASSERT(ProcessBuffer([&sender](char* ptr, size_t sz) -> size_t { return sender.Send(ptr, sz); }, &status, sizeof(status))); - memset(pages, 1, pagesSize); - UNIT_ASSERT(ProcessBuffer([&sender](char* ptr, size_t sz) -> size_t { return sender.Send(ptr, sz); }, pages, pagesSize)); - exit(0); - } - UNIT_ASSERT(status == 0); - - delete[] pages; - } - - Y_UNIT_TEST(WaitForMessagePipe) { - using namespace NDaemonMaker; - TPipeHandle sender; - TPipeHandle receiver; - TPipeHandle::Pipe(receiver, sender); - - int status = -1; - int* pages = new int[size]; - memset(pages, 0, pagesSize); - if (MakeMeDaemon(closeStdIoOnly, openDevNull, chdirNone, returnFromParent)) { - sender.Close(); - UNIT_ASSERT(ProcessBuffer([&receiver](char* ptr, size_t sz) -> size_t { return receiver.Read(ptr, sz); }, &status, sizeof(status))); - UNIT_ASSERT(ProcessBuffer([&receiver](char* ptr, size_t sz) -> size_t { return receiver.Read(ptr, sz); }, pages, pagesSize)); - UNIT_ASSERT(memchr(pages, 0, pagesSize) == nullptr); - } else { - receiver.Close(); - status = 0; - UNIT_ASSERT(ProcessBuffer([&sender](char* ptr, size_t sz) -> size_t { return sender.Write(ptr, sz); }, &status, sizeof(status))); - memset(pages, 1, pagesSize); - UNIT_ASSERT(ProcessBuffer([&sender](char* ptr, size_t sz) -> size_t { return sender.Write(ptr, sz); }, pages, pagesSize)); - exit(0); - } - UNIT_ASSERT(status == 0); - - delete[] pages; - } -#endif -} + char* buf = (char*)bufin; + do { + const ssize_t bytesDone = func(buf, size); + if (bytesDone == 0) { + return false; + } + + if (bytesDone < 0) { + if (errno == EAGAIN || errno == EINTR) { + continue; + } else { + return false; + } + } + + buf += bytesDone; + size -= bytesDone; + } while (size != 0); + + return true; + } + + const int size = 1024 * 4; + const int pagesSize = sizeof(int) * size; + + Y_UNIT_TEST(WaitForMessageSocket) { + using namespace NDaemonMaker; + SOCKET sockets[2]; + SocketPair(sockets, false, true); + TSocket sender(sockets[0]); + TSocket receiver(sockets[1]); + + int status = -1; + int* pages = new int[size]; + + memset(pages, 0, pagesSize); + if (MakeMeDaemon(closeStdIoOnly, openDevNull, chdirNone, returnFromParent)) { + sender.Close(); + UNIT_ASSERT(ProcessBuffer([&receiver](char* ptr, size_t sz) -> size_t { return receiver.Recv(ptr, sz); }, &status, sizeof(status))); + UNIT_ASSERT(ProcessBuffer([&receiver](char* ptr, size_t sz) -> size_t { return receiver.Recv(ptr, sz); }, pages, pagesSize)); + UNIT_ASSERT(memchr(pages, 0, pagesSize) == nullptr); + } else { + receiver.Close(); + status = 0; + UNIT_ASSERT(ProcessBuffer([&sender](char* ptr, size_t sz) -> size_t { return sender.Send(ptr, sz); }, &status, sizeof(status))); + memset(pages, 1, pagesSize); + UNIT_ASSERT(ProcessBuffer([&sender](char* ptr, size_t sz) -> size_t { return sender.Send(ptr, sz); }, pages, pagesSize)); + exit(0); + } + UNIT_ASSERT(status == 0); + + delete[] pages; + } + + Y_UNIT_TEST(WaitForMessagePipe) { + using namespace NDaemonMaker; + TPipeHandle sender; + TPipeHandle receiver; + TPipeHandle::Pipe(receiver, sender); + + int status = -1; + int* pages = new int[size]; + memset(pages, 0, pagesSize); + if (MakeMeDaemon(closeStdIoOnly, openDevNull, chdirNone, returnFromParent)) { + sender.Close(); + UNIT_ASSERT(ProcessBuffer([&receiver](char* ptr, size_t sz) -> size_t { return receiver.Read(ptr, sz); }, &status, sizeof(status))); + UNIT_ASSERT(ProcessBuffer([&receiver](char* ptr, size_t sz) -> size_t { return receiver.Read(ptr, sz); }, pages, pagesSize)); + UNIT_ASSERT(memchr(pages, 0, pagesSize) == nullptr); + } else { + receiver.Close(); + status = 0; + UNIT_ASSERT(ProcessBuffer([&sender](char* ptr, size_t sz) -> size_t { return sender.Write(ptr, sz); }, &status, sizeof(status))); + memset(pages, 1, pagesSize); + UNIT_ASSERT(ProcessBuffer([&sender](char* ptr, size_t sz) -> size_t { return sender.Write(ptr, sz); }, pages, pagesSize)); + exit(0); + } + UNIT_ASSERT(status == 0); + + delete[] pages; + } +#endif +} diff --git a/util/system/file.cpp b/util/system/file.cpp index f677819de6..4a261d020c 100644 --- a/util/system/file.cpp +++ b/util/system/file.cpp @@ -238,9 +238,9 @@ TFileHandle::TFileHandle(const TString& fName, EOpenMode oMode) noexcept { permMode |= S_IRUSR; } - do { - Fd_ = ::open(fName.data(), fcMode, permMode); - } while (Fd_ == -1 && errno == EINTR); + do { + Fd_ = ::open(fName.data(), fcMode, permMode); + } while (Fd_ == -1 && errno == EINTR); #if HAVE_POSIX_FADVISE if (Fd_ >= 0) { diff --git a/util/system/file_lock.cpp b/util/system/file_lock.cpp index 4af6110011..45d91282c5 100644 --- a/util/system/file_lock.cpp +++ b/util/system/file_lock.cpp @@ -15,7 +15,7 @@ namespace { default: Y_UNREACHABLE(); } - Y_UNREACHABLE(); + Y_UNREACHABLE(); } } diff --git a/util/system/flock.cpp b/util/system/flock.cpp index 480ae18369..fe88fecaff 100644 --- a/util/system/flock.cpp +++ b/util/system/flock.cpp @@ -55,7 +55,7 @@ extern "C" { break; default: break; - } + } errno = EINVAL; return -1; } diff --git a/util/system/pipe.cpp b/util/system/pipe.cpp index d16d6830b7..a543bd7472 100644 --- a/util/system/pipe.cpp +++ b/util/system/pipe.cpp @@ -32,12 +32,12 @@ bool TPipeHandle::Close() noexcept { return ok; } -void TPipeHandle::Pipe(TPipeHandle& reader, TPipeHandle& writer, EOpenMode mode) { +void TPipeHandle::Pipe(TPipeHandle& reader, TPipeHandle& writer, EOpenMode mode) { PIPEHANDLE fds[2]; #ifdef _win_ - int r = SocketPair(fds, false /* non-overlapped */, mode & CloseOnExec /* cloexec */); -#elif defined(_linux_) - int r = pipe2(fds, mode & CloseOnExec ? O_CLOEXEC : 0); + int r = SocketPair(fds, false /* non-overlapped */, mode & CloseOnExec /* cloexec */); +#elif defined(_linux_) + int r = pipe2(fds, mode & CloseOnExec ? O_CLOEXEC : 0); #else int r = pipe(fds); #endif @@ -45,22 +45,22 @@ void TPipeHandle::Pipe(TPipeHandle& reader, TPipeHandle& writer, EOpenMode mode) ythrow TFileError() << "failed to create a pipe"; } -#if !defined(_win_) && !defined(_linux_) - // Non-atomic wrt exec - if (mode & CloseOnExec) { - for (int i = 0; i < 2; ++i) { - int flags = fcntl(fds[i], F_GETFD, 0); - if (flags < 0) { - ythrow TFileError() << "failed to get flags"; - } - int r = fcntl(fds[i], F_SETFD, flags | FD_CLOEXEC); - if (r < 0) { - ythrow TFileError() << "failed to set flags"; - } - } - } -#endif - +#if !defined(_win_) && !defined(_linux_) + // Non-atomic wrt exec + if (mode & CloseOnExec) { + for (int i = 0; i < 2; ++i) { + int flags = fcntl(fds[i], F_GETFD, 0); + if (flags < 0) { + ythrow TFileError() << "failed to get flags"; + } + int r = fcntl(fds[i], F_SETFD, flags | FD_CLOEXEC); + if (r < 0) { + ythrow TFileError() << "failed to set flags"; + } + } + } +#endif + TPipeHandle(fds[0]).Swap(reader); TPipeHandle(fds[1]).Swap(writer); } @@ -150,11 +150,11 @@ size_t TPipe::Write(const void* buf, size_t len) const { return Impl_->Write(buf, len); } -void TPipe::Pipe(TPipe& reader, TPipe& writer, EOpenMode mode) { +void TPipe::Pipe(TPipe& reader, TPipe& writer, EOpenMode mode) { TImplRef r(new TImpl()); TImplRef w(new TImpl()); - TPipeHandle::Pipe(r->GetHandle(), w->GetHandle(), mode); + TPipeHandle::Pipe(r->GetHandle(), w->GetHandle(), mode); r.Swap(reader.Impl_); w.Swap(writer.Impl_); diff --git a/util/system/pipe.h b/util/system/pipe.h index 60e6440be3..75d0360049 100644 --- a/util/system/pipe.h +++ b/util/system/pipe.h @@ -6,7 +6,7 @@ #endif #include "defaults.h" -#include "file.h" +#include "file.h" #include <util/generic/ptr.h> #include <util/network/pair.h> #include <util/generic/noncopyable.h> @@ -54,8 +54,8 @@ public: ssize_t Read(void* buffer, size_t byteCount) const noexcept; ssize_t Write(const void* buffer, size_t byteCount) const noexcept; - // Only CloseOnExec is supported - static void Pipe(TPipeHandle& reader, TPipeHandle& writer, EOpenMode mode = 0); + // Only CloseOnExec is supported + static void Pipe(TPipeHandle& reader, TPipeHandle& writer, EOpenMode mode = 0); private: PIPEHANDLE Fd_; @@ -76,8 +76,8 @@ public: size_t Read(void* buf, size_t len) const; size_t Write(const void* buf, size_t len) const; - // Only CloseOnExec is supported - static void Pipe(TPipe& reader, TPipe& writer, EOpenMode mode = 0); + // Only CloseOnExec is supported + static void Pipe(TPipe& reader, TPipe& writer, EOpenMode mode = 0); private: class TImpl; diff --git a/util/system/shellcommand.cpp b/util/system/shellcommand.cpp index 099840f684..b1989b5c8c 100644 --- a/util/system/shellcommand.cpp +++ b/util/system/shellcommand.cpp @@ -168,10 +168,10 @@ public: return doneBytes; } - static void Pipe(TRealPipeHandle& reader, TRealPipeHandle& writer, EOpenMode mode) { - (void)mode; + static void Pipe(TRealPipeHandle& reader, TRealPipeHandle& writer, EOpenMode mode) { + (void)mode; REALPIPEHANDLE fds[2]; - if (!CreatePipe(&fds[0], &fds[1], nullptr /* handles are not inherited */, 0)) + if (!CreatePipe(&fds[0], &fds[1], nullptr /* handles are not inherited */, 0)) ythrow TFileError() << "failed to create a pipe"; TRealPipeHandle(fds[0]).Swap(reader); TRealPipeHandle(fds[1]).Swap(writer); @@ -254,9 +254,9 @@ private: if (ErrorPipeFd[1].IsOpen()) { ErrorPipeFd[1].Close(); } - if (InputPipeFd[1].IsOpen()) { + if (InputPipeFd[1].IsOpen()) { InputPipeFd[0].Close(); - } + } } void ReleaseParents() { InputPipeFd[1].Release(); @@ -763,13 +763,13 @@ void TShellCommand::TImpl::Run() { TPipes pipes; if (OutputMode != TShellCommandOptions::HANDLE_INHERIT) { - TRealPipeHandle::Pipe(pipes.OutputPipeFd[0], pipes.OutputPipeFd[1], CloseOnExec); + TRealPipeHandle::Pipe(pipes.OutputPipeFd[0], pipes.OutputPipeFd[1], CloseOnExec); } if (ErrorMode != TShellCommandOptions::HANDLE_INHERIT) { - TRealPipeHandle::Pipe(pipes.ErrorPipeFd[0], pipes.ErrorPipeFd[1], CloseOnExec); + TRealPipeHandle::Pipe(pipes.ErrorPipeFd[0], pipes.ErrorPipeFd[1], CloseOnExec); } if (InputMode != TShellCommandOptions::HANDLE_INHERIT) { - TRealPipeHandle::Pipe(pipes.InputPipeFd[0], pipes.InputPipeFd[1], CloseOnExec); + TRealPipeHandle::Pipe(pipes.InputPipeFd[0], pipes.InputPipeFd[1], CloseOnExec); } AtomicSet(ExecutionStatus, SHELL_RUNNING); diff --git a/util/system/ut/ya.make b/util/system/ut/ya.make index a15826cb0d..127e7c261e 100644 --- a/util/system/ut/ya.make +++ b/util/system/ut/ya.make @@ -35,7 +35,7 @@ SRCS( system/condvar_ut.cpp system/cpu_id_ut.cpp system/datetime_ut.cpp - system/daemon_ut.cpp + system/daemon_ut.cpp system/direct_io_ut.cpp system/env_ut.cpp system/error_ut.cpp |