blob: fb5cab24a642ec4a7814c193c57a4a0331330a57 [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
26 // Terminates process |pid|. Uses GLib to wait asynchronously for the process
27 // to exit. GLib supports only a single callback per process ID so there
28 // should be no other child watch callbacks registered for this |pid|. If
29 // |callback| is non-null, runs it when the process exits.
30 virtual void Kill(int pid, const base::Closure &callback);
31
32 protected:
33 ProcessKiller();
34
35 private:
36 friend struct base::DefaultLazyInstanceTraits<ProcessKiller>;
37 FRIEND_TEST(ProcessKillerTest, OnProcessDied);
38
39 static void OnProcessDied(GPid pid, gint status, gpointer data);
40
41 std::map<int, base::Closure> callbacks_;
42
43 DISALLOW_COPY_AND_ASSIGN(ProcessKiller);
44};
45
46} // namespace shill
47
48#endif // SHILL_PROCESS_KILLER_H_