}
/**
- * @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;
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;