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