Add helper utils for signal fd 25/43725/1
authorAleksander Zdyb <a.zdyb@samsung.com>
Fri, 22 May 2015 09:10:08 +0000 (11:10 +0200)
committerAleksander Zdyb <a.zdyb@samsung.com>
Fri, 10 Jul 2015 12:50:27 +0000 (14:50 +0200)
Change-Id: If2d5cd14ff3a66d3770d5282c8a88d113e8a3448

src/Utils/SignalFd.cpp [new file with mode: 0644]
src/Utils/SignalFd.h [new file with mode: 0644]

diff --git a/src/Utils/SignalFd.cpp b/src/Utils/SignalFd.cpp
new file mode 100644 (file)
index 0000000..c24caba
--- /dev/null
@@ -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 <a.zdyb@samsung.com>
+ * @version     1.0
+ */
+
+#include <cerrno>
+#include <csignal>
+#include <string>
+#include <sys/signalfd.h>
+#include <unistd.h>
+
+#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<int> &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<int> &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 (file)
index 0000000..9e14bca
--- /dev/null
@@ -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 <a.zdyb@samsung.com>
+ * @version     1.0
+ */
+
+#ifndef SRC_UTILS_SIGNALFD_H
+#define SRC_UTILS_SIGNALFD_H
+
+#include <cstdint>
+#include <set>
+
+#include <Utils/WithMessageException.h>
+
+namespace Utils {
+
+namespace SignalFd {
+
+class ErrorException : public WithMessageException {
+public:
+    ErrorException(const std::string &message) : WithMessageException("Signal Fd: " + message) {}
+};
+
+int createSignalFd(const std::set<int> &signals);
+uint32_t readSignalNo(int fd);
+
+}
+
+} /* namespace Utils */
+
+#endif /* SRC_UTILS_SIGNALFD_H */