action-executor: consider action firing policy on action execution
[lttng-tools.git] / src / common / actions / notify.c
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <assert.h>
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>
14
15 #define IS_NOTIFY_ACTION(action) \
16 (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_NOTIFY)
17
18 static 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
26 static 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 }
33
34 static
35 void lttng_action_notify_destroy(struct lttng_action *action)
36 {
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);
41 }
42
43 static
44 int lttng_action_notify_serialize(struct lttng_action *action,
45 struct lttng_payload *payload)
46 {
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
61 end:
62 return ret;
63 }
64
65 static
66 bool lttng_action_notify_is_equal(const struct lttng_action *a,
67 const struct lttng_action *b)
68 {
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);
74 }
75
76 static const struct lttng_firing_policy *
77 lttng_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
86 struct lttng_action *lttng_action_notify_create(void)
87 {
88 struct lttng_firing_policy *policy = NULL;
89 struct lttng_action_notify *notify = NULL;
90 struct lttng_action *action = NULL;
91
92 notify = zmalloc(sizeof(struct lttng_action_notify));
93 if (!notify) {
94 goto end;
95 }
96
97 /* Default policy. */
98 policy = lttng_firing_policy_every_n_create(1);
99 if (!policy) {
100 goto end;
101 }
102
103 lttng_action_init(&notify->parent, LTTNG_ACTION_TYPE_NOTIFY, NULL,
104 lttng_action_notify_serialize,
105 lttng_action_notify_is_equal,
106 lttng_action_notify_destroy,
107 lttng_action_notify_internal_get_firing_policy);
108
109 notify->policy = policy;
110 policy = NULL;
111
112 action = &notify->parent;
113 notify = NULL;
114
115 end:
116 free(notify);
117 lttng_firing_policy_destroy(policy);
118 return action;
119 }
120
121 ssize_t lttng_action_notify_create_from_payload(
122 struct lttng_payload_view *view,
123 struct lttng_action **action)
124 {
125 enum lttng_action_status status;
126 ssize_t consumed_length;
127 struct lttng_firing_policy *firing_policy = NULL;
128 struct lttng_action *_action = NULL;
129
130 consumed_length = lttng_firing_policy_create_from_payload(
131 view, &firing_policy);
132 if (!firing_policy) {
133 consumed_length = -1;
134 goto end;
135 }
136
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
152 end:
153 lttng_firing_policy_destroy(firing_policy);
154 lttng_action_destroy(_action);
155 return consumed_length;
156 }
157
158 enum 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
187 end:
188 lttng_firing_policy_destroy(copy);
189 return status;
190 }
191
192 enum 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;
208 end:
209 return status;
210 }
This page took 0.033377 seconds and 4 git commands to generate.