From: Lennart Poettering Date: Mon, 15 Oct 2018 10:06:07 +0000 (+0200) Subject: socket-util: add new sockaddr_un_unlink() helper X-Git-Tag: v240~538^2~18 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9f20fc28f0a624ea49ac941bcde57b5435a01b6e;p=platform%2Fupstream%2Fsystemd.git socket-util: add new sockaddr_un_unlink() helper The helper is supposed to properly handle cases where .sun_path does not contain a NUL byte, and thus copies out the path suffix a NUL as necessary. This also reworks the more specific socket_address_unlink() to be a wrapper around the more generic sockaddr_un_unlink() --- diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index 4e8b2ba..df96aba 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -747,21 +747,6 @@ int socknameinfo_pretty(union sockaddr_union *sa, socklen_t salen, char **_ret) return 0; } -int socket_address_unlink(SocketAddress *a) { - assert(a); - - if (socket_address_family(a) != AF_UNIX) - return 0; - - if (a->sockaddr.un.sun_path[0] == 0) - return 0; - - if (unlink(a->sockaddr.un.sun_path) < 0) - return -errno; - - return 1; -} - static const char* const netlink_family_table[] = { [NETLINK_ROUTE] = "route", [NETLINK_FIREWALL] = "firewall", @@ -1246,3 +1231,27 @@ int socket_ioctl_fd(void) { return fd; } + +int sockaddr_un_unlink(const struct sockaddr_un *sa) { + const char *p, * nul; + + assert(sa); + + if (sa->sun_family != AF_UNIX) + return -EPROTOTYPE; + + if (sa->sun_path[0] == 0) /* Nothing to do for abstract sockets */ + return 0; + + /* The path in .sun_path is not necessarily NUL terminated. Let's fix that. */ + nul = memchr(sa->sun_path, 0, sizeof(sa->sun_path)); + if (nul) + p = sa->sun_path; + else + p = memdupa_suffix0(sa->sun_path, sizeof(sa->sun_path)); + + if (unlink(p) < 0) + return -errno; + + return 1; +} diff --git a/src/basic/socket-util.h b/src/basic/socket-util.h index 82781a0..0a7d798 100644 --- a/src/basic/socket-util.h +++ b/src/basic/socket-util.h @@ -71,7 +71,12 @@ int socket_address_parse_and_warn(SocketAddress *a, const char *s); int socket_address_parse_netlink(SocketAddress *a, const char *s); int socket_address_print(const SocketAddress *a, char **p); int socket_address_verify(const SocketAddress *a) _pure_; -int socket_address_unlink(SocketAddress *a); + +int sockaddr_un_unlink(const struct sockaddr_un *sa); + +static inline int socket_address_unlink(const SocketAddress *a) { + return socket_address_family(a) == AF_UNIX ? sockaddr_un_unlink(&a->sockaddr.un) : 0; +} bool socket_address_can_accept(const SocketAddress *a) _pure_;