X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=socket-util.c;h=96567c6d81ec247e78e7303d483f166046633e05;hp=c8e8e5975b9b89709799ad8d9b77a8b3f56fd6c2;hb=ee2b489421ce8b47c08fb42c598c5af47043a9f9;hpb=a7334b0952ab66c17ee787e36e6d2c5ceb387de6 diff --git a/socket-util.c b/socket-util.c index c8e8e5975..96567c6d8 100644 --- a/socket-util.c +++ b/socket-util.c @@ -27,6 +27,8 @@ #include #include #include +#include +#include #include "macro.h" #include "util.h" @@ -103,7 +105,6 @@ int socket_address_parse(SocketAddress *a, const char *s) { } else { if ((e = strchr(s, ':'))) { - int r; if ((r = safe_atou(e+1, &u)) < 0) return r; @@ -299,7 +300,15 @@ int socket_address_print(const SocketAddress *a, char **p) { } } -int socket_address_listen(const SocketAddress *a, int backlog, SocketAddressBindIPv6Only only, const char *bind_to_device, int *ret) { +int socket_address_listen( + const SocketAddress *a, + int backlog, + SocketAddressBindIPv6Only only, + const char *bind_to_device, + mode_t directory_mode, + mode_t socket_mode, + int *ret) { + int r, fd, one; assert(a); assert(ret); @@ -325,7 +334,31 @@ int socket_address_listen(const SocketAddress *a, int backlog, SocketAddressBind if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) goto fail; - if (bind(fd, &a->sockaddr.sa, a->size) < 0) + if (socket_address_family(a) == AF_UNIX && a->sockaddr.un.sun_path[0] != 0) { + mode_t old_mask; + + /* Create parents */ + mkdir_parents(a->sockaddr.un.sun_path, directory_mode); + + /* Enforce the right access mode for the socket*/ + old_mask = umask(~ socket_mode); + + /* Include the original umask in our mask */ + umask(~socket_mode | old_mask); + + r = bind(fd, &a->sockaddr.sa, a->size); + + if (r < 0 && errno == EADDRINUSE) { + /* Unlink and try again */ + unlink(a->sockaddr.un.sun_path); + r = bind(fd, &a->sockaddr.sa, a->size); + } + + umask(old_mask); + } else + r = bind(fd, &a->sockaddr.sa, a->size); + + if (r < 0) goto fail; if (a->type == SOCK_STREAM)