5801622e22995610b4e5083d5623163f095ebe44
[lttng-tools.git] / src / common / fd-tracker / fd-tracker.h
1 /*
2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #ifndef FD_TRACKER_H
9 #define FD_TRACKER_H
10
11 #include <common/compat/directory-handle.h>
12 #include <common/macros.h>
13 #include <stdint.h>
14 #include <sys/types.h>
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 struct fs_handle;
21 struct fd_tracker;
22
23 /*
24 * Callback which returns a file descriptor to track through the fd
25 * tracker. This callback must not make use of the fd_tracker as a deadlock
26 * may occur.
27 *
28 * The int pointer argument is an output parameter that should be used to return
29 * the advertised number of file descriptors.
30 *
31 * Must return zero on success. Negative values should map to a UNIX error code.
32 */
33 typedef int (*fd_open_cb)(void *, int *out_fds);
34
35 /*
36 * Callback to allow the user to close a now-untracked file descriptor. This
37 * callback must not make use of the fd_tracker as a deadlock may occur.
38 *
39 * The callback can freely modify the in_fds argument as it is copied by the
40 * fd_tracker before being used. The fd tracker assumes in_fds to be closed by
41 * the time the callback returns.
42 *
43 * Must return zero on success. Negative values should map to a UNIX error code.
44 */
45 typedef int (*fd_close_cb)(void *, int *in_fds);
46
47 /*
48 * Set the maximal number of fds that the process should be allowed to open at
49 * any given time. This function must be called before any other of this
50 * interface.
51 *
52 * The unlinked_file_path is an absolute path (which does not need to exist)
53 * under which unlinked files will be stored for as long as a reference to them
54 * is held.
55 */
56 struct fd_tracker *fd_tracker_create(const char *unlinked_file_path,
57 unsigned int capacity);
58
59 /* Returns an error if file descriptors are leaked. */
60 int fd_tracker_destroy(struct fd_tracker *tracker);
61
62 /*
63 * Open a handle to a suspendable filesystem file descriptor.
64 *
65 * See OPEN(3) for an explanation of flags and mode. NULL is returned in case of
66 * error and errno is left untouched. Note that passing NULL as mode will result
67 * in open()'s default behaviour being used (using the process' umask).
68 *
69 * A fs_handle wraps a file descriptor created by OPEN(3). It is suspendable
70 * meaning that the underlying file may be closed at any time unless the
71 * handle is marked as being in-use (see fs_handle_get_fd() and
72 * fs_handle_put_fd()).
73 *
74 * If the tracker opted to close the underlying file descriptor, it will
75 * be restored to its last known state when it is obtained through
76 * the fs_handle's fs_handle_get_fd() method.
77 *
78 * Note that a suspendable file descriptor can be closed by the fd tracker at
79 * anytime when it is not in use. This means that the user should not rely on it
80 * being safe to unlink the file. Moreover, concurent modifications to the file
81 * (e.g. truncation) may react differently than if the file descriptor was kept
82 * open.
83 */
84 struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker,
85 struct lttng_directory_handle *directory,
86 const char *path,
87 int flags,
88 mode_t *mode);
89
90 /*
91 * Open a tracked unsuspendable file descriptor.
92 *
93 * This function allows the fd tracker to keep track of unsuspendable
94 * file descriptors. A callback, open, is passed to allow the tracker
95 * to atomically reserve an entry for a given count of new file descriptors,
96 * suspending file descriptors as needed, and invoke the provided callback
97 * without ever exceeding the tracker's capacity.
98 *
99 * fd_count indicates the count of file descriptors that will be opened and
100 * returned by the open callback. The storage location at out_fds is assumed
101 * to be large enough to hold 'fd_count * sizeof(int)'.
102 *
103 * Names may be provided to allow easier debugging of file descriptor
104 * exhaustions.
105 *
106 * The callback's return value is returned to the user. Additionally, two
107 * negative tracker-specific codes may be returned:
108 * - ENOMEM: allocation of a new entry failed,
109 * - EMFILE: too many unsuspendable fds are opened and the tracker can't
110 * accommodates the request for a new unsuspendable entry.
111 */
112 int fd_tracker_open_unsuspendable_fd(struct fd_tracker *tracker,
113 int *out_fds,
114 const char **names,
115 unsigned int fd_count,
116 fd_open_cb open,
117 void *data);
118
119 /*
120 * Close a tracked unsuspendable file descriptor.
121 *
122 * This function allows the fd tracker to keep track of unsuspendable
123 * file descriptors. A callback, close, is passed to allow the tracker
124 * to atomically release a file descriptor entry.
125 *
126 * Returns 0 if the close callback returned success. Returns the value returned
127 * by the close callback if it is negative. Additionally, a tracker-specific
128 * code may be returned:
129 * - EINVAL: a file descriptor was unknown to the tracker
130 *
131 * Closed fds are set to -1 in the fds array which, in the event of an error,
132 * allows the user to know which file descriptors are no longer being tracked.
133 */
134 int fd_tracker_close_unsuspendable_fd(struct fd_tracker *tracker,
135 int *fds,
136 unsigned int fd_count,
137 fd_close_cb close,
138 void *data);
139
140 /*
141 * Log the contents of the fd_tracker.
142 */
143 void fd_tracker_log(struct fd_tracker *tracker);
144
145 #ifdef __cplusplus
146 }
147 #endif
148
149 #endif /* FD_TRACKER_H */
This page took 0.031581 seconds and 3 git commands to generate.