]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blob - src/trackfds.h
Add warning() and use it in DEBUG mode.
[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_list;
25 /* Current number of items in the list. */
26 static size_t tracked_fds_list_count;
27 /* Allocated items, used to reduce realloc()s. */
28 static size_t tracked_fds_list_space;
29
30
31 #ifdef DEBUG
32 static void tracked_fds_debug(void) {
33     debug("    tracked_fds_list: %d/%d\t\t[%d]\n", tracked_fds_list_count,
34                                                    tracked_fds_list_space,
35                                                    getpid());
36     size_t i;
37     for (i = 0; i < tracked_fds_list_count; i++) {
38         debug("    tracked_fds_list[%d]: %d\n", i, tracked_fds_list[i]);
39     }
40 }
41 #endif
42
43 /* Load tracked file descriptors from the environment. The environment is used
44  * to pass the information to child processes.
45  *
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) {
49 #ifdef DEBUG
50     debug("init_from_environment()\t\t[%d]\n", getpid());
51 #endif
52     const char *env;
53
54     initialized = 1;
55     tracked_fds_list_count = 0;
56
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;
62     }
63
64     env = getenv(ENV_NAME_FDS);
65     if (!env) {
66         return;
67     }
68     /* Environment is read-only. */
69     char env_copy[strlen(env) + 1];
70     strcpy(env_copy, env);
71
72     char *x;
73
74     size_t count = 0;
75     for (x = env_copy; *x; x++) {
76         if (*x == ',') {
77             count++;
78         }
79     }
80     tracked_fds_list_space = count + TRACKFDS_REALLOC_STEP;
81
82     tracked_fds_list = malloc(tracked_fds_list_space * sizeof(*tracked_fds_list));
83     if (!tracked_fds_list) {
84 #ifdef DEBUG
85         warning("malloc(%zu): failed [%d]\n",
86                 tracked_fds_list_space * sizeof(*tracked_fds_list), getpid());
87 #endif
88         return;
89     }
90
91     size_t i = 0;
92
93     /* Parse file descriptor numbers from environment string and store them as
94      * integers in tracked_fds_list. */
95     char *last;
96     for (x = env_copy, last = env_copy; *x; x++) {
97         if (*x != ',') {
98             continue;
99         }
100         /* ',' at the beginning or double ',' - ignore. */
101         if (x == last) {
102             last = x + 1;
103             continue;
104         }
105
106         if (i == count) {
107             break;
108         }
109
110         *x = 0;
111         tracked_fds_list[i++] = atoi(last);
112
113         last = x + 1;
114     }
115
116     tracked_fds_list_count = count;
117
118 #ifdef DEBUG
119     tracked_fds_debug();
120 #endif
121 }
122
123 static void update_environment_buffer(char *x) {
124     size_t i;
125     for (i = 0; i < tracked_fds_list_count; i++) {
126         int length = snprintf(x, 10 + 1, "%d", tracked_fds_list[i]);
127         if (length >= 10 + 1) {
128             /* Integer too big to fit the buffer, skip it. */
129 #ifdef DEBUG
130             warning("update_environment_buffer_entry(): truncated fd: %d [%d]\n",
131                     tracked_fds_list[i], getpid());
132 #endif
133             continue;
134         }
135
136         /* Write comma after number. */
137         x += length;
138         *x++ = ',';
139         /* Make sure the string is always zero terminated. */
140         *x = 0;
141     }
142 }
143 inline static size_t update_environment_buffer_size(void) {
144     /* An integer (32-bit) has at most 10 digits, + 1 for the comma after each
145      * number. Bigger file descriptors (which shouldn't occur in reality) are
146      * skipped. */
147     return tracked_fds_list_count * (10 + 1) + 1 /* to fit '\0' */;
148 }
149 static void update_environment(void) {
150 #ifdef DEBUG
151     debug("update_environment()\t\t[%d]\n", getpid());
152 #endif
153
154     /* If we haven't parsed the environment we also haven't modified it - so
155      * nothing to do. */
156     if (!initialized) {
157         return;
158     }
159
160     char env[update_environment_buffer_size()];
161     env[0] = 0;
162
163     update_environment_buffer(env);
164
165 #if 0
166     debug("    setenv('%s', '%s', 1)\n", ENV_NAME_FDS, env);
167 #endif
168
169     setenv(ENV_NAME_FDS, env, 1 /* overwrite */);
170 }
171
172
173
174 static void tracked_fds_add(int fd) {
175     if (tracked_fds_list_count >= tracked_fds_list_space) {
176         size_t new_space = tracked_fds_list_space + TRACKFDS_REALLOC_STEP;
177         int *tmp = realloc(tracked_fds_list,
178                            sizeof(*tracked_fds_list) * new_space);
179         if (!tmp) {
180             /* We can do nothing, just ignore the error. We made sure not to
181              * destroy our state, so the new descriptor is ignored without any
182              * other consequences. */
183 #ifdef DEBUG
184             warning("realloc(tracked_fds_list, %zu) failed! [%d]\n",
185                     sizeof(*tracked_fds_list) * new_space, getpid());
186 #endif
187             return;
188         }
189         tracked_fds_list = tmp;
190         tracked_fds_list_space = new_space;
191     }
192
193     tracked_fds_list[tracked_fds_list_count++] = fd;
194
195 #ifdef DEBUG
196     debug("tracked_fds_add(): %-3d\t\t[%d]\n", fd, getpid());
197     tracked_fds_debug();
198 #endif
199 }
200 static int tracked_fds_remove(int fd) {
201     size_t i;
202     for (i = 0; i < tracked_fds_list_count; i++) {
203         if (fd != tracked_fds_list[i]) {
204             continue;
205         }
206
207         memmove(tracked_fds_list + i, tracked_fds_list + i + 1,
208                 sizeof(*tracked_fds_list) * (tracked_fds_list_count - i - 1));
209         tracked_fds_list_count--;
210
211 #ifdef DEBUG
212         debug("tracked_fds_remove(): %-3d\t[%d]\n", fd, getpid());
213         tracked_fds_debug();
214 #endif
215
216         /* Found. */
217         return 1;
218     }
219
220     /* Not found. */
221     return 0;
222 }
223 static int tracked_fds_find(int fd) {
224     size_t i;
225     for (i = 0; i < tracked_fds_list_count; i++) {
226         if (fd == tracked_fds_list[i]) {
227             return 1;
228         }
229     }
230     return 0;
231 }
232
233 #endif