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