Commit | Line | Data |
---|---|---|
f2b3ef9f JG |
1 | /* |
2 | * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0-only | |
5 | * | |
6 | */ | |
7 | ||
8 | #include "action-executor.h" | |
9 | #include "cmd.h" | |
10 | #include "health-sessiond.h" | |
11 | #include "lttng-sessiond.h" | |
12 | #include "notification-thread-internal.h" | |
13 | #include "session.h" | |
14 | #include "thread.h" | |
15 | #include <common/macros.h> | |
16 | #include <common/optional.h> | |
17 | #include <lttng/action/action-internal.h> | |
18 | #include <lttng/action/group.h> | |
2af3b9c9 | 19 | #include <lttng/action/notify-internal.h> |
f2b3ef9f JG |
20 | #include <lttng/action/notify.h> |
21 | #include <lttng/action/rotate-session.h> | |
22 | #include <lttng/action/snapshot-session.h> | |
23 | #include <lttng/action/start-session.h> | |
24 | #include <lttng/action/stop-session.h> | |
25 | #include <lttng/condition/evaluation.h> | |
429d9187 | 26 | #include <lttng/condition/event-rule-internal.h> |
f2b3ef9f JG |
27 | #include <lttng/lttng-error.h> |
28 | #include <lttng/trigger/trigger-internal.h> | |
29 | #include <pthread.h> | |
30 | #include <stdbool.h> | |
31 | #include <stddef.h> | |
32 | #include <urcu/list.h> | |
33 | ||
34 | #define THREAD_NAME "Action Executor" | |
35 | #define MAX_QUEUED_WORK_COUNT 8192 | |
36 | ||
37 | struct action_work_item { | |
38 | uint64_t id; | |
39 | struct lttng_trigger *trigger; | |
40 | struct lttng_evaluation *evaluation; | |
41 | struct notification_client_list *client_list; | |
42 | LTTNG_OPTIONAL(struct lttng_credentials) object_creds; | |
43 | struct cds_list_head list_node; | |
44 | }; | |
45 | ||
46 | struct action_executor { | |
47 | struct lttng_thread *thread; | |
48 | struct notification_thread_handle *notification_thread_handle; | |
49 | struct { | |
50 | uint64_t pending_count; | |
51 | struct cds_list_head list; | |
52 | pthread_cond_t cond; | |
53 | pthread_mutex_t lock; | |
54 | } work; | |
55 | bool should_quit; | |
56 | uint64_t next_work_item_id; | |
57 | }; | |
58 | ||
59 | /* | |
60 | * Only return non-zero on a fatal error that should shut down the action | |
61 | * executor. | |
62 | */ | |
63 | typedef int (*action_executor_handler)(struct action_executor *executor, | |
64 | const struct action_work_item *, | |
65 | const struct lttng_action *action); | |
66 | ||
67 | static int action_executor_notify_handler(struct action_executor *executor, | |
68 | const struct action_work_item *, | |
69 | const struct lttng_action *); | |
70 | static int action_executor_start_session_handler(struct action_executor *executor, | |
71 | const struct action_work_item *, | |
72 | const struct lttng_action *); | |
73 | static int action_executor_stop_session_handler(struct action_executor *executor, | |
74 | const struct action_work_item *, | |
75 | const struct lttng_action *); | |
76 | static int action_executor_rotate_session_handler(struct action_executor *executor, | |
77 | const struct action_work_item *, | |
78 | const struct lttng_action *); | |
79 | static int action_executor_snapshot_session_handler(struct action_executor *executor, | |
80 | const struct action_work_item *, | |
81 | const struct lttng_action *); | |
82 | static int action_executor_group_handler(struct action_executor *executor, | |
83 | const struct action_work_item *, | |
84 | const struct lttng_action *); | |
85 | static int action_executor_generic_handler(struct action_executor *executor, | |
86 | const struct action_work_item *, | |
87 | const struct lttng_action *); | |
88 | ||
89 | static const action_executor_handler action_executors[] = { | |
90 | [LTTNG_ACTION_TYPE_NOTIFY] = action_executor_notify_handler, | |
91 | [LTTNG_ACTION_TYPE_START_SESSION] = action_executor_start_session_handler, | |
92 | [LTTNG_ACTION_TYPE_STOP_SESSION] = action_executor_stop_session_handler, | |
93 | [LTTNG_ACTION_TYPE_ROTATE_SESSION] = action_executor_rotate_session_handler, | |
94 | [LTTNG_ACTION_TYPE_SNAPSHOT_SESSION] = action_executor_snapshot_session_handler, | |
95 | [LTTNG_ACTION_TYPE_GROUP] = action_executor_group_handler, | |
96 | }; | |
97 | ||
98 | static const char *action_type_names[] = { | |
99 | [LTTNG_ACTION_TYPE_NOTIFY] = "Notify", | |
100 | [LTTNG_ACTION_TYPE_START_SESSION] = "Start session", | |
101 | [LTTNG_ACTION_TYPE_STOP_SESSION] = "Stop session", | |
102 | [LTTNG_ACTION_TYPE_ROTATE_SESSION] = "Rotate session", | |
103 | [LTTNG_ACTION_TYPE_SNAPSHOT_SESSION] = "Snapshot session", | |
104 | [LTTNG_ACTION_TYPE_GROUP] = "Group", | |
105 | }; | |
106 | ||
107 | static const char *get_action_name(const struct lttng_action *action) | |
108 | { | |
0e43bcbf JG |
109 | const enum lttng_action_type action_type = lttng_action_get_type(action); |
110 | ||
111 | assert(action_type != LTTNG_ACTION_TYPE_UNKNOWN); | |
112 | ||
113 | return action_type_names[action_type]; | |
f2b3ef9f JG |
114 | } |
115 | ||
116 | /* Check if this trigger allowed to interect with a given session. */ | |
117 | static bool is_trigger_allowed_for_session(const struct lttng_trigger *trigger, | |
118 | struct ltt_session *session) | |
119 | { | |
120 | bool is_allowed = false; | |
121 | const struct lttng_credentials session_creds = { | |
ff588497 JR |
122 | .uid = LTTNG_OPTIONAL_INIT_VALUE(session->uid), |
123 | .gid = LTTNG_OPTIONAL_INIT_VALUE(session->gid), | |
f2b3ef9f JG |
124 | }; |
125 | /* Can never be NULL. */ | |
126 | const struct lttng_credentials *trigger_creds = | |
127 | lttng_trigger_get_credentials(trigger); | |
128 | ||
ff588497 JR |
129 | is_allowed = (lttng_credentials_is_equal_uid(trigger_creds, &session_creds)) || |
130 | (lttng_credentials_get_uid(trigger_creds) == 0); | |
f2b3ef9f | 131 | if (!is_allowed) { |
ff588497 | 132 | WARN("Trigger is not allowed to interact with session `%s`: session uid = %ld, session gid = %ld, trigger uid = %ld", |
f2b3ef9f JG |
133 | session->name, |
134 | (long int) session->uid, | |
135 | (long int) session->gid, | |
ff588497 | 136 | (long int) lttng_credentials_get_uid(trigger_creds)); |
f2b3ef9f JG |
137 | } |
138 | ||
139 | return is_allowed; | |
140 | } | |
141 | ||
34f87583 JR |
142 | static const char *get_trigger_name(const struct lttng_trigger *trigger) |
143 | { | |
144 | const char *trigger_name; | |
145 | enum lttng_trigger_status trigger_status; | |
146 | ||
147 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); | |
148 | assert(trigger_status == LTTNG_TRIGGER_STATUS_OK); | |
149 | ||
150 | return trigger_name; | |
151 | } | |
152 | ||
f2b3ef9f JG |
153 | static int client_handle_transmission_status( |
154 | struct notification_client *client, | |
155 | enum client_transmission_status status, | |
156 | void *user_data) | |
157 | { | |
158 | int ret = 0; | |
159 | struct action_executor *executor = user_data; | |
160 | bool update_communication = true; | |
161 | ||
f2b3ef9f JG |
162 | switch (status) { |
163 | case CLIENT_TRANSMISSION_STATUS_COMPLETE: | |
164 | DBG("Successfully sent full notification to client, client_id = %" PRIu64, | |
165 | client->id); | |
166 | update_communication = false; | |
167 | break; | |
168 | case CLIENT_TRANSMISSION_STATUS_QUEUED: | |
169 | DBG("Queued notification in client outgoing buffer, client_id = %" PRIu64, | |
170 | client->id); | |
171 | break; | |
172 | case CLIENT_TRANSMISSION_STATUS_FAIL: | |
173 | DBG("Communication error occurred while sending notification to client, client_id = %" PRIu64, | |
174 | client->id); | |
f2b3ef9f JG |
175 | break; |
176 | default: | |
177 | ERR("Fatal error encoutered while sending notification to client, client_id = %" PRIu64, | |
178 | client->id); | |
f2b3ef9f JG |
179 | ret = -1; |
180 | goto end; | |
181 | } | |
182 | ||
183 | if (!update_communication) { | |
184 | goto end; | |
185 | } | |
186 | ||
6c24d3fd | 187 | /* Safe to read client's id without locking as it is immutable. */ |
f2b3ef9f JG |
188 | ret = notification_thread_client_communication_update( |
189 | executor->notification_thread_handle, client->id, | |
190 | status); | |
191 | end: | |
192 | return ret; | |
193 | } | |
194 | ||
195 | static int action_executor_notify_handler(struct action_executor *executor, | |
196 | const struct action_work_item *work_item, | |
197 | const struct lttng_action *action) | |
198 | { | |
199 | return notification_client_list_send_evaluation(work_item->client_list, | |
200 | lttng_trigger_get_const_condition(work_item->trigger), | |
201 | work_item->evaluation, | |
202 | lttng_trigger_get_credentials(work_item->trigger), | |
c203f058 JR |
203 | work_item->object_creds.is_set ? |
204 | &(work_item->object_creds.value) : | |
205 | NULL, | |
64eafdf6 | 206 | client_handle_transmission_status, executor); |
f2b3ef9f JG |
207 | } |
208 | ||
209 | static int action_executor_start_session_handler(struct action_executor *executor, | |
210 | const struct action_work_item *work_item, | |
211 | const struct lttng_action *action) | |
212 | { | |
213 | int ret = 0; | |
214 | const char *session_name; | |
215 | enum lttng_action_status action_status; | |
216 | struct ltt_session *session; | |
217 | enum lttng_error_code cmd_ret; | |
218 | ||
219 | action_status = lttng_action_start_session_get_session_name( | |
220 | action, &session_name); | |
221 | if (action_status != LTTNG_ACTION_STATUS_OK) { | |
222 | ERR("Failed to get session name from `%s` action", | |
223 | get_action_name(action)); | |
224 | ret = -1; | |
225 | goto end; | |
226 | } | |
227 | ||
228 | session_lock_list(); | |
229 | session = session_find_by_name(session_name); | |
230 | if (!session) { | |
34f87583 | 231 | DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%s`", |
f2b3ef9f | 232 | session_name, get_action_name(action), |
34f87583 | 233 | get_trigger_name(work_item->trigger)); |
f2b3ef9f JG |
234 | goto error_unlock_list; |
235 | } | |
236 | ||
237 | session_lock(session); | |
238 | if (!is_trigger_allowed_for_session(work_item->trigger, session)) { | |
239 | goto error_dispose_session; | |
240 | } | |
241 | ||
242 | cmd_ret = cmd_start_trace(session); | |
243 | switch (cmd_ret) { | |
244 | case LTTNG_OK: | |
34f87583 JR |
245 | DBG("Successfully started session `%s` on behalf of trigger `%s`", |
246 | session_name, get_trigger_name(work_item->trigger)); | |
f2b3ef9f JG |
247 | break; |
248 | case LTTNG_ERR_TRACE_ALREADY_STARTED: | |
34f87583 JR |
249 | DBG("Attempted to start session `%s` on behalf of trigger `%s` but it was already started", |
250 | session_name, get_trigger_name(work_item->trigger)); | |
f2b3ef9f JG |
251 | break; |
252 | default: | |
34f87583 JR |
253 | WARN("Failed to start session `%s` on behalf of trigger `%s`: %s", |
254 | session_name, get_trigger_name(work_item->trigger), | |
f2b3ef9f JG |
255 | lttng_strerror(-cmd_ret)); |
256 | break; | |
257 | } | |
258 | ||
259 | error_dispose_session: | |
260 | session_unlock(session); | |
261 | session_put(session); | |
262 | error_unlock_list: | |
263 | session_unlock_list(); | |
264 | end: | |
265 | return ret; | |
266 | } | |
267 | ||
268 | static int action_executor_stop_session_handler(struct action_executor *executor, | |
269 | const struct action_work_item *work_item, | |
270 | const struct lttng_action *action) | |
271 | { | |
272 | int ret = 0; | |
273 | const char *session_name; | |
274 | enum lttng_action_status action_status; | |
275 | struct ltt_session *session; | |
276 | enum lttng_error_code cmd_ret; | |
277 | ||
278 | action_status = lttng_action_stop_session_get_session_name( | |
279 | action, &session_name); | |
280 | if (action_status != LTTNG_ACTION_STATUS_OK) { | |
281 | ERR("Failed to get session name from `%s` action", | |
282 | get_action_name(action)); | |
283 | ret = -1; | |
284 | goto end; | |
285 | } | |
286 | ||
287 | session_lock_list(); | |
288 | session = session_find_by_name(session_name); | |
289 | if (!session) { | |
34f87583 | 290 | DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%s`", |
f2b3ef9f | 291 | session_name, get_action_name(action), |
34f87583 | 292 | get_trigger_name(work_item->trigger)); |
f2b3ef9f JG |
293 | goto error_unlock_list; |
294 | } | |
295 | ||
296 | session_lock(session); | |
297 | if (!is_trigger_allowed_for_session(work_item->trigger, session)) { | |
298 | goto error_dispose_session; | |
299 | } | |
300 | ||
301 | cmd_ret = cmd_stop_trace(session); | |
302 | switch (cmd_ret) { | |
303 | case LTTNG_OK: | |
34f87583 JR |
304 | DBG("Successfully stopped session `%s` on behalf of trigger `%s`", |
305 | session_name, get_trigger_name(work_item->trigger)); | |
f2b3ef9f JG |
306 | break; |
307 | case LTTNG_ERR_TRACE_ALREADY_STOPPED: | |
34f87583 JR |
308 | DBG("Attempted to stop session `%s` on behalf of trigger `%s` but it was already stopped", |
309 | session_name, get_trigger_name(work_item->trigger)); | |
f2b3ef9f JG |
310 | break; |
311 | default: | |
34f87583 JR |
312 | WARN("Failed to stop session `%s` on behalf of trigger `%s`: %s", |
313 | session_name, get_trigger_name(work_item->trigger), | |
f2b3ef9f JG |
314 | lttng_strerror(-cmd_ret)); |
315 | break; | |
316 | } | |
317 | ||
318 | error_dispose_session: | |
319 | session_unlock(session); | |
320 | session_put(session); | |
321 | error_unlock_list: | |
322 | session_unlock_list(); | |
323 | end: | |
324 | return ret; | |
325 | } | |
326 | ||
327 | static int action_executor_rotate_session_handler(struct action_executor *executor, | |
328 | const struct action_work_item *work_item, | |
329 | const struct lttng_action *action) | |
330 | { | |
331 | int ret = 0; | |
332 | const char *session_name; | |
333 | enum lttng_action_status action_status; | |
334 | struct ltt_session *session; | |
335 | enum lttng_error_code cmd_ret; | |
336 | ||
337 | action_status = lttng_action_rotate_session_get_session_name( | |
338 | action, &session_name); | |
339 | if (action_status != LTTNG_ACTION_STATUS_OK) { | |
340 | ERR("Failed to get session name from `%s` action", | |
341 | get_action_name(action)); | |
342 | ret = -1; | |
343 | goto end; | |
344 | } | |
345 | ||
346 | session_lock_list(); | |
347 | session = session_find_by_name(session_name); | |
348 | if (!session) { | |
34f87583 | 349 | DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%s`", |
f2b3ef9f | 350 | session_name, get_action_name(action), |
34f87583 | 351 | get_trigger_name(work_item->trigger)); |
f2b3ef9f JG |
352 | goto error_unlock_list; |
353 | } | |
354 | ||
355 | session_lock(session); | |
356 | if (!is_trigger_allowed_for_session(work_item->trigger, session)) { | |
357 | goto error_dispose_session; | |
358 | } | |
359 | ||
360 | cmd_ret = cmd_rotate_session(session, NULL, false, | |
361 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED); | |
362 | switch (cmd_ret) { | |
363 | case LTTNG_OK: | |
34f87583 JR |
364 | DBG("Successfully started rotation of session `%s` on behalf of trigger `%s`", |
365 | session_name, get_trigger_name(work_item->trigger)); | |
f2b3ef9f JG |
366 | break; |
367 | case LTTNG_ERR_ROTATION_PENDING: | |
34f87583 JR |
368 | DBG("Attempted to start a rotation of session `%s` on behalf of trigger `%s` but a rotation is already ongoing", |
369 | session_name, get_trigger_name(work_item->trigger)); | |
f2b3ef9f JG |
370 | break; |
371 | case LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP: | |
372 | case LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR: | |
34f87583 JR |
373 | DBG("Attempted to start a rotation of session `%s` on behalf of trigger `%s` but a rotation has already been completed since the last stop or clear", |
374 | session_name, get_trigger_name(work_item->trigger)); | |
f2b3ef9f JG |
375 | break; |
376 | default: | |
34f87583 JR |
377 | WARN("Failed to start a rotation of session `%s` on behalf of trigger `%s`: %s", |
378 | session_name, get_trigger_name(work_item->trigger), | |
f2b3ef9f JG |
379 | lttng_strerror(-cmd_ret)); |
380 | break; | |
381 | } | |
382 | ||
383 | error_dispose_session: | |
384 | session_unlock(session); | |
385 | session_put(session); | |
386 | error_unlock_list: | |
387 | session_unlock_list(); | |
388 | end: | |
389 | return ret; | |
390 | } | |
391 | ||
392 | static int action_executor_snapshot_session_handler(struct action_executor *executor, | |
393 | const struct action_work_item *work_item, | |
394 | const struct lttng_action *action) | |
395 | { | |
396 | int ret = 0; | |
397 | const char *session_name; | |
398 | enum lttng_action_status action_status; | |
399 | struct ltt_session *session; | |
400 | const struct lttng_snapshot_output default_snapshot_output = { | |
401 | .max_size = UINT64_MAX, | |
402 | }; | |
403 | const struct lttng_snapshot_output *snapshot_output = | |
404 | &default_snapshot_output; | |
405 | enum lttng_error_code cmd_ret; | |
406 | ||
407 | action_status = lttng_action_snapshot_session_get_session_name( | |
408 | action, &session_name); | |
409 | if (action_status != LTTNG_ACTION_STATUS_OK) { | |
410 | ERR("Failed to get session name from `%s` action", | |
411 | get_action_name(action)); | |
412 | ret = -1; | |
413 | goto end; | |
414 | } | |
415 | ||
416 | action_status = lttng_action_snapshot_session_get_output( | |
417 | action, &snapshot_output); | |
418 | if (action_status != LTTNG_ACTION_STATUS_OK && | |
419 | action_status != LTTNG_ACTION_STATUS_UNSET) { | |
420 | ERR("Failed to get output from `%s` action", | |
421 | get_action_name(action)); | |
422 | ret = -1; | |
423 | goto end; | |
424 | } | |
425 | ||
426 | session_lock_list(); | |
427 | session = session_find_by_name(session_name); | |
428 | if (!session) { | |
ca46af4e | 429 | DBG("Failed to find session `%s` by name while executing `%s` action of trigger `%s`", |
f2b3ef9f | 430 | session_name, get_action_name(action), |
ca46af4e | 431 | get_trigger_name(work_item->trigger)); |
f2b3ef9f JG |
432 | goto error_unlock_list; |
433 | } | |
434 | ||
435 | ||
436 | session_lock(session); | |
437 | if (!is_trigger_allowed_for_session(work_item->trigger, session)) { | |
438 | goto error_dispose_session; | |
439 | } | |
440 | ||
441 | cmd_ret = cmd_snapshot_record(session, snapshot_output, 0); | |
442 | switch (cmd_ret) { | |
443 | case LTTNG_OK: | |
34f87583 JR |
444 | DBG("Successfully recorded snapshot of session `%s` on behalf of trigger `%s`", |
445 | session_name, get_trigger_name(work_item->trigger)); | |
f2b3ef9f JG |
446 | break; |
447 | default: | |
34f87583 JR |
448 | WARN("Failed to record snapshot of session `%s` on behalf of trigger `%s`: %s", |
449 | session_name, get_trigger_name(work_item->trigger), | |
f2b3ef9f JG |
450 | lttng_strerror(-cmd_ret)); |
451 | break; | |
452 | } | |
453 | ||
454 | error_dispose_session: | |
455 | session_unlock(session); | |
456 | session_put(session); | |
457 | error_unlock_list: | |
458 | session_unlock_list(); | |
459 | end: | |
460 | return ret; | |
461 | } | |
462 | ||
463 | static int action_executor_group_handler(struct action_executor *executor, | |
464 | const struct action_work_item *work_item, | |
465 | const struct lttng_action *action_group) | |
466 | { | |
467 | int ret = 0; | |
468 | unsigned int i, count; | |
469 | enum lttng_action_status action_status; | |
470 | ||
471 | action_status = lttng_action_group_get_count(action_group, &count); | |
472 | if (action_status != LTTNG_ACTION_STATUS_OK) { | |
473 | /* Fatal error. */ | |
474 | ERR("Failed to get count of action in action group"); | |
475 | ret = -1; | |
476 | goto end; | |
477 | } | |
478 | ||
479 | DBG("Action group has %u action%s", count, count != 1 ? "s" : ""); | |
480 | for (i = 0; i < count; i++) { | |
481 | const struct lttng_action *action = | |
482 | lttng_action_group_get_at_index( | |
483 | action_group, i); | |
484 | ||
485 | ret = action_executor_generic_handler( | |
486 | executor, work_item, action); | |
487 | if (ret) { | |
34f87583 JR |
488 | ERR("Stopping the execution of the action group of trigger `%s` following a fatal error", |
489 | get_trigger_name(work_item->trigger)); | |
f2b3ef9f JG |
490 | goto end; |
491 | } | |
492 | } | |
493 | end: | |
494 | return ret; | |
495 | } | |
496 | ||
497 | static int action_executor_generic_handler(struct action_executor *executor, | |
498 | const struct action_work_item *work_item, | |
499 | const struct lttng_action *action) | |
500 | { | |
0e43bcbf JG |
501 | const enum lttng_action_type action_type = lttng_action_get_type(action); |
502 | ||
503 | assert(action_type != LTTNG_ACTION_TYPE_UNKNOWN); | |
504 | ||
2516f2d8 | 505 | DBG("Executing action `%s` of trigger `%s` action work item %" PRIu64, |
f2b3ef9f | 506 | get_action_name(action), |
34f87583 | 507 | get_trigger_name(work_item->trigger), |
f2b3ef9f JG |
508 | work_item->id); |
509 | ||
0e43bcbf | 510 | return action_executors[action_type]( |
f2b3ef9f JG |
511 | executor, work_item, action); |
512 | } | |
513 | ||
514 | static int action_work_item_execute(struct action_executor *executor, | |
515 | struct action_work_item *work_item) | |
516 | { | |
517 | int ret; | |
518 | const struct lttng_action *action = | |
519 | lttng_trigger_get_const_action(work_item->trigger); | |
520 | ||
34f87583 JR |
521 | DBG("Starting execution of action work item %" PRIu64 " of trigger `%s`", |
522 | work_item->id, get_trigger_name(work_item->trigger)); | |
f2b3ef9f | 523 | ret = action_executor_generic_handler(executor, work_item, action); |
34f87583 JR |
524 | DBG("Completed execution of action work item %" PRIu64 " of trigger `%s`", |
525 | work_item->id, get_trigger_name(work_item->trigger)); | |
f2b3ef9f JG |
526 | return ret; |
527 | } | |
528 | ||
529 | static void action_work_item_destroy(struct action_work_item *work_item) | |
530 | { | |
531 | lttng_trigger_put(work_item->trigger); | |
532 | lttng_evaluation_destroy(work_item->evaluation); | |
533 | notification_client_list_put(work_item->client_list); | |
534 | free(work_item); | |
535 | } | |
536 | ||
537 | static void *action_executor_thread(void *_data) | |
538 | { | |
539 | struct action_executor *executor = _data; | |
540 | ||
541 | assert(executor); | |
542 | ||
543 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_ACTION_EXECUTOR); | |
544 | ||
545 | rcu_register_thread(); | |
546 | rcu_thread_online(); | |
547 | ||
548 | DBG("Entering work execution loop"); | |
549 | pthread_mutex_lock(&executor->work.lock); | |
550 | while (!executor->should_quit) { | |
551 | int ret; | |
552 | struct action_work_item *work_item; | |
553 | ||
554 | health_code_update(); | |
555 | if (executor->work.pending_count == 0) { | |
556 | health_poll_entry(); | |
557 | DBG("No work items enqueued, entering wait"); | |
558 | pthread_cond_wait(&executor->work.cond, | |
559 | &executor->work.lock); | |
560 | DBG("Woke-up from wait"); | |
561 | health_poll_exit(); | |
562 | continue; | |
563 | } | |
564 | ||
0db0f8e0 | 565 | /* Pop item from front of the list with work lock held. */ |
f2b3ef9f JG |
566 | work_item = cds_list_first_entry(&executor->work.list, |
567 | struct action_work_item, list_node); | |
568 | cds_list_del(&work_item->list_node); | |
569 | executor->work.pending_count--; | |
570 | ||
571 | /* | |
572 | * Work can be performed without holding the work lock, | |
573 | * allowing new items to be queued. | |
574 | */ | |
575 | pthread_mutex_unlock(&executor->work.lock); | |
576 | ret = action_work_item_execute(executor, work_item); | |
577 | action_work_item_destroy(work_item); | |
578 | if (ret) { | |
579 | /* Fatal error. */ | |
580 | break; | |
581 | } | |
582 | ||
583 | health_code_update(); | |
584 | pthread_mutex_lock(&executor->work.lock); | |
585 | } | |
586 | ||
f5f5c54d JG |
587 | if (executor->should_quit) { |
588 | pthread_mutex_unlock(&executor->work.lock); | |
589 | } | |
f2b3ef9f JG |
590 | DBG("Left work execution loop"); |
591 | ||
592 | health_code_update(); | |
593 | ||
594 | rcu_thread_offline(); | |
595 | rcu_unregister_thread(); | |
596 | health_unregister(health_sessiond); | |
597 | ||
598 | return NULL; | |
599 | } | |
600 | ||
601 | static bool shutdown_action_executor_thread(void *_data) | |
602 | { | |
603 | struct action_executor *executor = _data; | |
604 | ||
8db3acaf | 605 | pthread_mutex_lock(&executor->work.lock); |
f2b3ef9f JG |
606 | executor->should_quit = true; |
607 | pthread_cond_signal(&executor->work.cond); | |
8db3acaf | 608 | pthread_mutex_unlock(&executor->work.lock); |
f2b3ef9f JG |
609 | return true; |
610 | } | |
611 | ||
612 | static void clean_up_action_executor_thread(void *_data) | |
613 | { | |
614 | struct action_executor *executor = _data; | |
615 | ||
616 | assert(cds_list_empty(&executor->work.list)); | |
617 | ||
618 | pthread_mutex_destroy(&executor->work.lock); | |
619 | pthread_cond_destroy(&executor->work.cond); | |
620 | free(executor); | |
621 | } | |
622 | ||
623 | struct action_executor *action_executor_create( | |
624 | struct notification_thread_handle *handle) | |
625 | { | |
626 | struct action_executor *executor = zmalloc(sizeof(*executor)); | |
627 | ||
628 | if (!executor) { | |
629 | goto end; | |
630 | } | |
631 | ||
632 | CDS_INIT_LIST_HEAD(&executor->work.list); | |
633 | pthread_cond_init(&executor->work.cond, NULL); | |
634 | pthread_mutex_init(&executor->work.lock, NULL); | |
635 | executor->notification_thread_handle = handle; | |
636 | ||
637 | executor->thread = lttng_thread_create(THREAD_NAME, | |
638 | action_executor_thread, shutdown_action_executor_thread, | |
639 | clean_up_action_executor_thread, executor); | |
640 | end: | |
641 | return executor; | |
642 | } | |
643 | ||
644 | void action_executor_destroy(struct action_executor *executor) | |
645 | { | |
646 | struct action_work_item *work_item, *tmp; | |
647 | ||
648 | /* TODO Wait for work list to drain? */ | |
649 | lttng_thread_shutdown(executor->thread); | |
650 | pthread_mutex_lock(&executor->work.lock); | |
651 | if (executor->work.pending_count != 0) { | |
652 | WARN("%" PRIu64 | |
653 | " trigger action%s still queued for execution and will be discarded", | |
654 | executor->work.pending_count, | |
655 | executor->work.pending_count == 1 ? " is" : | |
656 | "s are"); | |
657 | } | |
658 | ||
659 | cds_list_for_each_entry_safe ( | |
660 | work_item, tmp, &executor->work.list, list_node) { | |
661 | WARN("Discarding action work item %" PRIu64 | |
34f87583 JR |
662 | " associated to trigger `%s`", |
663 | work_item->id, get_trigger_name(work_item->trigger)); | |
f2b3ef9f JG |
664 | cds_list_del(&work_item->list_node); |
665 | action_work_item_destroy(work_item); | |
666 | } | |
667 | pthread_mutex_unlock(&executor->work.lock); | |
668 | lttng_thread_put(executor->thread); | |
669 | } | |
670 | ||
671 | /* RCU read-lock must be held by the caller. */ | |
672 | enum action_executor_status action_executor_enqueue( | |
673 | struct action_executor *executor, | |
674 | struct lttng_trigger *trigger, | |
675 | struct lttng_evaluation *evaluation, | |
676 | const struct lttng_credentials *object_creds, | |
677 | struct notification_client_list *client_list) | |
678 | { | |
679 | enum action_executor_status executor_status = ACTION_EXECUTOR_STATUS_OK; | |
680 | const uint64_t work_item_id = executor->next_work_item_id++; | |
681 | struct action_work_item *work_item; | |
682 | bool signal = false; | |
683 | ||
684 | pthread_mutex_lock(&executor->work.lock); | |
685 | /* Check for queue overflow. */ | |
686 | if (executor->work.pending_count >= MAX_QUEUED_WORK_COUNT) { | |
687 | /* Most likely spammy, remove if it is the case. */ | |
34f87583 JR |
688 | DBG("Refusing to enqueue action for trigger `%s` as work item %" PRIu64 |
689 | " (overflow)", get_trigger_name(trigger), work_item_id); | |
f2b3ef9f JG |
690 | executor_status = ACTION_EXECUTOR_STATUS_OVERFLOW; |
691 | goto error_unlock; | |
692 | } | |
693 | ||
694 | work_item = zmalloc(sizeof(*work_item)); | |
695 | if (!work_item) { | |
2516f2d8 | 696 | PERROR("Failed to allocate action executor work item on behalf of trigger `%s`", |
34f87583 | 697 | get_trigger_name(trigger)); |
f2b3ef9f JG |
698 | executor_status = ACTION_EXECUTOR_STATUS_ERROR; |
699 | goto error_unlock; | |
700 | } | |
701 | ||
702 | lttng_trigger_get(trigger); | |
703 | if (client_list) { | |
704 | const bool reference_acquired = | |
705 | notification_client_list_get(client_list); | |
706 | ||
707 | assert(reference_acquired); | |
708 | } | |
709 | ||
710 | *work_item = (typeof(*work_item)){ | |
711 | .id = work_item_id, | |
712 | .trigger = trigger, | |
713 | /* Ownership transferred to the work item. */ | |
714 | .evaluation = evaluation, | |
715 | .object_creds = { | |
716 | .is_set = !!object_creds, | |
717 | .value = object_creds ? *object_creds : | |
718 | (typeof(work_item->object_creds.value)) {}, | |
719 | }, | |
720 | .client_list = client_list, | |
721 | .list_node = CDS_LIST_HEAD_INIT(work_item->list_node), | |
722 | }; | |
723 | ||
724 | evaluation = NULL; | |
725 | cds_list_add_tail(&work_item->list_node, &executor->work.list); | |
726 | executor->work.pending_count++; | |
2516f2d8 | 727 | DBG("Enqueued action for trigger `%s` as work item #%" PRIu64, |
34f87583 | 728 | get_trigger_name(trigger), work_item_id); |
f2b3ef9f JG |
729 | signal = true; |
730 | ||
731 | error_unlock: | |
f2b3ef9f JG |
732 | if (signal) { |
733 | pthread_cond_signal(&executor->work.cond); | |
734 | } | |
8db3acaf | 735 | pthread_mutex_unlock(&executor->work.lock); |
f2b3ef9f JG |
736 | |
737 | lttng_evaluation_destroy(evaluation); | |
738 | return executor_status; | |
739 | } |