diff options
Diffstat (limited to 'src/shared/socket-util.c')
-rw-r--r-- | src/shared/socket-util.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/shared/socket-util.c b/src/shared/socket-util.c index 6c94d69486..f6ddea3183 100644 --- a/src/shared/socket-util.c +++ b/src/shared/socket-util.c @@ -565,6 +565,45 @@ bool socket_address_matches_fd(const SocketAddress *a, int fd) { return false; } +int make_socket_fd(const char* address, int flags) { + SocketAddress a; + int fd, r; + char _cleanup_free_ *p = NULL; + + r = socket_address_parse(&a, address); + if (r < 0) { + log_error("failed to parse socket: %s", strerror(-r)); + return r; + } + + fd = socket(socket_address_family(&a), flags, 0); + if (fd < 0) { + log_error("socket(): %m"); + return -errno; + } + + r = socket_address_print(&a, &p); + if (r < 0) { + log_error("socket_address_print(): %s", strerror(-r)); + return r; + } + log_info("Listening on %s", p); + + r = bind(fd, &a.sockaddr.sa, a.size); + if (r < 0) { + log_error("bind to %s: %m", address); + return -errno; + } + + r = listen(fd, SOMAXCONN); + if (r < 0) { + log_error("listen on %s: %m", address); + return -errno; + } + + return fd; +} + static const char* const netlink_family_table[] = { [NETLINK_ROUTE] = "route", [NETLINK_FIREWALL] = "firewall", |