1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
32 #include "selinux-util.h"
33 #include "socket-util.h"
35 int socket_address_listen(
36 const SocketAddress *a,
39 SocketAddressBindIPv6Only only,
40 const char *bind_to_device,
43 mode_t directory_mode,
47 _cleanup_close_ int fd = -1;
52 r = socket_address_verify(a);
56 if (socket_address_family(a) == AF_INET6 && !socket_ipv6_is_supported())
60 r = mac_selinux_create_socket_prepare(label);
65 fd = socket(socket_address_family(a), a->type | flags, a->protocol);
66 r = fd < 0 ? -errno : 0;
69 mac_selinux_create_socket_clear();
74 if (socket_address_family(a) == AF_INET6 && only != SOCKET_ADDRESS_DEFAULT) {
75 int flag = only == SOCKET_ADDRESS_IPV6_ONLY;
77 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag)) < 0)
81 if (socket_address_family(a) == AF_INET || socket_address_family(a) == AF_INET6) {
83 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_to_device, strlen(bind_to_device)+1) < 0)
88 if (setsockopt(fd, IPPROTO_IP, IP_FREEBIND, &one, sizeof(one)) < 0)
89 log_warning_errno(errno, "IP_FREEBIND failed: %m");
94 if (setsockopt(fd, IPPROTO_IP, IP_TRANSPARENT, &one, sizeof(one)) < 0)
95 log_warning_errno(errno, "IP_TRANSPARENT failed: %m");
100 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
103 if (socket_address_family(a) == AF_UNIX && a->sockaddr.un.sun_path[0] != 0) {
107 mkdir_parents_label(a->sockaddr.un.sun_path, directory_mode);
109 /* Enforce the right access mode for the socket */
110 old_mask = umask(~ socket_mode);
112 /* Include the original umask in our mask */
113 umask(~socket_mode | old_mask);
115 r = mac_selinux_bind(fd, &a->sockaddr.sa, a->size);
117 if (r < 0 && errno == EADDRINUSE) {
118 /* Unlink and try again */
119 unlink(a->sockaddr.un.sun_path);
120 r = bind(fd, &a->sockaddr.sa, a->size);
125 r = bind(fd, &a->sockaddr.sa, a->size);
130 if (socket_address_can_accept(a))
131 if (listen(fd, backlog) < 0)
140 int make_socket_fd(int log_level, const char* address, int flags) {
144 r = socket_address_parse(&a, address);
146 log_error("Failed to parse socket address \"%s\": %s",
147 address, strerror(-r));
151 fd = socket_address_listen(&a, flags, SOMAXCONN, SOCKET_ADDRESS_DEFAULT,
152 NULL, false, false, 0755, 0644, NULL);
153 if (fd < 0 || log_get_max_level() >= log_level) {
154 _cleanup_free_ char *p = NULL;
156 r = socket_address_print(&a, &p);
158 return log_error_errno(r, "socket_address_print(): %m");
161 log_error_errno(fd, "Failed to listen on %s: %m", p);
163 log_full(log_level, "Listening on %s", p);