blob: b3fdba68d2f3a959656914d54878feb351dbe1cc [file] [log] [blame]
Darin Petkov9c0baf82010-10-07 13:44:48 -07001// Copyright (c) 2010 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 CHROMEOS_PLATFORM_UPDATE_ENGINE_TERMINATOR_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_TERMINATOR_H__
7
8#include <signal.h>
9
Darin Petkov80f19562010-11-19 12:00:15 -080010#include <gtest/gtest_prod.h> // for FRIEND_TEST
11
Darin Petkov9c0baf82010-10-07 13:44:48 -070012namespace chromeos_update_engine {
13
14// A class allowing graceful delayed exit.
15class Terminator {
16 public:
17 // Initializes the terminator and sets up signal handlers.
18 static void Init();
19
20 // Terminates the current process.
21 static void Exit();
22
23 // Set to true if the terminator should block termination requests in an
24 // attempt to block exiting.
25 static void set_exit_blocked(bool block) { exit_blocked_ = block ? 1 : 0; }
Darin Petkov80f19562010-11-19 12:00:15 -080026 static bool exit_blocked() { return exit_blocked_ != 0; }
Darin Petkov9c0baf82010-10-07 13:44:48 -070027
28 // Returns true if the system is trying to terminate the process, false
29 // otherwise. Returns true only if exit was blocked when the termination
30 // request arrived.
31 static bool exit_requested() { return exit_requested_ != 0; }
32
33 private:
Darin Petkov80f19562010-11-19 12:00:15 -080034 FRIEND_TEST(TerminatorTest, HandleSignalTest);
35 FRIEND_TEST(TerminatorDeathTest, ScopedTerminatorExitUnblockerExitTest);
36
Darin Petkov9c0baf82010-10-07 13:44:48 -070037 // The signal handler.
38 static void HandleSignal(int signum);
39
40 static volatile sig_atomic_t exit_blocked_;
41 static volatile sig_atomic_t exit_requested_;
42};
43
44class ScopedTerminatorExitUnblocker {
45 public:
46 ~ScopedTerminatorExitUnblocker();
47};
48
49} // namespace chromeos_update_engine
50
51#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_TERMINATOR_H__