summaryrefslogtreecommitdiff
path: root/unix-socket.c
blob: 84b15099f2fbf89dedf5241a6176b39abd4b1723 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "cache.h"
#include "unix-socket.h"

static int unix_stream_socket(void)
{
	int fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (fd < 0)
		die_errno("unable to create socket");
	return fd;
}

static void unix_sockaddr_init(struct sockaddr_un *sa, const char *path)
{
	int size = strlen(path) + 1;
	if (size > sizeof(sa->sun_path))
		die("socket path is too long to fit in sockaddr");
	memset(sa, 0, sizeof(*sa));
	sa->sun_family = AF_UNIX;
	memcpy(sa->sun_path, path, size);
}

int unix_stream_connect(const char *path)
{
	int fd;
	struct sockaddr_un sa;

	unix_sockaddr_init(&sa, path);
	fd = unix_stream_socket();
	if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
		close(fd);
		return -1;
	}
	return fd;
}

int unix_stream_listen(const char *path)
{
	int fd;
	struct sockaddr_un sa;

	unix_sockaddr_init(&sa, path);
	fd = unix_stream_socket();

	unlink(path);
	if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
		close(fd);
		return -1;
	}

	if (listen(fd, 5) < 0) {
		close(fd);
		return -1;
	}

	return fd;
}