Implement firing policy for the notify action
[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
a58c490f
JG
76struct lttng_action *lttng_action_notify_create(void)
77{
1c2c1c05
JR
78 struct lttng_firing_policy *policy = NULL;
79 struct lttng_action_notify *notify = NULL;
80 struct lttng_action *action = NULL;
a58c490f
JG
81
82 notify = zmalloc(sizeof(struct lttng_action_notify));
83 if (!notify) {
84 goto end;
85 }
86
1c2c1c05
JR
87 /* Default policy. */
88 policy = lttng_firing_policy_every_n_create(1);
89 if (!policy) {
90 goto end;
91 }
92
6acb3f46
SM
93 lttng_action_init(&notify->parent, LTTNG_ACTION_TYPE_NOTIFY, NULL,
94 lttng_action_notify_serialize,
3dd04a6a 95 lttng_action_notify_is_equal,
6acb3f46 96 lttng_action_notify_destroy);
1c2c1c05
JR
97
98 notify->policy = policy;
99 policy = NULL;
100
101 action = &notify->parent;
102 notify = NULL;
103
a58c490f 104end:
1c2c1c05
JR
105 free(notify);
106 lttng_firing_policy_destroy(policy);
107 return action;
a58c490f 108}
869a3c2d 109
c0a66c84
JG
110ssize_t lttng_action_notify_create_from_payload(
111 struct lttng_payload_view *view,
869a3c2d
SM
112 struct lttng_action **action)
113{
1c2c1c05 114 enum lttng_action_status status;
869a3c2d 115 ssize_t consumed_length;
1c2c1c05
JR
116 struct lttng_firing_policy *firing_policy = NULL;
117 struct lttng_action *_action = NULL;
869a3c2d 118
1c2c1c05
JR
119 consumed_length = lttng_firing_policy_create_from_payload(
120 view, &firing_policy);
121 if (!firing_policy) {
869a3c2d
SM
122 consumed_length = -1;
123 goto end;
124 }
125
1c2c1c05
JR
126 _action = lttng_action_notify_create();
127 if (!_action) {
128 consumed_length = -1;
129 goto end;
130 }
131
132 status = lttng_action_notify_set_firing_policy(_action, firing_policy);
133 if (status != LTTNG_ACTION_STATUS_OK) {
134 consumed_length = -1;
135 goto end;
136 }
137
138 *action = _action;
139 _action = NULL;
140
869a3c2d 141end:
1c2c1c05
JR
142 lttng_firing_policy_destroy(firing_policy);
143 lttng_action_destroy(_action);
869a3c2d
SM
144 return consumed_length;
145}
1c2c1c05
JR
146
147enum lttng_action_status lttng_action_notify_set_firing_policy(
148 struct lttng_action *action,
149 const struct lttng_firing_policy *policy)
150{
151 enum lttng_action_status status;
152 struct lttng_action_notify *notify_action;
153 struct lttng_firing_policy *copy = NULL;
154
155 if (!action || !policy || !IS_NOTIFY_ACTION(action)) {
156 status = LTTNG_ACTION_STATUS_INVALID;
157 goto end;
158 }
159
160 copy = lttng_firing_policy_copy(policy);
161 if (!copy) {
162 status = LTTNG_ACTION_STATUS_ERROR;
163 goto end;
164 }
165
166 notify_action = action_notify_from_action(action);
167
168 /* Free the previous firing policy .*/
169 lttng_firing_policy_destroy(notify_action->policy);
170
171 /* Assign the policy. */
172 notify_action->policy = copy;
173 status = LTTNG_ACTION_STATUS_OK;
174 copy = NULL;
175
176end:
177 lttng_firing_policy_destroy(copy);
178 return status;
179}
180
181enum lttng_action_status lttng_action_notify_get_firing_policy(
182 const struct lttng_action *action,
183 const struct lttng_firing_policy **policy)
184{
185 enum lttng_action_status status;
186 const struct lttng_action_notify *notify_action;
187
188 if (!action || !policy || !IS_NOTIFY_ACTION(action)) {
189 status = LTTNG_ACTION_STATUS_INVALID;
190 goto end;
191 }
192
193 notify_action = action_notify_from_action_const(action);
194
195 *policy = notify_action->policy;
196 status = LTTNG_ACTION_STATUS_OK;
197end:
198 return status;
199}
This page took 0.044195 seconds and 4 git commands to generate.