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