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 // 16 17 #ifndef UPDATE_ENGINE_COMMON_TERMINATOR_H_ 18 #define UPDATE_ENGINE_COMMON_TERMINATOR_H_ 19 20 #include <signal.h> 21 22 #include <gtest/gtest_prod.h> // for FRIEND_TEST 23 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(); 31 static void Init(int exit_status); 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. set_exit_blocked(bool block)38 static void set_exit_blocked(bool block) { exit_blocked_ = block ? 1 : 0; } exit_blocked()39 static bool exit_blocked() { return exit_blocked_ != 0; } 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. exit_requested()44 static bool exit_requested() { return exit_requested_ != 0; } 45 46 private: 47 FRIEND_TEST(TerminatorTest, HandleSignalTest); 48 FRIEND_TEST(TerminatorDeathTest, ScopedTerminatorExitUnblockerExitTest); 49 50 // The signal handler. 51 static void HandleSignal(int signum); 52 53 static volatile sig_atomic_t exit_status_; 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 65 #endif // UPDATE_ENGINE_COMMON_TERMINATOR_H_ 66