actions: introduce lttng_action_init
[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 <lttng/action/action-internal.h>
9 #include <lttng/action/notify-internal.h>
10 #include <common/error.h>
11 #include <assert.h>
12
13 enum lttng_action_type lttng_action_get_type(struct lttng_action *action)
14 {
15 return action ? action->type : LTTNG_ACTION_TYPE_UNKNOWN;
16 }
17
18 LTTNG_HIDDEN
19 enum lttng_action_type lttng_action_get_type_const(
20 const struct lttng_action *action)
21 {
22 return action->type;
23 }
24
25 LTTNG_HIDDEN
26 void 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
39 void 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
49 LTTNG_HIDDEN
50 bool 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);
66 end:
67 return valid;
68 }
69
70 LTTNG_HIDDEN
71 int lttng_action_serialize(struct lttng_action *action,
72 struct lttng_dynamic_buffer *buf)
73 {
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) {
82 goto end;
83 }
84
85 ret = action->serialize(action, buf);
86 if (ret) {
87 goto end;
88 }
89 end:
90 return ret;
91 }
92
93 LTTNG_HIDDEN
94 ssize_t lttng_action_create_from_buffer(const struct lttng_buffer_view *view,
95 struct lttng_action **action)
96 {
97 ssize_t consumed_len, specific_action_consumed_len;
98 const struct lttng_action_comm *action_comm;
99 action_create_from_buffer_cb create_from_buffer_cb;
100 struct lttng_buffer_view specific_action_view;
101
102 if (!view || !action) {
103 consumed_len = -1;
104 goto end;
105 }
106
107 action_comm = (const struct lttng_action_comm *) view->data;
108
109 DBG("Deserializing action from buffer");
110 switch (action_comm->action_type) {
111 case LTTNG_ACTION_TYPE_NOTIFY:
112 create_from_buffer_cb = lttng_action_notify_create_from_buffer;
113 break;
114 default:
115 consumed_len = -1;
116 goto end;
117 }
118
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;
129 goto end;
130 }
131
132 assert(*action);
133
134 consumed_len = sizeof(struct lttng_action_comm) +
135 specific_action_consumed_len;
136
137 end:
138 return consumed_len;
139 }
This page took 0.032452 seconds and 5 git commands to generate.