From e80f19ffccd6838f2dfaf61baf26f34d0b2a064e Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Mon, 30 May 2016 01:54:19 +0200
Subject: [PATCH 1/3] service de test fonctionne
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Protocole de test :
echo "1 1 1" > /tmp/ipc/windows
echo "blah" > /tmp/ipc/1-1-in
cat /tmp/ipc/1-1-out
Ce qui n'a aucun sens, faut changer ça.
---
lib/communication.c | 131 ++++++++++++++++++++++++++++++--------------
lib/communication.h | 13 +++--
service-test.c | 78 ++++++++++++++++++++------
3 files changed, 160 insertions(+), 62 deletions(-)
diff --git a/lib/communication.c b/lib/communication.c
index 944da9f..9aae555 100644
--- a/lib/communication.c
+++ b/lib/communication.c
@@ -1,4 +1,6 @@
#include "communication.h"
+#include
+#include
int service_path (char *buf, const char *sname)
{
@@ -70,17 +72,31 @@ int service_close (const char *fifopath)
return 0;
}
-// FIXME only works for a single process
-void service_get_new_processes (struct process **proc, int *nproc, int sfifo)
+int service_get_new_process (struct process *proc, const char * spath)
{
+ if (spath == NULL) {
+ return -1;
+ }
+
char buf[BUFSIZ];
bzero (buf, BUFSIZ);
- read (sfifo, buf, BUFSIZ);
- //proc[0] = malloc(sizeof(struct process*) * 1); // FIXME
- proc[0] = malloc(sizeof(struct process) * 1); // FIXME
+ // read the pipe, get a process to work on
+ int ret;
- // unsigned long long int val = strtoul(s, NULL, 10);
+ struct timespec ts = { 0 };
+ struct timespec ts2 = { 0 };
+
+ FILE * f = fopen (spath, "r");
+ clock_gettime(CLOCK_REALTIME, &ts);
+ fgets (buf, BUFSIZ, f);
+ clock_gettime(CLOCK_REALTIME, &ts2);
+ fclose (f);
+
+ printf("sec: %ld nsec: %ld\n", ts.tv_sec, ts.tv_nsec);
+ printf("sec: %ld nsec: %ld\n", ts2.tv_sec, ts2.tv_nsec);
+
+ printf("diff nsec: %ld\n", ts2.tv_nsec - ts.tv_nsec);
char *token, *saveptr;
char *str;
@@ -91,27 +107,71 @@ void service_get_new_processes (struct process **proc, int *nproc, int sfifo)
if (token == NULL)
break;
- printf ("token : %s\n", token);
-
- // do something
if (i == 1) {
- int index = strchr (token, '-') - token;
- char * buf_pid = strndup (token, index);
- proc[0]->pid = strtoul(buf_pid, NULL, 10);
- proc[0]->index = strtoul(token + index +1, NULL, 10);
-
- printf ("buf_pid : %s\n", buf_pid);
- printf ("pid : %d\n", proc[0]->pid);
- printf ("index : %d\n", proc[0]->index);
+ proc->pid = strtoul(token, NULL, 10);
}
else if (i == 2) {
- // FIXME
- proc[0]->version = strtoul(token, NULL, 10);
+ proc->index = strtoul(token, NULL, 10);
+ }
+ else if (i == 3) {
+ proc->version = strtoul(token, NULL, 10);
}
-
}
- *nproc = 1;
+ return 1;
+}
+
+// FIXME only works for a single process
+void service_get_new_processes (struct process ***proc, int *nproc, char * spath)
+{
+ if (proc == NULL || spath == NULL) {
+ return;
+ }
+
+ char buf[BUFSIZ];
+ bzero (buf, BUFSIZ);
+
+ // read the pipe, get a process to work on
+ FILE * f = fopen (spath, "rb");
+ fgets (buf, BUFSIZ, f);
+ fclose (f);
+
+ char *token, *line, *saveptr, *saveptr2;
+ char *str, *str2;
+ int i, j;
+
+ *nproc = 0;
+ proc[0] = malloc(sizeof(struct process**));
+
+ for (str2 = buf, j = 1; ; str2 = NULL, j++) {
+ line = strtok_r(str2, "\n", &saveptr2);
+ if (line == NULL)
+ break;
+
+ printf ("line : %s\n", line);
+
+ *nproc = *nproc +1;
+
+ proc[0] = realloc(proc[0], sizeof(struct process*) * (*nproc));
+ proc[0][*nproc -1] = malloc(sizeof(struct process));
+
+ for (str = line, i = 1; ; str = NULL, i++) {
+ token = strtok_r(str, " ", &saveptr);
+ if (token == NULL)
+ break;
+
+ if (i == 1) {
+ proc[0][*nproc -1]->pid = strtoul(token, NULL, 10);
+ }
+ else if (i == 2) {
+ proc[0][*nproc -1]->index = strtoul(token, NULL, 10);
+ }
+ else if (i == 3) {
+ proc[0][*nproc -1]->version = strtoul(token, NULL, 10);
+ }
+
+ }
+ }
}
void struct_process_free (struct process * p)
@@ -121,7 +181,10 @@ void struct_process_free (struct process * p)
void service_free_processes (struct process **procs, int nproc)
{
- while (--nproc != -1) {
+ printf ("free processes\n");
+ while (nproc--) {
+ printf ("free process %d\n", nproc);
+
struct_process_free (procs[nproc]);
}
}
@@ -135,7 +198,8 @@ void gen_process_structure (struct process *p
}
-void process_print (struct process *p) {
+void process_print (struct process *p)
+{
printf ("process %d : index %d\n", p->pid, p->index);
}
@@ -238,7 +302,7 @@ int process_open_in (struct process *proc)
printf ("opening in %s\n", fifopathin);
proc->in = fopen (fifopathin, "rb");
- printf ("opened\n");
+ printf ("opened : %d\n", proc->in);
return 0;
}
@@ -256,27 +320,13 @@ int process_open_out (struct process *proc)
return 0;
}
-
-int process_open (struct process *proc)
-{
- char fifopathin[PATH_MAX];
- char fifopathout[PATH_MAX];
- process_paths (fifopathin, fifopathout, proc->pid, proc->index);
-
- printf ("opening %s\n", fifopathin);
- proc->in = fopen (fifopathin, "rb");
- printf ("opening %s\n", fifopathout);
- proc->out = fopen (fifopathout, "wb");
- printf ("opened\n");
-
- return 0;
-}
-
int process_close_in (struct process *proc)
{
printf ("closing in\n");
if (proc->in != 0) {
+ printf ("before fclose in\n");
fclose (proc->in);
+ printf ("after fclose in\n");
proc->in = 0;
}
return 0;
@@ -302,6 +352,7 @@ int process_read (struct process *proc, void * buf, size_t * msize)
}
*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);
diff --git a/lib/communication.h b/lib/communication.h
index 05214d4..2029e0b 100644
--- a/lib/communication.h
+++ b/lib/communication.h
@@ -13,7 +13,7 @@
#include // error numbers
-#define TMPDIR "/tmp/"
+#define TMPDIR "/tmp/ipc/"
#define COMMUNICATION_VERSION 1
@@ -27,6 +27,11 @@ struct process {
FILE *in, *out;
};
+struct service {
+ unsigned int version;
+ unsigned int index;
+};
+
// TODO create the service process structure
int service_path (char *buf, const char *sname);
@@ -44,16 +49,14 @@ void gen_process_structure (struct process *p
int service_create (const char *sname);
int service_close (const char *sname);
-void service_get_new_processes (struct process **, int *nproc, int sfifo);
+int service_get_new_process (struct process *proc, const char * spath);
+void service_get_new_processes (struct process ***, int *nproc, char *spath);
void service_free_processes (struct process **, int nproc);
void process_print (struct process *);
int process_create (struct process *, int index); // called by the application
int process_destroy (struct process *); // called by the application
-int process_open (struct process *); // called by the service & application
-int process_close (struct process *); // called by the service & application
-
int process_read (struct process *, void * buf, size_t *);
int process_write (struct process *, void * buf, size_t);
diff --git a/service-test.c b/service-test.c
index 1d6b734..c7ac8d1 100644
--- a/service-test.c
+++ b/service-test.c
@@ -11,41 +11,85 @@
void main_loop (const char *spath)
{
int ret;
- int sfifo = open (spath, S_IRUSR); // opens the service named pipe
+ struct process proc;
+#if 1
+ while (1) {
- struct process *proc;
+ // -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);
+ } else if (ret == 0) {
+ continue;
+ }
+
+ printf ("before print\n");
+ process_print (&proc);
+ printf ("after print\n");
+
+ // about the message
+ size_t msize = BUFSIZ;
+ char buf[BUFSIZ];
+ bzero(buf, BUFSIZ);
+
+ printf ("before read\n");
+ if ((ret = process_read (&proc, &buf, &msize))) {
+ fprintf(stdout, "error process_read %d\n", ret);
+ exit (1);
+ }
+ printf ("after read\n");
+ printf ("read, size %ld : %s\n", msize, buf);
+
+ printf ("before proc write\n");
+ if ((ret = process_write (&proc, &buf, msize))) {
+ fprintf(stdout, "error process_write %d\n", ret);
+ exit (1);
+ }
+ printf ("after proc write\n");
+ }
+#endif
+
+#if 0
+
+ int ret;
+ struct process **proc;
int nproc = 0;
-
- do {
- service_get_new_processes (&proc, &nproc, sfifo);
-
- printf ("nb proc : %d\n", nproc);
+ while (1) {
+ service_get_new_processes (&proc, &nproc, spath);
// for each process : open, read, write, close
for (int i = 0 ; i < nproc ; i++) {
- process_print (&proc[i]);
+ printf ("before print\n");
+ process_print (proc[i]);
+ printf ("after print, i = %d\n", i);
// about the message
size_t msize = BUFSIZ;
char buf[BUFSIZ];
+ bzero(buf, BUFSIZ);
- if ((ret = process_read (&proc[i], &buf, &msize))) {
+ printf ("before read\n");
+ if ((ret = process_read (proc[i], &buf, &msize))) {
fprintf(stdout, "error process_read %d\n", ret);
exit (1);
}
-
+ printf ("after read\n");
printf ("read, size %ld : %s\n", msize, buf);
- if ((ret = process_write (&proc[i], &buf, msize))) {
- fprintf(stdout, "error process_read %d\n", ret);
+ printf ("before proc write\n");
+ if ((ret = process_write (proc[i], &buf, msize))) {
+ fprintf(stdout, "error process_write %d\n", ret);
exit (1);
}
+ printf ("after proc write\n");
}
-
- service_free_processes (&proc, nproc);
- } while (0); // it's a test, we only do it once
-
- close (sfifo); // closes the service named pipe
+ service_free_processes (proc, nproc);
+ free (proc);
+ }
+#endif
}
/*
From 6b2ad54284dff429f101ec45f1d12e17970e20b7 Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Mon, 30 May 2016 16:30:05 +0200
Subject: [PATCH 2/3] pingpong service with test application script : working
---
lib/communication.c | 60 +++++++++++++++---
lib/communication.h | 3 +
pingpong/Makefile | 22 +++++++
pingpong/README.markdown | 31 ++++++++++
pingpong/pingpong.c | 88 +++++++++++++++++++++++++++
pingpong/pingpong.sh | 37 +++++++++++
service-test.c | 128 ---------------------------------------
7 files changed, 233 insertions(+), 136 deletions(-)
create mode 100644 pingpong/Makefile
create mode 100644 pingpong/README.markdown
create mode 100644 pingpong/pingpong.c
create mode 100755 pingpong/pingpong.sh
delete mode 100644 service-test.c
diff --git a/lib/communication.c b/lib/communication.c
index 9aae555..9e5a382 100644
--- a/lib/communication.c
+++ b/lib/communication.c
@@ -82,8 +82,6 @@ int service_get_new_process (struct process *proc, const char * spath)
bzero (buf, BUFSIZ);
// read the pipe, get a process to work on
- int ret;
-
struct timespec ts = { 0 };
struct timespec ts2 = { 0 };
@@ -307,6 +305,32 @@ int process_open_in (struct process *proc)
return 0;
}
+int service_proc_open_in (struct process *proc)
+{
+ char fifopathin[PATH_MAX];
+ char fifopathout[PATH_MAX];
+ process_paths (fifopathin, fifopathout, proc->pid, proc->index);
+
+ printf ("opening in %s\n", fifopathin);
+ proc->in = fopen (fifopathin, "wb");
+ printf ("opened : %d\n", proc->in);
+
+ return 0;
+}
+
+int service_proc_open_out (struct process *proc)
+{
+ char fifopathin[PATH_MAX];
+ char fifopathout[PATH_MAX];
+ process_paths (fifopathin, fifopathout, proc->pid, proc->index);
+
+ printf ("opening out %s\n", fifopathout);
+ proc->out = fopen (fifopathout, "rb");
+ printf ("opened\n");
+
+ return 0;
+}
+
int process_open_out (struct process *proc)
{
char fifopathin[PATH_MAX];
@@ -347,12 +371,12 @@ int process_read (struct process *proc, void * buf, size_t * msize)
{
int ret;
if ((ret = process_open_in (proc))) {
- fprintf(stdout, "error process_create %d\n", ret);
+ fprintf(stdout, "error process_open_in %d\n", ret);
exit (1);
}
*msize = fread (buf, 1, *msize, proc->in); // FIXME check errors
- printf ("DEBUG read, size %ld : %s\n", *msize, buf);
+ // printf ("DEBUG read, size %ld : %s\n", *msize, buf);
if ((ret = process_close_in (proc))) {
fprintf(stdout, "error process_close_in %d\n", ret);
@@ -381,16 +405,36 @@ int process_write (struct process *proc, void * buf, size_t msize)
int service_read (struct process *proc, void * buf, size_t * msize)
{
- if ((*msize = fread (buf, 1, *msize, proc->out)))
- return *msize;
+ int ret;
+ if ((ret = service_proc_open_out (proc))) {
+ fprintf(stdout, "error process_open_out %d\n", ret);
+ exit (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 0;
}
int service_write (struct process *proc, void * buf, size_t msize)
{
- if ((msize = fwrite (buf, 1, msize, proc->in)))
- return msize;
+ int ret;
+ if ((ret = service_proc_open_in (proc))) {
+ fprintf(stdout, "error process_open_in %d\n", ret);
+ exit (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 0;
}
diff --git a/lib/communication.h b/lib/communication.h
index 2029e0b..4ab656c 100644
--- a/lib/communication.h
+++ b/lib/communication.h
@@ -60,4 +60,7 @@ int process_destroy (struct process *); // called by the application
int process_read (struct process *, void * buf, size_t *);
int process_write (struct process *, void * buf, size_t);
+int service_read (struct process *, void * buf, size_t *);
+int service_write (struct process *, void * buf, size_t);
+
#endif
diff --git a/pingpong/Makefile b/pingpong/Makefile
new file mode 100644
index 0000000..22d244e
--- /dev/null
+++ b/pingpong/Makefile
@@ -0,0 +1,22 @@
+CC=gcc
+CFLAGS=-Wall -g
+LDFLAGS=
+CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change
+EXEC=$(basename $(wildcard *.c))
+SOURCES=$(wildcard ../lib/*.c)
+OBJECTS=$(SOURCES:.c=.o)
+TESTS=$(addsuffix .test, $(EXEC))
+
+all: $(SOURCES) $(EXEC)
+
+$(EXEC): $(OBJECTS) $(CFILES)
+ $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@
+
+.c.o:
+ $(CC) -c $(CFLAGS) $< -o $@
+
+clean:
+ -rm $(OBJECTS)
+
+mrproper: clean
+ rm $(EXEC)
diff --git a/pingpong/README.markdown b/pingpong/README.markdown
new file mode 100644
index 0000000..b349e7f
--- /dev/null
+++ b/pingpong/README.markdown
@@ -0,0 +1,31 @@
+# Service ping-pong
+
+This service is a brain-dead application. It is only to a pedagogic end.
+
+The purpose is only to communicate with an application once, the application
+sends a message and the service answer with the same message.
+
+# How it works
+
+ * **S**: service
+ * **A**: application
+
+ 1. **S** creates the named pipe /tmp/pingpong, then listens
+ 2. **S** opens the named pipes in & out
+ 3. **A** talks with the test program *pingpong.sh*
+ 4. **S** closes the test program named pipes
+ 5. **S** removes the named pipe /tmp/pingpong after 10 served applications
+
+# pingpong.sh
+
+The script *pingpong.sh* lets you test the service.
+
+Usage :
+
+ pingpong.sh [NB]
+ # NB is the number of exchanged messages
+
+ or
+
+ pingpong.sh clean
+ # it is to clean the /tmp/ipc/ directory
diff --git a/pingpong/pingpong.c b/pingpong/pingpong.c
new file mode 100644
index 0000000..b6e5b6d
--- /dev/null
+++ b/pingpong/pingpong.c
@@ -0,0 +1,88 @@
+#include "../lib/communication.h"
+
+/*
+ * main loop
+ *
+ * opens the application pipes,
+ * reads then writes the same message,
+ * then closes the pipes
+ */
+
+void main_loop (const char *spath)
+{
+ int ret;
+ struct process proc;
+
+ int cnt = 10;
+
+ while (cnt--) {
+ // -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);
+ } else if (ret == 0) { // that should not happen
+ continue;
+ }
+
+ // printf ("before print\n");
+ process_print (&proc);
+ // printf ("after print\n");
+
+ // about the message
+ size_t msize = BUFSIZ;
+ char buf[BUFSIZ];
+ bzero(buf, BUFSIZ);
+
+ // printf ("before read\n");
+ if ((ret = service_read (&proc, &buf, &msize))) {
+ fprintf(stdout, "error service_read %d\n", ret);
+ exit (1);
+ }
+ // printf ("after read\n");
+ printf ("read, size %ld : %s\n", msize, buf);
+
+ // printf ("before proc write\n");
+ if ((ret = service_write (&proc, &buf, msize))) {
+ fprintf(stdout, "error service_write %d\n", ret);
+ exit (1);
+ }
+ // printf ("after proc write\n");
+ printf ("\033[32mStill \033[31m%d\033[32m applications to serve\n",cnt);
+ }
+}
+
+/*
+ * service ping-pong
+ *
+ * 1. creates the named pipe /tmp/, then listens
+ * 2. opens the named pipes in & out
+ * 3. talks with the (test) program
+ * 4. closes the test program named pipes
+ * 5. removes the named pipe /tmp/
+ */
+
+int main(int argc, char * argv[])
+{
+ // gets the service path, such as /tmp/
+ char spath[PATH_MAX];
+ service_path (spath, "pingpong");
+
+ // creates the service named pipe, that listens to client applications
+ int ret;
+ if ((ret = service_create (spath))) {
+ fprintf(stdout, "error service_create %d\n", ret);
+ exit (1);
+ }
+
+ // the service will loop until the end of time, a specific message, a signal
+ main_loop (spath);
+
+ // the application will shut down, and remove the service named pipe
+ if ((ret = service_close (spath))) {
+ fprintf(stdout, "error service_close %d\n", ret);
+ exit (1);
+ }
+
+ return EXIT_SUCCESS;
+}
diff --git a/pingpong/pingpong.sh b/pingpong/pingpong.sh
new file mode 100755
index 0000000..b622dce
--- /dev/null
+++ b/pingpong/pingpong.sh
@@ -0,0 +1,37 @@
+#!/bin/dash
+
+REP=/tmp/ipc/
+SERVICE="pingpong"
+NB=3
+
+# CLEAN UP !
+if [ $# -ne 0 ] && [ "$1" = clean ]
+then
+ echo "clean rep ${REP}"
+ rm ${REP}/${SERVICE}
+ rm ${REP}/*-in
+ rm ${REP}/*-out
+
+ exit 0
+fi
+
+if [ $# -ne 0 ]
+then
+ NB=$1
+fi
+
+for pid in `seq 1 ${NB}`
+do
+ # we make the application pipes
+ mkfifo ${REP}/${pid}-1-in 2>/dev/null
+ mkfifo ${REP}/${pid}-1-out 2>/dev/null
+
+ # pid index version
+ echo "${pid} 1 1" > ${REP}/${SERVICE}
+
+ # the purpose is to send something in the pipe
+ cat /dev/urandom | base64 | head -n 1 > ${REP}/${pid}-1-out
+
+ # the the service will answer with our message
+ cat ${REP}/${pid}-1-in
+done
diff --git a/service-test.c b/service-test.c
deleted file mode 100644
index c7ac8d1..0000000
--- a/service-test.c
+++ /dev/null
@@ -1,128 +0,0 @@
-#include "lib/communication.h"
-
-/*
- * main loop
- *
- * opens the application pipes,
- * reads then writes the same message,
- * then closes the pipes
- */
-
-void main_loop (const char *spath)
-{
- int ret;
- struct process proc;
-#if 1
- while (1) {
-
- // -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);
- } else if (ret == 0) {
- continue;
- }
-
- printf ("before print\n");
- process_print (&proc);
- printf ("after print\n");
-
- // about the message
- size_t msize = BUFSIZ;
- char buf[BUFSIZ];
- bzero(buf, BUFSIZ);
-
- printf ("before read\n");
- if ((ret = process_read (&proc, &buf, &msize))) {
- fprintf(stdout, "error process_read %d\n", ret);
- exit (1);
- }
- printf ("after read\n");
- printf ("read, size %ld : %s\n", msize, buf);
-
- printf ("before proc write\n");
- if ((ret = process_write (&proc, &buf, msize))) {
- fprintf(stdout, "error process_write %d\n", ret);
- exit (1);
- }
- printf ("after proc write\n");
- }
-#endif
-
-#if 0
-
- int ret;
- struct process **proc;
- int nproc = 0;
- while (1) {
- service_get_new_processes (&proc, &nproc, spath);
-
- // for each process : open, read, write, close
- for (int i = 0 ; i < nproc ; i++) {
- printf ("before print\n");
- process_print (proc[i]);
- printf ("after print, i = %d\n", i);
-
- // about the message
- size_t msize = BUFSIZ;
- char buf[BUFSIZ];
- bzero(buf, BUFSIZ);
-
- printf ("before read\n");
- if ((ret = process_read (proc[i], &buf, &msize))) {
- fprintf(stdout, "error process_read %d\n", ret);
- exit (1);
- }
- printf ("after read\n");
- printf ("read, size %ld : %s\n", msize, buf);
-
- printf ("before proc write\n");
- if ((ret = process_write (proc[i], &buf, msize))) {
- fprintf(stdout, "error process_write %d\n", ret);
- exit (1);
- }
- printf ("after proc write\n");
- }
- service_free_processes (proc, nproc);
- free (proc);
- }
-#endif
-}
-
-/*
- * service test
- *
- * 1. creates the named pipe /tmp/, then listens
- * 2. opens the named pipes in & out
- * 3. talks with the (test) program
- * 4. closes the test program named pipes
- * 5. removes the named pipe /tmp/
- */
-
-int main(int argc, char * argv[])
-{
- // gets the service path, such as /tmp/
- char spath[PATH_MAX];
- service_path (spath, "windows");
-
- // creates the service named pipe, that listens to client applications
- int ret;
- if ((ret = service_create (spath))) {
- fprintf(stdout, "error service_create %d\n", ret);
- exit (1);
- }
-
- // the service will loop until the end of time, a specific message, a signal
- main_loop (spath);
-
- // the application will shut down, and remove the service named pipe
- if ((ret = service_close (spath))) {
- fprintf(stdout, "error service_close %d\n", ret);
- exit (1);
- }
-
- return EXIT_SUCCESS;
-}
From 889adc499d3666930e5e82841771bd630f8a4a15 Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Tue, 31 May 2016 16:14:50 +0200
Subject: [PATCH 3/3] =?UTF-8?q?lib=20+=20pingpong=20avec=20v=C3=A9rificati?=
=?UTF-8?q?on=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);