From 889adc499d3666930e5e82841771bd630f8a4a15 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Tue, 31 May 2016 16:14:50 +0200 Subject: [PATCH] =?UTF-8?q?lib=20+=20pingpong=20avec=20v=C3=A9rification?= =?UTF-8?q?=20de=20la=20pr=C3=A9sence=20des=20pipes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/communication.c | 40 ++++++++++++++++++++++++---------------- pingpong/pingpong.c | 8 ++++---- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/lib/communication.c b/lib/communication.c index 9e5a382..d3e5c2b 100644 --- a/lib/communication.c +++ b/lib/communication.c @@ -300,6 +300,10 @@ int process_open_in (struct process *proc) printf ("opening in %s\n", fifopathin); proc->in = fopen (fifopathin, "rb"); + if (proc->in == NULL) { + fprintf (stderr, "\033[31mnot opened\033[00m\n"); + return -1; + } printf ("opened : %d\n", proc->in); return 0; @@ -313,6 +317,10 @@ int service_proc_open_in (struct process *proc) printf ("opening in %s\n", fifopathin); proc->in = fopen (fifopathin, "wb"); + if (proc->in == NULL) { + fprintf (stderr, "\033[31mnot opened\033[00m\n"); + return -1; + } printf ("opened : %d\n", proc->in); return 0; @@ -326,6 +334,10 @@ int service_proc_open_out (struct process *proc) printf ("opening out %s\n", fifopathout); proc->out = fopen (fifopathout, "rb"); + if (proc->out == NULL) { + fprintf (stderr, "\033[31mnot opened\033[00m\n"); + return -1; + } printf ("opened\n"); return 0; @@ -339,6 +351,10 @@ int process_open_out (struct process *proc) printf ("opening out %s\n", fifopathout); proc->out = fopen (fifopathout, "wb"); + if (proc->out == NULL) { + fprintf (stderr, "\033[31mnot opened\033[00m\n"); + return -1; + } printf ("opened\n"); return 0; @@ -371,16 +387,14 @@ int process_read (struct process *proc, void * buf, size_t * msize) { int ret; if ((ret = process_open_in (proc))) { - fprintf(stdout, "error process_open_in %d\n", ret); - exit (1); + return 1; } *msize = fread (buf, 1, *msize, proc->in); // FIXME check errors // printf ("DEBUG read, size %ld : %s\n", *msize, buf); if ((ret = process_close_in (proc))) { - fprintf(stdout, "error process_close_in %d\n", ret); - exit (1); + return 1; } return 0; @@ -390,15 +404,13 @@ int process_write (struct process *proc, void * buf, size_t msize) { int ret; if ((ret = process_open_out (proc))) { - fprintf(stdout, "error process_create %d\n", ret); - exit (1); + return 1; } fwrite (buf, 1, msize, proc->out); // FIXME check errors if ((ret = process_close_out (proc))) { - fprintf(stdout, "error process_close_out %d\n", ret); - exit (1); + return 1; } return 0; } @@ -407,16 +419,14 @@ int service_read (struct process *proc, void * buf, size_t * msize) { int ret; if ((ret = service_proc_open_out (proc))) { - fprintf(stdout, "error process_open_out %d\n", ret); - exit (1); + return 1; } *msize = fread (buf, 1, *msize, proc->out); // FIXME check errors // printf ("DEBUG read, size %ld : %s\n", *msize, buf); if ((ret = process_close_out (proc))) { - fprintf(stdout, "error process_close_out %d\n", ret); - exit (1); + return 1; } return 0; @@ -426,15 +436,13 @@ int service_write (struct process *proc, void * buf, size_t msize) { int ret; if ((ret = service_proc_open_in (proc))) { - fprintf(stdout, "error process_open_in %d\n", ret); - exit (1); + return 1; } fwrite (buf, 1, msize, proc->in); // FIXME check errors if ((ret = process_close_in (proc))) { - fprintf(stdout, "error process_close_in %d\n", ret); - exit (1); + return 1; } return 0; } diff --git a/pingpong/pingpong.c b/pingpong/pingpong.c index b6e5b6d..6b78ddc 100644 --- a/pingpong/pingpong.c +++ b/pingpong/pingpong.c @@ -19,8 +19,8 @@ void main_loop (const char *spath) // -1 : error, 0 = no new process, 1 = new process ret = service_get_new_process (&proc, spath); if (ret == -1) { - fprintf (stderr, "Error service_get_new_process\n"); - exit (1); + fprintf (stderr, "error service_get_new_process\n"); + continue; } else if (ret == 0) { // that should not happen continue; } @@ -37,7 +37,7 @@ void main_loop (const char *spath) // printf ("before read\n"); if ((ret = service_read (&proc, &buf, &msize))) { fprintf(stdout, "error service_read %d\n", ret); - exit (1); + continue; } // printf ("after read\n"); printf ("read, size %ld : %s\n", msize, buf); @@ -45,7 +45,7 @@ void main_loop (const char *spath) // printf ("before proc write\n"); if ((ret = service_write (&proc, &buf, msize))) { fprintf(stdout, "error service_write %d\n", ret); - exit (1); + continue; } // printf ("after proc write\n"); printf ("\033[32mStill \033[31m%d\033[32m applications to serve\n",cnt);