diff --git a/lib/communication.c b/lib/communication.c index cc2c87e..271e0ca 100644 --- a/lib/communication.c +++ b/lib/communication.c @@ -17,16 +17,22 @@ int file_write (const char *path, const char *buf, size_t msize) return ret; } - - int file_read (const char *path, char **buf, size_t *msize) { + if (buf == NULL) + return -1; + int fd = open (path, O_RDONLY); if (fd <= 0) { return ER_FILE_OPEN; } - if (*buf == NULL) + printf("FILE_READ: opened file %s\n", path); + + if (*buf == NULL) { *buf = malloc (BUFSIZ); + memset (*buf, 0, BUFSIZ); + } + int ret = 0; int ret2 = 0; ret = read (fd, *buf, BUFSIZ); @@ -36,11 +42,13 @@ int file_read (const char *path, char **buf, size_t *msize) else { *msize = ret; } + ret2 = close (fd); if (ret2 < 0) { fprintf (stderr, "err: close [err: %d] %s\n", ret2, path); perror ("closing"); - } + } + return ret; } @@ -161,7 +169,6 @@ int srv_get_new_process (const struct service *srv, struct process *p) } } - if (buf != NULL) free (buf); srv_process_gen (p, pid, index, version); diff --git a/lib/process.c b/lib/process.c index 0674b5f..899decc 100644 --- a/lib/process.c +++ b/lib/process.c @@ -29,17 +29,6 @@ void srv_process_gen (struct process *p snprintf(p->path_in , PATH_MAX, "%s%d-%d-in" , TMPDIR, pid, index); snprintf(p->path_out, PATH_MAX, "%s%d-%d-out", TMPDIR, pid, index); - //printf("path-in : %s\n", p->path_in ); - //printf("path-out : %s\n", p->path_out ); - -} - -void srv_process_free (struct process * p) -{ - // TODO nothing to do now - - // snprintf(p->path_in , PATH_MAX, "%s/%d-%d-in" , TMPDIR, pid, index); - // snprintf(p->path_out, PATH_MAX, "%s/%d-%d-out", TMPDIR, pid, index); } diff --git a/lib/pubsubd.c b/lib/pubsubd.c index d1619dd..a45f106 100644 --- a/lib/pubsubd.c +++ b/lib/pubsubd.c @@ -78,8 +78,10 @@ int pubsubd_channel_new (struct channel *c, const char * name) printf ("NAME : %s, SIZE : %ld\n", name, nlen); - if (c->chan == NULL) + if (c->chan == NULL) { c->chan = malloc (nlen +1); + memset (c->chan, 0, nlen +1); + } memset (c->chan, 0, nlen +1); memcpy (c->chan, name, nlen); @@ -126,8 +128,10 @@ void pubsubd_subscriber_init (struct app_list_head **chans) { if (chans == NULL) return; - if (*chans == NULL) + if (*chans == NULL) { *chans = malloc (sizeof(struct channels)); + memset (*chans, 0, sizeof(struct channels)); + } LIST_INIT(*chans); } @@ -149,12 +153,7 @@ void pubsubd_channel_print (const struct channel *c) if (c == NULL || c->chan == NULL) return; - if (c->chan == NULL) { - printf ( "\033[32mchan name not available\033[00m\n"); - } - else { - printf ( "\033[32mchan %s\033[00m\n", c->chan); - } + printf ( "\033[32mchan %s\033[00m\n", c->chan); if (c->alh == NULL) return; @@ -173,6 +172,7 @@ struct app_list_elm * pubsubd_app_list_elm_copy (const struct app_list_elm *ale) struct app_list_elm * n = NULL; n = malloc (sizeof (struct app_list_elm)); + memset (n, 0, sizeof (struct app_list_elm)); if (ale->p != NULL) n->p = srv_process_copy (ale->p); @@ -248,6 +248,9 @@ void pubsubd_app_list_elm_create (struct app_list_elm *ale, struct process *p) if (ale == NULL) return; + if (ale->p != NULL) + free (ale->p); + ale->p = srv_process_copy (p); } @@ -273,6 +276,7 @@ void pubsubd_msg_serialize (const struct pubsub_msg *msg, char **data, size_t *l *data = NULL; } *data = malloc(*len); + memset (*data, 0, *len); data[0][0] = msg->type; return; } @@ -286,6 +290,7 @@ void pubsubd_msg_serialize (const struct pubsub_msg *msg, char **data, size_t *l *data = NULL; } *data = malloc(*len); + memset (*data, 0, *len); size_t i = 0; @@ -334,6 +339,7 @@ void pubsubd_msg_unserialize (struct pubsub_msg *msg, const char *data, size_t l return; } msg->chan = malloc (msg->chanlen); + memset (msg->chan, 0, msg->chanlen); memcpy (msg->chan, data + i, msg->chanlen); i += msg->chanlen; memcpy (&msg->datalen, data + i, sizeof(size_t)); i += sizeof(size_t); @@ -342,6 +348,7 @@ void pubsubd_msg_unserialize (struct pubsub_msg *msg, const char *data, size_t l return; } msg->data = malloc (msg->datalen); + memset (msg->data, 0, msg->datalen); memcpy (msg->data, data + i, msg->datalen); i += msg->datalen; } @@ -389,9 +396,15 @@ int pubsubd_get_new_process (const char *spath, struct app_list_elm *ale char chan[BUFSIZ]; memset (chan, 0, BUFSIZ); + if (buf == NULL) { + return -2; + } + printf ("INIT: %s\n", buf); for (str = buf, i = 1; ; str = NULL, i++) { + if (str == NULL) + break; token = strtok_r(str, " ", &saveptr); if (token == NULL) break; @@ -441,17 +454,19 @@ 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); if (*c == NULL) { *c = malloc (sizeof (struct channel)); + memset (*c, 0, sizeof (struct channel)); } chan[BUFSIZ -1] = '\0'; pubsubd_channel_new (*c, chan); - struct channel *new_chan = NULL; + new_chan = pubsubd_channel_get (chans, *c); if (new_chan == NULL) { new_chan = pubsubd_channels_add (chans, *c); diff --git a/pingpong/pingpong b/pingpong/pingpong deleted file mode 100755 index 9ae7e44..0000000 Binary files a/pingpong/pingpong and /dev/null differ diff --git a/pingpong/pingpong.c b/pingpong/pingpong.c index e105db0..dd34e94 100644 --- a/pingpong/pingpong.c +++ b/pingpong/pingpong.c @@ -77,6 +77,7 @@ int main(int argc, char * argv[], char **env) fprintf(stdout, "error service_create %d\n", ret); exit (1); } + printf("MAIN: server created\n" ); // the service will loop until the end of time, a specific message, a signal main_loop (&srv); diff --git a/pingpong/pingpong.sh b/pingpong/pingpong.sh index bbb0250..7d7eb22 100755 --- a/pingpong/pingpong.sh +++ b/pingpong/pingpong.sh @@ -2,15 +2,13 @@ REP=/tmp/ipc/ SERVICE="pongd" -NB=3 +NB=10 # CLEAN UP ! if [ $# -ne 0 ] && [ "$1" = clean ] then echo "clean rep ${REP}" rm ${REP}/${SERVICE} - rm ${REP}/*-in - rm ${REP}/*-out exit 0 fi @@ -22,8 +20,6 @@ fi for pid in `seq 1 ${NB}` do - echo "${pid}" - # we make the application pipes mkfifo ${REP}${pid}-1-in 2>/dev/null mkfifo ${REP}${pid}-1-out 2>/dev/null @@ -36,5 +32,10 @@ do # echo "hello world" > ${REP}${pid}-1-out # the the service will answer with our message + echo "pid : ${pid}" cat ${REP}/${pid}-1-in done + + echo "clean rep" + rm ${REP}/*-in + rm ${REP}/*-out \ No newline at end of file diff --git a/pubsub/test-gen-new-process.c b/pubsub/test-gen-new-process.c index 6999ac1..cc2f733 100644 --- a/pubsub/test-gen-new-process.c +++ b/pubsub/test-gen-new-process.c @@ -27,7 +27,7 @@ main(int argc, char **argv) struct channels chans; memset (&chans, 0, sizeof (struct channels)); - for (int nb = 10, i = 0 ; nb > 0; i++, nb--) { + for (int nb = 1, i = 0 ; nb > 0; i++, nb--) { struct app_list_elm ale; memset (&ale, 0, sizeof (struct app_list_elm)); @@ -42,6 +42,8 @@ main(int argc, char **argv) pubsubd_channels_print (&chans); printf ("--\n"); printf ("still %d remaining processes\n", nb); + + pubsubd_app_list_elm_free (&ale); } pubsubd_channels_del_all (&chans);