-
Notifications
You must be signed in to change notification settings - Fork 1
/
uv_api.c
261 lines (232 loc) · 6.63 KB
/
uv_api.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/***************************************************************************
* Copyright (C) 2017 - 2020, Lanka Hsu, <[email protected]>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include "utilx9.h"
void on_uv_close(uv_handle_t *handle)
{
if (handle != NULL)
{
}
}
void on_uv_walk(uv_handle_t *handle, void *arg)
{
//SAFE_UV_CLOSE(handle, on_uv_close);
if (!uv_is_closing(handle))
{
uv_close(handle, NULL);
}
}
void uv_loop_close_ex(uv_loop_t *loop)
{
uv_walk(loop, on_uv_walk, NULL);
SAFE_UV_LOOP_RUN(loop);
uv_loop_close(loop);
}
static void uv_write_ex_cb(uv_write_t *req, int status)
{
UvWriteEx_t *wreq = (UvWriteEx_t *)req;
if (wreq->cb)
{
wreq->cb(req, status);
}
SAFE_FREE(wreq->buf.base);
SAFE_FREE(wreq);
}
void uv_write_ex(uv_stream_t *dest, size_t size, char *buf, uv_write_cb cb)
{
UvWriteEx_t *wreq = (UvWriteEx_t *)SAFE_CALLOC(1, sizeof(UvWriteEx_t));
{
wreq->buf = uv_buf_init((char *)SAFE_CALLOC(1, size), size);
{
SAFE_MEMCPY(wreq->buf.base, buf, size, size);
}
}
wreq->cb = cb;
uv_write((uv_write_t *)wreq, (uv_stream_t *)dest, &wreq->buf, 1, uv_write_ex_cb);
}
static void uv_spawn_exit_cb(uv_process_t *process, int64_t exit_status, int term_signal)
{
if (process)
{
SpawnX_t *spawn_req = (SpawnX_t *)process->data;
if (spawn_req)
{
SAFE_UV_CLOSE((uv_handle_t*)&spawn_req->pipe_in, NULL);
SAFE_UV_CLOSE((uv_handle_t*)&spawn_req->pipe_out, NULL);
}
SAFE_UV_CLOSE((uv_handle_t*)process, NULL);
spawn_req->quit = 1;
DBG_WN_LN("%s (%s)", DBG_TXT_BYE_BYE, spawn_req->name);
}
}
static void uv_spawn_pipe_init(SpawnX_t *spawn_req)
{
spawn_req->stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
spawn_req->stdio[0].data.stream = (uv_stream_t *) &spawn_req->pipe_in;
//spawn_req->stdio[0].flags = UV_IGNORE;
spawn_req->stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
spawn_req->stdio[1].data.stream = (uv_stream_t *) &spawn_req->pipe_out;
spawn_req->stdio[2].flags = UV_IGNORE;
spawn_req->options.stdio_count = 3;
spawn_req->options.stdio = spawn_req->stdio;
SAFE_UV_PIPE_INIT(spawn_req->loop, &spawn_req->pipe_in, 0);
SAFE_UV_PIPE_INIT(spawn_req->loop, &spawn_req->pipe_out, 0);
}
static void uv_alloc_ex_cb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
{
*buf = uv_buf_init((char*) SAFE_MALLOC(suggested_size), suggested_size);
}
static void uv_spawn_pipe_stdout_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
{
if (nread < 0)
{
//DBG_ER_LN("%s (nread: %d)", DBG_TXT_WRONG, nread);
SAFE_UV_CLOSE(stream, NULL);
}
else if (nread > 0)
{
SpawnX_t *spawn_req = (SpawnX_t *)stream->data;
if ( (spawn_req) && (spawn_req->pipe_out_cb) )
{
spawn_req->pipe_out_cb(stream, nread, buf);
}
else
{
DBG_IF_LN("(buf->base: %s)", buf->base);
}
}
// OK to free buffer as write_data copies it.
if (buf->base)
{
free(buf->base);
}
}
void uv_spawn_close_ex(SpawnX_t *spawn_req)
{
if ( (spawn_req) && (spawn_req->quit==0) )
{
SAFE_UV_PROCESS_KILL(&spawn_req->child_req, SIGTERM);
//SAFE_UV_PROCESS_KILL(&spawn_req->child_req, SIGQUIT);
//SAFE_UV_KILL(spawn_req->child_req.pid, SIGTERM);
if ( (spawn_req->options.flags & UV_PROCESS_DETACHED) == UV_PROCESS_DETACHED )
{
SAFE_UV_UNREF(&spawn_req->child_req);
}
}
}
void uv_spawn_open_ex(SpawnX_t *spawn_req)
{
if (spawn_req)
{
uv_spawn_pipe_init(spawn_req);
spawn_req->options.exit_cb = uv_spawn_exit_cb;
spawn_req->options.file = spawn_req->args[0];
spawn_req->options.args = spawn_req->args;
spawn_req->options.flags = spawn_req->flags; //UV_PROCESS_DETACHED;
spawn_req->child_req.data = (void *)spawn_req;
int r = uv_spawn(spawn_req->loop, &spawn_req->child_req, &spawn_req->options);
if ( r == 0 )
{
spawn_req->pipe_out.data = (void *)spawn_req;
SAFE_UV_READ_START((uv_stream_t*)&spawn_req->pipe_out, uv_alloc_ex_cb, uv_spawn_pipe_stdout_cb);
DBG_WN_LN("%s (spawn_req: %p, pid: %d)", DBG_TXT_LAUNCH_THREAD, spawn_req, spawn_req->child_req.pid);
}
else
{
DBG_ER_LN("%s - %s (%s)", DBG_TXT_WRONG, uv_strerror(r), spawn_req->args[0]);
}
}
else
{
DBG_IF_LN("spawn_req is NULL !!!");
}
}
void uv_spawn_on_exit(uv_process_t *req, int64_t exit_status, int term_signal)
{
DBG_DB_LN("(exit_status: %ld, term_signal: %d)", exit_status, term_signal);
SAFE_FREE(req->data);
uv_close((uv_handle_t*) req, NULL);
}
void uv_spawn_simple_detached(SpawnX_t *spawn_req, int num, ...)
{
if ( (spawn_req) && (spawn_req->loop) && (num < MAX_OF_SPAWN_ARGS) )
{
int i = 0;
va_list args;
va_start(args, num);
for (i = 0; i < num; i++)
{
spawn_req->args[i] = va_arg(args, char*);
}
spawn_req->args[num] = NULL;
va_end(args);
if (spawn_req->options.exit_cb == NULL)
{
spawn_req->options.exit_cb = uv_spawn_on_exit;
}
spawn_req->options.file = spawn_req->args[0];
spawn_req->options.args = spawn_req->args;
spawn_req->options.flags |= UV_PROCESS_DETACHED;
int r;
if ((r = uv_spawn(spawn_req->loop, &spawn_req->child_req, &spawn_req->options)))
{
DBG_IF_LN("uv_spawn error !!! (%s, file: %s)", uv_strerror(r), spawn_req->options.file);
}
else
{
DBG_IF_LN("uv_spawn ... (child_args[0]: %s)", spawn_req->args[0]);
}
uv_unref((uv_handle_t*) &spawn_req->child_req);
}
else
{
DBG_ER_LN("error !!! (spawn_req: %p, uv_loop: %p, num %d >= 20)", spawn_req, spawn_req->loop, num);
}
}
static void uv_event_cb(uv_fs_event_t *handle, const char *filename, int events, int status)
{
if (handle)
{
UvEvent_t *event_req = (UvEvent_t *)handle->data;
if (event_req)
{
if (event_req->detect_cb)
{
event_req->detect_cb(handle, filename, events, status);
}
else
{
DBG_IF_LN("(filename: %s, events: %d, status: %d)", filename, events, status);
}
}
}
}
void uv_event_close_ex(UvEvent_t *event_req)
{
if ( (event_req) && (event_req->isquit==0) )
{
event_req->isquit = 1;
uv_fs_event_stop(&event_req->req);
DBG_WN_LN("%s (%s)", DBG_TXT_BYE_BYE, event_req->name);
}
}
void uv_event_open_ex(UvEvent_t *event_req)
{
if ( (event_req) && (event_req->loop) )
{
event_req->req.data = (void *)event_req;
uv_fs_event_init(event_req->loop, &event_req->req);
uv_fs_event_start(&event_req->req, uv_event_cb, event_req->filename, event_req->flags);
}
}