2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
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.
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
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.
24 #include <sys/types.h>
27 #include <common/common.h>
28 #include <common/defaults.h>
29 #include <common/uri.h>
34 * Send destroy relayd command to consumer.
36 * On success return positive value. On error, negative value.
38 int consumer_send_destroy_relayd(struct consumer_socket
*sock
,
39 struct consumer_output
*consumer
)
42 struct lttcomm_consumer_msg msg
;
47 DBG2("Sending destroy relayd command to consumer...");
49 /* Bail out if consumer is disabled */
50 if (!consumer
->enabled
) {
52 DBG3("Consumer is disabled");
56 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_RELAYD
;
57 msg
.u
.destroy_relayd
.net_seq_idx
= consumer
->net_seq_index
;
59 pthread_mutex_lock(sock
->lock
);
60 ret
= lttcomm_send_unix_sock(sock
->fd
, &msg
, sizeof(msg
));
61 pthread_mutex_unlock(sock
->lock
);
63 PERROR("send consumer destroy relayd command");
67 DBG2("Consumer send destroy relayd command done");
74 * For each consumer socket in the consumer output object, send a destroy
77 void consumer_output_send_destroy_relayd(struct consumer_output
*consumer
)
80 struct lttng_ht_iter iter
;
81 struct consumer_socket
*socket
;
85 /* Destroy any relayd connection */
86 if (consumer
&& consumer
->type
== CONSUMER_DST_NET
) {
88 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
90 /* Send destroy relayd command */
91 ret
= consumer_send_destroy_relayd(socket
, consumer
);
93 ERR("Unable to send destroy relayd command to consumer");
94 /* Continue since we MUST delete everything at this point. */
102 * From a consumer_data structure, allocate and add a consumer socket to the
105 * Return 0 on success, else negative value on error
107 int consumer_create_socket(struct consumer_data
*data
,
108 struct consumer_output
*output
)
111 struct consumer_socket
*socket
;
115 if (output
== NULL
|| data
->cmd_sock
< 0) {
117 * Not an error. Possible there is simply not spawned consumer or it's
118 * disabled for the tracing session asking the socket.
124 socket
= consumer_find_socket(data
->cmd_sock
, output
);
126 if (socket
== NULL
) {
127 socket
= consumer_allocate_socket(data
->cmd_sock
);
128 if (socket
== NULL
) {
133 socket
->registered
= 0;
134 socket
->lock
= &data
->lock
;
136 consumer_add_socket(socket
, output
);
140 DBG3("Consumer socket created (fd: %d) and added to output",
148 * Find a consumer_socket in a consumer_output hashtable. Read side lock must
149 * be acquired before calling this function and across use of the
150 * returned consumer_socket.
152 struct consumer_socket
*consumer_find_socket(int key
,
153 struct consumer_output
*consumer
)
155 struct lttng_ht_iter iter
;
156 struct lttng_ht_node_ulong
*node
;
157 struct consumer_socket
*socket
= NULL
;
159 /* Negative keys are lookup failures */
160 if (key
< 0 || consumer
== NULL
) {
164 lttng_ht_lookup(consumer
->socks
, (void *)((unsigned long) key
),
166 node
= lttng_ht_iter_get_node_ulong(&iter
);
168 socket
= caa_container_of(node
, struct consumer_socket
, node
);
175 * Allocate a new consumer_socket and return the pointer.
177 struct consumer_socket
*consumer_allocate_socket(int fd
)
179 struct consumer_socket
*socket
= NULL
;
181 socket
= zmalloc(sizeof(struct consumer_socket
));
182 if (socket
== NULL
) {
183 PERROR("zmalloc consumer socket");
188 lttng_ht_node_init_ulong(&socket
->node
, fd
);
195 * Add consumer socket to consumer output object. Read side lock must be
196 * acquired before calling this function.
198 void consumer_add_socket(struct consumer_socket
*sock
,
199 struct consumer_output
*consumer
)
204 lttng_ht_add_unique_ulong(consumer
->socks
, &sock
->node
);
208 * Delte consumer socket to consumer output object. Read side lock must be
209 * acquired before calling this function.
211 void consumer_del_socket(struct consumer_socket
*sock
,
212 struct consumer_output
*consumer
)
215 struct lttng_ht_iter iter
;
220 iter
.iter
.node
= &sock
->node
.node
;
221 ret
= lttng_ht_del(consumer
->socks
, &iter
);
226 * RCU destroy call function.
228 static void destroy_socket_rcu(struct rcu_head
*head
)
230 struct lttng_ht_node_ulong
*node
=
231 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
232 struct consumer_socket
*socket
=
233 caa_container_of(node
, struct consumer_socket
, node
);
239 * Destroy and free socket pointer in a call RCU. Read side lock must be
240 * acquired before calling this function.
242 void consumer_destroy_socket(struct consumer_socket
*sock
)
247 * We DO NOT close the file descriptor here since it is global to the
248 * session daemon and is closed only if the consumer dies or a custom
249 * consumer was registered,
251 if (sock
->registered
) {
252 DBG3("Consumer socket was registered. Closing fd %d", sock
->fd
);
253 lttcomm_close_unix_sock(sock
->fd
);
256 call_rcu(&sock
->node
.head
, destroy_socket_rcu
);
260 * Allocate and assign data to a consumer_output object.
262 * Return pointer to structure.
264 struct consumer_output
*consumer_create_output(enum consumer_dst_type type
)
266 struct consumer_output
*output
= NULL
;
268 output
= zmalloc(sizeof(struct consumer_output
));
269 if (output
== NULL
) {
270 PERROR("zmalloc consumer_output");
274 /* By default, consumer output is enabled */
277 output
->net_seq_index
= -1;
279 output
->socks
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
286 * Delete the consumer_output object from the list and free the ptr.
288 void consumer_destroy_output(struct consumer_output
*obj
)
295 struct lttng_ht_iter iter
;
296 struct consumer_socket
*socket
;
299 cds_lfht_for_each_entry(obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
300 consumer_del_socket(socket
, obj
);
301 consumer_destroy_socket(socket
);
305 /* Finally destroy HT */
306 lttng_ht_destroy(obj
->socks
);
313 * Copy consumer output and returned the newly allocated copy.
315 struct consumer_output
*consumer_copy_output(struct consumer_output
*obj
)
317 struct lttng_ht
*tmp_ht_ptr
;
318 struct lttng_ht_iter iter
;
319 struct consumer_socket
*socket
, *copy_sock
;
320 struct consumer_output
*output
;
324 output
= consumer_create_output(obj
->type
);
325 if (output
== NULL
) {
328 /* Avoid losing the HT reference after the memcpy() */
329 tmp_ht_ptr
= output
->socks
;
331 memcpy(output
, obj
, sizeof(struct consumer_output
));
333 /* Putting back the HT pointer and start copying socket(s). */
334 output
->socks
= tmp_ht_ptr
;
336 cds_lfht_for_each_entry(obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
337 /* Create new socket object. */
338 copy_sock
= consumer_allocate_socket(socket
->fd
);
339 if (copy_sock
== NULL
) {
343 copy_sock
->registered
= socket
->registered
;
344 copy_sock
->lock
= socket
->lock
;
345 consumer_add_socket(copy_sock
, output
);
352 consumer_destroy_output(output
);
357 * Set network URI to the consumer output object.
359 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
362 int consumer_set_network_uri(struct consumer_output
*obj
,
363 struct lttng_uri
*uri
)
366 char tmp_path
[PATH_MAX
];
367 char hostname
[HOST_NAME_MAX
];
368 struct lttng_uri
*dst_uri
= NULL
;
370 /* Code flow error safety net. */
374 switch (uri
->stype
) {
375 case LTTNG_STREAM_CONTROL
:
376 dst_uri
= &obj
->dst
.net
.control
;
377 obj
->dst
.net
.control_isset
= 1;
378 if (uri
->port
== 0) {
379 /* Assign default port. */
380 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
382 DBG3("Consumer control URI set with port %d", uri
->port
);
384 case LTTNG_STREAM_DATA
:
385 dst_uri
= &obj
->dst
.net
.data
;
386 obj
->dst
.net
.data_isset
= 1;
387 if (uri
->port
== 0) {
388 /* Assign default port. */
389 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
391 DBG3("Consumer data URI set with port %d", uri
->port
);
394 ERR("Set network uri type unknown %d", uri
->stype
);
398 ret
= uri_compare(dst_uri
, uri
);
400 /* Same URI, don't touch it and return success. */
401 DBG3("URI network compare are the same");
405 /* URIs were not equal, replacing it. */
406 memset(dst_uri
, 0, sizeof(struct lttng_uri
));
407 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
408 obj
->type
= CONSUMER_DST_NET
;
410 /* Handle subdir and add hostname in front. */
411 if (dst_uri
->stype
== LTTNG_STREAM_CONTROL
) {
412 /* Get hostname to append it in the pathname */
413 ret
= gethostname(hostname
, sizeof(hostname
));
415 PERROR("gethostname. Fallback on default localhost");
416 strncpy(hostname
, "localhost", sizeof(hostname
));
418 hostname
[sizeof(hostname
) - 1] = '\0';
420 /* Setup consumer subdir if none present in the control URI */
421 if (strlen(dst_uri
->subdir
) == 0) {
422 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
423 hostname
, obj
->subdir
);
425 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
426 hostname
, dst_uri
->subdir
);
429 PERROR("snprintf set consumer uri subdir");
433 strncpy(obj
->subdir
, tmp_path
, sizeof(obj
->subdir
));
434 DBG3("Consumer set network uri subdir path %s", tmp_path
);
445 * Send file descriptor to consumer via sock.
447 int consumer_send_fds(int sock
, int *fds
, size_t nb_fd
)
454 ret
= lttcomm_send_fds_unix_sock(sock
, fds
, nb_fd
);
456 PERROR("send consumer fds");
465 * Consumer send channel communication message structure to consumer.
467 int consumer_send_channel(int sock
, struct lttcomm_consumer_msg
*msg
)
474 ret
= lttcomm_send_unix_sock(sock
, msg
,
475 sizeof(struct lttcomm_consumer_msg
));
477 PERROR("send consumer channel");
486 * Init channel communication message structure.
488 void consumer_init_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
489 enum lttng_consumer_command cmd
,
491 uint64_t max_sb_size
,
494 unsigned int nb_init_streams
)
498 /* TODO: Args validation */
500 /* Zeroed structure */
501 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
505 msg
->u
.channel
.channel_key
= channel_key
;
506 msg
->u
.channel
.max_sb_size
= max_sb_size
;
507 msg
->u
.channel
.mmap_len
= mmap_len
;
508 msg
->u
.channel
.nb_init_streams
= nb_init_streams
;
512 * Init stream communication message structure.
514 void consumer_init_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
515 enum lttng_consumer_command cmd
,
519 enum lttng_event_output output
,
524 unsigned int metadata_flag
,
526 const char *pathname
,
527 unsigned int session_id
)
531 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
533 /* TODO: Args validation */
536 msg
->u
.stream
.channel_key
= channel_key
;
537 msg
->u
.stream
.stream_key
= stream_key
;
538 msg
->u
.stream
.state
= state
;
539 msg
->u
.stream
.output
= output
;
540 msg
->u
.stream
.mmap_len
= mmap_len
;
541 msg
->u
.stream
.uid
= uid
;
542 msg
->u
.stream
.gid
= gid
;
543 msg
->u
.stream
.net_index
= net_index
;
544 msg
->u
.stream
.metadata_flag
= metadata_flag
;
545 msg
->u
.stream
.session_id
= (uint64_t) session_id
;
546 strncpy(msg
->u
.stream
.name
, name
, sizeof(msg
->u
.stream
.name
));
547 msg
->u
.stream
.name
[sizeof(msg
->u
.stream
.name
) - 1] = '\0';
548 strncpy(msg
->u
.stream
.path_name
, pathname
,
549 sizeof(msg
->u
.stream
.path_name
));
550 msg
->u
.stream
.path_name
[sizeof(msg
->u
.stream
.path_name
) - 1] = '\0';
554 * Send stream communication structure to the consumer.
556 int consumer_send_stream(int sock
, struct consumer_output
*dst
,
557 struct lttcomm_consumer_msg
*msg
, int *fds
, size_t nb_fd
)
565 case CONSUMER_DST_NET
:
566 /* Consumer should send the stream on the network. */
567 msg
->u
.stream
.net_index
= dst
->net_seq_index
;
569 case CONSUMER_DST_LOCAL
:
570 /* Add stream file name to stream path */
571 strncat(msg
->u
.stream
.path_name
, "/",
572 sizeof(msg
->u
.stream
.path_name
) -
573 strlen(msg
->u
.stream
.path_name
) - 1);
574 strncat(msg
->u
.stream
.path_name
, msg
->u
.stream
.name
,
575 sizeof(msg
->u
.stream
.path_name
) -
576 strlen(msg
->u
.stream
.path_name
) - 1);
577 msg
->u
.stream
.path_name
[sizeof(msg
->u
.stream
.path_name
) - 1] = '\0';
578 /* Indicate that the stream is NOT network */
579 msg
->u
.stream
.net_index
= -1;
582 ERR("Consumer unknown output type (%d)", dst
->type
);
588 ret
= lttcomm_send_unix_sock(sock
, msg
,
589 sizeof(struct lttcomm_consumer_msg
));
591 PERROR("send consumer stream");
595 ret
= consumer_send_fds(sock
, fds
, nb_fd
);
605 * Send relayd socket to consumer associated with a session name.
607 * On success return positive value. On error, negative value.
609 int consumer_send_relayd_socket(int consumer_sock
,
610 struct lttcomm_sock
*sock
, struct consumer_output
*consumer
,
611 enum lttng_stream_type type
)
614 struct lttcomm_consumer_msg msg
;
616 /* Code flow error. Safety net. */
620 /* Bail out if consumer is disabled */
621 if (!consumer
->enabled
) {
626 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
628 * Assign network consumer output index using the temporary consumer since
629 * this call should only be made from within a set_consumer_uri() function
630 * call in the session daemon.
632 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
633 msg
.u
.relayd_sock
.type
= type
;
634 memcpy(&msg
.u
.relayd_sock
.sock
, sock
, sizeof(msg
.u
.relayd_sock
.sock
));
636 DBG3("Sending relayd sock info to consumer on %d", consumer_sock
);
637 ret
= lttcomm_send_unix_sock(consumer_sock
, &msg
, sizeof(msg
));
639 PERROR("send consumer relayd socket info");
643 DBG3("Sending relayd socket file descriptor to consumer");
644 ret
= consumer_send_fds(consumer_sock
, &sock
->fd
, 1);
649 DBG2("Consumer relayd socket sent");
656 * Set consumer subdirectory using the session name and a generated datetime if
657 * needed. This is appended to the current subdirectory.
659 int consumer_set_subdir(struct consumer_output
*consumer
,
660 const char *session_name
)
663 unsigned int have_default_name
= 0;
664 char datetime
[16], tmp_path
[PATH_MAX
];
669 assert(session_name
);
671 memset(tmp_path
, 0, sizeof(tmp_path
));
673 /* Flag if we have a default session. */
674 if (strncmp(session_name
, DEFAULT_SESSION_NAME
"-",
675 strlen(DEFAULT_SESSION_NAME
) + 1) == 0) {
676 have_default_name
= 1;
678 /* Get date and time for session path */
680 timeinfo
= localtime(&rawtime
);
681 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
684 if (have_default_name
) {
685 ret
= snprintf(tmp_path
, sizeof(tmp_path
),
686 "%s/%s", consumer
->subdir
, session_name
);
688 ret
= snprintf(tmp_path
, sizeof(tmp_path
),
689 "%s/%s-%s/", consumer
->subdir
, session_name
, datetime
);
692 PERROR("snprintf session name date");
696 strncpy(consumer
->subdir
, tmp_path
, sizeof(consumer
->subdir
));
697 DBG2("Consumer subdir set to %s", consumer
->subdir
);
704 * Ask the consumer if the data is ready to read (NOT pending) for the specific
707 * This function has a different behavior with the consumer i.e. that it waits
708 * for a reply from the consumer if yes or no the data is pending.
710 int consumer_is_data_pending(unsigned int id
,
711 struct consumer_output
*consumer
)
714 int32_t ret_code
= 0; /* Default is that the data is NOT pending */
715 struct consumer_socket
*socket
;
716 struct lttng_ht_iter iter
;
717 struct lttcomm_consumer_msg msg
;
721 msg
.cmd_type
= LTTNG_CONSUMER_DATA_PENDING
;
723 msg
.u
.data_pending
.session_id
= (uint64_t) id
;
725 DBG3("Consumer data pending for id %u", id
);
727 /* Send command for each consumer */
728 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
, socket
,
730 /* Code flow error */
731 assert(socket
->fd
>= 0);
733 pthread_mutex_lock(socket
->lock
);
735 ret
= lttcomm_send_unix_sock(socket
->fd
, &msg
, sizeof(msg
));
737 PERROR("send consumer data pending command");
738 pthread_mutex_unlock(socket
->lock
);
742 ret
= lttcomm_recv_unix_sock(socket
->fd
, &ret_code
, sizeof(ret_code
));
744 PERROR("recv consumer data pending status");
745 pthread_mutex_unlock(socket
->lock
);
749 pthread_mutex_unlock(socket
->lock
);
756 DBG("Consumer data pending ret %d", ret_code
);