xref: /aosp_15_r20/tools/repohooks/rh/signals.py (revision d68f33bc6fb0cc2476107c2af0573a2f5a63dfc1)
1*d68f33bcSAndroid Build Coastguard Worker# Copyright 2016 The Android Open Source Project
2*d68f33bcSAndroid Build Coastguard Worker#
3*d68f33bcSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*d68f33bcSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*d68f33bcSAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*d68f33bcSAndroid Build Coastguard Worker#
7*d68f33bcSAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
8*d68f33bcSAndroid Build Coastguard Worker#
9*d68f33bcSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*d68f33bcSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*d68f33bcSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*d68f33bcSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*d68f33bcSAndroid Build Coastguard Worker# limitations under the License.
14*d68f33bcSAndroid Build Coastguard Worker
15*d68f33bcSAndroid Build Coastguard Worker"""Signal related functionality."""
16*d68f33bcSAndroid Build Coastguard Worker
17*d68f33bcSAndroid Build Coastguard Workerimport os
18*d68f33bcSAndroid Build Coastguard Workerimport signal
19*d68f33bcSAndroid Build Coastguard Workerimport sys
20*d68f33bcSAndroid Build Coastguard Worker
21*d68f33bcSAndroid Build Coastguard Worker_path = os.path.realpath(__file__ + '/../..')
22*d68f33bcSAndroid Build Coastguard Workerif sys.path[0] != _path:
23*d68f33bcSAndroid Build Coastguard Worker    sys.path.insert(0, _path)
24*d68f33bcSAndroid Build Coastguard Workerdel _path
25*d68f33bcSAndroid Build Coastguard Worker
26*d68f33bcSAndroid Build Coastguard Worker
27*d68f33bcSAndroid Build Coastguard Workerdef relay_signal(handler, signum, frame):
28*d68f33bcSAndroid Build Coastguard Worker    """Notify a listener returned from getsignal of receipt of a signal.
29*d68f33bcSAndroid Build Coastguard Worker
30*d68f33bcSAndroid Build Coastguard Worker    Returns:
31*d68f33bcSAndroid Build Coastguard Worker      True if it was relayed to the target, False otherwise.
32*d68f33bcSAndroid Build Coastguard Worker      False in particular occurs if the target isn't relayable.
33*d68f33bcSAndroid Build Coastguard Worker    """
34*d68f33bcSAndroid Build Coastguard Worker    if handler in (None, signal.SIG_IGN):
35*d68f33bcSAndroid Build Coastguard Worker        return True
36*d68f33bcSAndroid Build Coastguard Worker    if handler == signal.SIG_DFL:
37*d68f33bcSAndroid Build Coastguard Worker        # This scenario is a fairly painful to handle fully, thus we just
38*d68f33bcSAndroid Build Coastguard Worker        # state we couldn't handle it and leave it to client code.
39*d68f33bcSAndroid Build Coastguard Worker        return False
40*d68f33bcSAndroid Build Coastguard Worker    handler(signum, frame)
41*d68f33bcSAndroid Build Coastguard Worker    return True
42