Various fixes for prototype branch
[lttng-tools.git] / src / common / notification.c
1 /*
2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
7 *
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11 * for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 #include <lttng/notification/notification-internal.h>
19 #include <lttng/condition/condition-internal.h>
20 #include <lttng/condition/evaluation-internal.h>
21 #include <lttng/condition/condition.h>
22 #include <lttng/condition/evaluation.h>
23 #include <assert.h>
24
25 LTTNG_HIDDEN
26 struct lttng_notification *lttng_notification_create(
27 struct lttng_condition *condition,
28 struct lttng_evaluation *evaluation)
29 {
30 struct lttng_notification *notification = NULL;
31
32 if (!condition || !evaluation) {
33 goto end;
34 }
35
36 notification = zmalloc(sizeof(struct lttng_notification));
37 if (!notification) {
38 goto end;
39 }
40
41 notification->condition = condition;
42 notification->evaluation = evaluation;
43 end:
44 return notification;
45 }
46
47 LTTNG_HIDDEN
48 ssize_t lttng_notification_serialize(struct lttng_notification *notification,
49 char *buf)
50 {
51 ssize_t ret, condition_size, evaluation_size, offset = 0;
52 struct lttng_notification_comm notification_comm;
53
54 if (!notification) {
55 ret = -1;
56 goto end;
57 }
58
59 offset += sizeof(notification_comm);
60 condition_size = lttng_condition_serialize(notification->condition,
61 buf ? (buf + offset) : NULL);
62 if (condition_size < 0) {
63 ret = condition_size;
64 goto end;
65 }
66 offset += condition_size;
67
68 evaluation_size = lttng_evaluation_serialize(notification->evaluation,
69 buf ? (buf + offset) : NULL);
70 if (evaluation_size < 0) {
71 ret = evaluation_size;
72 goto end;
73 }
74 offset += evaluation_size;
75
76 if (buf) {
77 notification_comm.length =
78 (uint32_t) (condition_size + evaluation_size);
79 memcpy(buf, &notification_comm, sizeof(notification_comm));
80 }
81 ret = offset;
82 end:
83 return ret;
84
85 }
86
87 LTTNG_HIDDEN
88 ssize_t lttng_notification_create_from_buffer(const char *buf,
89 struct lttng_notification **notification)
90 {
91 ssize_t ret, offset = 0, condition_size, evaluation_size;
92 struct lttng_notification_comm *notification_comm;
93 struct lttng_condition *condition;
94 struct lttng_evaluation *evaluation;
95
96 if (!buf || !notification) {
97 ret = -1;
98 goto end;
99 }
100
101 notification_comm = (struct lttng_notification_comm *) buf;
102 offset += sizeof(*notification_comm);
103
104 /* struct lttng_condition */
105 condition_size = lttng_condition_create_from_buffer(buf + offset,
106 &condition);
107 if (condition_size < 0) {
108 ret = condition_size;
109 goto end;
110 }
111 offset += condition_size;
112
113 /* struct lttng_evaluation */
114 evaluation_size = lttng_evaluation_create_from_buffer(buf + offset,
115 &evaluation);
116 if (evaluation_size < 0) {
117 ret = evaluation_size;
118 goto end;
119 }
120 offset += evaluation_size;
121
122 /* Unexpected size of inner-elements; the buffer is corrupted. */
123 if ((ssize_t) notification_comm->length !=
124 condition_size + evaluation_size) {
125 ret = -1;
126 goto error;
127 }
128
129 *notification = lttng_notification_create(condition, evaluation);
130 if (!*notification) {
131 goto error;
132 }
133 ret = offset;
134 end:
135 return ret;
136 error:
137 lttng_condition_destroy(condition);
138 lttng_evaluation_destroy(evaluation);
139 return ret;
140 }
141
142 void lttng_notification_destroy(struct lttng_notification *notification)
143 {
144 if (!notification) {
145 return;
146 }
147
148 lttng_condition_destroy(notification->condition);
149 lttng_evaluation_destroy(notification->evaluation);
150 free(notification);
151 }
152
153 struct lttng_condition *lttng_notification_get_condition(
154 struct lttng_notification *notification)
155 {
156 return notification ? notification->condition : NULL;
157 }
158
159 struct lttng_evaluation *lttng_notification_get_evaluation(
160 struct lttng_notification *notification)
161 {
162 return notification ? notification->evaluation : NULL;
163 }
This page took 0.034489 seconds and 5 git commands to generate.