blob: 5f6e8cc114a03d0729b7e615ee5d3c0bd32b20b0 [file] [log] [blame]
Elly Jonesa44d22d2012-01-05 18:05:56 -05001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jones0c016cc2011-12-19 16:19:22 -05002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Vakulenko262be3f2014-07-30 15:25:50 -07005#include "debugd/src/subprocess_tool.h"
Elly Jones0c016cc2011-12-19 16:19:22 -05006
Elly Jones0c016cc2011-12-19 16:19:22 -05007#include <signal.h>
8
Ben Chana0011d82014-05-13 00:19:29 -07009#include <dbus-c++/dbus.h>
10
Alex Vakulenko262be3f2014-07-30 15:25:50 -070011#include "debugd/src/process_with_id.h"
Elly Jones0c016cc2011-12-19 16:19:22 -050012
13namespace debugd {
14
Ben Chana0011d82014-05-13 00:19:29 -070015namespace {
16
17const char kErrorNoSuchProcess[] = "org.chromium.debugd.error.NoSuchProcess";
18
19} // namespace
Elly Jones0c016cc2011-12-19 16:19:22 -050020
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070021ProcessWithId* SubprocessTool::CreateProcess(bool sandboxed) {
22 return CreateProcess(sandboxed, false);
23}
24
25ProcessWithId* SubprocessTool::CreateProcess(bool sandboxed,
26 bool access_root_mount_ns) {
Elly Jones0c016cc2011-12-19 16:19:22 -050027 ProcessWithId* p = new ProcessWithId();
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070028 if (!sandboxed)
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050029 p->DisableSandbox();
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070030 if (access_root_mount_ns)
31 p->AllowAccessRootMountNamespace();
Elly Jones0c016cc2011-12-19 16:19:22 -050032 if (!p->Init() || processes_.count(p->id()) == 1)
33 return NULL;
34 processes_[p->id()] = p;
35 return p;
36}
37
Ben Chana0011d82014-05-13 00:19:29 -070038void SubprocessTool::Stop(const std::string& handle, DBus::Error* error) {
Elly Jones0c016cc2011-12-19 16:19:22 -050039 if (processes_.count(handle) != 1) {
Ben Chana0011d82014-05-13 00:19:29 -070040 error->set(kErrorNoSuchProcess, handle.c_str());
Elly Jones0c016cc2011-12-19 16:19:22 -050041 return;
42 }
43 ProcessWithId* p = processes_[handle];
44 p->Kill(SIGKILL, 0); // no timeout
45 p->Wait();
46 processes_.erase(handle);
47 delete p;
48}
49
Ben Chana0011d82014-05-13 00:19:29 -070050} // namespace debugd