Fix: possible null dereference
[lttng-tools.git] / src / common / fd-tracker / fd-tracker.h
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
f7c3ffd7 11#include <common/compat/directory-handle.h>
df038819
JG
12#include <stdint.h>
13#include <sys/types.h>
14
15struct fs_handle;
16struct fd_tracker;
17
18/*
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
21 * may occur.
22 *
23 * The int pointer argument is an output parameter that should be used to return
24 * the advertised number of file descriptors.
25 *
26 * Must return zero on success. Negative values should map to a UNIX error code.
27 */
28typedef int (*fd_open_cb)(void *, int *out_fds);
29
30/*
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.
33 *
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.
37 *
38 * Must return zero on success. Negative values should map to a UNIX error code.
39 */
40typedef int (*fd_close_cb)(void *, int *in_fds);
41
42/*
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
45 * interface.
f7c3ffd7
JG
46 *
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
49 * is held.
df038819 50 */
f7c3ffd7
JG
51struct fd_tracker *fd_tracker_create(const char *unlinked_file_path,
52 unsigned int capacity);
df038819
JG
53
54/* Returns an error if file descriptors are leaked. */
55int fd_tracker_destroy(struct fd_tracker *tracker);
56
57/*
58 * Open a handle to a suspendable filesystem file descriptor.
59 *
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).
63 *
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()).
68 *
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.
72 *
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
77 * open.
78 */
79struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker,
f7c3ffd7 80 struct lttng_directory_handle *directory,
5c1f54d1
JG
81 const char *path,
82 int flags,
83 mode_t *mode);
df038819
JG
84
85/*
86 * Open a tracked unsuspendable file descriptor.
87 *
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.
93 *
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)'.
97 *
98 * Names may be provided to allow easier debugging of file descriptor
99 * exhaustions.
100 *
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.
106 */
107int fd_tracker_open_unsuspendable_fd(struct fd_tracker *tracker,
5c1f54d1
JG
108 int *out_fds,
109 const char **names,
110 unsigned int fd_count,
111 fd_open_cb open,
112 void *data);
df038819
JG
113
114/*
115 * Close a tracked unsuspendable file descriptor.
116 *
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.
120 *
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
125 *
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.
128 */
129int fd_tracker_close_unsuspendable_fd(struct fd_tracker *tracker,
5c1f54d1
JG
130 int *fds,
131 unsigned int fd_count,
132 fd_close_cb close,
df038819
JG
133 void *data);
134
135/*
136 * Log the contents of the fd_tracker.
137 */
138void fd_tracker_log(struct fd_tracker *tracker);
139
140/*
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.
144 *
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.
148 *
149 * Note that a restorable fd should never be held for longer than
150 * strictly necessary (e.g. the duration of a syscall()).
151 *
152 * Returns the fd on success, otherwise a negative value may be returned
153 * if the restoration of the fd failed.
154 */
155int fs_handle_get_fd(struct fs_handle *handle);
156
157/*
158 * Used by the application to signify that it is no longer using the
159 * underlying fd and that it may be suspended.
160 */
161void fs_handle_put_fd(struct fs_handle *handle);
162
9d16fc7f
JG
163/*
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.
168 *
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.
171 *
172 * Returns 0 on success, otherwise a negative value will be returned
173 * if the operation failed.
174 */
175int fs_handle_unlink(struct fs_handle *handle);
176
df038819
JG
177/*
178 * Frees the handle and discards the underlying fd.
179 */
180int fs_handle_close(struct fs_handle *handle);
181
182#endif /* FD_TRACKER_H */
This page took 0.029572 seconds and 4 git commands to generate.