Fix: handle shutdown on recv reply in relayd
[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_sock *sock,
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 = sock->ops->sendmsg(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_sock *sock, void *data, size_t size)
87 {
88 int ret;
89
90 DBG3("Relayd waiting for reply of size %ld", size);
91
92 ret = sock->ops->recvmsg(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", sock->fd);
97 } else {
98 DBG("Receiving reply failed on sock %d for size %lu with ret %d",
99 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_sock *sock, uint64_t *session_id)
118 {
119 int ret;
120 struct lttcomm_relayd_status_session reply;
121
122 assert(sock);
123 assert(session_id);
124
125 DBG("Relayd create session");
126
127 /* Send command */
128 ret = send_command(sock, RELAYD_CREATE_SESSION, NULL, 0, 0);
129 if (ret < 0) {
130 goto error;
131 }
132
133 /* Receive response */
134 ret = recv_reply(sock, (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_sock *sock, const char *channel_name,
164 const char *pathname, uint64_t *stream_id)
165 {
166 int ret;
167 struct lttcomm_relayd_add_stream msg;
168 struct lttcomm_relayd_status_stream reply;
169
170 /* Code flow error. Safety net. */
171 assert(sock);
172 assert(channel_name);
173 assert(pathname);
174
175 DBG("Relayd adding stream for channel name %s", channel_name);
176
177 strncpy(msg.channel_name, channel_name, sizeof(msg.channel_name));
178 strncpy(msg.pathname, pathname, sizeof(msg.pathname));
179
180 /* Send command */
181 ret = send_command(sock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0);
182 if (ret < 0) {
183 goto error;
184 }
185
186 /* Waiting for reply */
187 ret = recv_reply(sock, (void *) &reply, sizeof(reply));
188 if (ret < 0) {
189 goto error;
190 }
191
192 /* Back to host bytes order. */
193 reply.handle = be64toh(reply.handle);
194 reply.ret_code = be32toh(reply.ret_code);
195
196 /* Return session id or negative ret code. */
197 if (reply.ret_code != LTTNG_OK) {
198 ret = -1;
199 ERR("Relayd add stream replied error %d", reply.ret_code);
200 } else {
201 /* Success */
202 ret = 0;
203 *stream_id = reply.handle;
204 }
205
206 DBG("Relayd stream added successfully with handle %" PRIu64,
207 reply.handle);
208
209 error:
210 return ret;
211 }
212
213 /*
214 * Check version numbers on the relayd.
215 *
216 * Return 0 if compatible else negative value.
217 */
218 int relayd_version_check(struct lttcomm_sock *sock, uint32_t major,
219 uint32_t minor)
220 {
221 int ret;
222 struct lttcomm_relayd_version msg;
223
224 /* Code flow error. Safety net. */
225 assert(sock);
226
227 DBG("Relayd version check for major.minor %u.%u", major, minor);
228
229 /* Prepare network byte order before transmission. */
230 msg.major = htobe32(major);
231 msg.minor = htobe32(minor);
232
233 /* Send command */
234 ret = send_command(sock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0);
235 if (ret < 0) {
236 goto error;
237 }
238
239 /* Receive response */
240 ret = recv_reply(sock, (void *) &msg, sizeof(msg));
241 if (ret < 0) {
242 goto error;
243 }
244
245 /* Set back to host bytes order */
246 msg.major = be32toh(msg.major);
247 msg.minor = be32toh(msg.minor);
248
249 /*
250 * Only validate the major version. If the other side is higher,
251 * communication is not possible. Only major version equal can talk to each
252 * other. If the minor version differs, the lowest version is used by both
253 * sides.
254 *
255 * For now, before 2.1.0 stable release, we don't have to check the minor
256 * because this new mechanism with the relayd will only be available with
257 * 2.1 and NOT 2.0.x.
258 */
259 if (msg.major == major) {
260 /* Compatible */
261 ret = 0;
262 DBG2("Relayd version is compatible");
263 goto error;
264 }
265
266 /*
267 * After 2.1.0 release, for the 2.2 release, at this point will have to
268 * check the minor version in order for the session daemon to know which
269 * structure to use to communicate with the relayd. If the relayd's minor
270 * version is higher, it will adapt to our version so we can continue to
271 * use the latest relayd communication data structure.
272 */
273
274 /* Version number not compatible */
275 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
276 msg.major, major);
277 ret = -1;
278
279 error:
280 return ret;
281 }
282
283 /*
284 * Add stream on the relayd and assign stream handle to the stream_id argument.
285 *
286 * On success return 0 else return ret_code negative value.
287 */
288 int relayd_send_metadata(struct lttcomm_sock *sock, size_t len)
289 {
290 int ret;
291
292 /* Code flow error. Safety net. */
293 assert(sock);
294
295 DBG("Relayd sending metadata of size %zu", len);
296
297 /* Send command */
298 ret = send_command(sock, RELAYD_SEND_METADATA, NULL, len, 0);
299 if (ret < 0) {
300 goto error;
301 }
302
303 DBG2("Relayd metadata added successfully");
304
305 /*
306 * After that call, the metadata data MUST be sent to the relayd so the
307 * receive size on the other end matches the len of the metadata packet
308 * header. This is why we don't wait for a reply here.
309 */
310
311 error:
312 return ret;
313 }
314
315 /*
316 * Connect to relay daemon with an allocated lttcomm_sock.
317 */
318 int relayd_connect(struct lttcomm_sock *sock)
319 {
320 /* Code flow error. Safety net. */
321 assert(sock);
322
323 DBG3("Relayd connect ...");
324
325 return sock->ops->connect(sock);
326 }
327
328 /*
329 * Close relayd socket with an allocated lttcomm_sock.
330 */
331 int relayd_close(struct lttcomm_sock *sock)
332 {
333 /* Code flow error. Safety net. */
334 assert(sock);
335
336 DBG3("Relayd closing socket %d", sock->fd);
337
338 return sock->ops->close(sock);
339 }
340
341 /*
342 * Send data header structure to the relayd.
343 */
344 int relayd_send_data_hdr(struct lttcomm_sock *sock,
345 struct lttcomm_relayd_data_hdr *hdr, size_t size)
346 {
347 int ret;
348
349 /* Code flow error. Safety net. */
350 assert(sock);
351 assert(hdr);
352
353 DBG3("Relayd sending data header of size %ld", size);
354
355 /* Again, safety net */
356 if (size == 0) {
357 size = sizeof(struct lttcomm_relayd_data_hdr);
358 }
359
360 /* Only send data header. */
361 ret = sock->ops->sendmsg(sock, hdr, size, 0);
362 if (ret < 0) {
363 ret = -errno;
364 goto error;
365 }
366
367 /*
368 * The data MUST be sent right after that command for the receive on the
369 * other end to match the size in the header.
370 */
371
372 error:
373 return ret;
374 }
375
376 /*
377 * Send close stream command to the relayd.
378 */
379 int relayd_send_close_stream(struct lttcomm_sock *sock, uint64_t stream_id,
380 uint64_t last_net_seq_num)
381 {
382 int ret;
383 struct lttcomm_relayd_close_stream msg;
384 struct lttcomm_relayd_generic_reply reply;
385
386 /* Code flow error. Safety net. */
387 assert(sock);
388
389 DBG("Relayd closing stream id %" PRIu64, stream_id);
390
391 msg.stream_id = htobe64(stream_id);
392 msg.last_net_seq_num = htobe64(last_net_seq_num);
393
394 /* Send command */
395 ret = send_command(sock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0);
396 if (ret < 0) {
397 goto error;
398 }
399
400 /* Receive response */
401 ret = recv_reply(sock, (void *) &reply, sizeof(reply));
402 if (ret < 0) {
403 goto error;
404 }
405
406 reply.ret_code = be32toh(reply.ret_code);
407
408 /* Return session id or negative ret code. */
409 if (reply.ret_code != LTTNG_OK) {
410 ret = -1;
411 ERR("Relayd close stream replied error %d", reply.ret_code);
412 } else {
413 /* Success */
414 ret = 0;
415 }
416
417 DBG("Relayd close stream id %" PRIu64 " successfully", stream_id);
418
419 error:
420 return ret;
421 }
422
423 /*
424 * Check for data availability for a given stream id.
425 *
426 * Return 0 if NOT pending, 1 if so and a negative value on error.
427 */
428 int relayd_data_pending(struct lttcomm_sock *sock, uint64_t stream_id,
429 uint64_t last_net_seq_num)
430 {
431 int ret;
432 struct lttcomm_relayd_data_pending msg;
433 struct lttcomm_relayd_generic_reply reply;
434
435 /* Code flow error. Safety net. */
436 assert(sock);
437
438 DBG("Relayd data pending for stream id %" PRIu64, stream_id);
439
440 msg.stream_id = htobe64(stream_id);
441 msg.last_net_seq_num = htobe64(last_net_seq_num);
442
443 /* Send command */
444 ret = send_command(sock, RELAYD_DATA_PENDING, (void *) &msg,
445 sizeof(msg), 0);
446 if (ret < 0) {
447 goto error;
448 }
449
450 /* Receive response */
451 ret = recv_reply(sock, (void *) &reply, sizeof(reply));
452 if (ret < 0) {
453 goto error;
454 }
455
456 reply.ret_code = be32toh(reply.ret_code);
457
458 /* Return session id or negative ret code. */
459 if (reply.ret_code >= LTTNG_OK) {
460 ERR("Relayd data pending replied error %d", reply.ret_code);
461 }
462
463 /* At this point, the ret code is either 1 or 0 */
464 ret = reply.ret_code;
465
466 DBG("Relayd data is %s pending for stream id %" PRIu64,
467 ret == 1 ? "" : "NOT", stream_id);
468
469 error:
470 return ret;
471 }
472
473 /*
474 * Check on the relayd side for a quiescent state on the control socket.
475 */
476 int relayd_quiescent_control(struct lttcomm_sock *sock,
477 uint64_t metadata_stream_id)
478 {
479 int ret;
480 struct lttcomm_relayd_quiescent_control msg;
481 struct lttcomm_relayd_generic_reply reply;
482
483 /* Code flow error. Safety net. */
484 assert(sock);
485
486 DBG("Relayd checking quiescent control state");
487
488 msg.stream_id = htobe64(metadata_stream_id);
489
490 /* Send command */
491 ret = send_command(sock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0);
492 if (ret < 0) {
493 goto error;
494 }
495
496 /* Receive response */
497 ret = recv_reply(sock, (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 ret = -1;
507 ERR("Relayd quiescent control replied error %d", reply.ret_code);
508 goto error;
509 }
510
511 /* Control socket is quiescent */
512 return 0;
513
514 error:
515 return ret;
516 }
517
518 /*
519 * Begin a data pending command for a specific session id.
520 */
521 int relayd_begin_data_pending(struct lttcomm_sock *sock, uint64_t id)
522 {
523 int ret;
524 struct lttcomm_relayd_begin_data_pending msg;
525 struct lttcomm_relayd_generic_reply reply;
526
527 /* Code flow error. Safety net. */
528 assert(sock);
529
530 DBG("Relayd begin data pending");
531
532 msg.session_id = htobe64(id);
533
534 /* Send command */
535 ret = send_command(sock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0);
536 if (ret < 0) {
537 goto error;
538 }
539
540 /* Receive response */
541 ret = recv_reply(sock, (void *) &reply, sizeof(reply));
542 if (ret < 0) {
543 goto error;
544 }
545
546 reply.ret_code = be32toh(reply.ret_code);
547
548 /* Return session id or negative ret code. */
549 if (reply.ret_code != LTTNG_OK) {
550 ret = -1;
551 ERR("Relayd begin data pending replied error %d", reply.ret_code);
552 goto error;
553 }
554
555 return 0;
556
557 error:
558 return ret;
559 }
560
561 /*
562 * End a data pending command for a specific session id.
563 *
564 * Return 0 on success and set is_data_inflight to 0 if no data is being
565 * streamed or 1 if it is the case.
566 */
567 int relayd_end_data_pending(struct lttcomm_sock *sock, uint64_t id,
568 unsigned int *is_data_inflight)
569 {
570 int ret;
571 struct lttcomm_relayd_end_data_pending msg;
572 struct lttcomm_relayd_generic_reply reply;
573
574 /* Code flow error. Safety net. */
575 assert(sock);
576
577 DBG("Relayd end data pending");
578
579 msg.session_id = htobe64(id);
580
581 /* Send command */
582 ret = send_command(sock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0);
583 if (ret < 0) {
584 goto error;
585 }
586
587 /* Receive response */
588 ret = recv_reply(sock, (void *) &reply, sizeof(reply));
589 if (ret < 0) {
590 goto error;
591 }
592
593 reply.ret_code = be32toh(reply.ret_code);
594 if (reply.ret_code < 0) {
595 ret = reply.ret_code;
596 goto error;
597 }
598
599 *is_data_inflight = reply.ret_code;
600
601 DBG("Relayd end data pending is data inflight: %d", reply.ret_code);
602
603 return 0;
604
605 error:
606 return ret;
607 }
This page took 0.041033 seconds and 5 git commands to generate.