| 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 | |
| 24 | struct fs_handle; |
| 25 | struct 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 | */ |
| 37 | typedef 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 | */ |
| 49 | typedef 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 | */ |
| 56 | struct fd_tracker *fd_tracker_create(unsigned int capacity); |
| 57 | |
| 58 | /* Returns an error if file descriptors are leaked. */ |
| 59 | int 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 | */ |
| 83 | struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker, |
| 84 | const char *path, |
| 85 | int flags, |
| 86 | mode_t *mode); |
| 87 | |
| 88 | /* |
| 89 | * Open a tracked unsuspendable file descriptor. |
| 90 | * |
| 91 | * This function allows the fd tracker to keep track of unsuspendable |
| 92 | * file descriptors. A callback, open, is passed to allow the tracker |
| 93 | * to atomically reserve an entry for a given count of new file descriptors, |
| 94 | * suspending file descriptors as needed, and invoke the provided callback |
| 95 | * without ever exceeding the tracker's capacity. |
| 96 | * |
| 97 | * fd_count indicates the count of file descriptors that will be opened and |
| 98 | * returned by the open callback. The storage location at out_fds is assumed |
| 99 | * to be large enough to hold 'fd_count * sizeof(int)'. |
| 100 | * |
| 101 | * Names may be provided to allow easier debugging of file descriptor |
| 102 | * exhaustions. |
| 103 | * |
| 104 | * The callback's return value is returned to the user. Additionally, two |
| 105 | * negative tracker-specific codes may be returned: |
| 106 | * - ENOMEM: allocation of a new entry failed, |
| 107 | * - EMFILE: too many unsuspendable fds are opened and the tracker can't |
| 108 | * accomodate the request for a new unsuspendable entry. |
| 109 | */ |
| 110 | int fd_tracker_open_unsuspendable_fd(struct fd_tracker *tracker, |
| 111 | int *out_fds, |
| 112 | const char **names, |
| 113 | unsigned int fd_count, |
| 114 | fd_open_cb open, |
| 115 | void *data); |
| 116 | |
| 117 | /* |
| 118 | * Close a tracked unsuspendable file descriptor. |
| 119 | * |
| 120 | * This function allows the fd tracker to keep track of unsuspendable |
| 121 | * file descriptors. A callback, close, is passed to allow the tracker |
| 122 | * to atomically release a file descriptor entry. |
| 123 | * |
| 124 | * Returns 0 if the close callback returned success. Returns the value returned |
| 125 | * by the close callback if it is negative. Additionally, a tracker-specific |
| 126 | * code may be returned: |
| 127 | * - EINVAL: a file descriptor was unknown to the tracker |
| 128 | * |
| 129 | * Closed fds are set to -1 in the fds array which, in the event of an error, |
| 130 | * allows the user to know which file descriptors are no longer being tracked. |
| 131 | */ |
| 132 | int fd_tracker_close_unsuspendable_fd(struct fd_tracker *tracker, |
| 133 | int *fds, |
| 134 | unsigned int fd_count, |
| 135 | fd_close_cb close, |
| 136 | void *data); |
| 137 | |
| 138 | /* |
| 139 | * Log the contents of the fd_tracker. |
| 140 | */ |
| 141 | void fd_tracker_log(struct fd_tracker *tracker); |
| 142 | |
| 143 | /* |
| 144 | * Marks the handle as the most recently used and marks the 'fd' as |
| 145 | * "in-use". This prevents the tracker from recycling the underlying |
| 146 | * file descriptor while it is actively being used by a thread. |
| 147 | * |
| 148 | * Don't forget that the tracker may be initiating an fd 'suspension' |
| 149 | * from another thread as the need to free an fd slot may arise from any |
| 150 | * thread within the daemon. |
| 151 | * |
| 152 | * Note that a restorable fd should never be held for longer than |
| 153 | * strictly necessary (e.g. the duration of a syscall()). |
| 154 | * |
| 155 | * Returns the fd on success, otherwise a negative value may be returned |
| 156 | * if the restoration of the fd failed. |
| 157 | */ |
| 158 | int fs_handle_get_fd(struct fs_handle *handle); |
| 159 | |
| 160 | /* |
| 161 | * Used by the application to signify that it is no longer using the |
| 162 | * underlying fd and that it may be suspended. |
| 163 | */ |
| 164 | void fs_handle_put_fd(struct fs_handle *handle); |
| 165 | |
| 166 | /* |
| 167 | * Unlink the file associated to an fs_handle. Note that the unlink |
| 168 | * operation will not be performed immediately. It will only be performed |
| 169 | * once all references to the underlying file (through other fs_handle objects) |
| 170 | * have been released. |
| 171 | * |
| 172 | * However, note that the file will be renamed so as to provide the observable |
| 173 | * effect of an unlink(), that is removing a name from the filesystem. |
| 174 | * |
| 175 | * Returns 0 on success, otherwise a negative value will be returned |
| 176 | * if the operation failed. |
| 177 | */ |
| 178 | int fs_handle_unlink(struct fs_handle *handle); |
| 179 | |
| 180 | /* |
| 181 | * Frees the handle and discards the underlying fd. |
| 182 | */ |
| 183 | int fs_handle_close(struct fs_handle *handle); |
| 184 | |
| 185 | #endif /* FD_TRACKER_H */ |