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 * From a consumer_data structure, allocate and add a consumer socket to the
37 * Return 0 on success, else negative value on error
39 int consumer_create_socket(struct consumer_data
*data
,
40 struct consumer_output
*output
)
43 struct consumer_socket
*socket
;
47 if (output
== NULL
|| data
->cmd_sock
< 0) {
49 * Not an error. Possible there is simply not spawned consumer or it's
50 * disabled for the tracing session asking the socket.
56 socket
= consumer_find_socket(data
->cmd_sock
, output
);
59 socket
= consumer_allocate_socket(data
->cmd_sock
);
65 socket
->lock
= &data
->lock
;
67 consumer_add_socket(socket
, output
);
71 DBG3("Consumer socket created (fd: %d) and added to output",
79 * Find a consumer_socket in a consumer_output hashtable. Read side lock must
80 * be acquired before calling this function and across use of the
81 * returned consumer_socket.
83 struct consumer_socket
*consumer_find_socket(int key
,
84 struct consumer_output
*consumer
)
86 struct lttng_ht_iter iter
;
87 struct lttng_ht_node_ulong
*node
;
88 struct consumer_socket
*socket
= NULL
;
90 /* Negative keys are lookup failures */
91 if (key
< 0 || consumer
== NULL
) {
95 lttng_ht_lookup(consumer
->socks
, (void *)((unsigned long) key
),
97 node
= lttng_ht_iter_get_node_ulong(&iter
);
99 socket
= caa_container_of(node
, struct consumer_socket
, node
);
106 * Allocate a new consumer_socket and return the pointer.
108 struct consumer_socket
*consumer_allocate_socket(int fd
)
110 struct consumer_socket
*socket
= NULL
;
112 socket
= zmalloc(sizeof(struct consumer_socket
));
113 if (socket
== NULL
) {
114 PERROR("zmalloc consumer socket");
119 lttng_ht_node_init_ulong(&socket
->node
, fd
);
126 * Add consumer socket to consumer output object. Read side lock must be
127 * acquired before calling this function.
129 void consumer_add_socket(struct consumer_socket
*sock
,
130 struct consumer_output
*consumer
)
135 lttng_ht_add_unique_ulong(consumer
->socks
, &sock
->node
);
139 * Delte consumer socket to consumer output object. Read side lock must be
140 * acquired before calling this function.
142 void consumer_del_socket(struct consumer_socket
*sock
,
143 struct consumer_output
*consumer
)
146 struct lttng_ht_iter iter
;
151 iter
.iter
.node
= &sock
->node
.node
;
152 ret
= lttng_ht_del(consumer
->socks
, &iter
);
157 * RCU destroy call function.
159 static void destroy_socket_rcu(struct rcu_head
*head
)
161 struct lttng_ht_node_ulong
*node
=
162 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
163 struct consumer_socket
*socket
=
164 caa_container_of(node
, struct consumer_socket
, node
);
170 * Destroy and free socket pointer in a call RCU. Read side lock must be
171 * acquired before calling this function.
173 void consumer_destroy_socket(struct consumer_socket
*sock
)
178 * We DO NOT close the file descriptor here since it is global to the
179 * session daemon and is closed only if the consumer dies.
182 call_rcu(&sock
->node
.head
, destroy_socket_rcu
);
186 * Allocate and assign data to a consumer_output object.
188 * Return pointer to structure.
190 struct consumer_output
*consumer_create_output(enum consumer_dst_type type
)
192 struct consumer_output
*output
= NULL
;
194 output
= zmalloc(sizeof(struct consumer_output
));
195 if (output
== NULL
) {
196 PERROR("zmalloc consumer_output");
200 /* By default, consumer output is enabled */
203 output
->net_seq_index
= -1;
205 output
->socks
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
212 * Delete the consumer_output object from the list and free the ptr.
214 void consumer_destroy_output(struct consumer_output
*obj
)
221 struct lttng_ht_iter iter
;
222 struct consumer_socket
*socket
;
224 cds_lfht_for_each_entry(obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
225 consumer_destroy_socket(socket
);
233 * Copy consumer output and returned the newly allocated copy.
235 struct consumer_output
*consumer_copy_output(struct consumer_output
*obj
)
237 struct lttng_ht_iter iter
;
238 struct consumer_socket
*socket
, *copy_sock
;
239 struct consumer_output
*output
;
243 output
= consumer_create_output(obj
->type
);
244 if (output
== NULL
) {
248 memcpy(output
, obj
, sizeof(struct consumer_output
));
251 output
->socks
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
253 cds_lfht_for_each_entry(obj
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
254 /* Create new socket object. */
255 copy_sock
= consumer_allocate_socket(socket
->fd
);
256 if (copy_sock
== NULL
) {
260 copy_sock
->lock
= socket
->lock
;
261 consumer_add_socket(copy_sock
, output
);
268 consumer_destroy_output(output
);
273 * Set network URI to the consumer output object.
275 * Return 0 on success. Return 1 if the URI were equal. Else, negative value on
278 int consumer_set_network_uri(struct consumer_output
*obj
,
279 struct lttng_uri
*uri
)
282 char tmp_path
[PATH_MAX
];
283 char hostname
[HOST_NAME_MAX
];
284 struct lttng_uri
*dst_uri
= NULL
;
286 /* Code flow error safety net. */
290 switch (uri
->stype
) {
291 case LTTNG_STREAM_CONTROL
:
292 dst_uri
= &obj
->dst
.net
.control
;
293 obj
->dst
.net
.control_isset
= 1;
294 if (uri
->port
== 0) {
295 /* Assign default port. */
296 uri
->port
= DEFAULT_NETWORK_CONTROL_PORT
;
298 DBG3("Consumer control URI set with port %d", uri
->port
);
300 case LTTNG_STREAM_DATA
:
301 dst_uri
= &obj
->dst
.net
.data
;
302 obj
->dst
.net
.data_isset
= 1;
303 if (uri
->port
== 0) {
304 /* Assign default port. */
305 uri
->port
= DEFAULT_NETWORK_DATA_PORT
;
307 DBG3("Consumer data URI set with port %d", uri
->port
);
310 ERR("Set network uri type unknown %d", uri
->stype
);
314 ret
= uri_compare(dst_uri
, uri
);
316 /* Same URI, don't touch it and return success. */
317 DBG3("URI network compare are the same");
321 /* URIs were not equal, replacing it. */
322 memset(dst_uri
, 0, sizeof(struct lttng_uri
));
323 memcpy(dst_uri
, uri
, sizeof(struct lttng_uri
));
324 obj
->type
= CONSUMER_DST_NET
;
326 /* Handle subdir and add hostname in front. */
327 if (dst_uri
->stype
== LTTNG_STREAM_CONTROL
) {
328 /* Get hostname to append it in the pathname */
329 ret
= gethostname(hostname
, sizeof(hostname
));
331 PERROR("gethostname. Fallback on default localhost");
332 strncpy(hostname
, "localhost", sizeof(hostname
));
334 hostname
[sizeof(hostname
) - 1] = '\0';
336 /* Setup consumer subdir if none present in the control URI */
337 if (strlen(dst_uri
->subdir
) == 0) {
338 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
339 hostname
, obj
->subdir
);
341 ret
= snprintf(tmp_path
, sizeof(tmp_path
), "%s/%s",
342 hostname
, dst_uri
->subdir
);
345 PERROR("snprintf set consumer uri subdir");
349 strncpy(obj
->subdir
, tmp_path
, sizeof(obj
->subdir
));
350 DBG3("Consumer set network uri subdir path %s", tmp_path
);
361 * Send file descriptor to consumer via sock.
363 int consumer_send_fds(int sock
, int *fds
, size_t nb_fd
)
370 ret
= lttcomm_send_fds_unix_sock(sock
, fds
, nb_fd
);
372 PERROR("send consumer fds");
381 * Consumer send channel communication message structure to consumer.
383 int consumer_send_channel(int sock
, struct lttcomm_consumer_msg
*msg
)
390 ret
= lttcomm_send_unix_sock(sock
, msg
,
391 sizeof(struct lttcomm_consumer_msg
));
393 PERROR("send consumer channel");
402 * Init channel communication message structure.
404 void consumer_init_channel_comm_msg(struct lttcomm_consumer_msg
*msg
,
405 enum lttng_consumer_command cmd
,
407 uint64_t max_sb_size
,
413 /* TODO: Args validation */
415 /* Zeroed structure */
416 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
420 msg
->u
.channel
.channel_key
= channel_key
;
421 msg
->u
.channel
.max_sb_size
= max_sb_size
;
422 msg
->u
.channel
.mmap_len
= mmap_len
;
426 * Init stream communication message structure.
428 void consumer_init_stream_comm_msg(struct lttcomm_consumer_msg
*msg
,
429 enum lttng_consumer_command cmd
,
433 enum lttng_event_output output
,
438 unsigned int metadata_flag
,
440 const char *pathname
)
444 memset(msg
, 0, sizeof(struct lttcomm_consumer_msg
));
446 /* TODO: Args validation */
449 msg
->u
.stream
.channel_key
= channel_key
;
450 msg
->u
.stream
.stream_key
= stream_key
;
451 msg
->u
.stream
.state
= state
;
452 msg
->u
.stream
.output
= output
;
453 msg
->u
.stream
.mmap_len
= mmap_len
;
454 msg
->u
.stream
.uid
= uid
;
455 msg
->u
.stream
.gid
= gid
;
456 msg
->u
.stream
.net_index
= net_index
;
457 msg
->u
.stream
.metadata_flag
= metadata_flag
;
458 strncpy(msg
->u
.stream
.name
, name
, sizeof(msg
->u
.stream
.name
));
459 msg
->u
.stream
.name
[sizeof(msg
->u
.stream
.name
) - 1] = '\0';
460 strncpy(msg
->u
.stream
.path_name
, pathname
,
461 sizeof(msg
->u
.stream
.path_name
));
462 msg
->u
.stream
.path_name
[sizeof(msg
->u
.stream
.path_name
) - 1] = '\0';
466 * Send stream communication structure to the consumer.
468 int consumer_send_stream(int sock
, struct consumer_output
*dst
,
469 struct lttcomm_consumer_msg
*msg
, int *fds
, size_t nb_fd
)
477 case CONSUMER_DST_NET
:
478 /* Consumer should send the stream on the network. */
479 msg
->u
.stream
.net_index
= dst
->net_seq_index
;
481 case CONSUMER_DST_LOCAL
:
482 /* Add stream file name to stream path */
483 strncat(msg
->u
.stream
.path_name
, "/", sizeof(msg
->u
.stream
.path_name
));
484 strncat(msg
->u
.stream
.path_name
, msg
->u
.stream
.name
,
485 sizeof(msg
->u
.stream
.path_name
));
486 msg
->u
.stream
.path_name
[sizeof(msg
->u
.stream
.path_name
) - 1] = '\0';
487 /* Indicate that the stream is NOT network */
488 msg
->u
.stream
.net_index
= -1;
491 ERR("Consumer unknown output type (%d)", dst
->type
);
497 ret
= lttcomm_send_unix_sock(sock
, msg
,
498 sizeof(struct lttcomm_consumer_msg
));
500 PERROR("send consumer stream");
504 ret
= consumer_send_fds(sock
, fds
, nb_fd
);
514 * Send relayd socket to consumer associated with a session name.
516 * On success return positive value. On error, negative value.
518 int consumer_send_relayd_socket(int consumer_sock
,
519 struct lttcomm_sock
*sock
, struct consumer_output
*consumer
,
520 enum lttng_stream_type type
)
523 struct lttcomm_consumer_msg msg
;
525 /* Code flow error. Safety net. */
529 /* Bail out if consumer is disabled */
530 if (!consumer
->enabled
) {
535 msg
.cmd_type
= LTTNG_CONSUMER_ADD_RELAYD_SOCKET
;
537 * Assign network consumer output index using the temporary consumer since
538 * this call should only be made from within a set_consumer_uri() function
539 * call in the session daemon.
541 msg
.u
.relayd_sock
.net_index
= consumer
->net_seq_index
;
542 msg
.u
.relayd_sock
.type
= type
;
543 memcpy(&msg
.u
.relayd_sock
.sock
, sock
, sizeof(msg
.u
.relayd_sock
.sock
));
545 DBG3("Sending relayd sock info to consumer on %d", consumer_sock
);
546 ret
= lttcomm_send_unix_sock(consumer_sock
, &msg
, sizeof(msg
));
548 PERROR("send consumer relayd socket info");
552 DBG3("Sending relayd socket file descriptor to consumer");
553 ret
= consumer_send_fds(consumer_sock
, &sock
->fd
, 1);
558 DBG2("Consumer relayd socket sent");
565 * Send destroy relayd command to consumer.
567 * On success return positive value. On error, negative value.
569 int consumer_send_destroy_relayd(struct consumer_socket
*sock
,
570 struct consumer_output
*consumer
)
573 struct lttcomm_consumer_msg msg
;
578 DBG2("Sending destroy relayd command to consumer...");
580 /* Bail out if consumer is disabled */
581 if (!consumer
->enabled
) {
583 DBG3("Consumer is disabled");
587 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_RELAYD
;
588 msg
.u
.destroy_relayd
.net_seq_idx
= consumer
->net_seq_index
;
590 pthread_mutex_lock(sock
->lock
);
591 ret
= lttcomm_send_unix_sock(sock
->fd
, &msg
, sizeof(msg
));
592 pthread_mutex_unlock(sock
->lock
);
594 PERROR("send consumer destroy relayd command");
598 DBG2("Consumer send destroy relayd command done");