-
Notifications
You must be signed in to change notification settings - Fork 0
/
spawn.c
225 lines (222 loc) · 5.45 KB
/
spawn.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "spawn.h"
#include <windows.h>
#include <stdarg.h>
#include <stdio.h>
#define MAX_PIPE_BUF 4096
struct _spawn_pipe_
{
HANDLE in;
HANDLE out;
int cur_;
int size_;
char buf[MAX_PIPE_BUF];
};
static int get_imp(HANDLE in,char* buf,int max,int* cur)
{
*cur = 0;
DWORD count = 0;
if(!ReadFile(in,buf,max,&count,
NULL) || count == 0) {
return -1;
}
return count;
}
static int get_char(SPAWN_PIPE pipes)
{
int c ;
if(pipes->size_ == -1)
return EOF;
if(pipes->size_ == 0 && (pipes->size_ = get_imp(pipes->in,pipes->buf,
MAX_PIPE_BUF,&pipes->cur_)) == -1 ){
return EOF;
}
c = pipes->buf[pipes->cur_++];
pipes->size_--;
return c;
}
static char* get_line(SPAWN_PIPE pipes,char* buf,int lim)
{
int c;
char* org = buf;
while(--lim && (c = get_char(pipes)) != EOF )
if((*buf++ = c) == '\n')
break;
*buf = '\0';
return (c == EOF && org == buf)?NULL:org;
}
void pipe_closein(SPAWN_PIPE pipes)
{
CloseHandle(pipes->in);
}
void pipe_closeout(SPAWN_PIPE pipes)
{
CloseHandle(pipes->out);
}
int pipe_printf(SPAWN_PIPE pipes,const char* format,...)
{
char buf[MAX_PIPE_BUF] = {0};
unsigned long count = 0;
va_list argList;
va_start(argList, format);
vsprintf(buf,format,argList);
va_end(argList);
WriteFile(pipes->out,buf,strlen(buf),&count,NULL);
return count;
}
char* pipe_getline(SPAWN_PIPE pipes,char* buf,int max_size)
{
return get_line(pipes,buf,max_size);
}
SPAWN_PIPE create_spawnpipe()
{
SPAWN_PIPE pipes = (SPAWN_PIPE)malloc(sizeof(struct _spawn_pipe_));
if(!pipes) return 0;
memset(pipes,0,sizeof(struct _spawn_pipe_));
return pipes;
}
void free_spawnpipe(SPAWN_PIPE pipes)
{
free(pipes);
}
static int create_child(const char* cmd,SPAWN_PIPE pipes)
{
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
// Set up members of the PROCESS_INFORMATION structure.
ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
// Set up members of the STARTUPINFO structure.
ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
siStartInfo.wShowWindow = SW_HIDE;
siStartInfo.hStdInput = pipes->in;
siStartInfo.hStdOutput = pipes->out;
siStartInfo.hStdError = pipes->out;
// Create the child process.
return CreateProcess(NULL,
(LPTSTR)cmd, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION
}
static int dup_pipes(SPAWN_PIPE pipes)
{
int result = 0;
HANDLE temp;
result = DuplicateHandle(GetCurrentProcess(), pipes->in,
GetCurrentProcess(), &temp, 0,
FALSE,
DUPLICATE_SAME_ACCESS);
CloseHandle(pipes->in);
pipes->in = temp;
result = DuplicateHandle(GetCurrentProcess(), pipes->out,
GetCurrentProcess(), &temp, 0,
FALSE,
DUPLICATE_SAME_ACCESS);
CloseHandle(pipes->out);
pipes->out = temp;
return result;
}
int spawn_child(const char* cmd,SPAWN_PIPE pipes)
{
SECURITY_ATTRIBUTES saAttr;
struct _spawn_pipe_ child = {0};
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
if(!CreatePipe(&child.in,&pipes->out,&saAttr,0))
return 0;
if(!CreatePipe(&pipes->in,&child.out,&saAttr,0)){
CloseHandle(child.in);
CloseHandle(pipes->out);
return 0;
}
dup_pipes(pipes);
if(!create_child(cmd,&child))
return 0;
CloseHandle(child.in);
CloseHandle(child.out);
return 1;
}
static int lua_new_pipe(lua_State* L)
{
const char* cmd = lua_tostring(L,1);
//TRACE_OUT("lua_new pipe %s\n",cmd);
SPAWN_PIPE pipes = (SPAWN_PIPE)lua_newuserdata(L,sizeof(struct _spawn_pipe_));
if(!pipes){
//error(L,"*error* : create pipes !\n");
return 0;
}
memset(pipes,0,sizeof(struct _spawn_pipe_));
if(spawn_child(cmd,pipes) == 0){
//error(L,"*error* : spawn_child %s !\n",cmd);
lua_pop(L,1);
free(pipes);
lua_pushnil(L);
return 1;
}
luaL_getmetatable(L,"ge.pipe");
lua_setmetatable(L,-2);
return 1;
}
static int lua_pipe_print(lua_State* L)
{
SPAWN_PIPE pipes = (SPAWN_PIPE)lua_touserdata(L,1);
const char* str = lua_tostring(L,2);
unsigned long count = 0;
BOOL flag = WriteFile(pipes->out,str,strlen(str),&count,NULL);
if(flag)
lua_pushinteger(L,flag);
else
lua_pushnil(L);
return 1;
}
static int lua_pipe_closein(lua_State* L)
{
SPAWN_PIPE pipes = (SPAWN_PIPE)lua_touserdata(L,1);
pipe_closein(pipes);
return 0;
}
static int lua_pipe_closeout(lua_State* L)
{
SPAWN_PIPE pipes = (SPAWN_PIPE)lua_touserdata(L,1);
pipe_closeout(pipes);
return 0;
}
static int lua_pipe_getline(lua_State* L)
{
char* buf = (char*)calloc(MAX_PIPE_BUF,sizeof(char));
SPAWN_PIPE pipes = (SPAWN_PIPE)lua_touserdata(L,1);
if(pipe_getline(pipes,buf,MAX_PIPE_BUF)){
lua_pushstring(L,buf);
}
else{
lua_pushnil(L);
}
free(buf);
return 1;
}
static const struct luaL_Reg luapipe_f[] = {
{"new",lua_new_pipe},
{NULL,NULL}
};
static const struct luaL_Reg luapipe_m[] = {
{"print",lua_pipe_print},
{"closeout",lua_pipe_closeout},
{"closein",lua_pipe_closein},
{"getline",lua_pipe_getline},
{NULL,NULL}
};
int luaopen_luaext_pipe(lua_State* L){
luaL_newmetatable(L,"ge.pipe");
lua_pushvalue(L,-1);
lua_setfield(L,-2,"__index");
luaL_register(L,NULL,luapipe_m);
luaL_register(L,"pipe",luapipe_f);
return 1;
}