Remove unnecessary check of output parameter
[lttng-tools.git] / src / lib / lttng-ctl / channel.c
1 /*
2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
7 *
8 * This library 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 Lesser General Public License
11 * for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 #include <lttng/notification/notification-internal.h>
19 #include <lttng/notification/channel-internal.h>
20 #include <lttng/condition/condition-internal.h>
21 #include <lttng/endpoint.h>
22 #include <common/defaults.h>
23 #include <common/error.h>
24 #include <common/dynamic-buffer.h>
25 #include <common/utils.h>
26 #include <common/defaults.h>
27 #include <assert.h>
28 #include "lttng-ctl-helper.h"
29
30 static
31 int handshake(struct lttng_notification_channel *channel);
32
33 /*
34 * Populates the reception buffer with the next complete message.
35 * The caller must acquire the client's lock.
36 */
37 static
38 int receive_message(struct lttng_notification_channel *channel)
39 {
40 ssize_t ret;
41 struct lttng_notification_channel_message msg;
42
43 ret = lttng_dynamic_buffer_set_size(&channel->reception_buffer, 0);
44 if (ret) {
45 goto error;
46 }
47
48 ret = lttcomm_recv_unix_sock(channel->socket, &msg, sizeof(msg));
49 if (ret <= 0) {
50 ret = -1;
51 goto error;
52 }
53
54 if (msg.size > DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE) {
55 ret = -1;
56 goto error;
57 }
58
59 /* Add message header at buffer's start. */
60 ret = lttng_dynamic_buffer_append(&channel->reception_buffer, &msg,
61 sizeof(msg));
62 if (ret) {
63 goto error;
64 }
65
66 /* Reserve space for the payload. */
67 ret = lttng_dynamic_buffer_set_size(&channel->reception_buffer,
68 channel->reception_buffer.size + msg.size);
69 if (ret) {
70 goto error;
71 }
72
73 /* Receive message payload. */
74 ret = lttcomm_recv_unix_sock(channel->socket,
75 channel->reception_buffer.data + sizeof(msg), msg.size);
76 if (ret < (ssize_t) msg.size) {
77 ret = -1;
78 goto error;
79 }
80 ret = 0;
81 end:
82 return ret;
83 error:
84 if (lttng_dynamic_buffer_set_size(&channel->reception_buffer, 0)) {
85 ret = -1;
86 }
87 goto end;
88 }
89
90 static
91 enum lttng_notification_channel_message_type get_current_message_type(
92 struct lttng_notification_channel *channel)
93 {
94 struct lttng_notification_channel_message *msg;
95
96 assert(channel->reception_buffer.size >= sizeof(*msg));
97
98 msg = (struct lttng_notification_channel_message *)
99 channel->reception_buffer.data;
100 return (enum lttng_notification_channel_message_type) msg->type;
101 }
102
103 static
104 struct lttng_notification *create_notification_from_current_message(
105 struct lttng_notification_channel *channel)
106 {
107 ssize_t ret;
108 struct lttng_notification *notification = NULL;
109 struct lttng_buffer_view view;
110
111 if (channel->reception_buffer.size <=
112 sizeof(struct lttng_notification_channel_message)) {
113 goto end;
114 }
115
116 view = lttng_buffer_view_from_dynamic_buffer(&channel->reception_buffer,
117 sizeof(struct lttng_notification_channel_message), -1);
118
119 ret = lttng_notification_create_from_buffer(&view, &notification);
120 if (ret != channel->reception_buffer.size -
121 sizeof(struct lttng_notification_channel_message)) {
122 lttng_notification_destroy(notification);
123 notification = NULL;
124 goto end;
125 }
126 end:
127 return notification;
128 }
129
130 struct lttng_notification_channel *lttng_notification_channel_create(
131 struct lttng_endpoint *endpoint)
132 {
133 int fd, ret;
134 bool is_in_tracing_group = false, is_root = false;
135 char *sock_path = NULL;
136 struct lttng_notification_channel *channel = NULL;
137
138 if (!endpoint ||
139 endpoint != lttng_session_daemon_notification_endpoint) {
140 goto end;
141 }
142
143 sock_path = zmalloc(LTTNG_PATH_MAX);
144 if (!sock_path) {
145 goto end;
146 }
147
148 channel = zmalloc(sizeof(struct lttng_notification_channel));
149 if (!channel) {
150 goto end;
151 }
152 channel->socket = -1;
153 pthread_mutex_init(&channel->lock, NULL);
154 lttng_dynamic_buffer_init(&channel->reception_buffer);
155 CDS_INIT_LIST_HEAD(&channel->pending_notifications.list);
156
157 is_root = (getuid() == 0);
158 if (!is_root) {
159 is_in_tracing_group = lttng_check_tracing_group();
160 }
161
162 if (is_root || is_in_tracing_group) {
163 lttng_ctl_copy_string(sock_path,
164 DEFAULT_GLOBAL_NOTIFICATION_CHANNEL_UNIX_SOCK,
165 LTTNG_PATH_MAX);
166 ret = lttcomm_connect_unix_sock(sock_path);
167 if (ret >= 0) {
168 fd = ret;
169 goto set_fd;
170 }
171 }
172
173 /* Fallback to local session daemon. */
174 ret = snprintf(sock_path, LTTNG_PATH_MAX,
175 DEFAULT_HOME_NOTIFICATION_CHANNEL_UNIX_SOCK,
176 utils_get_home_dir());
177 if (ret < 0 || ret >= LTTNG_PATH_MAX) {
178 goto error;
179 }
180
181 ret = lttcomm_connect_unix_sock(sock_path);
182 if (ret < 0) {
183 goto error;
184 }
185 fd = ret;
186
187 set_fd:
188 channel->socket = fd;
189
190 ret = handshake(channel);
191 if (ret) {
192 goto error;
193 }
194 end:
195 free(sock_path);
196 return channel;
197 error:
198 lttng_notification_channel_destroy(channel);
199 channel = NULL;
200 goto end;
201 }
202
203 enum lttng_notification_channel_status
204 lttng_notification_channel_get_next_notification(
205 struct lttng_notification_channel *channel,
206 struct lttng_notification **_notification)
207 {
208 int ret;
209 struct lttng_notification *notification = NULL;
210 enum lttng_notification_channel_status status =
211 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
212
213 if (!channel || !_notification) {
214 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID;
215 goto end;
216 }
217
218 pthread_mutex_lock(&channel->lock);
219
220 if (channel->pending_notifications.count) {
221 struct pending_notification *pending_notification;
222
223 assert(!cds_list_empty(&channel->pending_notifications.list));
224
225 /* Deliver one of the pending notifications. */
226 pending_notification = cds_list_first_entry(
227 &channel->pending_notifications.list,
228 struct pending_notification,
229 node);
230 notification = pending_notification->notification;
231 if (!notification) {
232 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED;
233 }
234 cds_list_del(&pending_notification->node);
235 channel->pending_notifications.count--;
236 free(pending_notification);
237 goto end_unlock;
238 }
239
240 ret = receive_message(channel);
241 if (ret) {
242 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR;
243 goto end_unlock;
244 }
245
246 switch (get_current_message_type(channel)) {
247 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION:
248 notification = create_notification_from_current_message(
249 channel);
250 if (!notification) {
251 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR;
252 goto end_unlock;
253 }
254 break;
255 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED:
256 /* No payload to consume. */
257 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_NOTIFICATIONS_DROPPED;
258 break;
259 default:
260 /* Protocol error. */
261 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR;
262 goto end_unlock;
263 }
264
265 end_unlock:
266 pthread_mutex_unlock(&channel->lock);
267 *_notification = notification;
268 end:
269 return status;
270 }
271
272 static
273 int enqueue_dropped_notification(
274 struct lttng_notification_channel *channel)
275 {
276 int ret = 0;
277 struct pending_notification *pending_notification;
278 struct cds_list_head *last_element =
279 channel->pending_notifications.list.prev;
280
281 pending_notification = caa_container_of(last_element,
282 struct pending_notification, node);
283 if (!pending_notification->notification) {
284 /*
285 * The last enqueued notification indicates dropped
286 * notifications; there is nothing to do as we group
287 * dropped notifications together.
288 */
289 goto end;
290 }
291
292 if (channel->pending_notifications.count >=
293 DEFAULT_CLIENT_MAX_QUEUED_NOTIFICATIONS_COUNT &&
294 pending_notification->notification) {
295 /*
296 * Discard the last enqueued notification to indicate
297 * that notifications were dropped at this point.
298 */
299 lttng_notification_destroy(
300 pending_notification->notification);
301 pending_notification->notification = NULL;
302 goto end;
303 }
304
305 pending_notification = zmalloc(sizeof(*pending_notification));
306 if (!pending_notification) {
307 ret = -1;
308 goto end;
309 }
310 CDS_INIT_LIST_HEAD(&pending_notification->node);
311 cds_list_add(&pending_notification->node,
312 &channel->pending_notifications.list);
313 channel->pending_notifications.count++;
314 end:
315 return ret;
316 }
317
318 static
319 int enqueue_notification_from_current_message(
320 struct lttng_notification_channel *channel)
321 {
322 int ret = 0;
323 struct lttng_notification *notification;
324 struct pending_notification *pending_notification;
325
326 if (channel->pending_notifications.count >=
327 DEFAULT_CLIENT_MAX_QUEUED_NOTIFICATIONS_COUNT) {
328 /* Drop the notification. */
329 ret = enqueue_dropped_notification(channel);
330 goto end;
331 }
332
333 pending_notification = zmalloc(sizeof(*pending_notification));
334 if (!pending_notification) {
335 ret = -1;
336 goto error;
337 }
338 CDS_INIT_LIST_HEAD(&pending_notification->node);
339
340 notification = create_notification_from_current_message(channel);
341 if (!notification) {
342 ret = -1;
343 goto error;
344 }
345
346 pending_notification->notification = notification;
347 cds_list_add(&pending_notification->node,
348 &channel->pending_notifications.list);
349 channel->pending_notifications.count++;
350 end:
351 return ret;
352 error:
353 free(pending_notification);
354 goto end;
355 }
356
357 static
358 int receive_command_reply(struct lttng_notification_channel *channel,
359 enum lttng_notification_channel_status *status)
360 {
361 int ret;
362 struct lttng_notification_channel_command_reply *reply;
363
364 while (true) {
365 enum lttng_notification_channel_message_type msg_type;
366
367 ret = receive_message(channel);
368 if (ret) {
369 goto end;
370 }
371
372 msg_type = get_current_message_type(channel);
373 switch (msg_type) {
374 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY:
375 goto exit_loop;
376 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION:
377 ret = enqueue_notification_from_current_message(
378 channel);
379 if (ret) {
380 goto end;
381 }
382 break;
383 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED:
384 ret = enqueue_dropped_notification(channel);
385 if (ret) {
386 goto end;
387 }
388 break;
389 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE:
390 {
391 struct lttng_notification_channel_command_handshake *handshake;
392
393 handshake = (struct lttng_notification_channel_command_handshake *)
394 (channel->reception_buffer.data +
395 sizeof(struct lttng_notification_channel_message));
396 channel->version.major = handshake->major;
397 channel->version.minor = handshake->minor;
398 channel->version.set = true;
399 break;
400 }
401 default:
402 ret = -1;
403 goto end;
404 }
405 }
406
407 exit_loop:
408 if (channel->reception_buffer.size <
409 (sizeof(struct lttng_notification_channel_message) +
410 sizeof(*reply))) {
411 /* Invalid message received. */
412 ret = -1;
413 goto end;
414 }
415
416 reply = (struct lttng_notification_channel_command_reply *)
417 (channel->reception_buffer.data +
418 sizeof(struct lttng_notification_channel_message));
419 *status = (enum lttng_notification_channel_status) reply->status;
420 end:
421 return ret;
422 }
423
424 static
425 int handshake(struct lttng_notification_channel *channel)
426 {
427 ssize_t ret;
428 enum lttng_notification_channel_status status =
429 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
430 struct lttng_notification_channel_command_handshake handshake = {
431 .major = LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR,
432 .minor = LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR,
433 };
434 struct lttng_notification_channel_message msg_header = {
435 .type = LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE,
436 .size = sizeof(handshake),
437 };
438 char send_buffer[sizeof(msg_header) + sizeof(handshake)];
439
440 memcpy(send_buffer, &msg_header, sizeof(msg_header));
441 memcpy(send_buffer + sizeof(msg_header), &handshake, sizeof(handshake));
442
443 pthread_mutex_lock(&channel->lock);
444
445 ret = lttcomm_send_creds_unix_sock(channel->socket, send_buffer,
446 sizeof(send_buffer));
447 if (ret < 0) {
448 goto end_unlock;
449 }
450
451 /* Receive handshake info from the sessiond. */
452 ret = receive_command_reply(channel, &status);
453 if (ret < 0) {
454 goto end_unlock;
455 }
456
457 if (!channel->version.set) {
458 ret = -1;
459 goto end_unlock;
460 }
461
462 if (channel->version.major != LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR) {
463 ret = -1;
464 goto end_unlock;
465 }
466
467 end_unlock:
468 pthread_mutex_unlock(&channel->lock);
469 return ret;
470 }
471
472 static
473 enum lttng_notification_channel_status send_condition_command(
474 struct lttng_notification_channel *channel,
475 enum lttng_notification_channel_message_type type,
476 const struct lttng_condition *condition)
477 {
478 int socket;
479 ssize_t command_size, ret;
480 enum lttng_notification_channel_status status =
481 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
482 char *command_buffer = NULL;
483 struct lttng_notification_channel_message cmd_message = {
484 .type = type,
485 };
486
487 if (!channel) {
488 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID;
489 goto end;
490 }
491
492 assert(type == LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE ||
493 type == LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE);
494
495 pthread_mutex_lock(&channel->lock);
496 socket = channel->socket;
497 if (!lttng_condition_validate(condition)) {
498 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID;
499 goto end_unlock;
500 }
501
502 ret = lttng_condition_serialize(condition, NULL);
503 if (ret < 0) {
504 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_INVALID;
505 goto end_unlock;
506 }
507 assert(ret < UINT32_MAX);
508 cmd_message.size = (uint32_t) ret;
509 command_size = ret + sizeof(
510 struct lttng_notification_channel_message);
511 command_buffer = zmalloc(command_size);
512 if (!command_buffer) {
513 goto end_unlock;
514 }
515
516 memcpy(command_buffer, &cmd_message, sizeof(cmd_message));
517 ret = lttng_condition_serialize(condition,
518 command_buffer + sizeof(cmd_message));
519 if (ret < 0) {
520 goto end_unlock;
521 }
522
523 ret = lttcomm_send_unix_sock(socket, command_buffer, command_size);
524 if (ret < 0) {
525 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR;
526 goto end_unlock;
527 }
528
529 ret = receive_command_reply(channel, &status);
530 if (ret < 0) {
531 status = LTTNG_NOTIFICATION_CHANNEL_STATUS_ERROR;
532 goto end_unlock;
533 }
534 end_unlock:
535 pthread_mutex_unlock(&channel->lock);
536 end:
537 free(command_buffer);
538 return status;
539 }
540
541 enum lttng_notification_channel_status lttng_notification_channel_subscribe(
542 struct lttng_notification_channel *channel,
543 const struct lttng_condition *condition)
544 {
545 return send_condition_command(channel,
546 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE,
547 condition);
548 }
549
550 enum lttng_notification_channel_status lttng_notification_channel_unsubscribe(
551 struct lttng_notification_channel *channel,
552 const struct lttng_condition *condition)
553 {
554 return send_condition_command(channel,
555 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE,
556 condition);
557 }
558
559 void lttng_notification_channel_destroy(
560 struct lttng_notification_channel *channel)
561 {
562 if (!channel) {
563 return;
564 }
565
566 if (channel->socket >= 0) {
567 (void) lttcomm_close_unix_sock(channel->socket);
568 }
569 pthread_mutex_destroy(&channel->lock);
570 lttng_dynamic_buffer_reset(&channel->reception_buffer);
571 free(channel);
572 }
This page took 0.042523 seconds and 5 git commands to generate.