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 | #include "update_engine/common/terminator.h" |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 18 | |
| 19 | #include <cstdlib> |
| 20 | |
| 21 | namespace chromeos_update_engine { |
| 22 | |
Gilad Arnold | 0b4a6ff | 2012-04-30 13:13:03 -0700 | [diff] [blame] | 23 | volatile sig_atomic_t Terminator::exit_status_ = 1; // default exit status |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 24 | volatile sig_atomic_t Terminator::exit_blocked_ = 0; |
| 25 | volatile sig_atomic_t Terminator::exit_requested_ = 0; |
| 26 | |
| 27 | void Terminator::Init() { |
Darin Petkov | 80f1956 | 2010-11-19 12:00:15 -0800 | [diff] [blame] | 28 | exit_blocked_ = 0; |
| 29 | exit_requested_ = 0; |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 30 | signal(SIGTERM, HandleSignal); |
| 31 | } |
| 32 | |
Gilad Arnold | 0b4a6ff | 2012-04-30 13:13:03 -0700 | [diff] [blame] | 33 | void Terminator::Init(int exit_status) { |
| 34 | exit_status_ = exit_status; |
| 35 | Init(); |
| 36 | } |
| 37 | |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 38 | void Terminator::Exit() { |
Gilad Arnold | 0b4a6ff | 2012-04-30 13:13:03 -0700 | [diff] [blame] | 39 | exit(exit_status_); |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void Terminator::HandleSignal(int signum) { |
| 43 | if (exit_blocked_ == 0) { |
| 44 | Exit(); |
| 45 | } |
| 46 | exit_requested_ = 1; |
| 47 | } |
| 48 | |
| 49 | ScopedTerminatorExitUnblocker::~ScopedTerminatorExitUnblocker() { |
| 50 | Terminator::set_exit_blocked(false); |
| 51 | if (Terminator::exit_requested()) { |
| 52 | Terminator::Exit(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | } // namespace chromeos_update_engine |