forked from wryun/es-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fd.c
224 lines (199 loc) · 4.92 KB
/
fd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* fd.c -- file descriptor manipulations ($Revision: 1.2 $) */
#include "es.h"
/* mvfd -- duplicate a fd and close the old */
extern void mvfd(int old, int new) {
if (old != new) {
int fd = dup2(old, new);
if (fd == -1)
fail("es:mvfd", "dup2: %s", esstrerror(errno));
assert(fd == new);
close(old);
}
}
/*
* deferred file descriptor operations
* we maintain a stack of file descriptor operations as they occur,
* if we're operating in the context of a parent shell. (if we're
* already in a forked process, we just do them.) the operations
* are actually done at closefds() time.
*/
typedef struct {
int realfd, userfd;
} Defer;
static Defer *deftab;
static int defcount = 0, defmax = 0;
static void dodeferred(int realfd, int userfd) {
assert(userfd >= 0);
releasefd(userfd);
if (realfd == -1)
close(userfd);
else {
assert(realfd >= 0);
mvfd(realfd, userfd);
}
}
static int pushdefer(Boolean parent, int realfd, int userfd) {
if (parent) {
Defer *defer;
if (defcount >= defmax) {
int i;
for (i = 0; i < defcount; i++)
unregisterfd(&deftab[i].realfd);
defmax += 10;
deftab = erealloc(deftab, defmax * sizeof (Defer));
for (i = 0; i < defcount; i++)
registerfd(&deftab[i].realfd, TRUE);
}
defer = &deftab[defcount++];
defer->realfd = realfd;
defer->userfd = userfd;
registerfd(&defer->realfd, TRUE);
return defcount - 1;
} else {
dodeferred(realfd, userfd);
return UNREGISTERED;
}
}
extern int defer_mvfd(Boolean parent, int old, int new) {
assert(old >= 0);
assert(new >= 0);
return pushdefer(parent, old, new);
}
extern int defer_close(Boolean parent, int fd) {
assert(fd >= 0);
return pushdefer(parent, -1, fd);
}
extern void undefer(int ticket) {
if (ticket != UNREGISTERED) {
Defer *defer;
assert(ticket >= 0);
assert(defcount > 0);
defer = &deftab[--defcount];
assert(ticket == defcount);
unregisterfd(&defer->realfd);
if (defer->realfd != -1)
close(defer->realfd);
}
}
/* fdmap -- turn a deferred (user) fd into a real fd */
extern int fdmap(int fd) {
int i = defcount;
while (--i >= 0) {
Defer *defer = &deftab[i];
if (fd == defer->userfd) {
fd = defer->realfd;
if (fd == -1)
return -1;
}
}
return fd;
}
/* remapfds -- apply the fd map to the current file descriptor table */
static void remapfds(void) {
Defer *defer, *defend = &deftab[defcount];
for (defer = deftab; defer < defend; defer++) {
unregisterfd(&defer->realfd);
dodeferred(defer->realfd, defer->userfd);
}
defcount = 0;
}
/*
* the registered file descriptor list
* this is actually a list of pointers to file descriptors.
* when we start to work with a user-defined fd, we scan
* this list (releasefds) and it an entry matches the user
* defined one, we dup it to a different number. when we fork,
* we close all the descriptors on this list.
*/
typedef struct {
int *fdp;
Boolean closeonfork;
} Reserve;
static Reserve *reserved = NULL;
static int rescount = 0, resmax = 0;
/* registerfd -- reserve a file descriptor for es */
extern void registerfd(int *fdp, Boolean closeonfork) {
#if ASSERTIONS
int i;
for (i = 0; i < rescount; i++)
assert(fdp != reserved[i].fdp);
#endif
if (rescount >= resmax) {
resmax += 10;
reserved = erealloc(reserved, resmax * sizeof (Reserve));
}
reserved[rescount].fdp = fdp;
reserved[rescount].closeonfork = closeonfork;
rescount++;
}
/* unregisterfd -- give up our hold on a file descriptor */
extern void unregisterfd(int *fdp) {
int i;
assert(reserved != NULL);
assert(rescount > 0);
for (i = 0; i < rescount; i++)
if (reserved[i].fdp == fdp) {
reserved[i] = reserved[--rescount];
return;
}
panic("%x not on file descriptor reserved list", fdp);
}
/* closefds -- close file descriptors after a fork() */
extern void closefds(void) {
int i;
remapfds();
for (i = 0; i < rescount; i++) {
Reserve *r = &reserved[i];
if (r->closeonfork) {
int fd = *r->fdp;
if (fd >= 3)
close(fd);
*r->fdp = -1;
}
}
}
/* releasefd -- release a specific file descriptor from its es uses */
extern void releasefd(int n) {
int i;
assert(n >= 0);
for (i = 0; i < rescount; i++) {
int *fdp = reserved[i].fdp;
int fd = *fdp;
if (fd == n) {
*fdp = dup(fd);
if (*fdp == -1) {
assert(errno != EBADF);
fail("es:releasefd", "%s", esstrerror(errno));
}
close(fd);
}
}
}
/* isdeferred -- is this file descriptor on the deferral list */
static Boolean isdeferred(int fd) {
Defer *defer, *defend = &deftab[defcount];
for (defer = deftab; defer < defend; defer++)
if (defer->userfd == fd)
return TRUE;
return FALSE;
}
/* newfd -- return a new, free file descriptor */
extern int newfd(void) {
int i;
for (i = 3;; i++)
if (!isdeferred(i)) {
int fd = dup(i);
if (fd == -1) {
if (errno != EBADF)
fail("$&newfd", "newfd: %s", esstrerror(errno));
return i;
} else if (isdeferred(fd)) {
int n = newfd();
close(fd);
return n;
} else {
close(fd);
return fd;
}
}
}