blob: 20616f6deb4db1b7c7ea3a97de863f20eed7ed05 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Petkov9c0baf82010-10-07 13:44:48 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_COMMON_TERMINATOR_H_
18#define UPDATE_ENGINE_COMMON_TERMINATOR_H_
Darin Petkov9c0baf82010-10-07 13:44:48 -070019
20#include <signal.h>
21
Darin Petkov80f19562010-11-19 12:00:15 -080022#include <gtest/gtest_prod.h> // for FRIEND_TEST
23
Darin Petkov9c0baf82010-10-07 13:44:48 -070024namespace chromeos_update_engine {
25
26// A class allowing graceful delayed exit.
27class Terminator {
28 public:
29 // Initializes the terminator and sets up signal handlers.
30 static void Init();
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070031 static void Init(int exit_status);
Darin Petkov9c0baf82010-10-07 13:44:48 -070032
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 Petkov80f19562010-11-19 12:00:15 -080039 static bool exit_blocked() { return exit_blocked_ != 0; }
Darin Petkov9c0baf82010-10-07 13:44:48 -070040
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 Petkov80f19562010-11-19 12:00:15 -080047 FRIEND_TEST(TerminatorTest, HandleSignalTest);
48 FRIEND_TEST(TerminatorDeathTest, ScopedTerminatorExitUnblockerExitTest);
49
Darin Petkov9c0baf82010-10-07 13:44:48 -070050 // The signal handler.
51 static void HandleSignal(int signum);
52
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070053 static volatile sig_atomic_t exit_status_;
Darin Petkov9c0baf82010-10-07 13:44:48 -070054 static volatile sig_atomic_t exit_blocked_;
55 static volatile sig_atomic_t exit_requested_;
56};
57
58class ScopedTerminatorExitUnblocker {
59 public:
60 ~ScopedTerminatorExitUnblocker();
61};
62
63} // namespace chromeos_update_engine
64
Alex Deymo39910dc2015-11-09 17:04:30 -080065#endif // UPDATE_ENGINE_COMMON_TERMINATOR_H_