From 4549a5b36278625b6154baf73ac4f8533960d187 Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Sun, 12 Jun 2016 12:38:43 +0200
Subject: [PATCH] code bien plus propre
---
lib/communication.c | 253 ++++++++++++++++++++++++++++++++++----------
lib/communication.h | 19 +++-
lib/pubsubd.c | 50 +++++++--
lib/pubsubd.h | 1 -
4 files changed, 255 insertions(+), 68 deletions(-)
diff --git a/lib/communication.c b/lib/communication.c
index d2c3db0..d3acd33 100644
--- a/lib/communication.c
+++ b/lib/communication.c
@@ -7,12 +7,14 @@ int file_open (FILE **f, const char *path, const char *mode)
printf ("opening %s\n", path);
if (*f != NULL) {
printf ("f != NULL : %p\n", (void*) *f);
- fclose (*f);
+ if (file_close (*f)) {
+ return ER_FILE_CLOSE;
+ }
}
*f = fopen (path, mode);
if (*f == NULL) {
fprintf (stderr, "\033[31mnot opened\033[00m\n");
- return -1;
+ return ER_FILE_OPEN;
}
printf ("opened : %ld\n", (long) *f);
@@ -23,12 +25,56 @@ int file_close (FILE *f)
{
if (f != 0) {
printf ("before fclosing\n");
- fclose (f);
+ if (fclose (f)) {
+ return ER_FILE_CLOSE;
+ }
printf ("after fclosing\n");
}
return 0;
}
+int file_read (FILE *f, char **buf, size_t *msize) {
+ if (*msize == 0) {
+ *msize = BUFSIZ; // default value
+ }
+
+ if (*buf == NULL) {
+ *buf = malloc (*msize);
+ if (*buf == NULL) {
+ fprintf (stderr, "err can't allocate enough memory (%ld)\n", *msize);
+ int ret = file_close (f);
+ if (ret != 0)
+ return ret;
+ }
+ }
+
+ *msize = fread (*buf, *msize, 1, f);
+ if (*msize == 0) {
+ fprintf (stderr, "err can't read a file\n");
+ if (ER_FILE_CLOSE == file_close (f)) {
+ fprintf (stderr, "err closing the file\n");
+ return ER_FILE_CLOSE;
+ }
+ return ER_FILE_READ;
+ }
+
+ return 0;
+}
+
+int file_write (FILE *f, const char *buf, size_t msize)
+{
+ if (0 == fwrite (buf, msize, 1, f)) {
+ fprintf (stderr, "err writing in the file\n");
+ if (ER_FILE_CLOSE == file_close (f)) {
+ fprintf (stderr, "err closing the file\n");
+ return ER_FILE_CLOSE;
+ }
+ return ER_FILE_WRITE;
+ }
+
+ return 0;
+}
+
void srv_init (struct service *srv, const char *sname)
{
if (srv == NULL)
@@ -36,8 +82,8 @@ void srv_init (struct service *srv, const char *sname)
// gets the service path, such as /tmp/
memset (srv->spath, 0, PATH_MAX);
- strncat (srv->spath, TMPDIR, PATH_MAX);
- strncat (srv->spath, sname, PATH_MAX);
+ strncat (srv->spath, TMPDIR, PATH_MAX -1);
+ strncat (srv->spath, sname, PATH_MAX -1);
srv->version = COMMUNICATION_VERSION;
srv->index = 0; // TODO
@@ -95,9 +141,21 @@ int srv_get_listen_raw (const struct service *srv, char **buf, size_t *msize)
*buf = malloc(BUFSIZ);
memset (*buf, 0, BUFSIZ);
- FILE * f = fopen (srv->spath, "r");
- fgets (*buf, BUFSIZ, f);
- fclose (f);
+ FILE * f = NULL;
+ if (file_open (&f, srv->spath, "rb")) {
+ return ER_FILE_OPEN;
+ }
+
+ char *ret = NULL;
+ ret = fgets (*buf, BUFSIZ, f);
+ if (ret == NULL) {
+ return ER_FILE_READ;
+ }
+ buf[0][BUFSIZ -1] = '\0';
+
+ if (file_close (f)) {
+ return ER_FILE_CLOSE;
+ }
*msize = strlen (*buf);
@@ -117,11 +175,26 @@ int srv_get_new_process (const struct service *srv, struct process *p)
struct timespec ts = { 0 };
struct timespec ts2 = { 0 };
- FILE * f = fopen (srv->spath, "r");
+ FILE * f = NULL;
+ if (file_open (&f, srv->spath, "rb")) {
+ return ER_FILE_OPEN;
+ }
+
clock_gettime(CLOCK_REALTIME, &ts);
- fgets (buf, BUFSIZ, f);
+
+ char *ret = NULL;
+ ret = fgets (buf, BUFSIZ, f);
+ if (ret == NULL) {
+ if (file_close (f)) {
+ return ER_FILE_CLOSE;
+ }
+ return ER_FILE_READ;
+ }
+
clock_gettime(CLOCK_REALTIME, &ts2);
- fclose (f);
+ if (file_close (f)) {
+ return ER_FILE_CLOSE;
+ }
printf("sec: %ld nsec: %ld\n", ts.tv_sec, ts.tv_nsec);
printf("sec: %ld nsec: %ld\n", ts2.tv_sec, ts2.tv_nsec);
@@ -162,18 +235,30 @@ int srv_read_cb (struct process *p, char ** buf, size_t * msize
{
if (file_open (&p->out, p->path_out, "rb")) {
fprintf (stderr, "\033[31merr: srv_read_cb, file_open\033[00m\n");
- file_close (p->out);
- return 1;
+ if (ER_FILE_CLOSE == file_close (p->out)) {
+ fprintf (stderr, "err closing the file %s\n", p->path_out);
+ p->out = NULL;
+ }
+ return ER_FILE_OPEN;
}
- if (cb != NULL)
- (*cb) (p->out, buf, msize);
- else
- *msize = fread (*buf, 1, *msize, p->out); // FIXME check errors
+ if (cb != NULL) {
+ int ret = (*cb) (p->out, buf, msize);
+ if (ret != 0)
+ return ret;
+ }
+ else {
+ int ret = file_read (p->out, buf, msize);
+ if (ret != 0) {
+ return ret;
+ }
+ }
// printf ("DEBUG read, size %ld : %s\n", *msize, *buf);
- if (file_close (p->out))
- return 1;
+ if (ER_FILE_CLOSE == file_close (p->out)) {
+ fprintf (stderr, "err closing the file %s\n", p->path_out);
+ p->out = NULL;
+ }
p->out = NULL;
return 0;
@@ -181,28 +266,48 @@ int srv_read_cb (struct process *p, char ** buf, size_t * msize
int srv_read (struct process *p, char ** buf, size_t * msize)
{
- if (file_open (&p->out, p->path_out, "rb"))
- return 1;
+ if (ER_FILE_OPEN == file_open (&p->out, p->path_out, "rb")) {
+ fprintf (stderr, "err opening the file %s\n", p->path_out);
+ return ER_FILE_OPEN;
+ }
+
+ int ret = file_read (p->out, buf, msize);
+ if (ret != 0) {
+ p->out = NULL;
+ return ret;
+ }
- *msize = fread (*buf, 1, *msize, p->out); // FIXME check errors
// printf ("DEBUG read, size %ld : %s\n", *msize, buf);
- if (file_close (p->out))
- return 1;
+ if (ER_FILE_CLOSE == file_close (p->out)) {
+ fprintf (stderr, "err closing the file %s\n", p->path_out);
+ p->out = NULL;
+ return ER_FILE_CLOSE;
+ }
p->out = NULL;
return 0;
}
-int srv_write (struct process *p, void * buf, size_t msize)
+int srv_write (struct process *p, char * buf, size_t msize)
{
- if (file_open (&p->in, p->path_in, "wb"))
- return 1;
+ if (ER_FILE_OPEN == file_open (&p->in, p->path_in, "wb")) {
+ fprintf (stderr, "err opening the file %s\n", p->path_in);
+ return ER_FILE_OPEN;
+ }
- fwrite (buf, 1, msize, p->in); // FIXME check errors
+ int ret = file_write (p->in, buf, msize);
+ if (ret != 0) {
+ fprintf (stderr, "err writing in the file %s\n", p->path_in);
+ p->in = NULL;
+ return ret;
+ }
- if (file_close (p->in))
- return 1;
+ if (ER_FILE_CLOSE == file_close (p->in)) {
+ fprintf (stderr, "err closing the file %s\n", p->path_in);
+ p->in = NULL;
+ return ER_FILE_CLOSE;
+ }
p->in = NULL;
return 0;
@@ -218,13 +323,21 @@ int app_srv_connection (struct service *srv, const char *connectionstr, size_t m
}
FILE * f = NULL;
- if (file_open (&f, srv->spath, "wb"))
- return 2;
+ if (ER_FILE_OPEN == file_open (&f, srv->spath, "wb")) {
+ fprintf (stderr, "err opening the service file %s\n", srv->spath);
+ return ER_FILE_OPEN;
+ }
- fwrite (connectionstr, msize, 1, f); // FIXME check errors
+ int ret = file_write (f, connectionstr, msize);
+ if (ret != 0) {
+ fprintf (stderr, "err writing in the service file %s\n", srv->spath);
+ return ret;
+ }
- if (file_close (f))
- return 3;
+ if (ER_FILE_CLOSE == file_close (f)) {
+ fprintf (stderr, "err closing the file\n");
+ return ER_FILE_CLOSE;
+ }
return 0;
}
@@ -317,47 +430,75 @@ int app_read_cb (struct process *p, char ** buf, size_t * msize
, int (*cb)(FILE *f, char ** buf, size_t * msize))
{
if (file_open (&p->in, p->path_in, "rb")) {
- fprintf (stderr, "\033[31merr: srv_read_cb, file_open\033[00m\n");
- file_close (p->in);
+ fprintf (stderr, "\033[31merr: app_read_cb, file_open\033[00m\n");
+ p->in = NULL;
return 1;
}
- if (cb != NULL)
- (*cb) (p->in, buf, msize);
- else
- *msize = fread (*buf, 1, *msize, p->in); // FIXME check errors
+ if (cb != NULL) {
+ int ret = (*cb) (p->in, buf, msize);
+ if (ret != 0)
+ return ret;
+ }
+ else {
+ int ret = file_read (p->in, buf, msize);
+ if (ret != 0) {
+ p->in = NULL;
+ return ret;
+ }
+ }
- if (file_close (p->in))
- return 1;
+
+ if (ER_FILE_CLOSE == file_close (p->in)) {
+ fprintf (stderr, "err closing the file %s\n", p->path_in);
+ }
p->in = NULL;
return 0;
}
-int app_read (struct process *p, void * buf, size_t * msize)
+int app_read (struct process *p, char ** buf, size_t * msize)
{
- if (file_open (&p->in, p->path_in, "rb"))
- return 1;
+ if (ER_FILE_OPEN == file_open (&p->in, p->path_in, "rb")) {
+ fprintf (stderr, "err opening the file %s\n", p->path_in);
+ return ER_FILE_OPEN;
+ }
- *msize = fread (buf, 1, *msize, p->in); // FIXME check errors
- // printf ("DEBUG read, size %ld : %s\n", *msize, buf);
+ int ret = file_read (p->in, buf, msize);
+ if (ret != 0) {
+ p->in = NULL;
+ return ret;
+ }
- if (file_close (p->in))
- return 1;
+ if (ER_FILE_CLOSE == file_close (p->in)) {
+ fprintf (stderr, "err closing the file %s\n", p->path_in);
+ p->in = NULL;
+ return ER_FILE_CLOSE;
+ }
p->in = NULL;
return 0;
}
-int app_write (struct process *p, void * buf, size_t msize)
+int app_write (struct process *p, char * buf, size_t msize)
{
- if (file_open (&p->out, p->path_out, "wb"))
- return 1;
+ if (ER_FILE_OPEN == file_open (&p->out, p->path_out, "wb")) {
+ fprintf (stderr, "err opening the file %s\n", p->path_out);
+ return ER_FILE_OPEN;
+ }
- fwrite (buf, 1, msize, p->out); // FIXME check errors
+ int ret = file_write (p->out, buf, msize);
+ if (ret != 0) {
+ fprintf (stderr, "err writing in the file %s\n", p->path_out);
+ p->out = NULL;
+ return ret;
+ }
- if (file_close (p->out))
- return 1;
+ if (ER_FILE_CLOSE == file_close (p->out)) {
+ fprintf (stderr, "err closing the file %s\n", p->path_out);
+ p->out = NULL;
+ return ER_FILE_CLOSE;
+ }
p->out = NULL;
return 0;
diff --git a/lib/communication.h b/lib/communication.h
index e9139de..f646823 100644
--- a/lib/communication.h
+++ b/lib/communication.h
@@ -16,6 +16,13 @@
#define COMMUNICATION_VERSION 1
+#define ER_FILE_OPEN 1
+#define ER_FILE_CLOSE 2
+#define ER_FILE_READ 3
+#define ER_FILE_WRITE 4
+
+#define ER_MEM_ALLOC 100
+
struct service {
unsigned int version;
unsigned int index;
@@ -41,7 +48,7 @@ int srv_close (struct service *srv);
int srv_read_cb (struct process *p, char ** buf, size_t * msize
, int (*cb)(FILE *f, char ** buf, size_t * msize));
int srv_read (struct process *, char ** buf, size_t *);
-int srv_write (struct process *, void * buf, size_t);
+int srv_write (struct process *, char * buf, size_t);
// APPLICATION
@@ -53,7 +60,13 @@ int app_destroy (struct process *); // called by the application
int app_read_cb (struct process *p, char ** buf, size_t * msize
, int (*cb)(FILE *f, char ** buf, size_t * msize));
-int app_read (struct process *, void * buf, size_t *);
-int app_write (struct process *, void * buf, size_t);
+int app_read (struct process *, char ** buf, size_t *);
+int app_write (struct process *, char * buf, size_t);
+
+// wrappers
+int file_open (FILE **f, const char *path, const char *mode);
+int file_close (FILE *f);
+int file_read (FILE *f, char **buf, size_t *msize);
+int file_write (FILE *f, const char *buf, size_t msize);
#endif
diff --git a/lib/pubsubd.c b/lib/pubsubd.c
index 712b295..f61cdbe 100644
--- a/lib/pubsubd.c
+++ b/lib/pubsubd.c
@@ -1,6 +1,8 @@
#include "pubsubd.h"
#include
+#include // strndup
+
// CHANNELS
void pubsubd_channels_init (struct channels *chans) { LIST_INIT(chans); }
@@ -57,7 +59,9 @@ struct channel * pubsubd_channel_copy (struct channel *c)
memcpy (copy, c, sizeof(struct channel));
if (c->chan != NULL) {
- copy->chan = strndup (c->chan, c->chanlen);
+ // copy->chan = strndup (c->chan, c->chanlen);
+ copy->chan = malloc (BUFSIZ);
+ memcpy (copy->chan, c->chan, BUFSIZ);
copy->chanlen = c->chanlen;
}
@@ -396,7 +400,9 @@ int pubsubd_get_new_process (struct service *srv, struct app_list_elm *ale
}
chan[BUFSIZ -1] = '\0';
- c[0]->chan = strndup (chan, BUFSIZ);
+ // c[0]->chan = strndup (chan, BUFSIZ);
+ c[0]->chan = malloc (BUFSIZ);
+ memcpy(c[0]->chan, chan, BUFSIZ);
c[0]->chanlen = strlen (chan);
struct channel *new_chan = NULL;
@@ -425,21 +431,35 @@ int pubsubd_msg_read_cb (FILE *f, char ** buf, size_t * msize)
// read
char type = ' ';
- fread (&type, 1, 1, f);
+ if (0 == fread (&type, 1, 1, f)) {
+ return ER_FILE_READ;
+ }
size_t chanlen = 0;
- fread (&chanlen, sizeof (size_t), 1, f);
+ if (0 == fread (&chanlen, sizeof (size_t), 1, f)) {
+ return ER_FILE_READ;
+ }
if (chanlen > BUFSIZ) {
- return 1;
+ return ER_FILE_READ;
}
char *chan = NULL;
chan = malloc (chanlen);
- fread (chan, chanlen, 1, f);
+
+ if (chan == NULL) {
+ return ER_MEM_ALLOC;
+ }
+
+ if (0 == fread (chan, chanlen, 1, f)) {
+ return ER_FILE_READ;
+ }
size_t datalen = 0;
- fread (&datalen, sizeof (size_t), 1, f);
+ if (0 == fread (&datalen, sizeof (size_t), 1, f)) {
+ free (chan);
+ return ER_FILE_READ;
+ }
if (datalen > BUFSIZ) {
return 1;
@@ -447,11 +467,25 @@ int pubsubd_msg_read_cb (FILE *f, char ** buf, size_t * msize)
char *data = NULL;
data = malloc (datalen);
- fread (data, datalen, 1, f);
+ if (data == NULL) {
+ free (chan);
+ return ER_MEM_ALLOC;
+ }
+
+ if (0 == fread (data, datalen, 1, f)) {
+ free (chan);
+ free (data);
+ return ER_FILE_READ;
+ }
*msize = 1 + 2 * sizeof (size_t) + chanlen + datalen;
if (*buf == NULL) {
*buf = malloc(*msize);
+ if (*buf == NULL) {
+ free (chan);
+ free (data);
+ return ER_MEM_ALLOC;
+ }
}
// TODO CHECK THIS
diff --git a/lib/pubsubd.h b/lib/pubsubd.h
index 559026c..b7036cf 100644
--- a/lib/pubsubd.h
+++ b/lib/pubsubd.h
@@ -3,7 +3,6 @@
#include "communication.h"
#include "process.h"
-
#include "queue.h"
#define PUBSUB_TYPE_DISCONNECT 1 << 0