Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 1 | // 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 Petkov | 84a9553 | 2013-03-13 12:24:45 +0100 | [diff] [blame] | 7 | #include <errno.h> |
| 8 | #include <sys/wait.h> |
| 9 | |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 10 | using base::Closure; |
| 11 | using std::map; |
| 12 | |
| 13 | namespace shill { |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | base::LazyInstance<ProcessKiller> g_process_killer = LAZY_INSTANCE_INITIALIZER; |
| 18 | |
| 19 | } // namespace |
| 20 | |
| 21 | ProcessKiller::ProcessKiller() {} |
| 22 | |
| 23 | ProcessKiller::~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 |
| 30 | ProcessKiller *ProcessKiller::GetInstance() { |
| 31 | return g_process_killer.Pointer(); |
| 32 | } |
| 33 | |
Darin Petkov | 84a9553 | 2013-03-13 12:24:45 +0100 | [diff] [blame] | 34 | bool ProcessKiller::Wait(int pid, const Closure &callback) { |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 35 | LOG(INFO) << "Waiting for pid " << pid; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 36 | if (!callback.is_null()) { |
| 37 | callbacks_[pid] = callback; |
| 38 | } |
Darin Petkov | 84a9553 | 2013-03-13 12:24:45 +0100 | [diff] [blame] | 39 | // 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 Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 55 | g_child_watch_add(pid, OnProcessDied, this); |
Darin Petkov | 84a9553 | 2013-03-13 12:24:45 +0100 | [diff] [blame] | 56 | return true; |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void ProcessKiller::Kill(int pid, const Closure &callback) { |
Darin Petkov | 84a9553 | 2013-03-13 12:24:45 +0100 | [diff] [blame] | 60 | if (!Wait(pid, callback)) { |
| 61 | LOG(INFO) << "Process already dead, no need to kill."; |
| 62 | return; |
| 63 | } |
Darin Petkov | 3806676 | 2012-12-17 15:35:45 +0100 | [diff] [blame] | 64 | LOG(INFO) << "Killing pid " << pid; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 65 | // TODO(petkov): Consider sending subsequent periodic signals and raising the |
| 66 | // signal to SIGKILL if the process keeps running. |
Darin Petkov | 84a9553 | 2013-03-13 12:24:45 +0100 | [diff] [blame] | 67 | if (kill(pid, SIGTERM) == -1) { |
| 68 | PLOG(ERROR) << "SIGTERM failed"; |
| 69 | } |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // static |
| 73 | void 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 |