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