-#include "portable.h"
-#include "xbt/sysdep.h"
-#include "xbt/function_types.h"
-#include "xbt/log.h"
-
-#include <sys/types.h>
-#include <sys/wait.h>
-
-/**
- ** Buffer code
- **/
-typedef struct {
- char *data;
- int used,size;
-} buff_t;
-
-static void buff_empty(buff_t *b) {
- b->used=0;
- b->data[0]='\n';
- b->data[1]='\0';
-}
-static buff_t *buff_new(void) {
- buff_t *res=malloc(sizeof(buff_t));
- res->data=malloc(512);
- res->size=512;
- buff_empty(res);
- return res;
-}
-static void buff_free(buff_t *b) {
- if (b) {
- if (b->data)
- free(b->data);
- free(b);
- }
-}
-static void buff_append(buff_t *b, char *toadd) {
- int addlen=strlen(toadd);
- int needed_space=b->used+addlen+1;
-
- if (needed_space > b->size) {
- b->data = realloc(b->data, needed_space);
- b->size = needed_space;
- }
- strcpy(b->data+b->used, toadd);
- b->used += addlen;
-}
-static void buff_chomp(buff_t *b) {
- while (b->data[b->used] == '\n') {
- b->data[b->used] = '\0';
- if (b->used)
- b->used--;
- }
-}
-/**
- ** Options
- **/
-int timeout_value = 5; /* child timeout value */
-int expected_signal=0; /* !=0 if the following command should raise a signal */
-int expected_return=0; /* the exepeted return code of following command */
-int verbose=0; /* wheather we should wine on problems */
-
-/**
- ** Dealing with timeouts
- **/
-int timeouted;
-static void timeout_handler(int sig) {
- timeouted = 1;
-}
-/**
- ** Dealing with timeouts
- **/
-int brokenpipe;
-static void pipe_handler(int sig) {
- brokenpipe = 1;
-}
-
-/**
- ** Launching a child
- **/
-buff_t *input;
-buff_t *output_wanted;
-buff_t *output_got;
-
-static void check_output() {
- if (output_wanted->used==0
- && output_got->used==0)
- return;
- buff_chomp(output_got);
- buff_chomp(output_wanted);
-
- if ( output_got->used != output_wanted->used
- || strcmp(output_got->data, output_wanted->data)) {
- fprintf(stderr,"Output don't match expectations\n");
- fprintf(stderr,">>>>> Expected %d chars:\n%s\n<<<<< Expected\n",
- output_wanted->used,output_wanted->data);
- fprintf(stderr,">>>>> Got %d chars:\n%s\n<<<<< Got\n",
- output_got->used,output_got->data);
- exit(2);
- }
- buff_empty(output_wanted);
- buff_empty(output_got);
-
-}
-
-static void exec_cmd(char *cmd) {
- int child_stdin[2];
- int child_stdout[2];
-
- if (pipe(child_stdin) || pipe(child_stdout)) {
- perror("Cannot open the pipes");
- exit(4);
- }
-
- 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) && WTERMSIG(status) != expected_signal) {
- fprintf(stderr,"Child got signal %d instead of signal %d\n",
- WTERMSIG(status), expected_signal);
- exit(WTERMSIG(status)+4);
- }
- if (!WIFSIGNALED(status) && expected_signal != 0) {
- fprintf(stderr,"Child didn't got expected signal %d\n",
- expected_signal);
- exit(5);
- }