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/firing-policy-internal.h>
13 #include <lttng/action/notify-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_firing_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 firing policy");
59 ret
= lttng_firing_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_firing_policy_is_equal(_a
->policy
, _b
->policy
);
76 struct lttng_action
*lttng_action_notify_create(void)
78 struct lttng_firing_policy
*policy
= NULL
;
79 struct lttng_action_notify
*notify
= NULL
;
80 struct lttng_action
*action
= NULL
;
82 notify
= zmalloc(sizeof(struct lttng_action_notify
));
88 policy
= lttng_firing_policy_every_n_create(1);
93 lttng_action_init(¬ify
->parent
, LTTNG_ACTION_TYPE_NOTIFY
, NULL
,
94 lttng_action_notify_serialize
,
95 lttng_action_notify_is_equal
,
96 lttng_action_notify_destroy
);
98 notify
->policy
= policy
;
101 action
= ¬ify
->parent
;
106 lttng_firing_policy_destroy(policy
);
110 ssize_t
lttng_action_notify_create_from_payload(
111 struct lttng_payload_view
*view
,
112 struct lttng_action
**action
)
114 enum lttng_action_status status
;
115 ssize_t consumed_length
;
116 struct lttng_firing_policy
*firing_policy
= NULL
;
117 struct lttng_action
*_action
= NULL
;
119 consumed_length
= lttng_firing_policy_create_from_payload(
120 view
, &firing_policy
);
121 if (!firing_policy
) {
122 consumed_length
= -1;
126 _action
= lttng_action_notify_create();
128 consumed_length
= -1;
132 status
= lttng_action_notify_set_firing_policy(_action
, firing_policy
);
133 if (status
!= LTTNG_ACTION_STATUS_OK
) {
134 consumed_length
= -1;
142 lttng_firing_policy_destroy(firing_policy
);
143 lttng_action_destroy(_action
);
144 return consumed_length
;
147 enum lttng_action_status
lttng_action_notify_set_firing_policy(
148 struct lttng_action
*action
,
149 const struct lttng_firing_policy
*policy
)
151 enum lttng_action_status status
;
152 struct lttng_action_notify
*notify_action
;
153 struct lttng_firing_policy
*copy
= NULL
;
155 if (!action
|| !policy
|| !IS_NOTIFY_ACTION(action
)) {
156 status
= LTTNG_ACTION_STATUS_INVALID
;
160 copy
= lttng_firing_policy_copy(policy
);
162 status
= LTTNG_ACTION_STATUS_ERROR
;
166 notify_action
= action_notify_from_action(action
);
168 /* Free the previous firing policy .*/
169 lttng_firing_policy_destroy(notify_action
->policy
);
171 /* Assign the policy. */
172 notify_action
->policy
= copy
;
173 status
= LTTNG_ACTION_STATUS_OK
;
177 lttng_firing_policy_destroy(copy
);
181 enum lttng_action_status
lttng_action_notify_get_firing_policy(
182 const struct lttng_action
*action
,
183 const struct lttng_firing_policy
**policy
)
185 enum lttng_action_status status
;
186 const struct lttng_action_notify
*notify_action
;
188 if (!action
|| !policy
|| !IS_NOTIFY_ACTION(action
)) {
189 status
= LTTNG_ACTION_STATUS_INVALID
;
193 notify_action
= action_notify_from_action_const(action
);
195 *policy
= notify_action
->policy
;
196 status
= LTTNG_ACTION_STATUS_OK
;