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