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> | |
25 | #include <sys/socket.h> | |
26 | #include <sys/stat.h> | |
27 | #include <sys/types.h> | |
28 | #include <sys/un.h> | |
29 | #include <unistd.h> | |
3bd1e081 | 30 | #include <errno.h> |
fac6795d | 31 | |
e88129fc | 32 | #include <lttng-sessiond-comm.h> |
fac6795d DG |
33 | |
34 | /* | |
35 | * Human readable error message. | |
36 | */ | |
37 | static const char *lttcomm_readable_code[] = { | |
38 | [ LTTCOMM_ERR_INDEX(LTTCOMM_OK) ] = "Success", | |
39 | [ LTTCOMM_ERR_INDEX(LTTCOMM_ERR) ] = "Unknown error", | |
40 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UND) ] = "Undefined command", | |
7d29a247 DG |
41 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NOT_IMPLEMENTED) ] = "Not implemented", |
42 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UNKNOWN_DOMAIN) ] = "Unknown tracing domain", | |
fac6795d DG |
43 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_SESSION) ] = "No session found", |
44 | [ LTTCOMM_ERR_INDEX(LTTCOMM_LIST_FAIL) ] = "Unable to list traceable apps", | |
e065084a | 45 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_APPS) ] = "No traceable apps found", |
f3ed775e | 46 | [ LTTCOMM_ERR_INDEX(LTTCOMM_SESS_NOT_FOUND) ] = "Session name not found", |
ce3d728c | 47 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACE) ] = "No trace found", |
ca95a216 | 48 | [ LTTCOMM_ERR_INDEX(LTTCOMM_FATAL) ] = "Fatal error of the session daemon", |
df0da139 | 49 | [ LTTCOMM_ERR_INDEX(LTTCOMM_CREATE_FAIL) ] = "Create trace failed", |
379473d2 | 50 | [ LTTCOMM_ERR_INDEX(LTTCOMM_START_FAIL) ] = "Start trace failed", |
520ff687 | 51 | [ LTTCOMM_ERR_INDEX(LTTCOMM_STOP_FAIL) ] = "Stop trace failed", |
379473d2 DG |
52 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_TRACEABLE) ] = "App is not traceable", |
53 | [ LTTCOMM_ERR_INDEX(LTTCOMM_SELECT_SESS) ] = "A session MUST be selected", | |
27673bb6 | 54 | [ LTTCOMM_ERR_INDEX(LTTCOMM_EXIST_SESS) ] = "Session name already exist", |
d9800920 | 55 | [ LTTCOMM_ERR_INDEX(LTTCOMM_CONNECT_FAIL) ] = "Unable to connect to Unix socket", |
2b0bf864 | 56 | [ LTTCOMM_ERR_INDEX(LTTCOMM_APP_NOT_FOUND) ] = "Application not found", |
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 DG |
77 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_NOT_FOUND) ] = "UST channel not found", |
78 | [ LTTCOMM_ERR_INDEX(LTTCOMM_UST_CHAN_FAIL) ] = "UST create channel failed", | |
3bd1e081 MD |
79 | [ LTTCOMM_ERR_INDEX(CONSUMERD_COMMAND_SOCK_READY) ] = "consumerd command socket ready", |
80 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SUCCESS_RECV_FD) ] = "consumerd success on receiving fds", | |
81 | [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_FD) ] = "consumerd error on receiving fds", | |
f2fc6720 | 82 | [ LTTCOMM_ERR_INDEX(CONSUMERD_ERROR_RECV_CMD) ] = "consumerd error on receiving command", |
3bd1e081 MD |
83 | [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_ERROR) ] = "consumerd error in polling thread", |
84 | [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_NVAL) ] = "consumerd polling on closed fd", | |
85 | [ LTTCOMM_ERR_INDEX(CONSUMERD_POLL_HUP) ] = "consumerd all fd hung up", | |
86 | [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_SUCCESS) ] = "consumerd exiting normally", | |
87 | [ LTTCOMM_ERR_INDEX(CONSUMERD_EXIT_FAILURE) ] = "consumerd exiting on error", | |
88 | [ LTTCOMM_ERR_INDEX(CONSUMERD_OUTFD_ERROR) ] = "consumerd error opening the tracefile", | |
89 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EBADF) ] = "consumerd splice EBADF", | |
90 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_EINVAL) ] = "consumerd splice EINVAL", | |
91 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ENOMEM) ] = "consumerd splice ENOMEM", | |
92 | [ LTTCOMM_ERR_INDEX(CONSUMERD_SPLICE_ESPIPE) ] = "consumerd splice ESPIPE", | |
d65106b1 | 93 | [ LTTCOMM_ERR_INDEX(LTTCOMM_NO_EVENT) ] = "Event not found", |
fac6795d DG |
94 | }; |
95 | ||
96 | /* | |
917216f6 DG |
97 | * Return ptr to string representing a human readable error code from the |
98 | * lttcomm_return_code enum. | |
fac6795d | 99 | * |
917216f6 | 100 | * These code MUST be negative in other to treat that as an error value. |
fac6795d DG |
101 | */ |
102 | const char *lttcomm_get_readable_code(enum lttcomm_return_code code) | |
103 | { | |
104 | int tmp_code = -code; | |
105 | ||
106 | if (tmp_code >= LTTCOMM_OK && tmp_code < LTTCOMM_NR) { | |
107 | return lttcomm_readable_code[LTTCOMM_ERR_INDEX(tmp_code)]; | |
108 | } | |
109 | ||
110 | return "Unknown error code"; | |
111 | } | |
112 | ||
113 | /* | |
917216f6 | 114 | * Connect to unix socket using the path name. |
fac6795d DG |
115 | */ |
116 | int lttcomm_connect_unix_sock(const char *pathname) | |
117 | { | |
686204ab MD |
118 | struct sockaddr_un sun; |
119 | int fd; | |
a8afeb4a | 120 | int ret; |
fac6795d | 121 | |
686204ab | 122 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
fac6795d DG |
123 | if (fd < 0) { |
124 | perror("socket"); | |
a8afeb4a | 125 | ret = fd; |
fac6795d DG |
126 | goto error; |
127 | } | |
128 | ||
686204ab MD |
129 | memset(&sun, 0, sizeof(sun)); |
130 | sun.sun_family = AF_UNIX; | |
131 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); | |
99497cd0 | 132 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; |
fac6795d | 133 | |
686204ab MD |
134 | ret = connect(fd, (struct sockaddr *) &sun, sizeof(sun)); |
135 | if (ret < 0) { | |
a8afeb4a | 136 | /* |
917216f6 DG |
137 | * Don't print message on connect error, because connect is used in |
138 | * normal execution to detect if sessiond is alive. | |
a8afeb4a MD |
139 | */ |
140 | goto error_connect; | |
686204ab | 141 | } |
fac6795d | 142 | |
686204ab | 143 | return fd; |
fac6795d | 144 | |
a8afeb4a MD |
145 | error_connect: |
146 | close(fd); | |
fac6795d | 147 | error: |
a8afeb4a | 148 | return ret; |
fac6795d DG |
149 | } |
150 | ||
151 | /* | |
917216f6 DG |
152 | * Do an accept(2) on the sock and return the new file descriptor. The socket |
153 | * MUST be bind(2) before. | |
fac6795d DG |
154 | */ |
155 | int lttcomm_accept_unix_sock(int sock) | |
156 | { | |
157 | int new_fd; | |
158 | struct sockaddr_un sun; | |
159 | socklen_t len = 0; | |
160 | ||
161 | /* Blocking call */ | |
162 | new_fd = accept(sock, (struct sockaddr *) &sun, &len); | |
163 | if (new_fd < 0) { | |
164 | perror("accept"); | |
165 | goto error; | |
166 | } | |
167 | ||
168 | return new_fd; | |
169 | ||
170 | error: | |
171 | return -1; | |
172 | } | |
173 | ||
174 | /* | |
917216f6 DG |
175 | * Creates a AF_UNIX local socket using pathname bind the socket upon creation |
176 | * and return the fd. | |
fac6795d DG |
177 | */ |
178 | int lttcomm_create_unix_sock(const char *pathname) | |
179 | { | |
180 | struct sockaddr_un sun; | |
181 | int fd; | |
182 | int ret = -1; | |
183 | ||
184 | /* Create server socket */ | |
185 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { | |
186 | perror("socket"); | |
187 | goto error; | |
188 | } | |
189 | ||
190 | memset(&sun, 0, sizeof(sun)); | |
191 | sun.sun_family = AF_UNIX; | |
99497cd0 MD |
192 | strncpy(sun.sun_path, pathname, sizeof(sun.sun_path)); |
193 | sun.sun_path[sizeof(sun.sun_path) - 1] = '\0'; | |
fac6795d | 194 | |
7d8234d9 MD |
195 | /* Unlink the old file if present */ |
196 | (void) unlink(pathname); | |
fac6795d DG |
197 | ret = bind(fd, (struct sockaddr *) &sun, sizeof(sun)); |
198 | if (ret < 0) { | |
199 | perror("bind"); | |
200 | goto error; | |
201 | } | |
202 | ||
203 | return fd; | |
204 | ||
205 | error: | |
206 | return ret; | |
207 | } | |
208 | ||
209 | /* | |
f158a754 | 210 | * Make the socket listen using LTTNG_SESSIOND_COMM_MAX_LISTEN. |
fac6795d DG |
211 | */ |
212 | int lttcomm_listen_unix_sock(int sock) | |
213 | { | |
214 | int ret; | |
215 | ||
f158a754 | 216 | ret = listen(sock, LTTNG_SESSIOND_COMM_MAX_LISTEN); |
fac6795d DG |
217 | if (ret < 0) { |
218 | perror("listen"); | |
219 | } | |
220 | ||
221 | return ret; | |
222 | } | |
223 | ||
224 | /* | |
917216f6 DG |
225 | * Receive data of size len in put that data into the buf param. Using recvmsg |
226 | * API. | |
fac6795d | 227 | * |
917216f6 | 228 | * Return the size of received data. |
fac6795d DG |
229 | */ |
230 | ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len) | |
231 | { | |
159c7ff4 | 232 | struct msghdr msg = { 0 }; |
fac6795d DG |
233 | struct iovec iov[1]; |
234 | ssize_t ret = -1; | |
235 | ||
fac6795d DG |
236 | iov[0].iov_base = buf; |
237 | iov[0].iov_len = len; | |
238 | msg.msg_iov = iov; | |
239 | msg.msg_iovlen = 1; | |
240 | ||
241 | ret = recvmsg(sock, &msg, 0); | |
242 | if (ret < 0) { | |
243 | perror("recvmsg"); | |
244 | } | |
245 | ||
246 | return ret; | |
247 | } | |
248 | ||
249 | /* | |
917216f6 | 250 | * Send buf data of size len. Using sendmsg API. |
fac6795d | 251 | * |
917216f6 | 252 | * Return the size of sent data. |
fac6795d DG |
253 | */ |
254 | ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len) | |
255 | { | |
159c7ff4 | 256 | struct msghdr msg = { 0 }; |
fac6795d DG |
257 | struct iovec iov[1]; |
258 | ssize_t ret = -1; | |
259 | ||
fac6795d DG |
260 | iov[0].iov_base = buf; |
261 | iov[0].iov_len = len; | |
262 | msg.msg_iov = iov; | |
263 | msg.msg_iovlen = 1; | |
264 | ||
265 | ret = sendmsg(sock, &msg, 0); | |
266 | if (ret < 0) { | |
267 | perror("sendmsg"); | |
268 | } | |
269 | ||
270 | return ret; | |
271 | } | |
87378cf5 DG |
272 | |
273 | /* | |
917216f6 | 274 | * Shutdown cleanly a unix socket. |
87378cf5 DG |
275 | */ |
276 | int lttcomm_close_unix_sock(int sock) | |
277 | { | |
278 | int ret; | |
279 | ||
280 | /* Shutdown receptions and transmissions */ | |
281 | ret = shutdown(sock, SHUT_RDWR); | |
282 | if (ret < 0) { | |
283 | perror("shutdown"); | |
284 | } | |
285 | ||
286 | return ret; | |
287 | } | |
8c0faa1d DG |
288 | |
289 | /* | |
3bd1e081 MD |
290 | * Send a message accompanied by fd(s) over a unix socket. |
291 | * | |
292 | * Returns the size of data sent, or negative error value. | |
8c0faa1d | 293 | */ |
3bd1e081 | 294 | ssize_t lttcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
8c0faa1d | 295 | { |
159c7ff4 | 296 | struct msghdr msg = { 0 }; |
8c0faa1d DG |
297 | struct cmsghdr *cmptr; |
298 | struct iovec iov[1]; | |
299 | ssize_t ret = -1; | |
300 | unsigned int sizeof_fds = nb_fd * sizeof(int); | |
301 | char tmp[CMSG_SPACE(sizeof_fds)]; | |
3bd1e081 | 302 | char dummy = 0; |
8c0faa1d | 303 | |
3bd1e081 MD |
304 | if (nb_fd > LTTCOMM_MAX_SEND_FDS) |
305 | return -EINVAL; | |
8c0faa1d DG |
306 | |
307 | msg.msg_control = (caddr_t)tmp; | |
308 | msg.msg_controllen = CMSG_LEN(sizeof_fds); | |
309 | ||
310 | cmptr = CMSG_FIRSTHDR(&msg); | |
8c0faa1d DG |
311 | cmptr->cmsg_level = SOL_SOCKET; |
312 | cmptr->cmsg_type = SCM_RIGHTS; | |
159c7ff4 | 313 | cmptr->cmsg_len = CMSG_LEN(sizeof_fds); |
8c0faa1d | 314 | memcpy(CMSG_DATA(cmptr), fds, sizeof_fds); |
159c7ff4 MD |
315 | /* Sum of the length of all control messages in the buffer: */ |
316 | msg.msg_controllen = cmptr->cmsg_len; | |
8c0faa1d | 317 | |
3bd1e081 MD |
318 | iov[0].iov_base = &dummy; |
319 | iov[0].iov_len = 1; | |
8c0faa1d DG |
320 | msg.msg_iov = iov; |
321 | msg.msg_iovlen = 1; | |
322 | ||
323 | ret = sendmsg(sock, &msg, 0); | |
324 | if (ret < 0) { | |
325 | perror("sendmsg"); | |
326 | } | |
8c0faa1d DG |
327 | return ret; |
328 | } | |
09de5c68 DG |
329 | |
330 | /* | |
3bd1e081 | 331 | * Recv a message accompanied by fd(s) from a unix socket. |
09de5c68 | 332 | * |
3bd1e081 MD |
333 | * Returns the size of received data, or negative error value. |
334 | * | |
335 | * Expect at most "nb_fd" file descriptors. Returns the number of fd | |
336 | * actually received in nb_fd. | |
09de5c68 | 337 | */ |
3bd1e081 | 338 | ssize_t lttcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd) |
09de5c68 DG |
339 | { |
340 | struct iovec iov[1]; | |
3bd1e081 | 341 | ssize_t ret = 0; |
09de5c68 | 342 | struct cmsghdr *cmsg; |
3bd1e081 MD |
343 | size_t sizeof_fds = nb_fd * sizeof(int); |
344 | char recv_fd[CMSG_SPACE(sizeof_fds)]; | |
09de5c68 | 345 | struct msghdr msg = { 0 }; |
3bd1e081 | 346 | char dummy; |
09de5c68 DG |
347 | |
348 | /* Prepare to receive the structures */ | |
3bd1e081 MD |
349 | iov[0].iov_base = &dummy; |
350 | iov[0].iov_len = 1; | |
09de5c68 DG |
351 | msg.msg_iov = iov; |
352 | msg.msg_iovlen = 1; | |
353 | msg.msg_control = recv_fd; | |
354 | msg.msg_controllen = sizeof(recv_fd); | |
355 | ||
356 | ret = recvmsg(sock, &msg, 0); | |
357 | if (ret < 0) { | |
358 | perror("recvmsg fds"); | |
359 | goto end; | |
360 | } | |
3bd1e081 MD |
361 | if (ret != 1) { |
362 | fprintf(stderr, "Error: Received %zd bytes, expected %d\n", | |
363 | ret, 1); | |
364 | goto end; | |
365 | } | |
366 | if (msg.msg_flags & MSG_CTRUNC) { | |
367 | fprintf(stderr, "Error: Control message truncated.\n"); | |
368 | ret = -1; | |
09de5c68 DG |
369 | goto end; |
370 | } | |
09de5c68 DG |
371 | cmsg = CMSG_FIRSTHDR(&msg); |
372 | if (!cmsg) { | |
3bd1e081 | 373 | fprintf(stderr, "Error: Invalid control message header\n"); |
09de5c68 DG |
374 | ret = -1; |
375 | goto end; | |
376 | } | |
09de5c68 | 377 | if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) { |
3bd1e081 | 378 | fprintf(stderr, "Didn't received any fd\n"); |
09de5c68 DG |
379 | ret = -1; |
380 | goto end; | |
381 | } | |
3bd1e081 MD |
382 | if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) { |
383 | fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n", | |
384 | cmsg->cmsg_len, CMSG_LEN(sizeof_fds)); | |
385 | ret = -1; | |
386 | goto end; | |
09de5c68 | 387 | } |
3bd1e081 MD |
388 | memcpy(fds, CMSG_DATA(cmsg), sizeof_fds); |
389 | ret = sizeof_fds; | |
09de5c68 DG |
390 | end: |
391 | return ret; | |
392 | } |