2016-05-26 18:27:59 +02:00
|
|
|
#include "communication.h"
|
2016-05-30 01:54:19 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
2016-05-26 18:27:59 +02:00
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-05 20:48:13 +02:00
|
|
|
void srv_init (struct service *srv, const char *sname)
|
2016-05-26 18:27:59 +02:00
|
|
|
{
|
2016-06-05 20:48:13 +02:00
|
|
|
if (srv == NULL)
|
|
|
|
return;
|
2016-05-26 18:27:59 +02:00
|
|
|
|
2016-06-05 20:48:13 +02:00
|
|
|
// gets the service path, such as /tmp/<service>
|
|
|
|
bzero (srv->spath, PATH_MAX);
|
|
|
|
strncat (srv->spath, TMPDIR, PATH_MAX);
|
|
|
|
strncat (srv->spath, sname, PATH_MAX);
|
2016-05-26 18:27:59 +02:00
|
|
|
|
2016-06-05 20:48:13 +02:00
|
|
|
srv->version = COMMUNICATION_VERSION;
|
|
|
|
srv->index = 0; // TODO
|
2016-05-26 18:27:59 +02:00
|
|
|
}
|
|
|
|
|
2016-06-05 20:48:13 +02:00
|
|
|
// SERVICE
|
|
|
|
|
|
|
|
int srv_create (struct service *srv)
|
2016-05-26 18:27:59 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2016-06-05 20:48:13 +02:00
|
|
|
if ((ret = mkfifo (srv->spath, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) {
|
2016-05-27 17:00:02 +02:00
|
|
|
switch (errno) {
|
|
|
|
case EACCES :
|
2016-06-05 20:48:13 +02:00
|
|
|
printf ("file %s : EACCES\n", srv->spath);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 1;
|
|
|
|
case EEXIST :
|
2016-06-05 20:48:13 +02:00
|
|
|
printf ("file %s : EEXIST\n", srv->spath);
|
2016-05-27 17:00:02 +02:00
|
|
|
break;
|
|
|
|
case ENAMETOOLONG :
|
2016-06-05 20:48:13 +02:00
|
|
|
printf ("file %s : ENAMETOOLONG\n", srv->spath);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 2;
|
|
|
|
case ENOENT :
|
2016-06-05 20:48:13 +02:00
|
|
|
printf ("file %s : ENOENT\n", srv->spath);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 3;
|
|
|
|
case ENOSPC :
|
2016-06-05 20:48:13 +02:00
|
|
|
printf ("file %s : ENOSPC\n", srv->spath);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 4;
|
|
|
|
case ENOTDIR :
|
2016-06-05 20:48:13 +02:00
|
|
|
printf ("file %s : ENOTDIR\n", srv->spath);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 5;
|
|
|
|
case EROFS :
|
2016-06-05 20:48:13 +02:00
|
|
|
printf ("file %s : EROFS\n", srv->spath);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 6;
|
|
|
|
default :
|
2016-06-05 20:48:13 +02:00
|
|
|
printf ("err file %s unknown\n", srv->spath);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 7;
|
|
|
|
}
|
2016-05-26 18:27:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-05 20:48:13 +02:00
|
|
|
int srv_close (struct service *srv)
|
2016-05-26 18:27:59 +02:00
|
|
|
{
|
2016-06-05 20:48:13 +02:00
|
|
|
if (unlink (srv->spath)) {
|
2016-05-26 18:27:59 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-05 20:48:13 +02:00
|
|
|
int srv_get_new_process (struct process *p, const struct service *srv)
|
2016-05-26 18:27:59 +02:00
|
|
|
{
|
2016-06-05 20:48:13 +02:00
|
|
|
if (srv->spath == NULL) {
|
2016-05-30 01:54:19 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-26 18:27:59 +02:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
bzero (buf, BUFSIZ);
|
|
|
|
|
2016-05-30 01:54:19 +02:00
|
|
|
// read the pipe, get a process to work on
|
|
|
|
struct timespec ts = { 0 };
|
|
|
|
struct timespec ts2 = { 0 };
|
|
|
|
|
2016-06-05 20:48:13 +02:00
|
|
|
FILE * f = fopen (srv->spath, "r");
|
2016-05-30 01:54:19 +02:00
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
fgets (buf, BUFSIZ, f);
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ts2);
|
|
|
|
fclose (f);
|
2016-05-26 21:56:43 +02:00
|
|
|
|
2016-05-30 01:54:19 +02:00
|
|
|
printf("sec: %ld nsec: %ld\n", ts.tv_sec, ts.tv_nsec);
|
|
|
|
printf("sec: %ld nsec: %ld\n", ts2.tv_sec, ts2.tv_nsec);
|
|
|
|
|
|
|
|
printf("diff nsec: %ld\n", ts2.tv_nsec - ts.tv_nsec);
|
2016-05-26 18:27:59 +02:00
|
|
|
|
|
|
|
char *token, *saveptr;
|
|
|
|
char *str;
|
2016-05-26 21:56:43 +02:00
|
|
|
int i;
|
2016-05-26 18:27:59 +02:00
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
pid_t pid;
|
|
|
|
int index;
|
|
|
|
int version;
|
|
|
|
|
2016-05-26 21:56:43 +02:00
|
|
|
for (str = buf, i = 1; ; str = NULL, i++) {
|
|
|
|
token = strtok_r(str, " ", &saveptr);
|
2016-05-26 18:27:59 +02:00
|
|
|
if (token == NULL)
|
|
|
|
break;
|
|
|
|
|
2016-05-26 21:56:43 +02:00
|
|
|
if (i == 1) {
|
2016-06-05 03:19:36 +02:00
|
|
|
pid = strtoul(token, NULL, 10);
|
2016-05-26 18:27:59 +02:00
|
|
|
}
|
2016-05-26 21:56:43 +02:00
|
|
|
else if (i == 2) {
|
2016-06-05 03:19:36 +02:00
|
|
|
index = strtoul(token, NULL, 10);
|
2016-05-30 01:54:19 +02:00
|
|
|
}
|
|
|
|
else if (i == 3) {
|
2016-06-05 03:19:36 +02:00
|
|
|
version = strtoul(token, NULL, 10);
|
2016-05-26 18:27:59 +02:00
|
|
|
}
|
2016-05-30 01:54:19 +02:00
|
|
|
}
|
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
srv_process_gen (p, pid, index, version);
|
|
|
|
|
2016-05-30 01:54:19 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2016-05-26 21:56:43 +02:00
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
int srv_read (struct process *p, void * buf, size_t * msize)
|
2016-05-30 01:54:19 +02:00
|
|
|
{
|
2016-06-05 03:19:36 +02:00
|
|
|
if (file_open (&p->out, p->path_out, "rb"))
|
|
|
|
return 1;
|
2016-05-30 01:54:19 +02:00
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
*msize = fread (buf, 1, *msize, p->out); // FIXME check errors
|
|
|
|
// printf ("DEBUG read, size %ld : %s\n", *msize, buf);
|
2016-05-30 01:54:19 +02:00
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
if (file_close (p->out))
|
|
|
|
return 1;
|
2016-05-30 01:54:19 +02:00
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
return 0;
|
2016-05-26 21:56:43 +02:00
|
|
|
}
|
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
int srv_write (struct process *p, void * buf, size_t msize)
|
2016-05-26 21:56:43 +02:00
|
|
|
{
|
2016-06-05 03:19:36 +02:00
|
|
|
if (file_open (&p->in, p->path_in, "wb"))
|
|
|
|
return 1;
|
2016-05-26 21:56:43 +02:00
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
fwrite (buf, 1, msize, p->in); // FIXME check errors
|
2016-05-30 01:54:19 +02:00
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
if (file_close (p->in))
|
|
|
|
return 1;
|
2016-05-26 18:27:59 +02:00
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
return 0;
|
2016-05-26 18:27:59 +02:00
|
|
|
}
|
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
// APPLICATION
|
2016-05-26 18:27:59 +02:00
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
int app_create (struct process *p, int index)
|
2016-05-26 18:27:59 +02:00
|
|
|
{
|
|
|
|
pid_t pid = getpid();
|
2016-06-05 03:19:36 +02:00
|
|
|
|
|
|
|
// then creates the structure
|
|
|
|
srv_process_gen (p, pid, index, COMMUNICATION_VERSION);
|
2016-05-26 18:27:59 +02:00
|
|
|
|
|
|
|
// creates the pipes
|
|
|
|
int ret;
|
2016-06-05 03:19:36 +02:00
|
|
|
if ((ret = mkfifo (p->path_in, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)))
|
2016-05-27 17:00:02 +02:00
|
|
|
{
|
|
|
|
switch (errno) {
|
|
|
|
case EACCES :
|
2016-06-05 03:19:36 +02:00
|
|
|
printf ("file %s : EACCES\n", p->path_in);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 1;
|
|
|
|
case EEXIST :
|
2016-06-05 03:19:36 +02:00
|
|
|
printf ("file %s : EEXIST\n", p->path_in);
|
2016-05-27 17:00:02 +02:00
|
|
|
break;
|
|
|
|
case ENAMETOOLONG :
|
2016-06-05 03:19:36 +02:00
|
|
|
printf ("file %s : ENAMETOOLONG\n", p->path_in);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 2;
|
|
|
|
case ENOENT :
|
2016-06-05 03:19:36 +02:00
|
|
|
printf ("file %s : ENOENT\n", p->path_in);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 3;
|
|
|
|
case ENOSPC :
|
2016-06-05 03:19:36 +02:00
|
|
|
printf ("file %s : ENOSPC\n", p->path_in);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 4;
|
|
|
|
case ENOTDIR :
|
2016-06-05 03:19:36 +02:00
|
|
|
printf ("file %s : ENOTDIR\n", p->path_in);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 5;
|
|
|
|
case EROFS :
|
2016-06-05 03:19:36 +02:00
|
|
|
printf ("file %s : EROFS\n", p->path_in);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 6;
|
|
|
|
default :
|
2016-06-05 03:19:36 +02:00
|
|
|
printf ("err file %s unknown\n", p->path_in);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 7;
|
|
|
|
}
|
2016-05-26 18:27:59 +02:00
|
|
|
}
|
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
if ((ret = mkfifo (p->path_out, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) {
|
2016-05-27 17:00:02 +02:00
|
|
|
switch (errno) {
|
|
|
|
case EACCES :
|
2016-06-05 03:19:36 +02:00
|
|
|
printf ("file %s : EACCES\n", p->path_out);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 1;
|
|
|
|
case EEXIST :
|
2016-06-05 03:19:36 +02:00
|
|
|
printf ("file %s : EEXIST\n", p->path_out);
|
2016-05-27 17:00:02 +02:00
|
|
|
break;
|
|
|
|
case ENAMETOOLONG :
|
2016-06-05 03:19:36 +02:00
|
|
|
printf ("file %s : ENAMETOOLONG\n", p->path_out);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 2;
|
|
|
|
case ENOENT :
|
2016-06-05 03:19:36 +02:00
|
|
|
printf ("file %s : ENOENT\n", p->path_out);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 3;
|
|
|
|
case ENOSPC :
|
2016-06-05 03:19:36 +02:00
|
|
|
printf ("file %s : ENOSPC\n", p->path_out);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 4;
|
|
|
|
case ENOTDIR :
|
2016-06-05 03:19:36 +02:00
|
|
|
printf ("file %s : ENOTDIR\n", p->path_out);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 5;
|
|
|
|
case EROFS :
|
2016-06-05 03:19:36 +02:00
|
|
|
printf ("file %s : EROFS\n", p->path_out);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 6;
|
|
|
|
default :
|
2016-06-05 03:19:36 +02:00
|
|
|
printf ("err file %s unknown\n", p->path_out);
|
2016-05-27 17:00:02 +02:00
|
|
|
return 7;
|
|
|
|
}
|
2016-05-26 18:27:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
int app_destroy (struct process *p)
|
2016-05-26 18:27:59 +02:00
|
|
|
{
|
2016-06-05 03:19:36 +02:00
|
|
|
if (unlink (p->path_in)) {
|
2016-05-26 18:27:59 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
if (unlink (p->path_out)) {
|
2016-05-26 18:27:59 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
int app_read (struct process *p, void * buf, size_t * msize)
|
2016-05-27 17:00:02 +02:00
|
|
|
{
|
2016-06-05 03:19:36 +02:00
|
|
|
if (file_open (&p->in, p->path_in, "rb"))
|
2016-05-31 16:14:50 +02:00
|
|
|
return 1;
|
2016-05-27 17:00:02 +02:00
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
*msize = fread (buf, 1, *msize, p->in); // FIXME check errors
|
2016-05-30 16:30:05 +02:00
|
|
|
// printf ("DEBUG read, size %ld : %s\n", *msize, buf);
|
2016-05-27 17:00:02 +02:00
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
if (file_close (p->in))
|
2016-05-31 16:14:50 +02:00
|
|
|
return 1;
|
2016-05-26 21:56:43 +02:00
|
|
|
|
2016-05-26 18:27:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
int app_write (struct process *p, void * buf, size_t msize)
|
2016-05-26 18:27:59 +02:00
|
|
|
{
|
2016-06-05 03:19:36 +02:00
|
|
|
if (file_open (&p->out, p->path_out, "wb"))
|
2016-05-31 16:14:50 +02:00
|
|
|
return 1;
|
2016-05-26 21:56:43 +02:00
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
fwrite (buf, 1, msize, p->out); // FIXME check errors
|
2016-05-27 17:00:02 +02:00
|
|
|
|
2016-06-05 03:19:36 +02:00
|
|
|
if (file_close (p->out))
|
2016-05-31 16:14:50 +02:00
|
|
|
return 1;
|
2016-05-26 21:56:43 +02:00
|
|
|
|
2016-05-26 18:27:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|