982ca45ff27619f3c52d1ba6a15e937ae4b2b647
[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 void lttng_action_destroy(struct lttng_action *action)
26 {
27 if (!action) {
28 return;
29 }
30
31 assert(action->destroy);
32 action->destroy(action);
33 }
34
35 LTTNG_HIDDEN
36 bool lttng_action_validate(struct lttng_action *action)
37 {
38 bool valid;
39
40 if (!action) {
41 valid = false;
42 goto end;
43 }
44
45 if (!action->validate) {
46 /* Sub-class guarantees that it can never be invalid. */
47 valid = true;
48 goto end;
49 }
50
51 valid = action->validate(action);
52 end:
53 return valid;
54 }
55
56 LTTNG_HIDDEN
57 int lttng_action_serialize(struct lttng_action *action,
58 struct lttng_dynamic_buffer *buf)
59 {
60 int ret;
61 struct lttng_action_comm action_comm = {
62 .action_type = (int8_t) action->type,
63 };
64
65 ret = lttng_dynamic_buffer_append(buf, &action_comm,
66 sizeof(action_comm));
67 if (ret) {
68 goto end;
69 }
70
71 ret = action->serialize(action, buf);
72 if (ret) {
73 goto end;
74 }
75 end:
76 return ret;
77 }
78
79 LTTNG_HIDDEN
80 ssize_t lttng_action_create_from_buffer(const struct lttng_buffer_view *view,
81 struct lttng_action **action)
82 {
83 ssize_t consumed_len, specific_action_consumed_len;
84 const struct lttng_action_comm *action_comm;
85 action_create_from_buffer_cb create_from_buffer_cb;
86 struct lttng_buffer_view specific_action_view;
87
88 if (!view || !action) {
89 consumed_len = -1;
90 goto end;
91 }
92
93 action_comm = (const struct lttng_action_comm *) view->data;
94
95 DBG("Deserializing action from buffer");
96 switch (action_comm->action_type) {
97 case LTTNG_ACTION_TYPE_NOTIFY:
98 create_from_buffer_cb = lttng_action_notify_create_from_buffer;
99 break;
100 default:
101 consumed_len = -1;
102 goto end;
103 }
104
105 /* Create buffer view for the action-type-specific data. */
106 specific_action_view = lttng_buffer_view_from_view(view,
107 sizeof(struct lttng_action_comm),
108 view->size - sizeof(struct lttng_action_comm));
109
110 specific_action_consumed_len =
111 create_from_buffer_cb(&specific_action_view, action);
112 if (specific_action_consumed_len < 0) {
113 ERR("Failed to create specific action from buffer.");
114 consumed_len = -1;
115 goto end;
116 }
117
118 assert(*action);
119
120 consumed_len = sizeof(struct lttng_action_comm) +
121 specific_action_consumed_len;
122
123 end:
124 return consumed_len;
125 }
This page took 0.030917 seconds and 4 git commands to generate.