]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/signal.cc
update ceph source to reef 18.2.0
[ceph.git] / ceph / src / common / signal.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2011 New Dream Network
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
f67539c2
TL
15#include <cstdlib>
16#include <sstream>
17
18#include <sys/stat.h>
19#include <sys/types.h>
20
21#include <signal.h>
22
7c673cae 23#include "common/BackTrace.h"
f67539c2 24#include "common/config.h"
7c673cae
FG
25#include "common/debug.h"
26#include "common/signal.h"
f67539c2 27#include "common/perf_counters.h"
7c673cae 28
f67539c2 29#include "global/pidfile.h"
7c673cae 30
f67539c2
TL
31using namespace std::literals;
32
33#ifndef _WIN32
7c673cae
FG
34std::string signal_mask_to_str()
35{
36 sigset_t old_sigset;
37 if (pthread_sigmask(SIG_SETMASK, NULL, &old_sigset)) {
38 return "(pthread_signmask failed)";
39 }
40
f67539c2 41 std::ostringstream oss;
7c673cae 42 oss << "show_signal_mask: { ";
f67539c2 43 auto sep = ""s;
7c673cae
FG
44 for (int signum = 0; signum < NSIG; ++signum) {
45 if (sigismember(&old_sigset, signum) == 1) {
46 oss << sep << signum;
47 sep = ", ";
48 }
49 }
50 oss << " }";
51 return oss.str();
52}
53
54/* Block the signals in 'siglist'. If siglist == NULL, block all signals. */
55void block_signals(const int *siglist, sigset_t *old_sigset)
56{
57 sigset_t sigset;
58 if (!siglist) {
59 sigfillset(&sigset);
60 }
61 else {
62 int i = 0;
63 sigemptyset(&sigset);
64 while (siglist[i]) {
65 sigaddset(&sigset, siglist[i]);
66 ++i;
67 }
68 }
69 int ret = pthread_sigmask(SIG_BLOCK, &sigset, old_sigset);
11fdf7f2 70 ceph_assert(ret == 0);
7c673cae
FG
71}
72
73void restore_sigset(const sigset_t *old_sigset)
74{
75 int ret = pthread_sigmask(SIG_SETMASK, old_sigset, NULL);
11fdf7f2 76 ceph_assert(ret == 0);
7c673cae
FG
77}
78
79void unblock_all_signals(sigset_t *old_sigset)
80{
81 sigset_t sigset;
82 sigfillset(&sigset);
83 sigdelset(&sigset, SIGKILL);
84 int ret = pthread_sigmask(SIG_UNBLOCK, &sigset, old_sigset);
11fdf7f2 85 ceph_assert(ret == 0);
7c673cae 86}
f67539c2
TL
87#else
88std::string signal_mask_to_str()
89{
90 return "(unsupported signal)";
91}
92
93// Windows provides limited signal functionality.
94void block_signals(const int *siglist, sigset_t *old_sigset) {}
95void restore_sigset(const sigset_t *old_sigset) {}
96void unblock_all_signals(sigset_t *old_sigset) {}
97#endif /* _WIN32 */