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 | ||
14ec7e87 JR |
8 | #include <assert.h> |
9 | #include <common/error.h> | |
a58c490f | 10 | #include <lttng/action/action-internal.h> |
0c51e8f3 | 11 | #include <lttng/action/group-internal.h> |
a58c490f | 12 | #include <lttng/action/notify-internal.h> |
bfb2ec6a | 13 | #include <lttng/action/rotate-session-internal.h> |
757c48a2 | 14 | #include <lttng/action/snapshot-session-internal.h> |
58397d0d | 15 | #include <lttng/action/start-session-internal.h> |
931bdbaa | 16 | #include <lttng/action/stop-session-internal.h> |
a58c490f | 17 | |
10615eee JR |
18 | LTTNG_HIDDEN |
19 | const char *lttng_action_type_string(enum lttng_action_type action_type) | |
2666d352 SM |
20 | { |
21 | switch (action_type) { | |
22 | case LTTNG_ACTION_TYPE_UNKNOWN: | |
23 | return "UNKNOWN"; | |
0c51e8f3 SM |
24 | case LTTNG_ACTION_TYPE_GROUP: |
25 | return "GROUP"; | |
2666d352 SM |
26 | case LTTNG_ACTION_TYPE_NOTIFY: |
27 | return "NOTIFY"; | |
bfb2ec6a SM |
28 | case LTTNG_ACTION_TYPE_ROTATE_SESSION: |
29 | return "ROTATE_SESSION"; | |
757c48a2 SM |
30 | case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION: |
31 | return "SNAPSHOT_SESSION"; | |
58397d0d SM |
32 | case LTTNG_ACTION_TYPE_START_SESSION: |
33 | return "START_SESSION"; | |
931bdbaa SM |
34 | case LTTNG_ACTION_TYPE_STOP_SESSION: |
35 | return "STOP_SESSION"; | |
2666d352 SM |
36 | default: |
37 | return "???"; | |
38 | } | |
39 | } | |
40 | ||
17182cfd | 41 | enum lttng_action_type lttng_action_get_type(const struct lttng_action *action) |
a58c490f JG |
42 | { |
43 | return action ? action->type : LTTNG_ACTION_TYPE_UNKNOWN; | |
44 | } | |
45 | ||
6acb3f46 SM |
46 | LTTNG_HIDDEN |
47 | void lttng_action_init( | |
48 | struct lttng_action *action, | |
49 | enum lttng_action_type type, | |
50 | action_validate_cb validate, | |
51 | action_serialize_cb serialize, | |
3dd04a6a | 52 | action_equal_cb equal, |
6acb3f46 SM |
53 | action_destroy_cb destroy) |
54 | { | |
c852ce4e | 55 | urcu_ref_init(&action->ref); |
6acb3f46 SM |
56 | action->type = type; |
57 | action->validate = validate; | |
58 | action->serialize = serialize; | |
3dd04a6a | 59 | action->equal = equal; |
6acb3f46 SM |
60 | action->destroy = destroy; |
61 | } | |
62 | ||
c852ce4e JG |
63 | static |
64 | void action_destroy_ref(struct urcu_ref *ref) | |
65 | { | |
66 | struct lttng_action *action = | |
67 | container_of(ref, struct lttng_action, ref); | |
68 | ||
69 | action->destroy(action); | |
70 | } | |
71 | ||
72 | LTTNG_HIDDEN | |
73 | void lttng_action_get(struct lttng_action *action) | |
74 | { | |
75 | urcu_ref_get(&action->ref); | |
76 | } | |
77 | ||
78 | LTTNG_HIDDEN | |
79 | void lttng_action_put(struct lttng_action *action) | |
a58c490f JG |
80 | { |
81 | if (!action) { | |
82 | return; | |
83 | } | |
84 | ||
85 | assert(action->destroy); | |
c852ce4e JG |
86 | urcu_ref_put(&action->ref, action_destroy_ref); |
87 | } | |
88 | ||
89 | void lttng_action_destroy(struct lttng_action *action) | |
90 | { | |
91 | lttng_action_put(action); | |
a58c490f JG |
92 | } |
93 | ||
94 | LTTNG_HIDDEN | |
95 | bool lttng_action_validate(struct lttng_action *action) | |
96 | { | |
97 | bool valid; | |
98 | ||
99 | if (!action) { | |
100 | valid = false; | |
101 | goto end; | |
102 | } | |
103 | ||
104 | if (!action->validate) { | |
105 | /* Sub-class guarantees that it can never be invalid. */ | |
106 | valid = true; | |
107 | goto end; | |
108 | } | |
109 | ||
110 | valid = action->validate(action); | |
111 | end: | |
112 | return valid; | |
113 | } | |
114 | ||
115 | LTTNG_HIDDEN | |
3647288f | 116 | int lttng_action_serialize(struct lttng_action *action, |
c0a66c84 | 117 | struct lttng_payload *payload) |
a58c490f | 118 | { |
3647288f JG |
119 | int ret; |
120 | struct lttng_action_comm action_comm = { | |
121 | .action_type = (int8_t) action->type, | |
122 | }; | |
123 | ||
c0a66c84 | 124 | ret = lttng_dynamic_buffer_append(&payload->buffer, &action_comm, |
3647288f JG |
125 | sizeof(action_comm)); |
126 | if (ret) { | |
a58c490f JG |
127 | goto end; |
128 | } | |
129 | ||
c0a66c84 | 130 | ret = action->serialize(action, payload); |
3647288f | 131 | if (ret) { |
a58c490f JG |
132 | goto end; |
133 | } | |
a58c490f JG |
134 | end: |
135 | return ret; | |
136 | } | |
137 | ||
138 | LTTNG_HIDDEN | |
c0a66c84 | 139 | ssize_t lttng_action_create_from_payload(struct lttng_payload_view *view, |
869a3c2d | 140 | struct lttng_action **action) |
a58c490f | 141 | { |
869a3c2d | 142 | ssize_t consumed_len, specific_action_consumed_len; |
a58c490f | 143 | const struct lttng_action_comm *action_comm; |
c0a66c84 | 144 | action_create_from_payload_cb create_from_payload_cb; |
a58c490f | 145 | |
869a3c2d SM |
146 | if (!view || !action) { |
147 | consumed_len = -1; | |
a58c490f JG |
148 | goto end; |
149 | } | |
150 | ||
c0a66c84 | 151 | action_comm = (const struct lttng_action_comm *) view->buffer.data; |
869a3c2d | 152 | |
c0a66c84 | 153 | DBG("Create action from payload: action-type=%s", |
2666d352 SM |
154 | lttng_action_type_string(action_comm->action_type)); |
155 | ||
a58c490f JG |
156 | switch (action_comm->action_type) { |
157 | case LTTNG_ACTION_TYPE_NOTIFY: | |
c0a66c84 | 158 | create_from_payload_cb = lttng_action_notify_create_from_payload; |
a58c490f | 159 | break; |
bfb2ec6a | 160 | case LTTNG_ACTION_TYPE_ROTATE_SESSION: |
c0a66c84 JG |
161 | create_from_payload_cb = |
162 | lttng_action_rotate_session_create_from_payload; | |
bfb2ec6a | 163 | break; |
757c48a2 SM |
164 | case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION: |
165 | create_from_payload_cb = | |
166 | lttng_action_snapshot_session_create_from_payload; | |
167 | break; | |
58397d0d | 168 | case LTTNG_ACTION_TYPE_START_SESSION: |
c0a66c84 JG |
169 | create_from_payload_cb = |
170 | lttng_action_start_session_create_from_payload; | |
58397d0d | 171 | break; |
931bdbaa | 172 | case LTTNG_ACTION_TYPE_STOP_SESSION: |
c0a66c84 JG |
173 | create_from_payload_cb = |
174 | lttng_action_stop_session_create_from_payload; | |
931bdbaa | 175 | break; |
0c51e8f3 SM |
176 | case LTTNG_ACTION_TYPE_GROUP: |
177 | create_from_payload_cb = lttng_action_group_create_from_payload; | |
178 | break; | |
a58c490f | 179 | default: |
c0a66c84 | 180 | ERR("Failed to create action from payload, unhandled action type: action-type=%u (%s)", |
2666d352 SM |
181 | action_comm->action_type, |
182 | lttng_action_type_string( | |
183 | action_comm->action_type)); | |
869a3c2d | 184 | consumed_len = -1; |
a58c490f JG |
185 | goto end; |
186 | } | |
187 | ||
c0a66c84 JG |
188 | { |
189 | /* Create buffer view for the action-type-specific data. */ | |
190 | struct lttng_payload_view specific_action_view = | |
191 | lttng_payload_view_from_view(view, | |
192 | sizeof(struct lttng_action_comm), | |
193 | -1); | |
869a3c2d | 194 | |
c0a66c84 JG |
195 | specific_action_consumed_len = create_from_payload_cb( |
196 | &specific_action_view, action); | |
197 | } | |
869a3c2d SM |
198 | if (specific_action_consumed_len < 0) { |
199 | ERR("Failed to create specific action from buffer."); | |
200 | consumed_len = -1; | |
a58c490f JG |
201 | goto end; |
202 | } | |
869a3c2d SM |
203 | |
204 | assert(*action); | |
205 | ||
206 | consumed_len = sizeof(struct lttng_action_comm) + | |
207 | specific_action_consumed_len; | |
208 | ||
a58c490f | 209 | end: |
869a3c2d | 210 | return consumed_len; |
a58c490f | 211 | } |
3dd04a6a JR |
212 | |
213 | LTTNG_HIDDEN | |
214 | bool lttng_action_is_equal(const struct lttng_action *a, | |
215 | const struct lttng_action *b) | |
216 | { | |
217 | bool is_equal = false; | |
218 | ||
219 | if (!a || !b) { | |
220 | goto end; | |
221 | } | |
222 | ||
223 | if (a->type != b->type) { | |
224 | goto end; | |
225 | } | |
226 | ||
227 | if (a == b) { | |
228 | is_equal = true; | |
229 | goto end; | |
230 | } | |
231 | ||
232 | assert(a->equal); | |
233 | is_equal = a->equal(a, b); | |
234 | end: | |
235 | return is_equal; | |
236 | } |