/src/dbus-broker/src/util/fdlist.c
Line | Count | Source |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later */ |
2 | | /* SPDX-FileCopyrightText: D-Bus Broker Developers */ |
3 | | |
4 | | /* |
5 | | * File-Descriptor List |
6 | | * |
7 | | * The FDList object is a small wrapper around a fixed-size array of |
8 | | * file-descriptors. It allows easy handling of file-descriptor sets as atomic |
9 | | * entity, while still providing access to individual entries. |
10 | | * |
11 | | * Furthermore, the FDList object is meant as supplement for AF_UNIX sockets. |
12 | | * Hence, it stores FDs as a cmsghdr entry, ready to be used with sendmsg(2). |
13 | | */ |
14 | | |
15 | | #include <c-stdaux.h> |
16 | | #include <stdlib.h> |
17 | | #include <sys/socket.h> |
18 | | #include "util/error.h" |
19 | | #include "util/fdlist.h" |
20 | | |
21 | | /** |
22 | | * fdlist_new_with_fds() - create fdlist with a set of FDs |
23 | | * @listp: output for new fdlist |
24 | | * @fds: FD array to import |
25 | | * @n_fds: array size of @fds |
26 | | * |
27 | | * This creates a new fdlist and imports the FDs given as @fds + @n_fds. The |
28 | | * FDs are still owned by the caller and will not be freed on fdlist_free(). |
29 | | * |
30 | | * Return: 0 on success, negative error code on failure. |
31 | | */ |
32 | 0 | int fdlist_new_with_fds(FDList **listp, const int *fds, size_t n_fds) { |
33 | 0 | FDList *list; |
34 | |
|
35 | 0 | list = calloc(1, sizeof(*list) + CMSG_SPACE(n_fds * sizeof(int))); |
36 | 0 | if (!list) |
37 | 0 | return error_origin(-ENOMEM); |
38 | | |
39 | 0 | list->consumed = false; |
40 | 0 | list->cmsg->cmsg_len = CMSG_LEN(n_fds * sizeof(int)); |
41 | 0 | list->cmsg->cmsg_level = SOL_SOCKET; |
42 | 0 | list->cmsg->cmsg_type = SCM_RIGHTS; |
43 | 0 | c_memcpy(fdlist_data(list), fds, n_fds * sizeof(int)); |
44 | |
|
45 | 0 | *listp = list; |
46 | 0 | return 0; |
47 | 0 | } |
48 | | |
49 | | /** |
50 | | * fdlist_new_consume_fds() - create fdlist and consume FDs |
51 | | * @listp: output for new fdlist |
52 | | * @fds: FD array to import |
53 | | * @n_fds: array size of @fds |
54 | | * |
55 | | * This is the same as fdlist_new_with_fds() but consumes the FDs. That is, the |
56 | | * caller no longer owns the FDs, and the FDs will be closed on fdlist_free(). |
57 | | * |
58 | | * Return: 0 on success, negative error code on failure. |
59 | | */ |
60 | 0 | int fdlist_new_consume_fds(FDList **listp, const int *fds, size_t n_fds) { |
61 | 0 | int r; |
62 | |
|
63 | 0 | r = fdlist_new_with_fds(listp, fds, n_fds); |
64 | 0 | if (!r) |
65 | 0 | (*listp)->consumed = true; |
66 | |
|
67 | 0 | return r; |
68 | 0 | } |
69 | | |
70 | | /** |
71 | | * fdlist_new_dup_fds() - create fdlist and duplicate FDs |
72 | | * @listp: output for new fdlist |
73 | | * @fds: FD array to duplicate |
74 | | * @n_fds: array size of @fds |
75 | | * |
76 | | * This is the same as fdlist_new_with_fds() but duplicates the FDs. That is, |
77 | | * the caller retains the FDs, and an independent copy of each FD will be owned |
78 | | * by the new object. |
79 | | * |
80 | | * Return: 0 on success, negative error code on failure. |
81 | | */ |
82 | 0 | int fdlist_new_dup_fds(FDList **listp, const int *fds, size_t n_fds) { |
83 | 0 | _c_cleanup_(fdlist_freep) FDList *list = NULL; |
84 | 0 | size_t i; |
85 | 0 | int r, *p; |
86 | |
|
87 | 0 | r = fdlist_new_with_fds(&list, fds, n_fds); |
88 | 0 | if (r) |
89 | 0 | return error_trace(r); |
90 | | |
91 | 0 | p = fdlist_data(list); |
92 | 0 | for (i = 0; i < n_fds; ++i) { |
93 | 0 | p[i] = fcntl(p[i], F_DUPFD_CLOEXEC, 3); |
94 | 0 | if (p[i] < 0) { |
95 | 0 | r = -errno; |
96 | 0 | while (i > 0) { |
97 | 0 | --i; |
98 | 0 | p[i] = c_close(p[i]); |
99 | 0 | } |
100 | 0 | return error_origin(r); |
101 | 0 | } |
102 | 0 | } |
103 | | |
104 | 0 | list->consumed = true; |
105 | |
|
106 | 0 | *listp = list; |
107 | 0 | list = NULL; |
108 | 0 | return 0; |
109 | 0 | } |
110 | | |
111 | | /** |
112 | | * fdlist_free() - free fdlist |
113 | | * @list: fdlist to operate on, or NULL |
114 | | * |
115 | | * This frees the fdlist given as @list. If the file-descriptors were marked as |
116 | | * `consumed`, this function will close them. Otherwise, they're left |
117 | | * untouched. |
118 | | * |
119 | | * If @list is NULL, this is a no-op. |
120 | | * |
121 | | * Return: NULL is returned. |
122 | | */ |
123 | 3.34k | FDList *fdlist_free(FDList *list) { |
124 | 3.34k | size_t i, n; |
125 | 3.34k | int *p; |
126 | | |
127 | 3.34k | if (list) { |
128 | 0 | p = fdlist_data(list); |
129 | 0 | n = fdlist_count(list); |
130 | |
|
131 | 0 | if (list->consumed) |
132 | 0 | for (i = 0; i < n; ++i) |
133 | 0 | c_close(p[i]); |
134 | |
|
135 | 0 | free(list); |
136 | 0 | } |
137 | | |
138 | 3.34k | return NULL; |
139 | 3.34k | } |
140 | | |
141 | | /** |
142 | | * fdlist_truncate() - truncate fdlist |
143 | | * @list: fdlist to operate on |
144 | | * @n_fds: number of FDs to retain |
145 | | * |
146 | | * This shrinks the fdlist to size @n_fds. The caller must make sure the fdlist |
147 | | * is sized greater than, or equal to, @n_fds. |
148 | | * |
149 | | * If the discarded FDs were marked as consumed, then this will close them. |
150 | | * Otherwise, they're left untouched. |
151 | | */ |
152 | 0 | void fdlist_truncate(FDList *list, size_t n_fds) { |
153 | 0 | size_t i, n; |
154 | 0 | int *p; |
155 | |
|
156 | 0 | p = fdlist_data(list); |
157 | 0 | n = fdlist_count(list); |
158 | |
|
159 | 0 | c_assert(n_fds <= n); |
160 | | |
161 | 0 | if (list->consumed) |
162 | 0 | for (i = n_fds; i < n; ++i) |
163 | 0 | c_close(p[i]); |
164 | |
|
165 | 0 | list->cmsg->cmsg_len = CMSG_LEN(n_fds * sizeof(int)); |
166 | 0 | } |
167 | | |
168 | | /** |
169 | | * fdlist_steal() - steal FD |
170 | | * @list: fdlist to operate on |
171 | | * @index: index of FD |
172 | | * |
173 | | * This returns the FD at position @index and then drops it form the fdlist, by |
174 | | * replacing it with -1. |
175 | | * |
176 | | * If @index points outside the fdlist range, or if the FD was already stolen, |
177 | | * -1 is returned. |
178 | | * |
179 | | * Return: The FD at position @index is returned. |
180 | | */ |
181 | 0 | int fdlist_steal(FDList *list, size_t index) { |
182 | 0 | int *p, fd = -1; |
183 | |
|
184 | 0 | p = fdlist_data(list); |
185 | |
|
186 | 0 | if (index < fdlist_count(list)) { |
187 | 0 | fd = p[index]; |
188 | 0 | p[index] = -1; |
189 | 0 | } |
190 | |
|
191 | 0 | return fd; |
192 | 0 | } |