]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blob - src/trackfds.h
trackfds.h: Handle overflow in update_environment().
[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 /* 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;
29
30
31 /* Load tracked file descriptors from the environment. The environment is used
32  * to pass the information to child processes.
33  *
34  * ENV_NAME_FDS has the following format: Each descriptor as string followed
35  * by a comma; there's a trailing comma. Example: "2,4,". */
36 static void init_from_environment(void) {
37     const char *env;
38
39     initialized = 1;
40     tracked_fds_count = 0;
41
42     /* If ENV_NAME_FORCE_WRITE is set and not empty, allow writes to a non-tty
43      * device. Use with care! Mainly used for the test suite. */
44     env = getenv(ENV_NAME_FORCE_WRITE);
45     if (env && env[0] != '\0') {
46         force_write_to_non_tty = 1;
47     }
48
49     env = getenv(ENV_NAME_FDS);
50     if (!env) {
51         return;
52     }
53     /* Environment is read-only. */
54     char *env_copy = strdup(env);
55     if (!env_copy) {
56         return;
57     }
58
59     char *x;
60
61     size_t count = 0;
62     for (x = env_copy; *x; x++) {
63         if (*x == ',') {
64             count++;
65         }
66     }
67     tracked_fds_space = count + TRACKFDS_REALLOC_STEP;
68
69     tracked_fds = malloc(tracked_fds_space * sizeof(*tracked_fds));
70     if (!tracked_fds) {
71         free(env_copy);
72         return;
73     }
74
75     size_t i = 0;
76
77     /* Parse file descriptor numbers from environment string and store them as
78      * integers in tracked_fds. */
79     char *last;
80     for (x = env_copy, last = env_copy; *x; x++) {
81         if (*x != ',') {
82             continue;
83         }
84         /* ',' at the beginning or double ',' - ignore. */
85         if (x == last) {
86             last = x + 1;
87             continue;
88         }
89
90         if (i == count) {
91             break;
92         }
93
94         *x = 0;
95         tracked_fds[i++] = atoi(last);
96
97         last = x + 1;
98     }
99
100     tracked_fds_count = count;
101
102     free(env_copy);
103 }
104
105 static void update_environment(void) {
106     /* An integer (32-bit) has at most 10 digits, + 1 for the comma after each
107      * number. Bigger file descriptors (which shouldn't occur in reality) are
108      * skipped. */
109     char env[tracked_fds_count * (10 + 1) * sizeof(char)];
110     char *x = env;
111
112     size_t i;
113     for (i = 0; i < tracked_fds_count; i++) {
114         int length = snprintf(x, 10 + 1, "%d", tracked_fds[i]);
115         if (length >= 10 + 1) {
116             /* Integer too bit to fit the buffer, skip it. */
117             continue;
118         }
119
120         /* Write comma after number. */
121         x += length;
122         *x++ = ',';
123         /* Make sure the string is always zero terminated. */
124         *x = 0;
125     }
126
127     setenv(ENV_NAME_FDS, env, 1 /* overwrite */);
128 }
129
130
131 #ifdef DEBUG
132 static void tracked_fds_debug(void) {
133     debug("tracked_fds: %d/%d\t[%d]\n", tracked_fds_count, tracked_fds_space,
134                                         getpid());
135     size_t i;
136     for (i = 0; i < tracked_fds_count; i++) {
137         debug("tracked_fds[%d]: %d\n", i, tracked_fds[i]);
138     }
139 }
140 #endif
141
142 static void tracked_fds_add(int fd) {
143     if (tracked_fds_count >= tracked_fds_space) {
144         size_t new_space = tracked_fds_space + TRACKFDS_REALLOC_STEP;
145         if (!realloc(tracked_fds, sizeof(*tracked_fds) * new_space)) {
146             /* We can do nothing, just ignore the error. We made sure not to
147              * destroy our state, so the new descriptor is ignored without any
148              * other consequences. */
149             return;
150         }
151         tracked_fds_space = new_space;
152     }
153
154     tracked_fds[tracked_fds_count++] = fd;
155
156 #ifdef DEBUG
157     tracked_fds_debug();
158 #endif
159 }
160 static int tracked_fds_remove(int fd) {
161     size_t i;
162     for (i = 0; i < tracked_fds_count; i++) {
163         if (fd != tracked_fds[i]) {
164             continue;
165         }
166
167         memmove(tracked_fds + i, tracked_fds + i + 1,
168                 sizeof(*tracked_fds) * (tracked_fds_count - i - 1));
169         tracked_fds_count--;
170
171 #ifdef DEBUG
172         tracked_fds_debug();
173 #endif
174
175         /* Found. */
176         return 1;
177     }
178
179     /* Not found. */
180     return 0;
181 }
182 static int tracked_fds_find(int fd) {
183     size_t i;
184     for (i = 0; i < tracked_fds_count; i++) {
185         if (fd == tracked_fds[i]) {
186             return 1;
187         }
188     }
189     return 0;
190 }
191
192 #endif