From 7c7bcb63efafc39a4d0be105f8336ff28eeb62a1 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Wed, 14 Dec 2016 23:17:35 +0100 Subject: [PATCH] TO FIX - suppression de pid pour le process --- {lib => core}/communication.c | 24 +++++++++----------- {lib => core}/communication.h | 0 core/process.c | 31 ++++++++++++++++++++++++++ {lib => core}/process.h | 5 +---- {lib => core}/pubsub.c | 7 +++--- {lib => core}/pubsub.h | 0 {lib => core}/queue.h | 0 lib/process.c | 41 ----------------------------------- misc/init-connection.c | 16 +------------- pingpong/pingpong.sh | 5 ----- pubsub/app/test-subscribers.c | 2 -- pubsub/lib/pubsubd.c | 16 ++++++-------- pubsub/lib/pubsubd.h | 2 +- 13 files changed, 55 insertions(+), 94 deletions(-) rename {lib => core}/communication.c (91%) rename {lib => core}/communication.h (100%) create mode 100644 core/process.c rename {lib => core}/process.h (82%) rename {lib => core}/pubsub.c (97%) rename {lib => core}/pubsub.h (100%) rename {lib => core}/queue.h (100%) delete mode 100644 lib/process.c diff --git a/lib/communication.c b/core/communication.c similarity index 91% rename from lib/communication.c rename to core/communication.c index 1ac681a..b12428b 100644 --- a/lib/communication.c +++ b/core/communication.c @@ -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 diff --git a/lib/communication.h b/core/communication.h similarity index 100% rename from lib/communication.h rename to core/communication.h diff --git a/core/process.c b/core/process.c new file mode 100644 index 0000000..12cb66b --- /dev/null +++ b/core/process.c @@ -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); +} diff --git a/lib/process.h b/core/process.h similarity index 82% rename from lib/process.h rename to core/process.h index e653701..ff4bec9 100644 --- a/lib/process.h +++ b/core/process.h @@ -14,11 +14,8 @@ #include 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 *); diff --git a/lib/pubsub.c b/core/pubsub.c similarity index 97% rename from lib/pubsub.c rename to core/pubsub.c index 69e9c36..285d348 100644 --- a/lib/pubsub.c +++ b/core/pubsub.c @@ -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 diff --git a/lib/pubsub.h b/core/pubsub.h similarity index 100% rename from lib/pubsub.h rename to core/pubsub.h diff --git a/lib/queue.h b/core/queue.h similarity index 100% rename from lib/queue.h rename to core/queue.h diff --git a/lib/process.c b/lib/process.c deleted file mode 100644 index 5480e10..0000000 --- a/lib/process.c +++ /dev/null @@ -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); -} diff --git a/misc/init-connection.c b/misc/init-connection.c index c0a384f..143712a 100644 --- a/misc/init-connection.c +++ b/misc/init-connection.c @@ -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/ - * 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/ - */ - 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"); /* diff --git a/pingpong/pingpong.sh b/pingpong/pingpong.sh index ad1aa7b..c086c75 100755 --- a/pingpong/pingpong.sh +++ b/pingpong/pingpong.sh @@ -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 diff --git a/pubsub/app/test-subscribers.c b/pubsub/app/test-subscribers.c index 768e421..ff4205b 100644 --- a/pubsub/app/test-subscribers.c +++ b/pubsub/app/test-subscribers.c @@ -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}; diff --git a/pubsub/lib/pubsubd.c b/pubsub/lib/pubsubd.c index c1dab43..280bc5d 100644 --- a/pubsub/lib/pubsubd.c +++ b/pubsub/lib/pubsubd.c @@ -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/) // - // 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'; diff --git a/pubsub/lib/pubsubd.h b/pubsub/lib/pubsubd.h index 0bd9d6f..c6264db 100644 --- a/pubsub/lib/pubsubd.h +++ b/pubsub/lib/pubsubd.h @@ -11,7 +11,7 @@ struct app_list_elm; // parse pubsubd init msg (sent in TMPDIR/) // -// 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);