-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipc.c
88 lines (68 loc) · 1.27 KB
/
ipc.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdint.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
#include "ipc.h"
static const
struct sockaddr_un addr = {
.sun_family = AF_UNIX,
.sun_path = "/tmp/fader.sock"
};
static
int
init_socket(struct ipc * ipc) {
ipc->fd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (ipc->fd == -1) {
perror("socket");
return -1;
}
return 0;
}
int
ipc_bind(struct ipc * ipc) {
int ret = init_socket(ipc);
if (ret == -1) {
return ret;
}
unlink(addr.sun_path);
if (bind(ipc->fd, (struct sockaddr *)&addr, sizeof addr) == -1) {
perror("connect");
return -1;
}
chmod(addr.sun_path, 0777);
return 0;
}
int
ipc_connect(struct ipc * ipc) {
int ret = init_socket(ipc);
if (ret == -1) {
return ret;
}
if (connect(ipc->fd, (struct sockaddr *)&addr, sizeof addr) == -1) {
perror("connect");
return -1;
}
return 0;
}
int
ipc_close(struct ipc * ipc) {
return close(ipc->fd);
}
int
ipc_msg_send(struct ipc * ipc, const struct msg * msg) {
ssize_t ret;
ret = write(ipc->fd, msg, sizeof * msg);
if (ret != sizeof * msg)
return -1;
return 0;
}
int
ipc_msg_recv(struct ipc * ipc, struct msg * msg) {
ssize_t ret;
ret = read(ipc->fd, msg, sizeof * msg);
if (ret != sizeof * msg)
return -1;
return 0;
}