-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.c
More file actions
206 lines (183 loc) · 5.14 KB
/
interface.c
File metadata and controls
206 lines (183 loc) · 5.14 KB
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
196
197
198
199
200
201
202
203
204
#include <stdio.h>
#include <sys/stat.h> /* for mkfifo */
#include <sys/types.h> /* for open */
#include <sys/stat.h> /* for open */
#include <fcntl.h> /* for open */
#include <unistd.h> /* for write */
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#define BUFFSIZE 8192
#define MaxPipeName 100
#define logdir "/home/ruttan/classes/capstone/debugger/"
#define numDbgArgs 5
#define ArgSize 30
pid_t child_pid;
char buf[BUFFSIZE];
FILE * fdtoFILE;
void catch_sigpipe(int sig_num)
{
/* re-set the signal handler again to catch_int, for next time */
signal(SIGPIPE, catch_sigpipe);
fprintf(fdtoFILE,"got a signal\n");
fflush(fdtoFILE);
}
void catch_sigint(int sig_num)
{
/* re-set the signal handler again to catch_int, for next time */
signal(SIGINT, catch_sigint);
kill(child_pid,1);
fprintf(fdtoFILE,"got a sigint signal\n");
fflush(fdtoFILE);
sleep(1);
exit(1);
}
pid_t launch_debugger(char * argv[],int pipefd[2]){
int stdio[3];
pid_t child_pid;
int pipefd0[2],pipefd1[2];
pipe(pipefd0);
if(pipe(pipefd0) == -1) {
perror("Pipe 0 creation failed");
exit(1);
}
pipe(pipefd1);
if(pipe(pipefd1) == -1) {
perror("Pipe 1 creation failed");
exit(1);
}
//create function to setup pipes fork and exec
pipefd[0]=pipefd0[0];
pipefd[1]=pipefd1[1];
stdio[0]=dup(STDIN_FILENO);
stdio[1]=dup(STDOUT_FILENO);
stdio[2]=dup(STDERR_FILENO);
dup2(pipefd1[0],STDIN_FILENO);
dup2(pipefd0[1],STDOUT_FILENO);
dup2(pipefd0[1],STDERR_FILENO);
switch (child_pid=fork()){
case -1:
perror("Could not fork debugger");
exit(1);
break;
case 0:
if (execvp(argv[0],argv)==-1){
perror("exec failed");
exit(1);
}
default:
//after fork in parent.
dup2(stdio[0],STDIN_FILENO);
dup2(stdio[1],STDOUT_FILENO);
dup2(stdio[2],STDERR_FILENO);
close(pipefd1[0]);
close(pipefd0[1]);
}
return(child_pid);
}
void make_args(char * id,char * argv[]){
argv[0]="/usr/bin/python";
argv[1]="-i";
argv[2]="-c";
argv[3]="\"print 'Starting'\"";
argv[4]=0;
}
int main (int argc, char ** argv) {
int n;
char * nargv[numDbgArgs];
int fdtoWeb,fdfrmWeb,fdtoDbg,fdfrmDgb,pipefd[2];
char line[BUFFSIZE],toServer[MaxPipeName],fromServer[MaxPipeName];
sprintf(buf,"%s%s%s",logdir,"FILE",argv[1]);
unlink(buf);
fdtoFILE=fopen(buf,"w");
fprintf(fdtoFILE,"testing file output\n");
signal(SIGPIPE, catch_sigpipe);
signal(SIGINT, catch_sigint);
if ((argc < 2 )|| (argc >=3)){
perror("usage: debugger idcode");
exit(1);
}
//create input pipe from webserver
sprintf(fromServer,"%s%s%s",logdir,"OUT",argv[1]);
unlink(fromServer);
if (-1 == mkfifo(fromServer,S_IRWXU)) {
perror("Error, could not make fifo\n");
}
//create output pipe to webserver
sprintf(toServer,"%s%s%s",logdir,"IN",argv[1]);
unlink(toServer);
if (-1 == mkfifo(toServer,S_IRWXU)) {
perror("Error, could not make fifo\n");
}
if ((fdfrmWeb = open(fromServer,O_RDONLY)) == -1) {
perror("cannot open fifo from server");
exit(1);
}
if ((fdtoWeb = open(toServer,O_WRONLY)) == -1) {
perror("cannot open fifo to server");
exit(1);
}
//create pipes to debugger
if(pipe(pipefd) == -1) { //perhaps pipe(pipefd,O_NONBLOCK)
perror("Pipe 1 creation failed");
exit(1);
}
int i;
char c;
make_args(argv[1],nargv);
child_pid=launch_debugger(nargv,pipefd);
fprintf(fdtoFILE,"child_pid=%d\n",child_pid);
fflush(fdtoFILE);
setpgid(0, 0);
fprintf(fdtoFILE,"---starting---\n");
fflush(fdtoFILE);
while(1){
line[0]=0;
while (!strstr(line,">>>") && !strstr(line,"...")){
if ((n=read(pipefd[0],line,BUFFSIZE))){
if (!n) {
fprintf(fdtoFILE,"pipe fd[0] closed");
fflush(fdtoFILE);
exit(1);
}
//line[n++]='@';
line[n]=0;
//if n=BUFFSIZE...
}
fprintf(fdtoFILE,"p-%s",line);
fflush(fdtoFILE);
if (write(fdtoWeb,line,n) != n) {
perror("fdtoWeb write error");
fprintf(fdtoFILE,"fdtoWeb write error\n");
fflush(fdtoFILE);
}
}
if ((n=read(fdfrmWeb,line,BUFFSIZE))){
line[n]=0;
fprintf(fdtoFILE,"%d-%s%s",n,"w-",line);
fflush(fdtoFILE);
if (write(pipefd[1],line,n) != n) {
perror("pipefd[1] write error");
}
}
else{
printf("empty read\n");
close(fdtoWeb);
close(fdfrmWeb);
n=0;
if ((fdfrmWeb = open(fromServer,O_RDONLY)) == -1) {
perror("cannot open fifo from server");
exit(1);
}
if ((fdtoWeb = open(toServer,O_WRONLY)) == -1) {
perror("cannot open fifo to server");
exit(1);
}
line[0]='\n';
if (write(pipefd[1],line,1) != 1) {
perror("pipefd[1] write error");
}
}
}
exit(0);
}