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