forked from haiyuidesu/serialsh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialsh.m
140 lines (96 loc) · 2.82 KB
/
serialsh.m
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
#include <unistd.h>
#include <termios.h>
#import <sys/event.h>
#import <Foundation/Foundation.h>
pid_t pd = 0;
int pipe_shin[2], pipe_shout[2];
int attribs(int fd, int baudrate) {
struct termios tty;
memset(&tty, 0x0, sizeof(tty));
if (tcgetattr(fd, &tty) != 0) return -1;
cfsetispeed(&tty, baudrate);
cfsetospeed(&tty, baudrate);
tty.c_cflag &= ~CRTSCTS;
tty.c_cflag |= (CLOCAL | CREAD);
tty.c_iflag |= IGNPAR;
tty.c_iflag &= ~(IXON | IXOFF | INLCR | IGNCR);
tty.c_oflag &= ~OPOST;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~PARENB;
tty.c_iflag &= ~INPCK;
tty.c_cflag &= ~CSTOPB;
tty.c_iflag |= INPCK;
tty.c_cc[VTIME] = 1;
tty.c_cc[VMIN] = 0;
if (tcsetattr(fd, TCSANOW, &tty) != 0) return -1;
return 0;
}
#define spawn_shin pipe_shin[1]
#define main_sherr pipe_shout[1]
void block_attribs(int fd, int block_now) {
struct termios tty;
memset(&tty, 0x0, sizeof(tty));
if (tcgetattr(fd, &tty) != 0) return;
tty.c_cc[VMIN] = block_now ? 1 : 0;
tty.c_cc[VTIME] = 5;
tcsetattr(fd, TCSANOW, &tty);
}
int kq = 0;
void spawnproc(void) {
if (pd == 0) {
pd = fork();
if (pd == 0) {
dup2(pipe_shin[0], 0);
dup2(main_sherr, 1);
dup2(main_sherr, 2);
char *paramList[] = { "createpty", "-q", "/dev/null", "login" };
execv("/usr/bin/script", paramList);
exit(0);
}
struct kevent ke;
EV_SET(&ke, pd, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
kevent(kq, &ke, 1, NULL, 0, NULL);
}
}
int main(void) {
if (fork() == 0) {
/* it will works the same if you open /dev/uart.debug-console */
int fd = open("/dev/tty.debug-console", O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK);
if (!(fd < 0)) {
attribs(fd, 115200);
block_attribs(fd, 0);
write(fd, "\r\n[serialsh]: initialized from the userland!\r\n", 46);
pipe(pipe_shin);
pipe(pipe_shout);
dup2(pipe_shout[0], 0);
dup2(spawn_shin, 1);
dup2(spawn_shin, 2);
kq = kqueue();
assert(kq != -1);
struct kevent ke;
char buf[0x400];
EV_SET(&ke, fd, EVFILT_READ, EV_ADD, 0, 5, NULL);
kevent(kq, &ke, 1, NULL, 0, NULL);
EV_SET(&ke, 0, EVFILT_READ, EV_ADD, 0, 5, NULL);
kevent(kq, &ke, 1, NULL, 0, NULL);
spawnproc();
while (1) {
EV_SET(&ke, 0, 0, 0, 0, 0, NULL);
int uwu = kevent(kq, NULL, 0, &ke, 1, NULL);
if (uwu == 0) continue;
if (ke.ident == fd) {
int rd = read(fd, buf, 0x400);
write(1, buf, rd);
} else if (ke.ident == 0) {
int rd = read(0, buf, 0x400);
write(fd, buf, rd);
} else if ((ke.filter == -5) && (ke.ident == pd)) {
waitpid(pd, NULL, 0x0);
pd = 0;
spawnproc();
}
}
}
}
}