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