Commit | Line | Data |
---|---|---|
826d496d | 1 | /* |
917216f6 DG |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
fac6795d | 4 | * |
917216f6 DG |
5 | * This program is free software; you can redistribute it and/or modify it |
6 | * under the terms of the GNU General Public License as published by the Free | |
7 | * Software Foundation; only version 2 of the License. | |
fac6795d | 8 | * |
917216f6 DG |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
fac6795d | 13 | * |
917216f6 DG |
14 | * You should have received a copy of the GNU General Public License along with |
15 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
16 | * Place - Suite 330, Boston, MA 02111-1307, USA. | |
fac6795d DG |
17 | */ |
18 | ||
19 | #define _GNU_SOURCE | |
1e307fab | 20 | #include <assert.h> |
fac6795d DG |
21 | #include <limits.h> |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
fac6795d DG |
25 | #include <sys/stat.h> |
26 | #include <sys/types.h> | |
27 | #include <sys/un.h> | |
28 | #include <unistd.h> | |
3bd1e081 | 29 | #include <errno.h> |
fac6795d | 30 | |
e88129fc | 31 | #include <lttng-sessiond-comm.h> |
fac6795d DG |
32 | |
33 | /* | |
34 | * Human readable error message. | |
35 | */ | |
36 | static const char *lttcomm_readable_code[] = { | |
37 | [ LTTCOMM_ERR_INDEX(LTTCOMM_OK) ] = "Success", | |
38 | [ LTTCOMM_ERR_INDEX(LTTCOMM_ERR) ] = "Unknown error", | |
39 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UND) ] = "Undefined command", | |
7d29a247 DG |
40 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NOT_IMPLEMENTED) ] = "Not implemented", |
41 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UNKNOWN_DOMAIN) ] = "Unknown tracing domain", | |
fac6795d DG |
42 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESSION) ] = "No session found", |
43 | [ LTTCOMM_ERR_INDEX(LTTCOMM_LIST_FAIL) ] = "Unable to list traceable apps", | |
e065084a | 44 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_APPS) ] = "No traceable apps found", |
f3ed775e | 45 | [ LTTCOMM_ERR_INDEX(LTTCOMM_SESS_NOT_FOUND) ] = "Session name not found", |
ce3d728c | 46 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACE) ] = "No trace found", |
ca95a216 | 47 | [ LTTCOMM_ERR_INDEX(LTTCOMM_FATAL) ] = "Fatal error of the session daemon", |
df0da139 | 48 | [ LTTCOMM_ERR_INDEX(LTTCOMM_CREATE_FAIL) ] = "Create trace failed", |
379473d2 | 49 | [ LTTCOMM_ERR_INDEX(LTTCOMM_START_FAIL) ] = "Start trace failed", |
520ff687 | 50 | [ LTTCOMM_ERR_INDEX(LTTCOMM_STOP_FAIL) ] = "Stop trace failed", |
379473d2 DG |
51 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACEABLE) ] = "App is not traceable", |
52 | [ LTTCOMM_ERR_INDEX(LTTCOMM_SELECT_SESS) ] = "A session MUST be selected", | |
27673bb6 | 53 | [ LTTCOMM_ERR_INDEX(LTTCOMM_EXIST_SESS) ] = "Session name already exist", |
d9800920 | 54 | [ LTTCOMM_ERR_INDEX(LTTCOMM_CONNECT_FAIL) ] = "Unable to connect to Unix socket", |
2b0bf864 | 55 | [ LTTCOMM_ERR_INDEX(LTTCOMM_APP_NOT_FOUND) ] = "Application not found", |
8e0af1b4 | 56 | [ LTTCOMM_ERR_INDEX(LTTCOMM_EPERM) ] = "Permission denied", |
20fe2104 | 57 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NA) ] = "Kernel tracer not available", |
7d29a247 | 58 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_EVENT_EXIST) ] = "Kernel event already exists", |
20fe2104 DG |
59 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_SESS_FAIL) ] = "Kernel create session failed", |
60 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_FAIL) ] = "Kernel create channel failed", | |
f3ed775e | 61 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_NOT_FOUND) ] = "Kernel channel not found", |
26cc6b4e | 62 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_DISABLE_FAIL) ] = "Disable kernel channel failed", |
d36b8583 | 63 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_ENABLE_FAIL) ] = "Enable kernel channel failed", |
d65106b1 | 64 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONTEXT_FAIL) ] = "Add kernel context failed", |
f34daff7 DG |
65 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_ENABLE_FAIL) ] = "Enable kernel event failed", |
66 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DISABLE_FAIL) ] = "Disable kernel event failed", | |
aaf26714 | 67 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_META_FAIL) ] = "Opening metadata failed", |
8c0faa1d DG |
68 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_START_FAIL) ] = "Starting kernel trace failed", |
69 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STOP_FAIL) ] = "Stoping kernel trace failed", | |
70 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONSUMER_FAIL) ] = "Kernel consumer start failed", | |
71 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STREAM_FAIL) ] = "Kernel create stream failed", | |
72 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_FAIL) ] = "Kernel trace directory creation failed", | |
73 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_EXIST) ] = "Kernel trace directory already exist", | |
84291629 | 74 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NO_SESSION) ] = "No kernel session found", |
2ef84c95 | 75 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_LIST_FAIL) ] = "Listing kernel events failed", |
0177d773 | 76 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_SESS_FAIL) ] = "UST create session failed", |
2b0bf864 | 77 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_FAIL) ] = "UST create channel failed", |
f6a9efaa | 78 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_EXIST) ] = "UST channel already exist", |
2bdd86d4 MD |
79 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_NOT_FOUND) ] = "UST channel not found", |
80 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_DISABLE_FAIL) ] = "Disable UST channel failed", | |
81 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_ENABLE_FAIL) ] = "Enable UST channel failed", | |
82 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_FAIL) ] = "Add UST context failed", | |
83 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_ENABLE_FAIL) ] = "Enable UST event failed", | |
84 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DISABLE_FAIL) ] = "Disable UST event failed", | |
85 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_META_FAIL) ] = "Opening metadata failed", | |
86 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_START_FAIL) ] = "Starting UST trace failed", | |
87 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_STOP_FAIL) ] = "Stoping UST trace failed", | |
7753dea8 MD |
88 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONSUMER64_FAIL) ] = "64-bit UST consumer start failed", |
89 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONSUMER32_FAIL) ] = "32-bit UST consumer start failed", | |
2bdd86d4 MD |
90 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_STREAM_FAIL) ] = "UST create stream failed", |
91 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DIR_FAIL) ] = "UST trace directory creation failed", | |
92 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DIR_EXIST) ] = "UST trace directory already exist", | |
93 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_NO_SESSION) ] = "No UST session found", | |
94 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_LIST_FAIL) ] = "Listing UST events failed", | |
3b25d04d | 95 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_EVENT_EXIST) ] = "UST event already exist", |
be4f0921 | 96 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_EVENT_NOT_FOUND)] = "UST event not found", |
ae357384 | 97 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_EXIST)] = "UST context already exist", |
3bd1e081 MD |
98 | [ LTTCOMM_ERR_INDEX(CONSUMERD_COMMAND_SOCK_READY) ] = "consumerd command socket ready", |
99 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SUCCESS_RECV_FD) ] = "consumerd success on receiving fds", | |
100 | [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_FD) ] = "consumerd error on receiving fds", | |
f2fc6720 | 101 | [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_CMD) ] = "consumerd error on receiving command", |
3bd1e081 MD |
102 | [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_ERROR) ] = "consumerd error in polling thread", |
103 | [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_NVAL) ] = "consumerd polling on closed fd", | |
104 | [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_HUP) ] = "consumerd all fd hung up", | |
105 | [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_SUCCESS) ] = "consumerd exiting normally", | |
106 | [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_FAILURE) ] = "consumerd exiting on error", | |
107 | [ LTTCOMM_ERR_INDEX(CONSUMERD_OUTFD_ERROR) ] = "consumerd error opening the tracefile", | |
108 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EBADF) ] = "consumerd splice EBADF", | |
109 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EINVAL) ] = "consumerd splice EINVAL", | |
110 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ENOMEM) ] = "consumerd splice ENOMEM", | |
111 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ESPIPE) ] = "consumerd splice ESPIPE", | |
d65106b1 | 112 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_EVENT) ] = "Event not found", |
fac6795d DG |
113 | }; |
114 | ||
115 | /* | |
917216f6 DG |
116 | * Return ptr to string representing a human readable error code from the |
117 | * lttcomm_return_code enum. | |
fac6795d | 118 | * |
917216f6 | 119 | * These code MUST be negative in other to treat that as an error value. |
fac6795d DG |
120 | */ |
121 | const char *lttcomm_get_readable_code(enum lttcomm_return_code code) | |
122 | { | |
123 | int tmp_code = -code; | |
124 | ||
125 | if (tmp_code >= LTTCOMM_OK && tmp_code < LTTCOMM_NR) { | |
126 | return lttcomm_readable_code[LTTCOMM_ERR_INDEX(tmp_code)]; | |
127 | } | |
128 | ||
129 | return "Unknown error code"; | |
130 | } | |
131 | ||
132 | /* | |
917216f6 | 133 | * Connect to unix socket using the path name. |
fac6795d DG |
134 | */ |
135 | int lttcomm_connect_unix_sock(const char *pathname) | |
136 | { | |
686204ab MD |
137 | struct sockaddr_un sun; |
138 | int fd; | |
a8afeb4a | 139 | int ret; |
fac6795d | 140 | |
686204ab | 141 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
fac6795d DG |
142 | if (fd < 0) { |
143 | perror("socket"); | |
a8afeb4a | 144 | ret = fd; |
fac6795d DG |
145 | goto error; |
146 | } | |
147 | ||
686204ab MD |
148 | memset(&sun, 0, sizeof(sun)); |
149 | sun.sun_family = AF_UNIX; | |
150 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); | |
99497cd0 | 151 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; |
fac6795d | 152 | |
686204ab MD |
153 | ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun)); |
154 | if (ret < 0) { | |
a8afeb4a | 155 | /* |
917216f6 DG |
156 | * Don't print message on connect error, because connect is used in |
157 | * normal execution to detect if sessiond is alive. | |
a8afeb4a MD |
158 | */ |
159 | goto error_connect; | |
686204ab | 160 | } |
fac6795d | 161 | |
686204ab | 162 | return fd; |
fac6795d | 163 | |
a8afeb4a MD |
164 | error_connect: |
165 | close(fd); | |
fac6795d | 166 | error: |
a8afeb4a | 167 | return ret; |
fac6795d DG |
168 | } |
169 | ||
170 | /* | |
917216f6 DG |
171 | * Do an accept(2) on the sock and return the new file descriptor. The socket |
172 | * MUST be bind(2) before. | |
fac6795d DG |
173 | */ |
174 | int lttcomm_accept_unix_sock(int sock) | |
175 | { | |
176 | int new_fd; | |
177 | struct sockaddr_un sun; | |
178 | socklen_t len = 0; | |
179 | ||
180 | /* Blocking call */ | |
181 | new_fd = accept(sock, (struct sockaddr *) &sun, &len); | |
182 | if (new_fd < 0) { | |
183 | perror("accept"); | |
fac6795d DG |
184 | } |
185 | ||
186 | return new_fd; | |
fac6795d DG |
187 | } |
188 | ||
189 | /* | |
917216f6 DG |
190 | * Creates a AF_UNIX local socket using pathname bind the socket upon creation |
191 | * and return the fd. | |
fac6795d DG |
192 | */ |
193 | int lttcomm_create_unix_sock(const char *pathname) | |
194 | { | |
195 | struct sockaddr_un sun; | |
196 | int fd; | |
197 | int ret = -1; | |
198 | ||
199 | /* Create server socket */ | |
200 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { | |
201 | perror("socket"); | |
202 | goto error; | |
203 | } | |
204 | ||
205 | memset(&sun, 0, sizeof(sun)); | |
206 | sun.sun_family = AF_UNIX; | |
99497cd0 MD |
207 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); |
208 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; | |
fac6795d | 209 | |
7d8234d9 MD |
210 | /* Unlink the old file if present */ |
211 | (void) unlink(pathname); | |
fac6795d DG |
212 | ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun)); |
213 | if (ret < 0) { | |
214 | perror("bind"); | |
215 | goto error; | |
216 | } | |
217 | ||
218 | return fd; | |
219 | ||
220 | error: | |
221 | return ret; | |
222 | } | |
223 | ||
224 | /* | |
f158a754 | 225 | * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN. |
fac6795d DG |
226 | */ |
227 | int lttcomm_listen_unix_sock(int sock) | |
228 | { | |
229 | int ret; | |
230 | ||
f158a754 | 231 | ret = listen(sock, LTTNG_SESSIOND_COMM_MAX_LISTEN); |
fac6795d DG |
232 | if (ret < 0) { |
233 | perror("listen"); | |
234 | } | |
235 | ||
236 | return ret; | |
237 | } | |
238 | ||
239 | /* | |
917216f6 DG |
240 | * Receive data of size len in put that data into the buf param. Using recvmsg |
241 | * API. | |
fac6795d | 242 | * |
917216f6 | 243 | * Return the size of received data. |
fac6795d DG |
244 | */ |
245 | ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len) | |
246 | { | |
159c7ff4 | 247 | struct msghdr msg = { 0 }; |
fac6795d DG |
248 | struct iovec iov[1]; |
249 | ssize_t ret = -1; | |
250 | ||
fac6795d DG |
251 | iov[0].iov_base = buf; |
252 | iov[0].iov_len = len; | |
253 | msg.msg_iov = iov; | |
254 | msg.msg_iovlen = 1; | |
255 | ||
256 | ret = recvmsg(sock, &msg, 0); | |
257 | if (ret < 0) { | |
258 | perror("recvmsg"); | |
259 | } | |
260 | ||
261 | return ret; | |
262 | } | |
263 | ||
264 | /* | |
917216f6 | 265 | * Send buf data of size len. Using sendmsg API. |
fac6795d | 266 | * |
917216f6 | 267 | * Return the size of sent data. |
fac6795d DG |
268 | */ |
269 | ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len) | |
270 | { | |
159c7ff4 | 271 | struct msghdr msg = { 0 }; |
fac6795d DG |
272 | struct iovec iov[1]; |
273 | ssize_t ret = -1; | |
274 | ||
fac6795d DG |
275 | iov[0].iov_base = buf; |
276 | iov[0].iov_len = len; | |
277 | msg.msg_iov = iov; | |
278 | msg.msg_iovlen = 1; | |
279 | ||
280 | ret = sendmsg(sock, &msg, 0); | |
281 | if (ret < 0) { | |
282 | perror("sendmsg"); | |
283 | } | |
284 | ||
285 | return ret; | |
286 | } | |
87378cf5 DG |
287 | |
288 | /* | |
917216f6 | 289 | * Shutdown cleanly a unix socket. |
87378cf5 DG |
290 | */ |
291 | int lttcomm_close_unix_sock(int sock) | |
292 | { | |
293 | int ret; | |
294 | ||
295 | /* Shutdown receptions and transmissions */ | |
296 | ret = shutdown(sock, SHUT_RDWR); | |
297 | if (ret < 0) { | |
298 | perror("shutdown"); | |
299 | } | |
300 | ||
301 | return ret; | |
302 | } | |
8c0faa1d DG |
303 | |
304 | /* | |
3bd1e081 MD |
305 | * Send a message accompanied by fd(s) over a unix socket. |
306 | * | |
307 | * Returns the size of data sent, or negative error value. | |
8c0faa1d | 308 | */ |
3bd1e081 | 309 | ssize_t lttcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
8c0faa1d | 310 | { |
159c7ff4 | 311 | struct msghdr msg = { 0 }; |
8c0faa1d DG |
312 | struct cmsghdr *cmptr; |
313 | struct iovec iov[1]; | |
314 | ssize_t ret = -1; | |
315 | unsigned int sizeof_fds = nb_fd * sizeof(int); | |
316 | char tmp[CMSG_SPACE(sizeof_fds)]; | |
3bd1e081 | 317 | char dummy = 0; |
8c0faa1d | 318 | |
3bd1e081 MD |
319 | if (nb_fd > LTTCOMM_MAX_SEND_FDS) |
320 | return -EINVAL; | |
8c0faa1d DG |
321 | |
322 | msg.msg_control = (caddr_t)tmp; | |
323 | msg.msg_controllen = CMSG_LEN(sizeof_fds); | |
324 | ||
325 | cmptr = CMSG_FIRSTHDR(&msg); | |
8c0faa1d DG |
326 | cmptr->cmsg_level = SOL_SOCKET; |
327 | cmptr->cmsg_type = SCM_RIGHTS; | |
159c7ff4 | 328 | cmptr->cmsg_len = CMSG_LEN(sizeof_fds); |
8c0faa1d | 329 | memcpy(CMSG_DATA(cmptr), fds, sizeof_fds); |
159c7ff4 MD |
330 | /* Sum of the length of all control messages in the buffer: */ |
331 | msg.msg_controllen = cmptr->cmsg_len; | |
8c0faa1d | 332 | |
3bd1e081 MD |
333 | iov[0].iov_base = &dummy; |
334 | iov[0].iov_len = 1; | |
8c0faa1d DG |
335 | msg.msg_iov = iov; |
336 | msg.msg_iovlen = 1; | |
337 | ||
338 | ret = sendmsg(sock, &msg, 0); | |
339 | if (ret < 0) { | |
340 | perror("sendmsg"); | |
341 | } | |
8c0faa1d DG |
342 | return ret; |
343 | } | |
09de5c68 DG |
344 | |
345 | /* | |
3bd1e081 | 346 | * Recv a message accompanied by fd(s) from a unix socket. |
09de5c68 | 347 | * |
3bd1e081 MD |
348 | * Returns the size of received data, or negative error value. |
349 | * | |
350 | * Expect at most "nb_fd" file descriptors. Returns the number of fd | |
351 | * actually received in nb_fd. | |
09de5c68 | 352 | */ |
3bd1e081 | 353 | ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
09de5c68 DG |
354 | { |
355 | struct iovec iov[1]; | |
3bd1e081 | 356 | ssize_t ret = 0; |
09de5c68 | 357 | struct cmsghdr *cmsg; |
3bd1e081 MD |
358 | size_t sizeof_fds = nb_fd * sizeof(int); |
359 | char recv_fd[CMSG_SPACE(sizeof_fds)]; | |
09de5c68 | 360 | struct msghdr msg = { 0 }; |
3bd1e081 | 361 | char dummy; |
09de5c68 DG |
362 | |
363 | /* Prepare to receive the structures */ | |
3bd1e081 MD |
364 | iov[0].iov_base = &dummy; |
365 | iov[0].iov_len = 1; | |
09de5c68 DG |
366 | msg.msg_iov = iov; |
367 | msg.msg_iovlen = 1; | |
368 | msg.msg_control = recv_fd; | |
369 | msg.msg_controllen = sizeof(recv_fd); | |
370 | ||
371 | ret = recvmsg(sock, &msg, 0); | |
372 | if (ret < 0) { | |
373 | perror("recvmsg fds"); | |
374 | goto end; | |
375 | } | |
3bd1e081 MD |
376 | if (ret != 1) { |
377 | fprintf(stderr, "Error: Received %zd bytes, expected %d\n", | |
378 | ret, 1); | |
379 | goto end; | |
380 | } | |
381 | if (msg.msg_flags & MSG_CTRUNC) { | |
382 | fprintf(stderr, "Error: Control message truncated.\n"); | |
383 | ret = -1; | |
09de5c68 DG |
384 | goto end; |
385 | } | |
09de5c68 DG |
386 | cmsg = CMSG_FIRSTHDR(&msg); |
387 | if (!cmsg) { | |
3bd1e081 | 388 | fprintf(stderr, "Error: Invalid control message header\n"); |
09de5c68 DG |
389 | ret = -1; |
390 | goto end; | |
391 | } | |
09de5c68 | 392 | if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { |
3bd1e081 | 393 | fprintf(stderr, "Didn't received any fd\n"); |
09de5c68 DG |
394 | ret = -1; |
395 | goto end; | |
396 | } | |
3bd1e081 MD |
397 | if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) { |
398 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", | |
399 | cmsg->cmsg_len, CMSG_LEN(sizeof_fds)); | |
400 | ret = -1; | |
401 | goto end; | |
09de5c68 | 402 | } |
3bd1e081 MD |
403 | memcpy(fds, CMSG_DATA(cmsg), sizeof_fds); |
404 | ret = sizeof_fds; | |
09de5c68 DG |
405 | end: |
406 | return ret; | |
407 | } | |
d19d9ada DG |
408 | |
409 | /* | |
410 | * Send a message with credentials over a unix socket. | |
411 | * | |
412 | * Returns the size of data sent, or negative error value. | |
413 | */ | |
414 | ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len) | |
415 | { | |
416 | struct msghdr msg = { 0 }; | |
417 | struct cmsghdr *cmptr; | |
418 | struct iovec iov[1]; | |
419 | ssize_t ret = -1; | |
420 | struct ucred *creds; | |
421 | size_t sizeof_cred = sizeof(struct ucred); | |
422 | char anc_buf[CMSG_SPACE(sizeof_cred)]; | |
423 | ||
424 | iov[0].iov_base = buf; | |
425 | iov[0].iov_len = len; | |
426 | msg.msg_iov = iov; | |
427 | msg.msg_iovlen = 1; | |
428 | ||
429 | msg.msg_control = (caddr_t) anc_buf; | |
430 | msg.msg_controllen = CMSG_LEN(sizeof_cred); | |
431 | ||
432 | cmptr = CMSG_FIRSTHDR(&msg); | |
433 | cmptr->cmsg_level = SOL_SOCKET; | |
434 | cmptr->cmsg_type = SCM_CREDENTIALS; | |
435 | cmptr->cmsg_len = CMSG_LEN(sizeof_cred); | |
436 | ||
437 | creds = (struct ucred *) CMSG_DATA(cmptr); | |
438 | ||
439 | creds->uid = geteuid(); | |
440 | creds->gid = getegid(); | |
441 | creds->pid = getpid(); | |
442 | ||
443 | ret = sendmsg(sock, &msg, 0); | |
444 | if (ret < 0) { | |
445 | perror("sendmsg"); | |
446 | } | |
447 | ||
448 | return ret; | |
449 | } | |
450 | ||
451 | /* | |
452 | * Recv a message accompanied with credentials from a unix socket. | |
453 | * | |
454 | * Returns the size of received data, or negative error value. | |
455 | */ | |
456 | ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, | |
457 | struct ucred *creds) | |
458 | { | |
459 | struct msghdr msg = { 0 }; | |
460 | struct cmsghdr *cmptr; | |
461 | struct iovec iov[1]; | |
462 | ssize_t ret; | |
463 | size_t sizeof_cred = sizeof(struct ucred); | |
464 | char anc_buf[CMSG_SPACE(sizeof_cred)]; | |
465 | ||
466 | /* Not allowed */ | |
467 | if (creds == NULL) { | |
468 | ret = -1; | |
469 | goto end; | |
470 | } | |
471 | ||
472 | /* Prepare to receive the structures */ | |
473 | iov[0].iov_base = buf; | |
474 | iov[0].iov_len = len; | |
475 | msg.msg_iov = iov; | |
476 | msg.msg_iovlen = 1; | |
477 | ||
478 | msg.msg_control = anc_buf; | |
479 | msg.msg_controllen = sizeof(anc_buf); | |
480 | ||
481 | ret = recvmsg(sock, &msg, 0); | |
482 | if (ret < 0) { | |
483 | perror("recvmsg fds"); | |
484 | goto end; | |
485 | } | |
486 | ||
487 | if (msg.msg_flags & MSG_CTRUNC) { | |
488 | fprintf(stderr, "Error: Control message truncated.\n"); | |
489 | ret = -1; | |
490 | goto end; | |
491 | } | |
492 | ||
493 | cmptr = CMSG_FIRSTHDR(&msg); | |
494 | if (cmptr == NULL) { | |
495 | fprintf(stderr, "Error: Invalid control message header\n"); | |
496 | ret = -1; | |
497 | goto end; | |
498 | } | |
499 | ||
500 | if (cmptr->cmsg_level != SOL_SOCKET || | |
501 | cmptr->cmsg_type != SCM_CREDENTIALS) { | |
502 | fprintf(stderr, "Didn't received any credentials\n"); | |
503 | ret = -1; | |
504 | goto end; | |
505 | } | |
506 | ||
507 | if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) { | |
508 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", | |
509 | cmptr->cmsg_len, CMSG_LEN(sizeof_cred)); | |
510 | ret = -1; | |
511 | goto end; | |
512 | } | |
513 | ||
514 | memcpy(creds, CMSG_DATA(cmptr), sizeof_cred); | |
515 | ||
516 | end: | |
517 | return ret; | |
518 | } | |
519 | ||
520 | /* | |
521 | * Set socket option to use credentials passing. | |
522 | */ | |
523 | int lttcomm_setsockopt_creds_unix_sock(int sock) | |
524 | { | |
525 | int ret, on = 1; | |
526 | ||
527 | /* Set socket for credentials retrieval */ | |
528 | ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); | |
529 | if (ret < 0) { | |
530 | perror("setsockopt creds unix sock"); | |
531 | } | |
532 | ||
533 | return ret; | |
534 | } |