From 0ac3348edb32204a207dd61b96a39b8053c8677b Mon Sep 17 00:00:00 2001 From: Aleksander Zdyb Date: Fri, 22 May 2015 11:10:08 +0200 Subject: [PATCH] Add helper utils for signal fd Change-Id: If2d5cd14ff3a66d3770d5282c8a88d113e8a3448 --- src/Utils/SignalFd.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Utils/SignalFd.h | 46 ++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 src/Utils/SignalFd.cpp create mode 100644 src/Utils/SignalFd.h diff --git a/src/Utils/SignalFd.cpp b/src/Utils/SignalFd.cpp new file mode 100644 index 0000000..c24caba --- /dev/null +++ b/src/Utils/SignalFd.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file src/Utils/SignalFd.cpp + * @author Aleksander Zdyb + * @version 1.0 + */ + +#include +#include +#include +#include +#include + +#include "SignalFd.h" + +namespace Utils { + +namespace SignalFd { + +namespace { + +void blockAllSignals() { + sigset_t sigset; + if (sigfillset(&sigset) == -1) { + throw ErrorException("Could not fill signal set"); + } + + if (sigprocmask(SIG_BLOCK, &sigset, nullptr) == -1) { + throw ErrorException("Could not block signals"); + } +} + +void fillSignalSet(sigset_t *sigset, const std::set &signals) { + if (sigemptyset(sigset) == -1) { + throw ErrorException("Could not empty signal set"); + } + + for (int sig : signals) { + if (sigaddset(sigset, sig) == -1) { + throw ErrorException("Could not add signal to set"); + } + } +} + +} /* namespace */ + +int createSignalFd(const std::set &signals) { + sigset_t signalsToCatch; + + blockAllSignals(); + fillSignalSet(&signalsToCatch, signals); + + auto sigFd = signalfd(-1, &signalsToCatch, 0); + if (sigFd == -1) { + throw ErrorException("Could not create signal fd"); + } + + return sigFd; +} + +uint32_t readSignalNo(int sigFd) { + struct signalfd_siginfo fdsi; + errno = 0; + const auto ret = TEMP_FAILURE_RETRY(read(sigFd, &fdsi, sizeof(struct signalfd_siginfo))); + if (ret != sizeof(struct signalfd_siginfo)) { + throw ErrorException("Could not read signal [errno=" + std::to_string(errno) + "]"); + } + return fdsi.ssi_signo; +} + +} /* namespace SignalFd */ + +} /* namespace Utils */ diff --git a/src/Utils/SignalFd.h b/src/Utils/SignalFd.h new file mode 100644 index 0000000..9e14bca --- /dev/null +++ b/src/Utils/SignalFd.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file src/Utils/SignalFd.h + * @author Aleksander Zdyb + * @version 1.0 + */ + +#ifndef SRC_UTILS_SIGNALFD_H +#define SRC_UTILS_SIGNALFD_H + +#include +#include + +#include + +namespace Utils { + +namespace SignalFd { + +class ErrorException : public WithMessageException { +public: + ErrorException(const std::string &message) : WithMessageException("Signal Fd: " + message) {} +}; + +int createSignalFd(const std::set &signals); +uint32_t readSignalNo(int fd); + +} + +} /* namespace Utils */ + +#endif /* SRC_UTILS_SIGNALFD_H */ -- 2.7.4