Blob Blame History Raw
commit e9548127edae313da7840bcb87fd0afd04549c2e
Author: Ingo Franzki <ifranzki@linux.ibm.com>
Date:   Mon Feb 8 15:26:23 2021 +0100

    pkcsslotd: Refactoring in preparation for event support
    
    No functional change so far, just making things a bit bore clearer.
    
    Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>

diff --git a/usr/include/slotmgr.h b/usr/include/slotmgr.h
index 3950a9a3..4d038435 100644
--- a/usr/include/slotmgr.h
+++ b/usr/include/slotmgr.h
@@ -30,7 +30,7 @@
 #define TOK_PATH  SBIN_PATH "/pkcsslotd"
 #define OCK_API_LOCK_FILE LOCKDIR_PATH "/LCK..APIlock"
 
-#define SOCKET_FILE_PATH "/var/run/pkcsslotd.socket"
+#define PROC_SOCKET_FILE_PATH "/var/run/pkcsslotd.socket"
 
 #define PID_FILE_PATH "/var/run/pkcsslotd.pid"
 #define OCK_CONFIG OCK_CONFDIR "/opencryptoki.conf"
diff --git a/usr/lib/api/api_interface.c b/usr/lib/api/api_interface.c
index b74b763f..2873a20a 100644
--- a/usr/lib/api/api_interface.c
+++ b/usr/lib/api/api_interface.c
@@ -2831,7 +2831,7 @@ CK_RV C_Initialize(CK_VOID_PTR pVoid)
     TRACE_DEBUG("Shared memory %p \n", Anchor->SharedMemP);
 
     /* Connect to slot daemon and retrieve slot infos */
-    Anchor->socketfd = connect_socket(SOCKET_FILE_PATH);
+    Anchor->socketfd = connect_socket(PROC_SOCKET_FILE_PATH);
     if (Anchor->socketfd < 0) {
         OCK_SYSLOG(LOG_ERR, "C_Initialize: Module failed to create a "
                    "socket. Verify that the slot management daemon is "
diff --git a/usr/sbin/pkcsslotd/pkcsslotd.h b/usr/sbin/pkcsslotd/pkcsslotd.h
index 813db9f4..69eb59f3 100644
--- a/usr/sbin/pkcsslotd/pkcsslotd.h
+++ b/usr/sbin/pkcsslotd/pkcsslotd.h
@@ -61,7 +61,6 @@ extern key_t tok;
 extern Slot_Info_t_64 sinfo[NUMBER_SLOTS_MANAGED];
 extern unsigned int NumberSlotsInDB;
 
-extern int socketfd;
 extern Slot_Mgr_Socket_t socketData;
 
 
@@ -89,9 +88,9 @@ int XProcLock(void);
 int XProcUnLock(void);
 int CreateXProcLock(void);
 
-int CreateListenerSocket(void);
-int InitSocketData(Slot_Mgr_Socket_t *sp);
-int SocketConnectionHandler(int socketfd, int timeout_secs);
-void DetachSocketListener(int socketfd);
+int init_socket_server();
+int term_socket_server();
+int init_socket_data(Slot_Mgr_Socket_t *sp);
+int socket_connection_handler(int timeout_secs);
 
 #endif                          /* _SLOTMGR_H */
diff --git a/usr/sbin/pkcsslotd/signal.c b/usr/sbin/pkcsslotd/signal.c
index cf7b9087..49482a2f 100644
--- a/usr/sbin/pkcsslotd/signal.c
+++ b/usr/sbin/pkcsslotd/signal.c
@@ -101,7 +101,7 @@ void slotdGenericSignalHandler(int Signal)
 
     InfoLog("Exiting on %s (%d; %#x)", SignalConst(Signal), Signal, Signal);
 
-    DetachSocketListener(socketfd);
+    term_socket_server();
     DestroyMutexes();
     DetachFromSharedMemory();
     DestroySharedMemory();
diff --git a/usr/sbin/pkcsslotd/slotmgr.c b/usr/sbin/pkcsslotd/slotmgr.c
index ea5c86f5..94288f13 100644
--- a/usr/sbin/pkcsslotd/slotmgr.c
+++ b/usr/sbin/pkcsslotd/slotmgr.c
@@ -37,7 +37,6 @@ unsigned int NumberSlotsInDB = 0;
 
 Slot_Info_t_64 *psinfo;
 
-int socketfd;
 Slot_Mgr_Socket_t socketData;
 
 struct dircheckinfo_s {
@@ -569,15 +568,15 @@ int main(int argc, char *argv[], char *envp[])
     if (!XProcUnLock())
         return 4;
 
-    if ((socketfd = CreateListenerSocket()) < 0) {
+    if (!init_socket_server()) {
         DestroyMutexes();
         DetachFromSharedMemory();
         DestroySharedMemory();
         return 5;
     }
 
-    if (!InitSocketData(&socketData)) {
-        DetachSocketListener(socketfd);
+    if (!init_socket_data(&socketData)) {
+        term_socket_server();
         DestroyMutexes();
         DetachFromSharedMemory();
         DestroySharedMemory();
@@ -598,7 +597,7 @@ int main(int argc, char *argv[], char *envp[])
     if (Daemon) {
         pid_t pid;
         if ((pid = fork()) < 0) {
-            DetachSocketListener(socketfd);
+            term_socket_server();
             DestroyMutexes();
             DetachFromSharedMemory();
             DestroySharedMemory();
@@ -643,7 +642,7 @@ int main(int argc, char *argv[], char *envp[])
      * the daemonization process redefines our handler for (at least) SIGTERM
      */
     if (!SetupSignalHandlers()) {
-        DetachSocketListener(socketfd);
+        term_socket_server();
         DestroyMutexes();
         DetachFromSharedMemory();
         DestroySharedMemory();
@@ -664,7 +663,7 @@ int main(int argc, char *argv[], char *envp[])
     printf("Start garbage \n");
     /* start garbage collection thread */
     if (!StartGCThread(shmp)) {
-        DetachSocketListener(socketfd);
+        term_socket_server();
         DestroyMutexes();
         DetachFromSharedMemory();
         DestroySharedMemory();
@@ -684,7 +683,7 @@ int main(int argc, char *argv[], char *envp[])
 #if !(THREADED) && !(NOGARBAGE)
         CheckForGarbage(shmp);
 #endif
-        SocketConnectionHandler(socketfd, 10);
+        socket_connection_handler(10);
     }
 
     /*************************************************************
diff --git a/usr/sbin/pkcsslotd/socket_server.c b/usr/sbin/pkcsslotd/socket_server.c
index ae0eff92..1fae0b95 100644
--- a/usr/sbin/pkcsslotd/socket_server.c
+++ b/usr/sbin/pkcsslotd/socket_server.c
@@ -25,10 +25,14 @@
 #include "pkcsslotd.h"
 #include "apictl.h"
 
+int proc_listener_socket = -1;
+
+static void close_listener_socket(int socketfd, const char *file_path);
+
 // Creates the daemon's listener socket, to which clients will connect and
 // retrieve slot information through.  Returns the file descriptor of the
 // created socket.
-int CreateListenerSocket(void)
+static int create_listener_socket(const char *file_path)
 {
     struct sockaddr_un address;
     struct group *grp;
@@ -39,53 +43,60 @@ int CreateListenerSocket(void)
         ErrLog("Failed to create listener socket, errno 0x%X.", errno);
         return -1;
     }
-    if (unlink(SOCKET_FILE_PATH) && errno != ENOENT) {
+    if (unlink(file_path) && errno != ENOENT) {
         ErrLog("Failed to unlink socket file, errno 0x%X.", errno);
-        close(socketfd);
-        return -1;
+        goto error;
     }
 
     memset(&address, 0, sizeof(struct sockaddr_un));
     address.sun_family = AF_UNIX;
-    strcpy(address.sun_path, SOCKET_FILE_PATH);
+    strcpy(address.sun_path, file_path);
 
     if (bind(socketfd,
              (struct sockaddr *) &address, sizeof(struct sockaddr_un)) != 0) {
         ErrLog("Failed to bind to socket, errno 0x%X.", errno);
-        close(socketfd);
-        return -1;
+        goto error;
     }
     // make socket file part of the pkcs11 group, and write accessable
     // for that group
     grp = getgrnam("pkcs11");
     if (!grp) {
         ErrLog("Group PKCS#11 does not exist");
-        DetachSocketListener(socketfd);
-        return -1;
+        goto error;
     }
-    if (chown(SOCKET_FILE_PATH, 0, grp->gr_gid)) {
+    if (chown(file_path, 0, grp->gr_gid)) {
         ErrLog("Could not change file group on socket, errno 0x%X.", errno);
-        DetachSocketListener(socketfd);
-        return -1;
+        goto error;
     }
-    if (chmod(SOCKET_FILE_PATH,
+    if (chmod(file_path,
               S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP | S_IXUSR | S_IXGRP)) {
         ErrLog("Could not change file permissions on socket, errno 0x%X.",
                errno);
-        DetachSocketListener(socketfd);
-        return -1;
+        goto error;
     }
 
     if (listen(socketfd, 20) != 0) {
         ErrLog("Failed to listen to socket, errno 0x%X.", errno);
-        DetachSocketListener(socketfd);
-        return -1;
+        goto error;
     }
 
     return socketfd;
+
+error:
+    if (socketfd >= 0)
+        close_listener_socket(socketfd, file_path);
+
+    return -1;
+}
+
+
+static void close_listener_socket(int socketfd, const char *file_path)
+{
+    close(socketfd);
+    unlink(file_path);
 }
 
-int InitSocketData(Slot_Mgr_Socket_t *socketData)
+int init_socket_data(Slot_Mgr_Socket_t *socketData)
 {
     unsigned int processed = 0;
 
@@ -102,19 +113,19 @@ int InitSocketData(Slot_Mgr_Socket_t *socketData)
     return TRUE;
 }
 
-int SocketConnectionHandler(int socketfd, int timeout_secs)
+int socket_connection_handler(int timeout_secs)
 {
     int returnVal;
     fd_set set;
     struct timeval timeout;
 
     FD_ZERO(&set);
-    FD_SET(socketfd, &set);
+    FD_SET(proc_listener_socket, &set);
 
     timeout.tv_sec = timeout_secs;
     timeout.tv_usec = 0;
 
-    returnVal = select(socketfd + 1, &set, NULL, NULL, &timeout);
+    returnVal = select(proc_listener_socket + 1, &set, NULL, NULL, &timeout);
     if (returnVal == -1) {
         ErrLog("select failed on socket connection, errno 0x%X.", errno);
         return FALSE;
@@ -125,7 +136,7 @@ int SocketConnectionHandler(int socketfd, int timeout_secs)
         struct sockaddr_un address;
         socklen_t address_length = sizeof(address);
 
-        int connectionfd = accept(socketfd,
+        int connectionfd = accept(proc_listener_socket,
                                   (struct sockaddr *) &address,
                                   &address_length);
         if (connectionfd < 0) {
@@ -138,6 +149,10 @@ int SocketConnectionHandler(int socketfd, int timeout_secs)
             }
             return FALSE;
         }
+
+        DbgLog(DL0, "Accepted connection from process: socket: %d", 
+               connectionfd);
+
         if (write(connectionfd, &socketData, sizeof(socketData)) !=
             sizeof(socketData)) {
             ErrLog("Failed to write socket data, errno 0x%X.", errno);
@@ -149,8 +164,23 @@ int SocketConnectionHandler(int socketfd, int timeout_secs)
     }
 }
 
-void DetachSocketListener(int socketfd)
+int init_socket_server()
 {
-    close(socketfd);
-    unlink(SOCKET_FILE_PATH);
+    proc_listener_socket = create_listener_socket(PROC_SOCKET_FILE_PATH);
+    if (proc_listener_socket < 0)
+        return FALSE;
+
+    DbgLog(DL0, "Socket server started");
+
+    return TRUE;
+}
+
+int term_socket_server()
+{
+    if (proc_listener_socket >= 0)
+        close_listener_socket(proc_listener_socket, PROC_SOCKET_FILE_PATH);
+
+    DbgLog(DL0, "Socket server stopped");
+
+    return TRUE;
 }