1 | // |
2 | // Process.cpp |
3 | // |
4 | // Library: Foundation |
5 | // Package: Processes |
6 | // Module: Process |
7 | // |
8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/Process.h" |
16 | #include "Poco/Environment.h" |
17 | |
18 | |
19 | namespace |
20 | { |
21 | std::vector<char> getEnvironmentVariablesBuffer(const Poco::Process::Env& env) |
22 | { |
23 | std::vector<char> envbuf; |
24 | std::size_t pos = 0; |
25 | |
26 | for (Poco::Process::Env::const_iterator it = env.begin(); it != env.end(); ++it) |
27 | { |
28 | std::size_t envlen = it->first.length() + it->second.length() + 1; |
29 | |
30 | envbuf.resize(pos + envlen + 1); |
31 | std::copy(it->first.begin(), it->first.end(), &envbuf[pos]); |
32 | pos += it->first.length(); |
33 | envbuf[pos] = '='; |
34 | ++pos; |
35 | std::copy(it->second.begin(), it->second.end(), &envbuf[pos]); |
36 | pos += it->second.length(); |
37 | |
38 | envbuf[pos] = '\0'; |
39 | ++pos; |
40 | } |
41 | |
42 | envbuf.resize(pos + 1); |
43 | envbuf[pos] = '\0'; |
44 | |
45 | return envbuf; |
46 | } |
47 | } |
48 | |
49 | |
50 | #if defined(POCO_OS_FAMILY_WINDOWS) |
51 | #if defined(_WIN32_WCE) |
52 | #include "Process_WINCE.cpp" |
53 | #else |
54 | #include "Process_WIN32.cpp" |
55 | #endif |
56 | #elif defined(POCO_VXWORKS) |
57 | #include "Process_VX.cpp" |
58 | #elif defined(POCO_OS_FAMILY_UNIX) |
59 | #include "Process_UNIX.cpp" |
60 | #endif |
61 | |
62 | |
63 | namespace Poco { |
64 | |
65 | |
66 | // |
67 | // ProcessHandle |
68 | // |
69 | ProcessHandle::ProcessHandle(const ProcessHandle& handle): |
70 | _pImpl(handle._pImpl) |
71 | { |
72 | _pImpl->duplicate(); |
73 | } |
74 | |
75 | |
76 | ProcessHandle::~ProcessHandle() |
77 | { |
78 | _pImpl->release(); |
79 | } |
80 | |
81 | |
82 | ProcessHandle::ProcessHandle(ProcessHandleImpl* pImpl): |
83 | _pImpl(pImpl) |
84 | { |
85 | poco_check_ptr (_pImpl); |
86 | } |
87 | |
88 | |
89 | ProcessHandle& ProcessHandle::operator = (const ProcessHandle& handle) |
90 | { |
91 | if (&handle != this) |
92 | { |
93 | _pImpl->release(); |
94 | _pImpl = handle._pImpl; |
95 | _pImpl->duplicate(); |
96 | } |
97 | return *this; |
98 | } |
99 | |
100 | |
101 | ProcessHandle::PID ProcessHandle::id() const |
102 | { |
103 | return _pImpl->id(); |
104 | } |
105 | |
106 | |
107 | int ProcessHandle::wait() const |
108 | { |
109 | return _pImpl->wait(); |
110 | } |
111 | |
112 | |
113 | // |
114 | // Process |
115 | // |
116 | ProcessHandle Process::launch(const std::string& command, const Args& args) |
117 | { |
118 | std::string initialDirectory; |
119 | Env env; |
120 | return ProcessHandle(launchImpl(command, args, initialDirectory, 0, 0, 0, env)); |
121 | } |
122 | |
123 | |
124 | ProcessHandle Process::launch(const std::string& command, const Args& args, const std::string& initialDirectory) |
125 | { |
126 | Env env; |
127 | return ProcessHandle(launchImpl(command, args, initialDirectory, 0, 0, 0, env)); |
128 | } |
129 | |
130 | |
131 | ProcessHandle Process::launch(const std::string& command, const Args& args, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe) |
132 | { |
133 | poco_assert (inPipe == 0 || (inPipe != outPipe && inPipe != errPipe)); |
134 | std::string initialDirectory; |
135 | Env env; |
136 | return ProcessHandle(launchImpl(command, args, initialDirectory, inPipe, outPipe, errPipe, env)); |
137 | } |
138 | |
139 | |
140 | ProcessHandle Process::launch(const std::string& command, const Args& args, const std::string& initialDirectory, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe) |
141 | { |
142 | poco_assert (inPipe == 0 || (inPipe != outPipe && inPipe != errPipe)); |
143 | Env env; |
144 | return ProcessHandle(launchImpl(command, args, initialDirectory, inPipe, outPipe, errPipe, env)); |
145 | } |
146 | |
147 | |
148 | ProcessHandle Process::launch(const std::string& command, const Args& args, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe, const Env& env) |
149 | { |
150 | poco_assert (inPipe == 0 || (inPipe != outPipe && inPipe != errPipe)); |
151 | std::string initialDirectory; |
152 | return ProcessHandle(launchImpl(command, args, initialDirectory, inPipe, outPipe, errPipe, env)); |
153 | } |
154 | |
155 | |
156 | ProcessHandle Process::launch(const std::string& command, const Args& args, const std::string& initialDirectory, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe, const Env& env) |
157 | { |
158 | poco_assert (inPipe == 0 || (inPipe != outPipe && inPipe != errPipe)); |
159 | return ProcessHandle(launchImpl(command, args, initialDirectory, inPipe, outPipe, errPipe, env)); |
160 | } |
161 | |
162 | |
163 | int Process::wait(const ProcessHandle& handle) |
164 | { |
165 | return handle.wait(); |
166 | } |
167 | |
168 | |
169 | void Process::kill(ProcessHandle& handle) |
170 | { |
171 | killImpl(*handle._pImpl); |
172 | } |
173 | |
174 | |
175 | void Process::kill(PID pid) |
176 | { |
177 | killImpl(pid); |
178 | } |
179 | |
180 | bool Process::isRunning(const ProcessHandle& handle) |
181 | { |
182 | return isRunningImpl(*handle._pImpl); |
183 | } |
184 | bool Process::isRunning(PID pid) |
185 | { |
186 | return isRunningImpl(pid); |
187 | } |
188 | |
189 | void Process::requestTermination(PID pid) |
190 | { |
191 | requestTerminationImpl(pid); |
192 | } |
193 | |
194 | |
195 | } // namespace Poco |
196 | |