2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/compat/directory-handle.h>
13 #include <sys/types.h>
19 * Callback which returns a file descriptor to track through the fd
20 * tracker. This callback must not make use of the fd_tracker as a deadlock
23 * The int pointer argument is an output parameter that should be used to return
24 * the advertised number of file descriptors.
26 * Must return zero on success. Negative values should map to a UNIX error code.
28 typedef int (*fd_open_cb
)(void *, int *out_fds
);
31 * Callback to allow the user to close a now-untracked file descriptor. This
32 * callback must not make use of the fd_tracker as a deadlock may occur.
34 * The callback can freely modify the in_fds argument as it is copied by the
35 * fd_tracker before being used. The fd tracker assumes in_fds to be closed by
36 * the time the callback returns.
38 * Must return zero on success. Negative values should map to a UNIX error code.
40 typedef int (*fd_close_cb
)(void *, int *in_fds
);
43 * Set the maximal number of fds that the process should be allowed to open at
44 * any given time. This function must be called before any other of this
47 * The unlinked_file_path is an absolute path (which does not need to exist)
48 * under which unlinked files will be stored for as long as a reference to them
51 struct fd_tracker
*fd_tracker_create(const char *unlinked_file_path
,
52 unsigned int capacity
);
54 /* Returns an error if file descriptors are leaked. */
55 int fd_tracker_destroy(struct fd_tracker
*tracker
);
58 * Open a handle to a suspendable filesystem file descriptor.
60 * See OPEN(3) for an explanation of flags and mode. NULL is returned in case of
61 * error and errno is left untouched. Note that passing NULL as mode will result
62 * in open()'s default behaviour being used (using the process' umask).
64 * A fs_handle wraps a file descriptor created by OPEN(3). It is suspendable
65 * meaning that the underlying file may be closed at any time unless the
66 * handle is marked as being in-use (see fs_handle_get_fd() and
67 * fs_handle_put_fd()).
69 * If the tracker opted to close the underlying file descriptor, it will
70 * be restored to its last known state when it is obtained through
71 * the fs_handle's fs_handle_get_fd() method.
73 * Note that a suspendable file descriptor can be closed by the fd tracker at
74 * anytime when it is not in use. This means that the user should not rely on it
75 * being safe to unlink the file. Moreover, concurent modifications to the file
76 * (e.g. truncation) may react differently than if the file descriptor was kept
79 struct fs_handle
*fd_tracker_open_fs_handle(struct fd_tracker
*tracker
,
80 struct lttng_directory_handle
*directory
,
86 * Open a tracked unsuspendable file descriptor.
88 * This function allows the fd tracker to keep track of unsuspendable
89 * file descriptors. A callback, open, is passed to allow the tracker
90 * to atomically reserve an entry for a given count of new file descriptors,
91 * suspending file descriptors as needed, and invoke the provided callback
92 * without ever exceeding the tracker's capacity.
94 * fd_count indicates the count of file descriptors that will be opened and
95 * returned by the open callback. The storage location at out_fds is assumed
96 * to be large enough to hold 'fd_count * sizeof(int)'.
98 * Names may be provided to allow easier debugging of file descriptor
101 * The callback's return value is returned to the user. Additionally, two
102 * negative tracker-specific codes may be returned:
103 * - ENOMEM: allocation of a new entry failed,
104 * - EMFILE: too many unsuspendable fds are opened and the tracker can't
105 * accomodate the request for a new unsuspendable entry.
107 int fd_tracker_open_unsuspendable_fd(struct fd_tracker
*tracker
,
110 unsigned int fd_count
,
115 * Close a tracked unsuspendable file descriptor.
117 * This function allows the fd tracker to keep track of unsuspendable
118 * file descriptors. A callback, close, is passed to allow the tracker
119 * to atomically release a file descriptor entry.
121 * Returns 0 if the close callback returned success. Returns the value returned
122 * by the close callback if it is negative. Additionally, a tracker-specific
123 * code may be returned:
124 * - EINVAL: a file descriptor was unknown to the tracker
126 * Closed fds are set to -1 in the fds array which, in the event of an error,
127 * allows the user to know which file descriptors are no longer being tracked.
129 int fd_tracker_close_unsuspendable_fd(struct fd_tracker
*tracker
,
131 unsigned int fd_count
,
136 * Log the contents of the fd_tracker.
138 void fd_tracker_log(struct fd_tracker
*tracker
);
141 * Marks the handle as the most recently used and marks the 'fd' as
142 * "in-use". This prevents the tracker from recycling the underlying
143 * file descriptor while it is actively being used by a thread.
145 * Don't forget that the tracker may be initiating an fd 'suspension'
146 * from another thread as the need to free an fd slot may arise from any
147 * thread within the daemon.
149 * Note that a restorable fd should never be held for longer than
150 * strictly necessary (e.g. the duration of a syscall()).
152 * Returns the fd on success, otherwise a negative value may be returned
153 * if the restoration of the fd failed.
155 int fs_handle_get_fd(struct fs_handle
*handle
);
158 * Used by the application to signify that it is no longer using the
159 * underlying fd and that it may be suspended.
161 void fs_handle_put_fd(struct fs_handle
*handle
);
164 * Unlink the file associated to an fs_handle. Note that the unlink
165 * operation will not be performed immediately. It will only be performed
166 * once all references to the underlying file (through other fs_handle objects)
167 * have been released.
169 * However, note that the file will be renamed so as to provide the observable
170 * effect of an unlink(), that is removing a name from the filesystem.
172 * Returns 0 on success, otherwise a negative value will be returned
173 * if the operation failed.
175 int fs_handle_unlink(struct fs_handle
*handle
);
178 * Frees the handle and discards the underlying fd.
180 int fs_handle_close(struct fs_handle
*handle
);
182 #endif /* FD_TRACKER_H */
This page took 0.0409 seconds and 4 git commands to generate.