X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Ffd-handle.c;fp=src%2Fcommon%2Ffd-handle.c;h=a9c7be4a938ab86aa44d3c6316c3ccd8a04e6537;hp=0000000000000000000000000000000000000000;hb=dd1bac00c933616842aaf51ad4c4240ccee98838;hpb=38f2de9d8209cd72db7aa37f274be287a654d8db diff --git a/src/common/fd-handle.c b/src/common/fd-handle.c new file mode 100644 index 000000000..a9c7be4a9 --- /dev/null +++ b/src/common/fd-handle.c @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2020 Jérémie Galarneau + * + * SPDX-License-Identifier: LGPL-2.1-only + * + */ + +#include +#include + +#include "fd-handle.h" +#include + +struct fd_handle { + struct urcu_ref ref; + int fd; +}; + +static void fd_handle_release(struct urcu_ref *ref) +{ + int ret; + struct fd_handle *handle = container_of(ref, struct fd_handle, ref); + + assert(handle->fd >= 0); + ret = close(handle->fd); + if (ret == -1) { + PERROR("Failed to close file descriptor of fd_handle upon release: fd = %d", + handle->fd); + } + + free(handle); +} + +LTTNG_HIDDEN +struct fd_handle *fd_handle_create(int fd) +{ + struct fd_handle *handle = NULL; + + if (fd < 0) { + ERR("Attempted to create an fd_handle from an invalid file descriptor: fd = %d", + fd); + goto end; + } + + handle = zmalloc(sizeof(*handle)); + if (!handle) { + PERROR("Failed to allocate fd_handle"); + goto end; + } + + urcu_ref_init(&handle->ref); + handle->fd = fd; + +end: + return handle; +} + +LTTNG_HIDDEN +void fd_handle_get(struct fd_handle *handle) +{ + assert(handle); + urcu_ref_get(&handle->ref); +} + +LTTNG_HIDDEN +void fd_handle_put(struct fd_handle *handle) +{ + assert(handle); + urcu_ref_put(&handle->ref, fd_handle_release); +} + +LTTNG_HIDDEN +int fd_handle_get_fd(struct fd_handle *handle) +{ + assert(handle); + return handle->fd; +} + +LTTNG_HIDDEN +struct fd_handle *fd_handle_copy(const struct fd_handle *handle) +{ + struct fd_handle *new_handle = NULL; + const int new_fd = dup(handle->fd); + + if (new_fd < 0) { + PERROR("Failed to duplicate file descriptor while copying fd_handle: fd = %d", handle->fd); + goto end; + } + + new_handle = fd_handle_create(new_fd); +end: + return new_handle; +}