projects
/
musl
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
document self-synchronized destruction issue for stdio locking
[musl]
/
src
/
misc
/
forkpty.c
diff --git
a/src/misc/forkpty.c
b/src/misc/forkpty.c
index
2d1b0ae
..
07f8d01
100644
(file)
--- a/
src/misc/forkpty.c
+++ b/
src/misc/forkpty.c
@@
-1,21
+1,37
@@
#include <pty.h>
#include <unistd.h>
#include <pty.h>
#include <unistd.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
int forkpty(int *m, char *name, const struct termios *tio, const struct winsize *ws)
{
int forkpty(int *m, char *name, const struct termios *tio, const struct winsize *ws)
{
- int s;
+ int s
, t, i, istmp[3]={0}
;
pid_t pid;
if (openpty(m, &s, name, tio, ws) < 0) return -1;
pid_t pid;
if (openpty(m, &s, name, tio, ws) < 0) return -1;
- pid = fork();
+
+ /* Ensure before forking that we don't exceed fd limit */
+ for (i=0; i<3; i++) {
+ if (fcntl(i, F_GETFL) < 0) {
+ t = fcntl(s, F_DUPFD, i);
+ if (t<0) break;
+ else if (t!=i) close(t);
+ else istmp[i] = 1;
+ }
+ }
+ pid = i==3 ? fork() : -1;
if (!pid) {
close(*m);
if (!pid) {
close(*m);
+ setsid();
+ ioctl(s, TIOCSCTTY, (char *)0);
dup2(s, 0);
dup2(s, 1);
dup2(s, 2);
dup2(s, 0);
dup2(s, 1);
dup2(s, 2);
- close(s);
+
if (s>2)
close(s);
return 0;
}
return 0;
}
+ for (i=0; i<3; i++)
+ if (istmp[i]) close(i);
close(s);
if (pid < 0) close(*m);
return pid;
close(s);
if (pid < 0) close(*m);
return pid;