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 | * |
d14d33bf AM |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License, version 2 only, | |
7 | * as published by the Free Software Foundation. | |
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 | * |
d14d33bf AM |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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> | |
fac6795d | 27 | #include <unistd.h> |
3bd1e081 | 28 | #include <errno.h> |
fac6795d | 29 | |
990570ed | 30 | #include <common/defaults.h> |
4c462e79 | 31 | #include <common/error.h> |
990570ed | 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 | 62 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_SESS_FAIL) ] = "Kernel create session failed", |
42224349 | 63 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_EXIST) ] = "Kernel channel already exists", |
20fe2104 | 64 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_FAIL) ] = "Kernel create channel failed", |
f3ed775e | 65 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_NOT_FOUND) ] = "Kernel channel not found", |
26cc6b4e | 66 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_DISABLE_FAIL) ] = "Disable kernel channel failed", |
d36b8583 | 67 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CHAN_ENABLE_FAIL) ] = "Enable kernel channel failed", |
d65106b1 | 68 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONTEXT_FAIL) ] = "Add kernel context failed", |
f34daff7 DG |
69 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_ENABLE_FAIL) ] = "Enable kernel event failed", |
70 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DISABLE_FAIL) ] = "Disable kernel event failed", | |
aaf26714 | 71 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_META_FAIL) ] = "Opening metadata failed", |
8c0faa1d DG |
72 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_START_FAIL) ] = "Starting kernel trace failed", |
73 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STOP_FAIL) ] = "Stoping kernel trace failed", | |
74 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_CONSUMER_FAIL) ] = "Kernel consumer start failed", | |
75 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_STREAM_FAIL) ] = "Kernel create stream failed", | |
76 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_FAIL) ] = "Kernel trace directory creation failed", | |
77 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_DIR_EXIST) ] = "Kernel trace directory already exist", | |
84291629 | 78 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_NO_SESSION) ] = "No kernel session found", |
2ef84c95 | 79 | [ LTTCOMM_ERR_INDEX(LTTCOMM_KERN_LIST_FAIL) ] = "Listing kernel events failed", |
4466912f | 80 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CALIBRATE_FAIL) ] = "UST calibration failed", |
e0c7ec2b | 81 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_VERSION) ] = "UST tracer version is not compatible", |
0177d773 | 82 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_SESS_FAIL) ] = "UST create session failed", |
2b0bf864 | 83 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_FAIL) ] = "UST create channel failed", |
f6a9efaa | 84 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_EXIST) ] = "UST channel already exist", |
2bdd86d4 MD |
85 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_NOT_FOUND) ] = "UST channel not found", |
86 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_DISABLE_FAIL) ] = "Disable UST channel failed", | |
87 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_ENABLE_FAIL) ] = "Enable UST channel failed", | |
88 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_FAIL) ] = "Add UST context failed", | |
89 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_ENABLE_FAIL) ] = "Enable UST event failed", | |
90 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DISABLE_FAIL) ] = "Disable UST event failed", | |
91 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_META_FAIL) ] = "Opening metadata failed", | |
92 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_START_FAIL) ] = "Starting UST trace failed", | |
93 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_STOP_FAIL) ] = "Stoping UST trace failed", | |
7753dea8 MD |
94 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONSUMER64_FAIL) ] = "64-bit UST consumer start failed", |
95 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONSUMER32_FAIL) ] = "32-bit UST consumer start failed", | |
2bdd86d4 MD |
96 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_STREAM_FAIL) ] = "UST create stream failed", |
97 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DIR_FAIL) ] = "UST trace directory creation failed", | |
98 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_DIR_EXIST) ] = "UST trace directory already exist", | |
99 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_NO_SESSION) ] = "No UST session found", | |
100 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_LIST_FAIL) ] = "Listing UST events failed", | |
3b25d04d | 101 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_EVENT_EXIST) ] = "UST event already exist", |
be4f0921 | 102 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_EVENT_NOT_FOUND)] = "UST event not found", |
ae357384 | 103 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_EXIST)] = "UST context already exist", |
ae856491 | 104 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CONTEXT_INVAL)] = "UST invalid context", |
30905cf4 | 105 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NEED_ROOT_SESSIOND) ] = "Tracing the kernel requires a root lttng-sessiond daemon and \"tracing\" group user membership", |
80e327fa DG |
106 | [ LTTCOMM_ERR_INDEX(LTTCOMM_TRACE_ALREADY_STARTED) ] = "Tracing already started", |
107 | [ LTTCOMM_ERR_INDEX(LTTCOMM_TRACE_ALREADY_STOPPED) ] = "Tracing already stopped", | |
108 | ||
3bd1e081 MD |
109 | [ LTTCOMM_ERR_INDEX(CONSUMERD_COMMAND_SOCK_READY) ] = "consumerd command socket ready", |
110 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SUCCESS_RECV_FD) ] = "consumerd success on receiving fds", | |
111 | [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_FD) ] = "consumerd error on receiving fds", | |
f2fc6720 | 112 | [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_CMD) ] = "consumerd error on receiving command", |
3bd1e081 MD |
113 | [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_ERROR) ] = "consumerd error in polling thread", |
114 | [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_NVAL) ] = "consumerd polling on closed fd", | |
115 | [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_HUP) ] = "consumerd all fd hung up", | |
116 | [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_SUCCESS) ] = "consumerd exiting normally", | |
117 | [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_FAILURE) ] = "consumerd exiting on error", | |
118 | [ LTTCOMM_ERR_INDEX(CONSUMERD_OUTFD_ERROR) ] = "consumerd error opening the tracefile", | |
119 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EBADF) ] = "consumerd splice EBADF", | |
120 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EINVAL) ] = "consumerd splice EINVAL", | |
121 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ENOMEM) ] = "consumerd splice ENOMEM", | |
122 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ESPIPE) ] = "consumerd splice ESPIPE", | |
d65106b1 | 123 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_EVENT) ] = "Event not found", |
b024d072 | 124 | [ LTTCOMM_ERR_INDEX(LTTCOMM_INVALID) ] = "Invalid parameter", |
fac6795d DG |
125 | }; |
126 | ||
127 | /* | |
917216f6 DG |
128 | * Return ptr to string representing a human readable error code from the |
129 | * lttcomm_return_code enum. | |
fac6795d | 130 | * |
917216f6 | 131 | * These code MUST be negative in other to treat that as an error value. |
fac6795d DG |
132 | */ |
133 | const char *lttcomm_get_readable_code(enum lttcomm_return_code code) | |
134 | { | |
135 | int tmp_code = -code; | |
136 | ||
137 | if (tmp_code >= LTTCOMM_OK && tmp_code < LTTCOMM_NR) { | |
138 | return lttcomm_readable_code[LTTCOMM_ERR_INDEX(tmp_code)]; | |
139 | } | |
140 | ||
141 | return "Unknown error code"; | |
142 | } | |
143 | ||
144 | /* | |
917216f6 | 145 | * Connect to unix socket using the path name. |
fac6795d DG |
146 | */ |
147 | int lttcomm_connect_unix_sock(const char *pathname) | |
148 | { | |
686204ab | 149 | struct sockaddr_un sun; |
4c462e79 | 150 | int fd, ret, closeret; |
fac6795d | 151 | |
686204ab | 152 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
fac6795d | 153 | if (fd < 0) { |
4c462e79 | 154 | PERROR("socket"); |
a8afeb4a | 155 | ret = fd; |
fac6795d DG |
156 | goto error; |
157 | } | |
158 | ||
686204ab MD |
159 | memset(&sun, 0, sizeof(sun)); |
160 | sun.sun_family = AF_UNIX; | |
161 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); | |
99497cd0 | 162 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; |
fac6795d | 163 | |
686204ab MD |
164 | ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun)); |
165 | if (ret < 0) { | |
a8afeb4a | 166 | /* |
917216f6 DG |
167 | * Don't print message on connect error, because connect is used in |
168 | * normal execution to detect if sessiond is alive. | |
a8afeb4a MD |
169 | */ |
170 | goto error_connect; | |
686204ab | 171 | } |
fac6795d | 172 | |
686204ab | 173 | return fd; |
fac6795d | 174 | |
a8afeb4a | 175 | error_connect: |
4c462e79 MD |
176 | closeret = close(fd); |
177 | if (closeret) { | |
178 | PERROR("close"); | |
179 | } | |
fac6795d | 180 | error: |
a8afeb4a | 181 | return ret; |
fac6795d DG |
182 | } |
183 | ||
184 | /* | |
917216f6 DG |
185 | * Do an accept(2) on the sock and return the new file descriptor. The socket |
186 | * MUST be bind(2) before. | |
fac6795d DG |
187 | */ |
188 | int lttcomm_accept_unix_sock(int sock) | |
189 | { | |
190 | int new_fd; | |
191 | struct sockaddr_un sun; | |
192 | socklen_t len = 0; | |
193 | ||
194 | /* Blocking call */ | |
195 | new_fd = accept(sock, (struct sockaddr *) &sun, &len); | |
196 | if (new_fd < 0) { | |
4c462e79 | 197 | PERROR("accept"); |
fac6795d DG |
198 | } |
199 | ||
200 | return new_fd; | |
fac6795d DG |
201 | } |
202 | ||
203 | /* | |
917216f6 DG |
204 | * Creates a AF_UNIX local socket using pathname bind the socket upon creation |
205 | * and return the fd. | |
fac6795d DG |
206 | */ |
207 | int lttcomm_create_unix_sock(const char *pathname) | |
208 | { | |
209 | struct sockaddr_un sun; | |
210 | int fd; | |
211 | int ret = -1; | |
212 | ||
213 | /* Create server socket */ | |
214 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { | |
4c462e79 | 215 | PERROR("socket"); |
fac6795d DG |
216 | goto error; |
217 | } | |
218 | ||
219 | memset(&sun, 0, sizeof(sun)); | |
220 | sun.sun_family = AF_UNIX; | |
99497cd0 MD |
221 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); |
222 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; | |
fac6795d | 223 | |
7d8234d9 MD |
224 | /* Unlink the old file if present */ |
225 | (void) unlink(pathname); | |
fac6795d DG |
226 | ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun)); |
227 | if (ret < 0) { | |
4c462e79 | 228 | PERROR("bind"); |
fac6795d DG |
229 | goto error; |
230 | } | |
231 | ||
232 | return fd; | |
233 | ||
234 | error: | |
235 | return ret; | |
236 | } | |
237 | ||
238 | /* | |
f158a754 | 239 | * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN. |
fac6795d DG |
240 | */ |
241 | int lttcomm_listen_unix_sock(int sock) | |
242 | { | |
243 | int ret; | |
244 | ||
f158a754 | 245 | ret = listen(sock, LTTNG_SESSIOND_COMM_MAX_LISTEN); |
fac6795d | 246 | if (ret < 0) { |
4c462e79 | 247 | PERROR("listen"); |
fac6795d DG |
248 | } |
249 | ||
250 | return ret; | |
251 | } | |
252 | ||
253 | /* | |
917216f6 DG |
254 | * Receive data of size len in put that data into the buf param. Using recvmsg |
255 | * API. | |
fac6795d | 256 | * |
917216f6 | 257 | * Return the size of received data. |
fac6795d DG |
258 | */ |
259 | ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len) | |
260 | { | |
156ea65a | 261 | struct msghdr msg; |
fac6795d DG |
262 | struct iovec iov[1]; |
263 | ssize_t ret = -1; | |
264 | ||
156ea65a MD |
265 | memset(&msg, 0, sizeof(msg)); |
266 | ||
fac6795d DG |
267 | iov[0].iov_base = buf; |
268 | iov[0].iov_len = len; | |
269 | msg.msg_iov = iov; | |
270 | msg.msg_iovlen = 1; | |
271 | ||
91fe73e2 MD |
272 | do { |
273 | ret = recvmsg(sock, &msg, MSG_WAITALL); | |
274 | } while (ret < 0 && errno == EINTR); | |
fac6795d | 275 | if (ret < 0) { |
4c462e79 | 276 | PERROR("recvmsg"); |
fac6795d DG |
277 | } |
278 | ||
279 | return ret; | |
280 | } | |
281 | ||
282 | /* | |
917216f6 | 283 | * Send buf data of size len. Using sendmsg API. |
fac6795d | 284 | * |
917216f6 | 285 | * Return the size of sent data. |
fac6795d DG |
286 | */ |
287 | ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len) | |
288 | { | |
156ea65a | 289 | struct msghdr msg; |
fac6795d DG |
290 | struct iovec iov[1]; |
291 | ssize_t ret = -1; | |
292 | ||
156ea65a MD |
293 | memset(&msg, 0, sizeof(msg)); |
294 | ||
fac6795d DG |
295 | iov[0].iov_base = buf; |
296 | iov[0].iov_len = len; | |
297 | msg.msg_iov = iov; | |
298 | msg.msg_iovlen = 1; | |
299 | ||
300 | ret = sendmsg(sock, &msg, 0); | |
301 | if (ret < 0) { | |
d2641a83 MD |
302 | /* |
303 | * Only warn about EPIPE when quiet mode is deactivated. | |
304 | * We consider EPIPE as expected. | |
305 | */ | |
97e19046 | 306 | if (errno != EPIPE || !lttng_opt_quiet) { |
d2641a83 MD |
307 | PERROR("sendmsg"); |
308 | } | |
fac6795d DG |
309 | } |
310 | ||
311 | return ret; | |
312 | } | |
87378cf5 DG |
313 | |
314 | /* | |
917216f6 | 315 | * Shutdown cleanly a unix socket. |
87378cf5 DG |
316 | */ |
317 | int lttcomm_close_unix_sock(int sock) | |
318 | { | |
4c462e79 | 319 | int ret, closeret; |
87378cf5 DG |
320 | |
321 | /* Shutdown receptions and transmissions */ | |
322 | ret = shutdown(sock, SHUT_RDWR); | |
323 | if (ret < 0) { | |
4c462e79 | 324 | PERROR("shutdown"); |
87378cf5 DG |
325 | } |
326 | ||
4c462e79 MD |
327 | closeret = close(sock); |
328 | if (closeret) { | |
329 | PERROR("close"); | |
330 | } | |
b0ef677e | 331 | |
87378cf5 DG |
332 | return ret; |
333 | } | |
8c0faa1d DG |
334 | |
335 | /* | |
3bd1e081 MD |
336 | * Send a message accompanied by fd(s) over a unix socket. |
337 | * | |
338 | * Returns the size of data sent, or negative error value. | |
8c0faa1d | 339 | */ |
3bd1e081 | 340 | ssize_t lttcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
8c0faa1d | 341 | { |
156ea65a | 342 | struct msghdr msg; |
8c0faa1d DG |
343 | struct cmsghdr *cmptr; |
344 | struct iovec iov[1]; | |
345 | ssize_t ret = -1; | |
346 | unsigned int sizeof_fds = nb_fd * sizeof(int); | |
347 | char tmp[CMSG_SPACE(sizeof_fds)]; | |
3bd1e081 | 348 | char dummy = 0; |
8c0faa1d | 349 | |
156ea65a MD |
350 | memset(&msg, 0, sizeof(msg)); |
351 | ||
3bd1e081 MD |
352 | if (nb_fd > LTTCOMM_MAX_SEND_FDS) |
353 | return -EINVAL; | |
8c0faa1d DG |
354 | |
355 | msg.msg_control = (caddr_t)tmp; | |
356 | msg.msg_controllen = CMSG_LEN(sizeof_fds); | |
357 | ||
358 | cmptr = CMSG_FIRSTHDR(&msg); | |
8c0faa1d DG |
359 | cmptr->cmsg_level = SOL_SOCKET; |
360 | cmptr->cmsg_type = SCM_RIGHTS; | |
159c7ff4 | 361 | cmptr->cmsg_len = CMSG_LEN(sizeof_fds); |
8c0faa1d | 362 | memcpy(CMSG_DATA(cmptr), fds, sizeof_fds); |
159c7ff4 MD |
363 | /* Sum of the length of all control messages in the buffer: */ |
364 | msg.msg_controllen = cmptr->cmsg_len; | |
8c0faa1d | 365 | |
3bd1e081 MD |
366 | iov[0].iov_base = &dummy; |
367 | iov[0].iov_len = 1; | |
8c0faa1d DG |
368 | msg.msg_iov = iov; |
369 | msg.msg_iovlen = 1; | |
370 | ||
371 | ret = sendmsg(sock, &msg, 0); | |
372 | if (ret < 0) { | |
d2641a83 MD |
373 | /* |
374 | * Only warn about EPIPE when quiet mode is deactivated. | |
375 | * We consider EPIPE as expected. | |
376 | */ | |
97e19046 | 377 | if (errno != EPIPE || !lttng_opt_quiet) { |
d2641a83 MD |
378 | PERROR("sendmsg"); |
379 | } | |
8c0faa1d | 380 | } |
8c0faa1d DG |
381 | return ret; |
382 | } | |
09de5c68 DG |
383 | |
384 | /* | |
3bd1e081 | 385 | * Recv a message accompanied by fd(s) from a unix socket. |
09de5c68 | 386 | * |
3bd1e081 MD |
387 | * Returns the size of received data, or negative error value. |
388 | * | |
389 | * Expect at most "nb_fd" file descriptors. Returns the number of fd | |
390 | * actually received in nb_fd. | |
09de5c68 | 391 | */ |
3bd1e081 | 392 | ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
09de5c68 DG |
393 | { |
394 | struct iovec iov[1]; | |
3bd1e081 | 395 | ssize_t ret = 0; |
09de5c68 | 396 | struct cmsghdr *cmsg; |
3bd1e081 MD |
397 | size_t sizeof_fds = nb_fd * sizeof(int); |
398 | char recv_fd[CMSG_SPACE(sizeof_fds)]; | |
156ea65a | 399 | struct msghdr msg; |
3bd1e081 | 400 | char dummy; |
09de5c68 | 401 | |
156ea65a MD |
402 | memset(&msg, 0, sizeof(msg)); |
403 | ||
09de5c68 | 404 | /* Prepare to receive the structures */ |
3bd1e081 MD |
405 | iov[0].iov_base = &dummy; |
406 | iov[0].iov_len = 1; | |
09de5c68 DG |
407 | msg.msg_iov = iov; |
408 | msg.msg_iovlen = 1; | |
409 | msg.msg_control = recv_fd; | |
410 | msg.msg_controllen = sizeof(recv_fd); | |
411 | ||
91fe73e2 MD |
412 | do { |
413 | ret = recvmsg(sock, &msg, 0); | |
414 | } while (ret < 0 && errno == EINTR); | |
09de5c68 | 415 | if (ret < 0) { |
4c462e79 | 416 | PERROR("recvmsg fds"); |
09de5c68 DG |
417 | goto end; |
418 | } | |
3bd1e081 MD |
419 | if (ret != 1) { |
420 | fprintf(stderr, "Error: Received %zd bytes, expected %d\n", | |
421 | ret, 1); | |
422 | goto end; | |
423 | } | |
424 | if (msg.msg_flags & MSG_CTRUNC) { | |
425 | fprintf(stderr, "Error: Control message truncated.\n"); | |
426 | ret = -1; | |
09de5c68 DG |
427 | goto end; |
428 | } | |
09de5c68 DG |
429 | cmsg = CMSG_FIRSTHDR(&msg); |
430 | if (!cmsg) { | |
3bd1e081 | 431 | fprintf(stderr, "Error: Invalid control message header\n"); |
09de5c68 DG |
432 | ret = -1; |
433 | goto end; | |
434 | } | |
09de5c68 | 435 | if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { |
3bd1e081 | 436 | fprintf(stderr, "Didn't received any fd\n"); |
09de5c68 DG |
437 | ret = -1; |
438 | goto end; | |
439 | } | |
3bd1e081 MD |
440 | if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) { |
441 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", | |
b17231c6 | 442 | (size_t) cmsg->cmsg_len, (size_t) CMSG_LEN(sizeof_fds)); |
3bd1e081 MD |
443 | ret = -1; |
444 | goto end; | |
09de5c68 | 445 | } |
3bd1e081 MD |
446 | memcpy(fds, CMSG_DATA(cmsg), sizeof_fds); |
447 | ret = sizeof_fds; | |
09de5c68 DG |
448 | end: |
449 | return ret; | |
450 | } | |
d19d9ada DG |
451 | |
452 | /* | |
453 | * Send a message with credentials over a unix socket. | |
454 | * | |
455 | * Returns the size of data sent, or negative error value. | |
456 | */ | |
457 | ssize_t lttcomm_send_creds_unix_sock(int sock, void *buf, size_t len) | |
458 | { | |
156ea65a | 459 | struct msghdr msg; |
d19d9ada DG |
460 | struct iovec iov[1]; |
461 | ssize_t ret = -1; | |
d27c42b8 MD |
462 | #ifdef __linux__ |
463 | struct cmsghdr *cmptr; | |
b17231c6 | 464 | size_t sizeof_cred = sizeof(lttng_sock_cred); |
d19d9ada | 465 | char anc_buf[CMSG_SPACE(sizeof_cred)]; |
b17231c6 | 466 | lttng_sock_cred *creds; |
d27c42b8 | 467 | #endif /* __linux__ */ |
d19d9ada | 468 | |
156ea65a MD |
469 | memset(&msg, 0, sizeof(msg)); |
470 | ||
d19d9ada DG |
471 | iov[0].iov_base = buf; |
472 | iov[0].iov_len = len; | |
473 | msg.msg_iov = iov; | |
474 | msg.msg_iovlen = 1; | |
475 | ||
d27c42b8 | 476 | #ifdef __linux__ |
d19d9ada DG |
477 | msg.msg_control = (caddr_t) anc_buf; |
478 | msg.msg_controllen = CMSG_LEN(sizeof_cred); | |
479 | ||
480 | cmptr = CMSG_FIRSTHDR(&msg); | |
481 | cmptr->cmsg_level = SOL_SOCKET; | |
b17231c6 | 482 | cmptr->cmsg_type = LTTNG_SOCK_CREDS; |
d19d9ada DG |
483 | cmptr->cmsg_len = CMSG_LEN(sizeof_cred); |
484 | ||
b17231c6 | 485 | creds = (lttng_sock_cred*) CMSG_DATA(cmptr); |
d19d9ada | 486 | |
b17231c6 DG |
487 | LTTNG_SOCK_SET_UID_CRED(creds, geteuid()); |
488 | LTTNG_SOCK_SET_GID_CRED(creds, getegid()); | |
489 | LTTNG_SOCK_SET_PID_CRED(creds, getpid()); | |
d27c42b8 | 490 | #endif /* __linux__ */ |
d19d9ada DG |
491 | |
492 | ret = sendmsg(sock, &msg, 0); | |
493 | if (ret < 0) { | |
d2641a83 MD |
494 | /* |
495 | * Only warn about EPIPE when quiet mode is deactivated. | |
496 | * We consider EPIPE as expected. | |
497 | */ | |
97e19046 | 498 | if (errno != EPIPE || !lttng_opt_quiet) { |
d2641a83 MD |
499 | PERROR("sendmsg"); |
500 | } | |
d19d9ada | 501 | } |
d19d9ada DG |
502 | return ret; |
503 | } | |
504 | ||
505 | /* | |
506 | * Recv a message accompanied with credentials from a unix socket. | |
507 | * | |
508 | * Returns the size of received data, or negative error value. | |
509 | */ | |
510 | ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, | |
b17231c6 | 511 | lttng_sock_cred *creds) |
d19d9ada | 512 | { |
156ea65a | 513 | struct msghdr msg; |
d19d9ada DG |
514 | struct iovec iov[1]; |
515 | ssize_t ret; | |
d27c42b8 MD |
516 | #ifdef __linux__ |
517 | struct cmsghdr *cmptr; | |
b17231c6 | 518 | size_t sizeof_cred = sizeof(lttng_sock_cred); |
d19d9ada | 519 | char anc_buf[CMSG_SPACE(sizeof_cred)]; |
d27c42b8 | 520 | #endif /* __linux__ */ |
d19d9ada | 521 | |
156ea65a MD |
522 | memset(&msg, 0, sizeof(msg)); |
523 | ||
d19d9ada DG |
524 | /* Not allowed */ |
525 | if (creds == NULL) { | |
526 | ret = -1; | |
527 | goto end; | |
528 | } | |
529 | ||
530 | /* Prepare to receive the structures */ | |
531 | iov[0].iov_base = buf; | |
532 | iov[0].iov_len = len; | |
533 | msg.msg_iov = iov; | |
534 | msg.msg_iovlen = 1; | |
535 | ||
d27c42b8 | 536 | #ifdef __linux__ |
d19d9ada DG |
537 | msg.msg_control = anc_buf; |
538 | msg.msg_controllen = sizeof(anc_buf); | |
d27c42b8 | 539 | #endif /* __linux__ */ |
d19d9ada | 540 | |
91fe73e2 MD |
541 | do { |
542 | ret = recvmsg(sock, &msg, 0); | |
543 | } while (ret < 0 && errno == EINTR); | |
d19d9ada | 544 | if (ret < 0) { |
4c462e79 | 545 | PERROR("recvmsg fds"); |
d19d9ada DG |
546 | goto end; |
547 | } | |
548 | ||
d27c42b8 | 549 | #ifdef __linux__ |
d19d9ada DG |
550 | if (msg.msg_flags & MSG_CTRUNC) { |
551 | fprintf(stderr, "Error: Control message truncated.\n"); | |
552 | ret = -1; | |
553 | goto end; | |
554 | } | |
555 | ||
556 | cmptr = CMSG_FIRSTHDR(&msg); | |
557 | if (cmptr == NULL) { | |
558 | fprintf(stderr, "Error: Invalid control message header\n"); | |
559 | ret = -1; | |
560 | goto end; | |
561 | } | |
562 | ||
563 | if (cmptr->cmsg_level != SOL_SOCKET || | |
b17231c6 | 564 | cmptr->cmsg_type != LTTNG_SOCK_CREDS) { |
d19d9ada DG |
565 | fprintf(stderr, "Didn't received any credentials\n"); |
566 | ret = -1; | |
567 | goto end; | |
568 | } | |
569 | ||
570 | if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) { | |
571 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", | |
b17231c6 | 572 | (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred)); |
d19d9ada DG |
573 | ret = -1; |
574 | goto end; | |
575 | } | |
576 | ||
577 | memcpy(creds, CMSG_DATA(cmptr), sizeof_cred); | |
d27c42b8 | 578 | #elif defined(__FreeBSD__) |
9e89c931 MD |
579 | { |
580 | int peer_ret; | |
581 | ||
582 | peer_ret = getpeereid(sock, &creds->uid, &creds->gid); | |
583 | if (peer_ret != 0) { | |
584 | return peer_ret; | |
585 | } | |
67cf0ab7 | 586 | } |
d27c42b8 MD |
587 | #else |
588 | #error "Please implement credential support for your OS." | |
589 | #endif /* __linux__ */ | |
d19d9ada DG |
590 | |
591 | end: | |
592 | return ret; | |
593 | } | |
594 | ||
595 | /* | |
596 | * Set socket option to use credentials passing. | |
597 | */ | |
d27c42b8 | 598 | #ifdef __linux__ |
d19d9ada DG |
599 | int lttcomm_setsockopt_creds_unix_sock(int sock) |
600 | { | |
601 | int ret, on = 1; | |
602 | ||
603 | /* Set socket for credentials retrieval */ | |
604 | ret = setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); | |
605 | if (ret < 0) { | |
4c462e79 | 606 | PERROR("setsockopt creds unix sock"); |
d19d9ada | 607 | } |
d19d9ada DG |
608 | return ret; |
609 | } | |
d27c42b8 MD |
610 | #elif defined(__FreeBSD__) |
611 | int lttcomm_setsockopt_creds_unix_sock(int sock) | |
612 | { | |
613 | return 0; | |
614 | } | |
615 | #else | |
616 | #error "Please implement credential support for your OS." | |
617 | #endif /* __linux__ */ |