| 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 <assert.h> |
| 9 | #include <common/error.h> |
| 10 | #include <lttng/action/action-internal.h> |
| 11 | #include <lttng/action/notify-internal.h> |
| 12 | #include <lttng/action/start-session-internal.h> |
| 13 | #include <lttng/action/stop-session-internal.h> |
| 14 | |
| 15 | static const char *lttng_action_type_string(enum lttng_action_type action_type) |
| 16 | { |
| 17 | switch (action_type) { |
| 18 | case LTTNG_ACTION_TYPE_UNKNOWN: |
| 19 | return "UNKNOWN"; |
| 20 | case LTTNG_ACTION_TYPE_NOTIFY: |
| 21 | return "NOTIFY"; |
| 22 | case LTTNG_ACTION_TYPE_START_SESSION: |
| 23 | return "START_SESSION"; |
| 24 | case LTTNG_ACTION_TYPE_STOP_SESSION: |
| 25 | return "STOP_SESSION"; |
| 26 | default: |
| 27 | return "???"; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | enum lttng_action_type lttng_action_get_type(struct lttng_action *action) |
| 32 | { |
| 33 | return action ? action->type : LTTNG_ACTION_TYPE_UNKNOWN; |
| 34 | } |
| 35 | |
| 36 | LTTNG_HIDDEN |
| 37 | enum lttng_action_type lttng_action_get_type_const( |
| 38 | const struct lttng_action *action) |
| 39 | { |
| 40 | return action->type; |
| 41 | } |
| 42 | |
| 43 | LTTNG_HIDDEN |
| 44 | void lttng_action_init( |
| 45 | struct lttng_action *action, |
| 46 | enum lttng_action_type type, |
| 47 | action_validate_cb validate, |
| 48 | action_serialize_cb serialize, |
| 49 | action_equal_cb equal, |
| 50 | action_destroy_cb destroy) |
| 51 | { |
| 52 | action->type = type; |
| 53 | action->validate = validate; |
| 54 | action->serialize = serialize; |
| 55 | action->equal = equal; |
| 56 | action->destroy = destroy; |
| 57 | } |
| 58 | |
| 59 | void lttng_action_destroy(struct lttng_action *action) |
| 60 | { |
| 61 | if (!action) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | assert(action->destroy); |
| 66 | action->destroy(action); |
| 67 | } |
| 68 | |
| 69 | LTTNG_HIDDEN |
| 70 | bool lttng_action_validate(struct lttng_action *action) |
| 71 | { |
| 72 | bool valid; |
| 73 | |
| 74 | if (!action) { |
| 75 | valid = false; |
| 76 | goto end; |
| 77 | } |
| 78 | |
| 79 | if (!action->validate) { |
| 80 | /* Sub-class guarantees that it can never be invalid. */ |
| 81 | valid = true; |
| 82 | goto end; |
| 83 | } |
| 84 | |
| 85 | valid = action->validate(action); |
| 86 | end: |
| 87 | return valid; |
| 88 | } |
| 89 | |
| 90 | LTTNG_HIDDEN |
| 91 | int lttng_action_serialize(struct lttng_action *action, |
| 92 | struct lttng_dynamic_buffer *buf) |
| 93 | { |
| 94 | int ret; |
| 95 | struct lttng_action_comm action_comm = { |
| 96 | .action_type = (int8_t) action->type, |
| 97 | }; |
| 98 | |
| 99 | ret = lttng_dynamic_buffer_append(buf, &action_comm, |
| 100 | sizeof(action_comm)); |
| 101 | if (ret) { |
| 102 | goto end; |
| 103 | } |
| 104 | |
| 105 | ret = action->serialize(action, buf); |
| 106 | if (ret) { |
| 107 | goto end; |
| 108 | } |
| 109 | end: |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | LTTNG_HIDDEN |
| 114 | ssize_t lttng_action_create_from_buffer(const struct lttng_buffer_view *view, |
| 115 | struct lttng_action **action) |
| 116 | { |
| 117 | ssize_t consumed_len, specific_action_consumed_len; |
| 118 | const struct lttng_action_comm *action_comm; |
| 119 | action_create_from_buffer_cb create_from_buffer_cb; |
| 120 | struct lttng_buffer_view specific_action_view; |
| 121 | |
| 122 | if (!view || !action) { |
| 123 | consumed_len = -1; |
| 124 | goto end; |
| 125 | } |
| 126 | |
| 127 | action_comm = (const struct lttng_action_comm *) view->data; |
| 128 | |
| 129 | DBG("Create action from buffer: action-type=%s", |
| 130 | lttng_action_type_string(action_comm->action_type)); |
| 131 | |
| 132 | switch (action_comm->action_type) { |
| 133 | case LTTNG_ACTION_TYPE_NOTIFY: |
| 134 | create_from_buffer_cb = lttng_action_notify_create_from_buffer; |
| 135 | break; |
| 136 | case LTTNG_ACTION_TYPE_START_SESSION: |
| 137 | create_from_buffer_cb = |
| 138 | lttng_action_start_session_create_from_buffer; |
| 139 | break; |
| 140 | case LTTNG_ACTION_TYPE_STOP_SESSION: |
| 141 | create_from_buffer_cb = |
| 142 | lttng_action_stop_session_create_from_buffer; |
| 143 | break; |
| 144 | default: |
| 145 | ERR("Failed to create action from buffer, unhandled action type: action-type=%u (%s)", |
| 146 | action_comm->action_type, |
| 147 | lttng_action_type_string( |
| 148 | action_comm->action_type)); |
| 149 | consumed_len = -1; |
| 150 | goto end; |
| 151 | } |
| 152 | |
| 153 | /* Create buffer view for the action-type-specific data. */ |
| 154 | specific_action_view = lttng_buffer_view_from_view(view, |
| 155 | sizeof(struct lttng_action_comm), |
| 156 | view->size - sizeof(struct lttng_action_comm)); |
| 157 | |
| 158 | specific_action_consumed_len = |
| 159 | create_from_buffer_cb(&specific_action_view, action); |
| 160 | if (specific_action_consumed_len < 0) { |
| 161 | ERR("Failed to create specific action from buffer."); |
| 162 | consumed_len = -1; |
| 163 | goto end; |
| 164 | } |
| 165 | |
| 166 | assert(*action); |
| 167 | |
| 168 | consumed_len = sizeof(struct lttng_action_comm) + |
| 169 | specific_action_consumed_len; |
| 170 | |
| 171 | end: |
| 172 | return consumed_len; |
| 173 | } |
| 174 | |
| 175 | LTTNG_HIDDEN |
| 176 | bool lttng_action_is_equal(const struct lttng_action *a, |
| 177 | const struct lttng_action *b) |
| 178 | { |
| 179 | bool is_equal = false; |
| 180 | |
| 181 | if (!a || !b) { |
| 182 | goto end; |
| 183 | } |
| 184 | |
| 185 | if (a->type != b->type) { |
| 186 | goto end; |
| 187 | } |
| 188 | |
| 189 | if (a == b) { |
| 190 | is_equal = true; |
| 191 | goto end; |
| 192 | } |
| 193 | |
| 194 | assert(a->equal); |
| 195 | is_equal = a->equal(a, b); |
| 196 | end: |
| 197 | return is_equal; |
| 198 | } |