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 /* List of tracked file descriptors. */
24 static int *tracked_fds;
25 /* Current number of items in the list. */
26 static size_t tracked_fds_count;
27 /* Allocated items, used to reduce realloc()s. */
28 static size_t tracked_fds_space;
32 static void tracked_fds_debug(void) {
33 debug(" tracked_fds: %d/%d\t\t[%d]\n", tracked_fds_count,
37 for (i = 0; i < tracked_fds_count; i++) {
38 debug(" tracked_fds[%d]: %d\n", i, tracked_fds[i]);
43 /* Load tracked file descriptors from the environment. The environment is used
44 * to pass the information to child processes.
46 * ENV_NAME_FDS has the following format: Each descriptor as string followed
47 * by a comma; there's a trailing comma. Example: "2,4,". */
48 static void init_from_environment(void) {
50 debug("init_from_environment()\t\t[%d]\n", getpid());
55 tracked_fds_count = 0;
57 /* If ENV_NAME_FORCE_WRITE is set and not empty, allow writes to a non-tty
58 * device. Use with care! Mainly used for the test suite. */
59 env = getenv(ENV_NAME_FORCE_WRITE);
60 if (env && env[0] != '\0') {
61 force_write_to_non_tty = 1;
64 env = getenv(ENV_NAME_FDS);
68 /* Environment is read-only. */
69 char env_copy[strlen(env) + 1];
70 strcpy(env_copy, env);
75 for (x = env_copy; *x; x++) {
80 tracked_fds_space = count + TRACKFDS_REALLOC_STEP;
82 tracked_fds = malloc(tracked_fds_space * sizeof(*tracked_fds));
89 /* Parse file descriptor numbers from environment string and store them as
90 * integers in tracked_fds. */
92 for (x = env_copy, last = env_copy; *x; x++) {
96 /* ',' at the beginning or double ',' - ignore. */
107 tracked_fds[i++] = atoi(last);
112 tracked_fds_count = count;
119 static void update_environment_buffer(char *x) {
121 for (i = 0; i < tracked_fds_count; i++) {
122 int length = snprintf(x, 10 + 1, "%d", tracked_fds[i]);
123 if (length >= 10 + 1) {
124 /* Integer too big to fit the buffer, skip it. */
128 /* Write comma after number. */
131 /* Make sure the string is always zero terminated. */
135 inline static size_t update_environment_buffer_size(void) {
136 /* An integer (32-bit) has at most 10 digits, + 1 for the comma after each
137 * number. Bigger file descriptors (which shouldn't occur in reality) are
139 return tracked_fds_count * (10 + 1) + 1 /* to fit '\0' */;
141 static void update_environment(void) {
143 debug("update_environment()\t\t[%d]\n", getpid());
146 /* If we haven't parsed the environment we also haven't modified it - so
152 char env[update_environment_buffer_size()];
155 update_environment_buffer(env);
158 debug(" setenv('%s', '%s', 1)\n", ENV_NAME_FDS, env);
161 setenv(ENV_NAME_FDS, env, 1 /* overwrite */);
166 static void tracked_fds_add(int fd) {
167 if (tracked_fds_count >= tracked_fds_space) {
168 size_t new_space = tracked_fds_space + TRACKFDS_REALLOC_STEP;
169 int *tmp = realloc(tracked_fds, sizeof(*tracked_fds) * new_space);
171 /* We can do nothing, just ignore the error. We made sure not to
172 * destroy our state, so the new descriptor is ignored without any
173 * other consequences. */
175 debug("realloc(tracked_fds, %zu) failed! [%d]\n",
176 sizeof(*tracked_fds) * new_space, getpid());
181 tracked_fds_space = new_space;
184 tracked_fds[tracked_fds_count++] = fd;
187 debug("tracked_fds_add(): %-3d\t\t[%d]\n", fd, getpid());
191 static int tracked_fds_remove(int fd) {
193 for (i = 0; i < tracked_fds_count; i++) {
194 if (fd != tracked_fds[i]) {
198 memmove(tracked_fds + i, tracked_fds + i + 1,
199 sizeof(*tracked_fds) * (tracked_fds_count - i - 1));
203 debug("tracked_fds_remove(): %-3d\t[%d]\n", fd, getpid());
214 static int tracked_fds_find(int fd) {
216 for (i = 0; i < tracked_fds_count; i++) {
217 if (fd == tracked_fds[i]) {