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