A trigger can now have an optional name on the client side.
If no name is provided the sessiond will generate a name and return a
trigger object to populate the client side object.
For now, the name generation code generate the following pattern: TN
Where `N` is incremented each time a name has to be generated. If a
collision occurs, we increment `N` as needed.
Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: I5f303610713c049177e53937bfc9824cd61501e4
struct lttng_condition *condition;
struct lttng_action *action;
+ char *name;
/* For now only the uid portion of the credentials is used. */
struct lttng_credentials creds;
};
struct lttng_trigger_comm {
- /* length excludes its own length. */
- uint32_t length;
/*
* Credentials, only the uid portion is used for now.
* Used as an override when desired by the root user.
*/
uint64_t uid;
- /* A condition and action object follow. */
+ /*
+ * Length of the variable length payload (name, condition, and
+ * an action).
+ */
+ uint32_t length;
+ /* Includes '\0' terminator. */
+ uint32_t name_length;
+ /* A null-terminated name, a condition, and an action follow. */
char payload[];
} LTTNG_PACKED;
LTTNG_HIDDEN
bool lttng_trigger_validate(struct lttng_trigger *trigger);
+LTTNG_HIDDEN
+int lttng_trigger_assign_name(
+ struct lttng_trigger *dst, const struct lttng_trigger *src);
+
+LTTNG_HIDDEN
+int lttng_trigger_generate_name(struct lttng_trigger *trigger,
+ uint64_t unique_id);
+
LTTNG_HIDDEN
bool lttng_trigger_is_equal(
const struct lttng_trigger *a, const struct lttng_trigger *b);
extern struct lttng_action *lttng_trigger_get_action(
struct lttng_trigger *trigger);
+
+/*
+ * Get the name of a trigger.
+ *
+ * The caller does not assume the ownership of the returned name.
+ * The name shall only only be used for the duration of the trigger's
+ * lifetime, or until a different name is set.
+ *
+ * Returns LTTNG_TRIGGER_STATUS_OK and a pointer to the trigger's name on
+ * success, LTTNG_TRIGGER_STATUS_INVALID if an invalid parameter is passed,
+ * or LTTNG_TRIGGER_STATUS_UNSET if a name was not set prior to this call.
+ */
+extern enum lttng_trigger_status lttng_trigger_get_name(
+ const struct lttng_trigger *trigger, const char **name);
+
+/*
+ * Set the trigger name.
+ *
+ * A name is optional.
+ * A name will be assigned on trigger registration if no name is set.
+ *
+ * The name is copied.
+ *
+ * Return LTTNG_TRIGGER_STATUS_OK on success, LTTNG_TRIGGER_STATUS_INVALID
+ * if invalid parameters are passed.
+ */
+extern enum lttng_trigger_status lttng_trigger_set_name(
+ struct lttng_trigger *trigger, const char *name);
+
/*
* Destroy (frees) a trigger object.
*/
}
case LTTNG_REGISTER_TRIGGER:
{
+ struct lttng_trigger *return_trigger;
+ size_t original_payload_size;
+ size_t payload_size;
+
+ ret = setup_empty_lttng_msg(cmd_ctx);
+ if (ret) {
+ ret = LTTNG_ERR_NOMEM;
+ goto setup_error;
+ }
+
+ original_payload_size = cmd_ctx->reply_payload.buffer.size;
+
ret = cmd_register_trigger(cmd_ctx, *sock,
- notification_thread_handle);
+ notification_thread_handle, &return_trigger);
+ if (ret != LTTNG_OK) {
+ goto error;
+ }
+
+ ret = lttng_trigger_serialize(return_trigger, &cmd_ctx->reply_payload);
+ if (ret) {
+ ERR("Failed to serialize trigger in reply to \"register trigger\" command");
+ ret = LTTNG_ERR_NOMEM;
+ lttng_trigger_destroy(return_trigger);
+ goto error;
+ }
+
+ lttng_trigger_destroy(return_trigger);
+ return_trigger = NULL;
+
+ payload_size = cmd_ctx->reply_payload.buffer.size -
+ original_payload_size;
+
+ update_lttng_msg(cmd_ctx, 0, payload_size);
+
+ ret = LTTNG_OK;
break;
}
case LTTNG_UNREGISTER_TRIGGER:
}
int cmd_register_trigger(struct command_ctx *cmd_ctx, int sock,
- struct notification_thread_handle *notification_thread)
+ struct notification_thread_handle *notification_thread,
+ struct lttng_trigger **return_trigger)
{
int ret;
size_t trigger_len;
}
}
-
/*
* Validate the trigger credentials against the command credentials.
* Only the root user can register a trigger with non-matching
}
}
- /* Inform the notification thread */
+ /*
+ * A reference to the trigger is acquired by the notification thread.
+ * It is safe to return the same trigger to the caller since it the
+ * other user holds a reference.
+ *
+ * The trigger is modified during the execution of the
+ * "register trigger" command. However, by the time the command returns,
+ * it is safe to use without any locking as its properties are
+ * immutable.
+ */
ret = notification_thread_command_register_trigger(notification_thread,
trigger);
+ if (ret != LTTNG_OK) {
+ goto end_notification_thread;
+ }
+
+ /* Return an updated trigger to the client. */
+ *return_trigger = trigger;
+
+end_notification_thread:
/* Ownership of trigger was transferred. */
trigger = NULL;
end:
int cmd_regenerate_statedump(struct ltt_session *session);
int cmd_register_trigger(struct command_ctx *cmd_ctx, int sock,
- struct notification_thread_handle *notification_thread_handle);
+ struct notification_thread_handle *notification_thread_handle,
+ struct lttng_trigger **return_trigger);
int cmd_unregister_trigger(struct command_ctx *cmd_ctx, int sock,
struct notification_thread_handle *notification_thread_handle);
enum lttng_error_code ret_code;
struct notification_thread_command cmd = {};
+ assert(trigger);
init_notification_thread_command(&cmd);
cmd.type = NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER;
+ lttng_trigger_get(trigger);
cmd.parameters.trigger = trigger;
ret = run_command_wait(handle, &cmd);
struct lttng_trigger_ht_element {
struct lttng_trigger *trigger;
struct cds_lfht_node node;
+ struct cds_lfht_node node_by_name_uid;
/* call_rcu delayed reclaim. */
struct rcu_head rcu_node;
};
enum client_transmission_status transmission_status,
struct notification_thread_state *state);
+static
+void free_lttng_trigger_ht_element_rcu(struct rcu_head *node);
+
static
int match_client_socket(struct cds_lfht_node *node, const void *key)
{
}
static
-int match_condition(struct cds_lfht_node *node, const void *key)
+int match_trigger(struct cds_lfht_node *node, const void *key)
{
- struct lttng_condition *condition_key = (struct lttng_condition *) key;
- struct lttng_trigger_ht_element *trigger;
- struct lttng_condition *condition;
+ struct lttng_trigger *trigger_key = (struct lttng_trigger *) key;
+ struct lttng_trigger_ht_element *trigger_ht_element;
- trigger = caa_container_of(node, struct lttng_trigger_ht_element,
+ trigger_ht_element = caa_container_of(node, struct lttng_trigger_ht_element,
node);
- condition = lttng_trigger_get_condition(trigger->trigger);
- assert(condition);
- return !!lttng_condition_is_equal(condition_key, condition);
+ return !!lttng_trigger_is_equal(trigger_key, trigger_ht_element->trigger);
}
static
return !strcmp(session_info->name, name);
}
+/*
+ * Match trigger based on name and credentials only.
+ * Name duplication is NOT allowed for the same uid.
+ */
+static
+int match_trigger_by_name_uid(struct cds_lfht_node *node,
+ const void *key)
+{
+ bool match = false;
+ const char *name;
+ const char *key_name;
+ enum lttng_trigger_status status;
+ const struct lttng_credentials *key_creds;
+ const struct lttng_credentials *node_creds;
+ const struct lttng_trigger *trigger_key =
+ (const struct lttng_trigger *) key;
+ const struct lttng_trigger_ht_element *trigger_ht_element =
+ caa_container_of(node,
+ struct lttng_trigger_ht_element,
+ node_by_name_uid);
+
+ status = lttng_trigger_get_name(trigger_ht_element->trigger, &name);
+ assert(status == LTTNG_TRIGGER_STATUS_OK);
+
+ status = lttng_trigger_get_name(trigger_key, &key_name);
+ assert(status == LTTNG_TRIGGER_STATUS_OK);
+
+ /* Compare the names. */
+ if (strcmp(name, key_name) != 0) {
+ goto end;
+ }
+
+ /* Compare the owners' UIDs. */
+ key_creds = lttng_trigger_get_credentials(trigger_key);
+ node_creds = lttng_trigger_get_credentials(trigger_ht_element->trigger);
+
+ match = lttng_credentials_is_equal_uid(key_creds, node_creds);
+
+end:
+ return match;
+}
+
+/*
+ * Hash trigger based on name and credentials only.
+ */
+static
+unsigned long hash_trigger_by_name_uid(const struct lttng_trigger *trigger)
+{
+ unsigned long hash = 0;
+ const struct lttng_credentials *trigger_creds;
+ const char *trigger_name;
+ enum lttng_trigger_status status;
+
+ status = lttng_trigger_get_name(trigger, &trigger_name);
+ if (status == LTTNG_TRIGGER_STATUS_OK) {
+ hash = hash_key_str(trigger_name, lttng_ht_seed);
+ }
+
+ trigger_creds = lttng_trigger_get_credentials(trigger);
+ hash ^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds->uid),
+ lttng_ht_seed);
+
+ return hash;
+}
+
static
unsigned long lttng_condition_buffer_usage_hash(
const struct lttng_condition *_condition)
return is_notify;
}
+static bool trigger_name_taken(struct notification_thread_state *state,
+ const struct lttng_trigger *trigger)
+{
+ struct cds_lfht_iter iter;
+
+ /*
+ * No duplicata is allowed in the triggers_by_name_uid_ht.
+ * The match is done against the trigger name and uid.
+ */
+ cds_lfht_lookup(state->triggers_by_name_uid_ht,
+ hash_trigger_by_name_uid(trigger),
+ match_trigger_by_name_uid,
+ trigger,
+ &iter);
+ return !!cds_lfht_iter_get_node(&iter);
+}
+
+static
+enum lttng_error_code generate_trigger_name(
+ struct notification_thread_state *state,
+ struct lttng_trigger *trigger, const char **name)
+{
+ enum lttng_error_code ret_code = LTTNG_OK;
+ bool taken = false;
+ enum lttng_trigger_status status;
+
+ do {
+ const int ret = lttng_trigger_generate_name(trigger,
+ state->trigger_id.name_offset++);
+ if (ret) {
+ /* The only reason this can fail right now. */
+ ret_code = LTTNG_ERR_NOMEM;
+ break;
+ }
+
+ status = lttng_trigger_get_name(trigger, name);
+ assert(status == LTTNG_TRIGGER_STATUS_OK);
+
+ taken = trigger_name_taken(state, trigger);
+ } while (taken || state->trigger_id.name_offset == UINT64_MAX);
+
+ return ret_code;
+}
+
/*
* FIXME A client's credentials are not checked when registering a trigger.
*
struct notification_client_list_element *client_list_element;
struct cds_lfht_node *node;
struct cds_lfht_iter iter;
+ const char* trigger_name;
bool free_trigger = true;
struct lttng_evaluation *evaluation = NULL;
struct lttng_credentials object_creds;
rcu_read_lock();
+ if (lttng_trigger_get_name(trigger, &trigger_name) ==
+ LTTNG_TRIGGER_STATUS_UNSET) {
+ const enum lttng_error_code ret_code = generate_trigger_name(
+ state, trigger, &trigger_name);
+
+ if (ret_code != LTTNG_OK) {
+ /* Fatal error. */
+ ret = -1;
+ *cmd_result = ret_code;
+ goto error;
+ }
+ } else if (trigger_name_taken(state, trigger)) {
+ /* Not a fatal error. */
+ *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
+ ret = 0;
+ goto error;
+ }
+
condition = lttng_trigger_get_condition(trigger);
assert(condition);
/* Add trigger to the trigger_ht. */
cds_lfht_node_init(&trigger_ht_element->node);
+ cds_lfht_node_init(&trigger_ht_element->node_by_name_uid);
trigger_ht_element->trigger = trigger;
node = cds_lfht_add_unique(state->triggers_ht,
lttng_condition_hash(condition),
- match_condition,
- condition,
+ match_trigger,
+ trigger,
&trigger_ht_element->node);
if (node != &trigger_ht_element->node) {
/* Not a fatal error, simply report it to the client. */
goto error_free_ht_element;
}
+ node = cds_lfht_add_unique(state->triggers_by_name_uid_ht,
+ hash_trigger_by_name_uid(trigger),
+ match_trigger_by_name_uid,
+ trigger,
+ &trigger_ht_element->node_by_name_uid);
+ if (node != &trigger_ht_element->node_by_name_uid) {
+ /* Not a fatal error, simply report it to the client. */
+ cds_lfht_del(state->triggers_ht, &trigger_ht_element->node);
+ *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
+ goto error_free_ht_element;
+ }
+
/*
* Ownership of the trigger and of its wrapper was transfered to
* the triggers_ht.
break;
case LTTNG_OBJECT_TYPE_NONE:
ret = 0;
- goto error_put_client_list;
+ break;
case LTTNG_OBJECT_TYPE_UNKNOWN:
default:
ret = -1;
- goto error_put_client_list;
+ break;
}
if (ret) {
if (!evaluation) {
/* Evaluation yielded nothing. Normal exit. */
ret = 0;
- goto error_put_client_list;
+ goto end;
}
/*
*/
WARN("No space left when enqueuing action associated to newly registered trigger");
ret = 0;
- goto error_put_client_list;
+ goto end;
default:
abort();
}
+end:
*cmd_result = LTTNG_OK;
error_put_client_list:
notification_client_list_put(client_list);
error_free_ht_element:
- free(trigger_ht_element);
+ if (trigger_ht_element) {
+ /* Delayed removal due to RCU constraint on delete. */
+ call_rcu(&trigger_ht_element->rcu_node,
+ free_lttng_trigger_ht_element_rcu);
+ }
+
error:
if (free_trigger) {
lttng_trigger_destroy(trigger);
cds_lfht_lookup(state->triggers_ht,
lttng_condition_hash(condition),
- match_condition,
- condition,
+ match_trigger,
+ trigger,
&iter);
triggers_ht_node = cds_lfht_iter_get_node(&iter);
if (!triggers_ht_node) {
/* Remove trigger from triggers_ht. */
trigger_ht_element = caa_container_of(triggers_ht_node,
struct lttng_trigger_ht_element, node);
+ cds_lfht_del(state->triggers_by_name_uid_ht, &trigger_ht_element->node_by_name_uid);
cds_lfht_del(state->triggers_ht, triggers_ht_node);
/* Release the ownership of the trigger. */
ret = cds_lfht_destroy(state->sessions_ht, NULL);
assert(!ret);
}
+ if (state->triggers_by_name_uid_ht) {
+ ret = cds_lfht_destroy(state->triggers_by_name_uid_ht, NULL);
+ assert(!ret);
+ }
/*
* Must be destroyed after all channels have been destroyed.
* See comment in struct lttng_session_trigger_list.
if (!state->triggers_ht) {
goto error;
}
+ state->triggers_by_name_uid_ht = cds_lfht_new(DEFAULT_HT_SIZE,
+ 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
+ if (!state->triggers_by_name_uid_ht) {
+ goto error;
+ }
state->executor = action_executor_create(handle);
if (!state->executor) {
* channels through their struct channel_info (ref-counting is used).
*
* - triggers_ht:
- * associates a condition to a struct lttng_trigger_ht_element.
+ * associates a trigger to a struct lttng_trigger_ht_element.
* The hash table holds the ownership of the
* lttng_trigger_ht_elements along with the triggers themselves.
+ * - triggers_by_name_uid_ht:
+ * associates a trigger (name, uid) tuple to
+ * a struct lttng_trigger_ht_element.
+ * The hash table does not hold any ownership and is used strictly
+ * for lookup on registration.
*
* The thread reacts to the following internal events:
* 1) creation of a tracing channel,
* notification_trigger_clients_ht,
* - add trigger to channel_triggers_ht (if applicable),
* - add trigger to session_triggers_ht (if applicable),
+ * - add trigger to triggers_by_name_uid_ht
* - add trigger to triggers_ht
* - evaluate the trigger's condition right away to react if that condition
* is true from the beginning.
* - remove the trigger from the notification_trigger_clients_ht,
* - remove trigger from channel_triggers_ht (if applicable),
* - remove trigger from session_triggers_ht (if applicable),
+ * - remove trigger from triggers_by_name_uid_ht
* - remove trigger from triggers_ht
*
* 5) Reception of a channel monitor sample from the consumer daemon
struct cds_lfht *channels_ht;
struct cds_lfht *sessions_ht;
struct cds_lfht *triggers_ht;
+ struct cds_lfht *triggers_by_name_uid_ht;
+ struct {
+ uint64_t name_offset;
+ } trigger_id;
notification_client_id next_notification_client_id;
struct action_executor *executor;
};
#include <common/error.h>
#include <common/optional.h>
#include <assert.h>
+#include <inttypes.h>
LTTNG_HIDDEN
bool lttng_trigger_validate(struct lttng_trigger *trigger)
lttng_action_put(action);
lttng_condition_put(condition);
+ free(trigger->name);
free(trigger);
}
struct lttng_payload_view *src_view,
struct lttng_trigger **trigger)
{
- ssize_t ret, offset = 0, condition_size, action_size;
+ ssize_t ret, offset = 0, condition_size, action_size, name_size = 0;
struct lttng_condition *condition = NULL;
struct lttng_action *action = NULL;
const struct lttng_trigger_comm *trigger_comm;
+ const char *name = NULL;
struct lttng_credentials creds = {
.uid = LTTNG_OPTIONAL_INIT_UNSET,
.gid = LTTNG_OPTIONAL_INIT_UNSET,
LTTNG_OPTIONAL_SET(&creds.uid, trigger_comm->uid);
offset += sizeof(*trigger_comm);
+
+ if (trigger_comm->name_length != 0) {
+ /* Name. */
+ const struct lttng_payload_view name_view =
+ lttng_payload_view_from_view(
+ src_view, offset, trigger_comm->name_length);
+
+ name = name_view.buffer.data;
+ if (!lttng_buffer_view_contains_string(&name_view.buffer, name,
+ trigger_comm->name_length)) {
+ ret = -1;
+ goto end;
+ }
+
+ offset += trigger_comm->name_length;
+ name_size = trigger_comm->name_length;
+ }
+
{
/* struct lttng_condition */
struct lttng_payload_view condition_view =
offset += action_size;
/* Unexpected size of inner-elements; the buffer is corrupted. */
- if ((ssize_t) trigger_comm->length != condition_size + action_size) {
+ if ((ssize_t) trigger_comm->length != condition_size + action_size + name_size) {
ret = -1;
goto error;
}
lttng_action_put(action);
action = NULL;
+ if (name) {
+ const enum lttng_trigger_status status =
+ lttng_trigger_set_name(*trigger, name);
+
+ if (status != LTTNG_TRIGGER_STATUS_OK) {
+ ret = -1;
+ goto end;
+ }
+ }
+
ret = offset;
error:
struct lttng_payload *payload)
{
int ret;
- size_t header_offset, size_before_payload;
+ size_t header_offset, size_before_payload, size_name;
struct lttng_trigger_comm trigger_comm = {};
struct lttng_trigger_comm *header;
const struct lttng_credentials *creds = NULL;
trigger_comm.uid = LTTNG_OPTIONAL_GET(creds->uid);
+ if (trigger->name != NULL) {
+ size_name = strlen(trigger->name) + 1;
+ } else {
+ size_name = 0;
+ }
+
+ trigger_comm.name_length = size_name;
+
header_offset = payload->buffer.size;
ret = lttng_dynamic_buffer_append(&payload->buffer, &trigger_comm,
sizeof(trigger_comm));
}
size_before_payload = payload->buffer.size;
+
+ /* Trigger name. */
+ ret = lttng_dynamic_buffer_append(
+ &payload->buffer, trigger->name, size_name);
+ if (ret) {
+ goto end;
+ }
+
ret = lttng_condition_serialize(trigger->condition, payload);
if (ret) {
goto end;
return true;
}
+enum lttng_trigger_status lttng_trigger_set_name(struct lttng_trigger *trigger,
+ const char* name)
+{
+ char *name_copy = NULL;
+ enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
+
+ if (!trigger || !name ||
+ strlen(name) == 0) {
+ status = LTTNG_TRIGGER_STATUS_INVALID;
+ goto end;
+ }
+
+ name_copy = strdup(name);
+ if (!name_copy) {
+ status = LTTNG_TRIGGER_STATUS_ERROR;
+ goto end;
+ }
+
+ free(trigger->name);
+
+ trigger->name = name_copy;
+ name_copy = NULL;
+end:
+ return status;
+}
+
+enum lttng_trigger_status lttng_trigger_get_name(
+ const struct lttng_trigger *trigger, const char **name)
+{
+ enum lttng_trigger_status status = LTTNG_TRIGGER_STATUS_OK;
+
+ if (!trigger || !name) {
+ status = LTTNG_TRIGGER_STATUS_INVALID;
+ goto end;
+ }
+
+ if (!trigger->name) {
+ status = LTTNG_TRIGGER_STATUS_UNSET;
+ }
+
+ *name = trigger->name;
+end:
+ return status;
+}
+
+LTTNG_HIDDEN
+int lttng_trigger_assign_name(struct lttng_trigger *dst,
+ const struct lttng_trigger *src)
+{
+ int ret = 0;
+ enum lttng_trigger_status status;
+
+ status = lttng_trigger_set_name(dst, src->name);
+ if (status != LTTNG_TRIGGER_STATUS_OK) {
+ ret = -1;
+ ERR("Failed to set name for trigger");
+ goto end;
+ }
+end:
+ return ret;
+}
+
+LTTNG_HIDDEN
+int lttng_trigger_generate_name(struct lttng_trigger *trigger,
+ uint64_t unique_id)
+{
+ int ret = 0;
+ char *generated_name = NULL;
+
+ ret = asprintf(&generated_name, "T%" PRIu64 "", unique_id);
+ if (ret < 0) {
+ ERR("Failed to generate trigger name");
+ ret = -1;
+ goto end;
+ }
+
+ ret = 0;
+ free(trigger->name);
+ trigger->name = generated_name;
+end:
+ return ret;
+}
+
LTTNG_HIDDEN
void lttng_trigger_get(struct lttng_trigger *trigger)
{
struct lttcomm_session_msg *message_lsm;
struct lttng_payload message;
struct lttng_payload reply;
+ struct lttng_trigger *reply_trigger = NULL;
const struct lttng_credentials user_creds = {
.uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
.gid = LTTNG_OPTIONAL_INIT_UNSET,
};
+
lttng_payload_init(&message);
lttng_payload_init(&reply);
}
}
+ {
+ struct lttng_payload_view reply_view =
+ lttng_payload_view_from_payload(
+ &reply, 0, reply.buffer.size);
+
+ ret = lttng_trigger_create_from_payload(
+ &reply_view, &reply_trigger);
+ if (ret < 0) {
+ ret = -LTTNG_ERR_FATAL;
+ goto end;
+ }
+ }
+
+ ret = lttng_trigger_assign_name(trigger, reply_trigger);
+ if (ret < 0) {
+ ret = -LTTNG_ERR_FATAL;
+ goto end;
+ }
+
ret = 0;
end:
lttng_payload_reset(&message);
lttng_payload_reset(&reply);
+ lttng_trigger_destroy(reply_trigger);
return ret;
}