blob: f8a261e62db54d68557bd48d41aade5514cbfbc0 [file] [log] [blame]
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov9c0baf82010-10-07 13:44:48 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Gilad Arnoldcf175a02014-07-10 16:48:47 -07005#ifndef UPDATE_ENGINE_TERMINATOR_H_
6#define UPDATE_ENGINE_TERMINATOR_H_
Darin Petkov9c0baf82010-10-07 13:44:48 -07007
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();
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070019 static void Init(int exit_status);
Darin Petkov9c0baf82010-10-07 13:44:48 -070020
21 // Terminates the current process.
22 static void Exit();
23
24 // Set to true if the terminator should block termination requests in an
25 // attempt to block exiting.
26 static void set_exit_blocked(bool block) { exit_blocked_ = block ? 1 : 0; }
Darin Petkov80f19562010-11-19 12:00:15 -080027 static bool exit_blocked() { return exit_blocked_ != 0; }
Darin Petkov9c0baf82010-10-07 13:44:48 -070028
29 // Returns true if the system is trying to terminate the process, false
30 // otherwise. Returns true only if exit was blocked when the termination
31 // request arrived.
32 static bool exit_requested() { return exit_requested_ != 0; }
33
34 private:
Darin Petkov80f19562010-11-19 12:00:15 -080035 FRIEND_TEST(TerminatorTest, HandleSignalTest);
36 FRIEND_TEST(TerminatorDeathTest, ScopedTerminatorExitUnblockerExitTest);
37
Darin Petkov9c0baf82010-10-07 13:44:48 -070038 // The signal handler.
39 static void HandleSignal(int signum);
40
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070041 static volatile sig_atomic_t exit_status_;
Darin Petkov9c0baf82010-10-07 13:44:48 -070042 static volatile sig_atomic_t exit_blocked_;
43 static volatile sig_atomic_t exit_requested_;
44};
45
46class ScopedTerminatorExitUnblocker {
47 public:
48 ~ScopedTerminatorExitUnblocker();
49};
50
51} // namespace chromeos_update_engine
52
Gilad Arnoldcf175a02014-07-10 16:48:47 -070053#endif // UPDATE_ENGINE_TERMINATOR_H_