| 1 | /* |
| 2 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <lttng/action/action-internal.h> |
| 9 | #include <lttng/action/notify-internal.h> |
| 10 | #include <common/error.h> |
| 11 | #include <assert.h> |
| 12 | |
| 13 | enum lttng_action_type lttng_action_get_type(struct lttng_action *action) |
| 14 | { |
| 15 | return action ? action->type : LTTNG_ACTION_TYPE_UNKNOWN; |
| 16 | } |
| 17 | |
| 18 | LTTNG_HIDDEN |
| 19 | enum lttng_action_type lttng_action_get_type_const( |
| 20 | const struct lttng_action *action) |
| 21 | { |
| 22 | return action->type; |
| 23 | } |
| 24 | |
| 25 | void lttng_action_destroy(struct lttng_action *action) |
| 26 | { |
| 27 | if (!action) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | assert(action->destroy); |
| 32 | action->destroy(action); |
| 33 | } |
| 34 | |
| 35 | LTTNG_HIDDEN |
| 36 | bool lttng_action_validate(struct lttng_action *action) |
| 37 | { |
| 38 | bool valid; |
| 39 | |
| 40 | if (!action) { |
| 41 | valid = false; |
| 42 | goto end; |
| 43 | } |
| 44 | |
| 45 | if (!action->validate) { |
| 46 | /* Sub-class guarantees that it can never be invalid. */ |
| 47 | valid = true; |
| 48 | goto end; |
| 49 | } |
| 50 | |
| 51 | valid = action->validate(action); |
| 52 | end: |
| 53 | return valid; |
| 54 | } |
| 55 | |
| 56 | LTTNG_HIDDEN |
| 57 | int lttng_action_serialize(struct lttng_action *action, |
| 58 | struct lttng_dynamic_buffer *buf) |
| 59 | { |
| 60 | int ret; |
| 61 | struct lttng_action_comm action_comm = { |
| 62 | .action_type = (int8_t) action->type, |
| 63 | }; |
| 64 | |
| 65 | ret = lttng_dynamic_buffer_append(buf, &action_comm, |
| 66 | sizeof(action_comm)); |
| 67 | if (ret) { |
| 68 | goto end; |
| 69 | } |
| 70 | |
| 71 | ret = action->serialize(action, buf); |
| 72 | if (ret) { |
| 73 | goto end; |
| 74 | } |
| 75 | end: |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | LTTNG_HIDDEN |
| 80 | ssize_t lttng_action_create_from_buffer(const struct lttng_buffer_view *view, |
| 81 | struct lttng_action **_action) |
| 82 | { |
| 83 | ssize_t ret, action_size = sizeof(struct lttng_action_comm); |
| 84 | struct lttng_action *action; |
| 85 | const struct lttng_action_comm *action_comm; |
| 86 | |
| 87 | if (!view || !_action) { |
| 88 | ret = -1; |
| 89 | goto end; |
| 90 | } |
| 91 | |
| 92 | action_comm = (const struct lttng_action_comm *) view->data; |
| 93 | DBG("Deserializing action from buffer"); |
| 94 | switch (action_comm->action_type) { |
| 95 | case LTTNG_ACTION_TYPE_NOTIFY: |
| 96 | action = lttng_action_notify_create(); |
| 97 | break; |
| 98 | default: |
| 99 | ret = -1; |
| 100 | goto end; |
| 101 | } |
| 102 | |
| 103 | if (!action) { |
| 104 | ret = -1; |
| 105 | goto end; |
| 106 | } |
| 107 | ret = action_size; |
| 108 | *_action = action; |
| 109 | end: |
| 110 | return ret; |
| 111 | } |