Split socket creation into bind and listen parts 77/125977/4
authorMichal Bloch <m.bloch@samsung.com>
Wed, 12 Apr 2017 20:18:57 +0000 (22:18 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Fri, 21 Apr 2017 13:09:34 +0000 (06:09 -0700)
Change-Id: I09df695ec42ba6d8ad037bffc34c5d9c8f6a89ee
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/logger/logger.c

index c1e4898..9ea0299 100644 (file)
@@ -329,18 +329,18 @@ static int reset_self_privileges()
 }
 
 /**
- * @brief Create a socket
- * @details Creates a socket with given permissions under the given path
+ * @brief Create a bound socket
+ * @details Creates a socket of given type under the given path
  * @param[in] path The path to the socket
- * @param[in] permissions File permissions (the internal representation)
+ * @param[in] type Socket type (SOCK_DGRAM or SOCK_STREAM) with flags
  * @return The socket FD, or negative errno
  */
-static int listen_fd_create(const char* path, int permissions)
+static int bind_fd_create(const char* path, int type)
 {
        struct sockaddr_un server_addr;
        int sd;
 
-       sd = socket(AF_UNIX, SOCK_STREAM, 0);
+       sd = socket(AF_UNIX, type, 0);
        if (sd == -1)
                return -errno;
 
@@ -352,6 +352,26 @@ static int listen_fd_create(const char* path, int permissions)
        if (bind(sd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1)
                goto failure;
 
+       return sd;
+
+failure:
+       close(sd);
+       return -errno;
+}
+
+/**
+ * @brief Create a listening socket
+ * @details Creates a socket with given permissions under the given path
+ * @param[in] path The path to the socket
+ * @param[in] permissions File permissions (the internal representation)
+ * @return The socket FD, or negative errno
+ */
+static int listen_fd_create(const char* path, int permissions)
+{
+       int sd = bind_fd_create(path, SOCK_STREAM);
+       if (sd < 0)
+               return sd;
+
        if (permissions)
                if (chmod(path, permissions) < 0) // ideally, fchmod would be used, but that does not work
                        goto failure;