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