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