Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make subprocess async read non-blocking #76

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions subprocess.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ subprocess_weak int subprocess_alive(struct subprocess_s *const process);
#endif

#if !defined(_WIN32)
#include <fcntl.h>
#include <signal.h>
#include <spawn.h>
#include <stdlib.h>
Expand Down Expand Up @@ -757,6 +758,7 @@ int subprocess_create_ex(const char *const commandLine[], int options,
int stdinfd[2];
int stdoutfd[2];
int stderrfd[2];
int fd, fd_flags;
pid_t child;
extern char **environ;
char *const empty_environment[1] = {SUBPROCESS_NULL};
Expand Down Expand Up @@ -886,6 +888,13 @@ int subprocess_create_ex(const char *const commandLine[], int options,
// Store the stdout read end
out_process->stdout_file = fdopen(stdoutfd[0], "rb");

// Should we use async behaviour ?
if(options & subprocess_option_enable_async){
fd = fileno(out_process->stdout_file);
fd_flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
}

if (subprocess_option_combined_stdout_stderr ==
(options & subprocess_option_combined_stdout_stderr)) {
out_process->stderr_file = out_process->stdout_file;
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ add_executable(process_fail_stackoverflow process_fail_stackoverflow.c)
add_executable(process_hung process_hung.c)
add_executable(process_stdout_data process_stdout_data.c)
add_executable(process_stdout_poll process_stdout_poll.c)
add_executable(process_stdout_poll_nodata process_stdout_poll_nodata.c)
add_executable(process_stderr_poll process_stderr_poll.c)
add_executable(process_stdout_large process_stdout_large.c)
add_executable(process_call_return_argc process_call_return_argc.c)
Expand Down
44 changes: 38 additions & 6 deletions test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,11 +594,11 @@ UTEST(subprocess, read_stdout_async_small) {
ASSERT_EQ(0, subprocess_create(commandLine, subprocess_option_enable_async,
&process));

do {
while(subprocess_alive(&process)) {
bytes_read = subprocess_read_stdout(&process, data + index,
sizeof(data) - 1 - index);
index += bytes_read;
} while (bytes_read != 0);
}

ASSERT_EQ(13u, index);

Expand All @@ -624,11 +624,11 @@ UTEST(subprocess, read_stdout_async) {
ASSERT_EQ(0, subprocess_create(commandLine, subprocess_option_enable_async,
&process));

do {
while(subprocess_alive(&process)) {
bytes_read = subprocess_read_stdout(&process, data + index,
sizeof(data) - 1 - index);
index += bytes_read;
} while (bytes_read != 0);
}

ASSERT_EQ(212992u, index);

Expand All @@ -643,6 +643,38 @@ UTEST(subprocess, read_stdout_async) {
ASSERT_EQ(ret, 0);
}

UTEST(subprocess, read_stdout_async_nodata) {
const char *const commandLine[] = {"./process_stdout_poll_nodata", 0};
struct subprocess_s process;
int ret = -1;
static char data[14 + 1] = {0};
unsigned index = 0;
unsigned bytes_read = 0;

ASSERT_EQ(0, subprocess_create(commandLine, subprocess_option_enable_async,
&process));

while(subprocess_alive(&process)) {
bytes_read = subprocess_read_stdout(&process, data + index,
sizeof(data) - 1 - index);
index += bytes_read;

// Send any character to the subprocess to tell it to pursuit
if (bytes_read == 0) {
fputc('s', subprocess_stdin(&process));
fflush(subprocess_stdin(&process));
}
}

ASSERT_EQ(15u, bytes_read);
ASSERT_EQ(15u, index);
ASSERT_TRUE(0 == memcmp("Hello, world!", data, 14));

ASSERT_EQ(0, subprocess_join(&process, &ret));
ASSERT_EQ(0, subprocess_destroy(&process));
ASSERT_EQ(ret, 0);
}

UTEST(subprocess, poll_stdout_async) {
const char *const commandLine[] = {"./process_stdout_poll", "16384", 0};
struct subprocess_s process;
Expand All @@ -654,7 +686,7 @@ UTEST(subprocess, poll_stdout_async) {
ASSERT_EQ(0, subprocess_create(commandLine, subprocess_option_enable_async,
&process));

do {
while(subprocess_alive(&process)) {
bytes_read = subprocess_read_stdout(&process, data + index,
sizeof(data) - 1 - index);

Expand All @@ -667,7 +699,7 @@ UTEST(subprocess, poll_stdout_async) {
}

index += bytes_read;
} while (bytes_read != 0);
}

ASSERT_EQ(212992u, index);

Expand Down
37 changes: 37 additions & 0 deletions test/process_stdout_poll_nodata.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <http://unlicense.org/>

#include <stdio.h>
#include <string.h>

int main(int argc, const char *const argv[]) {
// Do nothing, wait for an event on stdin
while(fgetc(stdin) == EOF){}

// Event received, write something on stdout
printf("Hello, world!");

return 0;
}
Loading