Skip to content

Commit 2b99248

Browse files
committed
bug fix uv_spawn usage, add example and test
1 parent a4cda95 commit 2b99248

12 files changed

Lines changed: 278 additions & 126 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ find_package(raii QUIET)
2626
if(NOT raii_FOUND)
2727
FetchContent_Declare(raii
2828
URL https://github.com/zelang-dev/c-raii/archive/refs/heads/main.zip
29-
URL_MD5 5381f3b2c240e778a3d5d1d75a3a42e9
29+
URL_MD5 3dc8b8eaf534a3569d3f76805a4cfc15
3030
)
3131
FetchContent_MakeAvailable(raii)
3232
endif()

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ int main() {
7171

7272
* [Reading/Writing files](https://docs.libuv.org/en/v1.x/guide/filesystem.html#reading-writing-files) as in [uvcat/main.c](https://github.com/libuv/libuv/blob/master/docs/code/uvcat/main.c) - 62 line *script*.
7373
* [Buffers and Streams](https://docs.libuv.org/en/v1.x/guide/filesystem.html#buffers-and-streams) as in [uvtee/main.c](https://github.com/libuv/libuv/blob/master/docs/code/uvtee/main.c) - 79 line *script*.
74+
* [Spawning child processes](https://docs.libuv.org/en/v1.x/guide/processes.html#spawning-child-processes) as in [spawn/main.c](https://github.com/libuv/libuv/blob/master/docs/code/spawn/main.c) - 36 line *script*.
7475
* [Networking/TCP](https://docs.libuv.org/en/v1.x/guide/networking.html#tcp) as in [tcp-echo-server/main.c](https://github.com/libuv/libuv/blob/master/docs/code/tcp-echo-server/main.c) - 87 line *script*.
7576

7677
*Reduced to:*
@@ -128,6 +129,33 @@ int uv_main(int argc, char **argv) {
128129
</tr>
129130
</table>
130131

132+
<table>
133+
<tr>
134+
<th>spawn.c - 13 lines</th>
135+
</tr>
136+
<tr>
137+
<td>
138+
139+
```c
140+
#include "uv_coro.h"
141+
142+
void _on_exit(int64_t exit_status, int term_signal) {
143+
fprintf(stderr, "\nProcess exited with status %" PRId64 ", signal %d\n", exit_status, term_signal);
144+
}
145+
146+
int uv_main(int argc, char **argv) {
147+
spawn_t child = spawn("mkdir", "test-dir", nullptr);
148+
if (!spawn_atexit(child, _on_exit))
149+
fprintf(stderr, "\nLaunched process with ID %d\n", spawn_pid(child));
150+
151+
return coro_err_code();
152+
}
153+
```
154+
155+
</td>
156+
</tr>
157+
</table>
158+
131159
<table>
132160
<tr>
133161
<th>tcp-echo-server.c - 27 lines</th>

docs/index.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ int main() {
7171

7272
* [Reading/Writing files](https://docs.libuv.org/en/v1.x/guide/filesystem.html#reading-writing-files) as in [uvcat/main.c](https://github.com/libuv/libuv/blob/master/docs/code/uvcat/main.c) - 62 line *script*.
7373
* [Buffers and Streams](https://docs.libuv.org/en/v1.x/guide/filesystem.html#buffers-and-streams) as in [uvtee/main.c](https://github.com/libuv/libuv/blob/master/docs/code/uvtee/main.c) - 79 line *script*.
74+
* [Spawning child processes](https://docs.libuv.org/en/v1.x/guide/processes.html#spawning-child-processes) as in [spawn/main.c](https://github.com/libuv/libuv/blob/master/docs/code/spawn/main.c) - 36 line *script*.
7475
* [Networking/TCP](https://docs.libuv.org/en/v1.x/guide/networking.html#tcp) as in [tcp-echo-server/main.c](https://github.com/libuv/libuv/blob/master/docs/code/tcp-echo-server/main.c) - 87 line *script*.
7576

7677
*Reduced to:*
@@ -128,6 +129,33 @@ int uv_main(int argc, char **argv) {
128129
</tr>
129130
</table>
130131

132+
<table>
133+
<tr>
134+
<th>spawn.c - 13 lines</th>
135+
</tr>
136+
<tr>
137+
<td>
138+
139+
```c
140+
#include "uv_coro.h"
141+
142+
void _on_exit(int64_t exit_status, int term_signal) {
143+
fprintf(stderr, "\nProcess exited with status %" PRId64 ", signal %d\n", exit_status, term_signal);
144+
}
145+
146+
int uv_main(int argc, char **argv) {
147+
spawn_t child = spawn("mkdir", "test-dir", nullptr);
148+
if (!spawn_atexit(child, _on_exit))
149+
fprintf(stderr, "\nLaunched process with ID %d\n", spawn_pid(child));
150+
151+
return coro_err_code();
152+
}
153+
```
154+
155+
</td>
156+
</tr>
157+
</table>
158+
131159
<table>
132160
<tr>
133161
<th>tcp-echo-server.c - 27 lines</th>

examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8...3.14)
22

33
add_subdirectory(echo-server)
44

5-
set(TARGET_LIST child helloworld tcp-echo-server uvcat uvtee uv_coro_spawn)
5+
set(TARGET_LIST helloworld spawn tcp-echo-server uvcat uvtee)
66
foreach (TARGET ${TARGET_LIST})
77
add_executable(${TARGET} ${TARGET}.c)
88
target_link_libraries(${TARGET} uv_coro)

examples/child.c

Lines changed: 0 additions & 21 deletions
This file was deleted.

examples/spawn.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "uv_coro.h"
2+
3+
void _on_exit(int64_t exit_status, int term_signal) {
4+
fprintf(stderr, "\nProcess exited with status %" PRId64 ", signal %d\n", exit_status, term_signal);
5+
}
6+
7+
int uv_main(int argc, char **argv) {
8+
spawn_t child = spawn("mkdir", "test-dir", nullptr);
9+
if (!spawn_atexit(child, _on_exit))
10+
fprintf(stderr, "\nLaunched process with ID %d\n", spawn_pid(child));
11+
12+
return coro_err_code();
13+
}

examples/uv_coro_spawn.c

Lines changed: 0 additions & 18 deletions
This file was deleted.

include/uv_coro.h

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ typedef enum {
5959
UV_CORO_PIPE,
6060
UV_CORO_TCP,
6161
UV_CORO_UDP,
62+
UV_CORO_SPAWN,
6263
UV_CORO_SOCKET,
6364
UV_CORO_PIPE_0,
6465
UV_CORO_PIPE_1,
@@ -168,13 +169,8 @@ typedef struct {
168169
uv_process_options_t options[1];
169170
} spawn_options_t;
170171

171-
typedef struct {
172-
uv_coro_types type;
173-
bool is_detach;
174-
spawn_options_t *handle;
175-
uv_process_t process[1];
176-
} spawn_t;
177-
172+
typedef struct spawn_s _spawn_t;
173+
typedef _spawn_t *spawn_t;
178174
typedef struct nameinfo_s {
179175
uv_coro_types type;
180176
string_t host;
@@ -262,11 +258,25 @@ C_API uv_stdio_container_t *stdio_fd(int fd, int flags);
262258
* -`UV_NONBLOCK_PIPE`
263259
* -`UV_OVERLAPPED_PIPE`
264260
*/
265-
C_API uv_stdio_container_t *stdio_stream(void *handle, int flags);
261+
C_API uv_stdio_container_t *stdio_stream(void_t handle, int flags);
262+
263+
/*
264+
Stdio container `pipe` ~pointer~ with `uv_stdio_flags` transmitted to the child process.
265+
* -`UV_CREATE_PIPE`
266+
* -`UV_READABLE_PIPE`
267+
*/
268+
C_API uv_stdio_container_t *stdio_piperead(void);
269+
270+
/*
271+
Stdio container `pipe` ~pointer~ with `uv_stdio_flags` transmitted to the child process.
272+
* -`UV_CREATE_PIPE`
273+
* -`UV_WRITABLE_PIPE`
274+
*/
275+
C_API uv_stdio_container_t *stdio_pipewrite(void);
266276

267277
/**
268278
* @param env Environment for the new process. Key=value, separated with semicolon like:
269-
* `"Key1=Value1;Key=Value2;Key3=Value3"`. If NULL the parents environment is used.
279+
* `"Key1=Value1;Key2=Value2;Key3=Value3"`. If `NULL` the parents environment is used.
270280
*
271281
* @param cwd Current working directory for the subprocess.
272282
* @param flags Various process flags that control how `uv_spawn()` behaves:
@@ -304,17 +314,18 @@ C_API spawn_options_t *spawn_opts(string env, string_t cwd, int flags, uv_uid_t
304314
* @param options Use `spawn_opts()` function to produce `uv_stdio_container_t` and `uv_process_options_t` options.
305315
* If `NULL` defaults `stderr` of subprocess to parent.
306316
*/
307-
C_API spawn_t *spawn(string_t command, string_t args, spawn_options_t *options);
308-
C_API int spawn_exit(spawn_t *, spawn_cb exit_func);
309-
C_API int spawn_in(spawn_t *, stdin_cb std_func);
310-
C_API int spawn_out(spawn_t *, stdout_cb std_func);
311-
C_API int spawn_err(spawn_t *, stderr_cb std_func);
312-
C_API int spawn_pid(spawn_t *child);
313-
C_API int spawn_signal(spawn_t *, int sig);
314-
C_API int spawn_detach(spawn_t *);
315-
C_API uv_stream_t *ipc_in(spawn_t *);
316-
C_API uv_stream_t *ipc_out(spawn_t *);
317-
C_API uv_stream_t *ipc_err(spawn_t *);
317+
C_API spawn_t spawn(string_t command, string_t args, spawn_options_t *options);
318+
C_API int spawn_atexit(spawn_t, spawn_cb exit_func);
319+
C_API bool is_spawning(spawn_t);
320+
C_API int spawn_in(spawn_t, stdin_cb std_func);
321+
C_API int spawn_out(spawn_t, stdout_cb std_func);
322+
C_API int spawn_err(spawn_t, stderr_cb std_func);
323+
C_API int spawn_pid(spawn_t);
324+
C_API int spawn_signal(spawn_t, int sig);
325+
C_API int spawn_detach(spawn_t);
326+
C_API uv_stream_t *ipc_in(spawn_t);
327+
C_API uv_stream_t *ipc_out(spawn_t);
328+
C_API uv_stream_t *ipc_err(spawn_t);
318329

319330
C_API string fs_readfile(string_t path);
320331
C_API int fs_writefile(string_t path, string_t text);
@@ -361,6 +372,7 @@ C_API dnsinfo_t *get_addrinfo(string_t address, string_t service, u32 numhints_p
361372
C_API addrinfo_t *addrinfo_next(dnsinfo_t *);
362373
C_API nameinfo_t *get_nameinfo(string_t addr, int port, int flags);
363374

375+
C_API uv_pipe_t *pipe_create_ex(bool is_ipc, bool autofree);
364376
C_API uv_pipe_t *pipe_create(bool is_ipc);
365377
C_API uv_tcp_t *tcp_create(void);
366378

@@ -420,6 +432,7 @@ C_API bool is_pipe(void_t);
420432
C_API bool is_tty(void_t);
421433
C_API bool is_udp(void_t);
422434
C_API bool is_tcp(void_t);
435+
C_API bool is_process(void_t);
423436
C_API bool is_udp_packet(void_t);
424437
C_API bool is_socketpair(void_t);
425438
C_API bool is_pipepair(void_t);

0 commit comments

Comments
 (0)