+ }
+
+send_fds:
+ /* No fds to send, transmission is complete. */
+ if (fds_to_send_count == 0) {
+ status = CLIENT_TRANSMISSION_STATUS_COMPLETE;
+ goto end;
+ }
+
+ ret = lttcomm_send_payload_view_fds_unix_sock_non_block(
+ client->socket, &pv);
+ if (ret < 0) {
+ /* Generic error, disable the client's communication. */
+ ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
+ client->socket);
+ client->communication.active = false;
+ status = CLIENT_TRANSMISSION_STATUS_FAIL;
+ goto end;
+ } else if (ret == 0) {
+ /* Nothing could be sent. */
+ status = CLIENT_TRANSMISSION_STATUS_QUEUED;
+ } else {
+ /* Fd passing is an all or nothing kind of thing. */
+ status = CLIENT_TRANSMISSION_STATUS_COMPLETE;
+ /*
+ * The payload _fd_array count is used later to
+ * check if there is notifications queued. So although the
+ * direct caller knows that the transmission is complete, we
+ * need to clear the _fd_array for the queuing check.
+ */
+ lttng_dynamic_pointer_array_clear(
+ &client->communication.outbound.payload
+ ._fd_handles);
+ }
+
+end:
+ if (status == CLIENT_TRANSMISSION_STATUS_COMPLETE) {
+ client->communication.outbound.queued_command_reply = false;
+ client->communication.outbound.dropped_notification = false;
+ lttng_payload_clear(&client->communication.outbound.payload);
+ }
+
+ return status;
+error:
+ return CLIENT_TRANSMISSION_STATUS_ERROR;
+}
+
+static
+bool client_has_outbound_data_left(
+ const struct notification_client *client)
+{
+ const struct lttng_payload_view pv = lttng_payload_view_from_payload(
+ &client->communication.outbound.payload, 0, -1);
+ const bool has_data = pv.buffer.size != 0;
+ const bool has_fds = lttng_payload_view_get_fd_handle_count(&pv);
+
+ return has_data || has_fds;
+}
+
+/* Client lock must _not_ be held by the caller. */
+static
+int client_send_command_reply(struct notification_client *client,
+ struct notification_thread_state *state,
+ enum lttng_notification_channel_status status)
+{
+ int ret;
+ struct lttng_notification_channel_command_reply reply = {
+ .status = (int8_t) status,
+ };
+ struct lttng_notification_channel_message msg = {
+ .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY,
+ .size = sizeof(reply),
+ };
+ char buffer[sizeof(msg) + sizeof(reply)];
+ enum client_transmission_status transmission_status;
+
+ memcpy(buffer, &msg, sizeof(msg));
+ memcpy(buffer + sizeof(msg), &reply, sizeof(reply));
+ DBG("[notification-thread] Send command reply (%i)", (int) status);
+
+ pthread_mutex_lock(&client->lock);
+ if (client->communication.outbound.queued_command_reply) {
+ /* Protocol error. */
+ goto error_unlock;
+ }
+
+ /* Enqueue buffer to outgoing queue and flush it. */
+ ret = lttng_dynamic_buffer_append(
+ &client->communication.outbound.payload.buffer,
+ buffer, sizeof(buffer));
+ if (ret) {
+ goto error_unlock;
+ }
+
+ transmission_status = client_flush_outgoing_queue(client);
+
+ if (client_has_outbound_data_left(client)) {
+ /* Queue could not be emptied. */
+ client->communication.outbound.queued_command_reply = true;
+ }
+
+ pthread_mutex_unlock(&client->lock);
+ ret = client_handle_transmission_status(
+ client, transmission_status, state);
+ if (ret) {
+ goto error;
+ }
+
+ return 0;
+error_unlock:
+ pthread_mutex_unlock(&client->lock);
+error:
+ return -1;
+}
+
+static
+int client_handle_message_unknown(struct notification_client *client,
+ struct notification_thread_state *state)
+{
+ int ret;
+ /*
+ * Receiving message header. The function will be called again
+ * once the rest of the message as been received and can be
+ * interpreted.
+ */
+ const struct lttng_notification_channel_message *msg;
+
+ assert(sizeof(*msg) == client->communication.inbound.payload.buffer.size);
+ msg = (const struct lttng_notification_channel_message *)
+ client->communication.inbound.payload.buffer.data;
+
+ if (msg->size == 0 ||
+ msg->size > DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE) {
+ ERR("[notification-thread] Invalid notification channel message: length = %u",
+ msg->size);
+ ret = -1;
+ goto end;
+ }
+
+ switch (msg->type) {
+ case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE:
+ case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE:
+ case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE:
+ break;
+ default:
+ ret = -1;
+ ERR("[notification-thread] Invalid notification channel message: unexpected message type");
+ goto end;
+ }
+
+ client->communication.inbound.bytes_to_receive = msg->size;
+ client->communication.inbound.fds_to_receive = msg->fds;
+ client->communication.inbound.msg_type =
+ (enum lttng_notification_channel_message_type) msg->type;
+ ret = lttng_dynamic_buffer_set_size(
+ &client->communication.inbound.payload.buffer, msg->size);
+
+ /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
+ msg = NULL;
+end:
+ return ret;
+}
+
+static
+int client_handle_message_handshake(struct notification_client *client,
+ struct notification_thread_state *state)
+{
+ int ret;
+ struct lttng_notification_channel_command_handshake *handshake_client;
+ const struct lttng_notification_channel_command_handshake handshake_reply = {
+ .major = LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR,
+ .minor = LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR,
+ };
+ const struct lttng_notification_channel_message msg_header = {
+ .type = LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE,
+ .size = sizeof(handshake_reply),
+ };
+ enum lttng_notification_channel_status status =
+ LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
+ char send_buffer[sizeof(msg_header) + sizeof(handshake_reply)];
+
+ memcpy(send_buffer, &msg_header, sizeof(msg_header));
+ memcpy(send_buffer + sizeof(msg_header), &handshake_reply,
+ sizeof(handshake_reply));
+
+ handshake_client =
+ (struct lttng_notification_channel_command_handshake *)
+ client->communication.inbound.payload.buffer
+ .data;
+ client->major = handshake_client->major;
+ client->minor = handshake_client->minor;
+ if (!client->communication.inbound.creds_received) {
+ ERR("[notification-thread] No credentials received from client");
+ ret = -1;
+ goto end;
+ }