blob: 1da1d0ec9d56447641d4cd05bbc2afb89126ced9 [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
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050021ProcessWithId* SubprocessTool::CreateProcess(bool sandbox) {
Elly Jones0c016cc2011-12-19 16:19:22 -050022 ProcessWithId* p = new ProcessWithId();
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050023 if (!sandbox)
24 p->DisableSandbox();
Elly Jones0c016cc2011-12-19 16:19:22 -050025 if (!p->Init() || processes_.count(p->id()) == 1)
26 return NULL;
27 processes_[p->id()] = p;
28 return p;
29}
30
Ben Chana0011d82014-05-13 00:19:29 -070031void SubprocessTool::Stop(const std::string& handle, DBus::Error* error) {
Elly Jones0c016cc2011-12-19 16:19:22 -050032 if (processes_.count(handle) != 1) {
Ben Chana0011d82014-05-13 00:19:29 -070033 error->set(kErrorNoSuchProcess, handle.c_str());
Elly Jones0c016cc2011-12-19 16:19:22 -050034 return;
35 }
36 ProcessWithId* p = processes_[handle];
37 p->Kill(SIGKILL, 0); // no timeout
38 p->Wait();
39 processes_.erase(handle);
40 delete p;
41}
42
Ben Chana0011d82014-05-13 00:19:29 -070043} // namespace debugd