2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
9 #include <common/error.h>
10 #include <common/macros.h>
11 #include <common/mi-lttng.h>
12 #include <lttng/action/action-internal.h>
13 #include <lttng/action/notify-internal.h>
14 #include <lttng/action/rate-policy-internal.h>
15 #include <lttng/lttng-error.h>
17 #define IS_NOTIFY_ACTION(action) \
18 (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_NOTIFY)
20 static struct lttng_action_notify
*action_notify_from_action(
21 struct lttng_action
*action
)
25 return container_of(action
, struct lttng_action_notify
, parent
);
28 static const struct lttng_action_notify
*action_notify_from_action_const(
29 const struct lttng_action
*action
)
33 return container_of(action
, struct lttng_action_notify
, parent
);
37 void lttng_action_notify_destroy(struct lttng_action
*action
)
39 struct lttng_action_notify
*notify_action
;
40 notify_action
= action_notify_from_action(action
);
41 lttng_rate_policy_destroy(notify_action
->policy
);
46 int lttng_action_notify_serialize(struct lttng_action
*action
,
47 struct lttng_payload
*payload
)
50 struct lttng_action_notify
*notify_action
;
52 if (!action
|| !IS_NOTIFY_ACTION(action
) || !payload
) {
57 DBG("Serializing notify action");
59 notify_action
= action_notify_from_action(action
);
60 DBG("Serializing notify action rate policy");
61 ret
= lttng_rate_policy_serialize(notify_action
->policy
, payload
);
68 bool lttng_action_notify_is_equal(const struct lttng_action
*a
,
69 const struct lttng_action
*b
)
71 const struct lttng_action_notify
*_a
, *_b
;
73 _a
= action_notify_from_action_const(a
);
74 _b
= action_notify_from_action_const(b
);
75 return lttng_rate_policy_is_equal(_a
->policy
, _b
->policy
);
78 static const struct lttng_rate_policy
*
79 lttng_action_notify_internal_get_rate_policy(const struct lttng_action
*action
)
81 const struct lttng_action_notify
*_action
;
82 _action
= action_notify_from_action_const(action
);
84 return _action
->policy
;
87 static enum lttng_error_code
lttng_action_notify_mi_serialize(
88 const struct lttng_action
*action
, struct mi_writer
*writer
)
91 enum lttng_action_status status
;
92 enum lttng_error_code ret_code
;
93 const struct lttng_rate_policy
*policy
= NULL
;
96 assert(IS_NOTIFY_ACTION(action
));
99 status
= lttng_action_notify_get_rate_policy(action
, &policy
);
100 assert(status
== LTTNG_ACTION_STATUS_OK
);
101 assert(policy
!= NULL
);
103 /* Open action notify. */
104 ret
= mi_lttng_writer_open_element(
105 writer
, mi_lttng_element_action_notify
);
110 ret_code
= lttng_rate_policy_mi_serialize(policy
, writer
);
111 if (ret_code
!= LTTNG_OK
) {
115 /* Close action notify element. */
116 ret
= mi_lttng_writer_close_element(writer
);
125 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
130 struct lttng_action
*lttng_action_notify_create(void)
132 struct lttng_rate_policy
*policy
= NULL
;
133 struct lttng_action_notify
*notify
= NULL
;
134 struct lttng_action
*action
= NULL
;
136 notify
= zmalloc(sizeof(struct lttng_action_notify
));
141 /* Default policy. */
142 policy
= lttng_rate_policy_every_n_create(1);
147 lttng_action_init(¬ify
->parent
, LTTNG_ACTION_TYPE_NOTIFY
, NULL
,
148 lttng_action_notify_serialize
,
149 lttng_action_notify_is_equal
,
150 lttng_action_notify_destroy
,
151 lttng_action_notify_internal_get_rate_policy
,
152 lttng_action_generic_add_error_query_results
,
153 lttng_action_notify_mi_serialize
);
155 notify
->policy
= policy
;
158 action
= ¬ify
->parent
;
163 lttng_rate_policy_destroy(policy
);
167 ssize_t
lttng_action_notify_create_from_payload(
168 struct lttng_payload_view
*view
,
169 struct lttng_action
**action
)
171 enum lttng_action_status status
;
172 ssize_t consumed_length
;
173 struct lttng_rate_policy
*rate_policy
= NULL
;
174 struct lttng_action
*_action
= NULL
;
176 consumed_length
= lttng_rate_policy_create_from_payload(
179 consumed_length
= -1;
183 _action
= lttng_action_notify_create();
185 consumed_length
= -1;
189 status
= lttng_action_notify_set_rate_policy(_action
, rate_policy
);
190 if (status
!= LTTNG_ACTION_STATUS_OK
) {
191 consumed_length
= -1;
199 lttng_rate_policy_destroy(rate_policy
);
200 lttng_action_destroy(_action
);
201 return consumed_length
;
204 enum lttng_action_status
lttng_action_notify_set_rate_policy(
205 struct lttng_action
*action
,
206 const struct lttng_rate_policy
*policy
)
208 enum lttng_action_status status
;
209 struct lttng_action_notify
*notify_action
;
210 struct lttng_rate_policy
*copy
= NULL
;
212 if (!action
|| !policy
|| !IS_NOTIFY_ACTION(action
)) {
213 status
= LTTNG_ACTION_STATUS_INVALID
;
217 copy
= lttng_rate_policy_copy(policy
);
219 status
= LTTNG_ACTION_STATUS_ERROR
;
223 notify_action
= action_notify_from_action(action
);
225 /* Free the previous rate policy .*/
226 lttng_rate_policy_destroy(notify_action
->policy
);
228 /* Assign the policy. */
229 notify_action
->policy
= copy
;
230 status
= LTTNG_ACTION_STATUS_OK
;
234 lttng_rate_policy_destroy(copy
);
238 enum lttng_action_status
lttng_action_notify_get_rate_policy(
239 const struct lttng_action
*action
,
240 const struct lttng_rate_policy
**policy
)
242 enum lttng_action_status status
;
243 const struct lttng_action_notify
*notify_action
;
245 if (!action
|| !policy
|| !IS_NOTIFY_ACTION(action
)) {
246 status
= LTTNG_ACTION_STATUS_INVALID
;
250 notify_action
= action_notify_from_action_const(action
);
252 *policy
= notify_action
->policy
;
253 status
= LTTNG_ACTION_STATUS_OK
;