summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-10-15 12:06:07 +0200
committerSven Eden <sven.eden@prydeworx.com>2018-10-29 10:18:39 +0100
commit2eae0cc84c4492a3fe93d453c4ff5a7c6e633f26 (patch)
tree236d282aab19937d461b2fc5729c748dfc868b8f /src
parent36770c7d5854a17800583f24bf2ae69f5b68cb21 (diff)
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() (cherry picked from commit 9f20fc28f0a624ea49ac941bcde57b5435a01b6e)
Diffstat (limited to 'src')
-rw-r--r--src/basic/socket-util.c39
-rw-r--r--src/basic/socket-util.h7
2 files changed, 30 insertions, 16 deletions
diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c
index a642053ed..9fa638cb7 100644
--- a/src/basic/socket-util.c
+++ b/src/basic/socket-util.c
@@ -774,21 +774,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",
@@ -1221,3 +1206,27 @@ int socket_ioctl_fd(void) {
return fd;
}
#endif // 0
+
+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 35191616f..84c024a37 100644
--- a/src/basic/socket-util.h
+++ b/src/basic/socket-util.h
@@ -74,7 +74,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_;