-
Notifications
You must be signed in to change notification settings - Fork 173
/
ipc.c
87 lines (69 loc) · 1.52 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
/*
* keyd - A key remapping daemon.
*
* © 2019 Raheman Vaiya (see also: LICENSE).
*/
#include "keyd.h"
/* TODO (maybe): settle on an API and publish the protocol. */
static void chgid()
{
struct group *g = getgrnam("keyd");
if (!g) {
fprintf(stderr,
"WARNING: failed to set effective group to \"keyd\" (make sure the group exists)\n");
} else {
if (setgid(g->gr_gid)) {
perror("setgid");
exit(-1);
}
}
}
int ipc_connect()
{
int sd = socket(AF_UNIX, SOCK_STREAM, 0);
struct sockaddr_un addr = {0};
if (sd < 0) {
perror("socket");
exit(-1);
}
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, SOCKET_PATH, sizeof(addr.sun_path)-1);
if (connect(sd, (struct sockaddr *) &addr, sizeof addr) < 0) {
perror("bind");
exit(-1);
}
return sd;
}
int ipc_create_server()
{
char lockpath[PATH_MAX];
int sd = socket(AF_UNIX, SOCK_STREAM, 0);
int lfd;
struct sockaddr_un addr = {0};
chgid();
if (sd < 0) {
perror("socket");
exit(-1);
}
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, SOCKET_PATH, sizeof(addr.sun_path)-1);
snprintf(lockpath, sizeof lockpath, "%s.lock", SOCKET_PATH);
lfd = open(lockpath, O_CREAT | O_RDONLY, 0600);
if (lfd < 0) {
perror("open");
exit(-1);
}
if (flock(lfd, LOCK_EX | LOCK_NB))
return -1;
unlink(SOCKET_PATH);
if (bind(sd, (struct sockaddr *) &addr, sizeof addr) < 0) {
fprintf(stderr, "failed to bind to socket %s\n", SOCKET_PATH);
exit(-1);
}
if (listen(sd, 20) < 0) {
perror("listen");
exit(-1);
}
chmod(SOCKET_PATH, 0660);
return sd;
}