diff --git a/lib/communication.c b/lib/communication.c index 87cf5a3..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,47 +70,60 @@ 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) { char buf[BUFSIZ]; bzero (buf, BUFSIZ); read (sfifo, buf, BUFSIZ); - // TODO -#if 0 - unsigned long long int val = strtoull(s, NULL, 10); + //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); 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; + printf ("token : %s\n", token); - 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) { + 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 + proc[0]->version = strtoul(token, NULL, 10); } - // print the current token not humanized - if (found == 0) { - printf("%s ", token); - } } -#endif + + *nproc = 1; +} + +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 @@ -97,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(); @@ -106,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 @@ -137,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) { @@ -144,34 +263,83 @@ 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) { + 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) { + 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 = fread (buf, 1, *msize, proc->out))) + return *msize; + + return 0; +} + +int service_write (struct process *proc, void * buf, size_t msize) +{ + if ((msize = fwrite (buf, 1, msize, proc->in))) + return msize; + return 0; } diff --git a/lib/communication.h b/lib/communication.h index aec6da6..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 @@ -43,7 +45,9 @@ 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); +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 dca4865..1d6b734 100644 --- a/service-test.c +++ b/service-test.c @@ -19,35 +19,30 @@ 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); } while (0); // it's a test, we only do it once close (sfifo); // closes the service named pipe @@ -80,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); }