-
Notifications
You must be signed in to change notification settings - Fork 0
/
wtserver.c
196 lines (158 loc) · 3.4 KB
/
wtserver.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
/* wtserver.c
* truefinder [email protected]
*/
#include <errno.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <assert.h>
#include <stdio.h>
#include <setjmp.h>
#include <signal.h>
#include "ptypair.h"
#define TMPFILE "/tmp/local.sock"
#define BUFSIZE 4096
jmp_buf env ;
sigjmp_buf sigenv;
static int sfd , cfd ;
void
close_domain(void)
{
#ifdef DEBUGZ
printf("DEBUG : server closed\n");
#endif
unlink(TMPFILE);
}
void
sig_handler( int signum )
{
if ( signum == SIGALRM ) {
#ifdef DEBUGZ
printf("DEBUG : alarm\n");
#endif
close(cfd) ;
siglongjmp( sigenv, 1 );
}
if ( signum == SIGINT ) {
exit(0);
}
}
int
main( void )
{
int master ; int pid ;
char *name ; int i ;
char ch = 0;
char buf[BUFSIZE];
struct termios ot , t ;
fd_set ready;
int done = 0;
int pipe;
FILE *fp1, *fp2 ;
int flag = 1;
//socket
struct sockaddr_un sa, ca;
int slen, clen ;
//signal
sigset_t sigs;
struct sigaction sa_old;
struct sigaction sa_new ;
int mask;
//timer
struct sigaction siga;
struct itimerval timer;
master = get_master_pty(&name);
if ( master < 0 ) {
perror("ptypair: master open failed");
exit(1);
}
pid = fork() ;
if ( pid == 0 ) { //child
int slave ;
close(master);
slave = get_slave_pty(name);
if ( slave < 0 ) {
perror("ptypair: slave open failed");
exit(1);
}
free(name);
setsid();
ioctl(slave, TIOCSCTTY, NULL );
dup2(slave, STDIN_FILENO );
dup2(slave, STDOUT_FILENO );
dup2(slave, STDERR_FILENO );
execl("/bin/sh", "sh", 0 );
exit(1);
}
//parent
unlink(TMPFILE);
atexit( close_domain);
free(name);
fp1 = fdopen( master, "r");
/*
tcgetattr( master, &ot );
t = ot ;
t.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOCTL | ECHOE |
ECHOK | ECHOKE | ECHONL | ECHOPRT );
t.c_iflag |= IGNBRK;
t.c_cc[VMIN] = 1;
t.c_cc[VTIME] = 0;
tcsetattr( master, TCSANOW, &t);
*/
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = 250000;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 250000;
signal( SIGALRM, sig_handler );
signal( SIGINT, sig_handler );
signal( SIGPIPE, SIG_IGN);
sfd = socket( AF_UNIX, SOCK_STREAM, 0 );
if ( sfd < 0 ) { perror("socket"); exit(-1); }
sa.sun_family = AF_UNIX;
strcpy( sa.sun_path, TMPFILE);
slen = sizeof( sa.sun_family) + sizeof(sa.sun_path);
bind( sfd, (struct sockaddr *) &sa, slen );
listen( sfd, 5 );
do {
sigsetjmp( sigenv, 1 );
cfd = accept( sfd, (struct sockaddr*) &sa, &slen );
#ifdef DEBUGZ
pritnf("DEBUG : accept client");
#endif
read( cfd, &ch , 1 );
write( master, &ch, 1 );
//alarm(1);
setitimer( ITIMER_REAL, &timer, NULL );
while(1){
bzero( buf, sizeof(buf));
i = read( master , buf, sizeof(buf)) ;
if ( i <= 0 ) {
close( cfd ); break;
#ifdef DEBUGZ
if ( i == 0 ) printf("it's i = 0 \n");
else printf("it's i < 0\n");
#endif
}
write(cfd, buf, i);
#ifdef DEBUGZ
printf("DEBUG : buf = [%s]", buf );
#endif
}
close( cfd );
printf("DEBUG : sucked miss operation\n");
} while (!done);
/* this really doesn't matter because each time a master pty is
** opened, the corresponding slave pty has its termios settings
** reset
**/
//tcsetattr(STDIN_FILENO, TCSANOW, &ot);
unlink(TMPFILE);
exit(0);
}