+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;
+ }
+
+ client->uid = LTTNG_SOCK_GET_UID_CRED(
+ &client->communication.inbound.creds);
+ client->gid = LTTNG_SOCK_GET_GID_CRED(
+ &client->communication.inbound.creds);
+ DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
+ client->uid, client->gid, (int) client->major,
+ (int) client->minor);
+
+ if (handshake_client->major !=
+ LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR) {
+ status = LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION;
+ }
+
+ pthread_mutex_lock(&client->lock);
+ /* Outgoing queue will be flushed when the command reply is sent. */
+ ret = lttng_dynamic_buffer_append(
+ &client->communication.outbound.payload.buffer, send_buffer,
+ sizeof(send_buffer));
+ if (ret) {
+ ERR("[notification-thread] Failed to send protocol version to notification channel client");
+ goto end_unlock;
+ }
+
+ client->validated = true;
+ client->communication.active = true;
+ pthread_mutex_unlock(&client->lock);
+
+ /* Set reception state to receive the next message header. */
+ ret = client_reset_inbound_state(client);
+ if (ret) {
+ ERR("[notification-thread] Failed to reset client communication's inbound state");
+ goto end;
+ }
+
+ /* Flushes the outgoing queue. */
+ ret = client_send_command_reply(client, state, status);
+ if (ret) {
+ ERR("[notification-thread] Failed to send reply to notification channel client");
+ goto end;
+ }
+
+ goto end;
+end_unlock:
+ pthread_mutex_unlock(&client->lock);
+end:
+ return ret;
+}
+
+static
+int client_handle_message_subscription(
+ struct notification_client *client,
+ enum lttng_notification_channel_message_type msg_type,
+ struct notification_thread_state *state)
+{
+ int ret;
+ struct lttng_condition *condition;
+ enum lttng_notification_channel_status status =
+ LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
+ struct lttng_payload_view condition_view =
+ lttng_payload_view_from_payload(
+ &client->communication.inbound.payload,
+ 0, -1);
+ size_t expected_condition_size;
+
+ /*
+ * No need to lock client to sample the inbound state as the only
+ * other thread accessing clients (action executor) only uses the
+ * outbound state.
+ */
+ expected_condition_size = client->communication.inbound.payload.buffer.size;
+ ret = lttng_condition_create_from_payload(&condition_view, &condition);
+ if (ret != expected_condition_size) {
+ ERR("[notification-thread] Malformed condition received from client");
+ goto end;
+ }
+
+ if (msg_type == LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE) {
+ ret = notification_thread_client_subscribe(
+ client, condition, state, &status);
+ } else {
+ ret = notification_thread_client_unsubscribe(
+ client, condition, state, &status);
+ }
+
+ if (ret) {
+ goto end;
+ }
+
+ /* Set reception state to receive the next message header. */
+ ret = client_reset_inbound_state(client);
+ if (ret) {
+ ERR("[notification-thread] Failed to reset client communication's inbound state");
+ goto end;
+ }
+
+ ret = client_send_command_reply(client, state, status);
+ if (ret) {
+ ERR("[notification-thread] Failed to send reply to notification channel client");
+ goto end;
+ }
+
+end:
+ return ret;
+}
+