- int pid=fork();
- if (pid<0) {
- perror("Cannot fork the command");
- exit(4);
- }
-
- if (pid) { /* father */
- char buffout[4096];
- int posw,posr;
- int status;
- close(child_stdin[0]);
- fcntl(child_stdin[1], F_SETFL, O_NONBLOCK);
- close(child_stdout[1]);
- fcntl(child_stdout[0], F_SETFL, O_NONBLOCK);
-
- brokenpipe = 0;
- for (posw=0; posw<input->used && !brokenpipe; ) {
- int got;
- // fprintf(stderr,"Still %d chars to write\n",input->used-posw);
- got=write(child_stdin[1],input->data+posw,input->used-posw);
- if (got>0)
- posw+=got;
- if (got<0 && errno!=EINTR && errno!=EAGAIN && errno!=EPIPE) {
- perror("Error while writing input to child");
- exit(4);
- }
- // fprintf(stderr,"written %d chars so far\n",posw);
-
- posr=read(child_stdout[0],&buffout,4096);
- // fprintf(stderr,"got %d chars\n",posr);
- if (posr<0 && errno!=EINTR && errno!=EAGAIN) {
- perror("Error while reading output of child");
- exit(4);
- }
- if (posr>0) {
- buffout[posr]='\0';
- buff_append(output_got,buffout);
- }
-
- if (got <= 0 && posr <= 0)
- usleep(100);
- }
- input->data[0]='\0';
- input->used=0;
- close(child_stdin[1]);
-
- timeouted = 0;
- alarm(timeout_value);
- do {
- posr=read(child_stdout[0],&buffout,4096);
- if (posr<0 && errno!=EINTR && errno!=EAGAIN) {
- perror("Error while reading output of child");
- exit(4);
- }
- if (posr>0) {
- buffout[posr]='\0';
- buff_append(output_got,buffout);
- } else {
- usleep(100);
- }
- } while (!timeouted && posr!=0);
-
- /* Check for broken pipe */
- if (brokenpipe && verbose) {
- fprintf(stderr,"Warning: Child did not consume all its input (I got broken pipe)\n");
- }
-
- /* Check for timeouts */
- if (timeouted) {
- fprintf(stderr,"Child timeouted (waited %d sec)\n",timeout_value);
- exit(3);
- }
- alarm(0);
-
-
- /* Wait for child, and check why it terminated */
- wait(&status);
-
- if (WIFSIGNALED(status) && strcmp(signal_name(WTERMSIG(status)),expected_signal)) {
- fprintf(stderr,"Child got signal %s instead of signal %s\n",signal_name(WTERMSIG(status)), expected_signal);
- exit(WTERMSIG(status)+4);
- }
-
- if (!WIFSIGNALED(status) && expected_signal) {
- fprintf(stderr,"Child didn't got expected signal %s\n",
- expected_signal);
- exit(5);
- }
-
- if (WIFEXITED(status) && WEXITSTATUS(status) != expected_return ) {
- if (expected_return)
- fprintf(stderr,"Child returned code %d instead of %d\n",
- WEXITSTATUS(status), expected_return);
- else
- fprintf(stderr,"Child returned code %d\n", WEXITSTATUS(status));
- exit(40+WEXITSTATUS(status));
- }
- expected_return = 0;
-
- if(expected_signal){
- free(expected_signal);
- expected_signal = NULL;
- }