Setup connection to daemon int sd socket AF_UNIX SOCK_STREAM if sd pri

 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
// Setup connection to daemon
int sd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sd < 0) {
printf("Socket failed: %s\n", strerror(errno));
return;
}
struct sockaddr_un serveraddr;
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sun_family = AF_UNIX;
strcpy(serveraddr.sun_path, SOCKET_PATH);
int res;
do {
res = connect(sd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
} while (-1 == res && EINTR == errno);
if (res < 0) {
printf("Connect failed: %s\n", strerror(errno));
close(sd);
return;
}
// ...
// Send request
do {
res = write(sd, &input, sizeof(input));
} while (-1 == res && EINTR == errno);
if (-1 == res){
printf("Send request failed: %s\n", strerror(errno));
close(sd);
return;
}
// ...