2 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
11 #include "fd-handle.h"
12 #include <common/error.h>
19 static void fd_handle_release(struct urcu_ref
*ref
)
22 struct fd_handle
*handle
= container_of(ref
, struct fd_handle
, ref
);
24 assert(handle
->fd
>= 0);
25 ret
= close(handle
->fd
);
27 PERROR("Failed to close file descriptor of fd_handle upon release: fd = %d",
35 struct fd_handle
*fd_handle_create(int fd
)
37 struct fd_handle
*handle
= NULL
;
40 ERR("Attempted to create an fd_handle from an invalid file descriptor: fd = %d",
45 handle
= zmalloc(sizeof(*handle
));
47 PERROR("Failed to allocate fd_handle");
51 urcu_ref_init(&handle
->ref
);
59 void fd_handle_get(struct fd_handle
*handle
)
65 urcu_ref_get(&handle
->ref
);
69 void fd_handle_put(struct fd_handle
*handle
)
75 urcu_ref_put(&handle
->ref
, fd_handle_release
);
79 int fd_handle_get_fd(struct fd_handle
*handle
)
86 struct fd_handle
*fd_handle_copy(const struct fd_handle
*handle
)
88 struct fd_handle
*new_handle
= NULL
;
89 const int new_fd
= dup(handle
->fd
);
92 PERROR("Failed to duplicate file descriptor while copying fd_handle: fd = %d", handle
->fd
);
96 new_handle
= fd_handle_create(new_fd
);
This page took 0.034314 seconds and 5 git commands to generate.