| 1 | /* |
| 2 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <lttng/trigger/trigger.h> |
| 9 | #include <lttng/lttng-error.h> |
| 10 | #include "notification-thread.h" |
| 11 | #include "notification-thread-commands.h" |
| 12 | #include <common/error.h> |
| 13 | #include <unistd.h> |
| 14 | #include <stdint.h> |
| 15 | #include <inttypes.h> |
| 16 | |
| 17 | static |
| 18 | void init_notification_thread_command(struct notification_thread_command *cmd) |
| 19 | { |
| 20 | memset(cmd, 0, sizeof(*cmd)); |
| 21 | CDS_INIT_LIST_HEAD(&cmd->cmd_list_node); |
| 22 | lttng_waiter_init(&cmd->reply_waiter); |
| 23 | } |
| 24 | |
| 25 | static |
| 26 | int run_command_wait(struct notification_thread_handle *handle, |
| 27 | struct notification_thread_command *cmd) |
| 28 | { |
| 29 | int ret; |
| 30 | uint64_t notification_counter = 1; |
| 31 | |
| 32 | pthread_mutex_lock(&handle->cmd_queue.lock); |
| 33 | /* Add to queue. */ |
| 34 | cds_list_add_tail(&cmd->cmd_list_node, |
| 35 | &handle->cmd_queue.list); |
| 36 | /* Wake-up thread. */ |
| 37 | ret = lttng_write(lttng_pipe_get_writefd(handle->cmd_queue.event_pipe), |
| 38 | ¬ification_counter, sizeof(notification_counter)); |
| 39 | if (ret != sizeof(notification_counter)) { |
| 40 | PERROR("write to notification thread's queue event fd"); |
| 41 | /* |
| 42 | * Remove the command from the list so the notification |
| 43 | * thread does not process it. |
| 44 | */ |
| 45 | cds_list_del(&cmd->cmd_list_node); |
| 46 | goto error_unlock_queue; |
| 47 | } |
| 48 | pthread_mutex_unlock(&handle->cmd_queue.lock); |
| 49 | |
| 50 | lttng_waiter_wait(&cmd->reply_waiter); |
| 51 | return 0; |
| 52 | error_unlock_queue: |
| 53 | pthread_mutex_unlock(&handle->cmd_queue.lock); |
| 54 | return -1; |
| 55 | } |
| 56 | |
| 57 | enum lttng_error_code notification_thread_command_register_trigger( |
| 58 | struct notification_thread_handle *handle, |
| 59 | struct lttng_trigger *trigger) |
| 60 | { |
| 61 | int ret; |
| 62 | enum lttng_error_code ret_code; |
| 63 | struct notification_thread_command cmd; |
| 64 | |
| 65 | init_notification_thread_command(&cmd); |
| 66 | |
| 67 | cmd.type = NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER; |
| 68 | cmd.parameters.trigger = trigger; |
| 69 | |
| 70 | ret = run_command_wait(handle, &cmd); |
| 71 | if (ret) { |
| 72 | ret_code = LTTNG_ERR_UNK; |
| 73 | goto end; |
| 74 | } |
| 75 | ret_code = cmd.reply_code; |
| 76 | end: |
| 77 | return ret_code; |
| 78 | } |
| 79 | |
| 80 | enum lttng_error_code notification_thread_command_unregister_trigger( |
| 81 | struct notification_thread_handle *handle, |
| 82 | struct lttng_trigger *trigger) |
| 83 | { |
| 84 | int ret; |
| 85 | enum lttng_error_code ret_code; |
| 86 | struct notification_thread_command cmd; |
| 87 | |
| 88 | init_notification_thread_command(&cmd); |
| 89 | |
| 90 | cmd.type = NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER; |
| 91 | cmd.parameters.trigger = trigger; |
| 92 | |
| 93 | ret = run_command_wait(handle, &cmd); |
| 94 | if (ret) { |
| 95 | ret_code = LTTNG_ERR_UNK; |
| 96 | goto end; |
| 97 | } |
| 98 | ret_code = cmd.reply_code; |
| 99 | end: |
| 100 | return ret_code; |
| 101 | } |
| 102 | |
| 103 | enum lttng_error_code notification_thread_command_add_channel( |
| 104 | struct notification_thread_handle *handle, |
| 105 | char *session_name, uid_t uid, gid_t gid, |
| 106 | char *channel_name, uint64_t key, |
| 107 | enum lttng_domain_type domain, uint64_t capacity) |
| 108 | { |
| 109 | int ret; |
| 110 | enum lttng_error_code ret_code; |
| 111 | struct notification_thread_command cmd; |
| 112 | |
| 113 | init_notification_thread_command(&cmd); |
| 114 | |
| 115 | cmd.type = NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL; |
| 116 | cmd.parameters.add_channel.session.name = session_name; |
| 117 | cmd.parameters.add_channel.session.uid = uid; |
| 118 | cmd.parameters.add_channel.session.gid = gid; |
| 119 | cmd.parameters.add_channel.channel.name = channel_name; |
| 120 | cmd.parameters.add_channel.channel.key = key; |
| 121 | cmd.parameters.add_channel.channel.domain = domain; |
| 122 | cmd.parameters.add_channel.channel.capacity = capacity; |
| 123 | |
| 124 | ret = run_command_wait(handle, &cmd); |
| 125 | if (ret) { |
| 126 | ret_code = LTTNG_ERR_UNK; |
| 127 | goto end; |
| 128 | } |
| 129 | ret_code = cmd.reply_code; |
| 130 | end: |
| 131 | return ret_code; |
| 132 | } |
| 133 | |
| 134 | enum lttng_error_code notification_thread_command_remove_channel( |
| 135 | struct notification_thread_handle *handle, |
| 136 | uint64_t key, enum lttng_domain_type domain) |
| 137 | { |
| 138 | int ret; |
| 139 | enum lttng_error_code ret_code; |
| 140 | struct notification_thread_command cmd; |
| 141 | |
| 142 | init_notification_thread_command(&cmd); |
| 143 | |
| 144 | cmd.type = NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL; |
| 145 | cmd.parameters.remove_channel.key = key; |
| 146 | cmd.parameters.remove_channel.domain = domain; |
| 147 | |
| 148 | ret = run_command_wait(handle, &cmd); |
| 149 | if (ret) { |
| 150 | ret_code = LTTNG_ERR_UNK; |
| 151 | goto end; |
| 152 | } |
| 153 | ret_code = cmd.reply_code; |
| 154 | end: |
| 155 | return ret_code; |
| 156 | } |
| 157 | |
| 158 | enum lttng_error_code notification_thread_command_session_rotation_ongoing( |
| 159 | struct notification_thread_handle *handle, |
| 160 | const char *session_name, uid_t uid, gid_t gid, |
| 161 | uint64_t trace_archive_chunk_id) |
| 162 | { |
| 163 | int ret; |
| 164 | enum lttng_error_code ret_code; |
| 165 | struct notification_thread_command cmd; |
| 166 | |
| 167 | init_notification_thread_command(&cmd); |
| 168 | |
| 169 | cmd.type = NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING; |
| 170 | cmd.parameters.session_rotation.session_name = session_name; |
| 171 | cmd.parameters.session_rotation.uid = uid; |
| 172 | cmd.parameters.session_rotation.gid = gid; |
| 173 | cmd.parameters.session_rotation.trace_archive_chunk_id = |
| 174 | trace_archive_chunk_id; |
| 175 | |
| 176 | ret = run_command_wait(handle, &cmd); |
| 177 | if (ret) { |
| 178 | ret_code = LTTNG_ERR_UNK; |
| 179 | goto end; |
| 180 | } |
| 181 | ret_code = cmd.reply_code; |
| 182 | end: |
| 183 | return ret_code; |
| 184 | } |
| 185 | |
| 186 | enum lttng_error_code notification_thread_command_session_rotation_completed( |
| 187 | struct notification_thread_handle *handle, |
| 188 | const char *session_name, uid_t uid, gid_t gid, |
| 189 | uint64_t trace_archive_chunk_id, |
| 190 | struct lttng_trace_archive_location *location) |
| 191 | { |
| 192 | int ret; |
| 193 | enum lttng_error_code ret_code; |
| 194 | struct notification_thread_command cmd; |
| 195 | |
| 196 | init_notification_thread_command(&cmd); |
| 197 | |
| 198 | cmd.type = NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED; |
| 199 | cmd.parameters.session_rotation.session_name = session_name; |
| 200 | cmd.parameters.session_rotation.uid = uid; |
| 201 | cmd.parameters.session_rotation.gid = gid; |
| 202 | cmd.parameters.session_rotation.trace_archive_chunk_id = |
| 203 | trace_archive_chunk_id; |
| 204 | cmd.parameters.session_rotation.location = location; |
| 205 | |
| 206 | ret = run_command_wait(handle, &cmd); |
| 207 | if (ret) { |
| 208 | ret_code = LTTNG_ERR_UNK; |
| 209 | goto end; |
| 210 | } |
| 211 | ret_code = cmd.reply_code; |
| 212 | end: |
| 213 | return ret_code; |
| 214 | } |
| 215 | |
| 216 | void notification_thread_command_quit( |
| 217 | struct notification_thread_handle *handle) |
| 218 | { |
| 219 | int ret; |
| 220 | struct notification_thread_command cmd; |
| 221 | |
| 222 | init_notification_thread_command(&cmd); |
| 223 | |
| 224 | cmd.type = NOTIFICATION_COMMAND_TYPE_QUIT; |
| 225 | ret = run_command_wait(handle, &cmd); |
| 226 | assert(!ret && cmd.reply_code == LTTNG_OK); |
| 227 | } |