2 #include "unix-socket.h"
4 static int unix_stream_socket(void)
6 int fd = socket(AF_UNIX, SOCK_STREAM, 0);
8 die_errno("unable to create socket");
12 static void unix_sockaddr_init(struct sockaddr_un *sa, const char *path)
14 int size = strlen(path) + 1;
15 if (size > sizeof(sa->sun_path))
16 die("socket path is too long to fit in sockaddr");
17 memset(sa, 0, sizeof(*sa));
18 sa->sun_family = AF_UNIX;
19 memcpy(sa->sun_path, path, size);
22 int unix_stream_connect(const char *path)
25 struct sockaddr_un sa;
27 unix_sockaddr_init(&sa, path);
28 fd = unix_stream_socket();
29 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
36 int unix_stream_listen(const char *path)
39 struct sockaddr_un sa;
41 unix_sockaddr_init(&sa, path);
42 fd = unix_stream_socket();
45 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
50 if (listen(fd, 5) < 0) {