blob: b001e42e6447dcacc553d0f24e548bc0a9a7a15f [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 Chan8e9f6d02017-09-26 23:04:21 -07007#include <memory>
Mike Frysinger56379d72019-02-19 16:03:03 -05008#include <string>
Ben Chan23142892017-02-15 12:34:13 -08009#include <utility>
Mike Frysinger56379d72019-02-19 16:03:03 -050010#include <vector>
Elly Jones0c016cc2011-12-19 16:19:22 -050011
Mike Frysingerf9da3d32017-09-19 23:41:27 -040012#include <base/stl_util.h>
Elly Jones0c016cc2011-12-19 16:19:22 -050013
Eric Carusocc7106c2017-04-27 14:22:42 -070014#include "debugd/src/error_utils.h"
15
Elly Jones0c016cc2011-12-19 16:19:22 -050016namespace debugd {
17
Ben Chana0011d82014-05-13 00:19:29 -070018namespace {
19
20const char kErrorNoSuchProcess[] = "org.chromium.debugd.error.NoSuchProcess";
21
22} // namespace
Elly Jones0c016cc2011-12-19 16:19:22 -050023
Mike Frysinger56379d72019-02-19 16:03:03 -050024ProcessWithId* SubprocessTool::CreateProcess(
25 bool sandboxed,
26 bool access_root_mount_ns,
27 const std::vector<std::string>& minijail_extra_args) {
Ben Chan8e9f6d02017-09-26 23:04:21 -070028 auto process = std::make_unique<ProcessWithId>();
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070029 if (!sandboxed)
Ben Chan23142892017-02-15 12:34:13 -080030 process->DisableSandbox();
31
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070032 if (access_root_mount_ns)
Ben Chan23142892017-02-15 12:34:13 -080033 process->AllowAccessRootMountNamespace();
34
Mike Frysinger56379d72019-02-19 16:03:03 -050035 if (!process->Init(minijail_extra_args))
Ben Chan64d19b22017-02-06 14:03:47 -080036 return nullptr;
Ben Chan23142892017-02-15 12:34:13 -080037
38 ProcessWithId* process_ptr = process.get();
Mike Frysingerf9da3d32017-09-19 23:41:27 -040039 if (RecordProcess(std::move(process)))
40 return process_ptr;
41
42 return nullptr;
43}
44
Mike Frysinger56379d72019-02-19 16:03:03 -050045ProcessWithId* SubprocessTool::CreateProcess(bool sandboxed,
46 bool access_root_mount_ns) {
47 return CreateProcess(sandboxed, access_root_mount_ns, {});
48}
49
Mike Frysingerf9da3d32017-09-19 23:41:27 -040050bool SubprocessTool::RecordProcess(std::unique_ptr<ProcessWithId> process) {
Qijiang Fan52439042020-06-17 15:34:38 +090051 if (base::Contains(processes_, process->id()))
Mike Frysingerf9da3d32017-09-19 23:41:27 -040052 return false;
53
Ben Chan23142892017-02-15 12:34:13 -080054 processes_[process->id()] = std::move(process);
Mike Frysingerf9da3d32017-09-19 23:41:27 -040055 return true;
Elly Jones0c016cc2011-12-19 16:19:22 -050056}
57
Eric Carusocc7106c2017-04-27 14:22:42 -070058bool SubprocessTool::Stop(const std::string& handle, brillo::ErrorPtr* error) {
Elly Jones0c016cc2011-12-19 16:19:22 -050059 if (processes_.count(handle) != 1) {
Eric Carusocc7106c2017-04-27 14:22:42 -070060 DEBUGD_ADD_ERROR(error, kErrorNoSuchProcess, handle.c_str());
Eric Caruso8fe49c72017-04-25 10:43:59 -070061 return false;
Elly Jones0c016cc2011-12-19 16:19:22 -050062 }
Ben Chan23142892017-02-15 12:34:13 -080063 ProcessWithId* process_ptr = processes_[handle].get();
64 process_ptr->KillProcessGroup();
Elly Jones0c016cc2011-12-19 16:19:22 -050065 processes_.erase(handle);
Eric Caruso8fe49c72017-04-25 10:43:59 -070066 return true;
Elly Jones0c016cc2011-12-19 16:19:22 -050067}
68
Ben Chana0011d82014-05-13 00:19:29 -070069} // namespace debugd