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