0ab88be44fe6397ab7351dc058a3057dfef2c8a2
[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/notify-internal.h>
13 #include <lttng/action/rate-policy-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_rate_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 rate policy");
59 ret = lttng_rate_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_rate_policy_is_equal(_a->policy, _b->policy);
74 }
75
76 static const struct lttng_rate_policy *
77 lttng_action_notify_internal_get_rate_policy(const struct lttng_action *action)
78 {
79 const struct lttng_action_notify *_action;
80 _action = action_notify_from_action_const(action);
81
82 return _action->policy;
83 }
84
85 struct lttng_action *lttng_action_notify_create(void)
86 {
87 struct lttng_rate_policy *policy = NULL;
88 struct lttng_action_notify *notify = NULL;
89 struct lttng_action *action = NULL;
90
91 notify = zmalloc(sizeof(struct lttng_action_notify));
92 if (!notify) {
93 goto end;
94 }
95
96 /* Default policy. */
97 policy = lttng_rate_policy_every_n_create(1);
98 if (!policy) {
99 goto end;
100 }
101
102 lttng_action_init(&notify->parent, LTTNG_ACTION_TYPE_NOTIFY, NULL,
103 lttng_action_notify_serialize,
104 lttng_action_notify_is_equal,
105 lttng_action_notify_destroy,
106 lttng_action_notify_internal_get_rate_policy);
107
108 notify->policy = policy;
109 policy = NULL;
110
111 action = &notify->parent;
112 notify = NULL;
113
114 end:
115 free(notify);
116 lttng_rate_policy_destroy(policy);
117 return action;
118 }
119
120 ssize_t lttng_action_notify_create_from_payload(
121 struct lttng_payload_view *view,
122 struct lttng_action **action)
123 {
124 enum lttng_action_status status;
125 ssize_t consumed_length;
126 struct lttng_rate_policy *rate_policy = NULL;
127 struct lttng_action *_action = NULL;
128
129 consumed_length = lttng_rate_policy_create_from_payload(
130 view, &rate_policy);
131 if (!rate_policy) {
132 consumed_length = -1;
133 goto end;
134 }
135
136 _action = lttng_action_notify_create();
137 if (!_action) {
138 consumed_length = -1;
139 goto end;
140 }
141
142 status = lttng_action_notify_set_rate_policy(_action, rate_policy);
143 if (status != LTTNG_ACTION_STATUS_OK) {
144 consumed_length = -1;
145 goto end;
146 }
147
148 *action = _action;
149 _action = NULL;
150
151 end:
152 lttng_rate_policy_destroy(rate_policy);
153 lttng_action_destroy(_action);
154 return consumed_length;
155 }
156
157 enum lttng_action_status lttng_action_notify_set_rate_policy(
158 struct lttng_action *action,
159 const struct lttng_rate_policy *policy)
160 {
161 enum lttng_action_status status;
162 struct lttng_action_notify *notify_action;
163 struct lttng_rate_policy *copy = NULL;
164
165 if (!action || !policy || !IS_NOTIFY_ACTION(action)) {
166 status = LTTNG_ACTION_STATUS_INVALID;
167 goto end;
168 }
169
170 copy = lttng_rate_policy_copy(policy);
171 if (!copy) {
172 status = LTTNG_ACTION_STATUS_ERROR;
173 goto end;
174 }
175
176 notify_action = action_notify_from_action(action);
177
178 /* Free the previous rate policy .*/
179 lttng_rate_policy_destroy(notify_action->policy);
180
181 /* Assign the policy. */
182 notify_action->policy = copy;
183 status = LTTNG_ACTION_STATUS_OK;
184 copy = NULL;
185
186 end:
187 lttng_rate_policy_destroy(copy);
188 return status;
189 }
190
191 enum lttng_action_status lttng_action_notify_get_rate_policy(
192 const struct lttng_action *action,
193 const struct lttng_rate_policy **policy)
194 {
195 enum lttng_action_status status;
196 const struct lttng_action_notify *notify_action;
197
198 if (!action || !policy || !IS_NOTIFY_ACTION(action)) {
199 status = LTTNG_ACTION_STATUS_INVALID;
200 goto end;
201 }
202
203 notify_action = action_notify_from_action_const(action);
204
205 *policy = notify_action->policy;
206 status = LTTNG_ACTION_STATUS_OK;
207 end:
208 return status;
209 }
This page took 0.054337 seconds and 3 git commands to generate.