Gilad Arnold | 0b4a6ff | 2012-04-30 13:13:03 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 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 Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 10 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 11 | |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 12 | namespace chromeos_update_engine { |
| 13 | |
| 14 | // A class allowing graceful delayed exit. |
| 15 | class Terminator { |
| 16 | public: |
| 17 | // Initializes the terminator and sets up signal handlers. |
| 18 | static void Init(); |
Gilad Arnold | 0b4a6ff | 2012-04-30 13:13:03 -0700 | [diff] [blame] | 19 | static void Init(int exit_status); |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 20 | |
| 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 Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 27 | static bool exit_blocked() { return exit_blocked_ != 0; } |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 28 | |
| 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 Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 35 | FRIEND_TEST(TerminatorTest, HandleSignalTest); |
| 36 | FRIEND_TEST(TerminatorDeathTest, ScopedTerminatorExitUnblockerExitTest); |
| 37 | |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 38 | // The signal handler. |
| 39 | static void HandleSignal(int signum); |
| 40 | |
Gilad Arnold | 0b4a6ff | 2012-04-30 13:13:03 -0700 | [diff] [blame] | 41 | static volatile sig_atomic_t exit_status_; |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 42 | static volatile sig_atomic_t exit_blocked_; |
| 43 | static volatile sig_atomic_t exit_requested_; |
| 44 | }; |
| 45 | |
| 46 | class ScopedTerminatorExitUnblocker { |
| 47 | public: |
| 48 | ~ScopedTerminatorExitUnblocker(); |
| 49 | }; |
| 50 | |
| 51 | } // namespace chromeos_update_engine |
| 52 | |
| 53 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_TERMINATOR_H__ |