]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blob - src/trackfds.h
Use char const * instead of const char *.
[coloredstderr/coloredstderr.git] / src / trackfds.h
1 /*
2  * Utility functions to track file descriptors.
3  *
4  * Copyright (C) 2013  Simon Ruderich
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #ifndef TRACKFDS_H
21 #define TRACKFDS_H 1
22
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];
26
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;
33
34
35 #ifdef DEBUG
36 static void tracked_fds_debug(void) {
37     size_t i;
38
39     for (i = 0; i < TRACKFDS_STATIC_COUNT; i++) {
40         if (tracked_fds[i]) {
41             debug("    tracked_fds[%d]: %d\n", i, tracked_fds[i]);
42         }
43     }
44     debug("    tracked_fds_list: %d/%d\t[%d]\n", tracked_fds_list_count,
45                                                  tracked_fds_list_space,
46                                                  getpid());
47     for (i = 0; i < tracked_fds_list_count; i++) {
48         debug("    tracked_fds_list[%d]: %d\n", i, tracked_fds_list[i]);
49     }
50 }
51 #endif
52
53 static int init_tracked_fds_list(size_t count) {
54     /* Reduce reallocs. */
55     count += TRACKFDS_REALLOC_STEP;
56
57     tracked_fds_list = malloc(count * sizeof(*tracked_fds_list));
58     if (!tracked_fds_list) {
59 #ifdef DEBUG
60         warning("malloc(tracked_fds_list, %d) failed [%d]\n",
61                 count * sizeof(*tracked_fds_list), getpid());
62 #endif
63         return 0;
64     }
65
66     tracked_fds_list_space = count;
67     return 1;
68 }
69
70 /* Load tracked file descriptors from the environment. The environment is used
71  * to pass the information to child processes.
72  *
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) {
76 #ifdef DEBUG
77     debug("init_from_environment()\t\t[%d]\n", getpid());
78 #endif
79     char const *env;
80
81     initialized = 1;
82     tracked_fds_list_count = 0;
83
84     /* If ENV_NAME_FORCE_WRITE is set and not empty, allow writes to a non-tty
85      * device. Use with care! Mainly used for the test suite. */
86     env = getenv(ENV_NAME_FORCE_WRITE);
87     if (env && env[0] != '\0') {
88         force_write_to_non_tty = 1;
89     }
90
91     env = getenv(ENV_NAME_FDS);
92     if (!env) {
93         return;
94     }
95     /* Environment is read-only. */
96     char env_copy[strlen(env) + 1];
97     strcpy(env_copy, env);
98
99     char *x;
100
101     size_t count = 0;
102     for (x = env_copy; *x; x++) {
103         if (*x == ',') {
104             count++;
105         }
106     }
107
108     size_t i = 0;
109
110     /* Parse file descriptor numbers from environment string and store them as
111      * integers in tracked_fds and tracked_fds_list. */
112     char *last;
113     for (x = env_copy, last = env_copy; *x; x++) {
114         if (*x != ',') {
115             continue;
116         }
117         /* ',' at the beginning or double ',' - ignore. */
118         if (x == last) {
119             last = x + 1;
120             continue;
121         }
122
123         if (i == count) {
124             break;
125         }
126
127         *x = 0;
128
129         int fd = atoi(last);
130         if (fd < TRACKFDS_STATIC_COUNT) {
131             tracked_fds[fd] = 1;
132         } else {
133             if (!tracked_fds_list) {
134                 /* Pessimistic count estimate, but allocating a few more
135                  * elements doesn't hurt. */
136                 if (!init_tracked_fds_list(count)) {
137                     /* Couldn't allocate memory, skip this entry. */
138                     warning("foo\n");
139                     goto next;
140                 }
141             }
142             tracked_fds_list[i++] = fd;
143 #ifdef DEBUG
144             debug("  large fd: %d\n", fd);
145 #endif
146         }
147
148 next:
149         last = x + 1;
150     }
151
152     tracked_fds_list_count = i;
153
154 #ifdef DEBUG
155     tracked_fds_debug();
156 #endif
157 }
158
159 static char *update_environment_buffer_entry(char *x, int fd) {
160     int length = snprintf(x, 10 + 1, "%d", fd);
161     if (length >= 10 + 1) {
162         /* Integer too big to fit the buffer, skip it. */
163 #ifdef DEBUG
164         warning("update_environment_buffer_entry(): truncated fd: %d [%d]\n",
165                 fd, getpid());
166 #endif
167         return x;
168     }
169
170     /* Write comma after number. */
171     x += length;
172     *x++ = ',';
173     /* Make sure the string is always zero terminated. */
174     *x = 0;
175
176     return x;
177 }
178 static void update_environment_buffer(char *x) {
179     size_t i;
180     for (i = 0; i < TRACKFDS_STATIC_COUNT; i++) {
181         if (tracked_fds[i]) {
182             x = update_environment_buffer_entry(x, (int)i);
183         }
184     }
185     for (i = 0; i < tracked_fds_list_count; i++) {
186         x = update_environment_buffer_entry(x, tracked_fds_list[i]);
187     }
188 }
189 inline static size_t update_environment_buffer_size(void) {
190     /* Use the maximum count (TRACKFDS_STATIC_COUNT) of used descriptors
191      * because it's simple and small enough not to be a problem.
192      *
193      * An integer (32-bit) has at most 10 digits, + 1 for the comma after each
194      * number. Bigger file descriptors (which shouldn't occur in reality) are
195      * skipped. */
196     return (TRACKFDS_STATIC_COUNT + tracked_fds_list_count)
197                * (10 + 1) + 1 /* to fit '\0' */;
198 }
199 static void update_environment(void) {
200 #ifdef DEBUG
201     debug("update_environment()\t\t[%d]\n", getpid());
202 #endif
203
204     /* If we haven't parsed the environment we also haven't modified it - so
205      * nothing to do. */
206     if (!initialized) {
207         return;
208     }
209
210     char env[update_environment_buffer_size()];
211     env[0] = 0;
212
213     update_environment_buffer(env);
214
215 #if 0
216     debug("    setenv('%s', '%s', 1)\n", ENV_NAME_FDS, env);
217 #endif
218
219     setenv(ENV_NAME_FDS, env, 1 /* overwrite */);
220 }
221
222
223
224 static void tracked_fds_add(int fd) {
225     if (fd < TRACKFDS_STATIC_COUNT) {
226         tracked_fds[fd] = 1;
227 #if 0
228         debug("tracked_fds_add(): %-3d\t\t[%d]\n", fd, getpid());
229         tracked_fds_debug();
230 #endif
231         return;
232     }
233
234     if (tracked_fds_list_count >= tracked_fds_list_space) {
235         size_t new_space = tracked_fds_list_space + TRACKFDS_REALLOC_STEP;
236         int *tmp = realloc(tracked_fds_list,
237                            sizeof(*tracked_fds_list) * new_space);
238         if (!tmp) {
239             /* We can do nothing, just ignore the error. We made sure not to
240              * destroy our state, so the new descriptor is ignored without any
241              * other consequences. */
242 #ifdef DEBUG
243             warning("realloc(tracked_fds_list, %zu) failed! [%d]\n",
244                     sizeof(*tracked_fds_list) * new_space, getpid());
245 #endif
246             return;
247         }
248         tracked_fds_list = tmp;
249         tracked_fds_list_space = new_space;
250     }
251
252     tracked_fds_list[tracked_fds_list_count++] = fd;
253
254 #ifdef DEBUG
255     debug("tracked_fds_add(): %-3d\t\t[%d]\n", fd, getpid());
256     tracked_fds_debug();
257 #endif
258 }
259 static int tracked_fds_remove(int fd) {
260     if (fd < TRACKFDS_STATIC_COUNT) {
261         int old_value = tracked_fds[fd];
262         tracked_fds[fd] = 0;
263
264 #if 0
265         debug("tracked_fds_remove(): %-3d\t[%d]\n", fd, getpid());
266         tracked_fds_debug();
267 #endif
268         return old_value; /* Found vs. not found. */
269     }
270
271     size_t i;
272     for (i = 0; i < tracked_fds_list_count; i++) {
273         if (fd != tracked_fds_list[i]) {
274             continue;
275         }
276
277         memmove(tracked_fds_list + i, tracked_fds_list + i + 1,
278                 sizeof(*tracked_fds_list) * (tracked_fds_list_count - i - 1));
279         tracked_fds_list_count--;
280
281 #ifdef DEBUG
282         debug("tracked_fds_remove(): %-3d\t[%d]\n", fd, getpid());
283         tracked_fds_debug();
284 #endif
285
286         /* Found. */
287         return 1;
288     }
289
290     /* Not found. */
291     return 0;
292 }
293 static int tracked_fds_find(int fd) {
294     if (fd < TRACKFDS_STATIC_COUNT) {
295         return tracked_fds[fd];
296     }
297     if (tracked_fds_list_count == 0) {
298         return 0;
299     }
300
301     size_t i;
302     for (i = 0; i < tracked_fds_list_count; i++) {
303         if (fd == tracked_fds_list[i]) {
304             return 1;
305         }
306     }
307     return 0;
308 }
309
310 #endif