signal.cpp 2.6 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Longda on 2010
//

#include "common/os/signal.h"
#include "common/log/log.h"
#include "pthread.h"
namespace common {

20 21
void setSignalHandler(int sig, sighandler_t func)
{
羽飞's avatar
羽飞 已提交
22 23 24 25 26 27
  struct sigaction newsa, oldsa;
  sigemptyset(&newsa.sa_mask);
  newsa.sa_flags = 0;
  newsa.sa_handler = func;
  int rc = sigaction(sig, &newsa, &oldsa);
  if (rc) {
28
    std::cerr << "Failed to set signal " << sig << SYS_OUTPUT_FILE_POS << SYS_OUTPUT_ERROR << std::endl;
羽飞's avatar
羽飞 已提交
29 30 31 32 33 34
  }
}

/*
** Set Singal handling Fucntion
*/
35 36
void setSignalHandler(sighandler_t func)
{
羽飞's avatar
羽飞 已提交
37 38 39 40
  setSignalHandler(SIGQUIT, func);
  setSignalHandler(SIGINT, func);
  setSignalHandler(SIGHUP, func);
  setSignalHandler(SIGTERM, func);
羽飞's avatar
羽飞 已提交
41
  signal(SIGPIPE, SIG_IGN);
羽飞's avatar
羽飞 已提交
42 43
}

44 45
void blockDefaultSignals(sigset_t *signal_set, sigset_t *old_set)
{
羽飞's avatar
羽飞 已提交
46 47 48 49 50 51 52 53 54 55
  sigemptyset(signal_set);
#ifndef DEBUG
  // SIGINT will effect our gdb debugging
  sigaddset(signal_set, SIGINT);
#endif
  sigaddset(signal_set, SIGTERM);
  sigaddset(signal_set, SIGUSR1);
  pthread_sigmask(SIG_BLOCK, signal_set, old_set);
}

56 57
void unBlockDefaultSignals(sigset_t *signal_set, sigset_t *old_set)
{
羽飞's avatar
羽飞 已提交
58 59 60 61 62 63 64 65 66
  sigemptyset(signal_set);
#ifndef DEBUG
  sigaddset(signal_set, SIGINT);
#endif
  sigaddset(signal_set, SIGTERM);
  sigaddset(signal_set, SIGUSR1);
  pthread_sigmask(SIG_UNBLOCK, signal_set, old_set);
}

67 68
void *waitForSignals(void *args)
{
羽飞's avatar
羽飞 已提交
69 70 71 72 73 74 75
  LOG_INFO("Start thread to wait signals.");
  sigset_t *signal_set = (sigset_t *)args;
  int sig_number = -1;
  while (true) {
    errno = 0;
    int ret = sigwait(signal_set, &sig_number);
    LOG_INFO("sigwait return value: %d, %d \n", ret, sig_number);
76
    if (ret != 0) {
羽飞's avatar
羽飞 已提交
77
      LOG_ERROR("error (%d) %s\n", errno, strerror(errno));
羽飞's avatar
羽飞 已提交
78 79 80 81 82
    }
  }
  return NULL;
}

83 84
void startWaitForSignals(sigset_t *signal_set)
{
羽飞's avatar
羽飞 已提交
85 86 87 88 89 90 91 92 93
  pthread_t pThread;
  pthread_attr_t pThreadAttrs;

  // create all threads as detached.  We will not try to join them.
  pthread_attr_init(&pThreadAttrs);
  pthread_attr_setdetachstate(&pThreadAttrs, PTHREAD_CREATE_DETACHED);

  pthread_create(&pThread, &pThreadAttrs, waitForSignals, (void *)signal_set);
}
羽飞's avatar
羽飞 已提交
94
}  // namespace common