blob: 67229e9e2ca16537379fdc231bd9af54e9198f10 [file] [log] [blame]
Darin Petkov5a850472012-06-06 15:44:24 +02001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/process_killer.h"
6
Darin Petkov84a95532013-03-13 12:24:45 +01007#include <errno.h>
8#include <sys/wait.h>
9
Darin Petkov5a850472012-06-06 15:44:24 +020010using base::Closure;
11using std::map;
12
13namespace shill {
14
15namespace {
16
17base::LazyInstance<ProcessKiller> g_process_killer = LAZY_INSTANCE_INITIALIZER;
18
19} // namespace
20
21ProcessKiller::ProcessKiller() {}
22
23ProcessKiller::~ProcessKiller() {
24 // There's no need to release any GLib child watch sources because this class
25 // is a singleton and will be destroyed when this process exits, i.e., when
26 // GLib is shut down.
27}
28
29// static
30ProcessKiller *ProcessKiller::GetInstance() {
31 return g_process_killer.Pointer();
32}
33
Darin Petkov84a95532013-03-13 12:24:45 +010034bool ProcessKiller::Wait(int pid, const Closure &callback) {
Darin Petkov38066762012-12-17 15:35:45 +010035 LOG(INFO) << "Waiting for pid " << pid;
Darin Petkov5a850472012-06-06 15:44:24 +020036 if (!callback.is_null()) {
37 callbacks_[pid] = callback;
38 }
Darin Petkov84a95532013-03-13 12:24:45 +010039 // Check if the child process is dead already. This guards against the case
40 // when the caller had registered a child watch on that process but the
41 // process exited before the caller removed the watch and invoked this.
42 int status = 0;
43 pid_t rpid = waitpid(pid, &status, WNOHANG);
44 if (rpid == -1) {
45 DCHECK_EQ(ECHILD, errno);
46 LOG(INFO) << "No such child -- assuming process has already exited.";
47 OnProcessDied(pid, 0, this);
48 return false;
49 }
50 if (rpid == pid) {
51 LOG(INFO) << "Process has already exited.";
52 OnProcessDied(pid, status, this);
53 return false;
54 }
Darin Petkov5a850472012-06-06 15:44:24 +020055 g_child_watch_add(pid, OnProcessDied, this);
Darin Petkov84a95532013-03-13 12:24:45 +010056 return true;
Darin Petkov38066762012-12-17 15:35:45 +010057}
58
59void ProcessKiller::Kill(int pid, const Closure &callback) {
Darin Petkov84a95532013-03-13 12:24:45 +010060 if (!Wait(pid, callback)) {
61 LOG(INFO) << "Process already dead, no need to kill.";
62 return;
63 }
Darin Petkov38066762012-12-17 15:35:45 +010064 LOG(INFO) << "Killing pid " << pid;
Darin Petkov5a850472012-06-06 15:44:24 +020065 // TODO(petkov): Consider sending subsequent periodic signals and raising the
66 // signal to SIGKILL if the process keeps running.
Darin Petkov84a95532013-03-13 12:24:45 +010067 if (kill(pid, SIGTERM) == -1) {
68 PLOG(ERROR) << "SIGTERM failed";
69 }
Darin Petkov5a850472012-06-06 15:44:24 +020070}
71
72// static
73void ProcessKiller::OnProcessDied(GPid pid, gint status, gpointer data) {
74 LOG(INFO) << "pid " << pid << " died, status " << status;
75 ProcessKiller *me = reinterpret_cast<ProcessKiller *>(data);
76 map<int, Closure>::iterator callback_it = me->callbacks_.find(pid);
77 if (callback_it == me->callbacks_.end()) {
78 return;
79 }
80 const Closure &callback = callback_it->second;
81 if (!callback.is_null()) {
82 LOG(INFO) << "Running callback for dead pid " << pid;
83 callback.Run();
84 }
85 me->callbacks_.erase(callback_it);
86}
87
88} // namespace shill