github.com/searKing/golang/go@v1.2.74/os/signal/cgo/signal_handler_linux.hpp (about)

     1  // Copyright (c) 2019 The searKing authors. All Rights Reserved.
     2  //
     3  // Use of this source code is governed by a MIT-style license
     4  // that can be found in the LICENSE file in the root of the source
     5  // tree. An additional intellectual property rights grant can be found
     6  // in the file PATENTS.  All contributing project authors may
     7  // be found in the AUTHORS file in the root of the source tree.
     8  
     9  // +build cgo
    10  // +build linux
    11  
    12  #ifndef GO_OS_SIGNAL_CGO_SIGNAL_HANDLER_LINUX_HPP_
    13  #define GO_OS_SIGNAL_CGO_SIGNAL_HANDLER_LINUX_HPP_
    14  
    15  #include <unistd.h>
    16  // You can find out the version with _POSIX_VERSION.
    17  // POSIX compliant
    18  
    19  #include <csignal>
    20  #include <functional>
    21  #include <map>
    22  #include <memory>
    23  #include <utility>
    24  
    25  #include "base_signal_handler.hpp"
    26  
    27  namespace searking {
    28  
    29  // Callbacks Predefinations
    30  
    31  typedef void (*SignalHandlerSigActionHandler)(int signum, siginfo_t *info,
    32                                                void *context);
    33  typedef void (*SignalHandlerSignalHandler)(int signum);
    34  typedef void (*SignalHandlerOnSignal)(void *ctx, int fd, int signum,
    35                                        siginfo_t *info, void *context);
    36  
    37  class SignalHandler : public BaseSignalHandler<SignalHandler> {
    38   protected:
    39    SignalHandler() : on_signal_ctx_(nullptr), on_signal_(nullptr) {}
    40  
    41    void SetGoRegisteredSignalHandlersIfEmpty(
    42        int signum, SignalHandlerSigActionHandler action,
    43        SignalHandlerSignalHandler handler);
    44    void DoSignalChan(int signum, siginfo_t *info, void *context);
    45    void InvokeGoSignalHandler(int signum, siginfo_t *info, void *context);
    46  
    47   public:
    48    // Thread safe GetInstance.
    49    static SignalHandler &GetInstance();
    50  
    51    void operator()(int signum, siginfo_t *info, void *context);
    52    // never invoke a go function, see
    53    // https://github.com/golang/go/issues/35814
    54    void RegisterOnSignal(std::function<void(void *ctx, int fd, int signum,
    55                                             siginfo_t *info, void *context)>
    56                              callback,
    57                          void *ctx);
    58  
    59    static int SetSig(int signum);
    60    static int SetSig(int signum, SignalHandlerSigActionHandler action,
    61                      SignalHandlerSignalHandler handler);
    62  
    63   private:
    64    void *on_signal_ctx_;
    65    std::function<void(void *ctx, int fd, int signum, siginfo_t *info,
    66                       void *context)>
    67        on_signal_;
    68    std::map<int, std::pair<SignalHandlerSigActionHandler,
    69                            SignalHandlerSignalHandler> >
    70        go_registered_handlers_;
    71  };
    72  }  // namespace searking
    73  #endif