11#include "module.h"
22#include "poll_priv.h"
3- #include <string.h>
4- #include <stdarg.h>
53
64static module_ret_code init_ctx (const char * ctx_name , m_context * * context );
75static void destroy_ctx (const char * ctx_name , m_context * context );
86static m_context * check_ctx (const char * ctx_name );
7+ static int _pipe (module * mod );
98static module_ret_code init_pubsub_fd (module * mod );
109static void default_logger (const self_t * self , const char * fmt , va_list args , const void * userdata );
1110static int tell_if (void * data , void * m );
@@ -51,16 +50,30 @@ static void destroy_ctx(const char *ctx_name, m_context *context) {
5150}
5251
5352static m_context * check_ctx (const char * ctx_name ) {
54- m_context * context = NULL ;
55- map_get (ctx , ctx_name , (void * * )& context ); \
53+ m_context * context = map_get (ctx , ctx_name );
5654 if (!context ) {
5755 init_ctx (ctx_name , & context );
5856 }
5957 return context ;
6058}
6159
60+ static int _pipe (module * mod ) {
61+ int ret = pipe (mod -> pubsub_fd );
62+ if (ret == 0 ) {
63+ for (int i = 0 ; i < 2 ; i ++ ) {
64+ int flags = fcntl (mod -> pubsub_fd [i ], F_GETFL , 0 );
65+ if (flags == -1 ) {
66+ flags = 0 ;
67+ }
68+ fcntl (mod -> pubsub_fd [i ], F_SETFL , flags | O_NONBLOCK );
69+ fcntl (mod -> pubsub_fd [i ], F_SETFD , FD_CLOEXEC );
70+ }
71+ }
72+ return ret ;
73+ }
74+
6275static module_ret_code init_pubsub_fd (module * mod ) {
63- if (_pipe (mod -> pubsub_fd ) == 0 ) {
76+ if (_pipe (mod ) == 0 ) {
6477 if (module_register_fd (& mod -> self , mod -> pubsub_fd [0 ], true, NULL ) == MOD_OK ) {
6578 return MOD_OK ;
6679 }
@@ -82,8 +95,7 @@ module_ret_code module_register(const char *name, const char *ctx_name, const se
8295 m_context * context = check_ctx (ctx_name );
8396 MOD_ASSERT (context , "Failed to create context." , MOD_ERR );
8497
85- module * mod = NULL ;
86- map_get (context -> modules , name , (void * * )& mod );
98+ module * mod = map_get (context -> modules , name );
8799 MOD_ASSERT (!mod , "Module with same name already registered in context." , MOD_ERR );
88100
89101 MODULE_DEBUG ("Registering module '%s'.\n" , name );
@@ -277,9 +289,8 @@ char *mem_strdup(const char *s) {
277289module_ret_code module_register_topic (const self_t * self , const char * topic ) {
278290 MOD_ASSERT (topic , "NULL topic." , MOD_ERR );
279291 GET_MOD (self );
280- void * tmp = NULL ;
281292
282- if (map_get (c -> topics , topic , ( void * * ) & tmp ) == MAP_MISSING ) {
293+ if (! map_has_key (c -> topics , topic ) ) {
283294 if (map_put (c -> topics , topic , mod , true) == MAP_OK ) {
284295 tell_system_pubsub_msg (c , TOPIC_REGISTERED , topic );
285296 return MOD_OK ;
@@ -291,10 +302,10 @@ module_ret_code module_register_topic(const self_t *self, const char *topic) {
291302module_ret_code module_deregister_topic (const self_t * self , const char * topic ) {
292303 MOD_ASSERT (topic , "NULL topic." , MOD_ERR );
293304 GET_MOD (self );
294- void * tmp = NULL ;
305+ void * tmp = map_get ( c -> topics , topic ); // NULL if key is not present
295306
296307 /* Only same mod which registered topic can deregister it */
297- if (map_get ( c -> topics , topic , ( void * * ) & tmp ) == MAP_OK && tmp == mod ) {
308+ if (tmp == mod ) {
298309 if (map_remove (c -> topics , topic ) == MAP_OK ) {
299310 tell_system_pubsub_msg (c , TOPIC_DEREGISTERED , topic );
300311 return MOD_OK ;
@@ -306,11 +317,9 @@ module_ret_code module_deregister_topic(const self_t *self, const char *topic) {
306317module_ret_code module_subscribe (const self_t * self , const char * topic ) {
307318 MOD_ASSERT (topic , "NULL topic." , MOD_ERR );
308319 GET_MOD (self );
309- void * tmp = NULL ;
310320
311321 /* If topic exists and we are not already subscribed */
312- if (map_get (c -> topics , topic , (void * * )& tmp ) == MAP_OK &&
313- map_get (mod -> subscriptions , topic , (void * * )& tmp ) == MAP_MISSING ) {
322+ if (map_has_key (c -> topics , topic ) && !map_has_key (mod -> subscriptions , topic )) {
314323 /* Store pointer to mod as value, even if it will be unused; this should be a hashset */
315324 if (map_put (mod -> subscriptions , topic , mod , true) == MAP_OK ) {
316325 return MOD_OK ;
@@ -322,10 +331,8 @@ module_ret_code module_subscribe(const self_t *self, const char *topic) {
322331module_ret_code module_unsubscribe (const self_t * self , const char * topic ) {
323332 MOD_ASSERT (topic , "NULL topic." , MOD_ERR );
324333 GET_MOD (self );
325- void * tmp = NULL ;
326334
327- if (map_get (c -> topics , topic , (void * * )& tmp ) == MAP_OK &&
328- map_remove (mod -> subscriptions , topic ) == MAP_OK ) {
335+ if (map_remove (mod -> subscriptions , topic ) == MAP_OK ) {
329336 return MOD_OK ;
330337 }
331338 return MOD_ERR ;
@@ -358,15 +365,14 @@ int flush_pubsub_msg(void *data, void *m) {
358365static int tell_if (void * data , void * m ) {
359366 module * mod = (module * )m ;
360367 const pubsub_msg_t * msg = (pubsub_msg_t * )data ;
361- void * tmp = NULL ;
362368
363369 /*
364370 * Only if mod is actually running or paused and
365371 * it is a SYSTEM message or
366372 * topic is null or this module is subscribed to topic
367373 */
368374 if (module_is (& mod -> self , RUNNING | PAUSED ) && (msg -> type != USER || !msg -> topic ||
369- map_get (mod -> subscriptions , msg -> topic , ( void * * ) & tmp ) == MAP_OK )) {
375+ map_has_key (mod -> subscriptions , msg -> topic ) )) {
370376
371377 MODULE_DEBUG ("Telling a message to %s.\n" , mod -> self .name );
372378
@@ -433,7 +439,7 @@ static module_ret_code publish_msg(const self_t *self, const char *topic,
433439 * Only module that registered a topic can publish on the topic.
434440 * Moreover, a publish can only be made on existent topic.
435441 */
436- if (!topic || (map_get (c -> topics , topic , ( void * * ) & tmp ) == MAP_OK && tmp == mod )) {
442+ if (!topic || (( tmp = map_get (c -> topics , topic )) && tmp == mod )) {
437443 pubsub_msg_t m = { .topic = topic , .message = message , .sender = self , .type = USER , .size = size };
438444 return tell_pubsub_msg (& m , NULL , c );
439445 }
0 commit comments