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