diff options
author | morozyto <morozyto@yandex-team.ru> | 2022-02-10 16:49:48 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:48 +0300 |
commit | 7a7321022af5e93d07031ccb460d313e4f18d04e (patch) | |
tree | ef13bca2817079136b98297da1e27c5f840f248c /util/system | |
parent | 79b3cf0f1d6edfa0268153eddcb48f3467527461 (diff) | |
download | ydb-7a7321022af5e93d07031ccb460d313e4f18d04e.tar.gz |
Restoring authorship annotation for <morozyto@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'util/system')
-rw-r--r-- | util/system/shellcommand.cpp | 112 | ||||
-rw-r--r-- | util/system/shellcommand.h | 60 | ||||
-rw-r--r-- | util/system/shellcommand_ut.cpp | 58 |
3 files changed, 115 insertions, 115 deletions
diff --git a/util/system/shellcommand.cpp b/util/system/shellcommand.cpp index b1989b5c8c..4a097dea11 100644 --- a/util/system/shellcommand.cpp +++ b/util/system/shellcommand.cpp @@ -205,27 +205,27 @@ private: TThread* WatchThread; TMutex TerminateMutex; TFileHandle InputHandle; - TFileHandle OutputHandle; - TFileHandle ErrorHandle; - + TFileHandle OutputHandle; + TFileHandle ErrorHandle; + /// @todo: store const TShellCommandOptions, no need for so many vars - bool TerminateFlag = false; - bool ClearSignalMask = false; - bool CloseAllFdsOnExec = false; - bool AsyncMode = false; - size_t PollDelayMs = 0; - bool UseShell = false; - bool QuoteArguments = false; - bool DetachSession = false; - bool CloseStreams = false; + bool TerminateFlag = false; + bool ClearSignalMask = false; + bool CloseAllFdsOnExec = false; + bool AsyncMode = false; + size_t PollDelayMs = 0; + bool UseShell = false; + bool QuoteArguments = false; + bool DetachSession = false; + bool CloseStreams = false; TAtomic ShouldCloseInput; - TShellCommandOptions::EHandleMode InputMode = TShellCommandOptions::HANDLE_STREAM; - TShellCommandOptions::EHandleMode OutputMode = TShellCommandOptions::HANDLE_STREAM; - TShellCommandOptions::EHandleMode ErrorMode = TShellCommandOptions::HANDLE_STREAM; - + TShellCommandOptions::EHandleMode InputMode = TShellCommandOptions::HANDLE_STREAM; + TShellCommandOptions::EHandleMode OutputMode = TShellCommandOptions::HANDLE_STREAM; + TShellCommandOptions::EHandleMode ErrorMode = TShellCommandOptions::HANDLE_STREAM; + TShellCommandOptions::TUserOptions User; THashMap<TString, TString> Environment; - int Nice = 0; + int Nice = 0; std::function<void()> FuncAfterFork = {}; struct TProcessInfo { @@ -301,8 +301,8 @@ public: , CloseStreams(options.CloseStreams) , ShouldCloseInput(options.ShouldCloseInput) , InputMode(options.InputMode) - , OutputMode(options.OutputMode) - , ErrorMode(options.ErrorMode) + , OutputMode(options.OutputMode) + , ErrorMode(options.ErrorMode) , User(options.User) , Environment(options.Environment) , Nice(options.Nice) @@ -378,14 +378,14 @@ public: return InputHandle; } - inline TFileHandle& GetOutputHandle() { - return OutputHandle; - } - - inline TFileHandle& GetErrorHandle() { - return ErrorHandle; - } - + inline TFileHandle& GetOutputHandle() { + return OutputHandle; + } + + inline TFileHandle& GetErrorHandle() { + return ErrorHandle; + } + // start child process void Run(); @@ -520,12 +520,12 @@ void TShellCommand::TImpl::StartProcess(TShellCommand::TImpl::TPipes& pipes) { startup_info.cb = sizeof(startup_info); startup_info.dwFlags = STARTF_USESTDHANDLES; - if (OutputMode != TShellCommandOptions::HANDLE_INHERIT) { + if (OutputMode != TShellCommandOptions::HANDLE_INHERIT) { if (!SetHandleInformation(pipes.OutputPipeFd[1], HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT)) { ythrow TSystemError() << "cannot set handle info"; } } - if (ErrorMode != TShellCommandOptions::HANDLE_INHERIT) { + if (ErrorMode != TShellCommandOptions::HANDLE_INHERIT) { if (!SetHandleInformation(pipes.ErrorPipeFd[1], HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT)) { ythrow TSystemError() << "cannot set handle info"; } @@ -536,23 +536,23 @@ void TShellCommand::TImpl::StartProcess(TShellCommand::TImpl::TPipes& pipes) { } // A sockets do not work as std streams for some reason - if (OutputMode != TShellCommandOptions::HANDLE_INHERIT) { + if (OutputMode != TShellCommandOptions::HANDLE_INHERIT) { startup_info.hStdOutput = pipes.OutputPipeFd[1]; } else { startup_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); } - if (ErrorMode != TShellCommandOptions::HANDLE_INHERIT) { + if (ErrorMode != TShellCommandOptions::HANDLE_INHERIT) { startup_info.hStdError = pipes.ErrorPipeFd[1]; } else { startup_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); } - if (InputMode != TShellCommandOptions::HANDLE_INHERIT) { + if (InputMode != TShellCommandOptions::HANDLE_INHERIT) { startup_info.hStdInput = pipes.InputPipeFd[0]; - } else { + } else { // Don't leave hStdInput unfilled, otherwise any attempt to retrieve the operating-system file handle // that is associated with the specified file descriptor will led to errors. startup_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE); - } + } PROCESS_INFORMATION process_info; // TString cmd = "cmd /U" + TUtf16String can be used to read unicode messages from cmd @@ -701,14 +701,14 @@ void TShellCommand::TImpl::OnFork(TPipes& pipes, sigset_t oldmask, char* const* // do not close fd 0 - next open will return it and confuse all readers /// @todo in case of real need - reopen /dev/null } - if (OutputMode != TShellCommandOptions::HANDLE_INHERIT) { + if (OutputMode != TShellCommandOptions::HANDLE_INHERIT) { pipes.OutputPipeFd[0].Close(); TFileHandle sOutNew(pipes.OutputPipeFd[1]); sOut.LinkTo(sOutNew); sOut.Release(); sOutNew.Release(); } - if (ErrorMode != TShellCommandOptions::HANDLE_INHERIT) { + if (ErrorMode != TShellCommandOptions::HANDLE_INHERIT) { pipes.ErrorPipeFd[0].Close(); TFileHandle sErrNew(pipes.ErrorPipeFd[1]); sErr.LinkTo(sErrNew); @@ -762,10 +762,10 @@ void TShellCommand::TImpl::Run() { CollectedError.clear(); TPipes pipes; - if (OutputMode != TShellCommandOptions::HANDLE_INHERIT) { + if (OutputMode != TShellCommandOptions::HANDLE_INHERIT) { TRealPipeHandle::Pipe(pipes.OutputPipeFd[0], pipes.OutputPipeFd[1], CloseOnExec); } - if (ErrorMode != TShellCommandOptions::HANDLE_INHERIT) { + if (ErrorMode != TShellCommandOptions::HANDLE_INHERIT) { TRealPipeHandle::Pipe(pipes.ErrorPipeFd[0], pipes.ErrorPipeFd[1], CloseOnExec); } if (InputMode != TShellCommandOptions::HANDLE_INHERIT) { @@ -849,16 +849,16 @@ void TShellCommand::TImpl::Run() { InputHandle.Swap(inputHandle); } - if (OutputMode == TShellCommandOptions::HANDLE_PIPE) { - TFileHandle outputHandle(pipes.OutputPipeFd[0].Release()); - OutputHandle.Swap(outputHandle); - } - - if (ErrorMode == TShellCommandOptions::HANDLE_PIPE) { - TFileHandle errorHandle(pipes.ErrorPipeFd[0].Release()); - ErrorHandle.Swap(errorHandle); - } - + if (OutputMode == TShellCommandOptions::HANDLE_PIPE) { + TFileHandle outputHandle(pipes.OutputPipeFd[0].Release()); + OutputHandle.Swap(outputHandle); + } + + if (ErrorMode == TShellCommandOptions::HANDLE_PIPE) { + TFileHandle errorHandle(pipes.ErrorPipeFd[0].Release()); + ErrorHandle.Swap(errorHandle); + } + TProcessInfo* processInfo = new TProcessInfo(this, pipes.InputPipeFd[1].Release(), pipes.OutputPipeFd[0].Release(), pipes.ErrorPipeFd[0].Release()); if (AsyncMode) { @@ -1167,14 +1167,14 @@ TFileHandle& TShellCommand::GetInputHandle() { return Impl->GetInputHandle(); } -TFileHandle& TShellCommand::GetOutputHandle() { - return Impl->GetOutputHandle(); -} - -TFileHandle& TShellCommand::GetErrorHandle() { - return Impl->GetErrorHandle(); -} - +TFileHandle& TShellCommand::GetOutputHandle() { + return Impl->GetOutputHandle(); +} + +TFileHandle& TShellCommand::GetErrorHandle() { + return Impl->GetErrorHandle(); +} + TShellCommand& TShellCommand::Run() { Impl->Run(); return *this; diff --git a/util/system/shellcommand.h b/util/system/shellcommand.h index 8730627fe5..a84b915987 100644 --- a/util/system/shellcommand.h +++ b/util/system/shellcommand.h @@ -49,8 +49,8 @@ public: , CloseStreams(false) , ShouldCloseInput(true) , InputMode(HANDLE_INHERIT) - , OutputMode(HANDLE_STREAM) - , ErrorMode(HANDLE_STREAM) + , OutputMode(HANDLE_STREAM) + , ErrorMode(HANDLE_STREAM) , InputStream(nullptr) , OutputStream(nullptr) , ErrorStream(nullptr) @@ -259,18 +259,18 @@ public: return *this; } - inline TShellCommandOptions& PipeOutput() { - OutputMode = HANDLE_PIPE; - OutputStream = nullptr; - return *this; - } - - inline TShellCommandOptions& PipeError() { - ErrorMode = HANDLE_PIPE; - ErrorStream = nullptr; - return *this; - } - + inline TShellCommandOptions& PipeOutput() { + OutputMode = HANDLE_PIPE; + OutputStream = nullptr; + return *this; + } + + inline TShellCommandOptions& PipeError() { + ErrorMode = HANDLE_PIPE; + ErrorStream = nullptr; + return *this; + } + /** * @brief set if child should inherit output handle * @@ -279,7 +279,7 @@ public: * @return self */ inline TShellCommandOptions& SetInheritOutput(bool inherit) { - OutputMode = inherit ? HANDLE_INHERIT : HANDLE_STREAM; + OutputMode = inherit ? HANDLE_INHERIT : HANDLE_STREAM; return *this; } @@ -291,24 +291,24 @@ public: * @return self */ inline TShellCommandOptions& SetInheritError(bool inherit) { - ErrorMode = inherit ? HANDLE_INHERIT : HANDLE_STREAM; + ErrorMode = inherit ? HANDLE_INHERIT : HANDLE_STREAM; return *this; } public: - bool ClearSignalMask = false; - bool CloseAllFdsOnExec = false; - bool AsyncMode = false; - size_t PollDelayMs = 0; - bool UseShell = false; - bool QuoteArguments = false; - bool DetachSession = false; - bool CloseStreams = false; - bool ShouldCloseInput = false; - EHandleMode InputMode = HANDLE_STREAM; - EHandleMode OutputMode = HANDLE_STREAM; - EHandleMode ErrorMode = HANDLE_STREAM; - + bool ClearSignalMask = false; + bool CloseAllFdsOnExec = false; + bool AsyncMode = false; + size_t PollDelayMs = 0; + bool UseShell = false; + bool QuoteArguments = false; + bool DetachSession = false; + bool CloseStreams = false; + bool ShouldCloseInput = false; + EHandleMode InputMode = HANDLE_STREAM; + EHandleMode OutputMode = HANDLE_STREAM; + EHandleMode ErrorMode = HANDLE_STREAM; + /// @todo more options // bool SearchPath // search exe name in $PATH // bool UnicodeConsole @@ -319,7 +319,7 @@ public: IOutputStream* ErrorStream; TUserOptions User; THashMap<TString, TString> Environment; - int Nice = 0; + int Nice = 0; static const size_t DefaultSyncPollDelay = 1000; // ms std::function<void()> FuncAfterFork = {}; diff --git a/util/system/shellcommand_ut.cpp b/util/system/shellcommand_ut.cpp index 9d849279d2..88caf51d45 100644 --- a/util/system/shellcommand_ut.cpp +++ b/util/system/shellcommand_ut.cpp @@ -436,35 +436,35 @@ Y_UNIT_TEST_SUITE(TShellCommandTest) { 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); - options.PipeOutput(); - constexpr TStringBuf firstMessage = "first message"; - constexpr TStringBuf secondMessage = "second message"; - const TString command = TStringBuilder() << "echo '" << firstMessage << "' && sleep 10 && echo '" << secondMessage << "'"; - TShellCommand cmd(command, options); - cmd.Run(); - TUnbufferedFileInput cmdOutput(TFile(cmd.GetOutputHandle().Release())); - TString firstLineOutput, secondLineOutput; - { - Sleep(TDuration::Seconds(5)); - firstLineOutput = cmdOutput.ReadLine(); - cmd.Wait(); - secondLineOutput = cmdOutput.ReadLine(); - } - UNIT_ASSERT_VALUES_EQUAL(firstLineOutput, firstMessage); - UNIT_ASSERT_VALUES_EQUAL(secondLineOutput, secondLineOutput); - } - Y_UNIT_TEST(TestOptionsConsistency) { - TShellCommandOptions options; - - options.SetInheritOutput(false).SetInheritError(false); - options.SetOutputStream(nullptr).SetErrorStream(nullptr); - - UNIT_ASSERT(options.OutputMode == TShellCommandOptions::HANDLE_STREAM); - UNIT_ASSERT(options.ErrorMode == TShellCommandOptions::HANDLE_STREAM); - } + Y_UNIT_TEST(TestPipeOutput) { + TShellCommandOptions options; + options.SetAsync(true); + options.PipeOutput(); + constexpr TStringBuf firstMessage = "first message"; + constexpr TStringBuf secondMessage = "second message"; + const TString command = TStringBuilder() << "echo '" << firstMessage << "' && sleep 10 && echo '" << secondMessage << "'"; + TShellCommand cmd(command, options); + cmd.Run(); + TUnbufferedFileInput cmdOutput(TFile(cmd.GetOutputHandle().Release())); + TString firstLineOutput, secondLineOutput; + { + Sleep(TDuration::Seconds(5)); + firstLineOutput = cmdOutput.ReadLine(); + cmd.Wait(); + secondLineOutput = cmdOutput.ReadLine(); + } + UNIT_ASSERT_VALUES_EQUAL(firstLineOutput, firstMessage); + UNIT_ASSERT_VALUES_EQUAL(secondLineOutput, secondLineOutput); + } + Y_UNIT_TEST(TestOptionsConsistency) { + TShellCommandOptions options; + + options.SetInheritOutput(false).SetInheritError(false); + options.SetOutputStream(nullptr).SetErrorStream(nullptr); + + UNIT_ASSERT(options.OutputMode == TShellCommandOptions::HANDLE_STREAM); + UNIT_ASSERT(options.ErrorMode == TShellCommandOptions::HANDLE_STREAM); + } Y_UNIT_TEST(TestForkCallback) { TString tmpFile = TString("shellcommand_ut.test_for_callback.txt"); TFsPath cwd(::NFs::CurrentWorkingDirectory()); |