2 * Utility functions to track file descriptors.
4 * Copyright (C) 2013 Simon Ruderich
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* Array of tracked file descriptors. Used for fast lookups for the normally
24 * used file descriptors (0 <= fd < TRACKFDS_STATIC_COUNT). */
25 static int tracked_fds[TRACKFDS_STATIC_COUNT];
27 /* List of tracked file descriptors >= TRACKFDS_STATIC_COUNT. */
28 static int *tracked_fds_list;
29 /* Current number of items in the list. */
30 static size_t tracked_fds_list_count;
31 /* Allocated items, used to reduce realloc()s. */
32 static size_t tracked_fds_list_space;
36 static void tracked_fds_debug(void) {
39 for (i = 0; i < TRACKFDS_STATIC_COUNT; i++) {
41 debug(" tracked_fds[%d]: %d\n", i, tracked_fds[i]);
44 debug(" tracked_fds_list: %d/%d\t[%d]\n", tracked_fds_list_count,
45 tracked_fds_list_space,
47 for (i = 0; i < tracked_fds_list_count; i++) {
48 debug(" tracked_fds_list[%d]: %d\n", i, tracked_fds_list[i]);
53 static int init_tracked_fds_list(size_t count) {
54 /* Reduce reallocs. */
55 count += TRACKFDS_REALLOC_STEP;
57 tracked_fds_list = malloc(count * sizeof(*tracked_fds_list));
58 if (!tracked_fds_list) {
60 warning("malloc(tracked_fds_list, %d) failed [%d]\n",
61 count * sizeof(*tracked_fds_list), getpid());
66 tracked_fds_list_space = count;
70 /* Load tracked file descriptors from the environment. The environment is used
71 * to pass the information to child processes.
73 * ENV_NAME_FDS has the following format: Each descriptor as string followed
74 * by a comma; there's a trailing comma. Example: "2,4,". */
75 static void init_from_environment(void) {
77 debug("init_from_environment()\t\t[%d]\n", getpid());
81 int saved_errno = errno;
84 tracked_fds_list_count = 0;
86 /* If ENV_NAME_FORCE_WRITE is set and not empty, allow writes to a non-tty
87 * device. Use with care! Mainly used for the test suite. */
88 env = getenv(ENV_NAME_FORCE_WRITE);
89 if (env && env[0] != '\0') {
90 force_write_to_non_tty = 1;
93 env = getenv(ENV_NAME_FDS);
99 debug(" getenv(\"%s\"): \"%s\"\n", ENV_NAME_FDS, env);
101 /* Environment is read-only. */
102 char env_copy[strlen(env) + 1];
103 strcpy(env_copy, env);
108 for (x = env_copy; *x; x++) {
116 /* Parse file descriptor numbers from environment string and store them as
117 * integers in tracked_fds and tracked_fds_list. */
119 for (x = env_copy, last = env_copy; *x; x++) {
123 /* ',' at the beginning or double ',' - ignore. */
136 if (fd < TRACKFDS_STATIC_COUNT) {
139 if (!tracked_fds_list) {
140 /* Pessimistic count estimate, but allocating a few more
141 * elements doesn't hurt. */
142 if (!init_tracked_fds_list(count)) {
143 /* Couldn't allocate memory, skip this entry. */
147 tracked_fds_list[i++] = fd;
149 debug(" large fd: %d\n", fd);
157 tracked_fds_list_count = i;
166 static char *update_environment_buffer_entry(char *x, int fd) {
167 int length = snprintf(x, 10 + 1, "%d", fd);
168 if (length >= 10 + 1) {
169 /* Integer too big to fit the buffer, skip it. */
171 warning("update_environment_buffer_entry(): truncated fd: %d [%d]\n",
177 /* Write comma after number. */
180 /* Make sure the string is always zero terminated. */
185 static void update_environment_buffer(char *x) {
187 for (i = 0; i < TRACKFDS_STATIC_COUNT; i++) {
188 if (tracked_fds[i]) {
189 x = update_environment_buffer_entry(x, (int)i);
192 for (i = 0; i < tracked_fds_list_count; i++) {
193 x = update_environment_buffer_entry(x, tracked_fds_list[i]);
196 inline static size_t update_environment_buffer_size(void) {
197 /* Use the maximum count (TRACKFDS_STATIC_COUNT) of used descriptors
198 * because it's simple and small enough not to be a problem.
200 * An integer (32-bit) has at most 10 digits, + 1 for the comma after each
201 * number. Bigger file descriptors (which shouldn't occur in reality) are
203 return (TRACKFDS_STATIC_COUNT + tracked_fds_list_count)
204 * (10 + 1) + 1 /* to fit '\0' */;
206 static void update_environment(void) {
208 debug("update_environment()\t\t[%d]\n", getpid());
211 /* If we haven't parsed the environment we also haven't modified it - so
217 char env[update_environment_buffer_size()];
220 update_environment_buffer(env);
223 debug(" setenv(\"%s\", \"%s\", 1)\n", ENV_NAME_FDS, env);
226 setenv(ENV_NAME_FDS, env, 1 /* overwrite */);
231 static void tracked_fds_add(int fd) {
232 if (fd < TRACKFDS_STATIC_COUNT) {
235 debug("tracked_fds_add(): %-3d\t\t[%d]\n", fd, getpid());
241 if (tracked_fds_list_count >= tracked_fds_list_space) {
242 int saved_errno = errno;
244 size_t new_space = tracked_fds_list_space + TRACKFDS_REALLOC_STEP;
245 int *tmp = realloc(tracked_fds_list,
246 sizeof(*tracked_fds_list) * new_space);
248 /* We can do nothing, just ignore the error. We made sure not to
249 * destroy our state, so the new descriptor is ignored without any
250 * other consequences. */
252 warning("realloc(tracked_fds_list, %zu) failed! [%d]\n",
253 sizeof(*tracked_fds_list) * new_space, getpid());
260 tracked_fds_list = tmp;
261 tracked_fds_list_space = new_space;
264 tracked_fds_list[tracked_fds_list_count++] = fd;
267 debug("tracked_fds_add(): %-3d\t\t[%d]\n", fd, getpid());
271 static int tracked_fds_remove(int fd) {
272 if (fd < TRACKFDS_STATIC_COUNT) {
273 int old_value = tracked_fds[fd];
277 debug("tracked_fds_remove(): %-3d\t[%d]\n", fd, getpid());
280 return old_value; /* Found vs. not found. */
284 for (i = 0; i < tracked_fds_list_count; i++) {
285 if (fd != tracked_fds_list[i]) {
289 memmove(tracked_fds_list + i, tracked_fds_list + i + 1,
290 sizeof(*tracked_fds_list) * (tracked_fds_list_count - i - 1));
291 tracked_fds_list_count--;
294 debug("tracked_fds_remove(): %-3d\t[%d]\n", fd, getpid());
306 static int tracked_fds_find_slow(int fd) noinline;
308 * tracked_fds_find() is called for each hook call and should be as fast as
309 * possible. As most file descriptors are < TRACKFDS_STATIC_COUNT, force the
310 * compiler to inline that part which is almost exclusively used.
312 * Inlining tracked_fds_add()/tracked_fds_remove() isn't worth the effort as
313 * they are not called often enough.
315 inline static int tracked_fds_find(int fd) always_inline;
316 static int tracked_fds_find(int fd) {
317 if (fd < TRACKFDS_STATIC_COUNT) {
318 return tracked_fds[fd];
321 return tracked_fds_find_slow(fd);
323 static int tracked_fds_find_slow(int fd) {
324 if (tracked_fds_list_count == 0) {
329 for (i = 0; i < tracked_fds_list_count; i++) {
330 if (fd == tracked_fds_list[i]) {