+ call_rcu(&ua_chan->rcu_head, delete_ust_app_channel_rcu);
+}
+
+/*
+ * Push metadata to consumer socket.
+ *
+ * RCU read-side lock must be held to guarantee existance of socket.
+ * Must be called with the ust app session lock held.
+ * Must be called with the registry lock held.
+ *
+ * On success, return the len of metadata pushed or else a negative value.
+ * Returning a -EPIPE return value means we could not send the metadata,
+ * but it can be caused by recoverable errors (e.g. the application has
+ * terminated concurrently).
+ */
+ssize_t ust_app_push_metadata(struct ust_registry_session *registry,
+ struct consumer_socket *socket, int send_zero_data)
+{
+ int ret;
+ char *metadata_str = NULL;
+ size_t len, offset;
+ ssize_t ret_val;
+
+ assert(registry);
+ assert(socket);
+
+ /*
+ * Means that no metadata was assigned to the session. This can
+ * happens if no start has been done previously.
+ */
+ if (!registry->metadata_key) {
+ return 0;
+ }
+
+ /*
+ * On a push metadata error either the consumer is dead or the
+ * metadata channel has been destroyed because its endpoint
+ * might have died (e.g: relayd), or because the application has
+ * exited. If so, the metadata closed flag is set to 1 so we
+ * deny pushing metadata again which is not valid anymore on the
+ * consumer side.
+ */
+ if (registry->metadata_closed) {
+ return -EPIPE;
+ }
+
+ offset = registry->metadata_len_sent;
+ len = registry->metadata_len - registry->metadata_len_sent;
+ if (len == 0) {
+ DBG3("No metadata to push for metadata key %" PRIu64,
+ registry->metadata_key);
+ ret_val = len;
+ if (send_zero_data) {
+ DBG("No metadata to push");
+ goto push_data;
+ }
+ goto end;
+ }
+
+ /* Allocate only what we have to send. */
+ metadata_str = zmalloc(len);
+ if (!metadata_str) {
+ PERROR("zmalloc ust app metadata string");
+ ret_val = -ENOMEM;
+ goto error;
+ }
+ /* Copy what we haven't send out. */
+ memcpy(metadata_str, registry->metadata + offset, len);
+ registry->metadata_len_sent += len;
+
+push_data:
+ ret = consumer_push_metadata(socket, registry->metadata_key,
+ metadata_str, len, offset);
+ if (ret < 0) {
+ /*
+ * There is an acceptable race here between the registry
+ * metadata key assignment and the creation on the
+ * consumer. The session daemon can concurrently push
+ * metadata for this registry while being created on the
+ * consumer since the metadata key of the registry is
+ * assigned *before* it is setup to avoid the consumer
+ * to ask for metadata that could possibly be not found
+ * in the session daemon.
+ *
+ * The metadata will get pushed either by the session
+ * being stopped or the consumer requesting metadata if
+ * that race is triggered.
+ */
+ if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) {
+ ret = 0;
+ }
+
+ /*
+ * Update back the actual metadata len sent since it
+ * failed here.
+ */
+ registry->metadata_len_sent -= len;
+ ret_val = ret;
+ goto error_push;
+ }
+
+ free(metadata_str);
+ return len;
+
+end:
+error:
+ if (ret_val) {
+ /*
+ * On error, flag the registry that the metadata is
+ * closed. We were unable to push anything and this
+ * means that either the consumer is not responding or
+ * the metadata cache has been destroyed on the
+ * consumer.
+ */
+ registry->metadata_closed = 1;
+ }
+error_push:
+ free(metadata_str);
+ return ret_val;
+}
+
+/*
+ * For a given application and session, push metadata to consumer.
+ * Either sock or consumer is required : if sock is NULL, the default
+ * socket to send the metadata is retrieved from consumer, if sock
+ * is not NULL we use it to send the metadata.
+ * RCU read-side lock must be held while calling this function,
+ * therefore ensuring existance of registry. It also ensures existance
+ * of socket throughout this function.
+ *
+ * Return 0 on success else a negative error.
+ * Returning a -EPIPE return value means we could not send the metadata,
+ * but it can be caused by recoverable errors (e.g. the application has
+ * terminated concurrently).
+ */
+static int push_metadata(struct ust_registry_session *registry,
+ struct consumer_output *consumer)
+{
+ int ret_val;
+ ssize_t ret;
+ struct consumer_socket *socket;
+
+ assert(registry);
+ assert(consumer);
+
+ pthread_mutex_lock(®istry->lock);
+ if (registry->metadata_closed) {
+ ret_val = -EPIPE;
+ goto error;
+ }
+
+ /* Get consumer socket to use to push the metadata.*/
+ socket = consumer_find_socket_by_bitness(registry->bits_per_long,
+ consumer);
+ if (!socket) {
+ ret_val = -1;
+ goto error;
+ }
+
+ ret = ust_app_push_metadata(registry, socket, 0);
+ if (ret < 0) {
+ ret_val = ret;
+ goto error;
+ }
+ pthread_mutex_unlock(®istry->lock);
+ return 0;
+
+error:
+ pthread_mutex_unlock(®istry->lock);
+ return ret_val;
+}
+
+/*
+ * Send to the consumer a close metadata command for the given session. Once
+ * done, the metadata channel is deleted and the session metadata pointer is
+ * nullified. The session lock MUST be held unless the application is
+ * in the destroy path.
+ *
+ * Return 0 on success else a negative value.
+ */
+static int close_metadata(struct ust_registry_session *registry,
+ struct consumer_output *consumer)
+{
+ int ret;
+ struct consumer_socket *socket;
+
+ assert(registry);
+ assert(consumer);
+
+ rcu_read_lock();
+
+ pthread_mutex_lock(®istry->lock);
+
+ if (!registry->metadata_key || registry->metadata_closed) {
+ ret = 0;
+ goto end;
+ }
+
+ /* Get consumer socket to use to push the metadata.*/
+ socket = consumer_find_socket_by_bitness(registry->bits_per_long,
+ consumer);
+ if (!socket) {
+ ret = -1;
+ goto error;
+ }
+
+ ret = consumer_close_metadata(socket, registry->metadata_key);
+ if (ret < 0) {
+ goto error;
+ }
+
+error:
+ /*
+ * Metadata closed. Even on error this means that the consumer is not
+ * responding or not found so either way a second close should NOT be emit
+ * for this registry.
+ */
+ registry->metadata_closed = 1;
+end:
+ pthread_mutex_unlock(®istry->lock);
+ rcu_read_unlock();
+ return ret;
+}
+
+/*
+ * We need to execute ht_destroy outside of RCU read-side critical
+ * section and outside of call_rcu thread, so we postpone its execution
+ * using ht_cleanup_push. It is simpler than to change the semantic of
+ * the many callers of delete_ust_app_session().
+ */
+static
+void delete_ust_app_session_rcu(struct rcu_head *head)
+{
+ struct ust_app_session *ua_sess =
+ caa_container_of(head, struct ust_app_session, rcu_head);
+
+ ht_cleanup_push(ua_sess->channels);
+ free(ua_sess);