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>
12 #include <common/macros.h>
14 #include <sys/types.h>
20 * Callback which returns a file descriptor to track through the fd
21 * tracker. This callback must not make use of the fd_tracker as a deadlock
24 * The int pointer argument is an output parameter that should be used to return
25 * the advertised number of file descriptors.
27 * Must return zero on success. Negative values should map to a UNIX error code.
29 typedef int (*fd_open_cb
)(void *, int *out_fds
);
32 * Callback to allow the user to close a now-untracked file descriptor. This
33 * callback must not make use of the fd_tracker as a deadlock may occur.
35 * The callback can freely modify the in_fds argument as it is copied by the
36 * fd_tracker before being used. The fd tracker assumes in_fds to be closed by
37 * the time the callback returns.
39 * Must return zero on success. Negative values should map to a UNIX error code.
41 typedef int (*fd_close_cb
)(void *, int *in_fds
);
44 * Set the maximal number of fds that the process should be allowed to open at
45 * any given time. This function must be called before any other of this
48 * The unlinked_file_path is an absolute path (which does not need to exist)
49 * under which unlinked files will be stored for as long as a reference to them
52 struct fd_tracker
*fd_tracker_create(const char *unlinked_file_path
,
53 unsigned int capacity
);
55 /* Returns an error if file descriptors are leaked. */
56 int fd_tracker_destroy(struct fd_tracker
*tracker
);
59 * Open a handle to a suspendable filesystem file descriptor.
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).
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()).
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.
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
80 struct fs_handle
*fd_tracker_open_fs_handle(struct fd_tracker
*tracker
,
81 struct lttng_directory_handle
*directory
,
87 * Open a tracked unsuspendable file descriptor.
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.
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)'.
99 * Names may be provided to allow easier debugging of file descriptor
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
106 * accommodates the request for a new unsuspendable entry.
108 int fd_tracker_open_unsuspendable_fd(struct fd_tracker
*tracker
,
111 unsigned int fd_count
,
116 * Close a tracked unsuspendable file descriptor.
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.
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
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.
130 int fd_tracker_close_unsuspendable_fd(struct fd_tracker
*tracker
,
132 unsigned int fd_count
,
137 * Log the contents of the fd_tracker.
139 void fd_tracker_log(struct fd_tracker
*tracker
);
141 #endif /* FD_TRACKER_H */
This page took 0.031531 seconds and 4 git commands to generate.