From 5731dd8653a7d5e0fa9bb859f79458ba12a2ac15 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 5 May 2022 16:17:24 +0100 Subject: [PATCH] Add mosquitto_persistence_location() for plugins. --- ChangeLog.txt | 2 ++ include/mosquitto_broker.h | 14 ++++++++++++++ mosquitto.conf | 6 ++++++ src/conf.c | 8 ++++++++ src/linker-macosx.syms | 1 + src/linker.syms | 1 + src/plugin_public.c | 5 +++++ 7 files changed, 37 insertions(+) diff --git a/ChangeLog.txt b/ChangeLog.txt index c27f515e..16982b2a 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -105,6 +105,8 @@ Plugins / plugin interface: network loop, to give chance for PUBACK/PUBREC to be sent. Closes #2474. - Add MOSQ_EVT_SUBSCRIBE and MOSQ_EVT_UNSUBSCRIBE events that are called when subscribe/unsubscribes actually succeed. +- Add `mosquitto_persistence_location()` for plugins to use to find a valid + location for storing persistent data. Client library: - Add MOSQ_OPT_DISABLE_SOCKETPAIR to allow the disabling of the socketpair diff --git a/include/mosquitto_broker.h b/include/mosquitto_broker.h index 69c03a7c..ed1643a8 100644 --- a/include/mosquitto_broker.h +++ b/include/mosquitto_broker.h @@ -1148,6 +1148,20 @@ int mosquitto_persist_retain_msg_set(const char *topic, uint64_t store_id); */ int mosquitto_persist_retain_msg_delete(const char *topic); +/* Function: mosquitto_persistence_location + * + * Returns the `persistence_location` config option, or the contents of the + * MOSQUITTO_PERSISTENCE_LOCATION environment variable, if set. + * + * This location should be used by plugins needing to store persistent data. + * Use of sub directories is recommended. + * + * Returns: + * A valid pointer to the persistence location string + * A NULL pointer if neither the option nor the variable are set + */ +const char *mosquitto_persistence_location(void); + #ifdef __cplusplus } #endif diff --git a/mosquitto.conf b/mosquitto.conf index 4e6904b7..49e2265e 100644 --- a/mosquitto.conf +++ b/mosquitto.conf @@ -485,6 +485,12 @@ # Default is an empty string (current directory). # Set to e.g. /var/lib/mosquitto if running as a proper service on Linux or # similar. +# +# Can also be defined by setting the MOSQUITTO_PERSISTENCE_LOCATION environment +# variable prior to running the broker. If both the config file option and the +# environment variable are set, the environment variable takes precedent. +# +# This can also be used by plugins using the mosquitto_persistence_location() function. #persistence_location diff --git a/src/conf.c b/src/conf.c index cf2d2339..ac470230 100644 --- a/src/conf.c +++ b/src/conf.c @@ -564,6 +564,14 @@ int config__parse_args(struct mosquitto__config *config, int argc, char *argv[]) if(db.verbose){ config->log_type = UINT_MAX; } + + if(getenv("MOSQUITTO_PERSISTENCE_LOCATION")){ + mosquitto__FREE(config->persistence_location); + config->persistence_location = mosquitto_strdup(getenv("MOSQUITTO_PERSISTENCE_LOCATION")); + if(!config->persistence_location){ + return MOSQ_ERR_NOMEM; + } + } return config__check(config); } diff --git a/src/linker-macosx.syms b/src/linker-macosx.syms index 7c4b5ffd..c51c2f9b 100644 --- a/src/linker-macosx.syms +++ b/src/linker-macosx.syms @@ -23,6 +23,7 @@ _mosquitto_kick_client_by_username _mosquitto_log_printf _mosquitto_log_vprintf _mosquitto_malloc +_mosquitto_persistence_location _mosquitto_persist_base_msg_add _mosquitto_persist_base_msg_delete _mosquitto_persist_client_add diff --git a/src/linker.syms b/src/linker.syms index 700eaf21..57d7c5cb 100644 --- a/src/linker.syms +++ b/src/linker.syms @@ -35,6 +35,7 @@ mosquitto_persist_client_update; mosquitto_persist_retain_msg_delete; mosquitto_persist_retain_msg_set; + mosquitto_persistence_location; mosquitto_plugin_set_info; mosquitto_property_add_binary; mosquitto_property_add_byte; diff --git a/src/plugin_public.c b/src/plugin_public.c index f10c19a0..0da33858 100644 --- a/src/plugin_public.c +++ b/src/plugin_public.c @@ -865,3 +865,8 @@ BROKER_EXPORT int mosquitto_broker_node_id_set(uint16_t id) return MOSQ_ERR_SUCCESS; } } + +BROKER_EXPORT const char *mosquitto_persistence_location(void) +{ + return db.config->persistence_location; +}