| 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 <common/error.h> |
| 9 | #include <common/macros.h> |
| 10 | #include <common/mi-lttng.h> |
| 11 | #include <lttng/action/action-internal.h> |
| 12 | #include <lttng/action/notify-internal.h> |
| 13 | #include <lttng/action/rate-policy-internal.h> |
| 14 | #include <lttng/lttng-error.h> |
| 15 | |
| 16 | #define IS_NOTIFY_ACTION(action) \ |
| 17 | (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_NOTIFY) |
| 18 | |
| 19 | static struct lttng_action_notify *action_notify_from_action( |
| 20 | struct lttng_action *action) |
| 21 | { |
| 22 | LTTNG_ASSERT(action); |
| 23 | |
| 24 | return container_of(action, struct lttng_action_notify, parent); |
| 25 | } |
| 26 | |
| 27 | static const struct lttng_action_notify *action_notify_from_action_const( |
| 28 | const struct lttng_action *action) |
| 29 | { |
| 30 | LTTNG_ASSERT(action); |
| 31 | |
| 32 | return container_of(action, struct lttng_action_notify, parent); |
| 33 | } |
| 34 | |
| 35 | static |
| 36 | void lttng_action_notify_destroy(struct lttng_action *action) |
| 37 | { |
| 38 | struct lttng_action_notify *notify_action; |
| 39 | notify_action = action_notify_from_action(action); |
| 40 | lttng_rate_policy_destroy(notify_action->policy); |
| 41 | free(notify_action); |
| 42 | } |
| 43 | |
| 44 | static |
| 45 | int lttng_action_notify_serialize(struct lttng_action *action, |
| 46 | struct lttng_payload *payload) |
| 47 | { |
| 48 | int ret; |
| 49 | struct lttng_action_notify *notify_action; |
| 50 | |
| 51 | if (!action || !IS_NOTIFY_ACTION(action) || !payload) { |
| 52 | ret = -1; |
| 53 | goto end; |
| 54 | } |
| 55 | |
| 56 | DBG("Serializing notify action"); |
| 57 | |
| 58 | notify_action = action_notify_from_action(action); |
| 59 | DBG("Serializing notify action rate policy"); |
| 60 | ret = lttng_rate_policy_serialize(notify_action->policy, payload); |
| 61 | |
| 62 | end: |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | static |
| 67 | bool lttng_action_notify_is_equal(const struct lttng_action *a, |
| 68 | const struct lttng_action *b) |
| 69 | { |
| 70 | const struct lttng_action_notify *_a, *_b; |
| 71 | |
| 72 | _a = action_notify_from_action_const(a); |
| 73 | _b = action_notify_from_action_const(b); |
| 74 | return lttng_rate_policy_is_equal(_a->policy, _b->policy); |
| 75 | } |
| 76 | |
| 77 | static const struct lttng_rate_policy * |
| 78 | lttng_action_notify_internal_get_rate_policy(const struct lttng_action *action) |
| 79 | { |
| 80 | const struct lttng_action_notify *_action; |
| 81 | _action = action_notify_from_action_const(action); |
| 82 | |
| 83 | return _action->policy; |
| 84 | } |
| 85 | |
| 86 | static enum lttng_error_code lttng_action_notify_mi_serialize( |
| 87 | const struct lttng_action *action, struct mi_writer *writer) |
| 88 | { |
| 89 | int ret; |
| 90 | enum lttng_action_status status; |
| 91 | enum lttng_error_code ret_code; |
| 92 | const struct lttng_rate_policy *policy = NULL; |
| 93 | |
| 94 | LTTNG_ASSERT(action); |
| 95 | LTTNG_ASSERT(IS_NOTIFY_ACTION(action)); |
| 96 | LTTNG_ASSERT(writer); |
| 97 | |
| 98 | status = lttng_action_notify_get_rate_policy(action, &policy); |
| 99 | LTTNG_ASSERT(status == LTTNG_ACTION_STATUS_OK); |
| 100 | LTTNG_ASSERT(policy != NULL); |
| 101 | |
| 102 | /* Open action notify. */ |
| 103 | ret = mi_lttng_writer_open_element( |
| 104 | writer, mi_lttng_element_action_notify); |
| 105 | if (ret) { |
| 106 | goto mi_error; |
| 107 | } |
| 108 | |
| 109 | ret_code = lttng_rate_policy_mi_serialize(policy, writer); |
| 110 | if (ret_code != LTTNG_OK) { |
| 111 | goto end; |
| 112 | } |
| 113 | |
| 114 | /* Close action notify element. */ |
| 115 | ret = mi_lttng_writer_close_element(writer); |
| 116 | if (ret) { |
| 117 | goto mi_error; |
| 118 | } |
| 119 | |
| 120 | ret_code = LTTNG_OK; |
| 121 | goto end; |
| 122 | |
| 123 | mi_error: |
| 124 | ret_code = LTTNG_ERR_MI_IO_FAIL; |
| 125 | end: |
| 126 | return ret_code; |
| 127 | } |
| 128 | |
| 129 | struct lttng_action *lttng_action_notify_create(void) |
| 130 | { |
| 131 | struct lttng_rate_policy *policy = NULL; |
| 132 | struct lttng_action_notify *notify = NULL; |
| 133 | struct lttng_action *action = NULL; |
| 134 | |
| 135 | notify = zmalloc(sizeof(struct lttng_action_notify)); |
| 136 | if (!notify) { |
| 137 | goto end; |
| 138 | } |
| 139 | |
| 140 | /* Default policy. */ |
| 141 | policy = lttng_rate_policy_every_n_create(1); |
| 142 | if (!policy) { |
| 143 | goto end; |
| 144 | } |
| 145 | |
| 146 | lttng_action_init(¬ify->parent, LTTNG_ACTION_TYPE_NOTIFY, NULL, |
| 147 | lttng_action_notify_serialize, |
| 148 | lttng_action_notify_is_equal, |
| 149 | lttng_action_notify_destroy, |
| 150 | lttng_action_notify_internal_get_rate_policy, |
| 151 | lttng_action_generic_add_error_query_results, |
| 152 | lttng_action_notify_mi_serialize); |
| 153 | |
| 154 | notify->policy = policy; |
| 155 | policy = NULL; |
| 156 | |
| 157 | action = ¬ify->parent; |
| 158 | notify = NULL; |
| 159 | |
| 160 | end: |
| 161 | free(notify); |
| 162 | lttng_rate_policy_destroy(policy); |
| 163 | return action; |
| 164 | } |
| 165 | |
| 166 | ssize_t lttng_action_notify_create_from_payload( |
| 167 | struct lttng_payload_view *view, |
| 168 | struct lttng_action **action) |
| 169 | { |
| 170 | enum lttng_action_status status; |
| 171 | ssize_t consumed_length; |
| 172 | struct lttng_rate_policy *rate_policy = NULL; |
| 173 | struct lttng_action *_action = NULL; |
| 174 | |
| 175 | consumed_length = lttng_rate_policy_create_from_payload( |
| 176 | view, &rate_policy); |
| 177 | if (!rate_policy) { |
| 178 | consumed_length = -1; |
| 179 | goto end; |
| 180 | } |
| 181 | |
| 182 | _action = lttng_action_notify_create(); |
| 183 | if (!_action) { |
| 184 | consumed_length = -1; |
| 185 | goto end; |
| 186 | } |
| 187 | |
| 188 | status = lttng_action_notify_set_rate_policy(_action, rate_policy); |
| 189 | if (status != LTTNG_ACTION_STATUS_OK) { |
| 190 | consumed_length = -1; |
| 191 | goto end; |
| 192 | } |
| 193 | |
| 194 | *action = _action; |
| 195 | _action = NULL; |
| 196 | |
| 197 | end: |
| 198 | lttng_rate_policy_destroy(rate_policy); |
| 199 | lttng_action_destroy(_action); |
| 200 | return consumed_length; |
| 201 | } |
| 202 | |
| 203 | enum lttng_action_status lttng_action_notify_set_rate_policy( |
| 204 | struct lttng_action *action, |
| 205 | const struct lttng_rate_policy *policy) |
| 206 | { |
| 207 | enum lttng_action_status status; |
| 208 | struct lttng_action_notify *notify_action; |
| 209 | struct lttng_rate_policy *copy = NULL; |
| 210 | |
| 211 | if (!action || !policy || !IS_NOTIFY_ACTION(action)) { |
| 212 | status = LTTNG_ACTION_STATUS_INVALID; |
| 213 | goto end; |
| 214 | } |
| 215 | |
| 216 | copy = lttng_rate_policy_copy(policy); |
| 217 | if (!copy) { |
| 218 | status = LTTNG_ACTION_STATUS_ERROR; |
| 219 | goto end; |
| 220 | } |
| 221 | |
| 222 | notify_action = action_notify_from_action(action); |
| 223 | |
| 224 | /* Free the previous rate policy .*/ |
| 225 | lttng_rate_policy_destroy(notify_action->policy); |
| 226 | |
| 227 | /* Assign the policy. */ |
| 228 | notify_action->policy = copy; |
| 229 | status = LTTNG_ACTION_STATUS_OK; |
| 230 | copy = NULL; |
| 231 | |
| 232 | end: |
| 233 | lttng_rate_policy_destroy(copy); |
| 234 | return status; |
| 235 | } |
| 236 | |
| 237 | enum lttng_action_status lttng_action_notify_get_rate_policy( |
| 238 | const struct lttng_action *action, |
| 239 | const struct lttng_rate_policy **policy) |
| 240 | { |
| 241 | enum lttng_action_status status; |
| 242 | const struct lttng_action_notify *notify_action; |
| 243 | |
| 244 | if (!action || !policy || !IS_NOTIFY_ACTION(action)) { |
| 245 | status = LTTNG_ACTION_STATUS_INVALID; |
| 246 | goto end; |
| 247 | } |
| 248 | |
| 249 | notify_action = action_notify_from_action_const(action); |
| 250 | |
| 251 | *policy = notify_action->policy; |
| 252 | status = LTTNG_ACTION_STATUS_OK; |
| 253 | end: |
| 254 | return status; |
| 255 | } |