Skip to content

Commit 2d7e7e0

Browse files
committed
Implement addChildProcess() to kill child processes on termination
* Implements the kill argument of OS.exec. * Implements the automatic termination of processes spawned by OS.pipe. * It notably kills the corresponding Tk instance when a VM terminates.
1 parent 661b37b commit 2d7e7e0

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

vm/boostenv/main/boostvm-decl.hh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ public:
9494

9595
void addMonitor(VMIdentifier monitor);
9696

97+
void addChildProcess(nativeint pid);
98+
9799
private:
100+
void killChildProcesses();
101+
98102
void notifyMonitors();
99103

100104
void terminate();
@@ -180,6 +184,10 @@ private:
180184
nativeint _terminationStatus;
181185
std::string _terminationReason;
182186

187+
// Spawned child processes to kill when terminating
188+
private:
189+
std::vector<nativeint> _childProcesses;
190+
183191
// Running thread management
184192
private:
185193
boost::asio::io_service::work* const _work;

vm/boostenv/main/boostvm.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
#include <boost/bind.hpp>
2929
#include <boost/random/random_device.hpp>
3030

31+
#ifdef MOZART_WINDOWS
32+
# include <windows.h>
33+
#else
34+
# include <csignal>
35+
#endif
36+
3137
namespace mozart { namespace boostenv {
3238

3339
/////////////
@@ -280,6 +286,24 @@ void BoostVM::addMonitor(VMIdentifier monitor) {
280286
_monitors.push_back(monitor);
281287
}
282288

289+
void BoostVM::addChildProcess(nativeint pid) {
290+
_childProcesses.push_back(pid);
291+
}
292+
293+
void BoostVM::killChildProcesses() {
294+
for (auto pid : _childProcesses) {
295+
#ifndef MOZART_WINDOWS
296+
kill(pid, SIGTERM);
297+
#else
298+
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
299+
if (hProcess) {
300+
TerminateProcess(hProcess, 0);
301+
CloseHandle(hProcess);
302+
}
303+
#endif
304+
}
305+
}
306+
283307
void BoostVM::notifyMonitors() {
284308
VMIdentifier deadVM = this->identifier;
285309
std::string reason = this->_terminationReason;
@@ -300,6 +324,7 @@ void BoostVM::terminate() {
300324
alarmTimer.cancel();
301325

302326
portClosed = true; // close VM port
327+
killChildProcesses();
303328
notifyMonitors();
304329

305330
env.removeTerminatedVM(identifier, _terminationStatus, _work);

vm/boostenv/main/modos.hh

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -890,8 +890,7 @@ public:
890890
vm->deleteStaticArray(argv, argc);
891891

892892
if (doKill) {
893-
// TODO
894-
// addChildProc(pid);
893+
BoostVM::forVM(vm).addChildProcess(pid);
895894
}
896895

897896
outPid = build(vm, pid);
@@ -1004,8 +1003,7 @@ public:
10041003

10051004
vm->deleteStaticArray(argv, argc);
10061005

1007-
// TODO
1008-
// addChildProc(pid);
1006+
BoostVM::forVM(vm).addChildProcess(pid);
10091007

10101008
outPid = build(vm, pid);
10111009
outStatus = buildSharp(vm, rsock, wsock);
@@ -1092,8 +1090,7 @@ public:
10921090

10931091
vm->deleteStaticArray(argv, argc);
10941092

1095-
// TODO
1096-
// addChildProc(pid);
1093+
BoostVM::forVM(vm).addChildProcess(pid);
10971094

10981095
outPid = build(vm, pid);
10991096
outStatus = std::move(ozPipe);

0 commit comments

Comments
 (0)