Move SocketManager's exception to exception header
authorYonggoo Kang <ygace.kang@samsung.com>
Tue, 23 Apr 2024 03:39:22 +0000 (12:39 +0900)
committerDongsun Lee <ds73.lee@samsung.com>
Wed, 24 Apr 2024 07:35:08 +0000 (16:35 +0900)
Change-Id: I01cd901b9dfe94b0f72f119dc2a848bf56f97450

srcs/common/exception.h
srcs/common/utils.h
srcs/server/socket-manager.cpp
srcs/server/socket-manager.h

index 11f0cfdce499300084eff0b164a15615ab984b18..5bd46cf09caccd8b0ade7b89a835c8bcfbf18271 100644 (file)
@@ -393,4 +393,10 @@ public:
     DECLARE_EXCEPTION_TYPE(Base, LockFailed)
 };
 
+class SocketManagerException {
+public:
+    DECLARE_EXCEPTION_TYPE(WA::Exception, Base)
+    DECLARE_EXCEPTION_TYPE(Base, InitFailed)
+};
+
 } // namespace WebAuthn
index d3f0aff8b043dfb62cbaf08a32fdf560774469b4..c9860fa5cef791971817c7d425e96c2043dd3781 100644 (file)
@@ -66,6 +66,10 @@ int try_catch(F &&f) {
         LogError("WA::FileLockerException::LockFailed: " << e.DumpToString());
         std::cerr << "WA::FileLockerException::LockFailed: " << e.DumpToString() << std::endl;
         return WAUTHN_ERROR_INVALID_STATE;
+    } catch (const SocketManagerException::InitFailed &e) {
+        LogError("WA::SocketManagerException::InitFailed: " << e.DumpToString());
+        std::cerr << "WA::SocketManagerException::InitFailed: " << e.DumpToString() << std::endl;
+        return WAUTHN_ERROR_INVALID_STATE;
     } catch (const std::logic_error &e) {
         LogError("Invalid logic: " << e.what());
         std::cerr << "Invalid logic: " << e.what() << std::endl;
@@ -74,15 +78,14 @@ int try_catch(F &&f) {
         LogError("Memory allocation failed: " << e.what());
         std::cerr << "Memory allocation failed: " << e.what() << std::endl;
         return WAUTHN_ERROR_MEMORY;
-    } catch (const Exception &e) {
-        LogError("WA::Exception " << e.DumpToString());
-        std::cerr << "WA::Exception " << e.DumpToString() << std::endl;
     } catch (const std::system_error &e) {
         LogError("STD system_error: " <<  e.code() << "-" << e.what());
         std::cerr << "STD system_error: " <<  e.code() << "-" << e.what() << std::endl;
+        return WAUTHN_ERROR_UNKNOWN;
     } catch (const std::exception &e) {
         LogError("STD exception " << e.what());
         std::cerr << "STD exception " << e.what() << std::endl;
+        return WAUTHN_ERROR_UNKNOWN;
     } catch (...) {
         LogError("Unknown exception occurred");
         std::cerr << "Unknown exception occurred" << std::endl;
index 6f4e2472ed82fb43c7b130b92e1b2fae8ae23fa1..941fb66d82e4c2b4eb04af5e2d925529d220dfc7 100644 (file)
@@ -36,7 +36,7 @@
 
 #include <webauthn-log.h>
 #include <errno_string.h>
-
+#include <exception.h>
 #include <service.h>
 #include <socket-manager.h>
 
@@ -75,7 +75,7 @@ SocketManager::SocketManager()
     FD_ZERO(&m_readSet);
 
     if ((m_notifyMe = eventfd(0, 0)) < 0)
-        ThrowErrno(Exception::InitFailed, "eventfd");
+        ThrowErrno(SocketManagerException::InitFailed, "eventfd");
     LogInfo("Eventfd desc: " << m_notifyMe);
     RegisterFdForReading(m_notifyMe);
 
@@ -85,11 +85,11 @@ SocketManager::SocketManager()
     sigaddset(&set, SIGTERM);
     sigaddset(&set, SIGCHLD);
     if (auto err = pthread_sigmask(SIG_BLOCK, &set, nullptr))
-        ThrowMsg(Exception::InitFailed, "Error in pthread_sigmask: " << err);
+        ThrowMsg(SocketManagerException::InitFailed, "Error in pthread_sigmask: " << err);
 
     // add support for TERM signal (passed from systemd)
     if ((m_signalFd = signalfd(-1, &set, 0)) < 0)
-        ThrowErrno(Exception::InitFailed, "signalfd");
+        ThrowErrno(SocketManagerException::InitFailed, "signalfd");
     RegisterFdForReading(m_signalFd);
 }
 
@@ -281,7 +281,7 @@ int SocketManager::GetSocketFromSystemD(const ServiceDescription &desc)
 
     if (n < 0) {
         LogError("Error in sd_listen_fds");
-        ThrowMsg(Exception::InitFailed, "Error in sd_listen_fds");
+        ThrowMsg(SocketManagerException::InitFailed, "Error in sd_listen_fds");
     }
 
     for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; ++fd) {
@@ -306,12 +306,12 @@ int SocketManager::CreateDomainSocketHelp(const ServiceDescription &desc)
     static_assert(1 == sizeof(*desc.serviceHandlerPath.c_str()));
     if (desc.serviceHandlerPath.size() >= sizeof(static_cast<sockaddr_un*>(0)->sun_path)) {
         LogError("Service handler path too long: " << desc.serviceHandlerPath.size());
-        ThrowMsg(Exception::InitFailed,
+        ThrowMsg(SocketManagerException::InitFailed,
                  "Service handler path too long: " << desc.serviceHandlerPath.size());
     }
 
     if (-1 == (sockfd = socket(AF_UNIX, SOCK_STREAM, 0))) {
-        LogAndThrowErrno(Exception::InitFailed, "socket");
+        LogAndThrowErrno(SocketManagerException::InitFailed, "socket");
     }
     int flags;
     if (-1 == (flags = fcntl(sockfd, F_GETFL, 0)))
@@ -320,7 +320,7 @@ int SocketManager::CreateDomainSocketHelp(const ServiceDescription &desc)
     if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK)) {
         int err = errno;
         close(sockfd);
-        LogAndThrowWithErrno(err, Exception::InitFailed, "fcntl");
+        LogAndThrowWithErrno(err, SocketManagerException::InitFailed, "fcntl");
     }
 
     sockaddr_un serverAddress;
@@ -335,7 +335,7 @@ int SocketManager::CreateDomainSocketHelp(const ServiceDescription &desc)
     if (bind(sockfd, (struct sockaddr*)&serverAddress, sizeof(serverAddress))) {
         int err = errno;
         close(sockfd);
-        LogAndThrowWithErrno(err, Exception::InitFailed, "bind");
+        LogAndThrowWithErrno(err, SocketManagerException::InitFailed, "bind");
     }
 
     umask(originalUmask);
@@ -343,7 +343,7 @@ int SocketManager::CreateDomainSocketHelp(const ServiceDescription &desc)
     if (listen(sockfd, SOMAXCONN)) {
         int err = errno;
         close(sockfd);
-        LogAndThrowWithErrno(err, Exception::InitFailed, "listen");
+        LogAndThrowWithErrno(err, SocketManagerException::InitFailed, "listen");
     }
 
     return sockfd;
@@ -355,7 +355,7 @@ void SocketManager::CreateDomainSocket(const ServiceDescription &desc)
 
     if (-1 == sockfd)
     {
-        LogError("Creating domain socket because \
+        LogWarning("Creating domain socket because \
             the server is not running with on-demand socket activation.");
         sockfd = CreateDomainSocketHelp(desc);
     }
index 455c4bbd23516b8551394e2bec9abaab4f030f7b..832ca1bf610b44307cda336a23ee45f0d24c47d0 100644 (file)
@@ -26,7 +26,6 @@
 #include <mutex>
 #include <thread>
 
-#include <exception.h>
 #include <message-buffer.h>
 
 namespace WA {
@@ -45,11 +44,7 @@ public:
         unsigned counter;  // Unique counter per that descriptor number.
     };
 
-    class Exception {
-    public:
-        DECLARE_EXCEPTION_TYPE(WA::Exception, Base)
-        DECLARE_EXCEPTION_TYPE(Base, InitFailed)
-    };
+
     SocketManager();
     virtual ~SocketManager();
     void MainLoop();