pollfd
Karchnu 2020-06-28 01:13:48 +02:00
parent 91793c45cc
commit 05d483a712
3 changed files with 70 additions and 43 deletions

View File

@ -15,7 +15,8 @@
// print structures
#include "message.h"
struct ipc_error service_path (char *path, const char *sname)
struct ipc_error
service_path (char *path, const char *sname)
{
T_R ((path == NULL), IPC_ERROR_SERVICE_PATH__NO_PATH);
T_R ((sname == NULL), IPC_ERROR_SERVICE_PATH__NO_SERVICE_NAME);
@ -40,20 +41,19 @@ struct ipc_error ipc_ctx_init (struct ipc_ctx **ctx)
T_R ((*ctx == NULL), IPC_ERROR_CTX_INIT__MALLOC_CTX);
memset (ctx, 0, sizeof(struct ipc_ctx));
*ctx->pollfd = malloc(sizeof(struct pollfd));
T_R ((*ctx->pollfd == NULL), IPC_ERROR_CTX_INIT__MALLOC_POLLFD);
// (*ctx)->cinfos = malloc(sizeof(struct ));
// T_R (((*ctx)->pollfd == NULL), IPC_ERROR_CTX_INIT__MALLOC_POLLFD);
//
// (*ctx)->pollfd = malloc(sizeof(struct pollfd));
// T_R (((*ctx)->pollfd == NULL), IPC_ERROR_CTX_INIT__MALLOC_POLLFD);
IPC_RETURN_NO_ERROR;
}
struct ipc_error ipc_server_init (
char **env
, struct ipc_ctx **ctx
, struct ipc_connection_info *srv
, const char *sname)
struct ipc_error
ipc_server_init (char **env, struct ipc_ctx **ctx, const char *sname)
{
T_R ((env == NULL), IPC_ERROR_SERVER_INIT__NO_ENVIRONMENT_PARAM);
T_R ((srv == NULL), IPC_ERROR_SERVER_INIT__NO_SERVICE_PARAM);
T_R ((sname == NULL), IPC_ERROR_SERVER_INIT__NO_SERVER_NAME_PARAM);
ipc_ctx_init(ctx);
@ -70,6 +70,14 @@ struct ipc_error ipc_server_init (
}
#endif
struct ipc_connection_info *srv = NULL;
// Initialize the server IPC structure.
SECURE_BUFFER_HEAP_ALLOCATION_R (srv, sizeof (struct ipc_connection_info),,
IPC_ERROR_SERVER_INIT__NOT_ENOUGH_MEMORY);
srv->type = IPC_CONNECTION_TYPE_SERVER;
// gets the service path
SECURE_BUFFER_DECLARATION (char, buf, PATH_MAX);
TEST_IPC_RR (service_path (buf, sname), "cannot get server path");
@ -88,6 +96,9 @@ struct ipc_error ipc_server_init (
TEST_IPC_RETURN_ON_ERROR (usock_init (&srv->pollfd.fd, srv->spath));
// Add the server to the listened file descriptors.
TEST_IPC_RR (ipc_add (ctx, srv), "cannot add the server in the context");
IPC_RETURN_NO_ERROR;
}
@ -120,11 +131,10 @@ struct ipc_error ipc_contact_networkd (struct ipc_connection_info *srv, const ch
srv->pollfd.fd = 0;
IPC_RETURN_NO_ERROR;
}
// printf ("(;)sname %s found\n", sname);
// gets the service path
// Get the service path.
SECURE_BUFFER_DECLARATION (char, buf, PATH_MAX);
TEST_IPC_RR (service_path (buf, "network", 0, 0), "cannot get network service path");
TEST_IPC_RR (service_path (buf, "network"), "cannot get network service path");
int networkdfd = 0;
@ -150,7 +160,7 @@ struct ipc_error ipc_contact_networkd (struct ipc_connection_info *srv, const ch
return ret;
}
// Create context, contact networkd
// Create context, contact networkd, connects to the service.
struct ipc_error ipc_connection (char **env, struct ipc_ctx **ctx, const char *sname)
{
T_R ((env == NULL), IPC_ERROR_CONNECTION__NO_ENVIRONMENT_PARAM);
@ -159,6 +169,15 @@ struct ipc_error ipc_connection (char **env, struct ipc_ctx **ctx, const char *s
ipc_ctx_init(ctx);
struct ipc_connection_info *srv = NULL;
SECURE_BUFFER_HEAP_ALLOCATION_R (srv, sizeof (struct ipc_connection_info),,
IPC_ERROR_CONNECTION__NOT_ENOUGH_MEMORY);
srv->type = IPC_CONNECTION_TYPE_IPC;
// Add the server to the listened file descriptors.
TEST_IPC_RR (ipc_add (ctx, srv), "cannot add the server in the context");
TEST_IPC_P (ipc_contact_networkd (srv, sname), "error during networkd connection");
// if networkd did not initiate the connection
@ -195,7 +214,7 @@ struct ipc_error ipc_accept (
T_R ((srv == NULL), IPC_ERROR_ACCEPT__NO_SERVICE_PARAM);
T_R ((p == NULL), IPC_ERROR_ACCEPT__NO_CLIENT_PARAM);
TEST_IPC_RR (usock_accept (srv->pollfd.fd, p->pollfd.fd), "cannot accept IPC connection");
TEST_IPC_RR (usock_accept (srv->pollfd.fd, &p->pollfd.fd), "cannot accept IPC connection");
p->pollfd.events = POLLIN; // Tell to poll(2) to watch for incoming data from this fd.
p->type = IPC_CONNECTION_TYPE_IPC;
@ -250,7 +269,7 @@ handle_connection (struct ipc_event *event
, struct ipc_ctx *cinfos
, struct ipc_connection_info *cinfo)
{
T_R ((cinfo == NULL), IPC_ERROR_HANDLE_NEW_CONNECTION__NO_CINFO_PARAM);
T_R ((cinfo == NULL), IPC_ERROR_HANDLE_NEW_CONNECTION__NO_CINFO_PARAM);
T_R ((cinfos == NULL), IPC_ERROR_HANDLE_NEW_CONNECTION__NO_CINFOS_PARAM);
struct ipc_connection_info *new_client = NULL;
@ -607,7 +626,7 @@ struct ipc_error ipc_add (
, struct ipc_connection_info *p)
{
T_R ((cinfos == NULL), IPC_ERROR_ADD__NO_PARAM_CLIENTS);
T_R ((p == NULL), IPC_ERROR_ADD__NO_PARAM_CLIENT);
T_R ((p == NULL), IPC_ERROR_ADD__NO_PARAM_CLIENT);
cinfos->size++;
// In case this is the first allocation.
@ -626,7 +645,7 @@ struct ipc_error ipc_add (
T_R ((cinfos->cinfos == NULL), IPC_ERROR_ADD__EMPTY_LIST);
cinfos->cinfos[cinfos->size - 1] = p;
cinfos->pollfd[cinfos->size - 1] = p->pollfd;
cinfos->pollfd[cinfos->size - 1] = &p->pollfd;
IPC_RETURN_NO_ERROR;
}
@ -699,8 +718,7 @@ struct ipc_error ipc_add_fd (struct ipc_ctx *cinfos, int fd)
SECURE_BUFFER_HEAP_ALLOCATION_R (cinfo, sizeof (struct ipc_connection_info),,
IPC_ERROR_ADD_FD__NOT_ENOUGH_MEMORY);
cinfo->type = type;
ipc_connection_gen (cinfo, fd, IPC_CONNECTION_TYPE_EXTERNAL);
cinfo->type = IPC_CONNECTION_TYPE_EXTERNAL;
return ipc_add (cinfos, cinfo);
}
@ -711,20 +729,26 @@ struct ipc_error ipc_del_fd (struct ipc_ctx *cinfos, int fd)
T_R ((cinfos == NULL), IPC_ERROR_DEL_FD__NO_PARAM_CINFOS);
T_R ((cinfos->cinfos == NULL), IPC_ERROR_DEL_FD__EMPTY_LIST);
size_t i;
for (i = 0; i < cinfos->size; i++) {
for (size_t i = 0; i < cinfos->size; i++) {
if (cinfos->cinfos[i]->pollfd.fd == fd) {
cinfos->cinfos[i]->pollfd.fd = -1;
free (cinfos->cinfos[i]);
cinfos->pollfd[i] = NULL;
cinfos->size--;
if (cinfos->size == 0) {
// free cinfos->cinfos
ipc_connections_free (cinfos);
} else {
cinfos->cinfos[i] = cinfos->cinfos[cinfos->size];
cinfos->cinfos = realloc (cinfos->cinfos, sizeof (struct ipc_connection_info) * cinfos->size);
cinfos->pollfd[i] = cinfos->pollfd[cinfos->size];
if (cinfos->cinfos == NULL) {
cinfos->cinfos = realloc (cinfos->cinfos, sizeof (struct ipc_connection_info) * cinfos->size);
cinfos->pollfd = realloc (cinfos->pollfd, sizeof (struct pollfd ) * cinfos->size);
if (cinfos->cinfos == NULL || cinfos->pollfd == NULL) {
IPC_RETURN_ERROR (IPC_ERROR_DEL_FD__EMPTIED_LIST);
}
}

View File

@ -12,22 +12,15 @@ static struct ipc_errors_verbose ipc_errors_verbose[] = {
{IPC_ERROR_NONE, "no error"}
, {IPC_ERROR_CLOSED_RECIPIENT, "closed recipient"}
, {IPC_ERROR_SERVER_INIT__NO_DIR_CANNOT_CREATE_IT,
"ipc_server_init: no directory for ipc and cannot to create it"}
, {IPC_ERROR_SERVER_INIT__NON_WRITABLE_DIR, "ipc_server_init: non writable directory for ipc"}
, {IPC_ERROR_SERVER_INIT__NO_ENVIRONMENT_PARAM, "ipc_server_init: no environment param"}
, {IPC_ERROR_SERVER_INIT__NO_SERVICE_PARAM , "ipc_server_init: no service param"}
, {IPC_ERROR_SERVER_INIT__NO_SERVER_NAME_PARAM, "ipc_server_init: no server name param"}
, {IPC_ERROR_SERVER_INIT__MALLOC , "ipc_server_init: error on malloc function"}
, {IPC_ERROR_CONNECTION__NO_SERVER, "ipc_connection: no server parameter"}
, {IPC_ERROR_CONNECTION__NO_SERVICE_NAME, "ipc_connection: no service name parameter"}
, {IPC_ERROR_CONNECTION__NO_ENVIRONMENT_PARAM, "ipc_connection: no environment param"}
, {IPC_ERROR_USOCK_CONNECT__CONNECT, "ipc_connection: error on the connect function"}
, {IPC_ERROR_CONNECTION_GEN__NO_CINFO, "ipc_connection_gen: no cinfo"}
, {IPC_ERROR_ACCEPT__NO_SERVICE_PARAM, "ipc_accept: no service param"}
, {IPC_ERROR_ACCEPT__NO_CLIENT_PARAM , "ipc_accept: no client param"}

View File

@ -88,8 +88,6 @@ enum ipc_event_type {
*/
enum ipc_error_code {
IPC_ERROR_NONE = 0
, IPC_ERROR_SERVER_INIT__NON_WRITABLE_DIR = 1
, IPC_ERROR_SERVER_INIT__NO_DIR_CANNOT_CREATE_IT = 2
, IPC_ERROR_HANDLE_MESSAGE__NOT_ENOUGH_MEMORY = 3
, IPC_ERROR_CLOSED_RECIPIENT = 4
, IPC_ERROR_SERVICE_PATH__NO_PATH = 5
@ -101,10 +99,8 @@ enum ipc_error_code {
, IPC_ERROR_WRITE__NO_MESSAGE_PARAM = 11
, IPC_ERROR_WRITE__NOT_ENOUGH_DATA = 12
, IPC_ERROR_READ__NO_MESSAGE_PARAM = 13
, IPC_ERROR_CONNECTION__NO_SERVER = 14
, IPC_ERROR_CONNECTION__NO_SERVICE_NAME = 15
, IPC_ERROR_CONNECTION__NO_ENVIRONMENT_PARAM = 16
, IPC_ERROR_CONNECTION_GEN__NO_CINFO = 17
, IPC_ERROR_ACCEPT__NO_SERVICE_PARAM = 18
, IPC_ERROR_ACCEPT__NO_CLIENT_PARAM = 19
, IPC_ERROR_HANDLE_NEW_CONNECTION__NO_CINFO_PARAM = 20
@ -173,6 +169,19 @@ enum ipc_error_code {
, IPC_ERROR_DIR_SETUP__NOT_A_DIRECTORY = 83
, IPC_ERROR_DIR_SETUP__DIRECTORY_NOT_WRITABLE = 84
, IPC_ERROR_DIRECTORY_SETUP__PATH_PARAM = 85
, IPC_ERROR_SERVER_INIT__NOT_ENOUGH_MEMORY = 86
, IPC_ERROR_CONNECTION__NOT_ENOUGH_MEMORY = 87
, IPC_ERROR_CTX_INIT__NO_CONTEXT_PARAM = 88
, IPC_ERROR_CTX_INIT__CONTEXT_ALREADY_INIT = 89
, IPC_ERROR_ADD__MALLOC_POLLFD = 90
, IPC_ERROR_ADD_MESSAGE_TO_SEND__EMPTY_LIST = 91
, IPC_ERROR_ADD_MESSAGE_TO_SEND__MALLOC = 92
, IPC_ERROR_ADD_MESSAGE_TO_SEND__NO_PARAM_MESSAGE = 93
, IPC_ERROR_ADD_MESSAGE_TO_SEND__NO_PARAM_MESSAGES = 94
, IPC_ERROR_CONNECTION__NO_CTX = 95
, IPC_ERROR_CTX_INIT__MALLOC_CTX = 96
, IPC_ERROR_CTX_INIT__MALLOC_POLLFD = 97
};
struct ipc_error {
@ -242,6 +251,7 @@ struct ipc_event {
enum ipc_connection_types {
IPC_CONNECTION_TYPE_IPC = 0
, IPC_CONNECTION_TYPE_EXTERNAL = 1
, IPC_CONNECTION_TYPE_SERVER = 2 /** Messages received = new connections. */
};
#define IPC_EVENT_CLEAN(pevent) {\
@ -257,8 +267,12 @@ enum ipc_connection_types {
* main public functions
**/
struct ipc_error ipc_server_init (char **env, struct ipc_connection_info *srv, const char *sname);
struct ipc_error ipc_connection (char **env, struct ipc_connection_info *srv, const char *sname);
struct ipc_error
ipc_server_init (
char **env
, struct ipc_ctx **ctx
, const char *sname);
struct ipc_error ipc_connection (char **env, struct ipc_ctx **ctx, const char *sname);
struct ipc_error ipc_server_close (struct ipc_connection_info *srv);
struct ipc_error ipc_close (struct ipc_connection_info *p);
@ -278,11 +292,6 @@ struct ipc_error ipc_add_fd (struct ipc_ctx *cinfos, int fd);
struct ipc_error ipc_del_fd (struct ipc_ctx *cinfos, int fd);
void ipc_connections_free (struct ipc_ctx *);
// create the client service structure
struct ipc_error ipc_connection_gen (struct ipc_connection_info *cinfo
, int fd, char type);
void ipc_connections_close (struct ipc_ctx *cinfos);
/***
@ -345,9 +354,10 @@ struct networkd {
struct ipc_switchings *TCP_TO_IPC;
};
struct ipc_error ipc_wait_event_networkd (struct ipc_ctx *cinfos
struct ipc_error
ipc_wait_event_networkd (struct ipc_ctx *cinfos
, struct ipc_connection_info *cinfo // cinfo is NULL for clients
, struct ipc_event *event, struct ipc_switchings *switchdb, double *timer);
, struct ipc_event *event, struct ipc_switchings *switchdb, int *timer);
void ipc_switching_add (struct ipc_switchings *is, int orig, int dest);
int ipc_switching_del (struct ipc_switchings *is, int fd);