-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
150 lines (133 loc) · 5.58 KB
/
main.cpp
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
/*
_____ _____ _____ _____ _____ _____ _____
/\ \ /\ \ /\ \ /\ \ /\ \ /\ \ /\ \
/::\____\ /::\ \ /::\ \ /::\____\ /::\ \ /::\____\ /::\ \
/:::/ / /::::\ \ /::::\ \ /::::| | \:::\ \ /::::| | /::::\ \
/:::/ _/___ /::::::\ \ /::::::\ \ /:::::| | \:::\ \ /:::::| | /::::::\ \
/:::/ /\ \ /:::/\:::\ \ /:::/\:::\ \ /::::::| | \:::\ \ /::::::| | /:::/\:::\ \
/:::/ /::\____\ /:::/__\:::\ \ /:::/__\:::\ \ /:::/|::| | \:::\ \ /:::/|::| | /:::/ \:::\ \
/:::/ /:::/ / /::::\ \:::\ \ /::::\ \:::\ \ /:::/ |::| | /::::\ \ /:::/ |::| | /:::/ \:::\ \
/:::/ /:::/ _/___ /::::::\ \:::\ \ /::::::\ \:::\ \ /:::/ |::| | _____ ____ /::::::\ \ /:::/ |::| | _____ /:::/ / \:::\ \
/:::/___/:::/ /\ \ /:::/\:::\ \:::\ \ /:::/\:::\ \:::\____\ /:::/ |::| |/\ \ /\ \ /:::/\:::\ \ /:::/ |::| |/\ \ /:::/ / \:::\ ___\
|:::| /:::/ /::\____\/:::/ \:::\ \:::\____\/:::/ \:::\ \:::| |/:: / |::| /::\____\/::\ \/:::/ \:::\____\/:: / |::| /::\____\/:::/____/ ___\:::| |
|:::|__/:::/ /:::/ /\::/ \:::\ /:::/ /\::/ |::::\ /:::|____|\::/ /|::| /:::/ /\:::\ /:::/ \::/ /\::/ /|::| /:::/ /\:::\ \ /\ /:::|____|
\:::\/:::/ /:::/ / \/____/ \:::\/:::/ / \/____|:::::\/:::/ / \/____/ |::| /:::/ / \:::\/:::/ / \/____/ \/____/ |::| /:::/ / \:::\ /::\ \::/ /
\::::::/ /:::/ / \::::::/ / |:::::::::/ / |::|/:::/ / \::::::/ / |::|/:::/ / \:::\ \:::\ \/____/
\::::/___/:::/ / \::::/ / |::|\::::/ / |::::::/ / \::::/____/ |::::::/ / \:::\ \:::\____\
\:::\__/:::/ / /:::/ / |::| \::/____/ |:::::/ / \:::\ \ |:::::/ / \:::\ /:::/ /
\::::::::/ / /:::/ / |::| ~| |::::/ / \:::\ \ |::::/ / \:::\/:::/ /
\::::::/ / /:::/ / |::| | /:::/ / \:::\ \ /:::/ / \::::::/ /
\::::/ / /:::/ / \::| | /:::/ / \:::\____\ /:::/ / \::::/ /
\::/____/ \::/ / \:| | \::/ / \::/ / \::/ / \::/____/
~~ \/____/ \|___| \/____/ \/____/ \/____/
This is left unused because the java process now use the pty4j lib.
I left it here because it might be useful to someone one day.
*/
#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <cstddef>
#include <string.h>
#include <thread>
#include <sys/wait.h>
using namespace std;
bool running = false;
void isRunning(int PID)
{
while (running)
{
int status;
pid_t result = waitpid(PID, &status, WNOHANG);
if (result == 0)
{ // Child still alive
running = true;
}
else
{ // Child exited
running = false;
}
}
}
void readGmod(int fd) {
char buffer[1024];
while (running)
{
memset(buffer, 0, sizeof buffer);
int result = read(fd, buffer, sizeof buffer);
if (result > 0)
{
cout << buffer;
}
}
}
int main(int argc, char** argv)
{
int fdm, fds;
char* slavename;
fdm = posix_openpt(O_RDWR);
if (fdm < 0)
{
cout << "posix_openpt error" << endl;
}
else
{
//cout << "fdm : " << fdm << endl;
if (grantpt(fdm) != 0)
{
cout << "grantpt error" << endl;
}
else
{
if (unlockpt(fdm) != 0)
{
cout << "unlockpt error" << endl;
}
else
{
slavename = ptsname(fdm);
if (slavename == NULL)
{
cout << "ptsname error" << endl;
}
else
{
//cout << "slavename : " << slavename << endl;
int PID = fork();
if (PID == 0)
{
//child
close(0);
close(1);
close(2);
close(fdm);
int fds = open(slavename, O_RDWR);
dup2(fds, 0);
dup2(fds, 1);
dup2(fds, 2);
execvp("./srcds_run", argv);
} else {
running = true;
std:thread checkRunning (isRunning, PID);
thread scanGmod (readGmod, fdm);
while (running)
{
string gmodCmd;
getline(cin, gmodCmd);
gmodCmd += "\n";
char buffer[gmodCmd.length() + 1];
strcpy (buffer, gmodCmd.c_str());
write(fdm, buffer, gmodCmd.length() + 1);
}
scanGmod.join();
checkRunning.join();
}
}
}
}
}
return 0;
}