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 | ||
1c2c1c05 JR |
8 | #include <common/error.h> |
9 | #include <common/macros.h> | |
6a751b95 | 10 | #include <common/mi-lttng.h> |
a58c490f JG |
11 | #include <lttng/action/action-internal.h> |
12 | #include <lttng/action/notify-internal.h> | |
7f4d5b07 | 13 | #include <lttng/action/rate-policy-internal.h> |
6a751b95 | 14 | #include <lttng/lttng-error.h> |
1c2c1c05 JR |
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 | { | |
a0377dfe | 22 | LTTNG_ASSERT(action); |
1c2c1c05 JR |
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 | { | |
a0377dfe | 30 | LTTNG_ASSERT(action); |
1c2c1c05 JR |
31 | |
32 | return container_of(action, struct lttng_action_notify, parent); | |
33 | } | |
a58c490f JG |
34 | |
35 | static | |
36 | void lttng_action_notify_destroy(struct lttng_action *action) | |
37 | { | |
1c2c1c05 JR |
38 | struct lttng_action_notify *notify_action; |
39 | notify_action = action_notify_from_action(action); | |
7f4d5b07 | 40 | lttng_rate_policy_destroy(notify_action->policy); |
1c2c1c05 | 41 | free(notify_action); |
a58c490f JG |
42 | } |
43 | ||
44 | static | |
3647288f | 45 | int lttng_action_notify_serialize(struct lttng_action *action, |
c0a66c84 | 46 | struct lttng_payload *payload) |
a58c490f | 47 | { |
1c2c1c05 JR |
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); | |
7f4d5b07 JR |
59 | DBG("Serializing notify action rate policy"); |
60 | ret = lttng_rate_policy_serialize(notify_action->policy, payload); | |
1c2c1c05 JR |
61 | |
62 | end: | |
63 | return ret; | |
a58c490f JG |
64 | } |
65 | ||
3dd04a6a JR |
66 | static |
67 | bool lttng_action_notify_is_equal(const struct lttng_action *a, | |
68 | const struct lttng_action *b) | |
69 | { | |
1c2c1c05 JR |
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); | |
7f4d5b07 | 74 | return lttng_rate_policy_is_equal(_a->policy, _b->policy); |
3dd04a6a JR |
75 | } |
76 | ||
7f4d5b07 JR |
77 | static const struct lttng_rate_policy * |
78 | lttng_action_notify_internal_get_rate_policy(const struct lttng_action *action) | |
2d57482c JR |
79 | { |
80 | const struct lttng_action_notify *_action; | |
81 | _action = action_notify_from_action_const(action); | |
82 | ||
83 | return _action->policy; | |
84 | } | |
85 | ||
6a751b95 JR |
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 | ||
a0377dfe FD |
94 | LTTNG_ASSERT(action); |
95 | LTTNG_ASSERT(IS_NOTIFY_ACTION(action)); | |
96 | LTTNG_ASSERT(writer); | |
6a751b95 JR |
97 | |
98 | status = lttng_action_notify_get_rate_policy(action, &policy); | |
a0377dfe FD |
99 | LTTNG_ASSERT(status == LTTNG_ACTION_STATUS_OK); |
100 | LTTNG_ASSERT(policy != NULL); | |
6a751b95 JR |
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 | ||
a58c490f JG |
129 | struct lttng_action *lttng_action_notify_create(void) |
130 | { | |
7f4d5b07 | 131 | struct lttng_rate_policy *policy = NULL; |
1c2c1c05 JR |
132 | struct lttng_action_notify *notify = NULL; |
133 | struct lttng_action *action = NULL; | |
a58c490f JG |
134 | |
135 | notify = zmalloc(sizeof(struct lttng_action_notify)); | |
136 | if (!notify) { | |
137 | goto end; | |
138 | } | |
139 | ||
1c2c1c05 | 140 | /* Default policy. */ |
7f4d5b07 | 141 | policy = lttng_rate_policy_every_n_create(1); |
1c2c1c05 JR |
142 | if (!policy) { |
143 | goto end; | |
144 | } | |
145 | ||
6acb3f46 SM |
146 | lttng_action_init(¬ify->parent, LTTNG_ACTION_TYPE_NOTIFY, NULL, |
147 | lttng_action_notify_serialize, | |
3dd04a6a | 148 | lttng_action_notify_is_equal, |
2d57482c | 149 | lttng_action_notify_destroy, |
588c4b0d | 150 | lttng_action_notify_internal_get_rate_policy, |
6a751b95 JR |
151 | lttng_action_generic_add_error_query_results, |
152 | lttng_action_notify_mi_serialize); | |
1c2c1c05 JR |
153 | |
154 | notify->policy = policy; | |
155 | policy = NULL; | |
156 | ||
157 | action = ¬ify->parent; | |
158 | notify = NULL; | |
159 | ||
a58c490f | 160 | end: |
1c2c1c05 | 161 | free(notify); |
7f4d5b07 | 162 | lttng_rate_policy_destroy(policy); |
1c2c1c05 | 163 | return action; |
a58c490f | 164 | } |
869a3c2d | 165 | |
c0a66c84 JG |
166 | ssize_t lttng_action_notify_create_from_payload( |
167 | struct lttng_payload_view *view, | |
869a3c2d SM |
168 | struct lttng_action **action) |
169 | { | |
1c2c1c05 | 170 | enum lttng_action_status status; |
869a3c2d | 171 | ssize_t consumed_length; |
7f4d5b07 | 172 | struct lttng_rate_policy *rate_policy = NULL; |
1c2c1c05 | 173 | struct lttng_action *_action = NULL; |
869a3c2d | 174 | |
7f4d5b07 JR |
175 | consumed_length = lttng_rate_policy_create_from_payload( |
176 | view, &rate_policy); | |
177 | if (!rate_policy) { | |
869a3c2d SM |
178 | consumed_length = -1; |
179 | goto end; | |
180 | } | |
181 | ||
1c2c1c05 JR |
182 | _action = lttng_action_notify_create(); |
183 | if (!_action) { | |
184 | consumed_length = -1; | |
185 | goto end; | |
186 | } | |
187 | ||
7f4d5b07 | 188 | status = lttng_action_notify_set_rate_policy(_action, rate_policy); |
1c2c1c05 JR |
189 | if (status != LTTNG_ACTION_STATUS_OK) { |
190 | consumed_length = -1; | |
191 | goto end; | |
192 | } | |
193 | ||
194 | *action = _action; | |
195 | _action = NULL; | |
196 | ||
869a3c2d | 197 | end: |
7f4d5b07 | 198 | lttng_rate_policy_destroy(rate_policy); |
1c2c1c05 | 199 | lttng_action_destroy(_action); |
869a3c2d SM |
200 | return consumed_length; |
201 | } | |
1c2c1c05 | 202 | |
7f4d5b07 | 203 | enum lttng_action_status lttng_action_notify_set_rate_policy( |
1c2c1c05 | 204 | struct lttng_action *action, |
7f4d5b07 | 205 | const struct lttng_rate_policy *policy) |
1c2c1c05 JR |
206 | { |
207 | enum lttng_action_status status; | |
208 | struct lttng_action_notify *notify_action; | |
7f4d5b07 | 209 | struct lttng_rate_policy *copy = NULL; |
1c2c1c05 JR |
210 | |
211 | if (!action || !policy || !IS_NOTIFY_ACTION(action)) { | |
212 | status = LTTNG_ACTION_STATUS_INVALID; | |
213 | goto end; | |
214 | } | |
215 | ||
7f4d5b07 | 216 | copy = lttng_rate_policy_copy(policy); |
1c2c1c05 JR |
217 | if (!copy) { |
218 | status = LTTNG_ACTION_STATUS_ERROR; | |
219 | goto end; | |
220 | } | |
221 | ||
222 | notify_action = action_notify_from_action(action); | |
223 | ||
7f4d5b07 JR |
224 | /* Free the previous rate policy .*/ |
225 | lttng_rate_policy_destroy(notify_action->policy); | |
1c2c1c05 JR |
226 | |
227 | /* Assign the policy. */ | |
228 | notify_action->policy = copy; | |
229 | status = LTTNG_ACTION_STATUS_OK; | |
230 | copy = NULL; | |
231 | ||
232 | end: | |
7f4d5b07 | 233 | lttng_rate_policy_destroy(copy); |
1c2c1c05 JR |
234 | return status; |
235 | } | |
236 | ||
7f4d5b07 | 237 | enum lttng_action_status lttng_action_notify_get_rate_policy( |
1c2c1c05 | 238 | const struct lttng_action *action, |
7f4d5b07 | 239 | const struct lttng_rate_policy **policy) |
1c2c1c05 JR |
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 | } |