Fix: multiple type mismatch in debug statement
[lttng-tools.git] / src / common / relayd / relayd.c
1 /*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <assert.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <inttypes.h>
25
26 #include <common/common.h>
27 #include <common/defaults.h>
28 #include <common/sessiond-comm/relayd.h>
29
30 #include "relayd.h"
31
32 /*
33 * Send command. Fill up the header and append the data.
34 */
35 static int send_command(struct lttcomm_relayd_sock *rsock,
36 enum lttcomm_relayd_command cmd, void *data, size_t size,
37 int flags)
38 {
39 int ret;
40 struct lttcomm_relayd_hdr header;
41 char *buf;
42 uint64_t buf_size = sizeof(header);
43
44 if (data) {
45 buf_size += size;
46 }
47
48 buf = zmalloc(buf_size);
49 if (buf == NULL) {
50 PERROR("zmalloc relayd send command buf");
51 ret = -1;
52 goto alloc_error;
53 }
54
55 header.cmd = htobe32(cmd);
56 header.data_size = htobe64(size);
57
58 /* Zeroed for now since not used. */
59 header.cmd_version = 0;
60 header.circuit_id = 0;
61
62 /* Prepare buffer to send. */
63 memcpy(buf, &header, sizeof(header));
64 if (data) {
65 memcpy(buf + sizeof(header), data, size);
66 }
67
68 ret = rsock->sock.ops->sendmsg(&rsock->sock, buf, buf_size, flags);
69 if (ret < 0) {
70 ret = -errno;
71 goto error;
72 }
73
74 DBG3("Relayd sending command %d of size %" PRIu64, cmd, buf_size);
75
76 error:
77 free(buf);
78 alloc_error:
79 return ret;
80 }
81
82 /*
83 * Receive reply data on socket. This MUST be call after send_command or else
84 * could result in unexpected behavior(s).
85 */
86 static int recv_reply(struct lttcomm_relayd_sock *rsock, void *data, size_t size)
87 {
88 int ret;
89
90 DBG3("Relayd waiting for reply of size %zu", size);
91
92 ret = rsock->sock.ops->recvmsg(&rsock->sock, data, size, 0);
93 if (ret <= 0 || ret != size) {
94 if (ret == 0) {
95 /* Orderly shutdown. */
96 DBG("Socket %d has performed an orderly shutdown", rsock->sock.fd);
97 } else {
98 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
99 rsock->sock.fd, size, ret);
100 }
101 /* Always return -1 here and the caller can use errno. */
102 ret = -1;
103 goto error;
104 }
105
106 error:
107 return ret;
108 }
109
110 /*
111 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
112 * set session_id of the relayd if we have a successful reply from the relayd.
113 *
114 * On success, return 0 else a negative value which is either an errno error or
115 * a lttng error code from the relayd.
116 */
117 int relayd_create_session(struct lttcomm_relayd_sock *rsock, uint64_t *session_id)
118 {
119 int ret;
120 struct lttcomm_relayd_status_session reply;
121
122 assert(rsock);
123 assert(session_id);
124
125 DBG("Relayd create session");
126
127 /* Send command */
128 ret = send_command(rsock, RELAYD_CREATE_SESSION, NULL, 0, 0);
129 if (ret < 0) {
130 goto error;
131 }
132
133 /* Receive response */
134 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
135 if (ret < 0) {
136 goto error;
137 }
138
139 reply.session_id = be64toh(reply.session_id);
140 reply.ret_code = be32toh(reply.ret_code);
141
142 /* Return session id or negative ret code. */
143 if (reply.ret_code != LTTNG_OK) {
144 ret = -1;
145 ERR("Relayd create session replied error %d", reply.ret_code);
146 goto error;
147 } else {
148 ret = 0;
149 *session_id = reply.session_id;
150 }
151
152 DBG("Relayd session created with id %" PRIu64, reply.session_id);
153
154 error:
155 return ret;
156 }
157
158 /*
159 * Add stream on the relayd and assign stream handle to the stream_id argument.
160 *
161 * On success return 0 else return ret_code negative value.
162 */
163 int relayd_add_stream(struct lttcomm_relayd_sock *rsock, const char *channel_name,
164 const char *pathname, uint64_t *stream_id,
165 uint64_t tracefile_size, uint64_t tracefile_count)
166 {
167 int ret;
168 struct lttcomm_relayd_add_stream msg;
169 struct lttcomm_relayd_add_stream_2_2 msg_2_2;
170 struct lttcomm_relayd_status_stream reply;
171
172 /* Code flow error. Safety net. */
173 assert(rsock);
174 assert(channel_name);
175 assert(pathname);
176
177 DBG("Relayd adding stream for channel name %s", channel_name);
178
179 /* Compat with relayd 2.1 */
180 if (rsock->minor == 1) {
181 strncpy(msg.channel_name, channel_name, sizeof(msg.channel_name));
182 strncpy(msg.pathname, pathname, sizeof(msg.pathname));
183
184 /* Send command */
185 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0);
186 if (ret < 0) {
187 goto error;
188 }
189 } else {
190 /* Compat with relayd 2.2+ */
191 strncpy(msg_2_2.channel_name, channel_name, sizeof(msg_2_2.channel_name));
192 strncpy(msg_2_2.pathname, pathname, sizeof(msg_2_2.pathname));
193 msg_2_2.tracefile_size = htobe64(tracefile_size);
194 msg_2_2.tracefile_count = htobe64(tracefile_count);
195
196 /* Send command */
197 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg_2_2, sizeof(msg_2_2), 0);
198 if (ret < 0) {
199 goto error;
200 }
201 }
202
203 /* Waiting for reply */
204 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
205 if (ret < 0) {
206 goto error;
207 }
208
209 /* Back to host bytes order. */
210 reply.handle = be64toh(reply.handle);
211 reply.ret_code = be32toh(reply.ret_code);
212
213 /* Return session id or negative ret code. */
214 if (reply.ret_code != LTTNG_OK) {
215 ret = -1;
216 ERR("Relayd add stream replied error %d", reply.ret_code);
217 } else {
218 /* Success */
219 ret = 0;
220 *stream_id = reply.handle;
221 }
222
223 DBG("Relayd stream added successfully with handle %" PRIu64,
224 reply.handle);
225
226 error:
227 return ret;
228 }
229
230 /*
231 * Check version numbers on the relayd.
232 * If major versions are compatible, we assign minor_to_use to the
233 * minor version of the procotol we are going to use for this session.
234 *
235 * Return 0 if compatible else negative value.
236 */
237 int relayd_version_check(struct lttcomm_relayd_sock *rsock)
238 {
239 int ret;
240 struct lttcomm_relayd_version msg;
241
242 /* Code flow error. Safety net. */
243 assert(rsock);
244
245 DBG("Relayd version check for major.minor %u.%u", rsock->major,
246 rsock->minor);
247
248 /* Prepare network byte order before transmission. */
249 msg.major = htobe32(rsock->major);
250 msg.minor = htobe32(rsock->minor);
251
252 /* Send command */
253 ret = send_command(rsock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0);
254 if (ret < 0) {
255 goto error;
256 }
257
258 /* Receive response */
259 ret = recv_reply(rsock, (void *) &msg, sizeof(msg));
260 if (ret < 0) {
261 goto error;
262 }
263
264 /* Set back to host bytes order */
265 msg.major = be32toh(msg.major);
266 msg.minor = be32toh(msg.minor);
267
268 /*
269 * Only validate the major version. If the other side is higher,
270 * communication is not possible. Only major version equal can talk to each
271 * other. If the minor version differs, the lowest version is used by both
272 * sides.
273 */
274 if (msg.major != rsock->major) {
275 /* Not compatible */
276 ret = -1;
277 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
278 msg.major, rsock->major);
279 goto error;
280 }
281
282 /*
283 * If the relayd's minor version is higher, it will adapt to our version so
284 * we can continue to use the latest relayd communication data structure.
285 * If the received minor version is higher, the relayd should adapt to us.
286 */
287 if (rsock->minor > msg.minor) {
288 rsock->minor = msg.minor;
289 }
290
291 /* Version number compatible */
292 DBG2("Relayd version is compatible, using protocol version %u.%u",
293 rsock->major, rsock->minor);
294 ret = 0;
295
296 error:
297 return ret;
298 }
299
300 /*
301 * Add stream on the relayd and assign stream handle to the stream_id argument.
302 *
303 * On success return 0 else return ret_code negative value.
304 */
305 int relayd_send_metadata(struct lttcomm_relayd_sock *rsock, size_t len)
306 {
307 int ret;
308
309 /* Code flow error. Safety net. */
310 assert(rsock);
311
312 DBG("Relayd sending metadata of size %zu", len);
313
314 /* Send command */
315 ret = send_command(rsock, RELAYD_SEND_METADATA, NULL, len, 0);
316 if (ret < 0) {
317 goto error;
318 }
319
320 DBG2("Relayd metadata added successfully");
321
322 /*
323 * After that call, the metadata data MUST be sent to the relayd so the
324 * receive size on the other end matches the len of the metadata packet
325 * header. This is why we don't wait for a reply here.
326 */
327
328 error:
329 return ret;
330 }
331
332 /*
333 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
334 */
335 int relayd_connect(struct lttcomm_relayd_sock *rsock)
336 {
337 /* Code flow error. Safety net. */
338 assert(rsock);
339
340 DBG3("Relayd connect ...");
341
342 return rsock->sock.ops->connect(&rsock->sock);
343 }
344
345 /*
346 * Close relayd socket with an allocated lttcomm_relayd_sock.
347 *
348 * If no socket operations are found, simply return 0 meaning that everything
349 * is fine. Without operations, the socket can not possibly be opened or used.
350 * This is possible if the socket was allocated but not created. However, the
351 * caller could simply use it to store a valid file descriptor for instance
352 * passed over a Unix socket and call this to cleanup but still without a valid
353 * ops pointer.
354 *
355 * Return the close returned value. On error, a negative value is usually
356 * returned back from close(2).
357 */
358 int relayd_close(struct lttcomm_relayd_sock *rsock)
359 {
360 int ret;
361
362 /* Code flow error. Safety net. */
363 assert(rsock);
364
365 /* An invalid fd is fine, return success. */
366 if (rsock->sock.fd < 0) {
367 ret = 0;
368 goto end;
369 }
370
371 DBG3("Relayd closing socket %d", rsock->sock.fd);
372
373 if (rsock->sock.ops) {
374 ret = rsock->sock.ops->close(&rsock->sock);
375 } else {
376 /* Default call if no specific ops found. */
377 ret = close(rsock->sock.fd);
378 if (ret < 0) {
379 PERROR("relayd_close default close");
380 }
381 }
382
383 end:
384 return ret;
385 }
386
387 /*
388 * Send data header structure to the relayd.
389 */
390 int relayd_send_data_hdr(struct lttcomm_relayd_sock *rsock,
391 struct lttcomm_relayd_data_hdr *hdr, size_t size)
392 {
393 int ret;
394
395 /* Code flow error. Safety net. */
396 assert(rsock);
397 assert(hdr);
398
399 DBG3("Relayd sending data header of size %zu", size);
400
401 /* Again, safety net */
402 if (size == 0) {
403 size = sizeof(struct lttcomm_relayd_data_hdr);
404 }
405
406 /* Only send data header. */
407 ret = rsock->sock.ops->sendmsg(&rsock->sock, hdr, size, 0);
408 if (ret < 0) {
409 ret = -errno;
410 goto error;
411 }
412
413 /*
414 * The data MUST be sent right after that command for the receive on the
415 * other end to match the size in the header.
416 */
417
418 error:
419 return ret;
420 }
421
422 /*
423 * Send close stream command to the relayd.
424 */
425 int relayd_send_close_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
426 uint64_t last_net_seq_num)
427 {
428 int ret;
429 struct lttcomm_relayd_close_stream msg;
430 struct lttcomm_relayd_generic_reply reply;
431
432 /* Code flow error. Safety net. */
433 assert(rsock);
434
435 DBG("Relayd closing stream id %" PRIu64, stream_id);
436
437 msg.stream_id = htobe64(stream_id);
438 msg.last_net_seq_num = htobe64(last_net_seq_num);
439
440 /* Send command */
441 ret = send_command(rsock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0);
442 if (ret < 0) {
443 goto error;
444 }
445
446 /* Receive response */
447 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
448 if (ret < 0) {
449 goto error;
450 }
451
452 reply.ret_code = be32toh(reply.ret_code);
453
454 /* Return session id or negative ret code. */
455 if (reply.ret_code != LTTNG_OK) {
456 ret = -1;
457 ERR("Relayd close stream replied error %d", reply.ret_code);
458 } else {
459 /* Success */
460 ret = 0;
461 }
462
463 DBG("Relayd close stream id %" PRIu64 " successfully", stream_id);
464
465 error:
466 return ret;
467 }
468
469 /*
470 * Check for data availability for a given stream id.
471 *
472 * Return 0 if NOT pending, 1 if so and a negative value on error.
473 */
474 int relayd_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
475 uint64_t last_net_seq_num)
476 {
477 int ret;
478 struct lttcomm_relayd_data_pending msg;
479 struct lttcomm_relayd_generic_reply reply;
480
481 /* Code flow error. Safety net. */
482 assert(rsock);
483
484 DBG("Relayd data pending for stream id %" PRIu64, stream_id);
485
486 msg.stream_id = htobe64(stream_id);
487 msg.last_net_seq_num = htobe64(last_net_seq_num);
488
489 /* Send command */
490 ret = send_command(rsock, RELAYD_DATA_PENDING, (void *) &msg,
491 sizeof(msg), 0);
492 if (ret < 0) {
493 goto error;
494 }
495
496 /* Receive response */
497 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
498 if (ret < 0) {
499 goto error;
500 }
501
502 reply.ret_code = be32toh(reply.ret_code);
503
504 /* Return session id or negative ret code. */
505 if (reply.ret_code >= LTTNG_OK) {
506 ERR("Relayd data pending replied error %d", reply.ret_code);
507 }
508
509 /* At this point, the ret code is either 1 or 0 */
510 ret = reply.ret_code;
511
512 DBG("Relayd data is %s pending for stream id %" PRIu64,
513 ret == 1 ? "" : "NOT", stream_id);
514
515 error:
516 return ret;
517 }
518
519 /*
520 * Check on the relayd side for a quiescent state on the control socket.
521 */
522 int relayd_quiescent_control(struct lttcomm_relayd_sock *rsock,
523 uint64_t metadata_stream_id)
524 {
525 int ret;
526 struct lttcomm_relayd_quiescent_control msg;
527 struct lttcomm_relayd_generic_reply reply;
528
529 /* Code flow error. Safety net. */
530 assert(rsock);
531
532 DBG("Relayd checking quiescent control state");
533
534 msg.stream_id = htobe64(metadata_stream_id);
535
536 /* Send command */
537 ret = send_command(rsock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0);
538 if (ret < 0) {
539 goto error;
540 }
541
542 /* Receive response */
543 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
544 if (ret < 0) {
545 goto error;
546 }
547
548 reply.ret_code = be32toh(reply.ret_code);
549
550 /* Return session id or negative ret code. */
551 if (reply.ret_code != LTTNG_OK) {
552 ret = -1;
553 ERR("Relayd quiescent control replied error %d", reply.ret_code);
554 goto error;
555 }
556
557 /* Control socket is quiescent */
558 return 0;
559
560 error:
561 return ret;
562 }
563
564 /*
565 * Begin a data pending command for a specific session id.
566 */
567 int relayd_begin_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id)
568 {
569 int ret;
570 struct lttcomm_relayd_begin_data_pending msg;
571 struct lttcomm_relayd_generic_reply reply;
572
573 /* Code flow error. Safety net. */
574 assert(rsock);
575
576 DBG("Relayd begin data pending");
577
578 msg.session_id = htobe64(id);
579
580 /* Send command */
581 ret = send_command(rsock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0);
582 if (ret < 0) {
583 goto error;
584 }
585
586 /* Receive response */
587 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
588 if (ret < 0) {
589 goto error;
590 }
591
592 reply.ret_code = be32toh(reply.ret_code);
593
594 /* Return session id or negative ret code. */
595 if (reply.ret_code != LTTNG_OK) {
596 ret = -1;
597 ERR("Relayd begin data pending replied error %d", reply.ret_code);
598 goto error;
599 }
600
601 return 0;
602
603 error:
604 return ret;
605 }
606
607 /*
608 * End a data pending command for a specific session id.
609 *
610 * Return 0 on success and set is_data_inflight to 0 if no data is being
611 * streamed or 1 if it is the case.
612 */
613 int relayd_end_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id,
614 unsigned int *is_data_inflight)
615 {
616 int ret;
617 struct lttcomm_relayd_end_data_pending msg;
618 struct lttcomm_relayd_generic_reply reply;
619
620 /* Code flow error. Safety net. */
621 assert(rsock);
622
623 DBG("Relayd end data pending");
624
625 msg.session_id = htobe64(id);
626
627 /* Send command */
628 ret = send_command(rsock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0);
629 if (ret < 0) {
630 goto error;
631 }
632
633 /* Receive response */
634 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
635 if (ret < 0) {
636 goto error;
637 }
638
639 reply.ret_code = be32toh(reply.ret_code);
640 if (reply.ret_code < 0) {
641 ret = reply.ret_code;
642 goto error;
643 }
644
645 *is_data_inflight = reply.ret_code;
646
647 DBG("Relayd end data pending is data inflight: %d", reply.ret_code);
648
649 return 0;
650
651 error:
652 return ret;
653 }
This page took 0.042275 seconds and 5 git commands to generate.