rvm 1.08
|
00001 #include "config.h" 00002 00003 #ifdef HAVE_UNISTD_H 00004 #include <unistd.h> 00005 #endif 00006 00007 #include <string> 00008 #include <cstring> 00009 #include <cassert> 00010 00011 #include "asserts.h" 00012 #include "error.h" 00013 #include "fs.h" 00014 #include "exec.h" 00015 #include "fdstream.h" 00016 00017 // static char confdir[4096] = { 0 }; 00018 00019 void test1(void) 00020 { 00021 execute exe; 00022 bool thrown = false; 00023 int c = 0; 00024 00025 assert(!exe.child_started()); 00026 assert(!exe.child_running()); 00027 try { 00028 exe.fork(); 00029 } 00030 catch (error &e) { 00031 thrown = true; 00032 } 00033 assert(!thrown); 00034 if (exe.is_child()) { 00035 // std::cerr << "Child exiting now" << std::endl; 00036 exe.exit(0); 00037 } 00038 assert(exe.child_started()); 00039 while (exe.child_running() && (c < 5)) { 00040 // std::cout << "Child is running, waiting..." << std::endl; 00041 c++; 00042 sleep(1); 00043 } 00044 assert(!exe.child_running()); 00045 assert(exe.child_exited()); 00046 assert(exe.child_exited_normally()); 00047 assert(!exe.child_signaled()); 00048 assert(exe.child_exit_code() == 0); 00049 assert(exe.child_signal_no() == 0); 00050 assert(exe.child_exited_success()); 00051 } 00052 00053 void test2(void) 00054 { 00055 execute exe; 00056 00057 exe.fork(); 00058 if (exe.is_parent()) { 00059 char const *msg_to_child = "Message from parent"; 00060 char msg_from_child[4096] = { 0 }; 00061 char msg_from_err[4096] = { 0 }; 00062 00063 assert(exe.child_running()); 00064 assert(!exe.child_exited()); 00065 assert(!exe.child_exited_normally()); 00066 assert(!exe.child_signaled()); 00067 assert(exe.child_exit_code() == 0); 00068 assert(exe.child_signal_no() == 0); 00069 assert(!exe.child_exited_success()); 00070 00071 if (write(exe.in_fd(), msg_to_child, strlen(msg_to_child)) 00072 != (int)strlen(msg_to_child)) 00073 throw(ERROR(errno,"Error writing to child's input pipe")); 00074 if (read(exe.out_fd(), msg_from_child, 4096) == 0) 00075 throw(ERROR(errno,"Error reading from child's output pipe")); 00076 if (read(exe.err_fd(), msg_from_err, 4096) == 0) 00077 throw(ERROR(errno,"Error reading from child's error pipe")); 00078 00079 exe.wait(); 00080 00081 assert(static_cast<std::string>(msg_from_child) == "Message from child"); 00082 assert(static_cast<std::string>(msg_from_err) == "Message from child err"); 00083 assert(!exe.child_running()); 00084 assert(exe.child_exited()); 00085 assert(exe.child_exited_normally()); 00086 assert(!exe.child_signaled()); 00087 assert(exe.child_exit_code() == 0); 00088 assert(exe.child_signal_no() == 0); 00089 assert(exe.child_exited_success()); 00090 } 00091 if (exe.is_child()) { 00092 char msg_from_parent[4096] = { 0 }; 00093 char const *msg_to_parent = "Message from child"; 00094 char const *msg_to_err = "Message from child err"; 00095 00096 if (read(exe.in_fd(), msg_from_parent, 4096) == 0) 00097 throw(ERROR(errno,"Error reading input from parent")); 00098 if (write(exe.out_fd(), msg_to_parent, strlen(msg_to_parent)) 00099 != (int)strlen(msg_to_parent)) 00100 throw(ERROR(errno,"Error writing output to parent")); 00101 if (write(exe.err_fd(), msg_to_err, strlen(msg_to_err)) 00102 != (int)strlen(msg_to_err)) 00103 throw(ERROR(errno,"Error writing error to parent")); 00104 00105 assert(static_cast<std::string>(msg_from_parent) == "Message from parent"); 00106 00107 exe.exit(); 00108 } 00109 } 00110 00111 void test3(void) 00112 { 00113 execute exe; 00114 00115 exe.fork(); 00116 if (exe.is_parent()) { 00117 char const *msg_to_child = "Message from parent"; 00118 char msg_from_child[4096] = { 0 }; 00119 char msg_from_err[4096] = { 0 }; 00120 char extra_msg[4096] = { 0 }; 00121 int extra_msg_length = 0; 00122 int i; 00123 00124 assert(exe.child_running()); 00125 assert(!exe.child_exited()); 00126 assert(!exe.child_exited_normally()); 00127 assert(!exe.child_signaled()); 00128 assert(exe.child_exit_code() == 0); 00129 assert(exe.child_signal_no() == 0); 00130 assert(!exe.child_exited_success()); 00131 00132 assert(exe.in_ready()); 00133 assert(!exe.out_ready()); 00134 assert(!exe.err_ready()); 00135 00136 sleep(1); 00137 00138 assert(exe.in_ready()); 00139 assert(!exe.out_ready()); 00140 assert(!exe.err_ready()); 00141 00142 if (write(exe.in_fd(), msg_to_child, strlen(msg_to_child)) 00143 != (int)strlen(msg_to_child)) 00144 throw(ERROR(errno,"Error writing to child's input pipe")); 00145 00146 sleep(1); 00147 00148 assert(exe.in_ready()); 00149 assert(exe.out_ready()); 00150 assert(exe.err_ready()); 00151 00152 if (read(exe.out_fd(), msg_from_child, 4096) == 0) 00153 throw(ERROR(errno,"Error reading from child's output pipe")); 00154 00155 sleep(1); 00156 00157 assert(exe.in_ready()); 00158 assert(exe.out_ready()); 00159 assert(exe.err_ready()); 00160 00161 if (read(exe.err_fd(), msg_from_err, 4096) == 0) 00162 throw(ERROR(errno,"Error reading from child's error pipe")); 00163 00164 sleep(1); 00165 00166 assert(exe.in_ready()); 00167 assert(exe.out_ready()); 00168 assert(exe.err_ready()); 00169 00170 if (exe.out_ready()) { 00171 for (i = 0; i < 4096; extra_msg[i++] = 0); 00172 extra_msg_length = read(exe.out_fd(), extra_msg, 4096); 00173 } 00174 assert(extra_msg_length == 0); 00175 if (exe.err_ready()) { 00176 for (i = 0; i < 4096; extra_msg[i++] = 0); 00177 extra_msg_length = read(exe.err_fd(), extra_msg, 4096); 00178 } 00179 assert(extra_msg_length == 0); 00180 00181 exe.wait(); 00182 00183 assert(static_cast<std::string>(msg_from_child) == "Message from child"); 00184 assert(static_cast<std::string>(msg_from_err) == "Message from child err"); 00185 assert(!exe.child_running()); 00186 assert(exe.child_exited()); 00187 assert(exe.child_exited_normally()); 00188 assert(!exe.child_signaled()); 00189 assert(exe.child_exit_code() == 0); 00190 assert(exe.child_signal_no() == 0); 00191 assert(exe.child_exited_success()); 00192 } 00193 if (exe.is_child()) { 00194 char msg_from_parent[4096] = { 0 }; 00195 char const *msg_to_parent = "Message from child"; 00196 char const *msg_to_err = "Message from child err"; 00197 00198 if (read(exe.in_fd(), msg_from_parent, 4096) == 0) 00199 throw(ERROR(errno,"Error reading input from parent")); 00200 if (write(exe.out_fd(), msg_to_parent, strlen(msg_to_parent)) 00201 != (int)strlen(msg_to_parent)) 00202 throw(ERROR(errno,"Error writing output to parent")); 00203 if (write(exe.err_fd(), msg_to_err, strlen(msg_to_err)) 00204 != (int)strlen(msg_to_err)) 00205 throw(ERROR(errno,"Error writing error to parent")); 00206 00207 assert(static_cast<std::string>(msg_from_parent) == "Message from parent"); 00208 00209 exe.exit(); 00210 } 00211 } 00212 00213 void test4(void) 00214 { 00215 execute exe; 00216 00217 exe.fork(); 00218 if (exe.is_parent()) { 00219 assert(exe.child_running()); 00220 assert(!exe.child_exited()); 00221 assert(!exe.child_exited_normally()); 00222 assert(!exe.child_signaled()); 00223 assert(exe.child_exit_code() == 0); 00224 assert(exe.child_signal_no() == 0); 00225 assert(!exe.child_exited_success()); 00226 00227 exe.wait(); 00228 00229 assert(!exe.child_running()); 00230 assert(exe.child_exited()); 00231 assert(!exe.child_exited_normally()); 00232 assert(!exe.child_signaled()); 00233 assert(exe.child_exit_code() == 5); 00234 assert(exe.child_signal_no() == 0); 00235 assert(!exe.child_exited_success()); 00236 } 00237 else { 00238 sleep(1); 00239 exe.exit(5); 00240 } 00241 } 00242 00243 void test5(void) 00244 { 00245 execute exe1, exe2; 00246 00247 exe1.fork(); 00248 if (exe1.is_child()) { 00249 // std::cerr << "child exe1 pid = " << exe1.my_pid() << std::endl; 00250 sleep(10); 00251 // std::cerr << "Child exe1 exiting" << std::endl; 00252 exe1.exit(0); 00253 } 00254 00255 exe2.fork(); 00256 if (exe2.is_child()) { 00257 // std::cerr << "child exe2 pid = " << exe2.my_pid() << std::endl; 00258 sleep(20); 00259 // std::cerr << "Child exe2 exiting" << std::endl; 00260 exe2.exit(0); 00261 } 00262 00263 assert(exe1.child_running()); 00264 assert(exe2.child_running()); 00265 00266 exe1.wait(); 00267 00268 assert(exe1.child_exited()); 00269 assert(!exe1.child_running()); 00270 assert(exe1.child_exited_success()); 00271 assert(!exe2.child_exited()); 00272 assert(exe2.child_running()); 00273 assert(!exe2.child_exited_success()); 00274 00275 exe2.wait(); 00276 00277 assert(exe1.child_exited()); 00278 assert(!exe1.child_running()); 00279 assert(exe1.child_exited_success()); 00280 assert(exe2.child_exited()); 00281 assert(!exe2.child_running()); 00282 assert(exe2.child_exited_success()); 00283 } 00284 00285 void test6(void) 00286 { 00287 execute exe1, exe2; 00288 00289 exe1.fork(); 00290 if (exe1.is_child()) { 00291 // std::cerr << "child exe1 pid = " << exe1.my_pid() << std::endl; 00292 sleep(1000); 00293 // std::cerr << "Child exe1 exiting" << std::endl; 00294 exe1.exit(0); 00295 } 00296 00297 exe2.fork(); 00298 if (exe2.is_child()) { 00299 // std::cerr << "child exe2 pid = " << exe2.my_pid() << std::endl; 00300 sleep(1000); 00301 // std::cerr << "Child exe2 exiting" << std::endl; 00302 exe2.exit(0); 00303 } 00304 00305 assert(exe1.child_running()); 00306 assert(exe2.child_running()); 00307 00308 exe1.kill_child(); 00309 while (exe1.child_running()); // Wait for the child to die 00310 00311 assert(exe1.child_exited()); 00312 assert(!exe1.child_running()); 00313 assert(!exe1.child_exited_success()); 00314 assert(exe1.child_signal_no() == SIGKILL); 00315 assert(!exe2.child_exited()); 00316 assert(exe2.child_running()); 00317 assert(!exe2.child_exited_success()); 00318 00319 exe2.kill_child(); 00320 while (exe2.child_running()); // Wait for the child to die 00321 00322 assert(exe1.child_exited()); 00323 assert(!exe1.child_running()); 00324 assert(!exe1.child_exited_success()); 00325 assert(exe1.child_signal_no() == SIGKILL); 00326 assert(exe2.child_exited()); 00327 assert(!exe2.child_running()); 00328 assert(!exe2.child_exited_success()); 00329 assert(exe2.child_signal_no() == SIGKILL); 00330 } 00331 00332 void test7(void) 00333 { 00334 execute exe; 00335 00336 exe.fork(); 00337 if (exe.is_parent()) { 00338 assert(exe.child_running()); 00339 assert(!exe.child_exited()); 00340 assert(!exe.child_exited_normally()); 00341 assert(!exe.child_signaled()); 00342 assert(exe.child_exit_code() == 0); 00343 assert(exe.child_signal_no() == 0); 00344 00345 exe.signal_child(SIGINT); 00346 exe.wait(); 00347 00348 assert(!exe.child_running()); 00349 assert(exe.child_exited()); 00350 assert(exe.child_exited_normally()); 00351 assert(!exe.child_exited_success()); 00352 assert(exe.child_signaled()); 00353 assert(exe.child_exit_code() == 0); 00354 assert(exe.child_signal_no() == SIGINT); 00355 } 00356 else { 00357 sleep(5); 00358 exe.exit(5); 00359 } 00360 } 00361 00362 void test8(void) 00363 { 00364 execute exe; 00365 std::string command = "sleep 5 ; /bin/ls -1 ./test-exec.cc"; 00366 char buffer[4096] = { 0 }; 00367 int num_bytes; 00368 00369 exe.exec(command); 00370 00371 assert(exe.child_running()); 00372 assert(!exe.child_exited()); 00373 assert(!exe.child_exited_normally()); 00374 assert(!exe.child_signaled()); 00375 assert(exe.child_exit_code() == 0); 00376 assert(exe.child_signal_no() == 0); 00377 assert(exe.in_ready()); 00378 assert(!exe.out_ready()); 00379 assert(!exe.err_ready()); 00380 00381 for (num_bytes = 0; num_bytes < 4096; buffer[num_bytes++] = 0); 00382 num_bytes = read(exe.out_fd(), buffer, 4096); 00383 assert(num_bytes = 15); 00384 assert(static_cast<std::string>(buffer) == static_cast<std::string>("./test-exec.cc\n")); 00385 00386 for (num_bytes = 0; num_bytes < 4096; buffer[num_bytes++] = 0); 00387 num_bytes = read(exe.err_fd(), buffer, 4096); 00388 assert(num_bytes == 0); 00389 assert(static_cast<std::string>(buffer).size() == 0); 00390 00391 exe.wait(); 00392 00393 assert(!exe.child_running()); 00394 assert(exe.child_exited()); 00395 assert(exe.child_exited_normally()); 00396 assert(!exe.child_signaled()); 00397 assert(exe.child_exit_code() == 0); 00398 assert(exe.child_signal_no() == 0); 00399 assert(exe.in_ready()); 00400 assert(exe.out_ready()); 00401 assert(exe.err_ready()); 00402 } 00403 00404 void test9(void) 00405 { 00406 execute exe; 00407 std::string command; 00408 std::vector<std::string> argv; 00409 char buffer[4096] = { 0 }; 00410 int num_bytes; 00411 00412 command = "/bin/ls"; 00413 argv.push_back(std::string("-1")); 00414 argv.push_back(std::string("./test-exec.cc")); 00415 exe.exec(command, argv); 00416 00417 // assert(exe.child_running()); 00418 // assert(!exe.child_exited()); 00419 // assert(!exe.child_exited_normally()); 00420 // assert(!exe.child_signaled()); 00421 // assert(exe.child_exit_code() == 0); 00422 // assert(exe.child_signal_no() == 0); 00423 // assert(exe.in_ready()); 00424 // assert(!exe.out_ready()); 00425 // assert(!exe.err_ready()); 00426 00427 for (num_bytes = 0; num_bytes < 4096; buffer[num_bytes++] = 0); 00428 num_bytes = read(exe.out_fd(), buffer, 4096); 00429 assert(num_bytes = 15); 00430 assert(static_cast<std::string>(buffer) == static_cast<std::string>("./test-exec.cc\n")); 00431 00432 for (num_bytes = 0; num_bytes < 4096; buffer[num_bytes++] = 0); 00433 num_bytes = read(exe.err_fd(), buffer, 4096); 00434 assert(num_bytes == 0); 00435 assert(static_cast<std::string>(buffer).size() == 0); 00436 00437 exe.wait(); 00438 00439 assert(!exe.child_running()); 00440 assert(exe.child_exited()); 00441 assert(exe.child_exited_normally()); 00442 assert(!exe.child_signaled()); 00443 assert(exe.child_exit_code() == 0); 00444 assert(exe.child_signal_no() == 0); 00445 assert(exe.in_ready()); 00446 assert(exe.out_ready()); 00447 assert(exe.err_ready()); 00448 } 00449 00450 void test10(void) 00451 { 00452 execute exe; 00453 ofdstream fdout; 00454 00455 exe.fork(); 00456 if (exe.is_parent()) { 00457 char const *msg_to_child = "Message from parent"; 00458 char msg_from_child[4096] = { 0 }; 00459 char msg_from_err[4096] = { 0 }; 00460 00461 assert(exe.child_running()); 00462 assert(!exe.child_exited()); 00463 assert(!exe.child_exited_normally()); 00464 assert(!exe.child_signaled()); 00465 assert(exe.child_exit_code() == 0); 00466 assert(exe.child_signal_no() == 0); 00467 assert(fdout.good()); 00468 00469 fdout.attach(exe.in_fd()); 00470 fdout << msg_to_child; 00471 fdout.flush(); 00472 00473 assert(fdout.good()); 00474 00475 if (read(exe.out_fd(), msg_from_child, 4096) == 0) 00476 throw(ERROR(errno,"Error reading from child's output pipe")); 00477 if (read(exe.err_fd(), msg_from_err, 4096) == 0) 00478 throw(ERROR(errno,"Error reading from child's error pipe")); 00479 00480 exe.wait(); 00481 00482 assert(fdout.good()); 00483 00484 assert(static_cast<std::string>(msg_from_child) == "Message from child"); 00485 assert(static_cast<std::string>(msg_from_err) == "Message from child err"); 00486 assert(!exe.child_running()); 00487 assert(exe.child_exited()); 00488 assert(exe.child_exited_normally()); 00489 assert(!exe.child_signaled()); 00490 assert(exe.child_exit_code() == 0); 00491 assert(exe.child_signal_no() == 0); 00492 } 00493 else { 00494 char msg_from_parent[4096] = { 0 }; 00495 char const *msg_to_parent = "Message from child"; 00496 char const *msg_to_err = "Message from child err"; 00497 00498 if (read(exe.in_fd(), msg_from_parent, 4096) == 0) 00499 throw(ERROR(errno,"Error reading input from parent")); 00500 if (write(exe.out_fd(), msg_to_parent, strlen(msg_to_parent)) 00501 != (int)strlen(msg_to_parent)) 00502 throw(ERROR(errno,"Error writing output to parent")); 00503 if (write(exe.err_fd(), msg_to_err, strlen(msg_to_err)) 00504 != (int)strlen(msg_to_err)) 00505 throw(ERROR(errno,"Error writing error to parent")); 00506 00507 assert(static_cast<std::string>(msg_from_parent) == "Message from parent"); 00508 00509 exe.exit(); 00510 } 00511 } 00512 00513 void test11(void) 00514 { 00515 execute exe; 00516 ofdstream fdout; 00517 ifdstream fdin; 00518 00519 exe.fork(); 00520 if (exe.is_parent()) { 00521 char const *msg_to_child = "Message from parent"; 00522 char msg_from_child[4096] = { 0 }; 00523 char msg_from_err[4096] = { 0 }; 00524 00525 assert(exe.child_running()); 00526 assert(!exe.child_exited()); 00527 assert(!exe.child_exited_normally()); 00528 assert(!exe.child_signaled()); 00529 assert(exe.child_exit_code() == 0); 00530 assert(exe.child_signal_no() == 0); 00531 00532 fdout.attach(exe.in_fd()); 00533 fdout << msg_to_child; 00534 fdout.flush(); 00535 00536 fdin.attach(exe.out_fd()); 00537 fdin.get(msg_from_child,4096); 00538 00539 if (read(exe.err_fd(), msg_from_err, 4096) == 0) 00540 throw(ERROR(errno,"Error reading from child's error pipe")); 00541 00542 exe.wait(); 00543 00544 assert(static_cast<std::string>(msg_from_child) == "Message from child"); 00545 assert(static_cast<std::string>(msg_from_err) == "Message from child err"); 00546 assert(!exe.child_running()); 00547 assert(exe.child_exited()); 00548 assert(exe.child_exited_normally()); 00549 assert(!exe.child_signaled()); 00550 assert(exe.child_exit_code() == 0); 00551 assert(exe.child_signal_no() == 0); 00552 } 00553 else { 00554 char msg_from_parent[4096] = { 0 }; 00555 char const *msg_to_parent = "Message from child"; 00556 char const *msg_to_err = "Message from child err"; 00557 00558 if (read(exe.in_fd(), msg_from_parent, 4096) == 0) 00559 throw(ERROR(errno,"Error reading input from parent")); 00560 if (write(exe.out_fd(), msg_to_parent, strlen(msg_to_parent)) 00561 != (int)strlen(msg_to_parent)) 00562 throw(ERROR(errno,"Error writing output to parent")); 00563 if (write(exe.err_fd(), msg_to_err, strlen(msg_to_err)) 00564 != (int)strlen(msg_to_err)) 00565 throw(ERROR(errno,"Error writing error to parent")); 00566 00567 assert(static_cast<std::string>(msg_from_parent) == "Message from parent"); 00568 00569 exe.exit(); 00570 } 00571 } 00572 00573 void test12(void) 00574 { 00575 execute exe; 00576 ofdstream fdout; 00577 ifdstream fdin; 00578 00579 exe.fork(); 00580 if (exe.is_parent()) { 00581 char const *msg_to_child = "Message from parent"; 00582 char msg_from_child[4096] = { 0 }; 00583 char msg_from_err[4096] = { 0 }; 00584 00585 assert(exe.child_running()); 00586 assert(!exe.child_exited()); 00587 assert(!exe.child_exited_normally()); 00588 assert(!exe.child_signaled()); 00589 assert(exe.child_exit_code() == 0); 00590 assert(exe.child_signal_no() == 0); 00591 00592 fdout.attach(exe.in_fd()); 00593 // fdout << msg_to_child; 00594 // fdout.flush(); 00595 if (write(exe.in_fd(), msg_to_child, strlen(msg_to_child)) 00596 != (int)strlen(msg_to_child)) 00597 throw(ERROR(errno,"Error writing to child's input pipe")); 00598 00599 fdin.attach(exe.out_fd()); 00600 // fdin.get(msg_from_child,4096); 00601 if (read(exe.out_fd(), msg_from_child, 4096) == 0) 00602 throw(ERROR(errno,"Error reading from child's output pipe")); 00603 00604 if (read(exe.err_fd(), msg_from_err, 4096) == 0) 00605 throw(ERROR(errno,"Error reading from child's error pipe")); 00606 00607 exe.wait(); 00608 00609 assert(static_cast<std::string>(msg_from_child) == "Message from child"); 00610 assert(static_cast<std::string>(msg_from_err) == "Message from child err"); 00611 assert(!exe.child_running()); 00612 assert(exe.child_exited()); 00613 assert(exe.child_exited_normally()); 00614 assert(!exe.child_signaled()); 00615 assert(exe.child_exit_code() == 0); 00616 assert(exe.child_signal_no() == 0); 00617 } 00618 else { 00619 char msg_from_parent[4096] = { 0 }; 00620 char const *msg_to_parent = "Message from child"; 00621 char const *msg_to_err = "Message from child err"; 00622 00623 if (read(exe.in_fd(), msg_from_parent, 4096) == 0) 00624 throw(ERROR(errno,"Error reading input from parent")); 00625 if (write(exe.out_fd(), msg_to_parent, strlen(msg_to_parent)) 00626 != (int)strlen(msg_to_parent)) 00627 throw(ERROR(errno,"Error writing output to parent")); 00628 if (write(exe.err_fd(), msg_to_err, strlen(msg_to_err)) 00629 != (int)strlen(msg_to_err)) 00630 throw(ERROR(errno,"Error writing error to parent")); 00631 00632 assert(static_cast<std::string>(msg_from_parent) == "Message from parent"); 00633 00634 exe.exit(); 00635 } 00636 } 00637 00638 void test13(void) 00639 { 00640 execute exe; 00641 00642 exe.fork(); 00643 if (exe.is_parent()) { 00644 char const * msg_to_child = "Message from parent"; 00645 char msg_from_child[4096] = { 0 }; 00646 char msg_from_err[4096] = { 0 }; 00647 00648 assert(exe.child_running()); 00649 assert(!exe.child_exited()); 00650 assert(!exe.child_exited_normally()); 00651 assert(!exe.child_signaled()); 00652 assert(exe.child_exit_code() == 0); 00653 assert(exe.child_signal_no() == 0); 00654 00655 assert(exe.in_ready()); 00656 assert(!exe.out_ready()); 00657 assert(!exe.err_ready()); 00658 assert(!exe.in_eof()); 00659 assert(!exe.out_eof()); 00660 assert(!exe.err_eof()); 00661 00662 if (exe.in_write(msg_to_child, strlen(msg_to_child)) 00663 != (int)strlen(msg_to_child)) 00664 throw(ERROR(errno,"Error writing to child's input pipe")); 00665 00666 // Wait until input is ready from child 00667 while (!(exe.out_ready() && exe.err_ready())); 00668 00669 assert(exe.in_ready()); 00670 assert(exe.out_ready()); 00671 assert(exe.err_ready()); 00672 assert(!exe.in_eof()); 00673 assert(!exe.out_eof()); 00674 assert(!exe.err_eof()); 00675 00676 if (exe.out_read(msg_from_child, 4096) == 0) 00677 throw(ERROR(errno,"Error reading from child's output pipe")); 00678 00679 assert(exe.in_ready()); 00680 00681 assert(!exe.out_ready() || !exe.child_running()); 00682 assert(exe.err_ready()); 00683 assert(!exe.in_eof()); 00684 assert(!exe.out_eof()); 00685 assert(!exe.err_eof()); 00686 00687 if (exe.err_read(msg_from_err, 4096) == 0) 00688 throw(ERROR(errno,"Error reading from child's error pipe")); 00689 00690 while (!exe.out_ready() && !exe.err_ready()); 00691 00692 assert(exe.in_ready()); 00693 assert(exe.out_ready()); 00694 assert(exe.err_ready()); 00695 assert(!exe.in_eof()); 00696 assert(!exe.out_eof()); 00697 assert(!exe.err_eof()); 00698 00699 exe.wait(); 00700 00701 assert(exe.in_ready()); 00702 assert(exe.out_ready()); 00703 assert(exe.err_ready()); 00704 assert(!exe.in_eof()); 00705 assert(!exe.out_eof()); 00706 assert(!exe.err_eof()); 00707 00708 assert(static_cast<std::string>(msg_from_child) == "Message from child"); 00709 assert(static_cast<std::string>(msg_from_err) == "Message from child err"); 00710 00711 assert(exe.out_read(msg_from_child, 4096) == 0); 00712 assert(exe.in_ready()); 00713 assert(exe.out_ready()); 00714 assert(exe.err_ready()); 00715 assert(!exe.in_eof()); 00716 assert(exe.out_eof()); 00717 assert(!exe.err_eof()); 00718 00719 assert(!exe.child_running()); 00720 assert(exe.child_exited()); 00721 assert(exe.child_exited_normally()); 00722 assert(!exe.child_signaled()); 00723 assert(exe.child_exit_code() == 0); 00724 assert(exe.child_signal_no() == 0); 00725 } 00726 if (exe.is_child()) { 00727 char msg_from_parent[4096] = { 0 }; 00728 char const * msg_to_parent = "Message from child"; 00729 char const * msg_to_err = "Message from child err"; 00730 00731 if (read(exe.in_fd(), msg_from_parent, 4096) == 0) 00732 throw(ERROR(errno,"Error reading input from parent")); 00733 if (write(exe.out_fd(), msg_to_parent, strlen(msg_to_parent)) 00734 != (int)strlen(msg_to_parent)) 00735 throw(ERROR(errno,"Error writing output to parent")); 00736 if (write(exe.err_fd(), msg_to_err, strlen(msg_to_err)) 00737 != (int)strlen(msg_to_err)) 00738 throw(ERROR(errno,"Error writing error to parent")); 00739 00740 assert(static_cast<std::string>(msg_from_parent) == "Message from parent"); 00741 00742 sleep(5); 00743 exe.exit(); 00744 } 00745 } 00746 00747 int main(int argc, char const * argv[]) 00748 { 00749 try { 00750 test1(); 00751 test2(); 00752 test3(); 00753 test4(); 00754 test5(); 00755 test6(); 00756 test7(); 00757 test8(); 00758 test9(); 00759 test10(); 00760 test11(); 00761 test12(); 00762 test13(); 00763 } 00764 catch(error e) { 00765 std::cerr << e; 00766 assert(0); 00767 } 00768 catch(...) { 00769 std::cerr << err_unknown; 00770 assert(0); 00771 } 00772 return(0); 00773 } 00774