summaryrefslogtreecommitdiff
path: root/src/shared/socket-label.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2013-11-06 22:30:35 +0100
committerLennart Poettering <lennart@poettering.net>2013-11-06 23:03:12 +0100
commit175a3d25d0e8596d4ba0759aea3f89ee228e7d6d (patch)
tree95f09c22413d3109170fe0098ae1bdc1741c6354 /src/shared/socket-label.c
parenteceb8483e5a02e8e337486b89719a3b99cfcb7ce (diff)
active: rework make_socket_fd() to be based on socket_address_listen()
Among other things this makes sure we set SO_REUSEADDR which is immensely useful.
Diffstat (limited to 'src/shared/socket-label.c')
-rw-r--r--src/shared/socket-label.c44
1 files changed, 23 insertions, 21 deletions
diff --git a/src/shared/socket-label.c b/src/shared/socket-label.c
index ff212de825..c8be17a1d5 100644
--- a/src/shared/socket-label.c
+++ b/src/shared/socket-label.c
@@ -41,6 +41,7 @@
int socket_address_listen(
const SocketAddress *a,
+ int flags,
int backlog,
SocketAddressBindIPv6Only only,
const char *bind_to_device,
@@ -48,27 +49,31 @@ int socket_address_listen(
bool transparent,
mode_t directory_mode,
mode_t socket_mode,
- const char *label,
- int *ret) {
+ const char *label) {
+
+ _cleanup_close_ int fd = -1;
+ int r, one;
- int r, fd, one;
assert(a);
- assert(ret);
- if ((r = socket_address_verify(a)) < 0)
+ r = socket_address_verify(a);
+ if (r < 0)
return r;
if (socket_address_family(a) == AF_INET6 && !socket_ipv6_is_supported())
return -EAFNOSUPPORT;
- r = label_socket_set(label);
- if (r < 0)
- return r;
+ if (label) {
+ r = label_socket_set(label);
+ if (r < 0)
+ return r;
+ }
- fd = socket(socket_address_family(a), a->type | SOCK_NONBLOCK | SOCK_CLOEXEC, a->protocol);
+ fd = socket(socket_address_family(a), a->type | flags, a->protocol);
r = fd < 0 ? -errno : 0;
- label_socket_clear();
+ if (label)
+ label_socket_clear();
if (r < 0)
return r;
@@ -77,13 +82,13 @@ int socket_address_listen(
int flag = only == SOCKET_ADDRESS_IPV6_ONLY;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag)) < 0)
- goto fail;
+ return -errno;
}
if (socket_address_family(a) == AF_INET || socket_address_family(a) == AF_INET6) {
if (bind_to_device)
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_to_device, strlen(bind_to_device)+1) < 0)
- goto fail;
+ return -errno;
if (free_bind) {
one = 1;
@@ -100,7 +105,7 @@ int socket_address_listen(
one = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
- goto fail;
+ return -errno;
if (socket_address_family(a) == AF_UNIX && a->sockaddr.un.sun_path[0] != 0) {
mode_t old_mask;
@@ -108,7 +113,7 @@ int socket_address_listen(
/* Create parents */
mkdir_parents_label(a->sockaddr.un.sun_path, directory_mode);
- /* Enforce the right access mode for the socket*/
+ /* Enforce the right access mode for the socket */
old_mask = umask(~ socket_mode);
/* Include the original umask in our mask */
@@ -127,17 +132,14 @@ int socket_address_listen(
r = bind(fd, &a->sockaddr.sa, a->size);
if (r < 0)
- goto fail;
+ return -errno;
if (socket_address_can_accept(a))
if (listen(fd, backlog) < 0)
- goto fail;
+ return -errno;
- *ret = fd;
- return 0;
+ r = fd;
+ fd = -1;
-fail:
- r = -errno;
- close_nointr_nofail(fd);
return r;
}