}
/* Read as much as possible into the currently-allocated stdout buffer, and
- * possibly realloc it for the next read */
+ * possibly realloc it for the next read
+ *
+ * Returns:
+ * > 0 on success (even though no bytes may have been read)
+ * 0 on EOF (no error, but no more reads can be performed)
+ * < 0 on error
+ **/
static int process_read_stdout_once(struct process_info *procinfo)
{
struct process *process = &procinfo->process;
max_len = procinfo->stdout_buf_len - process->stdout_len - 1;
rc = read(fd, process->stdout_buf + process->stdout_len, max_len);
- if (rc <= 0)
+ if (rc == 0)
+ return 0;
+ if (rc < 0) {
+ if (errno == EINTR)
+ return 1;
+ pb_log("%s: read failed: %s\n", __func__, strerror(errno));
return rc;
+ }
process->stdout_len += rc;
if (process->stdout_len == procinfo->stdout_buf_len - 1) {
procinfo->stdout_buf_len);
}
- return rc;
+ return 1;
}
static int process_setup_stdout_pipe(struct process_info *procinfo)
--- /dev/null
+
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <signal.h>
+
+#include <process/process.h>
+#include <waiter/waiter.h>
+#include <talloc/talloc.h>
+
+static int do_child(int ppid)
+{
+ sleep(1);
+ kill(ppid, SIGCHLD);
+ printf("forty two\n");
+ return 42;
+}
+
+int main(int argc, char **argv)
+{
+ struct waitset *waitset;
+ struct process *process;
+ const char *child_argv[3];
+ void *ctx;
+
+ if (argc == 3 && !strcmp(argv[1], "child"))
+ return do_child(atoi(argv[2]));
+
+ ctx = talloc_new(NULL);
+
+ waitset = waitset_create(ctx);
+
+ process_init(ctx, waitset, false);
+
+ child_argv[0] = argv[0];
+ child_argv[1] = "child";
+ child_argv[2] = talloc_asprintf(ctx, "%d", getpid());
+ child_argv[3] = NULL;
+
+ process = process_create(ctx);
+ process->path = child_argv[0];
+ process->argv = child_argv;
+ process->keep_stdout = true;
+
+ process_run_sync(process);
+
+ assert(WIFEXITED(process->exit_status));
+ assert(WEXITSTATUS(process->exit_status) == 42);
+
+ assert(process->stdout_len == strlen("forty two\n"));
+ assert(!memcmp(process->stdout_buf, "forty two\n",
+ process->stdout_len));
+
+ talloc_free(ctx);
+
+ return EXIT_SUCCESS;
+}