Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 1 | // 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 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(); |
| 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 Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 26 | static bool exit_blocked() { return exit_blocked_ != 0; } |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 27 | |
| 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 Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 34 | FRIEND_TEST(TerminatorTest, HandleSignalTest); |
| 35 | FRIEND_TEST(TerminatorDeathTest, ScopedTerminatorExitUnblockerExitTest); |
| 36 | |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 37 | // 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 | |
| 44 | class ScopedTerminatorExitUnblocker { |
| 45 | public: |
| 46 | ~ScopedTerminatorExitUnblocker(); |
| 47 | }; |
| 48 | |
| 49 | } // namespace chromeos_update_engine |
| 50 | |
| 51 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_TERMINATOR_H__ |