From 340ff653d61009e09f62d4b3d6f052652225af62 Mon Sep 17 00:00:00 2001
From: Philippe PITTOLI
Date: Mon, 8 Oct 2018 16:15:35 +0200
Subject: [PATCH] unix socket rewrite: done
---
core-test/app/array_proc.c | 6 ++--
core/client.c | 7 ++--
core/client.h | 7 ++--
core/communication.c | 66 ++++----------------------------------
core/communication.h | 2 +-
core/msg.c | 22 +++----------
core/msg.h | 8 ++---
core/usocket.c | 7 ----
core/usocket.h | 2 ++
pong/app/pong.c | 14 ++++----
pong/app/pongd.c | 50 +++++++++++++++++++++--------
pubsub/lib/channels.c | 4 +--
pubsub/lib/pubsubd.c | 2 +-
remote/lib/remoted.c | 2 +-
14 files changed, 75 insertions(+), 124 deletions(-)
diff --git a/core-test/app/array_proc.c b/core-test/app/array_proc.c
index 4ec83ff..7c6626d 100644
--- a/core-test/app/array_proc.c
+++ b/core-test/app/array_proc.c
@@ -20,15 +20,15 @@ int main()
}
}
- ipc_client_array_print(&clients);
+ ipc_clients_print(&clients);
ret = ipc_client_del(&clients, &client_tab[2]);
if(ret < 0) {
printf("erreur %d\n", ret );
}
- ipc_client_array_print(&clients);
+ ipc_clients_print(&clients);
- ipc_client_array_free (&clients);
+ ipc_clients_free (&clients);
return 0;
}
diff --git a/core/client.c b/core/client.c
index fb159f1..8dcd542 100644
--- a/core/client.c
+++ b/core/client.c
@@ -34,6 +34,7 @@ int ipc_client_add (struct ipc_clients *clients, struct ipc_client *p)
{
assert(clients != NULL);
assert(p != NULL);
+
clients->size++;
clients->clients = realloc(clients->clients
, sizeof(struct ipc_client) * clients->size);
@@ -62,7 +63,7 @@ int ipc_client_del (struct ipc_clients *clients, struct ipc_client *p)
clients->clients[i] = clients->clients[clients->size-1];
clients->size--;
if (clients->size == 0) {
- ipc_client_array_free (clients);
+ ipc_clients_free (clients);
}
else {
clients->clients = realloc(clients->clients
@@ -87,7 +88,7 @@ void client_print (struct ipc_client *p)
, p->proc_fd, p->index, p->version);
}
-void ipc_client_array_print (struct ipc_clients *ap)
+void ipc_clients_print (struct ipc_clients *ap)
{
int i;
for (i = 0; i < ap->size; i++) {
@@ -96,7 +97,7 @@ void ipc_client_array_print (struct ipc_clients *ap)
}
}
-void ipc_client_array_free (struct ipc_clients *ap)
+void ipc_clients_free (struct ipc_clients *ap)
{
if (ap->clients != NULL) {
free (ap->clients);
diff --git a/core/client.h b/core/client.h
index f5d3273..51cebf2 100644
--- a/core/client.h
+++ b/core/client.h
@@ -12,11 +12,12 @@ struct ipc_clients {
int size;
};
+// store and remove only pointers on allocated structures
int ipc_client_add (struct ipc_clients *, struct ipc_client *);
-int ipc_client_del (struct ipc_clients *clients, struct ipc_client *p);
+int ipc_client_del (struct ipc_clients *, struct ipc_client *);
-void ipc_client_array_print (struct ipc_clients *);
-void ipc_client_array_free (struct ipc_clients *);
+void ipc_clients_print (struct ipc_clients *);
+void ipc_clients_free (struct ipc_clients *);
struct ipc_client * ipc_server_client_copy (const struct ipc_client *p);
int ipc_server_client_eq (const struct ipc_client *p1, const struct ipc_client *p2);
diff --git a/core/communication.c b/core/communication.c
index 2ff42ff..20ed953 100644
--- a/core/communication.c
+++ b/core/communication.c
@@ -53,39 +53,20 @@ int ipc_server_accept (struct ipc_service *srv, struct ipc_client *p)
return -1;
}
-#if 0
- struct ipc_message m_con;
- memset (&m_con, 0, sizeof (struct ipc_message));
- ipc_server_read (p, &m_con);
- // TODO: handle the parameters in the first message
- ipc_message_free (&m_con);
-
- struct ipc_message m_ack;
- memset (&m_ack, 0, sizeof (struct ipc_message));
- ipc_message_format_ack (&m_ack, NULL, 0);
- ipc_server_write (p, &m_ack);
- ipc_message_free (&m_ack);
-#endif
-
return 0;
}
+// empty the srv structure
int ipc_server_close (struct ipc_service *srv)
{
usock_close (srv->service_fd);
- return usock_remove (srv->spath);
+ int ret = usock_remove (srv->spath);
+ ipc_service_empty (srv);
+ return ret;
}
int ipc_server_close_client (struct ipc_client *p)
{
- // struct ipc_message m_ack_dis;
- // memset (&m_ack_dis, 0, sizeof (struct ipc_message));
- // m_ack_dis.type = MSG_TYPE_CLOSE;
-
- // if (ipc_message_write (p->proc_fd, &m_ack_dis) < 0) {
- // handle_err ("server_close_client", "ipc_message_write < 0");
- // }
-
return usock_close (p->proc_fd);
}
@@ -100,8 +81,7 @@ int ipc_server_write (const struct ipc_client *p, const struct ipc_message *m)
}
int ipc_application_connection (int argc, char **argv, char **env
- , struct ipc_service *srv, const char *sname
- , const char *connectionstr, size_t msize)
+ , struct ipc_service *srv, const char *sname)
{
argc = argc;
argv = argv;
@@ -123,48 +103,14 @@ int ipc_application_connection (int argc, char **argv, char **env
return -1;
}
-#if 0
- // send connection string and receive acknowledgement
- struct ipc_message m_con;
- memset (&m_con, 0, sizeof (struct ipc_message));
-
- // format the connection msg
- if (ipc_message_format_con (&m_con, connectionstr, msize) < 0) {
- handle_err ("application_connection", "ipc_message_format_con");
- return -1;
- }
-
- // send connection msg
- ipc_application_write (srv, &m_con);
- ipc_message_free (&m_con);
-
- // receive ack msg
- struct ipc_message m_ack;
- memset (&m_ack, 0, sizeof (struct ipc_message));
- ipc_application_read (srv, &m_ack);
-
- assert (m_ack.type == MSG_TYPE_ACK);
- assert (m_ack.length == 0);
- ipc_message_free (&m_ack);
-#endif
-
return 0;
}
-#if 0
// send a CLOSE message then close the socket
int ipc_application_close (struct ipc_service *srv)
{
- struct ipc_message m;
- memset (&m, 0, sizeof (struct ipc_message));
- m.type = MSG_TYPE_CLOSE;
- if (ipc_message_write (srv->service_fd, &m) < 0) {
- handle_err ("application_close", "ipc_message_write < 0");
- }
-
return usock_close (srv->service_fd);
}
-#endif
int ipc_application_read (struct ipc_service *srv, struct ipc_message *m)
{
@@ -211,7 +157,7 @@ int ipc_server_select (struct ipc_clients *clients, struct ipc_service *srv
assert (active_clients != NULL);
// delete previous read active_clients array
- ipc_client_array_free (active_clients);
+ ipc_clients_free (active_clients);
int i, j;
/* master file descriptor list */
diff --git a/core/communication.h b/core/communication.h
index 24d8401..5aabd0e 100644
--- a/core/communication.h
+++ b/core/communication.h
@@ -37,7 +37,7 @@ int ipc_server_select (struct ipc_clients * clients, struct ipc_service *srv
// send the connection string to $TMP/
// fill srv->spath && srv->service_fd
int ipc_application_connection (int argc, char **argv, char **env
- , struct ipc_service *, const char *, const char *, size_t);
+ , struct ipc_service *, const char *);
int ipc_application_close (struct ipc_service *);
int ipc_application_read (struct ipc_service *srv, struct ipc_message *m);
diff --git a/core/msg.c b/core/msg.c
index ef3a135..59bac05 100644
--- a/core/msg.c
+++ b/core/msg.c
@@ -10,7 +10,7 @@ void ipc_message_print (const struct ipc_message *m)
printf ("msg: type %d len %d\n", m->type, m->length);
}
-int ipc_message_format_read (struct ipc_message *m, const char *buf, size_t msize)
+int ipc_message_format_read (struct ipc_message *m, const char *buf, ssize_t msize)
{
assert (m != NULL);
assert (buf != NULL);
@@ -37,7 +37,7 @@ int ipc_message_format_read (struct ipc_message *m, const char *buf, size_t msiz
return 0;
}
-int ipc_message_format_write (const struct ipc_message *m, char **buf, size_t *msize)
+int ipc_message_format_write (const struct ipc_message *m, char **buf, ssize_t *msize)
{
assert (m != NULL);
assert (buf != NULL);
@@ -79,7 +79,7 @@ int ipc_message_read (int fd, struct ipc_message *m)
assert (m != NULL);
char *buf = NULL;
- size_t msize = BUFSIZ;
+ ssize_t msize = BUFSIZ;
int ret = usock_recv (fd, &buf, &msize);
if (ret < 0) {
@@ -134,7 +134,7 @@ int ipc_message_write (int fd, const struct ipc_message *m)
// MSG FORMAT
-int ipc_message_format (struct ipc_message *m, char type, const char *payload, size_t length)
+int ipc_message_format (struct ipc_message *m, char type, const char *payload, ssize_t length)
{
assert (m != NULL);
assert (length + 3 <= BUFSIZ);
@@ -166,23 +166,11 @@ int ipc_message_format (struct ipc_message *m, char type, const char *payload, s
return 0;
}
-int ipc_message_format_data (struct ipc_message *m, const char *payload, size_t length)
+int ipc_message_format_data (struct ipc_message *m, const char *payload, ssize_t length)
{
return ipc_message_format (m, MSG_TYPE_DATA, payload, length);
}
-#if 0
-
-int ipc_message_format_con (struct ipc_message *m, const char *payload, size_t length)
-{
- return ipc_message_format (m, MSG_TYPE_CON, payload, length);
-}
-int ipc_message_format_ack (struct ipc_message *m, const char *payload, size_t length)
-{
- return ipc_message_format (m, MSG_TYPE_ACK, payload, length);
-}
-#endif
-
int ipc_message_format_server_close (struct ipc_message *m)
{
return ipc_message_format (m, MSG_TYPE_SERVER_CLOSE, NULL, 0);
diff --git a/core/msg.h b/core/msg.h
index d3c5574..48e306e 100644
--- a/core/msg.h
+++ b/core/msg.h
@@ -20,9 +20,9 @@ struct ipc_message {
};
// used to create msg structure from buffer
-int ipc_message_format_read (struct ipc_message *m, const char *buf, size_t msize);
+int ipc_message_format_read (struct ipc_message *m, const char *buf, ssize_t msize);
// used to create buffer from msg structure
-int ipc_message_format_write (const struct ipc_message *m, char **buf, size_t *msize);
+int ipc_message_format_write (const struct ipc_message *m, char **buf, ssize_t *msize);
// read a structure msg from fd
// 1 on a recipient socket close
@@ -30,9 +30,7 @@ int ipc_message_read (int fd, struct ipc_message *m);
// write a structure msg to fd
int ipc_message_write (int fd, const struct ipc_message *m);
-// int ipc_message_format_con (struct ipc_message *m, const char *payload, size_t length);
-int ipc_message_format_data (struct ipc_message *m, const char *payload, size_t length);
-// int ipc_message_format_ack (struct ipc_message *m, const char *payload, size_t length);
+int ipc_message_format_data (struct ipc_message *m, const char *payload, ssize_t length);
int ipc_message_format_server_close (struct ipc_message *m);
int ipc_message_empty (struct ipc_message *m);
diff --git a/core/usocket.c b/core/usocket.c
index d37ea95..876ec53 100644
--- a/core/usocket.c
+++ b/core/usocket.c
@@ -246,13 +246,6 @@ int usock_close (int fd)
{
int ret = 0;
- ret = fsync (fd);
- if (ret < 0) {
- handle_err ("usock_close", "fsync ret < 0");
- perror ("closing");
- return -1;
- }
-
ret = close (fd);
if (ret < 0) {
handle_err ("usock_close", "close ret < 0");
diff --git a/core/usocket.h b/core/usocket.h
index 7e8d09f..75bf4ee 100644
--- a/core/usocket.h
+++ b/core/usocket.h
@@ -48,4 +48,6 @@ int usock_accept (int fd, int *pfd);
// same as unlink(2)
int usock_remove (const char *path);
+static inline int ipc_service_empty (struct ipc_service *srv) { srv = srv; return 0 ;};
+
#endif
diff --git a/pong/app/pong.c b/pong/app/pong.c
index a416872..3ec134c 100644
--- a/pong/app/pong.c
+++ b/pong/app/pong.c
@@ -22,7 +22,7 @@ void non_interactive (int argc, char *argv[], char *env[])
srv.version = 0;
// init service
- if (ipc_application_connection (argc, argv, env, &srv, SERVICE_NAME, NULL, 0) < 0) {
+ if (ipc_application_connection (argc, argv, env, &srv, SERVICE_NAME) < 0) {
handle_err("main", "server_init < 0");
exit (EXIT_FAILURE);
}
@@ -35,7 +35,7 @@ void non_interactive (int argc, char *argv[], char *env[])
handle_err("main", "application_write < 0");
exit (EXIT_FAILURE);
}
- ipc_message_free (&m);
+ ipc_message_empty (&m);
if (ipc_application_read (&srv, &m) < 0) {
handle_err("main", "application_read < 0");
@@ -43,7 +43,7 @@ void non_interactive (int argc, char *argv[], char *env[])
}
printf ("msg recv: %s\n", m.payload);
- ipc_message_free (&m);
+ ipc_message_empty (&m);
if (ipc_application_close (&srv) < 0) {
handle_err("main", "application_close < 0");
@@ -69,7 +69,7 @@ void interactive (int argc, char *argv[], char *env[])
srv.version = 0;
// init service
- if (ipc_application_connection (argc, argv, env, &srv, SERVICE_NAME, NULL, 0) < 0) {
+ if (ipc_application_connection (argc, argv, env, &srv, SERVICE_NAME) < 0) {
handle_err ("main", "server_init < 0");
exit (EXIT_FAILURE);
}
@@ -96,7 +96,7 @@ void interactive (int argc, char *argv[], char *env[])
handle_err("main", "application_write < 0");
exit (EXIT_FAILURE);
}
- ipc_message_free (&m);
+ ipc_message_empty (&m);
if (ipc_application_read (&srv, &m) < 0) {
handle_err("main", "application_read < 0");
@@ -104,7 +104,7 @@ void interactive (int argc, char *argv[], char *env[])
}
printf ("msg recv: %s", m.payload);
- ipc_message_free (&m);
+ ipc_message_empty (&m);
}
if (ask_server_to_quit) {
@@ -114,7 +114,7 @@ void interactive (int argc, char *argv[], char *env[])
handle_err("main", "application_write < 0");
exit (EXIT_FAILURE);
}
- ipc_message_free (&m);
+ ipc_message_empty (&m);
} else if (ipc_application_close (&srv) < 0) {
handle_err("main", "application_close < 0");
exit (EXIT_FAILURE);
diff --git a/pong/app/pongd.c b/pong/app/pongd.c
index 6de090b..9262800 100644
--- a/pong/app/pongd.c
+++ b/pong/app/pongd.c
@@ -39,37 +39,63 @@ void handle_new_msg (struct ipc_clients *clients, struct ipc_clients *clients_ta
int i = 0;
for (i = 0; i < clients_talking->size; i++) {
// printf ("loop handle_new_msg\n");
- ret = ipc_server_read (clients_talking->clients[i], &m);
+
+ // current talking client
+ struct ipc_client *pc = clients_talking->clients[i];
+
+ ret = ipc_server_read (pc, &m);
if (ret < 0) {
handle_error("server_read < 0");
}
- // close the client then delete it from the client array
+ // close the client then delete it from clients
if (ret == 1) {
cpt--;
printf ("disconnection => %d client(s) remaining\n", cpt);
- if (ipc_server_close_client (clients_talking->clients[i]) < 0)
+ if (ipc_server_close_client (pc) < 0)
handle_err( "handle_new_msg", "server_close_client < 0");
- if (ipc_client_del (clients, clients_talking->clients[i]) < 0)
+ if (ipc_client_del (clients, pc) < 0)
handle_err( "handle_new_msg", "ipc_client_del < 0");
- if (ipc_client_del (clients_talking, clients_talking->clients[i]) < 0)
+ if (ipc_client_del (clients_talking, pc) < 0)
handle_err( "handle_new_msg", "ipc_client_del < 0");
i--;
+
+ // free the ipc_client structure
+ free (pc);
continue;
}
if (m.type == MSG_TYPE_SERVER_CLOSE) {
+ // free remaining clients
+ for (int y = 0; y < clients->size ; y++) {
+ struct ipc_client *cli = clients->clients[y];
+ // TODO: replace with specific ipc_client_empty function
+ if (cli != NULL)
+ free (cli);
+ clients->clients[y] = NULL;
+ }
+
+ ipc_clients_free (clients);
+ ipc_clients_free (clients_talking);
+
if (ipc_server_close (srv) < 0) {
handle_error("server_close < 0");
}
+
+ ipc_message_empty (&m);
+ free (srv);
exit (0);
}
printf ("new message : %s", m.payload);
- if (ipc_server_write (clients_talking->clients[i], &m) < 0) {
+ if (ipc_server_write (pc, &m) < 0) {
handle_err( "handle_new_msg", "server_write < 0");
}
+
+ // empty the message structure
+ ipc_message_empty (&m);
+ memset (&m, 0, sizeof m);
}
}
@@ -83,7 +109,7 @@ void handle_new_msg (struct ipc_clients *clients, struct ipc_clients *clients_ta
void main_loop ()
{
- int i, ret = 0;
+ int ret = 0;
struct ipc_clients clients;
memset(&clients, 0, sizeof(struct ipc_clients));
@@ -105,14 +131,10 @@ void main_loop ()
if (clients_talking.size > 0) {
handle_new_msg (&clients, &clients_talking);
}
- ipc_client_array_free (&clients_talking);
- }
-
- for (i = 0; i < clients.size; i++) {
- if (ipc_server_close_client (clients.clients[i]) < 0) {
- handle_error( "server_close_client < 0");
- }
+ ipc_clients_free (&clients_talking);
}
+ // should never go there
+ exit (1);
}
diff --git a/pubsub/lib/channels.c b/pubsub/lib/channels.c
index c8e9168..4caf7fb 100644
--- a/pubsub/lib/channels.c
+++ b/pubsub/lib/channels.c
@@ -19,7 +19,7 @@ void pubsubd_channel_print (const struct channel *chan)
handle_err ("pubsubd_channel_print", "chan->subs == NULL");
}
else {
- ipc_client_array_print (chan->subs);
+ ipc_clients_print (chan->subs);
}
}
@@ -115,7 +115,7 @@ void pubsubd_channel_free (struct channel * c)
}
if (c->subs != NULL) {
- ipc_client_array_free (c->subs);
+ ipc_clients_free (c->subs);
free (c->subs);
}
}
diff --git a/pubsub/lib/pubsubd.c b/pubsub/lib/pubsubd.c
index 9f6f491..ef8ec84 100644
--- a/pubsub/lib/pubsubd.c
+++ b/pubsub/lib/pubsubd.c
@@ -186,7 +186,7 @@ void pubsubd_main_loop (struct ipc_service *srv, struct channels *chans)
handle_new_connection (srv, &ap);
handle_new_msg (chans, &ap, &proc_to_read);
}
- ipc_client_array_free (&proc_to_read);
+ ipc_clients_free (&proc_to_read);
}
for (i = 0; i < ap.size; i++) {
diff --git a/remote/lib/remoted.c b/remote/lib/remoted.c
index 707f6e0..61c2e67 100644
--- a/remote/lib/remoted.c
+++ b/remote/lib/remoted.c
@@ -132,7 +132,7 @@ void remoted_main_loop (struct ipc_service *srv, struct remoted_ctx *ctx)
handle_new_connection (srv, &ap);
handle_new_msg (&ap, &proc_to_read);
}
- ipc_client_array_free (&proc_to_read);
+ ipc_clients_free (&proc_to_read);
}
for (i = 0; i < ap.size; i++) {