blob: f616e3987dac5aa532e0d2da7983f44cdf86d243 [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>
Mike Frysingerf9da3d32017-09-19 23:41:27 -040010#include <base/stl_util.h>
Elly Jones0c016cc2011-12-19 16:19:22 -050011
Eric Carusocc7106c2017-04-27 14:22:42 -070012#include "debugd/src/error_utils.h"
13
Elly Jones0c016cc2011-12-19 16:19:22 -050014namespace debugd {
15
Ben Chana0011d82014-05-13 00:19:29 -070016namespace {
17
18const char kErrorNoSuchProcess[] = "org.chromium.debugd.error.NoSuchProcess";
19
20} // namespace
Elly Jones0c016cc2011-12-19 16:19:22 -050021
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070022ProcessWithId* SubprocessTool::CreateProcess(bool sandboxed,
23 bool access_root_mount_ns) {
Ben Chan23142892017-02-15 12:34:13 -080024 auto process = base::MakeUnique<ProcessWithId>();
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070025 if (!sandboxed)
Ben Chan23142892017-02-15 12:34:13 -080026 process->DisableSandbox();
27
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070028 if (access_root_mount_ns)
Ben Chan23142892017-02-15 12:34:13 -080029 process->AllowAccessRootMountNamespace();
30
Mike Frysingerf9da3d32017-09-19 23:41:27 -040031 if (!process->Init())
Ben Chan64d19b22017-02-06 14:03:47 -080032 return nullptr;
Ben Chan23142892017-02-15 12:34:13 -080033
34 ProcessWithId* process_ptr = process.get();
Mike Frysingerf9da3d32017-09-19 23:41:27 -040035 if (RecordProcess(std::move(process)))
36 return process_ptr;
37
38 return nullptr;
39}
40
41bool SubprocessTool::RecordProcess(std::unique_ptr<ProcessWithId> process) {
42 if (ContainsKey(processes_, process->id()))
43 return false;
44
Ben Chan23142892017-02-15 12:34:13 -080045 processes_[process->id()] = std::move(process);
Mike Frysingerf9da3d32017-09-19 23:41:27 -040046 return true;
Elly Jones0c016cc2011-12-19 16:19:22 -050047}
48
Eric Carusocc7106c2017-04-27 14:22:42 -070049bool SubprocessTool::Stop(const std::string& handle, brillo::ErrorPtr* error) {
Elly Jones0c016cc2011-12-19 16:19:22 -050050 if (processes_.count(handle) != 1) {
Eric Carusocc7106c2017-04-27 14:22:42 -070051 DEBUGD_ADD_ERROR(error, kErrorNoSuchProcess, handle.c_str());
Eric Caruso8fe49c72017-04-25 10:43:59 -070052 return false;
Elly Jones0c016cc2011-12-19 16:19:22 -050053 }
Ben Chan23142892017-02-15 12:34:13 -080054 ProcessWithId* process_ptr = processes_[handle].get();
55 process_ptr->KillProcessGroup();
Elly Jones0c016cc2011-12-19 16:19:22 -050056 processes_.erase(handle);
Eric Caruso8fe49c72017-04-25 10:43:59 -070057 return true;
Elly Jones0c016cc2011-12-19 16:19:22 -050058}
59
Ben Chana0011d82014-05-13 00:19:29 -070060} // namespace debugd