Commit | Line | Data |
---|---|---|
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 | ||
f7c3ffd7 | 21 | #include <common/compat/directory-handle.h> |
df038819 JG |
22 | #include <stdint.h> |
23 | #include <sys/types.h> | |
24 | ||
25 | struct fs_handle; | |
26 | struct fd_tracker; | |
27 | ||
28 | /* | |
29 | * Callback which returns a file descriptor to track through the fd | |
30 | * tracker. This callback must not make use of the fd_tracker as a deadlock | |
31 | * may occur. | |
32 | * | |
33 | * The int pointer argument is an output parameter that should be used to return | |
34 | * the advertised number of file descriptors. | |
35 | * | |
36 | * Must return zero on success. Negative values should map to a UNIX error code. | |
37 | */ | |
38 | typedef int (*fd_open_cb)(void *, int *out_fds); | |
39 | ||
40 | /* | |
41 | * Callback to allow the user to close a now-untracked file descriptor. This | |
42 | * callback must not make use of the fd_tracker as a deadlock may occur. | |
43 | * | |
44 | * The callback can freely modify the in_fds argument as it is copied by the | |
45 | * fd_tracker before being used. The fd tracker assumes in_fds to be closed by | |
46 | * the time the callback returns. | |
47 | * | |
48 | * Must return zero on success. Negative values should map to a UNIX error code. | |
49 | */ | |
50 | typedef int (*fd_close_cb)(void *, int *in_fds); | |
51 | ||
52 | /* | |
53 | * Set the maximal number of fds that the process should be allowed to open at | |
54 | * any given time. This function must be called before any other of this | |
55 | * interface. | |
f7c3ffd7 JG |
56 | * |
57 | * The unlinked_file_path is an absolute path (which does not need to exist) | |
58 | * under which unlinked files will be stored for as long as a reference to them | |
59 | * is held. | |
df038819 | 60 | */ |
f7c3ffd7 JG |
61 | struct fd_tracker *fd_tracker_create(const char *unlinked_file_path, |
62 | unsigned int capacity); | |
df038819 JG |
63 | |
64 | /* Returns an error if file descriptors are leaked. */ | |
65 | int fd_tracker_destroy(struct fd_tracker *tracker); | |
66 | ||
67 | /* | |
68 | * Open a handle to a suspendable filesystem file descriptor. | |
69 | * | |
70 | * See OPEN(3) for an explanation of flags and mode. NULL is returned in case of | |
71 | * error and errno is left untouched. Note that passing NULL as mode will result | |
72 | * in open()'s default behaviour being used (using the process' umask). | |
73 | * | |
74 | * A fs_handle wraps a file descriptor created by OPEN(3). It is suspendable | |
75 | * meaning that the underlying file may be closed at any time unless the | |
76 | * handle is marked as being in-use (see fs_handle_get_fd() and | |
77 | * fs_handle_put_fd()). | |
78 | * | |
79 | * If the tracker opted to close the underlying file descriptor, it will | |
80 | * be restored to its last known state when it is obtained through | |
81 | * the fs_handle's fs_handle_get_fd() method. | |
82 | * | |
83 | * Note that a suspendable file descriptor can be closed by the fd tracker at | |
84 | * anytime when it is not in use. This means that the user should not rely on it | |
85 | * being safe to unlink the file. Moreover, concurent modifications to the file | |
86 | * (e.g. truncation) may react differently than if the file descriptor was kept | |
87 | * open. | |
88 | */ | |
89 | struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker, | |
f7c3ffd7 | 90 | struct lttng_directory_handle *directory, |
5c1f54d1 JG |
91 | const char *path, |
92 | int flags, | |
93 | mode_t *mode); | |
df038819 JG |
94 | |
95 | /* | |
96 | * Open a tracked unsuspendable file descriptor. | |
97 | * | |
98 | * This function allows the fd tracker to keep track of unsuspendable | |
99 | * file descriptors. A callback, open, is passed to allow the tracker | |
100 | * to atomically reserve an entry for a given count of new file descriptors, | |
101 | * suspending file descriptors as needed, and invoke the provided callback | |
102 | * without ever exceeding the tracker's capacity. | |
103 | * | |
104 | * fd_count indicates the count of file descriptors that will be opened and | |
105 | * returned by the open callback. The storage location at out_fds is assumed | |
106 | * to be large enough to hold 'fd_count * sizeof(int)'. | |
107 | * | |
108 | * Names may be provided to allow easier debugging of file descriptor | |
109 | * exhaustions. | |
110 | * | |
111 | * The callback's return value is returned to the user. Additionally, two | |
112 | * negative tracker-specific codes may be returned: | |
113 | * - ENOMEM: allocation of a new entry failed, | |
114 | * - EMFILE: too many unsuspendable fds are opened and the tracker can't | |
115 | * accomodate the request for a new unsuspendable entry. | |
116 | */ | |
117 | int fd_tracker_open_unsuspendable_fd(struct fd_tracker *tracker, | |
5c1f54d1 JG |
118 | int *out_fds, |
119 | const char **names, | |
120 | unsigned int fd_count, | |
121 | fd_open_cb open, | |
122 | void *data); | |
df038819 JG |
123 | |
124 | /* | |
125 | * Close a tracked unsuspendable file descriptor. | |
126 | * | |
127 | * This function allows the fd tracker to keep track of unsuspendable | |
128 | * file descriptors. A callback, close, is passed to allow the tracker | |
129 | * to atomically release a file descriptor entry. | |
130 | * | |
131 | * Returns 0 if the close callback returned success. Returns the value returned | |
132 | * by the close callback if it is negative. Additionally, a tracker-specific | |
133 | * code may be returned: | |
134 | * - EINVAL: a file descriptor was unknown to the tracker | |
135 | * | |
136 | * Closed fds are set to -1 in the fds array which, in the event of an error, | |
137 | * allows the user to know which file descriptors are no longer being tracked. | |
138 | */ | |
139 | int fd_tracker_close_unsuspendable_fd(struct fd_tracker *tracker, | |
5c1f54d1 JG |
140 | int *fds, |
141 | unsigned int fd_count, | |
142 | fd_close_cb close, | |
df038819 JG |
143 | void *data); |
144 | ||
145 | /* | |
146 | * Log the contents of the fd_tracker. | |
147 | */ | |
148 | void fd_tracker_log(struct fd_tracker *tracker); | |
149 | ||
150 | /* | |
151 | * Marks the handle as the most recently used and marks the 'fd' as | |
152 | * "in-use". This prevents the tracker from recycling the underlying | |
153 | * file descriptor while it is actively being used by a thread. | |
154 | * | |
155 | * Don't forget that the tracker may be initiating an fd 'suspension' | |
156 | * from another thread as the need to free an fd slot may arise from any | |
157 | * thread within the daemon. | |
158 | * | |
159 | * Note that a restorable fd should never be held for longer than | |
160 | * strictly necessary (e.g. the duration of a syscall()). | |
161 | * | |
162 | * Returns the fd on success, otherwise a negative value may be returned | |
163 | * if the restoration of the fd failed. | |
164 | */ | |
165 | int fs_handle_get_fd(struct fs_handle *handle); | |
166 | ||
167 | /* | |
168 | * Used by the application to signify that it is no longer using the | |
169 | * underlying fd and that it may be suspended. | |
170 | */ | |
171 | void fs_handle_put_fd(struct fs_handle *handle); | |
172 | ||
9d16fc7f JG |
173 | /* |
174 | * Unlink the file associated to an fs_handle. Note that the unlink | |
175 | * operation will not be performed immediately. It will only be performed | |
176 | * once all references to the underlying file (through other fs_handle objects) | |
177 | * have been released. | |
178 | * | |
179 | * However, note that the file will be renamed so as to provide the observable | |
180 | * effect of an unlink(), that is removing a name from the filesystem. | |
181 | * | |
182 | * Returns 0 on success, otherwise a negative value will be returned | |
183 | * if the operation failed. | |
184 | */ | |
185 | int fs_handle_unlink(struct fs_handle *handle); | |
186 | ||
df038819 JG |
187 | /* |
188 | * Frees the handle and discards the underlying fd. | |
189 | */ | |
190 | int fs_handle_close(struct fs_handle *handle); | |
191 | ||
192 | #endif /* FD_TRACKER_H */ |