socket-label.c revision 1af719edc5958c01c19204fb68d6fc45c9eea85c
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2010 Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering const char *label) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (socket_address_family(a) == AF_INET6 && !socket_ipv6_is_supported())
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fd = socket(socket_address_family(a), a->type | flags, a->protocol);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (socket_address_family(a) == AF_INET6 && only != SOCKET_ADDRESS_DEFAULT) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag)) < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (socket_address_family(a) == AF_INET || socket_address_family(a) == AF_INET6) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, bind_to_device, strlen(bind_to_device)+1) < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (setsockopt(fd, IPPROTO_IP, IP_FREEBIND, &one, sizeof(one)) < 0)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (setsockopt(fd, IPPROTO_IP, IP_TRANSPARENT, &one, sizeof(one)) < 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (socket_address_family(a) == AF_UNIX && a->sockaddr.un.sun_path[0] != 0) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering /* Create parents */
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering mkdir_parents_label(a->sockaddr.un.sun_path, directory_mode);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering /* Enforce the right access mode for the socket */
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering /* Include the original umask in our mask */
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering r = label_bind(fd, &a->sockaddr.sa, a->size);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering /* Unlink and try again */
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poetteringint make_socket_fd(int log_level, const char* address, int flags) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering log_error("Failed to parse socket address \"%s\": %s",
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering fd = socket_address_listen(&a, flags, SOMAXCONN, SOCKET_ADDRESS_DEFAULT,
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (fd < 0 || log_get_max_level() >= log_level) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering log_error("socket_address_print(): %s", strerror(-r));
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers log_error("Failed to listen on %s: %s", p, strerror(-fd));