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 <lttng/action/action-internal.h>
12 #include <lttng/action/notify-internal.h>
13 #include <lttng/action/rate-policy-internal.h>
15 #define IS_NOTIFY_ACTION(action) \
16 (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_NOTIFY)
18 static struct lttng_action_notify
*action_notify_from_action(
19 struct lttng_action
*action
)
23 return container_of(action
, struct lttng_action_notify
, parent
);
26 static const struct lttng_action_notify
*action_notify_from_action_const(
27 const struct lttng_action
*action
)
31 return container_of(action
, struct lttng_action_notify
, parent
);
35 void lttng_action_notify_destroy(struct lttng_action
*action
)
37 struct lttng_action_notify
*notify_action
;
38 notify_action
= action_notify_from_action(action
);
39 lttng_rate_policy_destroy(notify_action
->policy
);
44 int lttng_action_notify_serialize(struct lttng_action
*action
,
45 struct lttng_payload
*payload
)
48 struct lttng_action_notify
*notify_action
;
50 if (!action
|| !IS_NOTIFY_ACTION(action
) || !payload
) {
55 DBG("Serializing notify action");
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
);
66 bool lttng_action_notify_is_equal(const struct lttng_action
*a
,
67 const struct lttng_action
*b
)
69 const struct lttng_action_notify
*_a
, *_b
;
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
);
76 static const struct lttng_rate_policy
*
77 lttng_action_notify_internal_get_rate_policy(const struct lttng_action
*action
)
79 const struct lttng_action_notify
*_action
;
80 _action
= action_notify_from_action_const(action
);
82 return _action
->policy
;
85 struct lttng_action
*lttng_action_notify_create(void)
87 struct lttng_rate_policy
*policy
= NULL
;
88 struct lttng_action_notify
*notify
= NULL
;
89 struct lttng_action
*action
= NULL
;
91 notify
= zmalloc(sizeof(struct lttng_action_notify
));
97 policy
= lttng_rate_policy_every_n_create(1);
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
);
109 notify
->policy
= policy
;
112 action
= ¬ify
->parent
;
117 lttng_rate_policy_destroy(policy
);
121 ssize_t
lttng_action_notify_create_from_payload(
122 struct lttng_payload_view
*view
,
123 struct lttng_action
**action
)
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
;
130 consumed_length
= lttng_rate_policy_create_from_payload(
133 consumed_length
= -1;
137 _action
= lttng_action_notify_create();
139 consumed_length
= -1;
143 status
= lttng_action_notify_set_rate_policy(_action
, rate_policy
);
144 if (status
!= LTTNG_ACTION_STATUS_OK
) {
145 consumed_length
= -1;
153 lttng_rate_policy_destroy(rate_policy
);
154 lttng_action_destroy(_action
);
155 return consumed_length
;
158 enum lttng_action_status
lttng_action_notify_set_rate_policy(
159 struct lttng_action
*action
,
160 const struct lttng_rate_policy
*policy
)
162 enum lttng_action_status status
;
163 struct lttng_action_notify
*notify_action
;
164 struct lttng_rate_policy
*copy
= NULL
;
166 if (!action
|| !policy
|| !IS_NOTIFY_ACTION(action
)) {
167 status
= LTTNG_ACTION_STATUS_INVALID
;
171 copy
= lttng_rate_policy_copy(policy
);
173 status
= LTTNG_ACTION_STATUS_ERROR
;
177 notify_action
= action_notify_from_action(action
);
179 /* Free the previous rate policy .*/
180 lttng_rate_policy_destroy(notify_action
->policy
);
182 /* Assign the policy. */
183 notify_action
->policy
= copy
;
184 status
= LTTNG_ACTION_STATUS_OK
;
188 lttng_rate_policy_destroy(copy
);
192 enum lttng_action_status
lttng_action_notify_get_rate_policy(
193 const struct lttng_action
*action
,
194 const struct lttng_rate_policy
**policy
)
196 enum lttng_action_status status
;
197 const struct lttng_action_notify
*notify_action
;
199 if (!action
|| !policy
|| !IS_NOTIFY_ACTION(action
)) {
200 status
= LTTNG_ACTION_STATUS_INVALID
;
204 notify_action
= action_notify_from_action_const(action
);
206 *policy
= notify_action
->policy
;
207 status
= LTTNG_ACTION_STATUS_OK
;