Fix: Revert of 814b4934e2604a419bcb8eec57c0450dbb47e2c3
[lttng-tools.git] / src / bin / lttng-sessiond / notification-thread-events.cpp
index cf18f63f7f2d340a0a01703d046bf97a88d0b26a..da4f8a4b2ad515f601b90435fe696acde129beb0 100644 (file)
@@ -6,46 +6,46 @@
  */
 
 #include "lttng/action/action.h"
-#include "lttng/trigger/trigger-internal.h"
+#include "lttng/trigger/trigger-internal.hpp"
 #define _LGPL_SOURCE
 #include <urcu.h>
 #include <urcu/rculfhash.h>
 
-#include <common/defaults.h>
-#include <common/error.h>
-#include <common/futex.h>
-#include <common/unix.h>
-#include <common/dynamic-buffer.h>
-#include <common/hashtable/utils.h>
-#include <common/sessiond-comm/sessiond-comm.h>
-#include <common/macros.h>
+#include <common/defaults.hpp>
+#include <common/error.hpp>
+#include <common/futex.hpp>
+#include <common/unix.hpp>
+#include <common/dynamic-buffer.hpp>
+#include <common/hashtable/utils.hpp>
+#include <common/sessiond-comm/sessiond-comm.hpp>
+#include <common/macros.hpp>
 #include <lttng/condition/condition.h>
-#include <lttng/action/action-internal.h>
-#include <lttng/action/list-internal.h>
-#include <lttng/domain-internal.h>
-#include <lttng/notification/notification-internal.h>
-#include <lttng/condition/condition-internal.h>
-#include <lttng/condition/buffer-usage-internal.h>
-#include <lttng/condition/session-consumed-size-internal.h>
-#include <lttng/condition/session-rotation-internal.h>
-#include <lttng/condition/event-rule-matches-internal.h>
-#include <lttng/domain-internal.h>
-#include <lttng/notification/channel-internal.h>
-#include <lttng/trigger/trigger-internal.h>
-#include <lttng/event-rule/event-rule-internal.h>
+#include <lttng/action/action-internal.hpp>
+#include <lttng/action/list-internal.hpp>
+#include <lttng/domain-internal.hpp>
+#include <lttng/notification/notification-internal.hpp>
+#include <lttng/condition/condition-internal.hpp>
+#include <lttng/condition/buffer-usage-internal.hpp>
+#include <lttng/condition/session-consumed-size-internal.hpp>
+#include <lttng/condition/session-rotation-internal.hpp>
+#include <lttng/condition/event-rule-matches-internal.hpp>
+#include <lttng/domain-internal.hpp>
+#include <lttng/notification/channel-internal.hpp>
+#include <lttng/trigger/trigger-internal.hpp>
+#include <lttng/event-rule/event-rule-internal.hpp>
 
 #include <time.h>
 #include <unistd.h>
 #include <inttypes.h>
 #include <fcntl.h>
 
-#include "condition-internal.h"
-#include "event-notifier-error-accounting.h"
-#include "notification-thread.h"
-#include "notification-thread-events.h"
-#include "notification-thread-commands.h"
-#include "lttng-sessiond.h"
-#include "kernel.h"
+#include "condition-internal.hpp"
+#include "event-notifier-error-accounting.hpp"
+#include "notification-thread.hpp"
+#include "notification-thread-events.hpp"
+#include "notification-thread-commands.hpp"
+#include "lttng-sessiond.hpp"
+#include "kernel.hpp"
 
 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
@@ -569,7 +569,7 @@ struct session_info *session_info_create(const char *name, uid_t uid, gid_t gid,
 
        LTTNG_ASSERT(name);
 
-       session_info = (struct session_info *) zmalloc(sizeof(*session_info));
+       session_info = zmalloc<struct session_info>();
        if (!session_info) {
                goto end;
        }
@@ -623,7 +623,7 @@ struct channel_info *channel_info_create(const char *channel_name,
                struct channel_key *channel_key, uint64_t channel_capacity,
                struct session_info *session_info)
 {
-       struct channel_info *channel_info = (struct channel_info *) zmalloc(sizeof(*channel_info));
+       struct channel_info *channel_info = zmalloc<struct channel_info>();
 
        if (!channel_info) {
                goto end;
@@ -723,7 +723,7 @@ struct notification_client_list *notification_client_list_create(
        struct cds_lfht_iter iter;
        struct notification_client_list *client_list;
 
-       client_list = (notification_client_list *) zmalloc(sizeof(*client_list));
+       client_list = zmalloc<notification_client_list>();
        if (!client_list) {
                PERROR("Failed to allocate notification client list");
                goto end;
@@ -755,7 +755,7 @@ struct notification_client_list *notification_client_list_create(
                        continue;
                }
 
-               client_list_element = (notification_client_list_element *) zmalloc(sizeof(*client_list_element));
+               client_list_element = zmalloc<notification_client_list_element>();
                if (!client_list_element) {
                        goto error_put_client_list;
                }
@@ -1031,8 +1031,15 @@ int evaluate_condition_for_client(const struct lttng_trigger *trigger,
        struct lttng_evaluation *evaluation = NULL;
        struct notification_client_list client_list = {
                .lock = PTHREAD_MUTEX_INITIALIZER,
+               .ref = {},
+               .condition = NULL,
+               .triggers_list = {},
+               .clients_list = {},
+               .notification_trigger_clients_ht = NULL,
+               .notification_trigger_clients_ht_node = {},
+               .rcu_node = {},
        };
-       struct notification_client_list_element client_list_element = { 0 };
+       struct notification_client_list_element client_list_element = {};
        uid_t object_uid = 0;
        gid_t object_gid = 0;
 
@@ -1116,12 +1123,12 @@ int notification_thread_client_subscribe(struct notification_client *client,
                }
        }
 
-       condition_list_element = (lttng_condition_list_element *) zmalloc(sizeof(*condition_list_element));
+       condition_list_element = zmalloc<lttng_condition_list_element>();
        if (!condition_list_element) {
                ret = -1;
                goto error;
        }
-       client_list_element = (notification_client_list_element *) zmalloc(sizeof(*client_list_element));
+       client_list_element = zmalloc<notification_client_list_element>();
        if (!client_list_element) {
                ret = -1;
                goto error;
@@ -1279,8 +1286,7 @@ void free_notification_client_rcu(struct rcu_head *node)
 }
 
 static
-void notification_client_destroy(struct notification_client *client,
-               struct notification_thread_state *state)
+void notification_client_destroy(struct notification_client *client)
 {
        if (!client) {
                return;
@@ -1500,7 +1506,7 @@ struct lttng_session_trigger_list *lttng_session_trigger_list_create(
 {
        struct lttng_session_trigger_list *list;
 
-       list = (lttng_session_trigger_list *) zmalloc(sizeof(*list));
+       list = zmalloc<lttng_session_trigger_list>();
        if (!list) {
                goto end;
        }
@@ -1551,7 +1557,7 @@ int lttng_session_trigger_list_add(struct lttng_session_trigger_list *list,
 {
        int ret = 0;
        struct lttng_trigger_list_element *new_element =
-                       (lttng_trigger_list_element *) zmalloc(sizeof(*new_element));
+                       zmalloc<lttng_trigger_list_element>();
 
        if (!new_element) {
                ret = -1;
@@ -1749,7 +1755,7 @@ int handle_notification_thread_command_add_channel(
                        continue;
                }
 
-               new_element = (lttng_trigger_list_element *) zmalloc(sizeof(*new_element));
+               new_element = zmalloc<lttng_trigger_list_element>();
                if (!new_element) {
                        rcu_read_unlock();
                        goto error;
@@ -1763,7 +1769,7 @@ int handle_notification_thread_command_add_channel(
 
        DBG("Found %i triggers that apply to newly added channel",
                        trigger_count);
-       channel_trigger_list = (lttng_channel_trigger_list *) zmalloc(sizeof(*channel_trigger_list));
+       channel_trigger_list = zmalloc<lttng_channel_trigger_list>();
        if (!channel_trigger_list) {
                goto error;
        }
@@ -2026,7 +2032,7 @@ int handle_notification_thread_command_add_tracer_event_source(
        enum lttng_error_code cmd_result = LTTNG_OK;
        struct notification_event_tracer_event_source_element *element = NULL;
 
-       element = (notification_event_tracer_event_source_element *) zmalloc(sizeof(*element));
+       element = zmalloc<notification_event_tracer_event_source_element>();
        if (!element) {
                cmd_result = LTTNG_ERR_NOMEM;
                ret = -1;
@@ -2043,7 +2049,7 @@ int handle_notification_thread_command_add_tracer_event_source(
                        lttng_domain_type_str(domain_type));
 
        /* Adding the read side pipe to the event poll. */
-       ret = lttng_poll_add(&state->events, tracer_event_source_fd, LPOLLIN | LPOLLERR);
+       ret = lttng_poll_add(&state->events, tracer_event_source_fd, LPOLLPRI | LPOLLIN | LPOLLERR);
        if (ret < 0) {
                ERR("Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
                                tracer_event_source_fd,
@@ -2065,7 +2071,7 @@ int drain_event_notifier_notification_pipe(
                struct notification_thread_state *state,
                int pipe, enum lttng_domain_type domain)
 {
-       struct lttng_poll_event events = {0};
+       struct lttng_poll_event events = {};
        int ret;
 
        ret = lttng_poll_create(&events, 1, LTTNG_CLOEXEC);
@@ -2233,7 +2239,7 @@ end:
 }
 
 static int handle_notification_thread_command_list_triggers(
-               struct notification_thread_handle *handle,
+               struct notification_thread_handle *handle __attribute__((unused)),
                struct notification_thread_state *state,
                uid_t client_uid,
                struct lttng_triggers **triggers,
@@ -2495,7 +2501,7 @@ int bind_trigger_to_matching_channels(struct lttng_trigger *trigger,
                                struct lttng_channel_trigger_list,
                                channel_triggers_ht_node);
 
-               trigger_list_element = (lttng_trigger_list_element *) zmalloc(sizeof(*trigger_list_element));
+               trigger_list_element = zmalloc<lttng_trigger_list_element>();
                if (!trigger_list_element) {
                        ret = -1;
                        goto end;
@@ -2616,7 +2622,7 @@ enum lttng_error_code setup_tracer_notifier(
        struct lttng_condition *condition = lttng_trigger_get_condition(trigger);
        struct notification_trigger_tokens_ht_element *trigger_tokens_ht_element = NULL;
 
-       trigger_tokens_ht_element = (notification_trigger_tokens_ht_element *) zmalloc(sizeof(*trigger_tokens_ht_element));
+       trigger_tokens_ht_element = zmalloc<notification_trigger_tokens_ht_element>();
        if (!trigger_tokens_ht_element) {
                ret = LTTNG_ERR_NOMEM;
                goto end;
@@ -2739,7 +2745,7 @@ int handle_notification_thread_command_register_trigger(
                goto error;
        }
 
-       trigger_ht_element = (lttng_trigger_ht_element *) zmalloc(sizeof(*trigger_ht_element));
+       trigger_ht_element = zmalloc<lttng_trigger_ht_element>();
        if (!trigger_ht_element) {
                ret = -1;
                goto error;
@@ -3117,28 +3123,55 @@ end:
        return 0;
 }
 
+static
+int pop_cmd_queue(struct notification_thread_handle *handle,
+               struct notification_thread_command **cmd)
+{
+       int ret;
+       uint64_t counter;
+
+       pthread_mutex_lock(&handle->cmd_queue.lock);
+       ret = lttng_read(handle->cmd_queue.event_fd, &counter, sizeof(counter));
+       if (ret != sizeof(counter)) {
+               ret = -1;
+               goto error_unlock;
+       }
+
+       /* Simulate behaviour of EFD_SEMAPHORE for older kernels. */
+       counter -= 1;
+       if (counter != 0) {
+               ret = lttng_write(handle->cmd_queue.event_fd, &counter,
+                               sizeof(counter));
+               if (ret != sizeof(counter)) {
+                       PERROR("Failed to write back to event_fd for EFD_SEMAPHORE emulation");
+                       ret = -1;
+                       goto error_unlock;
+               }
+       }
+
+       *cmd = cds_list_first_entry(&handle->cmd_queue.list,
+                       struct notification_thread_command, cmd_list_node);
+       cds_list_del(&((*cmd)->cmd_list_node));
+       ret = 0;
+
+error_unlock:
+       pthread_mutex_unlock(&handle->cmd_queue.lock);
+       return ret;
+}
+
 /* Returns 0 on success, 1 on exit requested, negative value on error. */
 int handle_notification_thread_command(
                struct notification_thread_handle *handle,
                struct notification_thread_state *state)
 {
        int ret;
-       uint64_t counter;
        struct notification_thread_command *cmd;
 
-       /* Read the event pipe to put it back into a quiescent state. */
-       ret = lttng_read(lttng_pipe_get_readfd(handle->cmd_queue.event_pipe), &counter,
-                       sizeof(counter));
-       if (ret != sizeof(counter)) {
+       ret = pop_cmd_queue(handle, &cmd);
+       if (ret) {
                goto error;
        }
 
-       pthread_mutex_lock(&handle->cmd_queue.lock);
-       cmd = cds_list_first_entry(&handle->cmd_queue.list,
-                       struct notification_thread_command, cmd_list_node);
-       cds_list_del(&cmd->cmd_list_node);
-       pthread_mutex_unlock(&handle->cmd_queue.lock);
-
        DBG("Received `%s` command",
                        notification_command_type_str(cmd->type));
        switch (cmd->type) {
@@ -3330,7 +3363,7 @@ int handle_notification_thread_client_connect(
 
        DBG("Handling new notification channel client connection");
 
-       client = (notification_client *) zmalloc(sizeof(*client));
+       client = zmalloc<notification_client>();
        if (!client) {
                /* Fatal error. */
                ret = -1;
@@ -3396,7 +3429,7 @@ int handle_notification_thread_client_connect(
        return ret;
 
 error:
-       notification_client_destroy(client, state);
+       notification_client_destroy(client);
        return ret;
 }
 
@@ -3438,7 +3471,7 @@ int notification_thread_client_disconnect(
         * Client no longer accessible to other threads (through the
         * client lists).
         */
-       notification_client_destroy(client, state);
+       notification_client_destroy(client);
        return ret;
 }
 
@@ -3701,6 +3734,7 @@ int client_send_command_reply(struct notification_client *client,
        struct lttng_notification_channel_message msg = {
                .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY,
                .size = sizeof(reply),
+               .fds = 0,
        };
        char buffer[sizeof(msg) + sizeof(reply)];
        enum client_transmission_status transmission_status;
@@ -3746,7 +3780,7 @@ error:
 
 static
 int client_handle_message_unknown(struct notification_client *client,
-               struct notification_thread_state *state)
+               struct notification_thread_state *state __attribute__((unused)))
 {
        int ret;
        /*
@@ -3805,6 +3839,7 @@ int client_handle_message_handshake(struct notification_client *client,
        const struct lttng_notification_channel_message msg_header = {
                        .type = LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE,
                        .size = sizeof(handshake_reply),
+                       .fds = 0,
        };
        enum lttng_notification_channel_status status =
                        LTTNG_NOTIFICATION_CHANNEL_STATUS_OK;
@@ -4186,7 +4221,7 @@ bool evaluate_session_consumed_size_condition(
 static
 int evaluate_buffer_condition(const struct lttng_condition *condition,
                struct lttng_evaluation **evaluation,
-               const struct notification_thread_state *state,
+               const struct notification_thread_state *state __attribute__((unused)),
                const struct channel_state_sample *previous_sample,
                const struct channel_state_sample *latest_sample,
                uint64_t previous_session_consumed_total,
@@ -4275,6 +4310,8 @@ int client_notification_overflow(struct notification_client *client)
        int ret = 0;
        const struct lttng_notification_channel_message msg = {
                .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED,
+               .size = 0,
+               .fds = 0,
        };
 
        ASSERT_LOCKED(client->lock);
@@ -4379,6 +4416,8 @@ int notification_client_list_send_evaluation(
        };
        struct lttng_notification_channel_message msg_header = {
                .type = (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION,
+               .size = 0,
+               .fds = 0,
        };
        const struct lttng_credentials *trigger_creds =
                        lttng_trigger_get_credentials(trigger);
@@ -4574,7 +4613,7 @@ struct lttng_event_notifier_notification *recv_one_event_notifier_notification(
                goto end;
        }
 
-       capture_buffer = (char *) zmalloc(capture_buffer_size);
+       capture_buffer = calloc<char>(capture_buffer_size);
        if (!capture_buffer) {
                ERR("Failed to allocate capture buffer");
                goto end;
@@ -4877,7 +4916,7 @@ int handle_notification_thread_channel_sample(
                 */
                struct channel_state_sample *stored_sample;
 
-               stored_sample = (channel_state_sample *) zmalloc(sizeof(*stored_sample));
+               stored_sample = zmalloc<channel_state_sample>();
                if (!stored_sample) {
                        ret = -1;
                        goto end_unlock;
This page took 0.029929 seconds and 4 git commands to generate.