Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
ProcessRunner.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4
6
7 /*
8 ********************************************************************************
9 ******************* Execution::ProcessRunner::Exception ************************
10 ********************************************************************************
11 */
12 inline ProcessRunner::Exception::Exception (const String& failureMessage, const optional<String>& stderrFragment,
13 const optional<ExitStatusType>& wExitStatus, const optional<SignalID>& wTermSig)
14 : inherited{mkMsg_ (failureMessage, stderrFragment, wExitStatus, wTermSig)}
15 , fFailureMessage{failureMessage}
16 , fStderrFragment{stderrFragment}
17 , fExitStatus{wExitStatus}
18 , fTermSignal{wTermSig}
19 {
20 }
21
22 /*
23 ********************************************************************************
24 *************************** Execution::ProcessRunner ***************************
25 ********************************************************************************
26 */
27#if qCompilerAndStdLib_DefaultMemberInitializerNeededEnclosingForDefaultFunArg_Buggy
28 inline ProcessRunner::ProcessRunner (const filesystem::path& executable, const CommandLine& args)
29 : ProcessRunner{executable, args, Options{}}
30 {
31 }
32 inline ProcessRunner::ProcessRunner (const CommandLine& args)
33 : ProcessRunner{args, Options{}}
34 {
35 }
36 inline ProcessRunner::ProcessRunner (const String& commandLine)
37 : ProcessRunner{commandLine, Options{}}
38 {
39 }
40#endif
41 inline ProcessRunner::ProcessRunner (const filesystem::path& executable, const CommandLine& args, const Options& o)
42 : fExecutable_{executable}
43 , fArgs_{args}
44 , fOptions_{o}
45 {
46 }
47 inline ProcessRunner::ProcessRunner (const CommandLine& args, const Options& o)
48 : fArgs_{args}
49 , fOptions_{o}
50 {
51 }
52 inline ProcessRunner::ProcessRunner (const filesystem::path& executable, const CommandLine& args, const Streams::InputStream::Ptr<byte>& in,
53 const Streams::OutputStream::Ptr<byte>& out, const Streams::OutputStream::Ptr<byte>& error)
54 : fExecutable_{executable}
55 , fArgs_{args}
56 , fStdIn_{in}
57 , fStdOut_{out}
58 , fStdErr_{error}
59 {
60 }
61 inline ProcessRunner::ProcessRunner (const CommandLine& args, const Streams::InputStream::Ptr<byte>& in,
62 const Streams::OutputStream::Ptr<byte>& out, const Streams::OutputStream::Ptr<byte>& error)
63 : fExecutable_{}
64 , fArgs_{args}
65 , fStdIn_{in}
66 , fStdOut_{out}
67 , fStdErr_{error}
68 {
69 }
70 inline CommandLine ProcessRunner::GetCommandLine () const
71 {
72 Debug::AssertExternallySynchronizedMutex::ReadContext declareContext{fThisAssertExternallySynchronized_};
73 return fArgs_;
74 }
75 inline void ProcessRunner::SetCommandLine (const CommandLine& args)
76 {
77 Debug::AssertExternallySynchronizedMutex::WriteContext declareContext{fThisAssertExternallySynchronized_};
78 fArgs_ = args;
79 }
80 inline optional<filesystem::path> ProcessRunner::GetWorkingDirectory () const
81 {
82 Debug::AssertExternallySynchronizedMutex::ReadContext declareContext{fThisAssertExternallySynchronized_};
83 return fOptions_.fWorkingDirectory;
84 }
85 inline void ProcessRunner::SetWorkingDirectory (const optional<filesystem::path>& d)
86 {
87 Debug::AssertExternallySynchronizedMutex::WriteContext declareContext{fThisAssertExternallySynchronized_};
88 fOptions_.fWorkingDirectory = d;
89 }
90 inline auto ProcessRunner::GetOptions () const -> Options
91 {
92 Debug::AssertExternallySynchronizedMutex::ReadContext declareContext{fThisAssertExternallySynchronized_};
93 return fOptions_;
94 }
95 inline void ProcessRunner::SetOptions (const Options& o)
96 {
97 Debug::AssertExternallySynchronizedMutex::WriteContext declareContext{fThisAssertExternallySynchronized_};
98 fOptions_ = o;
99 }
100 inline Streams::InputStream::Ptr<byte> ProcessRunner::GetStdIn () const
101 {
102 Debug::AssertExternallySynchronizedMutex::ReadContext declareContext{fThisAssertExternallySynchronized_};
103 return fStdIn_;
104 }
105 inline void ProcessRunner::SetStdIn (const Streams::InputStream::Ptr<byte>& in)
106 {
107 Debug::AssertExternallySynchronizedMutex::WriteContext declareContext{fThisAssertExternallySynchronized_};
108 fStdIn_ = in;
109 }
110 inline Streams::OutputStream::Ptr<byte> ProcessRunner::GetStdOut () const
111 {
112 Debug::AssertExternallySynchronizedMutex::ReadContext declareWriteContext{fThisAssertExternallySynchronized_};
113 return fStdOut_;
114 }
115 inline void ProcessRunner::SetStdOut (const Streams::OutputStream::Ptr<byte>& out)
116 {
117 Debug::AssertExternallySynchronizedMutex::WriteContext declareContext{fThisAssertExternallySynchronized_};
118 fStdOut_ = out;
119 }
120 inline Streams::OutputStream::Ptr<byte> ProcessRunner::GetStdErr () const
121 {
122 Debug::AssertExternallySynchronizedMutex::ReadContext declareContext{fThisAssertExternallySynchronized_};
123 return fStdErr_;
124 }
125 inline void ProcessRunner::SetStdErr (const Streams::OutputStream::Ptr<byte>& err)
126 {
127 Debug::AssertExternallySynchronizedMutex::WriteContext declareContext{fThisAssertExternallySynchronized_};
128 fStdErr_ = err;
129 }
130
131 /*
132 ********************************************************************************
133 ******************* Execution::ProcessRunner::BackgroundProcess ****************
134 ********************************************************************************
135 */
136 inline optional<ProcessRunner::ProcessResultType> ProcessRunner::BackgroundProcess::GetProcessResult () const
137 {
138 Debug::AssertExternallySynchronizedMutex::ReadContext declareContext{fThisAssertExternallySynchronized_};
139 return fRep_->fResult;
140 }
141 inline optional<pid_t> ProcessRunner::BackgroundProcess::GetChildProcessID () const
142 {
143 Debug::AssertExternallySynchronizedMutex::ReadContext declareContext{fThisAssertExternallySynchronized_};
144 return fRep_->fPID;
145 }
146
147 /// DEPRECATED
148
149 [[deprecated ("Since Stroika v3.0d13 - use ProcessRunner{Options{.fDetached=true}")]] inline pid_t
150 DetachedProcessRunner (const filesystem::path& executable, const Containers::Sequence<String>& args)
151 {
152 ProcessRunner pr{executable, CommandLine{args}, ProcessRunner::Options{.fDetached = true}};
153 ProcessRunner::BackgroundProcess bp = pr.RunInBackground ();
154 bp.WaitForStarted ();
155 return Memory::ValueOf (bp.GetChildProcessID ());
156 }
157 [[deprecated ("Since Stroika v3.0d13 - use ProcessRunner{Options{.fDetached=true}")]] inline pid_t DetachedProcessRunner (const String& commandLine)
158
159 {
160 ProcessRunner pr{CommandLine{commandLine}, ProcessRunner::Options{.fDetached = true}};
161 ProcessRunner::BackgroundProcess bp = pr.RunInBackground ();
162 bp.WaitForStarted ();
163 return Memory::ValueOf (bp.GetChildProcessID ());
164 }
165
166}
A generalization of a vector: a container whose elements are keyed by the natural numbers.
Definition Sequence.h:187
shared_lock< const AssertExternallySynchronizedMutex > ReadContext
Instantiate AssertExternallySynchronizedMutex::ReadContext to designate an area of code where protect...
unique_lock< AssertExternallySynchronizedMutex > WriteContext
Instantiate AssertExternallySynchronizedMutex::WriteContext to designate an area of code where protec...
nonvirtual void WaitForStarted(Time::DurationSeconds timeout=Time::kInfinity) const
wait until GetChildProcessID () returns a valid answer, or until the process failed to start (in whic...
optional< pid_t > GetChildProcessID() const
maybe missing if process not yet (or ever successfully) launched. Child process may have already exit...
Run the given command, and optionally support stdin/stdout/stderr as streams (either sync with Run,...
InputStream<>::Ptr is Smart pointer (with abstract Rep) class defining the interface to reading from ...
OutputStream<>::Ptr is Smart pointer to a stream-based sink of data.
int pid_t
TODO - maybe move this to configuraiotn module???
Definition Module.h:34
pid_t DetachedProcessRunner(const filesystem::path &executable, const Containers::Sequence< String > &args)
DEPRECATED.