Implement firing policy for the notify action
[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 struct lttng_action *lttng_action_notify_create(void)
77 {
78 struct lttng_firing_policy *policy = NULL;
79 struct lttng_action_notify *notify = NULL;
80 struct lttng_action *action = NULL;
81
82 notify = zmalloc(sizeof(struct lttng_action_notify));
83 if (!notify) {
84 goto end;
85 }
86
87 /* Default policy. */
88 policy = lttng_firing_policy_every_n_create(1);
89 if (!policy) {
90 goto end;
91 }
92
93 lttng_action_init(&notify->parent, LTTNG_ACTION_TYPE_NOTIFY, NULL,
94 lttng_action_notify_serialize,
95 lttng_action_notify_is_equal,
96 lttng_action_notify_destroy);
97
98 notify->policy = policy;
99 policy = NULL;
100
101 action = &notify->parent;
102 notify = NULL;
103
104 end:
105 free(notify);
106 lttng_firing_policy_destroy(policy);
107 return action;
108 }
109
110 ssize_t lttng_action_notify_create_from_payload(
111 struct lttng_payload_view *view,
112 struct lttng_action **action)
113 {
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;
118
119 consumed_length = lttng_firing_policy_create_from_payload(
120 view, &firing_policy);
121 if (!firing_policy) {
122 consumed_length = -1;
123 goto end;
124 }
125
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
141 end:
142 lttng_firing_policy_destroy(firing_policy);
143 lttng_action_destroy(_action);
144 return consumed_length;
145 }
146
147 enum 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
176 end:
177 lttng_firing_policy_destroy(copy);
178 return status;
179 }
180
181 enum 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;
197 end:
198 return status;
199 }
This page took 0.03421 seconds and 5 git commands to generate.