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