2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Jan Olszak <j.olszak@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
21 * @author Jan Olszak (j.olszak@samsung.com)
22 * @brief Signal related functions
25 #include "utils/signal.hpp"
26 #include "utils/exception.hpp"
27 #include "logger/logger.hpp"
38 void setSignalMask(int how, const ::sigset_t& set)
40 int ret = ::pthread_sigmask(how, &set, nullptr /*&oldSet*/);
42 const std::string msg = "Error in pthread_sigmask: " + getSystemErrorMessage(ret);
44 throw UtilsException(msg);
48 void changeSignal(int how, const int sigNum) {
50 if(-1 == ::sigemptyset(&set)) {
51 const std::string msg = "Error in sigfillset: " + getSystemErrorMessage();
53 throw UtilsException(msg);
56 if(-1 ==::sigaddset(&set, sigNum)) {
57 const std::string msg = "Error in sigdelset: " + getSystemErrorMessage();
59 throw UtilsException(msg);
62 setSignalMask(how, set);
67 ::sigset_t getSignalMask()
70 int ret = ::pthread_sigmask(0 /*ignored*/, nullptr /*get the oldset*/, &set);
72 const std::string msg = "Error in pthread_sigmask: " + getSystemErrorMessage(ret);
74 throw UtilsException(msg);
79 bool isSignalBlocked(const int sigNum)
81 ::sigset_t set = getSignalMask();
83 int ret = ::sigismember(&set, sigNum);
85 const std::string msg = "Error in sigismember: " + getSystemErrorMessage();
87 throw UtilsException(msg);
93 void signalBlock(const int sigNum)
95 changeSignal(SIG_BLOCK, sigNum);
98 void signalBlockAllExcept(const std::initializer_list<int>& signals)
101 if(-1 == ::sigfillset(&set)) {
102 const std::string msg = "Error in sigfillset: " + getSystemErrorMessage();
104 throw UtilsException(msg);
107 for(const int s: signals) {
108 if(-1 == ::sigaddset(&set, s)) {
109 const std::string msg = "Error in sigaddset: " + getSystemErrorMessage();
111 throw UtilsException(msg);
114 setSignalMask(SIG_BLOCK, set);
117 void signalUnblock(const int sigNum)
119 changeSignal(SIG_UNBLOCK, sigNum);
122 void signalIgnore(const std::initializer_list<int>& signals)
124 struct ::sigaction act;
125 act.sa_handler = SIG_IGN;
127 for(const int s: signals) {
128 if(-1 == ::sigaction(s, &act, nullptr)) {
129 const std::string msg = "Error in sigaction: " + getSystemErrorMessage();
131 throw UtilsException(msg);
136 void sendSignal(const pid_t pid, const int sigNum)
138 if (-1 == ::kill(pid, sigNum)) {
139 const std::string msg = "Error during killing pid: " + std::to_string(pid) +
140 " sigNum: " + std::to_string(sigNum) +
141 ": " + getSystemErrorMessage();
143 throw UtilsException(msg);