From: Jérémie Galarneau Date: Fri, 19 Jun 2020 22:43:47 +0000 (-0400) Subject: common: move lttng_payload[_view] to libcommon X-Git-Tag: v2.13.0-rc1~571 X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=commitdiff_plain;h=9e620ea7b016fc3fd4e08b5d094ffa40b36b50f6 common: move lttng_payload[_view] to libcommon The payload utils are moved from libsessiond-comm to libcommon since they present a circular dependancy: a payload uses the dynamic buffer utilities while the actions, triggers, and conditions make use of the lttng_payload utilities. Signed-off-by: Jérémie Galarneau Change-Id: Ib448bde0e442e7660bb7a61cd629f29442715b21 --- diff --git a/include/lttng/action/action-internal.h b/include/lttng/action/action-internal.h index 57035d869..20faea697 100644 --- a/include/lttng/action/action-internal.h +++ b/include/lttng/action/action-internal.h @@ -12,8 +12,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/include/lttng/condition/condition-internal.h b/include/lttng/condition/condition-internal.h index 306272ab8..98a830942 100644 --- a/include/lttng/condition/condition-internal.h +++ b/include/lttng/condition/condition-internal.h @@ -10,8 +10,8 @@ #include #include -#include -#include +#include +#include #include #include #include diff --git a/src/bin/lttng-sessiond/client.c b/src/bin/lttng-sessiond/client.c index 04c04465f..023853850 100644 --- a/src/bin/lttng-sessiond/client.c +++ b/src/bin/lttng-sessiond/client.c @@ -11,8 +11,8 @@ #include "common/compat/socket.h" #include "common/dynamic-buffer.h" #include "common/dynamic-array.h" -#include "common/sessiond-comm/payload.h" -#include "common/sessiond-comm/payload-view.h" +#include "common/payload.h" +#include "common/payload-view.h" #include "common/sessiond-comm/sessiond-comm.h" #include "lttng/lttng-error.h" #include "lttng/tracker.h" diff --git a/src/bin/lttng-sessiond/cmd.c b/src/bin/lttng-sessiond/cmd.c index 200396df6..8b78e627f 100644 --- a/src/bin/lttng-sessiond/cmd.c +++ b/src/bin/lttng-sessiond/cmd.c @@ -7,7 +7,6 @@ */ #include "bin/lttng-sessiond/tracker.h" -#include "common/sessiond-comm/payload.h" #include "lttng/lttng-error.h" #include "lttng/tracker.h" #define _LGPL_SOURCE @@ -27,8 +26,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/src/bin/lttng-sessiond/lttng-sessiond.h b/src/bin/lttng-sessiond/lttng-sessiond.h index 6c83b6a34..edc1bc115 100644 --- a/src/bin/lttng-sessiond/lttng-sessiond.h +++ b/src/bin/lttng-sessiond/lttng-sessiond.h @@ -13,7 +13,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/common/Makefile.am b/src/common/Makefile.am index 32b28c82f..d071bdfdf 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -55,6 +55,8 @@ libcommon_la_SOURCES = \ mi-lttng.c mi-lttng.h \ notification.c \ optional.h \ + payload.c payload.h \ + payload-view.c payload-view.h \ pipe.c pipe.h \ readwrite.c readwrite.h \ runas.c runas.h \ diff --git a/src/common/actions/group.c b/src/common/actions/group.c index 5931eb38b..1a434616f 100644 --- a/src/common/actions/group.c +++ b/src/common/actions/group.c @@ -7,8 +7,8 @@ #include #include -#include -#include +#include +#include #include #include #include diff --git a/src/common/actions/snapshot-session.c b/src/common/actions/snapshot-session.c index 058179633..667166770 100644 --- a/src/common/actions/snapshot-session.c +++ b/src/common/actions/snapshot-session.c @@ -9,8 +9,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/src/common/notification.c b/src/common/notification.c index 806c55619..2544d042f 100644 --- a/src/common/notification.c +++ b/src/common/notification.c @@ -10,8 +10,8 @@ #include #include #include -#include -#include +#include +#include #include LTTNG_HIDDEN diff --git a/src/common/payload-view.c b/src/common/payload-view.c new file mode 100644 index 000000000..fdfef6150 --- /dev/null +++ b/src/common/payload-view.c @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2020 Jérémie Galarneau + * + * SPDX-License-Identifier: LGPL-2.1-only + * + */ + +#include +#include "payload-view.h" +#include "payload.h" +#include + +LTTNG_HIDDEN +struct lttng_payload_view lttng_payload_view_from_payload( + const struct lttng_payload *payload, size_t offset, + ptrdiff_t len) +{ + return (struct lttng_payload_view) { + .buffer = lttng_buffer_view_from_dynamic_buffer( + &payload->buffer, offset, len), + ._fds = payload->_fds, + }; +} + +LTTNG_HIDDEN +struct lttng_payload_view lttng_payload_view_from_view( + struct lttng_payload_view *view, size_t offset, + ptrdiff_t len) +{ + return (struct lttng_payload_view) { + .buffer = lttng_buffer_view_from_view( + &view->buffer, offset, len), + ._fds = view->_fds, + ._iterator.p_fds_position = &view->_iterator.fds_position, + }; +} + +LTTNG_HIDDEN +struct lttng_payload_view lttng_payload_view_from_dynamic_buffer( + const struct lttng_dynamic_buffer *buffer, size_t offset, + ptrdiff_t len) +{ + return (struct lttng_payload_view) { + .buffer = lttng_buffer_view_from_dynamic_buffer( + buffer, offset, len) + }; +} + +LTTNG_HIDDEN +int lttng_payload_view_pop_fd(struct lttng_payload_view *view) +{ + int ret = 0; + size_t fd_count; + size_t *pos; + + if (!view) { + ret = -1; + goto end; + } + + fd_count = lttng_dynamic_array_get_count(&view->_fds); + pos = view->_iterator.p_fds_position ? view->_iterator.p_fds_position : + &view->_iterator.fds_position; + + if (*pos >= fd_count) { + ret = -1; + goto end; + } + + ret = *((int *) lttng_dynamic_array_get_element( + &view->_fds, *pos)); + (*pos)++; +end: + return ret; +} diff --git a/src/common/payload-view.h b/src/common/payload-view.h new file mode 100644 index 000000000..f125e8890 --- /dev/null +++ b/src/common/payload-view.h @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2020 Jérémie Galarneau + * + * SPDX-License-Identifier: LGPL-2.1-only + * + */ + +#ifndef LTTNG_PAYLOAD_VIEW_H +#define LTTNG_PAYLOAD_VIEW_H + +#include +#include + +struct lttng_payload; + +/* + * An lttng_payload_view references a payload and allows code to share + * a `const` version of a subset of a payload. + * + * A payload view is invalidated whenever its source (a payload, or another + * payload view) is modified. + * + * While a payload view does not allow users to modify the underlying bytes + * of the payload, it can be used to 'pop' file descriptors using an iterator + * belonging to the top-level payload view. + * + * Hence, a payload view created from a payload or a dynamic buffer contains + * an implicit file descriptor iterator. Any payload view created from another + * payload view will share the same underlying file descriptor iterator. + * + * The rationale for this is that a payload is never consumed directly, it + * must be consumed through a payload view. + * + * Typically, a payload view will be used to rebuild a previously serialized + * object hierarchy. Sharing an underlying iterator allows aggregate objects + * to provide a restricted view of the payload to their members, which will + * report the number of bytes consumed and `pop` the file descriptors they + * should own. In return, those objects can create an even narrower view for + * their children, allowing them to also consume file descriptors. + * + * Note that a payload view never assumes any ownership of the underlying + * payload. + */ +struct lttng_payload_view { + struct lttng_buffer_view buffer; + /* private */ + const struct lttng_dynamic_array _fds; + struct { + size_t *p_fds_position; + size_t fds_position; + } _iterator; +}; + +/** + * Return a payload view referencing a subset of a payload. + * + * @payload Source payload to reference + * @offset Offset to apply to the payload's buffer + * @len Length of the contents to reference. Passing -1 will + * cause the view to reference the whole payload from the + * offset provided. + */ +LTTNG_HIDDEN +struct lttng_payload_view lttng_payload_view_from_payload( + const struct lttng_payload *payload, size_t offset, + ptrdiff_t len); + +/** + * Return a payload view referencing a subset of a payload referenced by + * another payload view. + * + * @view Source payload view to reference + * @offset Offset to apply to the payload view's buffer view + * @len Length of the contents to reference. Passing -1 will + * cause the payload view to reference the whole payload view's + * buffer view from the offset provided. + */ +LTTNG_HIDDEN +struct lttng_payload_view lttng_payload_view_from_view( + struct lttng_payload_view *view, size_t offset, + ptrdiff_t len); + +/** + * Return a payload view referencing a subset of a dynamic buffer. + * + * Meant as an adapter for code paths that need to create a payload view + * from an existing dynamic buffer. + * + * @src Source dynamic buffer to reference + * @offset Offset to apply to the payload's buffer + * @len Length of the buffer contents to reference. Passing -1 will + * cause the payload view to reference the whole payload from the + * offset provided. + */ +LTTNG_HIDDEN +struct lttng_payload_view lttng_payload_view_from_dynamic_buffer( + const struct lttng_dynamic_buffer *buffer, size_t offset, + ptrdiff_t len); + +/** + * Pop an fd from a payload view. + * No ownership of the file descriptor is assumed by the payload. + * + * @payload Payload instance + * + * Returns a file descriptor on success, -1 on error. + */ +LTTNG_HIDDEN +int lttng_payload_view_pop_fd(struct lttng_payload_view *payload_view); + +#endif /* LTTNG_PAYLOAD_VIEW_H */ diff --git a/src/common/payload.c b/src/common/payload.c new file mode 100644 index 000000000..592964a51 --- /dev/null +++ b/src/common/payload.c @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2020 Jérémie Galarneau + * + * SPDX-License-Identifier: LGPL-2.1-only + * + */ + +#include "payload.h" + +LTTNG_HIDDEN +void lttng_payload_init(struct lttng_payload *payload) +{ + assert(payload); + lttng_dynamic_buffer_init(&payload->buffer); + lttng_dynamic_array_init(&payload->_fds, sizeof(int), NULL); +} + +LTTNG_HIDDEN +void lttng_payload_reset(struct lttng_payload *payload) +{ + if (!payload) { + return; + } + + lttng_dynamic_buffer_reset(&payload->buffer); + lttng_dynamic_array_reset(&payload->_fds); +} + +LTTNG_HIDDEN +int lttng_payload_push_fd(struct lttng_payload *payload, int fd) +{ + int ret; + + if (!payload) { + ret = -1; + goto end; + } + + ret = lttng_dynamic_array_add_element(&payload->_fds, &fd); +end: + return ret; +} diff --git a/src/common/payload.h b/src/common/payload.h new file mode 100644 index 000000000..3f1aa8cfd --- /dev/null +++ b/src/common/payload.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2020 Jérémie Galarneau + * + * SPDX-License-Identifier: LGPL-2.1-only + * + */ + +#ifndef LTTNG_PAYLOAD_H +#define LTTNG_PAYLOAD_H + +#include +#include + +/* + * An lttng_payload encompasses the 'data' (bytes) and any passed file + * descriptors as part of a message between liblttng-ctl and the session + * daemon. + */ +struct lttng_payload { + struct lttng_dynamic_buffer buffer; + /* private */ + struct lttng_dynamic_array _fds; +}; + +/* + * Initialize a payload. This performs no allocation and is meant + * to be used instead. + */ +LTTNG_HIDDEN +void lttng_payload_init(struct lttng_payload *payload); + +/* Release any memory used by the payload. */ +LTTNG_HIDDEN +void lttng_payload_reset(struct lttng_payload *payload); + +/** + * Add an fd to the payload. + * No ownership of the file descriptor is assumed by the payload. + * + * @payload Payload instance + * @fd File descriptor to add to the payload + * + * Returns 0 on success, -1 on allocation error. + */ +LTTNG_HIDDEN +int lttng_payload_push_fd(struct lttng_payload *payload, int fd); + +#endif /* LTTNG_PAYLOAD_H */ diff --git a/src/common/sessiond-comm/Makefile.am b/src/common/sessiond-comm/Makefile.am index 0e7eb4922..d026651a6 100644 --- a/src/common/sessiond-comm/Makefile.am +++ b/src/common/sessiond-comm/Makefile.am @@ -4,5 +4,4 @@ noinst_LTLIBRARIES = libsessiond-comm.la libsessiond_comm_la_SOURCES = sessiond-comm.c sessiond-comm.h \ inet.c inet.h inet6.c inet6.h \ - relayd.h agent.h payload.h \ - payload.c payload-view.h payload-view.c + relayd.h agent.h diff --git a/src/common/sessiond-comm/payload-view.c b/src/common/sessiond-comm/payload-view.c deleted file mode 100644 index fdfef6150..000000000 --- a/src/common/sessiond-comm/payload-view.c +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (C) 2020 Jérémie Galarneau - * - * SPDX-License-Identifier: LGPL-2.1-only - * - */ - -#include -#include "payload-view.h" -#include "payload.h" -#include - -LTTNG_HIDDEN -struct lttng_payload_view lttng_payload_view_from_payload( - const struct lttng_payload *payload, size_t offset, - ptrdiff_t len) -{ - return (struct lttng_payload_view) { - .buffer = lttng_buffer_view_from_dynamic_buffer( - &payload->buffer, offset, len), - ._fds = payload->_fds, - }; -} - -LTTNG_HIDDEN -struct lttng_payload_view lttng_payload_view_from_view( - struct lttng_payload_view *view, size_t offset, - ptrdiff_t len) -{ - return (struct lttng_payload_view) { - .buffer = lttng_buffer_view_from_view( - &view->buffer, offset, len), - ._fds = view->_fds, - ._iterator.p_fds_position = &view->_iterator.fds_position, - }; -} - -LTTNG_HIDDEN -struct lttng_payload_view lttng_payload_view_from_dynamic_buffer( - const struct lttng_dynamic_buffer *buffer, size_t offset, - ptrdiff_t len) -{ - return (struct lttng_payload_view) { - .buffer = lttng_buffer_view_from_dynamic_buffer( - buffer, offset, len) - }; -} - -LTTNG_HIDDEN -int lttng_payload_view_pop_fd(struct lttng_payload_view *view) -{ - int ret = 0; - size_t fd_count; - size_t *pos; - - if (!view) { - ret = -1; - goto end; - } - - fd_count = lttng_dynamic_array_get_count(&view->_fds); - pos = view->_iterator.p_fds_position ? view->_iterator.p_fds_position : - &view->_iterator.fds_position; - - if (*pos >= fd_count) { - ret = -1; - goto end; - } - - ret = *((int *) lttng_dynamic_array_get_element( - &view->_fds, *pos)); - (*pos)++; -end: - return ret; -} diff --git a/src/common/sessiond-comm/payload-view.h b/src/common/sessiond-comm/payload-view.h deleted file mode 100644 index f125e8890..000000000 --- a/src/common/sessiond-comm/payload-view.h +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright (C) 2020 Jérémie Galarneau - * - * SPDX-License-Identifier: LGPL-2.1-only - * - */ - -#ifndef LTTNG_PAYLOAD_VIEW_H -#define LTTNG_PAYLOAD_VIEW_H - -#include -#include - -struct lttng_payload; - -/* - * An lttng_payload_view references a payload and allows code to share - * a `const` version of a subset of a payload. - * - * A payload view is invalidated whenever its source (a payload, or another - * payload view) is modified. - * - * While a payload view does not allow users to modify the underlying bytes - * of the payload, it can be used to 'pop' file descriptors using an iterator - * belonging to the top-level payload view. - * - * Hence, a payload view created from a payload or a dynamic buffer contains - * an implicit file descriptor iterator. Any payload view created from another - * payload view will share the same underlying file descriptor iterator. - * - * The rationale for this is that a payload is never consumed directly, it - * must be consumed through a payload view. - * - * Typically, a payload view will be used to rebuild a previously serialized - * object hierarchy. Sharing an underlying iterator allows aggregate objects - * to provide a restricted view of the payload to their members, which will - * report the number of bytes consumed and `pop` the file descriptors they - * should own. In return, those objects can create an even narrower view for - * their children, allowing them to also consume file descriptors. - * - * Note that a payload view never assumes any ownership of the underlying - * payload. - */ -struct lttng_payload_view { - struct lttng_buffer_view buffer; - /* private */ - const struct lttng_dynamic_array _fds; - struct { - size_t *p_fds_position; - size_t fds_position; - } _iterator; -}; - -/** - * Return a payload view referencing a subset of a payload. - * - * @payload Source payload to reference - * @offset Offset to apply to the payload's buffer - * @len Length of the contents to reference. Passing -1 will - * cause the view to reference the whole payload from the - * offset provided. - */ -LTTNG_HIDDEN -struct lttng_payload_view lttng_payload_view_from_payload( - const struct lttng_payload *payload, size_t offset, - ptrdiff_t len); - -/** - * Return a payload view referencing a subset of a payload referenced by - * another payload view. - * - * @view Source payload view to reference - * @offset Offset to apply to the payload view's buffer view - * @len Length of the contents to reference. Passing -1 will - * cause the payload view to reference the whole payload view's - * buffer view from the offset provided. - */ -LTTNG_HIDDEN -struct lttng_payload_view lttng_payload_view_from_view( - struct lttng_payload_view *view, size_t offset, - ptrdiff_t len); - -/** - * Return a payload view referencing a subset of a dynamic buffer. - * - * Meant as an adapter for code paths that need to create a payload view - * from an existing dynamic buffer. - * - * @src Source dynamic buffer to reference - * @offset Offset to apply to the payload's buffer - * @len Length of the buffer contents to reference. Passing -1 will - * cause the payload view to reference the whole payload from the - * offset provided. - */ -LTTNG_HIDDEN -struct lttng_payload_view lttng_payload_view_from_dynamic_buffer( - const struct lttng_dynamic_buffer *buffer, size_t offset, - ptrdiff_t len); - -/** - * Pop an fd from a payload view. - * No ownership of the file descriptor is assumed by the payload. - * - * @payload Payload instance - * - * Returns a file descriptor on success, -1 on error. - */ -LTTNG_HIDDEN -int lttng_payload_view_pop_fd(struct lttng_payload_view *payload_view); - -#endif /* LTTNG_PAYLOAD_VIEW_H */ diff --git a/src/common/sessiond-comm/payload.c b/src/common/sessiond-comm/payload.c deleted file mode 100644 index 592964a51..000000000 --- a/src/common/sessiond-comm/payload.c +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) 2020 Jérémie Galarneau - * - * SPDX-License-Identifier: LGPL-2.1-only - * - */ - -#include "payload.h" - -LTTNG_HIDDEN -void lttng_payload_init(struct lttng_payload *payload) -{ - assert(payload); - lttng_dynamic_buffer_init(&payload->buffer); - lttng_dynamic_array_init(&payload->_fds, sizeof(int), NULL); -} - -LTTNG_HIDDEN -void lttng_payload_reset(struct lttng_payload *payload) -{ - if (!payload) { - return; - } - - lttng_dynamic_buffer_reset(&payload->buffer); - lttng_dynamic_array_reset(&payload->_fds); -} - -LTTNG_HIDDEN -int lttng_payload_push_fd(struct lttng_payload *payload, int fd) -{ - int ret; - - if (!payload) { - ret = -1; - goto end; - } - - ret = lttng_dynamic_array_add_element(&payload->_fds, &fd); -end: - return ret; -} diff --git a/src/common/sessiond-comm/payload.h b/src/common/sessiond-comm/payload.h deleted file mode 100644 index 3f1aa8cfd..000000000 --- a/src/common/sessiond-comm/payload.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (C) 2020 Jérémie Galarneau - * - * SPDX-License-Identifier: LGPL-2.1-only - * - */ - -#ifndef LTTNG_PAYLOAD_H -#define LTTNG_PAYLOAD_H - -#include -#include - -/* - * An lttng_payload encompasses the 'data' (bytes) and any passed file - * descriptors as part of a message between liblttng-ctl and the session - * daemon. - */ -struct lttng_payload { - struct lttng_dynamic_buffer buffer; - /* private */ - struct lttng_dynamic_array _fds; -}; - -/* - * Initialize a payload. This performs no allocation and is meant - * to be used instead. - */ -LTTNG_HIDDEN -void lttng_payload_init(struct lttng_payload *payload); - -/* Release any memory used by the payload. */ -LTTNG_HIDDEN -void lttng_payload_reset(struct lttng_payload *payload); - -/** - * Add an fd to the payload. - * No ownership of the file descriptor is assumed by the payload. - * - * @payload Payload instance - * @fd File descriptor to add to the payload - * - * Returns 0 on success, -1 on allocation error. - */ -LTTNG_HIDDEN -int lttng_payload_push_fd(struct lttng_payload *payload, int fd); - -#endif /* LTTNG_PAYLOAD_H */ diff --git a/src/common/snapshot.c b/src/common/snapshot.c index c2efcc02b..b0bebc81d 100644 --- a/src/common/snapshot.c +++ b/src/common/snapshot.c @@ -5,8 +5,8 @@ * */ -#include -#include +#include +#include #include #include #include diff --git a/src/common/trigger.c b/src/common/trigger.c index 57f84d6a6..5ff478a46 100644 --- a/src/common/trigger.c +++ b/src/common/trigger.c @@ -8,8 +8,8 @@ #include #include #include -#include -#include +#include +#include #include #include diff --git a/src/common/userspace-probe.c b/src/common/userspace-probe.c index 8c6adaca6..e6c987c23 100644 --- a/src/common/userspace-probe.c +++ b/src/common/userspace-probe.c @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include #include #include diff --git a/src/lib/lttng-ctl/lttng-ctl.c b/src/lib/lttng-ctl/lttng-ctl.c index b74c78749..d1b2fdfd9 100644 --- a/src/lib/lttng-ctl/lttng-ctl.c +++ b/src/lib/lttng-ctl/lttng-ctl.c @@ -23,8 +23,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/tests/unit/test_payload.c b/tests/unit/test_payload.c index 50f08cd75..11c57f21d 100644 --- a/tests/unit/test_payload.c +++ b/tests/unit/test_payload.c @@ -5,8 +5,8 @@ * */ -#include -#include +#include +#include #include static const int TEST_COUNT = 5;