Clean-up: coverity warns of uncaught exception during logging
[lttng-tools.git] / src / common / fd-tracker / fd-tracker.hpp
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.hpp>
12 #include <common/macros.hpp>
13 #include <stdint.h>
14 #include <sys/types.h>
15
16 struct fs_handle;
17 struct fd_tracker;
18
19 /*
20 * Callback which returns a file descriptor to track through the fd
21 * tracker. This callback must not make use of the fd_tracker as a deadlock
22 * may occur.
23 *
24 * The int pointer argument is an output parameter that should be used to return
25 * the advertised number of file descriptors.
26 *
27 * Must return zero on success. Negative values should map to a UNIX error code.
28 */
29 typedef int (*fd_open_cb)(void *, int *out_fds);
30
31 /*
32 * Callback to allow the user to close a now-untracked file descriptor. This
33 * callback must not make use of the fd_tracker as a deadlock may occur.
34 *
35 * The callback can freely modify the in_fds argument as it is copied by the
36 * fd_tracker before being used. The fd tracker assumes in_fds to be closed by
37 * the time the callback returns.
38 *
39 * Must return zero on success. Negative values should map to a UNIX error code.
40 */
41 typedef int (*fd_close_cb)(void *, int *in_fds);
42
43 /*
44 * Set the maximal number of fds that the process should be allowed to open at
45 * any given time. This function must be called before any other of this
46 * interface.
47 *
48 * The unlinked_file_path is an absolute path (which does not need to exist)
49 * under which unlinked files will be stored for as long as a reference to them
50 * is held.
51 */
52 struct fd_tracker *fd_tracker_create(const char *unlinked_file_path,
53 unsigned int capacity);
54
55 /* Returns an error if file descriptors are leaked. */
56 int fd_tracker_destroy(struct fd_tracker *tracker);
57
58 /*
59 * Open a handle to a suspendable filesystem file descriptor.
60 *
61 * See OPEN(3) for an explanation of flags and mode. NULL is returned in case of
62 * error and errno is left untouched. Note that passing NULL as mode will result
63 * in open()'s default behaviour being used (using the process' umask).
64 *
65 * A fs_handle wraps a file descriptor created by OPEN(3). It is suspendable
66 * meaning that the underlying file may be closed at any time unless the
67 * handle is marked as being in-use (see fs_handle_get_fd() and
68 * fs_handle_put_fd()).
69 *
70 * If the tracker opted to close the underlying file descriptor, it will
71 * be restored to its last known state when it is obtained through
72 * the fs_handle's fs_handle_get_fd() method.
73 *
74 * Note that a suspendable file descriptor can be closed by the fd tracker at
75 * anytime when it is not in use. This means that the user should not rely on it
76 * being safe to unlink the file. Moreover, concurent modifications to the file
77 * (e.g. truncation) may react differently than if the file descriptor was kept
78 * open.
79 */
80 struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker,
81 struct lttng_directory_handle *directory,
82 const char *path,
83 int flags,
84 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 * accommodates the request for a new unsuspendable entry.
107 */
108 int fd_tracker_open_unsuspendable_fd(struct fd_tracker *tracker,
109 int *out_fds,
110 const char **names,
111 unsigned int fd_count,
112 fd_open_cb open,
113 void *data);
114
115 /*
116 * Close a tracked unsuspendable file descriptor.
117 *
118 * This function allows the fd tracker to keep track of unsuspendable
119 * file descriptors. A callback, close, is passed to allow the tracker
120 * to atomically release a file descriptor entry.
121 *
122 * Returns 0 if the close callback returned success. Returns the value returned
123 * by the close callback if it is negative. Additionally, a tracker-specific
124 * code may be returned:
125 * - EINVAL: a file descriptor was unknown to the tracker
126 *
127 * Closed fds are set to -1 in the fds array which, in the event of an error,
128 * allows the user to know which file descriptors are no longer being tracked.
129 */
130 int fd_tracker_close_unsuspendable_fd(struct fd_tracker *tracker,
131 int *fds,
132 unsigned int fd_count,
133 fd_close_cb close,
134 void *data);
135
136 /*
137 * Log the contents of the fd_tracker.
138 */
139 void fd_tracker_log(struct fd_tracker *tracker);
140
141 #endif /* FD_TRACKER_H */
This page took 0.032534 seconds and 4 git commands to generate.