59 char const *msg_to_child =
"Message from parent";
60 char msg_from_child[4096] = { 0 };
61 char msg_from_err[4096] = { 0 };
71 if (write(exe.
in_fd(), msg_to_child, strlen(msg_to_child))
72 != (
int)strlen(msg_to_child))
73 throw(
ERROR(errno,
"Error writing to child's input pipe"));
74 if (read(exe.
out_fd(), msg_from_child, 4096) == 0)
75 throw(
ERROR(errno,
"Error reading from child's output pipe"));
76 if (read(exe.
err_fd(), msg_from_err, 4096) == 0)
77 throw(
ERROR(errno,
"Error reading from child's error pipe"));
81 assert(static_cast<std::string>(msg_from_child) ==
"Message from child");
82 assert(static_cast<std::string>(msg_from_err) ==
"Message from child err");
92 char msg_from_parent[4096] = { 0 };
93 char const *msg_to_parent =
"Message from child";
94 char const *msg_to_err =
"Message from child err";
96 if (read(exe.
in_fd(), msg_from_parent, 4096) == 0)
97 throw(
ERROR(errno,
"Error reading input from parent"));
98 if (write(exe.
out_fd(), msg_to_parent, strlen(msg_to_parent))
99 != (
int)strlen(msg_to_parent))
100 throw(
ERROR(errno,
"Error writing output to parent"));
101 if (write(exe.
err_fd(), msg_to_err, strlen(msg_to_err))
102 != (
int)strlen(msg_to_err))
103 throw(
ERROR(errno,
"Error writing error to parent"));
105 assert(static_cast<std::string>(msg_from_parent) ==
"Message from parent");
117 char const *msg_to_child =
"Message from parent";
118 char msg_from_child[4096] = { 0 };
119 char msg_from_err[4096] = { 0 };
120 char extra_msg[4096] = { 0 };
121 int extra_msg_length = 0;
142 if (write(exe.
in_fd(), msg_to_child, strlen(msg_to_child))
143 != (
int)strlen(msg_to_child))
144 throw(
ERROR(errno,
"Error writing to child's input pipe"));
152 if (read(exe.
out_fd(), msg_from_child, 4096) == 0)
153 throw(
ERROR(errno,
"Error reading from child's output pipe"));
161 if (read(exe.
err_fd(), msg_from_err, 4096) == 0)
162 throw(
ERROR(errno,
"Error reading from child's error pipe"));
171 for (i = 0; i < 4096; extra_msg[i++] = 0);
172 extra_msg_length = read(exe.
out_fd(), extra_msg, 4096);
174 assert(extra_msg_length == 0);
176 for (i = 0; i < 4096; extra_msg[i++] = 0);
177 extra_msg_length = read(exe.
err_fd(), extra_msg, 4096);
179 assert(extra_msg_length == 0);
183 assert(static_cast<std::string>(msg_from_child) ==
"Message from child");
184 assert(static_cast<std::string>(msg_from_err) ==
"Message from child err");
194 char msg_from_parent[4096] = { 0 };
195 char const *msg_to_parent =
"Message from child";
196 char const *msg_to_err =
"Message from child err";
198 if (read(exe.
in_fd(), msg_from_parent, 4096) == 0)
199 throw(
ERROR(errno,
"Error reading input from parent"));
200 if (write(exe.
out_fd(), msg_to_parent, strlen(msg_to_parent))
201 != (
int)strlen(msg_to_parent))
202 throw(
ERROR(errno,
"Error writing output to parent"));
203 if (write(exe.
err_fd(), msg_to_err, strlen(msg_to_err))
204 != (
int)strlen(msg_to_err))
205 throw(
ERROR(errno,
"Error writing error to parent"));
207 assert(static_cast<std::string>(msg_from_parent) ==
"Message from parent");
365 std::string command =
"sleep 5 ; /bin/ls -1 ./test-exec.cc";
366 char buffer[4096] = { 0 };
381 for (num_bytes = 0; num_bytes < 4096; buffer[num_bytes++] = 0);
382 num_bytes = read(exe.
out_fd(), buffer, 4096);
383 assert(num_bytes = 15);
384 assert(static_cast<std::string>(buffer) == static_cast<std::string>(
"./test-exec.cc\n"));
386 for (num_bytes = 0; num_bytes < 4096; buffer[num_bytes++] = 0);
387 num_bytes = read(exe.
err_fd(), buffer, 4096);
388 assert(num_bytes == 0);
389 assert(static_cast<std::string>(buffer).size() == 0);
408 std::vector<std::string> argv;
409 char buffer[4096] = { 0 };
413 argv.push_back(std::string(
"-1"));
414 argv.push_back(std::string(
"./test-exec.cc"));
415 exe.
exec(command, argv);
427 for (num_bytes = 0; num_bytes < 4096; buffer[num_bytes++] = 0);
428 num_bytes = read(exe.
out_fd(), buffer, 4096);
429 assert(num_bytes = 15);
430 assert(static_cast<std::string>(buffer) == static_cast<std::string>(
"./test-exec.cc\n"));
432 for (num_bytes = 0; num_bytes < 4096; buffer[num_bytes++] = 0);
433 num_bytes = read(exe.
err_fd(), buffer, 4096);
434 assert(num_bytes == 0);
435 assert(static_cast<std::string>(buffer).size() == 0);
457 char const *msg_to_child =
"Message from parent";
458 char msg_from_child[4096] = { 0 };
459 char msg_from_err[4096] = { 0 };
467 assert(fdout.good());
470 fdout << msg_to_child;
473 assert(fdout.good());
475 if (read(exe.
out_fd(), msg_from_child, 4096) == 0)
476 throw(
ERROR(errno,
"Error reading from child's output pipe"));
477 if (read(exe.
err_fd(), msg_from_err, 4096) == 0)
478 throw(
ERROR(errno,
"Error reading from child's error pipe"));
482 assert(fdout.good());
484 assert(static_cast<std::string>(msg_from_child) ==
"Message from child");
485 assert(static_cast<std::string>(msg_from_err) ==
"Message from child err");
494 char msg_from_parent[4096] = { 0 };
495 char const *msg_to_parent =
"Message from child";
496 char const *msg_to_err =
"Message from child err";
498 if (read(exe.
in_fd(), msg_from_parent, 4096) == 0)
499 throw(
ERROR(errno,
"Error reading input from parent"));
500 if (write(exe.
out_fd(), msg_to_parent, strlen(msg_to_parent))
501 != (
int)strlen(msg_to_parent))
502 throw(
ERROR(errno,
"Error writing output to parent"));
503 if (write(exe.
err_fd(), msg_to_err, strlen(msg_to_err))
504 != (
int)strlen(msg_to_err))
505 throw(
ERROR(errno,
"Error writing error to parent"));
507 assert(static_cast<std::string>(msg_from_parent) ==
"Message from parent");
521 char const *msg_to_child =
"Message from parent";
522 char msg_from_child[4096] = { 0 };
523 char msg_from_err[4096] = { 0 };
533 fdout << msg_to_child;
537 fdin.get(msg_from_child,4096);
539 if (read(exe.
err_fd(), msg_from_err, 4096) == 0)
540 throw(
ERROR(errno,
"Error reading from child's error pipe"));
544 assert(static_cast<std::string>(msg_from_child) ==
"Message from child");
545 assert(static_cast<std::string>(msg_from_err) ==
"Message from child err");
554 char msg_from_parent[4096] = { 0 };
555 char const *msg_to_parent =
"Message from child";
556 char const *msg_to_err =
"Message from child err";
558 if (read(exe.
in_fd(), msg_from_parent, 4096) == 0)
559 throw(
ERROR(errno,
"Error reading input from parent"));
560 if (write(exe.
out_fd(), msg_to_parent, strlen(msg_to_parent))
561 != (
int)strlen(msg_to_parent))
562 throw(
ERROR(errno,
"Error writing output to parent"));
563 if (write(exe.
err_fd(), msg_to_err, strlen(msg_to_err))
564 != (
int)strlen(msg_to_err))
565 throw(
ERROR(errno,
"Error writing error to parent"));
567 assert(static_cast<std::string>(msg_from_parent) ==
"Message from parent");
581 char const *msg_to_child =
"Message from parent";
582 char msg_from_child[4096] = { 0 };
583 char msg_from_err[4096] = { 0 };
595 if (write(exe.
in_fd(), msg_to_child, strlen(msg_to_child))
596 != (
int)strlen(msg_to_child))
597 throw(
ERROR(errno,
"Error writing to child's input pipe"));
601 if (read(exe.
out_fd(), msg_from_child, 4096) == 0)
602 throw(
ERROR(errno,
"Error reading from child's output pipe"));
604 if (read(exe.
err_fd(), msg_from_err, 4096) == 0)
605 throw(
ERROR(errno,
"Error reading from child's error pipe"));
609 assert(static_cast<std::string>(msg_from_child) ==
"Message from child");
610 assert(static_cast<std::string>(msg_from_err) ==
"Message from child err");
619 char msg_from_parent[4096] = { 0 };
620 char const *msg_to_parent =
"Message from child";
621 char const *msg_to_err =
"Message from child err";
623 if (read(exe.
in_fd(), msg_from_parent, 4096) == 0)
624 throw(
ERROR(errno,
"Error reading input from parent"));
625 if (write(exe.
out_fd(), msg_to_parent, strlen(msg_to_parent))
626 != (
int)strlen(msg_to_parent))
627 throw(
ERROR(errno,
"Error writing output to parent"));
628 if (write(exe.
err_fd(), msg_to_err, strlen(msg_to_err))
629 != (
int)strlen(msg_to_err))
630 throw(
ERROR(errno,
"Error writing error to parent"));
632 assert(static_cast<std::string>(msg_from_parent) ==
"Message from parent");
644 char const * msg_to_child =
"Message from parent";
645 char msg_from_child[4096] = { 0 };
646 char msg_from_err[4096] = { 0 };
662 if (exe.
in_write(msg_to_child, strlen(msg_to_child))
663 != (int)strlen(msg_to_child))
664 throw(
ERROR(errno,
"Error writing to child's input pipe"));
676 if (exe.
out_read(msg_from_child, 4096) == 0)
677 throw(
ERROR(errno,
"Error reading from child's output pipe"));
687 if (exe.
err_read(msg_from_err, 4096) == 0)
688 throw(
ERROR(errno,
"Error reading from child's error pipe"));
708 assert(static_cast<std::string>(msg_from_child) ==
"Message from child");
709 assert(static_cast<std::string>(msg_from_err) ==
"Message from child err");
711 assert(exe.
out_read(msg_from_child, 4096) == 0);
727 char msg_from_parent[4096] = { 0 };
728 char const * msg_to_parent =
"Message from child";
729 char const * msg_to_err =
"Message from child err";
731 if (read(exe.
in_fd(), msg_from_parent, 4096) == 0)
732 throw(
ERROR(errno,
"Error reading input from parent"));
733 if (write(exe.
out_fd(), msg_to_parent, strlen(msg_to_parent))
734 != (
int)strlen(msg_to_parent))
735 throw(
ERROR(errno,
"Error writing output to parent"));
736 if (write(exe.
err_fd(), msg_to_err, strlen(msg_to_err))
737 != (
int)strlen(msg_to_err))
738 throw(
ERROR(errno,
"Error writing error to parent"));
740 assert(static_cast<std::string>(msg_from_parent) ==
"Message from parent");
747 int main(
int argc,
char const * argv[])
int child_signal_no(void)
If the child was signaled, return the signal number.
bool err_eof(void)
Check for err EOF.
bool out_eof(void)
Check for output EOF.
void fork(void)
Fork a child process.
void kill_child(void)
Send a KILL signal to the child.
int err_read(char *buf, const int len)
Allow parent to read from err_fd()
void exit(int code=0)
Called by the child to exit with a particular code.
void signal_child(int signal_no)
Send a signal to the child.
bool err_ready(void)
Check I/O for output.
bool out_ready(void)
Check I/O for output.
bool child_started(void) const
Returns true if the child has been started.
int in_write(const char *buf, const int len)
Allow parent to write output to in_fd()
int out_fd(void)
Return a file descriptor for I/O between parent a child.
bool child_running(void)
Returns true if the child is running.
bool child_exited_success(void)
Returns true if the child returned exit code 0 and no caught signals.
void exec(const std::string command)
Execute a command, rerouting stdin, stdout, and stderr to parent.
int out_read(char *buf, const int len)
Allow parent to read out_fd()
bool child_signaled(void)
Returns true if the child was signaled.
int child_exit_code(void)
Return the child's exit code.
bool child_exited_normally(void)
Returns true if the child has exited normally.
Fork a child process or execute an external program.
bool in_ready(void)
Check I/O for input.
bool in_eof(void)
Check for input EOF.
bool child_exited(void)
Returns true of the child has existed.
int err_fd(void)
Return a file descriptor for I/O between parent and child.
void wait(void)
Wait for the child to exit.
bool is_child(void)
Returns true if called by the child.
int main(int argc, char const *argv[])
bool is_parent(void)
Returns true if called by the parent.
int in_fd(void)
Return a file descriptor for I/O between parent and child.