1 | // |
---|
2 | // Process_VMS.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/Process_VMS.cpp#1 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Processes |
---|
8 | // Module: Process |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include <Poco/Process_VMS.h> |
---|
38 | #include <Poco/NumberFormatter.h> |
---|
39 | #include <Poco/NamedEvent.h> |
---|
40 | #include <times.h> |
---|
41 | #include <time.h> |
---|
42 | |
---|
43 | |
---|
44 | namespace Poco { |
---|
45 | |
---|
46 | |
---|
47 | // |
---|
48 | // ProcessHandleImpl |
---|
49 | // |
---|
50 | ProcessHandleImpl::ProcessHandleImpl(pid_t pid): |
---|
51 | _pid(pid) |
---|
52 | { |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | ProcessHandleImpl::~ProcessHandleImpl() |
---|
57 | { |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | pid_t ProcessHandleImpl::id() const |
---|
62 | { |
---|
63 | return _pid; |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | int ProcessHandleImpl::wait() const |
---|
68 | { |
---|
69 | int status; |
---|
70 | if (waitpid(_pid, &status, 0) != _pid) |
---|
71 | throw SystemException("Cannot wait for process", NumberFormatter::format(_pid)); |
---|
72 | return WEXITSTATUS(status); |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | // |
---|
77 | // ProcessImpl |
---|
78 | // |
---|
79 | ProcessImpl::PIDImpl ProcessImpl::idImpl() |
---|
80 | { |
---|
81 | return getpid(); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | void ProcessImpl::timesImpl(long& userTime, long& kernelTime) |
---|
86 | { |
---|
87 | struct tms buffer; |
---|
88 | times(&buffer)*1000/CLOCKS_PER_SEC; |
---|
89 | userTime = buffer.tms_utime/CLOCKS_PER_SEC; |
---|
90 | kernelTime = buffer.tms_stime/CLOCKS_PER_SEC; |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const ArgsImpl& args, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe) |
---|
95 | { |
---|
96 | char** argv = new char*[args.size() + 2]; |
---|
97 | int i = 0; |
---|
98 | argv[i++] = const_cast<char*>(command.c_str()); |
---|
99 | for (ArgsImpl::const_iterator it = args.begin(); it != args.end(); ++it) |
---|
100 | argv[i++] = const_cast<char*>(it->c_str()); |
---|
101 | argv[i] = NULL; |
---|
102 | try |
---|
103 | { |
---|
104 | int pid = vfork(); |
---|
105 | if (pid < 0) |
---|
106 | { |
---|
107 | throw SystemException("Cannot fork process for", command); |
---|
108 | } |
---|
109 | else if (pid == 0) |
---|
110 | { |
---|
111 | if (execvp(command.c_str(), argv) == -1) |
---|
112 | throw SystemException("Cannot execute command", command); |
---|
113 | } |
---|
114 | else |
---|
115 | { |
---|
116 | delete [] argv; |
---|
117 | return new ProcessHandleImpl(pid); |
---|
118 | } |
---|
119 | } |
---|
120 | catch (...) |
---|
121 | { |
---|
122 | delete [] argv; |
---|
123 | throw; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | void ProcessImpl::killImpl(PIDImpl pid) |
---|
129 | { |
---|
130 | if (kill(pid, SIGKILL) != 0) |
---|
131 | { |
---|
132 | switch (errno) |
---|
133 | { |
---|
134 | case ESRCH: |
---|
135 | throw NotFoundException("cannot kill process"); |
---|
136 | case EPERM: |
---|
137 | throw NoPermissionException("cannot kill process"); |
---|
138 | default: |
---|
139 | throw SystemException("cannot kill process"); |
---|
140 | } |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | void ProcessImpl::requestTerminationImpl(PIDImpl pid) |
---|
146 | { |
---|
147 | std::string evName("POCOTRM"); |
---|
148 | evName.append(NumberFormatter::formatHex(pid, 8)); |
---|
149 | NamedEvent ev(evName); |
---|
150 | ev.set(); |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | } // namespace Poco |
---|