blob: be7d0be42471d6842df0640081c663c53ae3b4ea [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
Eric Carusocc7106c2017-04-27 14:22:42 -070011#include "debugd/src/error_utils.h"
12
Elly Jones0c016cc2011-12-19 16:19:22 -050013namespace 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 bool access_root_mount_ns) {
Ben Chan23142892017-02-15 12:34:13 -080023 auto process = base::MakeUnique<ProcessWithId>();
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070024 if (!sandboxed)
Ben Chan23142892017-02-15 12:34:13 -080025 process->DisableSandbox();
26
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070027 if (access_root_mount_ns)
Ben Chan23142892017-02-15 12:34:13 -080028 process->AllowAccessRootMountNamespace();
29
30 if (!process->Init() || processes_.count(process->id()) == 1)
Ben Chan64d19b22017-02-06 14:03:47 -080031 return nullptr;
Ben Chan23142892017-02-15 12:34:13 -080032
33 ProcessWithId* process_ptr = process.get();
34 processes_[process->id()] = std::move(process);
35 return process_ptr;
Elly Jones0c016cc2011-12-19 16:19:22 -050036}
37
Eric Carusocc7106c2017-04-27 14:22:42 -070038bool SubprocessTool::Stop(const std::string& handle, brillo::ErrorPtr* error) {
Elly Jones0c016cc2011-12-19 16:19:22 -050039 if (processes_.count(handle) != 1) {
Eric Carusocc7106c2017-04-27 14:22:42 -070040 DEBUGD_ADD_ERROR(error, kErrorNoSuchProcess, handle.c_str());
Eric Caruso8fe49c72017-04-25 10:43:59 -070041 return false;
Elly Jones0c016cc2011-12-19 16:19:22 -050042 }
Ben Chan23142892017-02-15 12:34:13 -080043 ProcessWithId* process_ptr = processes_[handle].get();
44 process_ptr->KillProcessGroup();
Elly Jones0c016cc2011-12-19 16:19:22 -050045 processes_.erase(handle);
Eric Caruso8fe49c72017-04-25 10:43:59 -070046 return true;
Elly Jones0c016cc2011-12-19 16:19:22 -050047}
48
Ben Chana0011d82014-05-13 00:19:29 -070049} // namespace debugd