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