Home | History | Annotate | Download | only in common
      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 #include "update_engine/common/terminator.h"
     18 
     19 #include <cstdlib>
     20 
     21 namespace chromeos_update_engine {
     22 
     23 volatile sig_atomic_t Terminator::exit_status_ = 1;  // default exit status
     24 volatile sig_atomic_t Terminator::exit_blocked_ = 0;
     25 volatile sig_atomic_t Terminator::exit_requested_ = 0;
     26 
     27 void Terminator::Init() {
     28   exit_blocked_ = 0;
     29   exit_requested_ = 0;
     30   signal(SIGTERM, HandleSignal);
     31 }
     32 
     33 void Terminator::Init(int exit_status) {
     34   exit_status_ = exit_status;
     35   Init();
     36 }
     37 
     38 void Terminator::Exit() {
     39   exit(exit_status_);
     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
     57