create_local_socket(): Avoid code duplication.
authorAndre Noll <maan@tuebingen.mpg.de>
Sun, 11 Jan 2015 23:40:22 +0000 (00:40 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Sat, 14 Feb 2015 10:45:26 +0000 (11:45 +0100)
The two users of create_local_socket(), afs.c and audiod.c, both
call mark_fd_nonblocking() and listen(2) on the returned socket file
descriptor. This patch moves the code for this additional work to
create_local_socket(), simplifying the callers.

afs.c
audiod.c
net.c

diff --git a/afs.c b/afs.c
index 7832a32482ae51604d80e4a897d0c68c3a7186e3..c677b6cd8d9cb504969021c6d99fcab75a78a9dc 100644 (file)
--- a/afs.c
+++ b/afs.c
@@ -646,15 +646,6 @@ static int setup_command_socket_or_die(void)
                exit(EXIT_FAILURE);
        }
        socket_fd = ret;
                exit(EXIT_FAILURE);
        }
        socket_fd = ret;
-       if (listen(socket_fd , 5) < 0) {
-               PARA_EMERG_LOG("can not listen on socket\n");
-               exit(EXIT_FAILURE);
-       }
-       ret = mark_fd_nonblocking(socket_fd);
-       if (ret < 0) {
-               close(socket_fd);
-               return ret;
-       }
        PARA_INFO_LOG("listening on socket %s (fd %d)\n", socket_name,
                socket_fd);
        return socket_fd;
        PARA_INFO_LOG("listening on socket %s (fd %d)\n", socket_name,
                socket_fd);
        return socket_fd;
index 40f02f74907c1b1b60778acb4a742c018fb51014..e8a82b5ed254d447b8edc106e0c6f98d2a64d2f7 100644 (file)
--- a/audiod.c
+++ b/audiod.c
@@ -958,7 +958,7 @@ static int parse_stream_args(void)
 /* does not unlink socket on errors */
 static int audiod_get_socket(void)
 {
 /* does not unlink socket on errors */
 static int audiod_get_socket(void)
 {
-       int ret, fd;
+       int ret;
 
        if (conf.socket_given)
                socket_name = para_strdup(conf.socket_arg);
 
        if (conf.socket_given)
                socket_name = para_strdup(conf.socket_arg);
@@ -975,15 +975,7 @@ static int audiod_get_socket(void)
                S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH);
        if (ret < 0)
                goto err;
                S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH);
        if (ret < 0)
                goto err;
-       fd = ret;
-       if (listen(fd , 5) < 0) {
-               ret = -ERRNO_TO_PARA_ERROR(errno);
-               goto err;
-       }
-       ret = mark_fd_nonblocking(fd);
-       if (ret < 0)
-               goto err;
-       return fd;
+       return ret;
 err:
        PARA_EMERG_LOG("%s\n", para_strerror(-ret));
        exit(EXIT_FAILURE);
 err:
        PARA_EMERG_LOG("%s\n", para_strerror(-ret));
        exit(EXIT_FAILURE);
diff --git a/net.c b/net.c
index c28cf0964b0468bc9e616dc2c014536bc84e2ad0..9698e427868e212646f682d9b8dc2d7e8f6235ae 100644 (file)
--- a/net.c
+++ b/net.c
@@ -845,19 +845,19 @@ static int init_unix_addr(struct sockaddr_un *u, const char *name)
 }
 
 /**
 }
 
 /**
- * Prepare, create, and bind a socket for local communication.
+ * Create a socket for local communication and listen on it.
  *
  * \param name The socket pathname.
  * \param mode The desired permissions of the socket.
  *
  *
  * \param name The socket pathname.
  * \param mode The desired permissions of the socket.
  *
- * This function creates a local socket for sequenced, reliable,
- * two-way, connection-based byte streams.
+ * This function creates a passive local socket for sequenced, reliable,
+ * two-way, connection-based byte streams. The socket file descriptor is set to
+ * nonblocking mode and listen(2) is called to prepare the socket for
+ * accepting incoming connection requests.
  *
  *
- * \return The file descriptor, on success, negative on errors.
+ * \return The file descriptor on success, negative error code on failure.
  *
  *
- * \sa socket(2)
- * \sa bind(2)
- * \sa chmod(2)
+ * \sa socket(2), \sa bind(2), \sa chmod(2), listen(2), unix(7).
  */
 int create_local_socket(const char *name, mode_t mode)
 {
  */
 int create_local_socket(const char *name, mode_t mode)
 {
@@ -871,6 +871,9 @@ int create_local_socket(const char *name, mode_t mode)
        if (ret < 0)
                return -ERRNO_TO_PARA_ERROR(errno);
        fd = ret;
        if (ret < 0)
                return -ERRNO_TO_PARA_ERROR(errno);
        fd = ret;
+       ret = mark_fd_nonblocking(fd);
+       if (ret < 0)
+               goto err;
        ret = bind(fd, (struct sockaddr *)&unix_addr, UNIX_PATH_MAX);
        if (ret < 0) {
                ret = -ERRNO_TO_PARA_ERROR(errno);
        ret = bind(fd, (struct sockaddr *)&unix_addr, UNIX_PATH_MAX);
        if (ret < 0) {
                ret = -ERRNO_TO_PARA_ERROR(errno);
@@ -879,6 +882,10 @@ int create_local_socket(const char *name, mode_t mode)
        ret = -E_CHMOD;
        if (chmod(name, mode) < 0)
                goto err;
        ret = -E_CHMOD;
        if (chmod(name, mode) < 0)
                goto err;
+       if (listen(fd , 5) < 0) {
+               ret = -ERRNO_TO_PARA_ERROR(errno);
+               goto err;
+       }
        return fd;
 err:
        close(fd);
        return fd;
 err:
        close(fd);