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_iter iter
;
318 struct consumer_socket
*socket
, *copy_sock
;
319 struct consumer_output
*output
;
323 output
= consumer_create_output(obj
->type
);
324 if (output
== NULL
) {
328 memcpy(output
, obj
, sizeof(struct consumer_output
));
331 output
->socks
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
333 cds_lfht_for_each_entry(obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
334 /* Create new socket object. */
335 copy_sock
= consumer_allocate_socket(socket
->fd
);
336 if (copy_sock
== NULL
) {
340 copy_sock
->lock
= socket
->lock
;
341 consumer_add_socket(copy_sock
, output
);
348 consumer_destroy_output(output
);
353 * Set network URI to the consumer output object.
355 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
358 int consumer_set_network_uri(struct consumer_output
*obj
,
359 struct lttng_uri
*uri
)
362 char tmp_path
[PATH_MAX
];
363 char hostname
[HOST_NAME_MAX
];
364 struct lttng_uri
*dst_uri
= NULL
;
366 /* Code flow error safety net. */
370 switch (uri
->stype
) {
371 case LTTNG_STREAM_CONTROL
:
372 dst_uri
= &obj
->dst
.net
.control
;
373 obj
->dst
.net
.control_isset
= 1;
374 if (uri
->port
== 0) {
375 /* Assign default port. */
376 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
378 DBG3("Consumer control URI set with port %d", uri
->port
);
380 case LTTNG_STREAM_DATA
:
381 dst_uri
= &obj
->dst
.net
.data
;
382 obj
->dst
.net
.data_isset
= 1;
383 if (uri
->port
== 0) {
384 /* Assign default port. */
385 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
387 DBG3("Consumer data URI set with port %d", uri
->port
);
390 ERR("Set network uri type unknown %d", uri
->stype
);
394 ret
= uri_compare(dst_uri
, uri
);
396 /* Same URI, don't touch it and return success. */
397 DBG3("URI network compare are the same");
401 /* URIs were not equal, replacing it. */
402 memset(dst_uri
, 0, sizeof(struct lttng_uri
));
403 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
404 obj
->type
= CONSUMER_DST_NET
;
406 /* Handle subdir and add hostname in front. */
407 if (dst_uri
->stype
== LTTNG_STREAM_CONTROL
) {
408 /* Get hostname to append it in the pathname */
409 ret
= gethostname(hostname
, sizeof(hostname
));
411 PERROR("gethostname. Fallback on default localhost");
412 strncpy(hostname
, "localhost", sizeof(hostname
));
414 hostname
[sizeof(hostname
) - 1] = '\0';
416 /* Setup consumer subdir if none present in the control URI */
417 if (strlen(dst_uri
->subdir
) == 0) {
418 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
419 hostname
, obj
->subdir
);
421 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
422 hostname
, dst_uri
->subdir
);
425 PERROR("snprintf set consumer uri subdir");
429 strncpy(obj
->subdir
, tmp_path
, sizeof(obj
->subdir
));
430 DBG3("Consumer set network uri subdir path %s", tmp_path
);
441 * Send file descriptor to consumer via sock.
443 int consumer_send_fds(int sock
, int *fds
, size_t nb_fd
)
450 ret
= lttcomm_send_fds_unix_sock(sock
, fds
, nb_fd
);
452 PERROR("send consumer fds");
461 * Consumer send channel communication message structure to consumer.
463 int consumer_send_channel(int sock
, struct lttcomm_consumer_msg
*msg
)
470 ret
= lttcomm_send_unix_sock(sock
, msg
,
471 sizeof(struct lttcomm_consumer_msg
));
473 PERROR("send consumer channel");
482 * Init channel communication message structure.
484 void consumer_init_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
485 enum lttng_consumer_command cmd
,
487 uint64_t max_sb_size
,
490 unsigned int nb_init_streams
)
494 /* TODO: Args validation */
496 /* Zeroed structure */
497 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
501 msg
->u
.channel
.channel_key
= channel_key
;
502 msg
->u
.channel
.max_sb_size
= max_sb_size
;
503 msg
->u
.channel
.mmap_len
= mmap_len
;
504 msg
->u
.channel
.nb_init_streams
= nb_init_streams
;
508 * Init stream communication message structure.
510 void consumer_init_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
511 enum lttng_consumer_command cmd
,
515 enum lttng_event_output output
,
520 unsigned int metadata_flag
,
522 const char *pathname
)
526 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
528 /* TODO: Args validation */
531 msg
->u
.stream
.channel_key
= channel_key
;
532 msg
->u
.stream
.stream_key
= stream_key
;
533 msg
->u
.stream
.state
= state
;
534 msg
->u
.stream
.output
= output
;
535 msg
->u
.stream
.mmap_len
= mmap_len
;
536 msg
->u
.stream
.uid
= uid
;
537 msg
->u
.stream
.gid
= gid
;
538 msg
->u
.stream
.net_index
= net_index
;
539 msg
->u
.stream
.metadata_flag
= metadata_flag
;
540 strncpy(msg
->u
.stream
.name
, name
, sizeof(msg
->u
.stream
.name
));
541 msg
->u
.stream
.name
[sizeof(msg
->u
.stream
.name
) - 1] = '\0';
542 strncpy(msg
->u
.stream
.path_name
, pathname
,
543 sizeof(msg
->u
.stream
.path_name
));
544 msg
->u
.stream
.path_name
[sizeof(msg
->u
.stream
.path_name
) - 1] = '\0';
548 * Send stream communication structure to the consumer.
550 int consumer_send_stream(int sock
, struct consumer_output
*dst
,
551 struct lttcomm_consumer_msg
*msg
, int *fds
, size_t nb_fd
)
559 case CONSUMER_DST_NET
:
560 /* Consumer should send the stream on the network. */
561 msg
->u
.stream
.net_index
= dst
->net_seq_index
;
563 case CONSUMER_DST_LOCAL
:
564 /* Add stream file name to stream path */
565 strncat(msg
->u
.stream
.path_name
, "/",
566 sizeof(msg
->u
.stream
.path_name
) -
567 strlen(msg
->u
.stream
.path_name
) - 1);
568 strncat(msg
->u
.stream
.path_name
, msg
->u
.stream
.name
,
569 sizeof(msg
->u
.stream
.path_name
) -
570 strlen(msg
->u
.stream
.path_name
) - 1);
571 msg
->u
.stream
.path_name
[sizeof(msg
->u
.stream
.path_name
) - 1] = '\0';
572 /* Indicate that the stream is NOT network */
573 msg
->u
.stream
.net_index
= -1;
576 ERR("Consumer unknown output type (%d)", dst
->type
);
582 ret
= lttcomm_send_unix_sock(sock
, msg
,
583 sizeof(struct lttcomm_consumer_msg
));
585 PERROR("send consumer stream");
589 ret
= consumer_send_fds(sock
, fds
, nb_fd
);
599 * Send relayd socket to consumer associated with a session name.
601 * On success return positive value. On error, negative value.
603 int consumer_send_relayd_socket(int consumer_sock
,
604 struct lttcomm_sock
*sock
, struct consumer_output
*consumer
,
605 enum lttng_stream_type type
)
608 struct lttcomm_consumer_msg msg
;
610 /* Code flow error. Safety net. */
614 /* Bail out if consumer is disabled */
615 if (!consumer
->enabled
) {
620 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
622 * Assign network consumer output index using the temporary consumer since
623 * this call should only be made from within a set_consumer_uri() function
624 * call in the session daemon.
626 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
627 msg
.u
.relayd_sock
.type
= type
;
628 memcpy(&msg
.u
.relayd_sock
.sock
, sock
, sizeof(msg
.u
.relayd_sock
.sock
));
630 DBG3("Sending relayd sock info to consumer on %d", consumer_sock
);
631 ret
= lttcomm_send_unix_sock(consumer_sock
, &msg
, sizeof(msg
));
633 PERROR("send consumer relayd socket info");
637 DBG3("Sending relayd socket file descriptor to consumer");
638 ret
= consumer_send_fds(consumer_sock
, &sock
->fd
, 1);
643 DBG2("Consumer relayd socket sent");
650 * Set consumer subdirectory using the session name and a generated datetime if
651 * needed. This is appended to the current subdirectory.
653 int consumer_set_subdir(struct consumer_output
*consumer
,
654 const char *session_name
)
657 unsigned int have_default_name
= 0;
658 char datetime
[16], tmp_path
[PATH_MAX
];
663 assert(session_name
);
665 memset(tmp_path
, 0, sizeof(tmp_path
));
667 /* Flag if we have a default session. */
668 if (strncmp(session_name
, DEFAULT_SESSION_NAME
"-",
669 strlen(DEFAULT_SESSION_NAME
) + 1) == 0) {
670 have_default_name
= 1;
672 /* Get date and time for session path */
674 timeinfo
= localtime(&rawtime
);
675 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
678 if (have_default_name
) {
679 ret
= snprintf(tmp_path
, sizeof(tmp_path
),
680 "%s/%s", consumer
->subdir
, session_name
);
682 ret
= snprintf(tmp_path
, sizeof(tmp_path
),
683 "%s/%s-%s/", consumer
->subdir
, session_name
, datetime
);
686 PERROR("snprintf session name date");
690 strncpy(consumer
->subdir
, tmp_path
, sizeof(consumer
->subdir
));
691 DBG2("Consumer subdir set to %s", consumer
->subdir
);