blob: 5bd3ce598d9208d923b82ca7bd93770177b630c1 [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
Ben Chan23142892017-02-15 12:34:13 -08007#include <utility>
Elly Jones0c016cc2011-12-19 16:19:22 -05008
Ben Chan23142892017-02-15 12:34:13 -08009#include <base/memory/ptr_util.h>
Elly Jones0c016cc2011-12-19 16:19:22 -050010
11namespace debugd {
12
Ben Chana0011d82014-05-13 00:19:29 -070013namespace {
14
15const char kErrorNoSuchProcess[] = "org.chromium.debugd.error.NoSuchProcess";
16
17} // namespace
Elly Jones0c016cc2011-12-19 16:19:22 -050018
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070019ProcessWithId* SubprocessTool::CreateProcess(bool sandboxed) {
20 return CreateProcess(sandboxed, false);
21}
22
23ProcessWithId* SubprocessTool::CreateProcess(bool sandboxed,
24 bool access_root_mount_ns) {
Ben Chan23142892017-02-15 12:34:13 -080025 auto process = base::MakeUnique<ProcessWithId>();
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070026 if (!sandboxed)
Ben Chan23142892017-02-15 12:34:13 -080027 process->DisableSandbox();
28
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070029 if (access_root_mount_ns)
Ben Chan23142892017-02-15 12:34:13 -080030 process->AllowAccessRootMountNamespace();
31
32 if (!process->Init() || processes_.count(process->id()) == 1)
Ben Chan64d19b22017-02-06 14:03:47 -080033 return nullptr;
Ben Chan23142892017-02-15 12:34:13 -080034
35 ProcessWithId* process_ptr = process.get();
36 processes_[process->id()] = std::move(process);
37 return process_ptr;
Elly Jones0c016cc2011-12-19 16:19:22 -050038}
39
Ben Chana0011d82014-05-13 00:19:29 -070040void SubprocessTool::Stop(const std::string& handle, DBus::Error* error) {
Elly Jones0c016cc2011-12-19 16:19:22 -050041 if (processes_.count(handle) != 1) {
Ben Chana0011d82014-05-13 00:19:29 -070042 error->set(kErrorNoSuchProcess, handle.c_str());
Elly Jones0c016cc2011-12-19 16:19:22 -050043 return;
44 }
Ben Chan23142892017-02-15 12:34:13 -080045 ProcessWithId* process_ptr = processes_[handle].get();
46 process_ptr->KillProcessGroup();
Elly Jones0c016cc2011-12-19 16:19:22 -050047 processes_.erase(handle);
Elly Jones0c016cc2011-12-19 16:19:22 -050048}
49
Ben Chana0011d82014-05-13 00:19:29 -070050} // namespace debugd