diff --git a/src/bridge.c b/src/bridge.c index f1d08900..cc6a8139 100644 --- a/src/bridge.c +++ b/src/bridge.c @@ -55,6 +55,19 @@ Contributors: static void bridge__backoff_step(struct mosquitto *context); static void bridge__backoff_reset(struct mosquitto *context); +void bridge__start_all(struct mosquitto_db *db) +{ + int i; + + for(i=0; iconfig->bridge_count; i++){ + if(bridge__new(db, &(db->config->bridges[i]))){ + log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Unable to connect to bridge %s.", + db->config->bridges[i].name); + } + } +} + + int bridge__new(struct mosquitto_db *db, struct mosquitto__bridge *bridge) { struct mosquitto *new_context = NULL; diff --git a/src/mosquitto.c b/src/mosquitto.c index 2429d426..bb9a2b0d 100644 --- a/src/mosquitto.c +++ b/src/mosquitto.c @@ -206,14 +206,129 @@ void mosquitto__daemonise(void) } +int listeners__start(struct mosquitto_db *db, mosq_sock_t **listensock, int *listensock_count) +{ + int i, j; + int listensock_index = 0; + + listensock_index = 0; + (*listensock_count) = 0; + for(i=0; iconfig->listener_count; i++){ + if(db->config->listeners[i].protocol == mp_mqtt){ + if(net__socket_listen(&db->config->listeners[i])){ + db__close(db); + if(db->config->pid_file){ + remove(db->config->pid_file); + } + return 1; + } + (*listensock_count) += db->config->listeners[i].sock_count; + *listensock = mosquitto__realloc(*listensock, sizeof(mosq_sock_t)*(*listensock_count)); + if(!listensock){ + db__close(db); + if(db->config->pid_file){ + remove(db->config->pid_file); + } + return 1; + } + for(j=0; jconfig->listeners[i].sock_count; j++){ + if(db->config->listeners[i].socks[j] == INVALID_SOCKET){ + db__close(db); + if(db->config->pid_file){ + remove(db->config->pid_file); + } + return 1; + } + (*listensock)[listensock_index] = db->config->listeners[i].socks[j]; + listensock_index++; + } + }else if(db->config->listeners[i].protocol == mp_websockets){ +#ifdef WITH_WEBSOCKETS + db->config->listeners[i].ws_context = mosq_websockets_init(&db->config->listeners[i], db->config); + if(!db->config->listeners[i].ws_context){ + log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to create websockets listener on port %d.", db->config->listeners[i].port); + return 1; + } +#endif + } + } + if((*listensock) == NULL){ + log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to start any listening sockets, exiting."); + return 1; + } + return MOSQ_ERR_SUCCESS; +} + + +void listeners__stop(struct mosquitto_db *db, mosq_sock_t *listensock, int listensock_count) +{ + int i; + + for(i=0; iconfig->listener_count; i++){ +#ifdef WITH_WEBSOCKETS + if(db->config->listeners[i].ws_context){ + libwebsocket_context_destroy(db->config->listeners[i].ws_context); + } + mosquitto__free(db->config->listeners[i].ws_protocol); +#endif +#ifdef WITH_UNIX_SOCKETS + if(db->config->listeners[i].unix_socket_path != NULL){ + unlink(db->config->listeners[i].unix_socket_path); + } +#endif + } + + for(i=0; iconfig->daemon && db->config->pid_file){ + pid = mosquitto__fopen(db->config->pid_file, "wt", false); + if(pid){ + fprintf(pid, "%d", getpid()); + fclose(pid); + }else{ + log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to write pid file."); + return 1; + } + } + return MOSQ_ERR_SUCCESS; +} + + int main(int argc, char *argv[]) { mosq_sock_t *listensock = NULL; int listensock_count = 0; - int listensock_index = 0; struct mosquitto__config config; - int i, j; - FILE *pid; + int i; int rc; #ifdef WIN32 SYSTEMTIME st; @@ -263,16 +378,7 @@ int main(int argc, char *argv[]) mosquitto__daemonise(); } - if(config.daemon && config.pid_file){ - pid = mosquitto__fopen(config.pid_file, "wt", false); - if(pid){ - fprintf(pid, "%d", getpid()); - fclose(pid); - }else{ - log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to write pid file."); - return 1; - } - } + if(pid__write(&int_db)) return 1; rc = db__open(&config, &int_db); if(rc != MOSQ_ERR_SUCCESS){ @@ -302,75 +408,15 @@ int main(int argc, char *argv[]) sys_tree__init(&int_db); #endif - listensock_index = 0; - for(i=0; ilistener_count; i++){ -#ifdef WITH_WEBSOCKETS - if(int_db.config->listeners[i].ws_context){ - libwebsocket_context_destroy(int_db.config->listeners[i].ws_context); - } - mosquitto__free(int_db.config->listeners[i].ws_protocol); -#endif -#ifdef WITH_UNIX_SOCKETS - if(int_db.config->listeners[i].unix_socket_path != NULL){ - unlink(int_db.config->listeners[i].unix_socket_path); - } -#endif - } - /* FIXME - this isn't quite right, all wills with will delay zero should be * sent now, but those with positive will delay should be persisted and * restored, pending the client reconnecting in time. */ @@ -405,9 +437,7 @@ int main(int argc, char *argv[]) will_delay__send_all(&int_db); #ifdef WITH_PERSISTENCE - if(config.persistence){ - persist__backup(&int_db, true); - } + persist__backup(&int_db, true); #endif session_expiry__remove_all(&int_db); @@ -435,16 +465,7 @@ int main(int argc, char *argv[]) db__close(&int_db); - for(i=0; iconfig || !db->config->persistence_filepath) return MOSQ_ERR_INVAL; + if(db == NULL || db->config == NULL) return MOSQ_ERR_INVAL; if(db->config->persistence == false) return MOSQ_ERR_SUCCESS; + if(db->config->persistence_filepath == NULL) return MOSQ_ERR_INVAL; log__printf(NULL, MOSQ_LOG_INFO, "Saving in-memory database to %s.", db->config->persistence_filepath); diff --git a/test/unit/persist_write_test.c b/test/unit/persist_write_test.c index b6043761..6165a62d 100644 --- a/test/unit/persist_write_test.c +++ b/test/unit/persist_write_test.c @@ -80,6 +80,7 @@ static void TEST_persistence_disabled(void) memset(&db, 0, sizeof(struct mosquitto_db)); memset(&config, 0, sizeof(struct mosquitto__config)); db.config = &config; + config.persistence = true; rc = persist__backup(&db, false); CU_ASSERT_EQUAL(rc, MOSQ_ERR_INVAL);