From 3e857026bf2ec7c18fe0d5e10fe1218767c394bb Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Sun, 5 Jun 2016 03:19:36 +0200 Subject: [PATCH] GROS BORDEL 2, le retour ! --- lib/communication.c | 362 +++++++++++--------------------------------- lib/communication.h | 46 ++---- lib/communication.o | Bin 16952 -> 19624 bytes lib/process.c | 43 ++++++ lib/process.h | 35 +++++ pingpong/pingpong.c | 12 +- pubsub/pubsubd.c | 2 +- pubsub/pubsubd.h | 5 +- 8 files changed, 191 insertions(+), 314 deletions(-) create mode 100644 lib/process.c create mode 100644 lib/process.h diff --git a/lib/communication.c b/lib/communication.c index 313e697..1976b41 100644 --- a/lib/communication.c +++ b/lib/communication.c @@ -2,7 +2,32 @@ #include #include -int service_path (char *buf, const char *sname) +int file_open (FILE **f, const char *path, const char *mode) +{ + printf ("opening %s\n", path); + *f = fopen (path, mode); + if (*f == NULL) { + fprintf (stderr, "\033[31mnot opened\033[00m\n"); + return -1; + } + printf ("opened : %ld\n", (long) *f); + + return 0; +} + +int file_close (FILE *f) +{ + if (f != 0) { + printf ("before fclosing\n"); + fclose (f); + printf ("after fclosing\n"); + } + return 0; +} + +// SERVICE + +int srv_path (char *buf, const char *sname) { if (buf == NULL) { return 1; @@ -20,15 +45,7 @@ int service_path (char *buf, const char *sname) return 0; } -void process_paths (char *in, char *out, pid_t pid, int index) -{ - bzero (in, PATH_MAX); - bzero (out, PATH_MAX); - snprintf(in , PATH_MAX, "%s/%d-%d-in" , TMPDIR, pid, index); - snprintf(out, PATH_MAX, "%s/%d-%d-out", TMPDIR, pid, index); -} - -int service_create (const char *fifopath) +int srv_create (const char *fifopath) { int ret; if ((ret = mkfifo (fifopath, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) { @@ -63,7 +80,7 @@ int service_create (const char *fifopath) return 0; } -int service_close (const char *fifopath) +int srv_close (const char *fifopath) { if (unlink (fifopath)) { return 1; @@ -72,24 +89,7 @@ int service_close (const char *fifopath) return 0; } -struct process * srv_process_copy (const struct process *p) -{ - if (p == NULL) - return NULL; - - struct process * copy = malloc (sizeof(struct process)); - memcpy (copy, p, sizeof (struct process)); - - return copy; -} - -int srv_process_eq (const struct process *p1, const struct process *p2) -{ - return (p1->pid == p2->pid && p1>version == p2->version - && p1->index == p2->index); -} - -int service_get_new_process (struct process *proc, const char * spath) +int srv_get_new_process (struct process *p, const char * spath) { if (spath == NULL) { return -1; @@ -117,349 +117,167 @@ int service_get_new_process (struct process *proc, const char * spath) char *str; int i; + pid_t pid; + int index; + int version; + for (str = buf, i = 1; ; str = NULL, i++) { token = strtok_r(str, " ", &saveptr); if (token == NULL) break; if (i == 1) { - proc->pid = strtoul(token, NULL, 10); + pid = strtoul(token, NULL, 10); } else if (i == 2) { - proc->index = strtoul(token, NULL, 10); + index = strtoul(token, NULL, 10); } else if (i == 3) { - proc->version = strtoul(token, NULL, 10); + version = strtoul(token, NULL, 10); } } + srv_process_gen (p, pid, index, version); + return 1; } -// FIXME only works for a single process -void service_get_new_processes (struct process ***proc, int *nproc, char * spath) +int srv_read (struct process *p, void * buf, size_t * msize) { - if (proc == NULL || spath == NULL) { - return; - } + if (file_open (&p->out, p->path_out, "rb")) + return 1; - char buf[BUFSIZ]; - bzero (buf, BUFSIZ); + *msize = fread (buf, 1, *msize, p->out); // FIXME check errors + // printf ("DEBUG read, size %ld : %s\n", *msize, buf); - // read the pipe, get a process to work on - FILE * f = fopen (spath, "rb"); - fgets (buf, BUFSIZ, f); - fclose (f); + if (file_close (p->out)) + return 1; - 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); - } - - } - } + return 0; } -void struct_process_free (struct process * p) +int srv_write (struct process *p, void * buf, size_t msize) { - free (p); + if (file_open (&p->in, p->path_in, "wb")) + return 1; + + fwrite (buf, 1, msize, p->in); // FIXME check errors + + if (file_close (p->in)) + return 1; + + return 0; } -void service_free_processes (struct process **procs, int nproc) -{ - printf ("free processes\n"); - while (nproc--) { - printf ("free process %d\n", nproc); +// APPLICATION - struct_process_free (procs[nproc]); - } -} - -void gen_process_structure (struct process *p - , pid_t pid, unsigned int index, unsigned int version) -{ - p->pid = pid; - p->version = version; - p->index = index; -} - - -void process_print (struct process *p) -{ - printf ("process %d : index %d\n", p->pid, p->index); -} - -int process_create (struct process *p, int index) +int app_create (struct process *p, int index) { pid_t pid = getpid(); - char fifopathin[PATH_MAX]; - char fifopathout[PATH_MAX]; - process_paths (fifopathin, fifopathout, pid, index); + + // then creates the structure + srv_process_gen (p, pid, index, COMMUNICATION_VERSION); // creates the pipes int ret; - if ((ret = mkfifo (fifopathin, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) + if ((ret = mkfifo (p->path_in, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) { switch (errno) { case EACCES : - printf ("file %s : EACCES\n", fifopathin); + printf ("file %s : EACCES\n", p->path_in); return 1; case EEXIST : - printf ("file %s : EEXIST\n", fifopathin); + printf ("file %s : EEXIST\n", p->path_in); break; case ENAMETOOLONG : - printf ("file %s : ENAMETOOLONG\n", fifopathin); + printf ("file %s : ENAMETOOLONG\n", p->path_in); return 2; case ENOENT : - printf ("file %s : ENOENT\n", fifopathin); + printf ("file %s : ENOENT\n", p->path_in); return 3; case ENOSPC : - printf ("file %s : ENOSPC\n", fifopathin); + printf ("file %s : ENOSPC\n", p->path_in); return 4; case ENOTDIR : - printf ("file %s : ENOTDIR\n", fifopathin); + printf ("file %s : ENOTDIR\n", p->path_in); return 5; case EROFS : - printf ("file %s : EROFS\n", fifopathin); + printf ("file %s : EROFS\n", p->path_in); return 6; default : - printf ("err file %s unknown\n", fifopathin); + printf ("err file %s unknown\n", p->path_in); return 7; } } - if ((ret = mkfifo (fifopathout, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) { + if ((ret = mkfifo (p->path_out, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) { switch (errno) { case EACCES : - printf ("file %s : EACCES\n", fifopathout); + printf ("file %s : EACCES\n", p->path_out); return 1; case EEXIST : - printf ("file %s : EEXIST\n", fifopathout); + printf ("file %s : EEXIST\n", p->path_out); break; case ENAMETOOLONG : - printf ("file %s : ENAMETOOLONG\n", fifopathout); + printf ("file %s : ENAMETOOLONG\n", p->path_out); return 2; case ENOENT : - printf ("file %s : ENOENT\n", fifopathout); + printf ("file %s : ENOENT\n", p->path_out); return 3; case ENOSPC : - printf ("file %s : ENOSPC\n", fifopathout); + printf ("file %s : ENOSPC\n", p->path_out); return 4; case ENOTDIR : - printf ("file %s : ENOTDIR\n", fifopathout); + printf ("file %s : ENOTDIR\n", p->path_out); return 5; case EROFS : - printf ("file %s : EROFS\n", fifopathout); + printf ("file %s : EROFS\n", p->path_out); return 6; default : - printf ("err file %s unknown\n", fifopathout); + printf ("err file %s unknown\n", p->path_out); return 7; } } - // then creates the structure - gen_process_structure (p, pid, index, COMMUNICATION_VERSION); - return 0; } -int process_destroy (struct process *p) +int app_destroy (struct process *p) { - char fifopathin[PATH_MAX]; - char fifopathout[PATH_MAX]; - process_paths (fifopathin, fifopathout, p->pid, p->index); - - if (unlink (fifopathin)) { + if (unlink (p->path_in)) { return 1; } - if (unlink (fifopathout)) { + if (unlink (p->path_out)) { return 1; } return 0; } -int process_open_in (struct process *proc) +int app_read (struct process *p, void * buf, size_t * msize) { - 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"); - if (proc->in == NULL) { - fprintf (stderr, "\033[31mnot opened\033[00m\n"); - return -1; - } - printf ("opened : %d\n", proc->in); - - 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"); - if (proc->in == NULL) { - fprintf (stderr, "\033[31mnot opened\033[00m\n"); - return -1; - } - 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"); - if (proc->out == NULL) { - fprintf (stderr, "\033[31mnot opened\033[00m\n"); - return -1; - } - 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"); - if (proc->out == NULL) { - fprintf (stderr, "\033[31mnot opened\033[00m\n"); - return -1; - } - 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; -} - -int process_close_out (struct process *proc) -{ - printf ("closing out\n"); - if (proc->out != 0) { - fclose (proc->out); - proc->out = 0; - } - return 0; -} - - -int process_read (struct process *proc, void * buf, size_t * msize) -{ - int ret; - if ((ret = process_open_in (proc))) { + if (file_open (&p->in, p->path_in, "rb")) return 1; - } - *msize = fread (buf, 1, *msize, proc->in); // FIXME check errors + *msize = fread (buf, 1, *msize, p->in); // FIXME check errors // printf ("DEBUG read, size %ld : %s\n", *msize, buf); - if ((ret = process_close_in (proc))) { + if (file_close (p->in)) return 1; - } return 0; } -int process_write (struct process *proc, void * buf, size_t msize) +int app_write (struct process *p, void * buf, size_t msize) { - int ret; - if ((ret = process_open_out (proc))) { + if (file_open (&p->out, p->path_out, "wb")) return 1; - } - fwrite (buf, 1, msize, proc->out); // FIXME check errors + fwrite (buf, 1, msize, p->out); // FIXME check errors - if ((ret = process_close_out (proc))) { + if (file_close (p->out)) return 1; - } - return 0; -} - -int service_read (struct process *proc, void * buf, size_t * msize) -{ - int ret; - if ((ret = service_proc_open_out (proc))) { - 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))) { - return 1; - } return 0; } - -int service_write (struct process *proc, void * buf, size_t msize) -{ - int ret; - if ((ret = service_proc_open_in (proc))) { - return 1; - } - - fwrite (buf, 1, msize, proc->in); // FIXME check errors - - if ((ret = process_close_in (proc))) { - return 1; - } - return 0; -} diff --git a/lib/communication.h b/lib/communication.h index e9a27b0..ebe062e 100644 --- a/lib/communication.h +++ b/lib/communication.h @@ -5,6 +5,7 @@ #include #include +#include "process.h" #include // unlink #include // mkfifo @@ -13,35 +14,16 @@ #include // error numbers -#define TMPDIR "/tmp/ipc/" - #define COMMUNICATION_VERSION 1 -// TODO to check the right length for a path -#define PATH_MAX BUFSIZ - -struct process { - pid_t pid; - unsigned int version; - unsigned int index; - FILE *in, *out; -}; - struct service { unsigned int version; unsigned int index; }; -// TODO create the service process structure +int srv_path (char *buf, const char *sname); -int service_path (char *buf, const char *sname); - -struct process * srv_process_copy (struct process *p); - -int srv_process_eq (const struct process *p1, const struct process *p2); - -void gen_process_structure (struct process *p - , pid_t pid, unsigned int index, unsigned int version); +int srv_get_new_process (struct process *proc, const char * spath); /* * returns @@ -50,22 +32,18 @@ void gen_process_structure (struct process *p * 2 : service name too long * 3 : unable to create fifo */ -int service_create (const char *sname); -int service_close (const char *sname); +int srv_create (const char *sname); +int srv_close (const char *sname); -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); +int srv_read (struct process *, void * buf, size_t *); +int srv_write (struct process *, void * buf, size_t); +// APPLICATION -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 app_create (struct process *, int index); // called by the application +int app_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); +int app_read (struct process *, void * buf, size_t *); +int app_write (struct process *, void * buf, size_t); #endif diff --git a/lib/communication.o b/lib/communication.o index a0cea9da474b6b4da1b905dd5750f3a466c7a97e..967d37ee0dce8288a6edad1d4b8612f8987fea18 100644 GIT binary patch literal 19624 zcmeHOdvugVmanfnfsc1c0E4KsEKkS0cnykZG=j}h0R?svMVn62owP~1+kOP_MKp#< zHj7I{U7TeP8Bmv99YuE?qYfAuM{w4iVb-$_d&b?tJ<18I?lL>*;L4i4x9Z+brP7sQ z=5One{_6hfSHHUT_-@trb+dK(O)ET(BRDzYYB69jD#Y9y2J+=HTP~)E0a=5gCBxr|`G+rf2y( zb0xpy&DSPF<`B9gwPc;1en_8O!WD)zBO83@$K+R@^sDv$vm)gi&`^yk9|q8DHL*un zJ3YDw-8j_&A5a^*M?Glm9pzspd_pvj?D0V1CwHOvAw)79?BbC((QkjpVcCkm6)DwTzWXciv!mDFne!9Ki2f5;bLf1j4*voe*~?G8 zXf2W>aq07r=p>)Zf`?1mJC1RC8cPj{zoV@O#E!Ie!zV0H7^=q)D0Rfm z9v{V`)`NI~^}G}Ktp{bz^rher!mo`6a8R|5wx49nb)%(;r`O-~<2^EqI` zyrGjgE3K*nr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H{`EzMOcc z!hLifWhlIv^gRmKNxx3v?WEtJ@J`~r3O9&vR(KEbEec;pd_dvbiEmZ-0PU-`DSRvO zrxZTovcPtQKSlZ-3g1qAr^0s--=*-K#CI!v7x6s`-%Wg?uXB2*#_&J52A%0%r z=ZIfa_<7b9AFf8$NiFm%kW&d06FEjH-KJClLD|+d_SK;GHZ@t&d z*zpoCRP+;xPf>Uwai7Ac5SRChvR!^^SFxfGl76PbD~ZeZjM9$mx7mt5O!{(#%lA$5 z6%KzkDN<134-l_Z_;TX9!XG3aRyh0-qex2>-b%b);qAoneJv*0-cI83{kG(@i9e|5 zdx$qHd>!#th0FWHc7@Bh?NqqDZ!{FXh3xkzT=vg8g>NPO28C}U-mCDZh;LT-cH&zU zzJvIH!gmtis_rSL)GyA^(v_#TBHBfd}J zGJbp_sxom!r&_tf{dC@`a5)dE>r|5JQ|JHl7^slcd04)$pQ89F-;b;DDc^Ui@gv`B ztNkh8UmsBH%lFqq3YYJ%kdCI) zeUr@a$^-hH48QsC3DUXz@!e=0ZKnG){K0x?dyxKnQM{K&PR)ZOD@BxgC8uzZr{X>Y zZT_L<@tq#p#Ah))$@-sDDwL=4HwcCBSnWSAzO>7cWI35t4+qF7+$7dyd3jyP4D4&3 zYT+bT|L??xL29M=$NtA#TDAX|1H<~|JOYOz9lO^0#d~O;r2q0a0-5rd>pr9MoZ+#m z{Vp-J{o-XJ&FvZ}LtNq^jB3qO9lh}TIR42<9TG<*7lTwAqIfD;(A@y26 q>2KA4Jip9$V4ah!B0;JU!+9or)cSFom8<^U__74jDkW2`|GxmgJRQUU diff --git a/lib/process.c b/lib/process.c new file mode 100644 index 0000000..7f3cba7 --- /dev/null +++ b/lib/process.c @@ -0,0 +1,43 @@ +#include "process.h" + +struct process * srv_process_copy (const struct process *p) +{ + if (p == NULL) + return NULL; + + struct process * copy = malloc (sizeof(struct process)); + memcpy (copy, p, sizeof (struct process)); + + return copy; +} + +int srv_process_eq (const struct process *p1, const struct process *p2) +{ + return (p1->pid == p2->pid && p1->version == p2->version + && p1->index == p2->index); +} + +void srv_process_gen (struct process *p + , pid_t pid, unsigned int index, unsigned int version) +{ + p->pid = pid; + p->version = version; + p->index = index; + + bzero (p->path_in, PATH_MAX); + bzero (p->path_out, PATH_MAX); + snprintf(p->path_in , PATH_MAX, "%s/%d-%d-in" , TMPDIR, pid, index); + snprintf(p->path_out, PATH_MAX, "%s/%d-%d-out", TMPDIR, pid, index); +} + +void srv_process_free (struct process * p) +{ + if (! p) + return; + free (p); +} + +void process_print (struct process *p) +{ + printf ("process %d : index %d\n", p->pid, p->index); +} diff --git a/lib/process.h b/lib/process.h new file mode 100644 index 0000000..13461fd --- /dev/null +++ b/lib/process.h @@ -0,0 +1,35 @@ +#ifndef __PROCESS_H__ +#define __PROCESS_H__ + +#include +#include +#include +#include + +#define TMPDIR "/tmp/ipc/" + +// TODO to check the right length for a path +#define PATH_MAX BUFSIZ + +#include + +struct process { + pid_t pid; + unsigned int version; + unsigned int index; + char path_in [PATH_MAX]; + char path_out [PATH_MAX]; + FILE *in, *out; +}; + +struct process * srv_process_copy (const struct process *p); + +int srv_process_eq (const struct process *p1, const struct process *p2); + +// create the service process structure +void srv_process_gen (struct process *p + , pid_t pid, unsigned int index, unsigned int version); + +void process_print (struct process *); + +#endif diff --git a/pingpong/pingpong.c b/pingpong/pingpong.c index 6b78ddc..4b932ba 100644 --- a/pingpong/pingpong.c +++ b/pingpong/pingpong.c @@ -17,7 +17,7 @@ void main_loop (const char *spath) while (cnt--) { // -1 : error, 0 = no new process, 1 = new process - ret = service_get_new_process (&proc, spath); + ret = srv_get_new_process (&proc, spath); if (ret == -1) { fprintf (stderr, "error service_get_new_process\n"); continue; @@ -35,7 +35,7 @@ void main_loop (const char *spath) bzero(buf, BUFSIZ); // printf ("before read\n"); - if ((ret = service_read (&proc, &buf, &msize))) { + if ((ret = srv_read (&proc, &buf, &msize))) { fprintf(stdout, "error service_read %d\n", ret); continue; } @@ -43,7 +43,7 @@ void main_loop (const char *spath) printf ("read, size %ld : %s\n", msize, buf); // printf ("before proc write\n"); - if ((ret = service_write (&proc, &buf, msize))) { + if ((ret = srv_write (&proc, &buf, msize))) { fprintf(stdout, "error service_write %d\n", ret); continue; } @@ -66,11 +66,11 @@ int main(int argc, char * argv[]) { // gets the service path, such as /tmp/ char spath[PATH_MAX]; - service_path (spath, "pingpong"); + srv_path (spath, "pingpong"); // creates the service named pipe, that listens to client applications int ret; - if ((ret = service_create (spath))) { + if ((ret = srv_create (spath))) { fprintf(stdout, "error service_create %d\n", ret); exit (1); } @@ -79,7 +79,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 (spath))) { + if ((ret = srv_close (spath))) { fprintf(stdout, "error service_close %d\n", ret); exit (1); } diff --git a/pubsub/pubsubd.c b/pubsub/pubsubd.c index c1263b0..0295c87 100644 --- a/pubsub/pubsubd.c +++ b/pubsub/pubsubd.c @@ -1,4 +1,4 @@ -#include "../lib/communication.h" +#include "pubsubd.h" #include const char* service_name = "pubsub"; diff --git a/pubsub/pubsubd.h b/pubsub/pubsubd.h index 4c70462..9e507d0 100644 --- a/pubsub/pubsubd.h +++ b/pubsub/pubsubd.h @@ -1,7 +1,10 @@ #ifndef __PUBSUBD_H__ #define __PUBSUBD_H__ -#include "queue.h" +#include "../lib/communication.h" +#include "../lib/process.h" + +#include "../lib/queue.h" struct message { unsigned char *chan;