blob: 62adafd07f6299c9950ee0547ece0959a6c972e3 [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#include "update_engine/common/terminator.h"
Darin Petkov9c0baf82010-10-07 13:44:48 -070018
19#include <cstdlib>
20
21namespace chromeos_update_engine {
22
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070023volatile sig_atomic_t Terminator::exit_status_ = 1; // default exit status
Darin Petkov9c0baf82010-10-07 13:44:48 -070024volatile sig_atomic_t Terminator::exit_blocked_ = 0;
25volatile sig_atomic_t Terminator::exit_requested_ = 0;
26
27void Terminator::Init() {
Darin Petkov80f19562010-11-19 12:00:15 -080028 exit_blocked_ = 0;
29 exit_requested_ = 0;
Darin Petkov9c0baf82010-10-07 13:44:48 -070030 signal(SIGTERM, HandleSignal);
31}
32
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070033void Terminator::Init(int exit_status) {
34 exit_status_ = exit_status;
35 Init();
36}
37
Darin Petkov9c0baf82010-10-07 13:44:48 -070038void Terminator::Exit() {
Gilad Arnold0b4a6ff2012-04-30 13:13:03 -070039 exit(exit_status_);
Darin Petkov9c0baf82010-10-07 13:44:48 -070040}
41
42void Terminator::HandleSignal(int signum) {
43 if (exit_blocked_ == 0) {
44 Exit();
45 }
46 exit_requested_ = 1;
47}
48
49ScopedTerminatorExitUnblocker::~ScopedTerminatorExitUnblocker() {
50 Terminator::set_exit_blocked(false);
51 if (Terminator::exit_requested()) {
52 Terminator::Exit();
53 }
54}
55
56} // namespace chromeos_update_engine