-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunix_socket.c
175 lines (143 loc) · 5.08 KB
/
unix_socket.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright (C) 2015 by Andreas Fuchs <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
* Routines to open UNIX domain sockets & send fds along
* them. Boilerplate copied mostly wholesale from:
* <http://blog.varunajayasiri.com/passing-file-descriptors-between-processes-using-sendmsg-and-recvmsg>.
*/
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <strings.h>
#include <fcntl.h>
#include "deptyr.h"
int create_server(char *socket_path) {
struct sockaddr_un addr;
int fd;
if ((fd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
die("Failed to create server socket");
return fd;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_LOCAL;
unlink(socket_path);
strcpy(addr.sun_path, socket_path);
if (bind(fd, (struct sockaddr *) &(addr),
sizeof(addr)) < 0) {
die("Failed to bind server socket");
return -1;
}
if (listen(fd, 0) < 0) {
die("Failed to listen on server socket");
return -1;
}
return fd;
}
int connect_server(char *socket_path) {
struct sockaddr_un addr;
int fd;
if ((fd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
die("Failed to create client socket");
return fd;
}
if (fcntl(fd, FD_CLOEXEC) < 0) {
die("Failed to set CLOEXEC on client socket");
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_LOCAL;
strcpy(addr.sun_path, socket_path);
if (connect(fd,
(struct sockaddr *) &(addr),
sizeof(addr)) < 0) {
die("Failed to connect to server");
return -1;
}
return fd;
}
int
recv_file_descriptor(
int socket) /* Socket from which the file descriptor is read */
{
int sent_fd;
struct msghdr message;
struct iovec iov[1];
struct cmsghdr *control_message = NULL;
char ctrl_buf[CMSG_SPACE(sizeof(int))];
char data[1];
int res;
memset(&message, 0, sizeof(struct msghdr));
memset(ctrl_buf, 0, CMSG_SPACE(sizeof(int)));
/* For the dummy data */
iov[0].iov_base = data;
iov[0].iov_len = sizeof(data);
message.msg_name = NULL;
message.msg_namelen = 0;
message.msg_control = ctrl_buf;
message.msg_controllen = CMSG_SPACE(sizeof(int));
message.msg_iov = iov;
message.msg_iovlen = 1;
if((res = recvmsg(socket, &message, 0)) <= 0)
return res;
/* Iterate through header to find if there is a file descriptor */
for(control_message = CMSG_FIRSTHDR(&message);
control_message != NULL;
control_message = CMSG_NXTHDR(&message,
control_message))
{
if( (control_message->cmsg_level == SOL_SOCKET) &&
(control_message->cmsg_type == SCM_RIGHTS) )
{
return *((int *) CMSG_DATA(control_message));
}
}
return -1;
}
int
send_file_descriptor(
int socket, /* Socket through which the file descriptor is passed */
int fd_to_send) /* File descriptor to be passed, could be another socket */
{
struct msghdr message;
struct iovec iov[1];
struct cmsghdr *control_message = NULL;
char ctrl_buf[CMSG_SPACE(sizeof(int))];
char data[1];
memset(&message, 0, sizeof(struct msghdr));
memset(ctrl_buf, 0, CMSG_SPACE(sizeof(int)));
/* We are passing at least one byte of data so that recvmsg() will not return 0 */
data[0] = ' ';
iov[0].iov_base = data;
iov[0].iov_len = sizeof(data);
message.msg_name = NULL;
message.msg_namelen = 0;
message.msg_iov = iov;
message.msg_iovlen = 1;
message.msg_controllen = CMSG_SPACE(sizeof(int));
message.msg_control = ctrl_buf;
control_message = CMSG_FIRSTHDR(&message);
control_message->cmsg_level = SOL_SOCKET;
control_message->cmsg_type = SCM_RIGHTS;
control_message->cmsg_len = CMSG_LEN(sizeof(int));
*((int *) CMSG_DATA(control_message)) = fd_to_send;
return sendmsg(socket, &message, 0);
}