| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2012 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 16 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_COMMON_TERMINATOR_H_ | 
|  | 18 | #define UPDATE_ENGINE_COMMON_TERMINATOR_H_ | 
| Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 19 |  | 
|  | 20 | #include <signal.h> | 
|  | 21 |  | 
| Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 22 | #include <gtest/gtest_prod.h>  // for FRIEND_TEST | 
|  | 23 |  | 
| Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 24 | namespace chromeos_update_engine { | 
|  | 25 |  | 
|  | 26 | // A class allowing graceful delayed exit. | 
|  | 27 | class Terminator { | 
|  | 28 | public: | 
|  | 29 | // Initializes the terminator and sets up signal handlers. | 
|  | 30 | static void Init(); | 
| Gilad Arnold | 0b4a6ff | 2012-04-30 13:13:03 -0700 | [diff] [blame] | 31 | static void Init(int exit_status); | 
| Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 32 |  | 
|  | 33 | // Terminates the current process. | 
|  | 34 | static void Exit(); | 
|  | 35 |  | 
|  | 36 | // Set to true if the terminator should block termination requests in an | 
|  | 37 | // attempt to block exiting. | 
|  | 38 | static void set_exit_blocked(bool block) { exit_blocked_ = block ? 1 : 0; } | 
| Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 39 | static bool exit_blocked() { return exit_blocked_ != 0; } | 
| Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 40 |  | 
|  | 41 | // Returns true if the system is trying to terminate the process, false | 
|  | 42 | // otherwise. Returns true only if exit was blocked when the termination | 
|  | 43 | // request arrived. | 
|  | 44 | static bool exit_requested() { return exit_requested_ != 0; } | 
|  | 45 |  | 
|  | 46 | private: | 
| Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 47 | FRIEND_TEST(TerminatorTest, HandleSignalTest); | 
|  | 48 | FRIEND_TEST(TerminatorDeathTest, ScopedTerminatorExitUnblockerExitTest); | 
|  | 49 |  | 
| Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 50 | // The signal handler. | 
|  | 51 | static void HandleSignal(int signum); | 
|  | 52 |  | 
| Gilad Arnold | 0b4a6ff | 2012-04-30 13:13:03 -0700 | [diff] [blame] | 53 | static volatile sig_atomic_t exit_status_; | 
| Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 54 | static volatile sig_atomic_t exit_blocked_; | 
|  | 55 | static volatile sig_atomic_t exit_requested_; | 
|  | 56 | }; | 
|  | 57 |  | 
|  | 58 | class ScopedTerminatorExitUnblocker { | 
|  | 59 | public: | 
|  | 60 | ~ScopedTerminatorExitUnblocker(); | 
|  | 61 | }; | 
|  | 62 |  | 
|  | 63 | }  // namespace chromeos_update_engine | 
|  | 64 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 65 | #endif  // UPDATE_ENGINE_COMMON_TERMINATOR_H_ |