From e2142f0c72d4d5d1db2ebfe55d42f334304ded72 Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Thu, 26 May 2016 21:56:43 +0200
Subject: [PATCH 1/2] pas fini
---
lib/communication.c | 75 ++++++++++++++++++++++++++++++---------------
lib/communication.h | 1 +
service-test.c | 2 ++
3 files changed, 53 insertions(+), 25 deletions(-)
diff --git a/lib/communication.c b/lib/communication.c
index 87cf5a3..a7a9d6e 100644
--- a/lib/communication.c
+++ b/lib/communication.c
@@ -45,47 +45,50 @@ int service_close (const char *fifopath)
return 0;
}
+// TODO only works for a single process
void service_get_new_processes (struct process **proc, int *nproc, int sfifo)
{
char buf[BUFSIZ];
bzero (buf, BUFSIZ);
read (sfifo, buf, BUFSIZ);
- // TODO
-#if 0
- unsigned long long int val = strtoull(s, NULL, 10);
+ proc = malloc(sizeof(struct process*) * 1); // FIXME
+ proc[0] = malloc(sizeof(struct process) * 1); // FIXME
+
+ // unsigned long long int val = strtoul(s, NULL, 10);
char *token, *saveptr;
char *str;
- int i, j;
+ int i;
- // printf("%d fields :: ", f->nbfields);
- // for( i = 0 ; i < f->nbfields ; i++) {
- // printf("%u, ", f->fields[i]);
- // }
- // printf("\n");
-
- for (str = s, i = 1; ; str = NULL, i++) {
- token = strtok_r(str, delim, &saveptr);
+ for (str = buf, i = 1; ; str = NULL, i++) {
+ token = strtok_r(str, " ", &saveptr);
if (token == NULL)
break;
- // check if we need to print the current token humanized
- int found = 0;
-
- for (j = 0 ; j < f->nbfields && found == 0; j++ ) {
- if( i == f->fields[j] ) {
- print_token_to_human(token);
- found = 1;
- }
+ // do something
+ if (i == 1) {
+ // FIXME
+ proc[0]->pid = strtoul(token, NULL, 10);
+ }
+ else if (i == 2) {
+ // FIXME
+ proc[0]->version = strtoul(token, NULL, 10);
}
- // print the current token not humanized
- if (found == 0) {
- printf("%s ", token);
- }
}
-#endif
+}
+
+void struct_process_free (struct process * p)
+{
+ free (p);
+}
+
+void service_free_processes (struct process **procs, int nproc)
+{
+ while (--nproc != -1) {
+ struct_process_free (procs[nproc]);
+ }
}
void gen_process_structure (struct process *p
@@ -168,10 +171,32 @@ int process_close (struct process *proc)
int process_read (struct process *proc, void * buf, size_t * msize)
{
+ if ((*msize = read (proc->in, buf, *msize)))
+ return *msize;
+
return 0;
}
int process_write (struct process *proc, void * buf, size_t msize)
{
+ if ((msize = write (proc->out, buf, msize)))
+ return msize;
+
+ return 0;
+}
+
+int service_read (struct process *proc, void * buf, size_t * msize)
+{
+ if ((*msize = read (proc->out, buf, *msize)))
+ return *msize;
+
+ return 0;
+}
+
+int service_write (struct process *proc, void * buf, size_t msize)
+{
+ if ((msize = write (proc->in, buf, msize)))
+ return msize;
+
return 0;
}
diff --git a/lib/communication.h b/lib/communication.h
index aec6da6..491f044 100644
--- a/lib/communication.h
+++ b/lib/communication.h
@@ -43,6 +43,7 @@ int service_create (const char *sname);
int service_close (const char *sname);
void service_get_new_processes (struct process **, int *nproc, int sfifo);
+void service_free_processes (struct process **, int nproc);
int process_create (struct process *, int index); // called by the application
int process_destroy (struct process *); // called by the application
diff --git a/service-test.c b/service-test.c
index dca4865..28fe4e3 100644
--- a/service-test.c
+++ b/service-test.c
@@ -48,6 +48,8 @@ void main_loop (const char *spath)
exit (1);
}
}
+
+ service_free_processes (&proc, nproc);
} while (0); // it's a test, we only do it once
close (sfifo); // closes the service named pipe
From ea885106e175d9d6c22fd37454a9f02e594fd8e8 Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Fri, 27 May 2016 17:00:02 +0200
Subject: [PATCH 2/2] =?UTF-8?q?plein=20de=20programmes=20d=C3=A9biles,=20q?=
=?UTF-8?q?ui=20marchent=20un=20peu=20mieux?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
lib/communication.c | 183 +++++++++++++++++++++++++++++++++++-----
lib/communication.h | 5 +-
open-read-close-fifo.c | 29 +++++++
open-write-close-fifo.c | 30 +++++++
service-test.c | 23 ++---
5 files changed, 234 insertions(+), 36 deletions(-)
create mode 100644 open-read-close-fifo.c
create mode 100644 open-write-close-fifo.c
diff --git a/lib/communication.c b/lib/communication.c
index a7a9d6e..944da9f 100644
--- a/lib/communication.c
+++ b/lib/communication.c
@@ -30,7 +30,32 @@ int service_create (const char *fifopath)
{
int ret;
if ((ret = mkfifo (fifopath, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) {
- return 1;
+ switch (errno) {
+ case EACCES :
+ printf ("file %s : EACCES\n", fifopath);
+ return 1;
+ case EEXIST :
+ printf ("file %s : EEXIST\n", fifopath);
+ break;
+ case ENAMETOOLONG :
+ printf ("file %s : ENAMETOOLONG\n", fifopath);
+ return 2;
+ case ENOENT :
+ printf ("file %s : ENOENT\n", fifopath);
+ return 3;
+ case ENOSPC :
+ printf ("file %s : ENOSPC\n", fifopath);
+ return 4;
+ case ENOTDIR :
+ printf ("file %s : ENOTDIR\n", fifopath);
+ return 5;
+ case EROFS :
+ printf ("file %s : EROFS\n", fifopath);
+ return 6;
+ default :
+ printf ("err file %s unknown\n", fifopath);
+ return 7;
+ }
}
return 0;
@@ -45,14 +70,14 @@ int service_close (const char *fifopath)
return 0;
}
-// TODO only works for a single process
+// FIXME only works for a single process
void service_get_new_processes (struct process **proc, int *nproc, int sfifo)
{
char buf[BUFSIZ];
bzero (buf, BUFSIZ);
read (sfifo, buf, BUFSIZ);
- proc = malloc(sizeof(struct process*) * 1); // FIXME
+ //proc[0] = malloc(sizeof(struct process*) * 1); // FIXME
proc[0] = malloc(sizeof(struct process) * 1); // FIXME
// unsigned long long int val = strtoul(s, NULL, 10);
@@ -66,10 +91,18 @@ 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) {
- // FIXME
- proc[0]->pid = strtoul(token, NULL, 10);
+ 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);
}
else if (i == 2) {
// FIXME
@@ -77,6 +110,8 @@ void service_get_new_processes (struct process **proc, int *nproc, int sfifo)
}
}
+
+ *nproc = 1;
}
void struct_process_free (struct process * p)
@@ -100,6 +135,10 @@ void gen_process_structure (struct process *p
}
+void process_print (struct process *p) {
+ printf ("process %d : index %d\n", p->pid, p->index);
+}
+
int process_create (struct process *p, int index)
{
pid_t pid = getpid();
@@ -109,12 +148,63 @@ int process_create (struct process *p, int index)
// creates the pipes
int ret;
- if ((ret = mkfifo (fifopathin, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) {
- return 1;
+ if ((ret = mkfifo (fifopathin, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)))
+ {
+ switch (errno) {
+ case EACCES :
+ printf ("file %s : EACCES\n", fifopathin);
+ return 1;
+ case EEXIST :
+ printf ("file %s : EEXIST\n", fifopathin);
+ break;
+ case ENAMETOOLONG :
+ printf ("file %s : ENAMETOOLONG\n", fifopathin);
+ return 2;
+ case ENOENT :
+ printf ("file %s : ENOENT\n", fifopathin);
+ return 3;
+ case ENOSPC :
+ printf ("file %s : ENOSPC\n", fifopathin);
+ return 4;
+ case ENOTDIR :
+ printf ("file %s : ENOTDIR\n", fifopathin);
+ return 5;
+ case EROFS :
+ printf ("file %s : EROFS\n", fifopathin);
+ return 6;
+ default :
+ printf ("err file %s unknown\n", fifopathin);
+ return 7;
+ }
}
if ((ret = mkfifo (fifopathout, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) {
- return 1;
+ switch (errno) {
+ case EACCES :
+ printf ("file %s : EACCES\n", fifopathout);
+ return 1;
+ case EEXIST :
+ printf ("file %s : EEXIST\n", fifopathout);
+ break;
+ case ENAMETOOLONG :
+ printf ("file %s : ENAMETOOLONG\n", fifopathout);
+ return 2;
+ case ENOENT :
+ printf ("file %s : ENOENT\n", fifopathout);
+ return 3;
+ case ENOSPC :
+ printf ("file %s : ENOSPC\n", fifopathout);
+ return 4;
+ case ENOTDIR :
+ printf ("file %s : ENOTDIR\n", fifopathout);
+ return 5;
+ case EROFS :
+ printf ("file %s : EROFS\n", fifopathout);
+ return 6;
+ default :
+ printf ("err file %s unknown\n", fifopathout);
+ return 7;
+ }
}
// then creates the structure
@@ -140,6 +230,32 @@ int process_destroy (struct process *p)
return 0;
}
+int process_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, "rb");
+ printf ("opened\n");
+
+ return 0;
+}
+
+int process_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, "wb");
+ printf ("opened\n");
+
+ return 0;
+}
+
int process_open (struct process *proc)
{
@@ -147,47 +263,74 @@ int process_open (struct process *proc)
char fifopathout[PATH_MAX];
process_paths (fifopathin, fifopathout, proc->pid, proc->index);
- proc->in = open (fifopathin, S_IRUSR);
- proc->out = open (fifopathout, S_IRUSR);
+ 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 (struct process *proc)
+int process_close_in (struct process *proc)
{
+ printf ("closing in\n");
if (proc->in != 0) {
- close (proc->in);
+ fclose (proc->in);
proc->in = 0;
}
+ return 0;
+}
+int process_close_out (struct process *proc)
+{
+ printf ("closing out\n");
if (proc->out != 0) {
- close (proc->out);
+ fclose (proc->out);
proc->out = 0;
}
-
return 0;
}
int process_read (struct process *proc, void * buf, size_t * msize)
{
- if ((*msize = read (proc->in, buf, *msize)))
- return *msize;
+ int ret;
+ if ((ret = process_open_in (proc))) {
+ fprintf(stdout, "error process_create %d\n", ret);
+ exit (1);
+ }
+
+ *msize = fread (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;
}
int process_write (struct process *proc, void * buf, size_t msize)
{
- if ((msize = write (proc->out, buf, msize)))
- return msize;
+ int ret;
+ if ((ret = process_open_out (proc))) {
+ fprintf(stdout, "error process_create %d\n", ret);
+ exit (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 0;
}
int service_read (struct process *proc, void * buf, size_t * msize)
{
- if ((*msize = read (proc->out, buf, *msize)))
+ if ((*msize = fread (buf, 1, *msize, proc->out)))
return *msize;
return 0;
@@ -195,7 +338,7 @@ int service_read (struct process *proc, void * buf, size_t * msize)
int service_write (struct process *proc, void * buf, size_t msize)
{
- if ((msize = write (proc->in, buf, msize)))
+ if ((msize = fwrite (buf, 1, msize, proc->in)))
return msize;
return 0;
diff --git a/lib/communication.h b/lib/communication.h
index 491f044..05214d4 100644
--- a/lib/communication.h
+++ b/lib/communication.h
@@ -11,6 +11,8 @@
#include // mkfifo
#include // open
+#include // error numbers
+
#define TMPDIR "/tmp/"
#define COMMUNICATION_VERSION 1
@@ -22,7 +24,7 @@ struct process {
pid_t pid;
unsigned int version;
unsigned int index;
- int in, out;
+ FILE *in, *out;
};
// TODO create the service process structure
@@ -45,6 +47,7 @@ int service_close (const char *sname);
void service_get_new_processes (struct process **, int *nproc, int sfifo);
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
diff --git a/open-read-close-fifo.c b/open-read-close-fifo.c
new file mode 100644
index 0000000..919c225
--- /dev/null
+++ b/open-read-close-fifo.c
@@ -0,0 +1,29 @@
+#include
+#include
+#include
+#include
+
+int main(int argc, char * argv[])
+{
+ char *fifopathin = "/tmp/123000-1-in";
+ size_t msize = 100;
+
+ char buf[BUFSIZ];
+ FILE *in = fopen (fifopathin, "rb");
+
+ printf ("opened\n");
+
+ if ((msize = fread (buf, msize, 1, in))) {
+ printf ("error read %ld\n", msize);
+ return EXIT_FAILURE;
+ }
+
+ printf ("%s\n", buf);
+
+ sleep (10);
+ printf ("read end\n");
+
+ fclose (in);
+
+ return EXIT_SUCCESS;
+}
diff --git a/open-write-close-fifo.c b/open-write-close-fifo.c
new file mode 100644
index 0000000..dcf3e94
--- /dev/null
+++ b/open-write-close-fifo.c
@@ -0,0 +1,30 @@
+#include
+#include
+#include
+#include
+
+int main(int argc, char * argv[])
+{
+
+ char *fifopathin = "/tmp/123000-1-in";
+ size_t msize;
+
+ FILE *out = fopen (fifopathin, "wb");
+
+ printf ("opened\n");
+
+ char *buf = "coucou";
+ printf ("write %s\n", buf);
+
+ if ((msize = fread (buf, 6, 1, out))) {
+ printf ("error read %ld\n", msize);
+ return EXIT_FAILURE;
+ }
+
+ sleep (10);
+ printf ("write end\n");
+
+ fclose (out);
+
+ return EXIT_SUCCESS;
+}
diff --git a/service-test.c b/service-test.c
index 28fe4e3..1d6b734 100644
--- a/service-test.c
+++ b/service-test.c
@@ -19,34 +19,27 @@ void main_loop (const char *spath)
do {
service_get_new_processes (&proc, &nproc, sfifo);
+ printf ("nb proc : %d\n", nproc);
+
// for each process : open, read, write, close
for (int i = 0 ; i < nproc ; i++) {
- printf ("new connected process : %d, version %d, index %d\n"
- , proc[i].pid, proc[i].version, proc[i].index);
-
- if ((ret = process_open (&proc[i]))) {
- fprintf(stdout, "error process_create %d\n", ret);
- exit (1);
- }
+ process_print (&proc[i]);
// about the message
- size_t msize;
- char *buf;
+ size_t msize = BUFSIZ;
+ char buf[BUFSIZ];
if ((ret = process_read (&proc[i], &buf, &msize))) {
fprintf(stdout, "error process_read %d\n", ret);
exit (1);
}
+ printf ("read, size %ld : %s\n", msize, buf);
+
if ((ret = process_write (&proc[i], &buf, msize))) {
fprintf(stdout, "error process_read %d\n", ret);
exit (1);
}
-
- if ((ret = process_close (&proc[i]))) {
- fprintf(stdout, "error process_destroy %d\n", ret);
- exit (1);
- }
}
service_free_processes (&proc, nproc);
@@ -82,7 +75,7 @@ int main(int argc, char * argv[])
main_loop (spath);
// the application will shut down, and remove the service named pipe
- if ((ret = service_close ("windows"))) {
+ if ((ret = service_close (spath))) {
fprintf(stdout, "error service_close %d\n", ret);
exit (1);
}