plein de programmes débiles, qui marchent un peu mieux
parent
e2142f0c72
commit
ea885106e1
|
@ -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;
|
||||
|
|
|
@ -11,6 +11,8 @@
|
|||
#include <sys/stat.h> // mkfifo
|
||||
#include <fcntl.h> // open
|
||||
|
||||
#include <errno.h> // 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
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
Reference in New Issue