-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathngx_result.c
216 lines (176 loc) · 3.72 KB
/
ngx_result.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
/*
* ngx_result.c
*
* Author: Gandalf
* email: [email protected]
*/
#include "ngx_result.h"
#include "ngx_str.h"
int ngxexecute_execute(const char *command, char **buffer, char *error, size_t max_error_len)
{
size_t buf_size = PIPE_BUFFER_SIZE, offset = 0;
int ret = FAIL;
pid_t pid;
int fd;
*error = '\0';
if (NULL != buffer)
{
*buffer = ngxexecute_realloc(*buffer, buf_size);
**buffer = '\0';
}
alarm(3);
if (-1 != (fd = ngxexecute_popen(&pid, command)))
{
int rc = 0;
char tmp_buf[PIPE_BUFFER_SIZE];
if (NULL != buffer)
{
while (0 < (rc = read(fd, tmp_buf, sizeof(tmp_buf) - 1)) &&
MAX_EXECUTE_OUTPUT_LEN > offset + rc)
{
tmp_buf[rc] = '\0';
ngxexecute_strcpy_alloc(buffer, &buf_size, &offset, tmp_buf);
}
}
close(fd);
if (-1 == rc || -1 == ngx_waitpid(pid))
{
kill(-pid, SIGTERM);
}
else
ret = SUCCEED;
}
return ret;
}
int ngxexecute_popen(pid_t *pid, const char *command)
{
int fd[2];
if (-1 == pipe(fd))
return -1;
if (-1 == (*pid = ngxexecute_fork()))
{
close(fd[0]);
close(fd[1]);
return -1;
}
if (0 != *pid) /* parent process */
{
close(fd[1]);
return fd[0];
}
/* child process */
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
dup2(fd[1], STDERR_FILENO);
close(fd[1]);
/* set the child as the process group leader, otherwise orphans may be left after timeout */
if (-1 == setpgid(0, 0))
{
exit(EXIT_SUCCESS);
}
execl("/bin/sh", "sh", "-c", command, NULL);
exit(EXIT_SUCCESS);
}
void *ngxexecute_realloc2(const char *filename, int line, void *old, size_t size)
{
int max_attempts;
void *ptr = NULL;
for (
max_attempts = 10, size = MAX(size, 1);
0 < max_attempts && NULL == ptr;
ptr = realloc(old, size), max_attempts--
);
if (NULL != ptr)
return ptr;
exit(EXIT_FAILURE);
}
void ngxexecute_strcpy_alloc(char **str, size_t *alloc_len, size_t *offset, const char *src)
{
ngxexecute_strncpy_alloc(str, alloc_len, offset, src, strlen(src));
}
void ngxexecute_strncpy_alloc(char **str, size_t *alloc_len, size_t *offset, const char *src, size_t n)
{
if (NULL == *str)
{
*alloc_len = n + 1;
*offset = 0;
*str = ngxexecute_malloc(*str, *alloc_len);
}
else if (*offset + n >= *alloc_len)
{
while (*offset + n >= *alloc_len)
*alloc_len *= 2;
*str = ngxexecute_realloc(*str, *alloc_len);
}
while (0 != n && '\0' != *src)
{
(*str)[(*offset)++] = *src++;
n--;
}
(*str)[*offset] = '\0';
}
void *ngxexecute_malloc2(const char *filename, int line, void *old, size_t size)
{
int max_attempts;
void *ptr = NULL;
/* old pointer must be NULL */
if (NULL != old)
{
}
for (
max_attempts = 10, size = MAX(size, 1);
0 < max_attempts && NULL == ptr;
ptr = malloc(size), max_attempts--
);
if (NULL != ptr)
return ptr;
exit(EXIT_FAILURE);
}
size_t ngxexecute_strlcpy(char *dst, const char *src, size_t siz)
{
const char *s = src;
if (0 != siz)
{
while (0 != --siz && '\0' != *s)
*dst++ = *s++;
*dst = '\0';
}
return s - src; /* count does not include null */
}
char *ngxexecute_strdup2(const char *filename, int line, char *old, const char *str)
{
int retry;
char *ptr = NULL;
ngxexecute_free(old);
for (retry = 10; 0 < retry && NULL == ptr; ptr = strdup(str), retry--)
;
if (NULL != ptr)
return ptr;
exit(EXIT_FAILURE);
}
int ngx_waitpid(pid_t pid)
{
int rc, status;
do
{
#ifdef WCONTINUED
static int wcontinued = WCONTINUED;
retry:
if (-1 == (rc = waitpid(pid, &status, WUNTRACED | wcontinued)))
{
if (EINVAL == errno && 0 != wcontinued)
{
wcontinued = 0;
goto retry;
}
#else
if (-1 == (rc = waitpid(pid, &status, WUNTRACED)))
{
#endif
goto exit;
}
}
while (!WIFEXITED(status) && !WIFSIGNALED(status));
exit:
return rc;
}