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, |
7f4d5b07 | 106 | lttng_action_notify_internal_get_rate_policy); |
1c2c1c05 JR |
107 | |
108 | notify->policy = policy; | |
109 | policy = NULL; | |
110 | ||
111 | action = ¬ify->parent; | |
112 | notify = NULL; | |
113 | ||
a58c490f | 114 | end: |
1c2c1c05 | 115 | free(notify); |
7f4d5b07 | 116 | lttng_rate_policy_destroy(policy); |
1c2c1c05 | 117 | return action; |
a58c490f | 118 | } |
869a3c2d | 119 | |
c0a66c84 JG |
120 | ssize_t lttng_action_notify_create_from_payload( |
121 | struct lttng_payload_view *view, | |
869a3c2d SM |
122 | struct lttng_action **action) |
123 | { | |
1c2c1c05 | 124 | enum lttng_action_status status; |
869a3c2d | 125 | ssize_t consumed_length; |
7f4d5b07 | 126 | struct lttng_rate_policy *rate_policy = NULL; |
1c2c1c05 | 127 | struct lttng_action *_action = NULL; |
869a3c2d | 128 | |
7f4d5b07 JR |
129 | consumed_length = lttng_rate_policy_create_from_payload( |
130 | view, &rate_policy); | |
131 | if (!rate_policy) { | |
869a3c2d SM |
132 | consumed_length = -1; |
133 | goto end; | |
134 | } | |
135 | ||
1c2c1c05 JR |
136 | _action = lttng_action_notify_create(); |
137 | if (!_action) { | |
138 | consumed_length = -1; | |
139 | goto end; | |
140 | } | |
141 | ||
7f4d5b07 | 142 | status = lttng_action_notify_set_rate_policy(_action, rate_policy); |
1c2c1c05 JR |
143 | if (status != LTTNG_ACTION_STATUS_OK) { |
144 | consumed_length = -1; | |
145 | goto end; | |
146 | } | |
147 | ||
148 | *action = _action; | |
149 | _action = NULL; | |
150 | ||
869a3c2d | 151 | end: |
7f4d5b07 | 152 | lttng_rate_policy_destroy(rate_policy); |
1c2c1c05 | 153 | lttng_action_destroy(_action); |
869a3c2d SM |
154 | return consumed_length; |
155 | } | |
1c2c1c05 | 156 | |
7f4d5b07 | 157 | enum lttng_action_status lttng_action_notify_set_rate_policy( |
1c2c1c05 | 158 | struct lttng_action *action, |
7f4d5b07 | 159 | const struct lttng_rate_policy *policy) |
1c2c1c05 JR |
160 | { |
161 | enum lttng_action_status status; | |
162 | struct lttng_action_notify *notify_action; | |
7f4d5b07 | 163 | struct lttng_rate_policy *copy = NULL; |
1c2c1c05 JR |
164 | |
165 | if (!action || !policy || !IS_NOTIFY_ACTION(action)) { | |
166 | status = LTTNG_ACTION_STATUS_INVALID; | |
167 | goto end; | |
168 | } | |
169 | ||
7f4d5b07 | 170 | copy = lttng_rate_policy_copy(policy); |
1c2c1c05 JR |
171 | if (!copy) { |
172 | status = LTTNG_ACTION_STATUS_ERROR; | |
173 | goto end; | |
174 | } | |
175 | ||
176 | notify_action = action_notify_from_action(action); | |
177 | ||
7f4d5b07 JR |
178 | /* Free the previous rate policy .*/ |
179 | lttng_rate_policy_destroy(notify_action->policy); | |
1c2c1c05 JR |
180 | |
181 | /* Assign the policy. */ | |
182 | notify_action->policy = copy; | |
183 | status = LTTNG_ACTION_STATUS_OK; | |
184 | copy = NULL; | |
185 | ||
186 | end: | |
7f4d5b07 | 187 | lttng_rate_policy_destroy(copy); |
1c2c1c05 JR |
188 | return status; |
189 | } | |
190 | ||
7f4d5b07 | 191 | enum lttng_action_status lttng_action_notify_get_rate_policy( |
1c2c1c05 | 192 | const struct lttng_action *action, |
7f4d5b07 | 193 | const struct lttng_rate_policy **policy) |
1c2c1c05 JR |
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 | } |