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
19namespace
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#if defined(POCO_OS_FAMILY_VMS)
49 void setEnvironmentVariables(const Poco::Process::Env& env)
50 {
51 for (Poco::Process::Env::const_iterator it = env.begin(); it != env.end(); ++it)
52 {
53 Poco::Environment::set(it->first, it->second);
54 }
55 }
56#endif
57}
58
59
60#if defined(POCO_OS_FAMILY_WINDOWS)
61#if defined(_WIN32_WCE)
62#include "Process_WINCE.cpp"
63#else
64#include "Process_WIN32.cpp"
65#endif
66#elif defined(POCO_VXWORKS)
67#include "Process_VX.cpp"
68#elif defined(POCO_OS_FAMILY_UNIX)
69#include "Process_UNIX.cpp"
70#else
71#include "Process_VMS.cpp"
72#endif
73
74
75namespace Poco {
76
77
78//
79// ProcessHandle
80//
81ProcessHandle::ProcessHandle(const ProcessHandle& handle):
82 _pImpl(handle._pImpl)
83{
84 _pImpl->duplicate();
85}
86
87
88ProcessHandle::~ProcessHandle()
89{
90 _pImpl->release();
91}
92
93
94ProcessHandle::ProcessHandle(ProcessHandleImpl* pImpl):
95 _pImpl(pImpl)
96{
97 poco_check_ptr (_pImpl);
98}
99
100
101ProcessHandle& ProcessHandle::operator = (const ProcessHandle& handle)
102{
103 if (&handle != this)
104 {
105 _pImpl->release();
106 _pImpl = handle._pImpl;
107 _pImpl->duplicate();
108 }
109 return *this;
110}
111
112
113ProcessHandle::PID ProcessHandle::id() const
114{
115 return _pImpl->id();
116}
117
118
119int ProcessHandle::wait() const
120{
121 return _pImpl->wait();
122}
123
124
125//
126// Process
127//
128ProcessHandle Process::launch(const std::string& command, const Args& args)
129{
130 std::string initialDirectory;
131 Env env;
132 return ProcessHandle(launchImpl(command, args, initialDirectory, 0, 0, 0, env));
133}
134
135
136ProcessHandle Process::launch(const std::string& command, const Args& args, const std::string& initialDirectory)
137{
138 Env env;
139 return ProcessHandle(launchImpl(command, args, initialDirectory, 0, 0, 0, env));
140}
141
142
143ProcessHandle Process::launch(const std::string& command, const Args& args, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe)
144{
145 poco_assert (inPipe == 0 || (inPipe != outPipe && inPipe != errPipe));
146 std::string initialDirectory;
147 Env env;
148 return ProcessHandle(launchImpl(command, args, initialDirectory, inPipe, outPipe, errPipe, env));
149}
150
151
152ProcessHandle Process::launch(const std::string& command, const Args& args, const std::string& initialDirectory, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe)
153{
154 poco_assert (inPipe == 0 || (inPipe != outPipe && inPipe != errPipe));
155 Env env;
156 return ProcessHandle(launchImpl(command, args, initialDirectory, inPipe, outPipe, errPipe, env));
157}
158
159
160ProcessHandle Process::launch(const std::string& command, const Args& args, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe, const Env& env)
161{
162 poco_assert (inPipe == 0 || (inPipe != outPipe && inPipe != errPipe));
163 std::string initialDirectory;
164 return ProcessHandle(launchImpl(command, args, initialDirectory, inPipe, outPipe, errPipe, env));
165}
166
167
168ProcessHandle Process::launch(const std::string& command, const Args& args, const std::string& initialDirectory, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe, const Env& env)
169{
170 poco_assert (inPipe == 0 || (inPipe != outPipe && inPipe != errPipe));
171 return ProcessHandle(launchImpl(command, args, initialDirectory, inPipe, outPipe, errPipe, env));
172}
173
174
175int Process::wait(const ProcessHandle& handle)
176{
177 return handle.wait();
178}
179
180
181void Process::kill(ProcessHandle& handle)
182{
183 killImpl(*handle._pImpl);
184}
185
186
187void Process::kill(PID pid)
188{
189 killImpl(pid);
190}
191
192bool Process::isRunning(const ProcessHandle& handle)
193{
194 return isRunningImpl(*handle._pImpl);
195}
196bool Process::isRunning(PID pid)
197{
198 return isRunningImpl(pid);
199}
200
201void Process::requestTermination(PID pid)
202{
203 requestTerminationImpl(pid);
204}
205
206
207} // namespace Poco
208