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