TO FIX - suppression de pid pour le process

more_to_read
Philippe PITTOLI 2016-12-14 23:17:35 +01:00
parent f451e0f88d
commit 7c7bcb63ef
13 changed files with 55 additions and 94 deletions

View File

@ -9,7 +9,7 @@
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int file_write (const int fd, const char *buf, const int msize)
int msg_send (const int fd, const char *buf, const int msize)
{
int ret = 0;
//printf ("%ld bytes to write\n", msize);
@ -21,7 +21,7 @@ int file_write (const int fd, const char *buf, const int msize)
return ret;
}
int file_read (const int fd, char **buf)
int msg_recv (const int fd, char **buf)
{
int ret = 0;
ret = recv (fd, *buf, BUFSIZ, 0);
@ -128,7 +128,6 @@ int srv_get_new_process (char *buf, struct process *p)
char *str = NULL;
int i = 0;
pid_t pid = 0;
int index = 0;
int version = 0;
@ -138,19 +137,16 @@ int srv_get_new_process (char *buf, struct process *p)
break;
if (i == 1) {
pid = strtoul(token, NULL, 10);
}
else if (i == 2) {
index = strtoul(token, NULL, 10);
}
else if (i == 3) {
else if (i == 2) {
version = strtoul(token, NULL, 10);
}
}
//if (buf != NULL)
// free (buf);
srv_process_gen (p, pid, index, version);
srv_process_gen (p, index, version);
return 0;
}
@ -158,13 +154,13 @@ int srv_get_new_process (char *buf, struct process *p)
int srv_read (const struct service *srv, char ** buf)
{
//printf("---%s\n", srv->spath);
return file_read (srv->service_fd, buf);
return msg_recv (srv->service_fd, buf);
}
int srv_write (const struct service *srv, const char * buf, size_t msize)
{
//printf("---%s\n", srv->spath);
return file_write (srv->service_fd, buf, msize);
return msg_send (srv->service_fd, buf, msize);
}
// APPLICATION
@ -227,14 +223,14 @@ int proc_connection(struct process *p) {
return 0;
}
int app_create (struct process *p, pid_t pid, int index, int version)
int app_create (struct process *p, int index, int version)
{
if (version == 0) {
version = COMMUNICATION_VERSION;
}
// then creates the structure
srv_process_gen (p, pid, index, version);
srv_process_gen (p, index, version);
return 0;
}
@ -251,13 +247,13 @@ int app_destroy (struct process *p)
int app_read (struct process *p, char ** buf)
{
//printf("---%s\n", p->path_proc);
return file_read (p->proc_fd, buf);
return msg_recv (p->proc_fd, buf);
}
int app_write (struct process *p, char * buf, size_t msize)
{
//printf("---%s\n", p->path_proc);
return file_write (p->proc_fd, buf, msize);
return msg_send (p->proc_fd, buf, msize);
}
/*init a unix socket : bind, listen

31
core/process.c Normal file
View File

@ -0,0 +1,31 @@
#include "process.h"
struct process * srv_process_copy (const struct process *p)
{
if (p == NULL)
return NULL;
struct process * copy = malloc (sizeof(struct process));
memcpy (copy, p, sizeof (struct process));
return copy;
}
int srv_process_eq (const struct process *p1, const struct process *p2)
{
return (p1->version == p2->version && p1->index == p2->index);
}
void srv_process_gen (struct process *p
, unsigned int index, unsigned int version)
{
p->version = version;
p->index = index;
snprintf(p->path_proc, PATH_MAX, "%s%d-%d", TMPDIR, index, version);
}
void srv_process_print (struct process *p)
{
if (p != NULL)
printf ("process %d : index %d, version %d\n", p->index, p->version);
}

View File

@ -14,11 +14,8 @@
#include <string.h>
struct process {
pid_t pid;
unsigned int version;
unsigned int index;
char path_in [PATH_MAX];
char path_out [PATH_MAX];
char path_proc [PATH_MAX];
int proc_fd;
};
@ -29,7 +26,7 @@ int srv_process_eq (const struct process *p1, const struct process *p2);
// create the service process structure
void srv_process_gen (struct process *p
, pid_t pid, unsigned int index, unsigned int version);
, unsigned int index, unsigned int version);
void srv_process_print (struct process *);

View File

@ -113,10 +113,11 @@ void pubsub_connection (struct service *srv, struct process *p, enum app_list_el
char line[BUFSIZ];
memset (line, 0, BUFSIZ);
// line fmt : pid index version action chan
// line fmt : index version action chan
// "quit" action is also possible (see pubsubd_quit)
snprintf (line, BUFSIZ, "%d %d %d %s %s\n"
, p->pid, p->index, p->version
snprintf (line, BUFSIZ, "%d %d %s %s\n"
, p->index
, p->version
, straction
, channame);
line[BUFSIZ -1] = '\0'; // to be sure

View File

@ -1,41 +0,0 @@
#include "process.h"
struct process * srv_process_copy (const struct process *p)
{
if (p == NULL)
return NULL;
struct process * copy = malloc (sizeof(struct process));
memcpy (copy, p, sizeof (struct process));
return copy;
}
int srv_process_eq (const struct process *p1, const struct process *p2)
{
return (p1->pid == p2->pid && p1->version == p2->version
&& p1->index == p2->index);
}
void srv_process_gen (struct process *p
, pid_t pid, unsigned int index, unsigned int version)
{
p->pid = pid;
p->version = version;
p->index = index;
memset (p->path_in, 0, PATH_MAX);
memset (p->path_out, 0, PATH_MAX);
snprintf(p->path_in , PATH_MAX, "%s%d-%d-%d-in" , TMPDIR, pid, index, version);
snprintf(p->path_out, PATH_MAX, "%s%d-%d-%d-out", TMPDIR, pid, index, version);
snprintf(p->path_proc, PATH_MAX, "%s%d-%d-%d", TMPDIR, pid, index, version);
}
void srv_process_print (struct process *p)
{
if (p != NULL)
printf ("process %d : index %d, version %d\n"
, p->pid, p->index, p->version);
}

View File

@ -8,19 +8,6 @@ ohshit(int rvalue, const char* str) {
exit(rvalue);
}
/*
* pipes creation and removal test program
*
* 1. S creates the named pipe /tmp/ipc/<service>
* 2. App creates named pipes in & out, /tmp/ipc/$pid-$index-$version-{in,out}
*
* ... some communication between S and App...
* App wants to stop
*
* 3. App removes the named pipes in and out
* 4. S removes the named pipe /tmp/ipc/<service>
*/
int main(int argc, char * argv[], char *env[])
{
struct service srv;
@ -39,12 +26,11 @@ int main(int argc, char * argv[], char *env[])
struct process p;
memset (&p, 0, sizeof (struct process));
pid_t pid = getpid();
int index = 0; // first time we communication with the service
int version = 1;
printf ("app creation\n");
if (app_create (&p, pid, index, version)) // called by the application
if (app_create (&p, index, version)) // called by the application
ohshit (1, "app_create");
/*

View File

@ -21,15 +21,10 @@ fi
for pid in `seq 1 ${NB}`
do
# we make the application pipes
# mkfifo ${REP}${pid}-1-1-in 2>/dev/null
# mkfifo ${REP}${pid}-1-1-out 2>/dev/null
# pid index version
echo "${pid} 1 1" | nc -U ${REP}${SERVICE}
# the purpose is to send something in the pipe
#cat /dev/urandom | base64 | head -n 1 > ${REP}${pid}-1-1-out
echo "hello frero" | nc -U ${REP}${pid}-1-1
#echo "exit" | nc -U ${REP}${pid}-1-1

View File

@ -35,8 +35,6 @@ void fill_process (struct process *p)
p->pid = 10;
p->version = 1;
p->index = 1;
memcpy (p->path_in, "pathin", strlen ("pathin") +1);
memcpy (p->path_out, "pathout", strlen ("pathout") +1);
}
// enum app_list_elm_action {PUBSUB_QUIT = 1, PUBSUB_PUB, PUBSUB_SUB, PUBSUB_BOTH};

View File

@ -126,7 +126,7 @@ void * pubsubd_worker_thread (void *params)
pubsubd_msg_recv (ale->p, &m);
if (m.type == PUBSUB_TYPE_DISCONNECT) {
printf ("process %d disconnecting...\n", ale->p->pid);
// printf ("process %d disconnecting...\n", ale->p->pid);
if ( 0 != pubsubd_subscriber_del (chan->alh, ale)) {
fprintf (stderr, "err : subscriber not registered\n");
}
@ -462,13 +462,12 @@ int pubsubd_get_new_process (const char *spath, struct app_list_elm *ale
file_read (spath, &buf, &msize);
// parse pubsubd init msg (sent in TMPDIR/<service>)
//
// line fmt : pid index version action chan
// line fmt : index version action chan
// action : quit | pub | sub
size_t i = 0;
char *str = NULL, *token = NULL, *saveptr = NULL;
pid_t pid = 0;
int index = 0;
int version = 0;
@ -488,10 +487,9 @@ int pubsubd_get_new_process (const char *spath, struct app_list_elm *ale
break;
switch (i) {
case 1 : pid = strtoul(token, NULL, 10); break;
case 2 : index = strtoul(token, NULL, 10); break;
case 3 : version = strtoul(token, NULL, 10); break;
case 4 : {
case 1 : index = strtoul(token, NULL, 10); break;
case 2 : version = strtoul(token, NULL, 10); break;
case 3 : {
if (strncmp("both", token, 4) == 0) {
ale->action = PUBSUB_BOTH;
}
@ -506,7 +504,7 @@ int pubsubd_get_new_process (const char *spath, struct app_list_elm *ale
}
break;
}
case 5 : {
case 4 : {
// for the last element of the line
// drop the following \n
if (ale->action != PUBSUB_QUIT) {
@ -534,7 +532,7 @@ int pubsubd_get_new_process (const char *spath, struct app_list_elm *ale
ale->p = malloc (sizeof (struct process));
memset (ale->p, 0, sizeof (struct process));
srv_process_gen (ale->p, pid, index, version);
srv_process_gen (ale->p, index, version);
chan[BUFSIZ -1] = '\0';

View File

@ -11,7 +11,7 @@ struct app_list_elm;
// parse pubsubd init msg (sent in TMPDIR/<service>)
//
// line fmt : pid index version action chan
// TODO TLV line fmt : index version action chan
// action : quit | pub | sub
int pubsubd_get_new_process (const char *spath, struct app_list_elm *ale
, struct channels *chans, struct channel **c);