actions: introduce start session action
[lttng-tools.git] / src / common / actions / action.c
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <assert.h>
9 #include <common/error.h>
10 #include <lttng/action/action-internal.h>
11 #include <lttng/action/notify-internal.h>
12 #include <lttng/action/start-session-internal.h>
13
14 static 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";
21 case LTTNG_ACTION_TYPE_START_SESSION:
22 return "START_SESSION";
23 default:
24 return "???";
25 }
26 }
27
28 enum lttng_action_type lttng_action_get_type(struct lttng_action *action)
29 {
30 return action ? action->type : LTTNG_ACTION_TYPE_UNKNOWN;
31 }
32
33 LTTNG_HIDDEN
34 enum lttng_action_type lttng_action_get_type_const(
35 const struct lttng_action *action)
36 {
37 return action->type;
38 }
39
40 LTTNG_HIDDEN
41 void lttng_action_init(
42 struct lttng_action *action,
43 enum lttng_action_type type,
44 action_validate_cb validate,
45 action_serialize_cb serialize,
46 action_equal_cb equal,
47 action_destroy_cb destroy)
48 {
49 action->type = type;
50 action->validate = validate;
51 action->serialize = serialize;
52 action->equal = equal;
53 action->destroy = destroy;
54 }
55
56 void 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
66 LTTNG_HIDDEN
67 bool 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);
83 end:
84 return valid;
85 }
86
87 LTTNG_HIDDEN
88 int lttng_action_serialize(struct lttng_action *action,
89 struct lttng_dynamic_buffer *buf)
90 {
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) {
99 goto end;
100 }
101
102 ret = action->serialize(action, buf);
103 if (ret) {
104 goto end;
105 }
106 end:
107 return ret;
108 }
109
110 LTTNG_HIDDEN
111 ssize_t lttng_action_create_from_buffer(const struct lttng_buffer_view *view,
112 struct lttng_action **action)
113 {
114 ssize_t consumed_len, specific_action_consumed_len;
115 const struct lttng_action_comm *action_comm;
116 action_create_from_buffer_cb create_from_buffer_cb;
117 struct lttng_buffer_view specific_action_view;
118
119 if (!view || !action) {
120 consumed_len = -1;
121 goto end;
122 }
123
124 action_comm = (const struct lttng_action_comm *) view->data;
125
126 DBG("Create action from buffer: action-type=%s",
127 lttng_action_type_string(action_comm->action_type));
128
129 switch (action_comm->action_type) {
130 case LTTNG_ACTION_TYPE_NOTIFY:
131 create_from_buffer_cb = lttng_action_notify_create_from_buffer;
132 break;
133 case LTTNG_ACTION_TYPE_START_SESSION:
134 create_from_buffer_cb =
135 lttng_action_start_session_create_from_buffer;
136 break;
137 default:
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));
142 consumed_len = -1;
143 goto end;
144 }
145
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;
156 goto end;
157 }
158
159 assert(*action);
160
161 consumed_len = sizeof(struct lttng_action_comm) +
162 specific_action_consumed_len;
163
164 end:
165 return consumed_len;
166 }
167
168 LTTNG_HIDDEN
169 bool 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);
189 end:
190 return is_equal;
191 }
This page took 0.034035 seconds and 5 git commands to generate.