diff options
author | deshevoy <deshevoy@yandex-team.ru> | 2022-02-10 16:46:56 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:56 +0300 |
commit | e988f30484abe5fdeedcc7a5d3c226c01a21800c (patch) | |
tree | 0a217b173aabb57b7e51f8a169989b1a3e0309fe /util/system | |
parent | 33ee501c05d3f24036ae89766a858930ae66c548 (diff) | |
download | ydb-e988f30484abe5fdeedcc7a5d3c226c01a21800c.tar.gz |
Restoring authorship annotation for <deshevoy@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'util/system')
-rw-r--r-- | util/system/daemon.cpp | 4 | ||||
-rw-r--r-- | util/system/file.cpp | 4 | ||||
-rw-r--r-- | util/system/file_lock.h | 24 | ||||
-rw-r--r-- | util/system/mutex.h | 48 | ||||
-rw-r--r-- | util/system/shellcommand.cpp | 146 | ||||
-rw-r--r-- | util/system/shellcommand.h | 124 | ||||
-rw-r--r-- | util/system/shellcommand_ut.cpp | 38 | ||||
-rw-r--r-- | util/system/spinlock.h | 40 | ||||
-rw-r--r-- | util/system/spinlock_ut.cpp | 14 | ||||
-rw-r--r-- | util/system/user.cpp | 10 |
10 files changed, 226 insertions, 226 deletions
diff --git a/util/system/daemon.cpp b/util/system/daemon.cpp index 130e6c8f45..8856c073d4 100644 --- a/util/system/daemon.cpp +++ b/util/system/daemon.cpp @@ -25,7 +25,7 @@ static bool Fork(EParent parent) { while (waitpid(pid, &status, 0) < 0 && errno == EINTR) { } if (parent == callExitFromParent) { - _exit(0); + _exit(0); } else { return true; } @@ -40,7 +40,7 @@ static bool Fork(EParent parent) { pid = fork(); if (pid > 0) { - _exit(0); + _exit(0); } else if (pid < 0) { ythrow TSystemError() << "Cannot second fork"; } diff --git a/util/system/file.cpp b/util/system/file.cpp index 4a261d020c..7a7a8a4fa0 100644 --- a/util/system/file.cpp +++ b/util/system/file.cpp @@ -432,12 +432,12 @@ bool TFileHandle::Flush() noexcept { * (PIPE, FIFO, or socket) which does not support synchronization. * Fail in case of EIO, ENOSPC, EDQUOT - data might be lost. */ - return ret == 0 || errno == EROFS || errno == EINVAL + return ret == 0 || errno == EROFS || errno == EINVAL #if defined(_darwin_) // ENOTSUP fd does not refer to a vnode || errno == ENOTSUP #endif - ; + ; #else #error unsupported platform #endif diff --git a/util/system/file_lock.h b/util/system/file_lock.h index b2aaff5baf..294696e5fe 100644 --- a/util/system/file_lock.h +++ b/util/system/file_lock.h @@ -17,18 +17,18 @@ public: bool TryAcquire(); void Release(); - inline void lock() { - Acquire(); - } - - inline bool try_lock() { - return TryAcquire(); - } - - inline void unlock() { - Release(); - } - + inline void lock() { + Acquire(); + } + + inline bool try_lock() { + return TryAcquire(); + } + + inline void unlock() { + Release(); + } + private: EFileLockType Type; }; diff --git a/util/system/mutex.h b/util/system/mutex.h index 032630d134..4b873998ca 100644 --- a/util/system/mutex.h +++ b/util/system/mutex.h @@ -18,18 +18,18 @@ public: inline void Release() noexcept { } - inline void lock() noexcept { - Acquire(); - } - - inline bool try_lock() noexcept { - return TryAcquire(); - } - - inline void unlock() noexcept { - Release(); - } - + inline void lock() noexcept { + Acquire(); + } + + inline bool try_lock() noexcept { + return TryAcquire(); + } + + inline void unlock() noexcept { + Release(); + } + ~TFakeMutex() = default; }; @@ -43,18 +43,18 @@ public: bool TryAcquire() noexcept; void Release() noexcept; - inline void lock() noexcept { - Acquire(); - } - - inline bool try_lock() noexcept { - return TryAcquire(); - } - - inline void unlock() noexcept { - Release(); - } - + inline void lock() noexcept { + Acquire(); + } + + inline bool try_lock() noexcept { + return TryAcquire(); + } + + inline void unlock() noexcept { + Release(); + } + //return opaque pointer to real handler void* Handle() const noexcept; diff --git a/util/system/shellcommand.cpp b/util/system/shellcommand.cpp index b1989b5c8c..3a04e3f52e 100644 --- a/util/system/shellcommand.cpp +++ b/util/system/shellcommand.cpp @@ -204,7 +204,7 @@ private: TString InternalError; TThread* WatchThread; TMutex TerminateMutex; - TFileHandle InputHandle; + TFileHandle InputHandle; TFileHandle OutputHandle; TFileHandle ErrorHandle; @@ -248,12 +248,12 @@ private: TRealPipeHandle InputPipeFd[2]; // pipes are closed by automatic dtor void PrepareParents() { - if (OutputPipeFd[1].IsOpen()) { - OutputPipeFd[1].Close(); - } - if (ErrorPipeFd[1].IsOpen()) { - ErrorPipeFd[1].Close(); - } + if (OutputPipeFd[1].IsOpen()) { + OutputPipeFd[1].Close(); + } + if (ErrorPipeFd[1].IsOpen()) { + ErrorPipeFd[1].Close(); + } if (InputPipeFd[1].IsOpen()) { InputPipeFd[0].Close(); } @@ -300,7 +300,7 @@ public: , DetachSession(options.DetachSession) , CloseStreams(options.CloseStreams) , ShouldCloseInput(options.ShouldCloseInput) - , InputMode(options.InputMode) + , InputMode(options.InputMode) , OutputMode(options.OutputMode) , ErrorMode(options.ErrorMode) , User(options.User) @@ -308,10 +308,10 @@ public: , Nice(options.Nice) , FuncAfterFork(options.FuncAfterFork) { - if (InputStream) { - // TODO change usages to call SetInputStream instead of directly assigning to InputStream - InputMode = TShellCommandOptions::HANDLE_STREAM; - } + if (InputStream) { + // TODO change usages to call SetInputStream instead of directly assigning to InputStream + InputMode = TShellCommandOptions::HANDLE_STREAM; + } } inline ~TImpl() { @@ -374,10 +374,10 @@ public: #endif } - inline TFileHandle& GetInputHandle() { - return InputHandle; - } - + inline TFileHandle& GetInputHandle() { + return InputHandle; + } + inline TFileHandle& GetOutputHandle() { return OutputHandle; } @@ -521,31 +521,31 @@ void TShellCommand::TImpl::StartProcess(TShellCommand::TImpl::TPipes& pipes) { startup_info.dwFlags = STARTF_USESTDHANDLES; if (OutputMode != TShellCommandOptions::HANDLE_INHERIT) { - if (!SetHandleInformation(pipes.OutputPipeFd[1], HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT)) { - ythrow TSystemError() << "cannot set handle info"; - } - } + if (!SetHandleInformation(pipes.OutputPipeFd[1], HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT)) { + ythrow TSystemError() << "cannot set handle info"; + } + } if (ErrorMode != TShellCommandOptions::HANDLE_INHERIT) { - if (!SetHandleInformation(pipes.ErrorPipeFd[1], HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT)) { - ythrow TSystemError() << "cannot set handle info"; - } - } - if (InputMode != TShellCommandOptions::HANDLE_INHERIT) { + if (!SetHandleInformation(pipes.ErrorPipeFd[1], HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT)) { + ythrow TSystemError() << "cannot set handle info"; + } + } + if (InputMode != TShellCommandOptions::HANDLE_INHERIT) { if (!SetHandleInformation(pipes.InputPipeFd[0], HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT)) ythrow TSystemError() << "cannot set handle info"; - } + } // A sockets do not work as std streams for some reason if (OutputMode != TShellCommandOptions::HANDLE_INHERIT) { - startup_info.hStdOutput = pipes.OutputPipeFd[1]; - } else { - startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); - } + startup_info.hStdOutput = pipes.OutputPipeFd[1]; + } else { + startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); + } if (ErrorMode != TShellCommandOptions::HANDLE_INHERIT) { - startup_info.hStdError = pipes.ErrorPipeFd[1]; - } else { - startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); - } + startup_info.hStdError = pipes.ErrorPipeFd[1]; + } else { + startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); + } if (InputMode != TShellCommandOptions::HANDLE_INHERIT) { startup_info.hStdInput = pipes.InputPipeFd[0]; } else { @@ -691,7 +691,7 @@ void TShellCommand::TImpl::OnFork(TPipes& pipes, sigset_t oldmask, char* const* TFileHandle sIn(0); TFileHandle sOut(1); TFileHandle sErr(2); - if (InputMode != TShellCommandOptions::HANDLE_INHERIT) { + if (InputMode != TShellCommandOptions::HANDLE_INHERIT) { pipes.InputPipeFd[1].Close(); TFileHandle sInNew(pipes.InputPipeFd[0]); sIn.LinkTo(sInNew); @@ -702,19 +702,19 @@ void TShellCommand::TImpl::OnFork(TPipes& pipes, sigset_t oldmask, char* const* /// @todo in case of real need - reopen /dev/null } if (OutputMode != TShellCommandOptions::HANDLE_INHERIT) { - pipes.OutputPipeFd[0].Close(); - TFileHandle sOutNew(pipes.OutputPipeFd[1]); - sOut.LinkTo(sOutNew); - sOut.Release(); - sOutNew.Release(); - } + pipes.OutputPipeFd[0].Close(); + TFileHandle sOutNew(pipes.OutputPipeFd[1]); + sOut.LinkTo(sOutNew); + sOut.Release(); + sOutNew.Release(); + } if (ErrorMode != TShellCommandOptions::HANDLE_INHERIT) { - pipes.ErrorPipeFd[0].Close(); - TFileHandle sErrNew(pipes.ErrorPipeFd[1]); - sErr.LinkTo(sErrNew); - sErr.Release(); - sErrNew.Release(); - } + pipes.ErrorPipeFd[0].Close(); + TFileHandle sErrNew(pipes.ErrorPipeFd[1]); + sErr.LinkTo(sErrNew); + sErr.Release(); + sErrNew.Release(); + } if (WorkDir.size()) { NFs::SetCurrentWorkingDirectory(WorkDir); @@ -764,11 +764,11 @@ void TShellCommand::TImpl::Run() { if (OutputMode != TShellCommandOptions::HANDLE_INHERIT) { TRealPipeHandle::Pipe(pipes.OutputPipeFd[0], pipes.OutputPipeFd[1], CloseOnExec); - } + } if (ErrorMode != TShellCommandOptions::HANDLE_INHERIT) { TRealPipeHandle::Pipe(pipes.ErrorPipeFd[0], pipes.ErrorPipeFd[1], CloseOnExec); - } - if (InputMode != TShellCommandOptions::HANDLE_INHERIT) { + } + if (InputMode != TShellCommandOptions::HANDLE_INHERIT) { TRealPipeHandle::Pipe(pipes.InputPipeFd[0], pipes.InputPipeFd[1], CloseOnExec); } @@ -844,11 +844,11 @@ void TShellCommand::TImpl::Run() { return; } - if (InputMode == TShellCommandOptions::HANDLE_PIPE) { - TFileHandle inputHandle(pipes.InputPipeFd[1].Release()); - InputHandle.Swap(inputHandle); - } - + if (InputMode == TShellCommandOptions::HANDLE_PIPE) { + TFileHandle inputHandle(pipes.InputPipeFd[1].Release()); + InputHandle.Swap(inputHandle); + } + if (OutputMode == TShellCommandOptions::HANDLE_PIPE) { TFileHandle outputHandle(pipes.OutputPipeFd[0].Release()); OutputHandle.Swap(outputHandle); @@ -868,7 +868,7 @@ void TShellCommand::TImpl::Run() { } else { Communicate(processInfo); } - + pipes.ReleaseParents(); // not needed } @@ -887,19 +887,19 @@ void TShellCommand::TImpl::Communicate(TProcessInfo* pi) { IInputStream*& input = pi->Parent->InputStream; -#if defined(_unix_) - // not really needed, io is done via poll - if (pi->OutputFd.IsOpen()) { - SetNonBlock(pi->OutputFd); - } - if (pi->ErrorFd.IsOpen()) { - SetNonBlock(pi->ErrorFd); - } - if (pi->InputFd.IsOpen()) { - SetNonBlock(pi->InputFd); - } -#endif - +#if defined(_unix_) + // not really needed, io is done via poll + if (pi->OutputFd.IsOpen()) { + SetNonBlock(pi->OutputFd); + } + if (pi->ErrorFd.IsOpen()) { + SetNonBlock(pi->ErrorFd); + } + if (pi->InputFd.IsOpen()) { + SetNonBlock(pi->InputFd); + } +#endif + try { #if defined(_win_) TPipePump pumps[3] = {0}; @@ -1163,10 +1163,10 @@ TProcessId TShellCommand::GetPid() const { return Impl->GetPid(); } -TFileHandle& TShellCommand::GetInputHandle() { - return Impl->GetInputHandle(); -} - +TFileHandle& TShellCommand::GetInputHandle() { + return Impl->GetInputHandle(); +} + TFileHandle& TShellCommand::GetOutputHandle() { return Impl->GetOutputHandle(); } diff --git a/util/system/shellcommand.h b/util/system/shellcommand.h index 8730627fe5..181b5bd913 100644 --- a/util/system/shellcommand.h +++ b/util/system/shellcommand.h @@ -8,7 +8,7 @@ #include <util/generic/maybe.h> #include <util/stream/input.h> #include <util/stream/output.h> -#include "file.h" +#include "file.h" #include "getpid.h" #include "thread.h" #include "mutex.h" @@ -31,12 +31,12 @@ public: #endif }; - enum EHandleMode { - HANDLE_INHERIT, - HANDLE_PIPE, - HANDLE_STREAM - }; - + enum EHandleMode { + HANDLE_INHERIT, + HANDLE_PIPE, + HANDLE_STREAM + }; + public: inline TShellCommandOptions() noexcept : ClearSignalMask(false) @@ -48,7 +48,7 @@ public: , DetachSession(true) , CloseStreams(false) , ShouldCloseInput(true) - , InputMode(HANDLE_INHERIT) + , InputMode(HANDLE_INHERIT) , OutputMode(HANDLE_STREAM) , ErrorMode(HANDLE_STREAM) , InputStream(nullptr) @@ -131,11 +131,11 @@ public: */ inline TShellCommandOptions& SetInputStream(IInputStream* stream) { InputStream = stream; - if (InputStream == nullptr) { - InputMode = HANDLE_INHERIT; - } else { - InputMode = HANDLE_STREAM; - } + if (InputStream == nullptr) { + InputMode = HANDLE_INHERIT; + } else { + InputMode = HANDLE_STREAM; + } return *this; } @@ -236,7 +236,7 @@ public: return *this; } - /** + /** * @brief specifies pure function to be called in the child process after fork, before calling execve * @note currently ignored on windows * @param function function to be called after fork @@ -248,17 +248,17 @@ public: } /** - * @brief create a pipe for child input - * Write end of the pipe will be accessible via TShellCommand::GetInputHandle - * - * @return self - */ - inline TShellCommandOptions& PipeInput() { - InputMode = HANDLE_PIPE; - InputStream = nullptr; - return *this; - } - + * @brief create a pipe for child input + * Write end of the pipe will be accessible via TShellCommand::GetInputHandle + * + * @return self + */ + inline TShellCommandOptions& PipeInput() { + InputMode = HANDLE_PIPE; + InputStream = nullptr; + return *this; + } + inline TShellCommandOptions& PipeOutput() { OutputMode = HANDLE_PIPE; OutputStream = nullptr; @@ -271,30 +271,30 @@ public: return *this; } - /** - * @brief set if child should inherit output handle - * - * @param inherit if child should inherit output handle - * - * @return self - */ - inline TShellCommandOptions& SetInheritOutput(bool inherit) { + /** + * @brief set if child should inherit output handle + * + * @param inherit if child should inherit output handle + * + * @return self + */ + inline TShellCommandOptions& SetInheritOutput(bool inherit) { OutputMode = inherit ? HANDLE_INHERIT : HANDLE_STREAM; - return *this; - } - - /** - * @brief set if child should inherit stderr handle - * - * @param inherit if child should inherit error output handle - * - * @return self - */ - inline TShellCommandOptions& SetInheritError(bool inherit) { + return *this; + } + + /** + * @brief set if child should inherit stderr handle + * + * @param inherit if child should inherit error output handle + * + * @return self + */ + inline TShellCommandOptions& SetInheritError(bool inherit) { ErrorMode = inherit ? HANDLE_INHERIT : HANDLE_STREAM; - return *this; - } - + return *this; + } + public: bool ClearSignalMask = false; bool CloseAllFdsOnExec = false; @@ -417,25 +417,25 @@ public: /** * @brief return the file handle that provides input to the child process * - * @return input file handle - */ - TFileHandle& GetInputHandle(); - - /** + * @return input file handle + */ + TFileHandle& GetInputHandle(); + + /** * @brief return the file handle that provides output from the child process * - * @return output file handle - */ - TFileHandle& GetOutputHandle(); - - /** + * @return output file handle + */ + TFileHandle& GetOutputHandle(); + + /** * @brief return the file handle that provides error output from the child process * - * @return error file handle - */ - TFileHandle& GetErrorHandle(); - - /** + * @return error file handle + */ + TFileHandle& GetErrorHandle(); + + /** * @brief run the execution * * @return self diff --git a/util/system/shellcommand_ut.cpp b/util/system/shellcommand_ut.cpp index 9d849279d2..ca0fa88ef5 100644 --- a/util/system/shellcommand_ut.cpp +++ b/util/system/shellcommand_ut.cpp @@ -11,7 +11,7 @@ #include <util/folder/dirut.h> #include <util/random/random.h> -#include <util/stream/file.h> +#include <util/stream/file.h> #include <util/stream/str.h> #include <util/stream/mem.h> #include <util/string/strip.h> @@ -418,24 +418,24 @@ Y_UNIT_TEST_SUITE(TShellCommandTest) { UNIT_ASSERT_VALUES_EQUAL(stream.Str(), input + NL); } - Y_UNIT_TEST(TestPipeInput) { - TShellCommandOptions options; - options.SetAsync(true); - options.PipeInput(); - - TShellCommand cmd(catCommand, options); - cmd.Run(); - - { - TFile file(cmd.GetInputHandle().Release()); - TUnbufferedFileOutput fo(file); - fo << "hello" << Endl; - } - - cmd.Wait(); - UNIT_ASSERT_VALUES_EQUAL(cmd.GetOutput(), "hello" NL); - UNIT_ASSERT_VALUES_EQUAL(cmd.GetError().size(), 0u); - } + Y_UNIT_TEST(TestPipeInput) { + TShellCommandOptions options; + options.SetAsync(true); + options.PipeInput(); + + TShellCommand cmd(catCommand, options); + cmd.Run(); + + { + TFile file(cmd.GetInputHandle().Release()); + TUnbufferedFileOutput fo(file); + fo << "hello" << Endl; + } + + cmd.Wait(); + UNIT_ASSERT_VALUES_EQUAL(cmd.GetOutput(), "hello" NL); + UNIT_ASSERT_VALUES_EQUAL(cmd.GetError().size(), 0u); + } Y_UNIT_TEST(TestPipeOutput) { TShellCommandOptions options; options.SetAsync(true); diff --git a/util/system/spinlock.h b/util/system/spinlock.h index af2630890a..843bc4e868 100644 --- a/util/system/spinlock.h +++ b/util/system/spinlock.h @@ -18,10 +18,10 @@ public: return AtomicTryLock(&Val_); } - inline bool try_lock() noexcept { - return TryAcquire(); - } - + inline bool try_lock() noexcept { + return TryAcquire(); + } + protected: TAtomic Val_; }; @@ -63,14 +63,14 @@ public: inline void Acquire() noexcept { AcquireSpinLock(&Val_); } - - inline void unlock() noexcept { - Release(); - } - - inline void lock() noexcept { - Acquire(); - } + + inline void unlock() noexcept { + Release(); + } + + inline void lock() noexcept { + Acquire(); + } }; static inline void AcquireAdaptiveLock(TAtomic* l) { @@ -98,14 +98,14 @@ public: inline void Acquire() noexcept { AcquireAdaptiveLock(&Val_); } - - inline void unlock() noexcept { - Release(); - } - - inline void lock() noexcept { - Acquire(); - } + + inline void unlock() noexcept { + Release(); + } + + inline void lock() noexcept { + Acquire(); + } }; #include "guard.h" diff --git a/util/system/spinlock_ut.cpp b/util/system/spinlock_ut.cpp index e8639a6404..501c499a71 100644 --- a/util/system/spinlock_ut.cpp +++ b/util/system/spinlock_ut.cpp @@ -18,13 +18,13 @@ Y_UNIT_TEST_SUITE(TSpinLock) { UNIT_ASSERT(lock.IsLocked()); lock.Release(); UNIT_ASSERT(!lock.IsLocked()); - - // Lockable requirements - lock.lock(); - UNIT_ASSERT(lock.IsLocked()); - UNIT_ASSERT(!lock.try_lock()); - lock.unlock(); - UNIT_ASSERT(!lock.IsLocked()); + + // Lockable requirements + lock.lock(); + UNIT_ASSERT(lock.IsLocked()); + UNIT_ASSERT(!lock.try_lock()); + lock.unlock(); + UNIT_ASSERT(!lock.IsLocked()); } Y_UNIT_TEST(TSpinLock_IsLocked) { diff --git a/util/system/user.cpp b/util/system/user.cpp index 83e89ea0a8..9eec362ce3 100644 --- a/util/system/user.cpp +++ b/util/system/user.cpp @@ -46,12 +46,12 @@ TString GetUsername() { passwd pwd; passwd* tmpPwd; int err = getpwuid_r(geteuid(), &pwd, nameBuf.Data(), nameBuf.Size(), &tmpPwd); - if (err == 0 && tmpPwd) { - return TString(pwd.pw_name); - } else if (err == ERANGE) { - nameBuf = TTempBuf(nameBuf.Size() * 2); + if (err == 0 && tmpPwd) { + return TString(pwd.pw_name); + } else if (err == ERANGE) { + nameBuf = TTempBuf(nameBuf.Size() * 2); } else { - ythrow TSystemError(err) << " getpwuid_r failed"; + ythrow TSystemError(err) << " getpwuid_r failed"; } #endif } |