From d32e8bc648fc9c9691e54d76d7d9b3959717e5ec Mon Sep 17 00:00:00 2001 From: Norbert Heusser Date: Thu, 23 May 2024 10:19:57 +0200 Subject: [PATCH] Add option to build embedd broker as shared library Signed-off-by: Norbert Heusser --- .gitignore | 3 +- CMakeLists.txt | 2 + Makefile | 4 + config.mk | 9 ++ fuzzing/broker/Makefile | 15 +- fuzzing/broker/broker_fuzz.cpp | 4 +- fuzzing/broker/broker_fuzz_test_config.cpp | 4 +- fuzzing/broker/broker_fuzz_with_init.cpp | 4 +- fuzzing/plugins/dynamic-security/Makefile | 2 +- include/mosquitto/embedded_broker.h | 44 +++++ src/CMakeLists.txt | 180 ++++++++++++++------- src/Makefile | 53 ++++-- src/broker-embedd-macos.syms | 1 + src/embedd-broker.version | 4 + src/embedd_broker.c | 12 ++ src/main.c | 7 + src/mosquitto.c | 6 +- 17 files changed, 264 insertions(+), 90 deletions(-) create mode 100644 include/mosquitto/embedded_broker.h create mode 100644 src/broker-embedd-macos.syms create mode 100644 src/embedd-broker.version create mode 100644 src/embedd_broker.c create mode 100644 src/main.c diff --git a/.gitignore b/.gitignore index 76eff21f36..405e0a2297 100644 --- a/.gitignore +++ b/.gitignore @@ -93,7 +93,8 @@ man/mqtt.7 out/ src/mosquitto -src/mosquitto_broker.a +src/libmosquitto_broker.a +src/libmosquitto_broker.so* test/broker/broker.pid test/test_client diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e1f4bd0c2..444c1cc6fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,6 +89,8 @@ if(WITH_THREADING) endif() endif() +option(WITH_EMBEDD_BROKER, "Create a library mosquitto_broker to embedd the broker" OFF) + option(WITH_DLT "Include DLT support?" OFF) message(STATUS "WITH_DLT = ${WITH_DLT}") if(WITH_DLT) diff --git a/Makefile b/Makefile index 8feee308e7..da3cbfa33f 100644 --- a/Makefile +++ b/Makefile @@ -116,6 +116,9 @@ endif $(INSTALL) include/mosquitto_plugin.h "${DESTDIR}${prefix}/include/mosquitto_plugin.h" $(INSTALL) include/mosquittopp.h "${DESTDIR}${prefix}/include/mosquittopp.h" $(INSTALL) include/mqtt_protocol.h "${DESTDIR}${prefix}/include/mqtt_protocol.h" +ifeq ($(WITH_EMBEDD_BROKER),yes) + $(INSTALL) include/mosquitto/embedded_broker.h "${DESTDIR}${prefix}/include/mosquitto/" +endif uninstall : set -e; for d in ${DIRS}; do $(MAKE) -C $${d} uninstall; done @@ -133,6 +136,7 @@ uninstall : rm -f "${DESTDIR}${prefix}/include/mosquitto_plugin.h" rm -f "${DESTDIR}${prefix}/include/mosquittopp.h" rm -f "${DESTDIR}${prefix}/include/mqtt_protocol.h" + rm -f "${DESTDIR}${prefix}/include/mosquitto/embedded_broker.h" dist : reallyclean set -e; for d in ${DISTDIRS}; do $(MAKE) -C $${d} dist; done diff --git a/config.mk b/config.mk index afbecb037e..e4b283e66e 100644 --- a/config.mk +++ b/config.mk @@ -106,6 +106,10 @@ CLIENT_STATIC_LDADD:= # Build shared libraries WITH_SHARED_LIBRARIES:=yes +# Build the broker additionally as a library, which can be used to +# embedd a broker into an own executable. +WITH_EMBEDD_BROKER:=no + # Build with async dns lookup support for bridges (temporary). Requires glibc. #WITH_ADNS:=yes @@ -232,6 +236,7 @@ ifeq ($(WITH_SHARED_LIBRARIES),yes) else LIBMOSQ:=${R}/lib/libmosquitto.a endif + LIBMOSQ_COMMON:=-Wl,--whole-archive ${R}/libcommon/libmosquitto_common.a -Wl,--no-whole-archive -lcjson ifeq ($(WITH_TLS),yes) @@ -288,6 +293,10 @@ ifeq ($(WITH_COVERAGE),yes) LOCAL_LDFLAGS+=-coverage endif +ifeq ($(WITH_SHARED_LIBRARIES),yes) + LOCAL_CFLAGS+=-fPIC +endif + ifeq ($(WITH_FUZZING),yes) MAKE_ALL+=fuzzing LOCAL_CPPFLAGS+=-DWITH_FUZZING diff --git a/fuzzing/broker/Makefile b/fuzzing/broker/Makefile index 51ac7d5382..a57911c403 100644 --- a/fuzzing/broker/Makefile +++ b/fuzzing/broker/Makefile @@ -17,37 +17,36 @@ LOCAL_CPPFLAGS+=-I${R}/include/ -I${R}/src -I${R}/lib -I${R} -I${R}/common -I${R -DWITH_SYS_TREE -DWITH_TLS -DWITH_TLS_PSK -DWITH_UNIX_SOCKETS -DWITH_WEBSOCKETS=WS_IS_BUILTIN LOCAL_CXXFLAGS+=-g -Wall -Werror -pthread LOCAL_LDFLAGS+= -LOCAL_LIBADD+=$(LIB_FUZZING_ENGINE) ${R}/src/mosquitto_broker.a -lssl -lcrypto -lcjson -lm ${R}/libcommon/libmosquitto_common.a -Wl,-Bdynamic -Wl,-Bstatic -largon2 -Wl,-Bdynamic - +LOCAL_LIBADD+=$(LIB_FUZZING_ENGINE) ${R}/src/libmosquitto_broker.a -lssl -lcrypto -lcjson -lm ${R}/libcommon/libmosquitto_common.a -Wl,-Bdynamic -Wl,-Bstatic -largon2 -Wl,-Bdynamic all: $(FUZZERS) -broker_fuzz_acl_file : broker_fuzz_acl_file.cpp ${R}/src/mosquitto_broker.a +broker_fuzz_acl_file : broker_fuzz_acl_file.cpp ${R}/src/libmosquitto_broker.a $(CXX) $(LOCAL_CXXFLAGS) $(LOCAL_CPPFLAGS) $(LOCAL_LDFLAGS) -o $@ $< $(LOCAL_LIBADD) install $@ ${OUT}/$@ cp ${R}/fuzzing/corpora/broker_acl_file_seed_corpus.zip ${OUT}/$@_seed_corpus.zip cp ${R}/fuzzing/corpora/broker_acl_file.dict ${OUT}/$@.dict -broker_fuzz_password_file : broker_fuzz_password_file.cpp ${R}/src/mosquitto_broker.a +broker_fuzz_password_file : broker_fuzz_password_file.cpp ${R}/src/libmosquitto_broker.a $(CXX) $(LOCAL_CXXFLAGS) $(LOCAL_CPPFLAGS) $(LOCAL_LDFLAGS) -o $@ $< $(LOCAL_LIBADD) install $@ ${OUT}/$@ cp ${R}/fuzzing/corpora/broker_password_file_seed_corpus.zip ${OUT}/$@_seed_corpus.zip -broker_fuzz_psk_file : broker_fuzz_psk_file.cpp ${R}/src/mosquitto_broker.a +broker_fuzz_psk_file : broker_fuzz_psk_file.cpp ${R}/src/libmosquitto_broker.a $(CXX) $(LOCAL_CXXFLAGS) $(LOCAL_CPPFLAGS) $(LOCAL_LDFLAGS) -o $@ $< $(LOCAL_LIBADD) install $@ ${OUT}/$@ cp ${R}/fuzzing/corpora/broker_psk_file_seed_corpus.zip ${OUT}/$@_seed_corpus.zip -broker_fuzz_queue_msg : broker_fuzz_queue_msg.cpp ${R}/src/mosquitto_broker.a +broker_fuzz_queue_msg : broker_fuzz_queue_msg.cpp ${R}/src/libmosquitto_broker.a $(CXX) $(LOCAL_CXXFLAGS) $(LOCAL_CPPFLAGS) $(LOCAL_LDFLAGS) -o $@ $< $(LOCAL_LIBADD) install $@ ${OUT}/$@ cp ${R}/fuzzing/corpora/broker_queue_msg_seed_corpus.zip ${OUT}/$@_seed_corpus.zip -broker_fuzz_read_handle : broker_fuzz_read_handle.cpp ${R}/src/mosquitto_broker.a +broker_fuzz_read_handle : broker_fuzz_read_handle.cpp ${R}/src/libmosquitto_broker.a $(CXX) $(LOCAL_CXXFLAGS) $(LOCAL_CPPFLAGS) $(LOCAL_LDFLAGS) -o $@ $< $(LOCAL_LIBADD) install $@ ${OUT}/$@ cp ${R}/fuzzing/corpora/broker_packet_seed_corpus.zip ${OUT}/$@_seed_corpus.zip -broker_fuzz_test_config : broker_fuzz_test_config.cpp ${R}/src/mosquitto_broker.a +broker_fuzz_test_config : broker_fuzz_test_config.cpp ${R}/src/libmosquitto_broker.a $(CXX) $(LOCAL_CXXFLAGS) $(LOCAL_CPPFLAGS) $(LOCAL_LDFLAGS) -o $@ $< $(LOCAL_LIBADD) install $@ ${OUT}/$@ cp ${R}/fuzzing/corpora/broker_fuzz_test_config_seed_corpus.zip ${OUT}/$@_seed_corpus.zip diff --git a/fuzzing/broker/broker_fuzz.cpp b/fuzzing/broker/broker_fuzz.cpp index e4b382a830..09e73a34f5 100644 --- a/fuzzing/broker/broker_fuzz.cpp +++ b/fuzzing/broker/broker_fuzz.cpp @@ -33,7 +33,7 @@ SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause #define PORT 1883 /* The broker fuzz-only main function. */ -extern "C" int mosquitto_fuzz_main(int argc, char *argv[]); +extern "C" int mosquitto_broker_main(int argc, char *argv[]); void *run_broker(void *args) { @@ -45,7 +45,7 @@ void *run_broker(void *args) argv[2] = strdup("-c"); argv[3] = strdup("/tmp/mosquitto.conf"); - mosquitto_fuzz_main(argc, argv); + mosquitto_broker_main(argc, argv); for(int i=0; i + +All rights reserved. This program and the accompanying materials +are made available under the terms of the Eclipse Public License 2.0 +and Eclipse Distribution License v1.0 which accompany this distribution. + +The Eclipse Public License is available at + https://www.eclipse.org/legal/epl-2.0/ +and the Eclipse Distribution License is available at + http://www.eclipse.org/org/documents/edl-v10.php. + +SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + +Contributors: +*/ + +/* + * File: mosquitto_broker.h + * + * This header contains functions for use by plugins. + */ +#ifndef MOSQUITTO_EMBEDDED_BROKER_H +#define MOSQUITTO_EMBEDDED_BROKER_H + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(WIN32) && defined(mosquitto_EXPORTS) +# define mosq_EXPORT __declspec(dllexport) +#else +# define mosq_EXPORT +#endif + +mosq_EXPORT int mosquitto_broker_main(int argc, char *argv[]); + +mosq_EXPORT void mosquitto_broker_stop(); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c6ba069261..16be63c663 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,4 @@ -add_executable(mosquitto +set(BROKER_SRC ../lib/alias_mosq.c ../lib/alias_mosq.h bridge.c bridge_topic.c broker_control.c @@ -78,45 +78,83 @@ add_executable(mosquitto ../lib/will_mosq.c ../lib/will_mosq.h ) +add_executable(mosquitto ${BROKER_SRC} main.c) + +if (WITH_EMBEDD_BROKER) + add_library(libmosquitto_broker SHARED ${BROKER_SRC}) +endif() + +function(broker_add_compile_definitions scope define) + message(STATUS "broker_add_compile_definitions ${scope} ${define}") + target_compile_definitions(mosquitto ${scope} ${define}) + if (WITH_EMBEDD_BROKER) + target_compile_definitions(libmosquitto_broker ${scope} ${define}) + endif() +endfunction() + +function(broker_include_directories scope file) + message(STATUS "broker_sources ${scope} ${file}") + target_include_directories(mosquitto ${scope} ${file}) + if (WITH_EMBEDD_BROKER) + target_include_directories(libmosquitto_broker ${scope} ${file}) + endif() +endfunction() + +function(broker_sources scope file) + message(STATUS "broker_sources ${scope} ${file}") + target_sources(mosquitto ${scope} ${file}) + if (WITH_EMBEDD_BROKER) + target_sources(libmosquitto_broker ${scope} ${file}) + endif() +endfunction() + +function(broker_link_libraries scope library) + message(STATUS "broker_link_libraries ${scope} ${library}") + target_link_libraries(mosquitto ${scope} ${library}) + if (WITH_EMBEDD_BROKER) + target_link_libraries(libmosquitto_broker ${scope} ${library}) + endif() +endfunction() + CHECK_INCLUDE_FILES(sys/event.h HAVE_SYS_EVENT_H) if(HAVE_SYS_EVENT_H) - target_compile_definitions(mosquitto PRIVATE "WITH_KQUEUE") + broker_add_compile_definitions(PRIVATE "WITH_KQUEUE") endif() find_path(HAVE_SYS_EPOLL_H sys/epoll.h) if(HAVE_SYS_EPOLL_H) - target_compile_definitions(mosquitto PRIVATE "WITH_EPOLL") + broker_add_compile_definitions(PRIVATE "WITH_EPOLL") endif() option(INC_BRIDGE_SUPPORT "Include bridge support for connecting to other brokers?" ON) if(INC_BRIDGE_SUPPORT) - target_sources(mosquitto PRIVATE bridge.c) - target_compile_definitions(mosquitto PRIVATE "WITH_BRIDGE") + broker_sources(PRIVATE bridge.c) + broker_add_compile_definitions(PRIVATE "WITH_BRIDGE") endif() option(USE_LIBWRAP "Include tcp-wrappers support?" OFF) if(USE_LIBWRAP) - target_sources(mosquitto wrap) - target_compile_definitions(mosquitto PRIVATE "WITH_WRAP") + broker_sources(wrap) + broker_add_compile_definitions(PRIVATE "WITH_WRAP") endif() option(INC_DB_UPGRADE "Include database upgrade support? (recommended)" ON) if(INC_MEMTRACK) - target_compile_definitions(mosquitto PUBLIC "WITH_MEMORY_TRACKING") + broker_add_compile_definitions(PUBLIC "WITH_MEMORY_TRACKING") endif() option(WITH_PERSISTENCE "Include persistence support?" ON) if(WITH_PERSISTENCE) - target_compile_definitions(mosquitto PRIVATE "WITH_PERSISTENCE") + broker_add_compile_definitions(PRIVATE "WITH_PERSISTENCE") endif() option(WITH_SYS_TREE "Include $SYS tree support?" ON) if(WITH_SYS_TREE) - target_compile_definitions(mosquitto PRIVATE "WITH_SYS_TREE") + broker_add_compile_definitions(PRIVATE "WITH_SYS_TREE") endif() option(WITH_ADNS "Include ADNS support?" OFF) @@ -124,9 +162,9 @@ option(WITH_ADNS "Include ADNS support?" OFF) if(CMAKE_SYSTEM_NAME STREQUAL Linux) option(WITH_SYSTEMD "Include systemd support?" OFF) if(WITH_SYSTEMD) - target_compile_definitions(mosquitto PRIVATE "WITH_SYSTEMD") + broker_add_compile_definitions(PRIVATE "WITH_SYSTEMD") find_library(SYSTEMD_LIBRARY systemd) - target_link_libraries(mosquitto PRIVATE ${SYSTEMD_LIBRARY}) + broker_link_libraries(PRIVATE "${SYSTEMD_LIBRARY}") endif() endif() @@ -134,78 +172,80 @@ option(STATIC_WEBSOCKETS "Use the static libwebsockets library?" OFF) option(WITH_CONTROL "Include $CONTROL topic support?" ON) if(WITH_CONTROL) - target_compile_definitions(mosquitto PRIVATE "WITH_CONTROL") + broker_add_compile_definitions(PRIVATE "WITH_CONTROL") endif() if(WIN32 OR CYGWIN) - target_sources(mosquitto PRIVATE service.c) + broker_sources(PRIVATE service.c) endif() -target_compile_definitions(mosquitto PRIVATE "WITH_BROKER") +broker_add_compile_definitions(PRIVATE "WITH_BROKER") if(WITH_TLS) - target_link_libraries(mosquitto PRIVATE OpenSSL::SSL) + broker_link_libraries(PRIVATE OpenSSL::SSL) endif() # Check for getaddrinfo_a include(CheckLibraryExists) check_library_exists(anl getaddrinfo_a "" HAVE_GETADDRINFO_A) if(HAVE_GETADDRINFO_A AND WITH_ADNS) - target_compile_definitions(mosquitto PRIVATE "WITH_ADNS") - target_compile_definitions(mosquitto PRIVATE "HAVE_GETADDRINFO_A") - target_link_libraries(mosquitto PRIVATE anl) + broker_add_compile_definitions(PRIVATE "WITH_ADNS") + broker_add_compile_definitions(PRIVATE "HAVE_GETADDRINFO_A") + broker_link_libraries(PRIVATE anl) endif() if(UNIX) if(APPLE) - target_link_libraries(mosquitto PRIVATE dl m) + broker_link_libraries(PRIVATE dl m) elseif (${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD") - target_link_libraries(mosquitto PRIVATE m) + broker_link_libraries(PRIVATE m) elseif (${CMAKE_SYSTEM_NAME} MATCHES "NetBSD") - target_link_libraries(mosquitto PRIVATE m) + broker_link_libraries(PRIVATE m) elseif (${CMAKE_SYSTEM_NAME} MATCHES "Haiku") - target_link_libraries(mosquitto PRIVATE m network) + broker_link_libraries(PRIVATE network) + broker_link_libraries(PRIVATE m) elseif(QNX) - target_link_libraries(mosquitto PRIVATE m socket) + broker_link_libraries(PRIVATE socket) + broker_link_libraries(PRIVATE m) else() - target_link_libraries(mosquitto PRIVATE dl m) + broker_link_libraries(PRIVATE dl) + broker_link_libraries(PRIVATE m) find_library(LIBRT rt) if(LIBRT) - target_link_libraries(mosquitto PRIVATE rt) + broker_link_libraries(PRIVATE rt) endif() endif() endif() if(WIN32) - target_link_libraries(mosquitto PRIVATE ws2_32) + broker_link_libraries(PRIVATE ws2_32) endif() if(WITH_WEBSOCKETS) if(WITH_WEBSOCKETS_BUILTIN) - target_compile_definitions(mosquitto PRIVATE "WITH_WEBSOCKETS=WS_IS_BUILTIN") - target_sources(mosquitto PRIVATE ${mosquitto_SOURCE_DIR}/deps/picohttpparser/picohttpparser.c) + broker_add_compile_definitions(PRIVATE "WITH_WEBSOCKETS=WS_IS_BUILTIN") + broker_sources(PRIVATE "${mosquitto_SOURCE_DIR}/deps/picohttpparser/picohttpparser.c") else() find_package(libwebsockets) - target_compile_definitions(mosquitto PRIVATE "WITH_WEBSOCKETS=WS_IS_LWS") + broker_add_compile_definitions(PRIVATE "WITH_WEBSOCKETS=WS_IS_LWS") endif() endif() if (ANDROID) - target_link_libraries(mosquitto PRIVATE log) + broker_link_libraries(PRIVATE log) endif (ANDROID) if(WITH_WEBSOCKETS) if(WITH_WEBSOCKETS_BUILTIN) - target_include_directories(mosquitto PRIVATE - "${mosquitto_SOURCE_DIR}/deps/picohttpparser") + broker_include_directories(PRIVATE "${mosquitto_SOURCE_DIR}/deps/picohttpparser") else() if(STATIC_WEBSOCKETS) - target_link_libraries(mosquitto PRIVATE websockets_static) + broker_link_libraries(PRIVATE websockets_static) if(WIN32) - target_link_libraries(mosquitto PRIVATE iphlpapi) + broker_link_libraries(PRIVATE iphlpapi) endif() else(STATIC_WEBSOCKETS) - target_link_libraries(mosquitto PRIVATE websockets) + broker_link_libraries(PRIVATE websockets) endif() endif() endif() @@ -213,41 +253,31 @@ endif() if(WITH_DLT) message(STATUS "DLT_LIBDIR = ${DLT_LIBDIR}") target_link_directories(mosquitto PRIVATE ${DLT_LIBDIR}) - target_link_libraries(mosquitto PRIVATE ${DLT_LIBRARIES}) - target_compile_definitions(mosquitto PRIVATE "WITH_DLT") + broker_link_libraries(PRIVATE "${DLT_LIBRARIES}") + broker_add_compile_definitions(PRIVATE "WITH_DLT") endif() -target_link_libraries(mosquitto PRIVATE cJSON) +broker_link_libraries(PRIVATE cJSON) + +broker_include_directories(PUBLIC "${mosquitto_SOURCE_DIR}/include") +broker_include_directories(PRIVATE "${mosquitto_SOURCE_DIR}/common") +broker_include_directories(PRIVATE "${mosquitto_SOURCE_DIR}/lib") +broker_include_directories(PRIVATE "${mosquitto_SOURCE_DIR}/libcommon") +broker_include_directories(PRIVATE "${mosquitto_SOURCE_DIR}/src") -target_include_directories(mosquitto - PUBLIC - "${mosquitto_SOURCE_DIR}/include" - PRIVATE - "${mosquitto_SOURCE_DIR}/common" - "${mosquitto_SOURCE_DIR}/lib" - "${mosquitto_SOURCE_DIR}/libcommon" - "${mosquitto_SOURCE_DIR}/src" -) if(WITH_BUNDLED_DEPS) - target_include_directories(mosquitto PRIVATE - "${mosquitto_SOURCE_DIR}/deps" - ) + broker_include_directories(PRIVATE "${mosquitto_SOURCE_DIR}/deps") endif() -target_link_libraries(mosquitto - PUBLIC - config-header - libmosquitto_common - PRIVATE - common-options - ${MOSQ_LIBS} -) +broker_link_libraries(PUBLIC config-header) +broker_link_libraries(PUBLIC libmosquitto_common) +broker_link_libraries(PRIVATE common-options) if (WITH_THREADING AND NOT WIN32) set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) - target_link_libraries(mosquitto PRIVATE Threads::Threads) + broker_link_libraries(PRIVATE Threads::Threads) endif() set_target_properties(mosquitto PROPERTIES @@ -269,3 +299,33 @@ endif() install(TARGETS mosquitto RUNTIME DESTINATION "${CMAKE_INSTALL_SBINDIR}" ) + +if (WITH_EMBEDD_BROKER) + set_target_properties(libmosquitto_broker PROPERTIES + OUTPUT_NAME mosquitto_broker + VERSION ${VERSION} + SOVERSION 1 + POSITION_INDEPENDENT_CODE 1 + ) + if(APPLE) + set_target_properties(libmosquitto_broker PROPERTIES + LINK_FLAGS "-Wl,-exported_symbols_list -Wl,${mosquitto_SOURCE_DIR}/src/broker-embedd-macos.syms" + ) + else() + set_target_properties(libmosquitto_broker PROPERTIES + LINK_FLAGS "-Wl,-dynamic-list=${mosquitto_SOURCE_DIR}/src/broker-embedd.syms" + ) + endif() + + install(FILES + ${mosquitto_SOURCE_DIR}/include/mosquitto/embedded_broker.h + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/mosquitto" + ) + + install(TARGETS libmosquitto_broker + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ) +endif() + diff --git a/src/Makefile b/src/Makefile index 0c65d5c985..956104ddc7 100644 --- a/src/Makefile +++ b/src/Makefile @@ -6,6 +6,7 @@ include ${R}/config.mk LOCAL_CFLAGS+= LOCAL_CPPFLAGS+=-DWITH_BROKER -I${R}/lib -I${R}/libcommon LOCAL_LDFLAGS+= +LOCAL_EMBEDD_LDFLAGS=${LOCAL_LDFLAGS} LOCAL_LDADD+=-lcjson -lm ${LIBMOSQ_COMMON} # ------------------------------------------ @@ -13,6 +14,7 @@ LOCAL_LDADD+=-lcjson -lm ${LIBMOSQ_COMMON} # ------------------------------------------ ifneq ($(or $(findstring $(UNAME),FreeBSD), $(findstring $(UNAME),OpenBSD), $(findstring $(UNAME),NetBSD)),) LOCAL_LDFLAGS+=-Wl,--dynamic-list=linker.syms + LOCAL_EMBEDD_LDFLAGS+=-Wl,--version-script=embedd-broker.version else LOCAL_LDADD+=-ldl endif @@ -20,6 +22,7 @@ endif ifeq ($(UNAME),Linux) LOCAL_LDADD+=-lrt LOCAL_LDFLAGS+=-Wl,--dynamic-list=linker.syms + LOCAL_EMBEDD_LDFLAGS+=-Wl,--version-script=embedd-broker.version endif ifeq ($(UNAME),QNX) @@ -35,12 +38,20 @@ include ${R}/make/broker.mk # Targets # ------------------------------------------ ifeq ($(WITH_FUZZING),yes) -all : mosquitto_broker.a +ALL_TARGETS=libmosquitto_broker.a else -all : mosquitto +ALL_TARGETS=mosquitto +ifeq ($(WITH_EMBEDD_BROKER),yes) +ALL_TARGETS+=libmosquitto_broker.a +ifeq ($(WITH_SHARED_LIBRARIES),yes) +ALL_TARGETS+=libmosquitto_broker.so.${SOVERSION} +endif +endif endif -OBJS= mosquitto.o \ +all : ${ALL_TARGETS} + +COMMON_OBJS= mosquitto.o \ bridge.o \ bridge_topic.o \ broker_control.o \ @@ -105,7 +116,6 @@ OBJS= mosquitto.o \ send_unsuback.o \ service.o \ session_expiry.o \ - signals.o \ subs.o \ sys_tree.o \ topic_tok.o \ @@ -113,6 +123,13 @@ OBJS= mosquitto.o \ will_delay.o \ xtreport.o +BROKER_OBJS=${COMMON_OBJS} \ + main.o \ + signals.o + +EMBED_OBJS=${COMMON_OBJS} \ + embedd_broker.o + OBJS_EXTERNAL= \ alias_mosq.o \ handle_ping.o \ @@ -142,13 +159,21 @@ ifeq ($(WITH_WEBSOCKETS),yes) OBJS_EXTERNAL+=${R}/deps/picohttpparser/picohttpparser.o endif -mosquitto : ${OBJS} ${OBJS_EXTERNAL} +libmosquitto_broker.a : ${EMBED_OBJS} ${OBJS_EXTERNAL} + ${CROSS_COMPILE}$(AR) cr $@ $^ + +ifeq ($(WITH_SHARED_LIBRARIES),yes) +libmosquitto_broker.so.${SOVERSION} : ${EMBED_OBJS} ${OBJS_EXTERNAL} + ${CROSS_COMPILE}$(CC) $(LOCAL_EMBEDD_LDFLAGS) -shared $^ -o $@ ${LOCAL_LDADD} +endif + +mosquitto : ${BROKER_OBJS} ${OBJS_EXTERNAL} ${CROSS_COMPILE}${CC} ${LOCAL_LDFLAGS} $^ -o $@ $(LOCAL_LDADD) -mosquitto_broker.a : ${OBJS} ${OBJS_EXTERNAL} - ${CROSS_COMPILE}$(AR) cr $@ $^ +${BROKER_OBJS} : %.o: %.c mosquitto_broker_internal.h + ${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@ -${OBJS} : %.o: %.c mosquitto_broker_internal.h +embedd_broker.o : embedd_broker.c mosquitto_broker_internal.h Makefile ${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(LOCAL_CFLAGS) -c $< -o $@ alias_mosq.o : ${R}/lib/alias_mosq.c ${R}/lib/alias_mosq.h @@ -226,14 +251,24 @@ will_mosq.o : ${R}/lib/will_mosq.c ${R}/lib/will_mosq.h install : all $(INSTALL) -d "${DESTDIR}$(prefix)/sbin" $(INSTALL) ${STRIP_OPTS} mosquitto "${DESTDIR}${prefix}/sbin/mosquitto" +ifeq ($(WITH_EMBEDD_BROKER),yes) + $(INSTALL) ${STRIP_OPTS} libmosquitto_broker.so.${SOVERSION} "${DESTDIR}${libdir}/libmosquitto_broker.so.${SOVERSION}" + ln -sf libmosquitto_broker.so.${SOVERSION} "${DESTDIR}${libdir}/libmosquitto_broker.so" +ifeq ($(WITH_STATIC_LIBRARIES),yes) + $(INSTALL) ${STRIP_OPTS} libmosquitto_broker.a "${DESTDIR}${libdir}/libmosquitto_broker.a" +endif +endif uninstall : -rm -f "${DESTDIR}${prefix}/sbin/mosquitto" -rm -f "${DESTDIR}${prefix}/include/mosquitto_broker.h" -rm -f "${DESTDIR}${prefix}/include/mosquitto_plugin.h" + -rm -f "${DESTDIR}${libdir}/libmosquitto_broker.so.${SOVERSION}" + -rm -f "${DESTDIR}${libdir}/libmosquitto_broker.so" + -rm -f "${DESTDIR}${libdir}/libmosquitto_broker.a" clean : - -rm -f ${OBJS} ${OBJS_EXTERNAL} mosquitto mosquitto_broker.a *.gcda *.gcno + -rm -f ${OBJS} ${OBJS_EXTERNAL} mosquitto libmosquitto_broker.a libmosquitto_broker.so.${VERSION} *.gcda *.gcno reallyclean : clean -rm -rf *.orig *.db diff --git a/src/broker-embedd-macos.syms b/src/broker-embedd-macos.syms new file mode 100644 index 0000000000..932d95a5ba --- /dev/null +++ b/src/broker-embedd-macos.syms @@ -0,0 +1 @@ +_mosquitto_broker_main diff --git a/src/embedd-broker.version b/src/embedd-broker.version new file mode 100644 index 0000000000..a97932f732 --- /dev/null +++ b/src/embedd-broker.version @@ -0,0 +1,4 @@ +EMBEDD { + global: mosquitto_broker_main; mosquitto_broker_stop; # explicitly list symbols to be exported + local: *; # hide everything else +}; diff --git a/src/embedd_broker.c b/src/embedd_broker.c new file mode 100644 index 0000000000..eb0a1f1e6d --- /dev/null +++ b/src/embedd_broker.c @@ -0,0 +1,12 @@ +#include +#include "mosquitto_broker_internal.h" + +extern int g_run; + +void signal__setup(void) {} +void signal__flag_check(void) {} + +void mosquitto_broker_stop() +{ + g_run = false; +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000000..885ca34c59 --- /dev/null +++ b/src/main.c @@ -0,0 +1,7 @@ + +int mosquitto_broker_main(int argc, char *argv[]); + +int main(int argc, char *argv[]) +{ + return mosquitto_broker_main(argc, argv); +} diff --git a/src/mosquitto.c b/src/mosquitto.c index 6b19ac4049..7f4ce93967 100644 --- a/src/mosquitto.c +++ b/src/mosquitto.c @@ -370,11 +370,7 @@ static void cjson_init(void) cJSON_InitHooks(&hooks); } -#ifdef WITH_FUZZING -int mosquitto_fuzz_main(int argc, char *argv[]) -#else -int main(int argc, char *argv[]) -#endif +int mosquitto_broker_main(int argc, char *argv[]) { struct mosquitto__config config; int rc;