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