From: Andre Noll Date: Sun, 11 Jan 2015 23:40:22 +0000 (+0100) Subject: create_local_socket(): Avoid code duplication. X-Git-Tag: v0.5.5~60^2~2 X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=commitdiff_plain;h=00e95557839f3fef5fa06702f3864e8376d2a29b create_local_socket(): Avoid code duplication. 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. --- diff --git a/afs.c b/afs.c index 7832a324..c677b6cd 100644 --- a/afs.c +++ b/afs.c @@ -646,15 +646,6 @@ static int setup_command_socket_or_die(void) 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; diff --git a/audiod.c b/audiod.c index 40f02f74..e8a82b5e 100644 --- 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) { - int ret, fd; + int ret; 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; - 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); diff --git a/net.c b/net.c index c28cf096..9698e427 100644 --- 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. * - * 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) { @@ -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; + 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); @@ -879,6 +882,10 @@ int create_local_socket(const char *name, mode_t mode) 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);