blob: 30cb58064f366065fefdc2d05563a8fe0ce6c872 [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#ifndef SHILL_PROCESS_KILLER_H_
6#define SHILL_PROCESS_KILLER_H_
7
8#include <map>
9
10#include <base/callback.h>
11#include <base/lazy_instance.h>
12#include <glib.h>
13#include <gtest/gtest_prod.h> // for FRIEND_TEST
14
15namespace shill {
16
17// The ProcessKiller singleton can be used to asynchronously and robustly
18// terminate and reap child processes by their process IDs.
19class ProcessKiller {
20 public:
21 virtual ~ProcessKiller();
22
23 // This is a singleton -- use ProcessKiller::GetInstance()->Foo()
24 static ProcessKiller *GetInstance();
25
Darin Petkov38066762012-12-17 15:35:45 +010026 // Uses GLib to wait asynchronously for the process to exit and reap it. GLib
27 // supports only a single callback per process ID so there should be no other
28 // child watch callbacks registered for this |pid|. If |callback| is non-null,
29 // runs it when the process exits.
30 virtual void Wait(int pid, const base::Closure &callback);
31
32 // Terminates process |pid| and reaps it through Wait(pid, callback).
Darin Petkov5a850472012-06-06 15:44:24 +020033 virtual void Kill(int pid, const base::Closure &callback);
34
35 protected:
36 ProcessKiller();
37
38 private:
39 friend struct base::DefaultLazyInstanceTraits<ProcessKiller>;
40 FRIEND_TEST(ProcessKillerTest, OnProcessDied);
41
42 static void OnProcessDied(GPid pid, gint status, gpointer data);
43
44 std::map<int, base::Closure> callbacks_;
45
46 DISALLOW_COPY_AND_ASSIGN(ProcessKiller);
47};
48
49} // namespace shill
50
51#endif // SHILL_PROCESS_KILLER_H_