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-09-09 12:06:25 +02:00
|
|
|
int file_write (const char *path, const char *buf, size_t msize)
|
2016-06-05 03:19:36 +02:00
|
|
|
{
|
2016-09-09 12:06:25 +02:00
|
|
|
int fd = open (path, O_WRONLY);
|
|
|
|
if (fd <= 0) {
|
2016-06-12 12:38:43 +02:00
|
|
|
return ER_FILE_OPEN;
|
2016-06-05 03:19:36 +02:00
|
|
|
}
|
2016-09-09 12:06:25 +02:00
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
ret = write (fd, buf, msize);
|
2016-06-05 03:19:36 +02:00
|
|
|
|
2016-09-09 12:06:25 +02:00
|
|
|
close (fd);
|
2016-06-05 03:19:36 +02:00
|
|
|
|
2016-09-09 12:06:25 +02:00
|
|
|
return ret;
|
2016-06-05 03:19:36 +02:00
|
|
|
}
|
|
|
|
|
2016-09-10 16:59:53 +02:00
|
|
|
int file_read (const char *path, char **buf, size_t *msize)
|
2016-06-12 12:38:43 +02:00
|
|
|
{
|
2016-09-10 17:16:39 +02:00
|
|
|
if (buf == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2016-09-10 16:59:53 +02:00
|
|
|
int fd = open (path, O_RDONLY);
|
|
|
|
if (fd <= 0) {
|
|
|
|
return ER_FILE_OPEN;
|
2016-06-12 12:38:43 +02:00
|
|
|
}
|
2016-09-10 18:34:02 +02:00
|
|
|
printf("FILE_READ: opened file %s\n", path);
|
2016-06-12 12:38:43 +02:00
|
|
|
|
2016-09-10 17:16:39 +02:00
|
|
|
if (*buf == NULL) {
|
2016-09-10 16:59:53 +02:00
|
|
|
*buf = malloc (BUFSIZ);
|
2016-09-10 17:16:39 +02:00
|
|
|
memset (*buf, 0, BUFSIZ);
|
|
|
|
}
|
2016-09-09 14:44:15 +02:00
|
|
|
|
2016-09-10 16:59:53 +02:00
|
|
|
int ret = 0;
|
|
|
|
int ret2 = 0;
|
|
|
|
ret = read (fd, *buf, BUFSIZ);
|
|
|
|
if (ret <= 0) {
|
|
|
|
fprintf (stderr, "err: read %s\n", path);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*msize = ret;
|
|
|
|
}
|
2016-06-12 12:38:43 +02:00
|
|
|
|
2016-09-10 16:59:53 +02:00
|
|
|
ret2 = close (fd);
|
|
|
|
if (ret2 < 0) {
|
|
|
|
fprintf (stderr, "err: close [err: %d] %s\n", ret2, path);
|
|
|
|
perror ("closing");
|
2016-09-09 14:44:15 +02:00
|
|
|
}
|
2016-06-12 12:38:43 +02:00
|
|
|
|
2016-09-10 17:50:02 +02:00
|
|
|
|
2016-09-09 12:06:25 +02:00
|
|
|
return ret;
|
2016-06-12 12:38:43 +02:00
|
|
|
}
|
|
|
|
|
2016-06-13 09:47:19 +02:00
|
|
|
int srv_init (int argc, char **argv, char **env, struct service *srv, const char *sname, int (*cb)(int argc, char **argv, char **env, 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)
|
2016-06-13 09:47:19 +02:00
|
|
|
return ER_PARAMS;
|
2016-05-26 18:27:59 +02:00
|
|
|
|
2016-06-12 14:41:25 +02:00
|
|
|
// TODO
|
|
|
|
// use the argc, argv and env parameters
|
|
|
|
// it will be useful to change some parameters transparently
|
|
|
|
// ex: to get resources from other machines, choosing the
|
|
|
|
// remote with environment variables
|
|
|
|
|
|
|
|
argc = argc;
|
|
|
|
argv = argv;
|
|
|
|
env = env;
|
|
|
|
|
2016-06-05 20:48:13 +02:00
|
|
|
// gets the service path, such as /tmp/<service>
|
2016-06-10 20:02:58 +02:00
|
|
|
memset (srv->spath, 0, PATH_MAX);
|
2016-06-12 12:38:43 +02:00
|
|
|
strncat (srv->spath, TMPDIR, PATH_MAX -1);
|
|
|
|
strncat (srv->spath, sname, PATH_MAX -1);
|
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-06-13 09:47:19 +02:00
|
|
|
|
|
|
|
if (cb != NULL) {
|
|
|
|
int ret = (*cb) (argc, argv, env, srv, sname);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
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-07 11:45:21 +02:00
|
|
|
int srv_get_new_process (const struct service *srv, struct process *p)
|
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-09-09 12:06:25 +02:00
|
|
|
char *buf = NULL;
|
|
|
|
size_t msize = 0;
|
|
|
|
int ret = file_read (srv->spath, &buf, &msize);
|
|
|
|
if (ret <= 0) {
|
|
|
|
fprintf (stderr, "err: listening on %s\n", srv->spath);
|
|
|
|
exit (1);
|
2016-06-12 12:38:43 +02:00
|
|
|
}
|
|
|
|
|
2016-06-10 01:21:04 +02:00
|
|
|
char *token = NULL, *saveptr = NULL;
|
|
|
|
char *str = NULL;
|
|
|
|
int i = 0;
|
2016-05-26 18:27:59 +02:00
|
|
|
|
2016-06-10 01:21:04 +02:00
|
|
|
pid_t pid = 0;
|
|
|
|
int index = 0;
|
|
|
|
int version = 0;
|
2016-06-05 03:19:36 +02:00
|
|
|
|
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-09-10 02:54:53 +02:00
|
|
|
if (buf != NULL)
|
|
|
|
free (buf);
|
2016-06-05 03:19:36 +02:00
|
|
|
srv_process_gen (p, pid, index, version);
|
|
|
|
|
2016-09-10 16:23:30 +02:00
|
|
|
return 1;
|
2016-06-07 11:45:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int srv_read (struct process *p, char ** buf, size_t * msize)
|
2016-05-30 01:54:19 +02:00
|
|
|
{
|
2016-09-09 12:06:25 +02:00
|
|
|
return file_read (p->path_out, buf, msize);
|
2016-05-26 21:56:43 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 12:38:43 +02:00
|
|
|
int srv_write (struct process *p, char * buf, size_t msize)
|
2016-05-26 21:56:43 +02:00
|
|
|
{
|
2016-09-09 12:06:25 +02:00
|
|
|
return file_write (p->path_in, buf, msize);
|
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-10 01:21:04 +02:00
|
|
|
// send the connection string to $TMP/<service>
|
|
|
|
int app_srv_connection (struct service *srv, const char *connectionstr, size_t msize)
|
|
|
|
{
|
|
|
|
if (srv == NULL) {
|
2016-09-09 12:06:25 +02:00
|
|
|
return -1;
|
2016-06-12 12:38:43 +02:00
|
|
|
}
|
2016-09-09 12:06:25 +02:00
|
|
|
|
|
|
|
return file_write (srv->spath, connectionstr, msize);
|
2016-06-10 01:21:04 +02:00
|
|
|
}
|
|
|
|
|
2016-06-13 09:47:19 +02:00
|
|
|
int app_create (struct process *p, pid_t pid, int index, int version)
|
2016-05-26 18:27:59 +02:00
|
|
|
{
|
2016-06-13 09:47:19 +02:00
|
|
|
if (version == 0) {
|
|
|
|
version = COMMUNICATION_VERSION;
|
|
|
|
}
|
2016-06-05 03:19:36 +02:00
|
|
|
|
|
|
|
// then creates the structure
|
2016-06-13 09:47:19 +02:00
|
|
|
srv_process_gen (p, pid, index, 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-12 12:38:43 +02:00
|
|
|
int app_read (struct process *p, char ** buf, size_t * msize)
|
2016-05-27 17:00:02 +02:00
|
|
|
{
|
2016-09-09 12:06:25 +02:00
|
|
|
return file_read (p->path_in, buf, msize);
|
2016-05-26 18:27:59 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 12:38:43 +02:00
|
|
|
int app_write (struct process *p, char * buf, size_t msize)
|
2016-05-26 18:27:59 +02:00
|
|
|
{
|
2016-09-09 12:06:25 +02:00
|
|
|
return file_write (p->path_out, buf, msize);
|
2016-05-26 18:27:59 +02:00
|
|
|
}
|