diff --git a/ChangeLog.txt b/ChangeLog.txt index 1109359f..e6f24e7c 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -14,6 +14,8 @@ Broker: - Set persistence file to only be readable by owner, except on Windows. Closes #468. - Fix CONNECT check for reserved=0, as per MQTT v3.1.1 check MQTT-3.1.2-3. +- When the broker stop, wills for any connected clients are now "sent". Closes + #477. Clients: - Don't use / in auto-generated client ids. diff --git a/src/context.c b/src/context.c index 20e1820f..561ff4aa 100644 --- a/src/context.c +++ b/src/context.c @@ -143,6 +143,8 @@ void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, b context->address = NULL; } + mqtt3_context_send_will(db, context); + if(context->id){ assert(db); /* db can only be NULL here if the client hasn't sent a CONNECT and hence wouldn't have an id. */ @@ -163,12 +165,6 @@ void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, b context->out_packet = context->out_packet->next; _mosquitto_free(packet); } - if(context->will){ - if(context->will->topic) _mosquitto_free(context->will->topic); - if(context->will->payload) _mosquitto_free(context->will->payload); - _mosquitto_free(context->will); - context->will = NULL; - } if(do_free || context->clean_session){ msg = context->msgs; while(msg){ @@ -185,7 +181,8 @@ void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, b } } -void mqtt3_context_disconnect(struct mosquitto_db *db, struct mosquitto *ctxt) + +void mqtt3_context_send_will(struct mosquitto_db *db, struct mosquitto *ctxt) { if(ctxt->state != mosq_cs_disconnecting && ctxt->will){ if(mosquitto_acl_check(db, ctxt, ctxt->will->topic, MOSQ_ACL_WRITE) == MOSQ_ERR_SUCCESS){ @@ -199,6 +196,13 @@ void mqtt3_context_disconnect(struct mosquitto_db *db, struct mosquitto *ctxt) _mosquitto_free(ctxt->will); ctxt->will = NULL; } +} + + +void mqtt3_context_disconnect(struct mosquitto_db *db, struct mosquitto *ctxt) +{ + mqtt3_context_send_will(db, ctxt); + ctxt->disconnect_t = time(NULL); _mosquitto_socket_close(db, ctxt); } diff --git a/src/mosquitto.c b/src/mosquitto.c index c1bd328f..1fd1e7ff 100644 --- a/src/mosquitto.c +++ b/src/mosquitto.c @@ -387,12 +387,6 @@ int main(int argc, char *argv[]) _mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "mosquitto version %s terminating", VERSION); mqtt3_log_close(&config); -#ifdef WITH_PERSISTENCE - if(config.persistence){ - mqtt3_db_backup(&int_db, true); - } -#endif - #ifdef WITH_WEBSOCKETS for(i=0; ilistener_count; i++){ if(int_db.config->listeners[i].ws_context){ @@ -428,6 +422,12 @@ int main(int argc, char *argv[]) #endif mosquitto__free_disused_contexts(&int_db); +#ifdef WITH_PERSISTENCE + if(config.persistence){ + mqtt3_db_backup(&int_db, true); + } +#endif + mqtt3_db_close(&int_db); if(listensock){ diff --git a/src/mosquitto_broker.h b/src/mosquitto_broker.h index c89489b6..9ab3ab08 100644 --- a/src/mosquitto_broker.h +++ b/src/mosquitto_broker.h @@ -452,6 +452,7 @@ void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, b void mqtt3_context_disconnect(struct mosquitto_db *db, struct mosquitto *context); void mosquitto__add_context_to_disused(struct mosquitto_db *db, struct mosquitto *context); void mosquitto__free_disused_contexts(struct mosquitto_db *db); +void mqtt3_context_send_will(struct mosquitto_db *db, struct mosquitto *context); /* ============================================================ * Logging functions