-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathewpi_spawn.c
146 lines (115 loc) · 2.98 KB
/
ewpi_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
#include <string.h>
#ifdef _WIN32
# include <windows.h>
#else
# include <errno.h>
# include <fcntl.h>
# include <stdlib.h>
# include <sys/wait.h>
# include <unistd.h>
#endif
#include "ewpi_spawn.h"
#ifdef _WIN32
int ewpi_spawn(const char *host, const char *prog, const char *option)
{
char cmd[256];
STARTUPINFO si;
PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES sa;
HANDLE pipe_out_read;
HANDLE pipe_out_write;
HANDLE pipe_err_read;
HANDLE pipe_err_write;
DWORD exit_code;
BOOL res;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (!CreatePipe(&pipe_out_read, &pipe_out_write, &sa, 0))
return 0;
if (!SetHandleInformation(pipe_out_read, HANDLE_FLAG_INHERIT, 0) )
goto close_pipe_out;
if (!CreatePipe(&pipe_err_read, &pipe_err_write, &sa, 0))
goto close_pipe_out;
if (!SetHandleInformation(pipe_err_read, HANDLE_FLAG_INHERIT, 0))
goto close_pipe_err;
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdError = pipe_err_write;
si.hStdOutput = pipe_out_write;
*cmd = '\0';
if (host)
{
strcat(cmd, host);
strcat(cmd, "-");
}
strcat(cmd, prog);
strcat(cmd, " ");
strcat(cmd, option);
if (!CreateProcess(NULL, cmd, NULL, NULL,
TRUE, 0UL, NULL, NULL, &si, &pi))
goto close_pipe_err;
CloseHandle(pipe_err_write);
CloseHandle(pipe_out_write);
WaitForSingleObject(pi.hProcess, INFINITE);
res = GetExitCodeProcess(pi.hProcess, &exit_code);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
if (!res)
goto close_pipe_err;
if (exit_code != 0UL)
goto close_pipe_err;
return 1;
close_pipe_err:
CloseHandle(pipe_err_read);
CloseHandle(pipe_err_write);
close_pipe_out:
CloseHandle(pipe_out_read);
CloseHandle(pipe_out_write);
return 0;
}
#else
int ewpi_spawn(const char *host, const char *prog, const char *option)
{
pid_t pid;
pid = fork();
if (pid < 0)
return 0;
if (pid == 0)
{
/* child */
char cmd[256];
char *args[3];
int fd;
fd = open("/dev/null", O_WRONLY);
if (fd < 0) exit(1);
if (dup2(fd, 1) < 0) exit(1);
if (dup2(fd, 2) < 0) exit(1);
if (close(fd) < 0) exit(1);;
*cmd = '\0';
if (host)
{
strcat(cmd, host);
strcat(cmd, "-");
}
strcat(cmd, prog);
args[0] = cmd;
args[1] = (char *)option;
args[2] = NULL;
execvp(args[0], args);
exit(errno);
}
else
{
/* parent */
int status;
int ret;
ret = waitpid(pid, &status, 0);
if (ret != 0)
return 0;
return WIFEXITED(status) ? WEXITSTATUS(status) == 0 : 0;
}
}
#endif