fd-tracker: add the unlink operation to fs handles
[lttng-tools.git] / src / common / fd-tracker / fd-tracker.h
CommitLineData
df038819
JG
1/*
2 * Copyright (C) 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18#ifndef FD_TRACKER_H
19#define FD_TRACKER_H
20
21#include <stdint.h>
22#include <sys/types.h>
23
24struct fs_handle;
25struct fd_tracker;
26
27/*
28 * Callback which returns a file descriptor to track through the fd
29 * tracker. This callback must not make use of the fd_tracker as a deadlock
30 * may occur.
31 *
32 * The int pointer argument is an output parameter that should be used to return
33 * the advertised number of file descriptors.
34 *
35 * Must return zero on success. Negative values should map to a UNIX error code.
36 */
37typedef int (*fd_open_cb)(void *, int *out_fds);
38
39/*
40 * Callback to allow the user to close a now-untracked file descriptor. This
41 * callback must not make use of the fd_tracker as a deadlock may occur.
42 *
43 * The callback can freely modify the in_fds argument as it is copied by the
44 * fd_tracker before being used. The fd tracker assumes in_fds to be closed by
45 * the time the callback returns.
46 *
47 * Must return zero on success. Negative values should map to a UNIX error code.
48 */
49typedef int (*fd_close_cb)(void *, int *in_fds);
50
51/*
52 * Set the maximal number of fds that the process should be allowed to open at
53 * any given time. This function must be called before any other of this
54 * interface.
55 */
56struct fd_tracker *fd_tracker_create(unsigned int capacity);
57
58/* Returns an error if file descriptors are leaked. */
59int fd_tracker_destroy(struct fd_tracker *tracker);
60
61/*
62 * Open a handle to a suspendable filesystem file descriptor.
63 *
64 * See OPEN(3) for an explanation of flags and mode. NULL is returned in case of
65 * error and errno is left untouched. Note that passing NULL as mode will result
66 * in open()'s default behaviour being used (using the process' umask).
67 *
68 * A fs_handle wraps a file descriptor created by OPEN(3). It is suspendable
69 * meaning that the underlying file may be closed at any time unless the
70 * handle is marked as being in-use (see fs_handle_get_fd() and
71 * fs_handle_put_fd()).
72 *
73 * If the tracker opted to close the underlying file descriptor, it will
74 * be restored to its last known state when it is obtained through
75 * the fs_handle's fs_handle_get_fd() method.
76 *
77 * Note that a suspendable file descriptor can be closed by the fd tracker at
78 * anytime when it is not in use. This means that the user should not rely on it
79 * being safe to unlink the file. Moreover, concurent modifications to the file
80 * (e.g. truncation) may react differently than if the file descriptor was kept
81 * open.
82 */
83struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker,
84 const char *path, int flags, mode_t *mode);
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
106 * accomodate the request for a new unsuspendable entry.
107 */
108int fd_tracker_open_unsuspendable_fd(struct fd_tracker *tracker,
109 int *out_fds, const char **names, unsigned int fd_count,
110 fd_open_cb open, void *data);
111
112/*
113 * Close a tracked unsuspendable file descriptor.
114 *
115 * This function allows the fd tracker to keep track of unsuspendable
116 * file descriptors. A callback, close, is passed to allow the tracker
117 * to atomically release a file descriptor entry.
118 *
119 * Returns 0 if the close callback returned success. Returns the value returned
120 * by the close callback if it is negative. Additionally, a tracker-specific
121 * code may be returned:
122 * - EINVAL: a file descriptor was unknown to the tracker
123 *
124 * Closed fds are set to -1 in the fds array which, in the event of an error,
125 * allows the user to know which file descriptors are no longer being tracked.
126 */
127int fd_tracker_close_unsuspendable_fd(struct fd_tracker *tracker,
128 int *fds, unsigned int fd_count, fd_close_cb close,
129 void *data);
130
131/*
132 * Log the contents of the fd_tracker.
133 */
134void fd_tracker_log(struct fd_tracker *tracker);
135
136/*
137 * Marks the handle as the most recently used and marks the 'fd' as
138 * "in-use". This prevents the tracker from recycling the underlying
139 * file descriptor while it is actively being used by a thread.
140 *
141 * Don't forget that the tracker may be initiating an fd 'suspension'
142 * from another thread as the need to free an fd slot may arise from any
143 * thread within the daemon.
144 *
145 * Note that a restorable fd should never be held for longer than
146 * strictly necessary (e.g. the duration of a syscall()).
147 *
148 * Returns the fd on success, otherwise a negative value may be returned
149 * if the restoration of the fd failed.
150 */
151int fs_handle_get_fd(struct fs_handle *handle);
152
153/*
154 * Used by the application to signify that it is no longer using the
155 * underlying fd and that it may be suspended.
156 */
157void fs_handle_put_fd(struct fs_handle *handle);
158
9d16fc7f
JG
159/*
160 * Unlink the file associated to an fs_handle. Note that the unlink
161 * operation will not be performed immediately. It will only be performed
162 * once all references to the underlying file (through other fs_handle objects)
163 * have been released.
164 *
165 * However, note that the file will be renamed so as to provide the observable
166 * effect of an unlink(), that is removing a name from the filesystem.
167 *
168 * Returns 0 on success, otherwise a negative value will be returned
169 * if the operation failed.
170 */
171int fs_handle_unlink(struct fs_handle *handle);
172
df038819
JG
173/*
174 * Frees the handle and discards the underlying fd.
175 */
176int fs_handle_close(struct fs_handle *handle);
177
178#endif /* FD_TRACKER_H */
This page took 0.028505 seconds and 4 git commands to generate.