-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsd_is_socket_unix.c
34 lines (31 loc) · 940 Bytes
/
sd_is_socket_unix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <assert.h>
#include <errno.h>
#include <stddef.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "systemd/sd-daemon.h"
int sd_is_socket_unix(int fd, int type, int listening,
const char *path, size_t length)
{
struct sockaddr_un un;
socklen_t len = sizeof un;
int r;
if ((r = sd_is_socket(fd, AF_UNSPEC, type, listening)) <= 0)
return r;
if (getsockname(fd, (struct sockaddr *)&un, &len) < 0)
return errno != EOPNOTSUPP ? -errno : 0;
assert(len >= offsetof(struct sockaddr_un, sun_family) + sizeof un.sun_family);
if (un.sun_family != AF_UNIX)
return 0;
if (path) {
assert(len >= offsetof(struct sockaddr_un, sun_path));
assert(len <= offsetof(struct sockaddr_un, sun_path) + sizeof un.sun_path);
if (!length)
length = strlen(path);
if (length != len - offsetof(struct sockaddr_un, sun_path))
return 0;
if (memcmp(un.sun_path, path, length))
return 0;
}
return 1;
}