fix: relayd: unaligned access in trace_chunk_registry_ht_key_hash
[lttng-tools.git] / src / common / fd-tracker / fd-tracker.hpp
CommitLineData
df038819 1/*
ab5be9fa 2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
df038819 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
df038819 5 *
df038819
JG
6 */
7
8#ifndef FD_TRACKER_H
9#define FD_TRACKER_H
10
c9e313bc
SM
11#include <common/compat/directory-handle.hpp>
12#include <common/macros.hpp>
28f23191 13
df038819
JG
14#include <stdint.h>
15#include <sys/types.h>
16
17struct fs_handle;
18struct fd_tracker;
19
20/*
21 * Callback which returns a file descriptor to track through the fd
22 * tracker. This callback must not make use of the fd_tracker as a deadlock
23 * may occur.
24 *
25 * The int pointer argument is an output parameter that should be used to return
26 * the advertised number of file descriptors.
27 *
28 * Must return zero on success. Negative values should map to a UNIX error code.
29 */
e665dfbc 30using fd_open_cb = int (*)(void *, int *);
df038819
JG
31
32/*
33 * Callback to allow the user to close a now-untracked file descriptor. This
34 * callback must not make use of the fd_tracker as a deadlock may occur.
35 *
36 * The callback can freely modify the in_fds argument as it is copied by the
37 * fd_tracker before being used. The fd tracker assumes in_fds to be closed by
38 * the time the callback returns.
39 *
40 * Must return zero on success. Negative values should map to a UNIX error code.
41 */
e665dfbc 42using fd_close_cb = int (*)(void *, int *);
df038819
JG
43
44/*
45 * Set the maximal number of fds that the process should be allowed to open at
46 * any given time. This function must be called before any other of this
47 * interface.
f7c3ffd7
JG
48 *
49 * The unlinked_file_path is an absolute path (which does not need to exist)
50 * under which unlinked files will be stored for as long as a reference to them
51 * is held.
df038819 52 */
28f23191 53struct fd_tracker *fd_tracker_create(const char *unlinked_file_path, unsigned int capacity);
df038819
JG
54
55/* Returns an error if file descriptors are leaked. */
56int fd_tracker_destroy(struct fd_tracker *tracker);
57
58/*
59 * Open a handle to a suspendable filesystem file descriptor.
60 *
61 * See OPEN(3) for an explanation of flags and mode. NULL is returned in case of
62 * error and errno is left untouched. Note that passing NULL as mode will result
63 * in open()'s default behaviour being used (using the process' umask).
64 *
65 * A fs_handle wraps a file descriptor created by OPEN(3). It is suspendable
66 * meaning that the underlying file may be closed at any time unless the
67 * handle is marked as being in-use (see fs_handle_get_fd() and
68 * fs_handle_put_fd()).
69 *
70 * If the tracker opted to close the underlying file descriptor, it will
71 * be restored to its last known state when it is obtained through
72 * the fs_handle's fs_handle_get_fd() method.
73 *
74 * Note that a suspendable file descriptor can be closed by the fd tracker at
75 * anytime when it is not in use. This means that the user should not rely on it
76 * being safe to unlink the file. Moreover, concurent modifications to the file
77 * (e.g. truncation) may react differently than if the file descriptor was kept
78 * open.
79 */
80struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker,
28f23191
JG
81 struct lttng_directory_handle *directory,
82 const char *path,
83 int flags,
84 mode_t *mode);
df038819
JG
85
86/*
87 * Open a tracked unsuspendable file descriptor.
88 *
89 * This function allows the fd tracker to keep track of unsuspendable
90 * file descriptors. A callback, open, is passed to allow the tracker
91 * to atomically reserve an entry for a given count of new file descriptors,
92 * suspending file descriptors as needed, and invoke the provided callback
93 * without ever exceeding the tracker's capacity.
94 *
95 * fd_count indicates the count of file descriptors that will be opened and
96 * returned by the open callback. The storage location at out_fds is assumed
97 * to be large enough to hold 'fd_count * sizeof(int)'.
98 *
99 * Names may be provided to allow easier debugging of file descriptor
100 * exhaustions.
101 *
102 * The callback's return value is returned to the user. Additionally, two
103 * negative tracker-specific codes may be returned:
104 * - ENOMEM: allocation of a new entry failed,
105 * - EMFILE: too many unsuspendable fds are opened and the tracker can't
d49487dc 106 * accommodates the request for a new unsuspendable entry.
df038819
JG
107 */
108int fd_tracker_open_unsuspendable_fd(struct fd_tracker *tracker,
28f23191
JG
109 int *out_fds,
110 const char **names,
111 unsigned int fd_count,
112 fd_open_cb open,
113 void *data);
df038819
JG
114
115/*
116 * Close a tracked unsuspendable file descriptor.
117 *
118 * This function allows the fd tracker to keep track of unsuspendable
119 * file descriptors. A callback, close, is passed to allow the tracker
120 * to atomically release a file descriptor entry.
121 *
122 * Returns 0 if the close callback returned success. Returns the value returned
123 * by the close callback if it is negative. Additionally, a tracker-specific
124 * code may be returned:
125 * - EINVAL: a file descriptor was unknown to the tracker
126 *
127 * Closed fds are set to -1 in the fds array which, in the event of an error,
128 * allows the user to know which file descriptors are no longer being tracked.
129 */
28f23191
JG
130int fd_tracker_close_unsuspendable_fd(
131 struct fd_tracker *tracker, int *fds, unsigned int fd_count, fd_close_cb close, void *data);
df038819
JG
132
133/*
134 * Log the contents of the fd_tracker.
135 */
136void fd_tracker_log(struct fd_tracker *tracker);
137
df038819 138#endif /* FD_TRACKER_H */
This page took 0.063531 seconds and 4 git commands to generate.