Commit | Line | Data |
---|---|---|
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 JG |
11 | #include <lttng/action/action-internal.h> |
12 | #include <lttng/action/notify-internal.h> | |
7f4d5b07 | 13 | #include <lttng/action/rate-policy-internal.h> |
1c2c1c05 JR |
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 | } | |
a58c490f JG |
33 | |
34 | static | |
35 | void 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); | |
7f4d5b07 | 39 | lttng_rate_policy_destroy(notify_action->policy); |
1c2c1c05 | 40 | free(notify_action); |
a58c490f JG |
41 | } |
42 | ||
43 | static | |
3647288f | 44 | int 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); | |
7f4d5b07 JR |
58 | DBG("Serializing notify action rate policy"); |
59 | ret = lttng_rate_policy_serialize(notify_action->policy, payload); | |
1c2c1c05 JR |
60 | |
61 | end: | |
62 | return ret; | |
a58c490f JG |
63 | } |
64 | ||
3dd04a6a JR |
65 | static |
66 | bool 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); | |
7f4d5b07 | 73 | return lttng_rate_policy_is_equal(_a->policy, _b->policy); |
3dd04a6a JR |
74 | } |
75 | ||
7f4d5b07 JR |
76 | static const struct lttng_rate_policy * |
77 | lttng_action_notify_internal_get_rate_policy(const struct lttng_action *action) | |
2d57482c JR |
78 | { |
79 | const struct lttng_action_notify *_action; | |
80 | _action = action_notify_from_action_const(action); | |
81 | ||
82 | return _action->policy; | |
83 | } | |
84 | ||
a58c490f JG |
85 | struct lttng_action *lttng_action_notify_create(void) |
86 | { | |
7f4d5b07 | 87 | struct lttng_rate_policy *policy = NULL; |
1c2c1c05 JR |
88 | struct lttng_action_notify *notify = NULL; |
89 | struct lttng_action *action = NULL; | |
a58c490f JG |
90 | |
91 | notify = zmalloc(sizeof(struct lttng_action_notify)); | |
92 | if (!notify) { | |
93 | goto end; | |
94 | } | |
95 | ||
1c2c1c05 | 96 | /* Default policy. */ |
7f4d5b07 | 97 | policy = lttng_rate_policy_every_n_create(1); |
1c2c1c05 JR |
98 | if (!policy) { |
99 | goto end; | |
100 | } | |
101 | ||
6acb3f46 SM |
102 | lttng_action_init(¬ify->parent, LTTNG_ACTION_TYPE_NOTIFY, NULL, |
103 | lttng_action_notify_serialize, | |
3dd04a6a | 104 | lttng_action_notify_is_equal, |
2d57482c | 105 | lttng_action_notify_destroy, |
588c4b0d JG |
106 | lttng_action_notify_internal_get_rate_policy, |
107 | lttng_action_generic_add_error_query_results); | |
1c2c1c05 JR |
108 | |
109 | notify->policy = policy; | |
110 | policy = NULL; | |
111 | ||
112 | action = ¬ify->parent; | |
113 | notify = NULL; | |
114 | ||
a58c490f | 115 | end: |
1c2c1c05 | 116 | free(notify); |
7f4d5b07 | 117 | lttng_rate_policy_destroy(policy); |
1c2c1c05 | 118 | return action; |
a58c490f | 119 | } |
869a3c2d | 120 | |
c0a66c84 JG |
121 | ssize_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; |
7f4d5b07 | 127 | struct lttng_rate_policy *rate_policy = NULL; |
1c2c1c05 | 128 | struct lttng_action *_action = NULL; |
869a3c2d | 129 | |
7f4d5b07 JR |
130 | consumed_length = lttng_rate_policy_create_from_payload( |
131 | view, &rate_policy); | |
132 | if (!rate_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 | ||
7f4d5b07 | 143 | status = lttng_action_notify_set_rate_policy(_action, rate_policy); |
1c2c1c05 JR |
144 | if (status != LTTNG_ACTION_STATUS_OK) { |
145 | consumed_length = -1; | |
146 | goto end; | |
147 | } | |
148 | ||
149 | *action = _action; | |
150 | _action = NULL; | |
151 | ||
869a3c2d | 152 | end: |
7f4d5b07 | 153 | lttng_rate_policy_destroy(rate_policy); |
1c2c1c05 | 154 | lttng_action_destroy(_action); |
869a3c2d SM |
155 | return consumed_length; |
156 | } | |
1c2c1c05 | 157 | |
7f4d5b07 | 158 | enum lttng_action_status lttng_action_notify_set_rate_policy( |
1c2c1c05 | 159 | struct lttng_action *action, |
7f4d5b07 | 160 | const struct lttng_rate_policy *policy) |
1c2c1c05 JR |
161 | { |
162 | enum lttng_action_status status; | |
163 | struct lttng_action_notify *notify_action; | |
7f4d5b07 | 164 | struct lttng_rate_policy *copy = NULL; |
1c2c1c05 JR |
165 | |
166 | if (!action || !policy || !IS_NOTIFY_ACTION(action)) { | |
167 | status = LTTNG_ACTION_STATUS_INVALID; | |
168 | goto end; | |
169 | } | |
170 | ||
7f4d5b07 | 171 | copy = lttng_rate_policy_copy(policy); |
1c2c1c05 JR |
172 | if (!copy) { |
173 | status = LTTNG_ACTION_STATUS_ERROR; | |
174 | goto end; | |
175 | } | |
176 | ||
177 | notify_action = action_notify_from_action(action); | |
178 | ||
7f4d5b07 JR |
179 | /* Free the previous rate policy .*/ |
180 | lttng_rate_policy_destroy(notify_action->policy); | |
1c2c1c05 JR |
181 | |
182 | /* Assign the policy. */ | |
183 | notify_action->policy = copy; | |
184 | status = LTTNG_ACTION_STATUS_OK; | |
185 | copy = NULL; | |
186 | ||
187 | end: | |
7f4d5b07 | 188 | lttng_rate_policy_destroy(copy); |
1c2c1c05 JR |
189 | return status; |
190 | } | |
191 | ||
7f4d5b07 | 192 | enum lttng_action_status lttng_action_notify_get_rate_policy( |
1c2c1c05 | 193 | const struct lttng_action *action, |
7f4d5b07 | 194 | const struct lttng_rate_policy **policy) |
1c2c1c05 JR |
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 | } |