actions: Make lttng_action reference countable
[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
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
17static 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
37enum 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
42LTTNG_HIDDEN
43enum lttng_action_type lttng_action_get_type_const(
44 const struct lttng_action *action)
45{
46 return action->type;
47}
48
6acb3f46
SM
49LTTNG_HIDDEN
50void 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{
c852ce4e 58 urcu_ref_init(&action->ref);
6acb3f46
SM
59 action->type = type;
60 action->validate = validate;
61 action->serialize = serialize;
3dd04a6a 62 action->equal = equal;
6acb3f46
SM
63 action->destroy = destroy;
64}
65
c852ce4e
JG
66static
67void action_destroy_ref(struct urcu_ref *ref)
68{
69 struct lttng_action *action =
70 container_of(ref, struct lttng_action, ref);
71
72 action->destroy(action);
73}
74
75LTTNG_HIDDEN
76void lttng_action_get(struct lttng_action *action)
77{
78 urcu_ref_get(&action->ref);
79}
80
81LTTNG_HIDDEN
82void lttng_action_put(struct lttng_action *action)
a58c490f
JG
83{
84 if (!action) {
85 return;
86 }
87
88 assert(action->destroy);
c852ce4e
JG
89 urcu_ref_put(&action->ref, action_destroy_ref);
90}
91
92void lttng_action_destroy(struct lttng_action *action)
93{
94 lttng_action_put(action);
a58c490f
JG
95}
96
97LTTNG_HIDDEN
98bool lttng_action_validate(struct lttng_action *action)
99{
100 bool valid;
101
102 if (!action) {
103 valid = false;
104 goto end;
105 }
106
107 if (!action->validate) {
108 /* Sub-class guarantees that it can never be invalid. */
109 valid = true;
110 goto end;
111 }
112
113 valid = action->validate(action);
114end:
115 return valid;
116}
117
118LTTNG_HIDDEN
3647288f 119int lttng_action_serialize(struct lttng_action *action,
c0a66c84 120 struct lttng_payload *payload)
a58c490f 121{
3647288f
JG
122 int ret;
123 struct lttng_action_comm action_comm = {
124 .action_type = (int8_t) action->type,
125 };
126
c0a66c84 127 ret = lttng_dynamic_buffer_append(&payload->buffer, &action_comm,
3647288f
JG
128 sizeof(action_comm));
129 if (ret) {
a58c490f
JG
130 goto end;
131 }
132
c0a66c84 133 ret = action->serialize(action, payload);
3647288f 134 if (ret) {
a58c490f
JG
135 goto end;
136 }
a58c490f
JG
137end:
138 return ret;
139}
140
141LTTNG_HIDDEN
c0a66c84 142ssize_t lttng_action_create_from_payload(struct lttng_payload_view *view,
869a3c2d 143 struct lttng_action **action)
a58c490f 144{
869a3c2d 145 ssize_t consumed_len, specific_action_consumed_len;
a58c490f 146 const struct lttng_action_comm *action_comm;
c0a66c84 147 action_create_from_payload_cb create_from_payload_cb;
a58c490f 148
869a3c2d
SM
149 if (!view || !action) {
150 consumed_len = -1;
a58c490f
JG
151 goto end;
152 }
153
c0a66c84 154 action_comm = (const struct lttng_action_comm *) view->buffer.data;
869a3c2d 155
c0a66c84 156 DBG("Create action from payload: action-type=%s",
2666d352
SM
157 lttng_action_type_string(action_comm->action_type));
158
a58c490f
JG
159 switch (action_comm->action_type) {
160 case LTTNG_ACTION_TYPE_NOTIFY:
c0a66c84 161 create_from_payload_cb = lttng_action_notify_create_from_payload;
a58c490f 162 break;
bfb2ec6a 163 case LTTNG_ACTION_TYPE_ROTATE_SESSION:
c0a66c84
JG
164 create_from_payload_cb =
165 lttng_action_rotate_session_create_from_payload;
bfb2ec6a 166 break;
757c48a2
SM
167 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION:
168 create_from_payload_cb =
169 lttng_action_snapshot_session_create_from_payload;
170 break;
58397d0d 171 case LTTNG_ACTION_TYPE_START_SESSION:
c0a66c84
JG
172 create_from_payload_cb =
173 lttng_action_start_session_create_from_payload;
58397d0d 174 break;
931bdbaa 175 case LTTNG_ACTION_TYPE_STOP_SESSION:
c0a66c84
JG
176 create_from_payload_cb =
177 lttng_action_stop_session_create_from_payload;
931bdbaa 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 209end:
869a3c2d 210 return consumed_len;
a58c490f 211}
3dd04a6a
JR
212
213LTTNG_HIDDEN
214bool 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);
234end:
235 return is_equal;
236}
This page took 0.042481 seconds and 4 git commands to generate.