action-executor: consider action firing policy on action execution
[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>
a58c490f 11#include <lttng/action/action-internal.h>
1c2c1c05 12#include <lttng/action/firing-policy-internal.h>
a58c490f 13#include <lttng/action/notify-internal.h>
1c2c1c05
JR
14
15#define IS_NOTIFY_ACTION(action) \
16 (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_NOTIFY)
17
18static struct lttng_action_notify *action_notify_from_action(
19 struct lttng_action *action)
20{
21 assert(action);
22
23 return container_of(action, struct lttng_action_notify, parent);
24}
25
26static const struct lttng_action_notify *action_notify_from_action_const(
27 const struct lttng_action *action)
28{
29 assert(action);
30
31 return container_of(action, struct lttng_action_notify, parent);
32}
a58c490f
JG
33
34static
35void lttng_action_notify_destroy(struct lttng_action *action)
36{
1c2c1c05
JR
37 struct lttng_action_notify *notify_action;
38 notify_action = action_notify_from_action(action);
39 lttng_firing_policy_destroy(notify_action->policy);
40 free(notify_action);
a58c490f
JG
41}
42
43static
3647288f 44int lttng_action_notify_serialize(struct lttng_action *action,
c0a66c84 45 struct lttng_payload *payload)
a58c490f 46{
1c2c1c05
JR
47 int ret;
48 struct lttng_action_notify *notify_action;
49
50 if (!action || !IS_NOTIFY_ACTION(action) || !payload) {
51 ret = -1;
52 goto end;
53 }
54
55 DBG("Serializing notify action");
56
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);
60
61end:
62 return ret;
a58c490f
JG
63}
64
3dd04a6a
JR
65static
66bool lttng_action_notify_is_equal(const struct lttng_action *a,
67 const struct lttng_action *b)
68{
1c2c1c05
JR
69 const struct lttng_action_notify *_a, *_b;
70
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);
3dd04a6a
JR
74}
75
2d57482c
JR
76static const struct lttng_firing_policy *
77lttng_action_notify_internal_get_firing_policy(
78 const struct lttng_action *action)
79{
80 const struct lttng_action_notify *_action;
81 _action = action_notify_from_action_const(action);
82
83 return _action->policy;
84}
85
a58c490f
JG
86struct lttng_action *lttng_action_notify_create(void)
87{
1c2c1c05
JR
88 struct lttng_firing_policy *policy = NULL;
89 struct lttng_action_notify *notify = NULL;
90 struct lttng_action *action = NULL;
a58c490f
JG
91
92 notify = zmalloc(sizeof(struct lttng_action_notify));
93 if (!notify) {
94 goto end;
95 }
96
1c2c1c05
JR
97 /* Default policy. */
98 policy = lttng_firing_policy_every_n_create(1);
99 if (!policy) {
100 goto end;
101 }
102
6acb3f46
SM
103 lttng_action_init(&notify->parent, LTTNG_ACTION_TYPE_NOTIFY, NULL,
104 lttng_action_notify_serialize,
3dd04a6a 105 lttng_action_notify_is_equal,
2d57482c
JR
106 lttng_action_notify_destroy,
107 lttng_action_notify_internal_get_firing_policy);
1c2c1c05
JR
108
109 notify->policy = policy;
110 policy = NULL;
111
112 action = &notify->parent;
113 notify = NULL;
114
a58c490f 115end:
1c2c1c05
JR
116 free(notify);
117 lttng_firing_policy_destroy(policy);
118 return action;
a58c490f 119}
869a3c2d 120
c0a66c84
JG
121ssize_t lttng_action_notify_create_from_payload(
122 struct lttng_payload_view *view,
869a3c2d
SM
123 struct lttng_action **action)
124{
1c2c1c05 125 enum lttng_action_status status;
869a3c2d 126 ssize_t consumed_length;
1c2c1c05
JR
127 struct lttng_firing_policy *firing_policy = NULL;
128 struct lttng_action *_action = NULL;
869a3c2d 129
1c2c1c05
JR
130 consumed_length = lttng_firing_policy_create_from_payload(
131 view, &firing_policy);
132 if (!firing_policy) {
869a3c2d
SM
133 consumed_length = -1;
134 goto end;
135 }
136
1c2c1c05
JR
137 _action = lttng_action_notify_create();
138 if (!_action) {
139 consumed_length = -1;
140 goto end;
141 }
142
143 status = lttng_action_notify_set_firing_policy(_action, firing_policy);
144 if (status != LTTNG_ACTION_STATUS_OK) {
145 consumed_length = -1;
146 goto end;
147 }
148
149 *action = _action;
150 _action = NULL;
151
869a3c2d 152end:
1c2c1c05
JR
153 lttng_firing_policy_destroy(firing_policy);
154 lttng_action_destroy(_action);
869a3c2d
SM
155 return consumed_length;
156}
1c2c1c05
JR
157
158enum lttng_action_status lttng_action_notify_set_firing_policy(
159 struct lttng_action *action,
160 const struct lttng_firing_policy *policy)
161{
162 enum lttng_action_status status;
163 struct lttng_action_notify *notify_action;
164 struct lttng_firing_policy *copy = NULL;
165
166 if (!action || !policy || !IS_NOTIFY_ACTION(action)) {
167 status = LTTNG_ACTION_STATUS_INVALID;
168 goto end;
169 }
170
171 copy = lttng_firing_policy_copy(policy);
172 if (!copy) {
173 status = LTTNG_ACTION_STATUS_ERROR;
174 goto end;
175 }
176
177 notify_action = action_notify_from_action(action);
178
179 /* Free the previous firing policy .*/
180 lttng_firing_policy_destroy(notify_action->policy);
181
182 /* Assign the policy. */
183 notify_action->policy = copy;
184 status = LTTNG_ACTION_STATUS_OK;
185 copy = NULL;
186
187end:
188 lttng_firing_policy_destroy(copy);
189 return status;
190}
191
192enum lttng_action_status lttng_action_notify_get_firing_policy(
193 const struct lttng_action *action,
194 const struct lttng_firing_policy **policy)
195{
196 enum lttng_action_status status;
197 const struct lttng_action_notify *notify_action;
198
199 if (!action || !policy || !IS_NOTIFY_ACTION(action)) {
200 status = LTTNG_ACTION_STATUS_INVALID;
201 goto end;
202 }
203
204 notify_action = action_notify_from_action_const(action);
205
206 *policy = notify_action->policy;
207 status = LTTNG_ACTION_STATUS_OK;
208end:
209 return status;
210}
This page took 0.043681 seconds and 4 git commands to generate.