Obsolete
/
libipc-old
Archived
3
0
Fork 0

Merge branch 'pasfini'

more_to_read
Philippe PITTOLI 2016-05-27 17:00:47 +02:00
commit bc9e807683
5 changed files with 276 additions and 50 deletions

View File

@ -30,7 +30,32 @@ int service_create (const char *fifopath)
{ {
int ret; int ret;
if ((ret = mkfifo (fifopath, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) { 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; return 0;
@ -45,47 +70,60 @@ int service_close (const char *fifopath)
return 0; return 0;
} }
// FIXME only works for a single process
void service_get_new_processes (struct process **proc, int *nproc, int sfifo) void service_get_new_processes (struct process **proc, int *nproc, int sfifo)
{ {
char buf[BUFSIZ]; char buf[BUFSIZ];
bzero (buf, BUFSIZ); bzero (buf, BUFSIZ);
read (sfifo, buf, BUFSIZ); read (sfifo, buf, BUFSIZ);
// TODO //proc[0] = malloc(sizeof(struct process*) * 1); // FIXME
#if 0 proc[0] = malloc(sizeof(struct process) * 1); // FIXME
unsigned long long int val = strtoull(s, NULL, 10);
// unsigned long long int val = strtoul(s, NULL, 10);
char *token, *saveptr; char *token, *saveptr;
char *str; char *str;
int i, j; int i;
// printf("%d fields :: ", f->nbfields); for (str = buf, i = 1; ; str = NULL, i++) {
// for( i = 0 ; i < f->nbfields ; i++) { token = strtok_r(str, " ", &saveptr);
// printf("%u, ", f->fields[i]);
// }
// printf("\n");
for (str = s, i = 1; ; str = NULL, i++) {
token = strtok_r(str, delim, &saveptr);
if (token == NULL) if (token == NULL)
break; break;
// check if we need to print the current token humanized printf ("token : %s\n", token);
int found = 0;
for (j = 0 ; j < f->nbfields && found == 0; j++ ) { // do something
if( i == f->fields[j] ) { if (i == 1) {
print_token_to_human(token); int index = strchr (token, '-') - token;
found = 1; 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 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) int process_create (struct process *p, int index)
{ {
pid_t pid = getpid(); pid_t pid = getpid();
@ -106,12 +148,63 @@ int process_create (struct process *p, int index)
// creates the pipes // creates the pipes
int ret; int ret;
if ((ret = mkfifo (fifopathin, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) { if ((ret = mkfifo (fifopathin, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)))
return 1; {
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))) { 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 // then creates the structure
@ -137,6 +230,32 @@ int process_destroy (struct process *p)
return 0; 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) int process_open (struct process *proc)
{ {
@ -144,34 +263,83 @@ int process_open (struct process *proc)
char fifopathout[PATH_MAX]; char fifopathout[PATH_MAX];
process_paths (fifopathin, fifopathout, proc->pid, proc->index); process_paths (fifopathin, fifopathout, proc->pid, proc->index);
proc->in = open (fifopathin, S_IRUSR); printf ("opening %s\n", fifopathin);
proc->out = open (fifopathout, S_IRUSR); proc->in = fopen (fifopathin, "rb");
printf ("opening %s\n", fifopathout);
proc->out = fopen (fifopathout, "wb");
printf ("opened\n");
return 0; return 0;
} }
int process_close (struct process *proc) int process_close_in (struct process *proc)
{ {
printf ("closing in\n");
if (proc->in != 0) { if (proc->in != 0) {
close (proc->in); fclose (proc->in);
proc->in = 0; proc->in = 0;
} }
return 0;
}
int process_close_out (struct process *proc)
{
printf ("closing out\n");
if (proc->out != 0) { if (proc->out != 0) {
close (proc->out); fclose (proc->out);
proc->out = 0; proc->out = 0;
} }
return 0; return 0;
} }
int process_read (struct process *proc, void * buf, size_t * msize) 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; return 0;
} }
int process_write (struct process *proc, void * buf, size_t msize) 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; return 0;
} }

View File

@ -11,6 +11,8 @@
#include <sys/stat.h> // mkfifo #include <sys/stat.h> // mkfifo
#include <fcntl.h> // open #include <fcntl.h> // open
#include <errno.h> // error numbers
#define TMPDIR "/tmp/" #define TMPDIR "/tmp/"
#define COMMUNICATION_VERSION 1 #define COMMUNICATION_VERSION 1
@ -22,7 +24,7 @@ struct process {
pid_t pid; pid_t pid;
unsigned int version; unsigned int version;
unsigned int index; unsigned int index;
int in, out; FILE *in, *out;
}; };
// TODO create the service process structure // TODO create the service process structure
@ -43,7 +45,9 @@ int service_create (const char *sname);
int service_close (const char *sname); int service_close (const char *sname);
void service_get_new_processes (struct process **, int *nproc, int sfifo); 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_create (struct process *, int index); // called by the application
int process_destroy (struct process *); // called by the application int process_destroy (struct process *); // called by the application

29
open-read-close-fifo.c Normal file
View File

@ -0,0 +1,29 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
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;
}

30
open-write-close-fifo.c Normal file
View File

@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
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;
}

View File

@ -19,35 +19,30 @@ void main_loop (const char *spath)
do { do {
service_get_new_processes (&proc, &nproc, sfifo); service_get_new_processes (&proc, &nproc, sfifo);
printf ("nb proc : %d\n", nproc);
// for each process : open, read, write, close // for each process : open, read, write, close
for (int i = 0 ; i < nproc ; i++) { for (int i = 0 ; i < nproc ; i++) {
printf ("new connected process : %d, version %d, index %d\n" process_print (&proc[i]);
, 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);
}
// about the message // about the message
size_t msize; size_t msize = BUFSIZ;
char *buf; char buf[BUFSIZ];
if ((ret = process_read (&proc[i], &buf, &msize))) { if ((ret = process_read (&proc[i], &buf, &msize))) {
fprintf(stdout, "error process_read %d\n", ret); fprintf(stdout, "error process_read %d\n", ret);
exit (1); exit (1);
} }
printf ("read, size %ld : %s\n", msize, buf);
if ((ret = process_write (&proc[i], &buf, msize))) { if ((ret = process_write (&proc[i], &buf, msize))) {
fprintf(stdout, "error process_read %d\n", ret); fprintf(stdout, "error process_read %d\n", ret);
exit (1); 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 } while (0); // it's a test, we only do it once
close (sfifo); // closes the service named pipe close (sfifo); // closes the service named pipe
@ -80,7 +75,7 @@ int main(int argc, char * argv[])
main_loop (spath); main_loop (spath);
// the application will shut down, and remove the service named pipe // 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); fprintf(stdout, "error service_close %d\n", ret);
exit (1); exit (1);
} }