Skip to content

Commit 43de12d

Browse files
committed
Introduce forkpty()
1 parent fa20edc commit 43de12d

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

libc/BUILD.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ libc/isystem/pmmintrin.h \
147147
libc/isystem/poll.h \
148148
libc/isystem/popcntintrin.h \
149149
libc/isystem/pthread.h \
150+
libc/isystem/pty.h \
150151
libc/isystem/pwd.h \
151152
libc/isystem/queue \
152153
libc/isystem/random \

libc/isystem/pty.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef _PTY_H
2+
#define _PTY_H
3+
#include "libc/calls/termios.h"
4+
#include "libc/calls/weirdtypes.h"
5+
#include "libc/sysv/consts/baud.internal.h"
6+
#include "libc/sysv/consts/termios.h"
7+
#endif /* _PTY_H */

third_party/musl/BUILD.mk

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ THIRD_PARTY_MUSL_A_OBJS = \
1818

1919
THIRD_PARTY_MUSL_A_DIRECTDEPS = \
2020
LIBC_CALLS \
21-
LIBC_INTRIN \
2221
LIBC_FMT \
22+
LIBC_INTRIN \
2323
LIBC_MEM \
2424
LIBC_NEXGEN32E \
25+
LIBC_PROC \
2526
LIBC_RUNTIME \
2627
LIBC_STDIO \
2728
LIBC_STR \
2829
LIBC_SYSV \
30+
LIBC_THREAD \
2931
THIRD_PARTY_ZLIB
3032

3133
THIRD_PARTY_MUSL_A_DEPS := \

third_party/musl/forkpty.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
2+
│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│
3+
╚──────────────────────────────────────────────────────────────────────────────╝
4+
│ │
5+
│ Musl Libc │
6+
│ Copyright © 2005-2014 Rich Felker, et al. │
7+
│ │
8+
│ Permission is hereby granted, free of charge, to any person obtaining │
9+
│ a copy of this software and associated documentation files (the │
10+
│ "Software"), to deal in the Software without restriction, including │
11+
│ without limitation the rights to use, copy, modify, merge, publish, │
12+
│ distribute, sublicense, and/or sell copies of the Software, and to │
13+
│ permit persons to whom the Software is furnished to do so, subject to │
14+
│ the following conditions: │
15+
│ │
16+
│ The above copyright notice and this permission notice shall be │
17+
│ included in all copies or substantial portions of the Software. │
18+
│ │
19+
│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │
20+
│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │
21+
│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │
22+
│ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │
23+
│ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │
24+
│ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │
25+
│ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │
26+
│ │
27+
╚─────────────────────────────────────────────────────────────────────────────*/
28+
#include "libc/calls/weirdtypes.h"
29+
#include "libc/calls/struct/sigset.h"
30+
#include "libc/sysv/consts/sig.h"
31+
#include "libc/calls/struct/sigset.h"
32+
#include "libc/thread/thread.h"
33+
#include "libc/calls/calls.h"
34+
#include "libc/sysv/consts/o.h"
35+
#include "libc/runtime/runtime.h"
36+
#include "libc/errno.h"
37+
#include "libc/calls/termios.h"
38+
39+
asm(".ident\t\"\\n\\n\
40+
Musl libc (MIT License)\\n\
41+
Copyright 2005-2014 Rich Felker, et. al.\"");
42+
asm(".include \"libc/disclaimer.inc\"");
43+
44+
int forkpty(int *pm, char *name, const struct termios *tio, const struct winsize *ws)
45+
{
46+
int m, s, ec=0, p[2], cs;
47+
pid_t pid=-1;
48+
sigset_t set, oldset;
49+
50+
if (openpty(&m, &s, name, tio, ws) < 0) return -1;
51+
52+
sigfillset(&set);
53+
pthread_sigmask(SIG_BLOCK, &set, &oldset);
54+
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
55+
56+
if (pipe2(p, O_CLOEXEC)) {
57+
close(s);
58+
goto out;
59+
}
60+
61+
pid = fork();
62+
if (!pid) {
63+
close(m);
64+
close(p[0]);
65+
if (login_tty(s)) {
66+
write(p[1], &errno, sizeof errno);
67+
_exit(127);
68+
}
69+
close(p[1]);
70+
pthread_setcancelstate(cs, 0);
71+
pthread_sigmask(SIG_SETMASK, &oldset, 0);
72+
return 0;
73+
}
74+
close(s);
75+
close(p[1]);
76+
if (read(p[0], &ec, sizeof ec) > 0) {
77+
int status;
78+
waitpid(pid, &status, 0);
79+
pid = -1;
80+
errno = ec;
81+
}
82+
close(p[0]);
83+
84+
out:
85+
if (pid > 0) *pm = m;
86+
else close(m);
87+
88+
pthread_setcancelstate(cs, 0);
89+
pthread_sigmask(SIG_SETMASK, &oldset, 0);
90+
91+
return pid;
92+
}

0 commit comments

Comments
 (0)