summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Bentele2020-10-16 17:15:49 +0200
committerManuel Bentele2020-10-16 17:15:49 +0200
commit969496f15e1e0359e26c2c6e995ad4ef82720f86 (patch)
tree358466216630eb10e8ee91b9ede490424f6db848
parent[FUSE] turn on compiler optimization to fix warning (diff)
downloaddnbd3-969496f15e1e0359e26c2c6e995ad4ef82720f86.tar.gz
dnbd3-969496f15e1e0359e26c2c6e995ad4ef82720f86.tar.xz
dnbd3-969496f15e1e0359e26c2c6e995ad4ef82720f86.zip
[BUILD] rewrite CMake build system to track changes of source files
This change restructures the source code directories, separates shared form non-shared application code and adds CMake dependencies. These dependencies allow the tracking of changes and trigger a rebuild of those build targets where changed files are involved. WARNING: Note that the support of the DNBD3_SERVER_AFL build option is not supported yet. Thus, the option should be never turned on.
-rw-r--r--.gitignore2
-rw-r--r--CMakeLists.txt378
-rw-r--r--Kbuild.in2
-rwxr-xr-xbuild.sh6
-rw-r--r--cmake/GenerateVersion.cmake15
-rw-r--r--cmake/Kernel.cmake57
-rw-r--r--cmake/KernelVersion.cmake12
-rw-r--r--cmake/ProjectVersion.cmake27
-rwxr-xr-xget-version.sh24
-rw-r--r--inc/dnbd3/config.h (renamed from src/config.h)0
-rw-r--r--inc/dnbd3/config/client.h (renamed from src/clientconfig.h)0
-rw-r--r--inc/dnbd3/config/server.h (renamed from src/serverconfig.h)2
-rw-r--r--inc/dnbd3/shared/crc32.h (renamed from src/shared/crc32.h)0
-rw-r--r--inc/dnbd3/shared/fdsignal.h (renamed from src/shared/fdsignal.h)0
-rw-r--r--inc/dnbd3/shared/log.h (renamed from src/shared/log.h)0
-rw-r--r--inc/dnbd3/shared/protocol.h (renamed from src/shared/protocol.h)7
-rw-r--r--inc/dnbd3/shared/serialize.h (renamed from src/serialize.h)4
-rw-r--r--inc/dnbd3/shared/sockhelper.h (renamed from src/shared/sockhelper.h)2
-rw-r--r--inc/dnbd3/shared/timing.h (renamed from src/shared/timing.h)0
-rw-r--r--inc/dnbd3/types.h (renamed from src/types.h)11
-rw-r--r--inc/dnbd3/version.h.in9
-rwxr-xr-xpack.sh6
-rw-r--r--src/CMakeLists.txt13
-rw-r--r--src/bench/CMakeLists.txt13
-rw-r--r--src/bench/connection.c10
-rw-r--r--src/bench/connection.h2
-rw-r--r--src/bench/helper.h2
-rw-r--r--src/bench/main.c6
-rw-r--r--src/bench/serialize.c5
-rw-r--r--src/client/CMakeLists.txt14
-rw-r--r--src/client/client.c10
-rw-r--r--src/fuse/CMakeLists.txt14
-rw-r--r--src/fuse/connection.c10
-rw-r--r--src/fuse/connection.h4
-rw-r--r--src/fuse/helper.h2
-rw-r--r--src/fuse/main.c10
-rw-r--r--src/fuse/serialize.c5
-rw-r--r--src/kernel/CMakeLists.txt48
-rw-r--r--src/kernel/Kbuild5
-rw-r--r--src/kernel/blk.c2
-rw-r--r--src/kernel/blk.h2
-rw-r--r--src/kernel/dnbd3_main.c (renamed from src/kernel/core.c)8
-rw-r--r--src/kernel/dnbd3_main.h (renamed from src/kernel/dnbd3.h)7
-rw-r--r--src/kernel/net.c4
-rw-r--r--src/kernel/net.h2
l---------src/kernel/serialize.c1
-rw-r--r--src/kernel/serialize_kmod.c5
-rw-r--r--src/kernel/sysfs.h2
-rw-r--r--src/server/CMakeLists.txt66
-rw-r--r--src/server/altservers.c6
-rw-r--r--src/server/fuse.c10
-rw-r--r--src/server/globals.c2
-rw-r--r--src/server/globals.h8
-rw-r--r--src/server/helper.h4
-rw-r--r--src/server/image.c6
-rw-r--r--src/server/locks.c4
-rw-r--r--src/server/locks.h6
-rw-r--r--src/server/net.c16
-rw-r--r--src/server/rpc.c12
-rw-r--r--src/server/serialize.c5
-rw-r--r--src/server/server.c18
-rw-r--r--src/server/server.h2
-rw-r--r--src/server/threadpool.c4
-rw-r--r--src/server/threadpool.h2
-rw-r--r--src/server/uplink.c14
-rw-r--r--src/server/uplink.h2
-rw-r--r--src/shared/CMakeLists.txt15
-rw-r--r--src/shared/crc32.c2
-rw-r--r--src/shared/fdsignal.c2
-rw-r--r--src/shared/log.c4
-rw-r--r--src/shared/serialize.c (renamed from src/serialize.c)5
-rw-r--r--src/shared/sockhelper.c20
-rw-r--r--src/shared/timing.c2
-rw-r--r--src/version.c.in3
-rw-r--r--src/version.h26
75 files changed, 542 insertions, 494 deletions
diff --git a/.gitignore b/.gitignore
index 38ae262..6617c58 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,5 +4,3 @@ build/
*.swp
.autotools
.idea
-/version.txt
-
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 079fd9b..a1a91aa 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,279 +1,103 @@
-################################################################################
-# GENERAL #
-################################################################################
-
-PROJECT(dnbd3 C)
-CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12)
-IF (CMAKE_BUILD_TYPE STREQUAL "")
- SET(CMAKE_BUILD_TYPE Debug)
-ENDIF()
-
-SET(CMAKE_INSTALL_PREFIX "/usr/local" CACHE PATH "Path prefix for system installation")
-OPTION(BUILD_FUSE_CLIENT "Build dnbd3 fuse client" ON)
-OPTION(BUILD_SERVER "Build dnbd3 server" ON)
-OPTION(BUILD_SERVER_FUSE "Enable FUSE-Integration for dnbd3 server" OFF)
-OPTION(BUILD_STRESSTEST "Build dnbd3 stress testing tool" OFF)
-SET(EXTRA_C_FLAGS "" CACHE STRING "Additional options to pass to compiler")
-OPTION(DEBUG_LOCKS "Add lock debugging code to dnbd3-server" OFF)
-OPTION(DEBUG_THREADS "Add thread debugging code to dnbd3-server" OFF)
-
-OPTION(SERVER_FOR_AFL "Build dnbd3-server for usage with afl-fuzz" OFF)
-
-# Is there a non-retarded way to check if build type is debug or release?
-# When specifying, it is case insensitive, so DeBuG would also enable debug builds,
-# but in cmake, we can only do case sensitive matches... :/
-string( TOLOWER "${CMAKE_BUILD_TYPE}" bt_lower )
-if (NOT bt_lower MATCHES "^(debug|release)$")
- message( FATAL_ERROR "Build type needs to be either Debug or Release" )
-endif()
-
-message( "Build Type selected: ${CMAKE_BUILD_TYPE}" )
-
-IF(CMAKE_SYSTEM_NAME MATCHES "BSD")
- message("Detected *BSD System: disable build of Kernel Module.")
- SET(BUILD_KERNEL_MODULE False)
-ELSE()
- OPTION(BUILD_KERNEL_MODULE "Build the dnbd3 Linux kernel module" ON)
-ENDIF()
-
-INCLUDE(CheckCCompilerFlag)
-macro (TRY_ADD_FLAG _FLAG)
- UNSET(TMP_TEST CACHE)
- CHECK_C_COMPILER_FLAG("${_FLAG}" TMP_TEST)
- if (TMP_TEST)
- message(":-) Compiler supports ${_FLAG}")
- SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_FLAG}")
- else()
- message(":-( Compiler does not support ${_FLAG}")
- endif()
-
-endmacro()
-
-# Common for gcc and clang
-#SET(CMAKE_EXE_LINKER_FLAGS "-Wl,-z,relro,-z,now,-z,defs -pie")
-SET(CMAKE_C_FLAGS_RELEASE "-DNDEBUG")
-SET(CMAKE_C_FLAGS "-O2 -std=c11 -Wno-multichar -fno-strict-aliasing -D_GNU_SOURCE -D_FORTIFY_SOURCE=2 ${EXTRA_C_FLAGS}")
-# Hardening. Try as much as is possible.
-#TRY_ADD_FLAG("-mmitigate-rop")
-#TRY_ADD_FLAG("-fstack-protector-strong")
-#TRY_ADD_FLAG("-fstack-clash-protection")
-#TRY_ADD_FLAG("-Wformat")
-#TRY_ADD_FLAG("-Wformat-security")
-#TRY_ADD_FLAG("-Werror=format-security")
-if(CMAKE_C_COMPILER MATCHES "clang")
- message( "Using clang flags." )
- SET(CMAKE_C_FLAGS_DEBUG " -Og -fno-omit-frame-pointer -g -Wall -Wextra -Wpedantic -Wno-unused-result -D_DEBUG")
-elseif (CMAKE_C_COMPILER MATCHES "(cc-)|(cc$)")
- message( "Using (g)cc flags." )
- SET(CMAKE_C_FLAGS_DEBUG " -Og -g -Wall -Wextra -Wpedantic -Wconversion -Wno-sign-conversion -D_DEBUG")
-else()
- message( FATAL_ERROR "Could not determine compiler type." )
-endif()
-
-set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
-
-ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
-ADD_DEFINITIONS(-DWITH_IPV6)
-ADD_DEFINITIONS(-DBUILD_TYPE=${CMAKE_BUILD_TYPE})
-if (DEBUG_LOCKS)
- ADD_DEFINITIONS(-DDEBUG_LOCKS)
-ENDIF()
-if (DEBUG_THREADS)
- ADD_DEFINITIONS(-DDEBUG_THREADS)
-ENDIF()
-
-FIND_PACKAGE(Threads)
-if(BUILD_SERVER_FUSE OR BUILD_FUSE_CLIENT)
- FIND_PACKAGE(Fuse)
- message( " FUSE: ${FUSE_VERSION} " )
-endif()
-
-SET(DO_ABORT False)
-
-message( " *************************************************" )
-if(BUILD_FUSE_CLIENT)
- if(NOT FUSE_FOUND)
- message( " *** No fuse dev libs found, can't build dnbd3-fuse" )
- SET(DO_ABORT True)
- endif()
- if(NOT THREADS_FOUND)
- message( " *** No threads found, can't build dnbd3-fuse" )
- SET(DO_ABORT True)
- endif()
-endif()
-if(BUILD_SERVER)
- FIND_PACKAGE(Jansson)
- if(NOT THREADS_FOUND)
- message( " *** No threads found, can't build dnbd3-server" )
- SET(DO_ABORT True)
- endif()
- if(NOT JANSSON_FOUND)
- message( " *** No jansson lib found, can't build dnbd3-server" )
- SET(DO_ABORT True)
- endif()
- if(BUILD_SERVER_FUSE AND NOT FUSE_FOUND)
- message( " *** No fuse dev libs found, can't build dnbd3-server with fuse support" )
- SET(DO_ABORT True)
- endif()
-endif()
-if(BUILD_STRESSTEST)
- if(NOT THREADS_FOUND)
- message( " *** No threads found, can't build dnbd3-bench" )
- SET(DO_ABORT True)
- endif()
-endif()
-message( " *************************************************" )
-if(DO_ABORT)
- message( FATAL_ERROR "Aborting." )
-endif()
-
-#SET(FUSE_INCLUDE_DIR "")
-#SET(JANSSON_INCLUDE_DIR "")
-
-################################################################################
-# VERSION HEADER #
-################################################################################
-
-FILE(WRITE ${CMAKE_BINARY_DIR}/version.cmake
-"EXECUTE_PROCESS(
- COMMAND \${CMD}
- OUTPUT_VARIABLE VERSION
- OUTPUT_STRIP_TRAILING_WHITESPACE
- )
- CONFIGURE_FILE(\${SRC} \${DST} @ONLY)
-")
-ADD_CUSTOM_COMMAND(OUTPUT fake-file.c ${CMAKE_BINARY_DIR}/generated/version.c
- COMMAND
- ${CMAKE_COMMAND} -D SRC=${CMAKE_SOURCE_DIR}/src/version.c.in
- -D DST=${CMAKE_BINARY_DIR}/generated/version.c
- -D CMD=${CMAKE_SOURCE_DIR}/get-version.sh
- -P ${CMAKE_BINARY_DIR}/version.cmake
-)
-ADD_CUSTOM_TARGET(version ALL DEPENDS fake-file.c)
-
-################################################################################
-# CLIENT #
-################################################################################
-
-if(BUILD_KERNEL_MODULE)
- FILE(GLOB_RECURSE CLIENT_SRCS src/client/*.c)
- ADD_EXECUTABLE(dnbd3-client ${CMAKE_BINARY_DIR}/generated/version.c ${CLIENT_SRCS})
- TARGET_LINK_LIBRARIES(dnbd3-client)
- ADD_DEPENDENCIES(dnbd3-client version)
- INSTALL(TARGETS dnbd3-client RUNTIME DESTINATION sbin)
-ENDIF()
-
-
-################################################################################
-# SERVER #
-################################################################################
-
-if(BUILD_SERVER)
- IF(SERVER_FOR_AFL)
- message(" ######################## Building server for AFL mode - will be useless otherwise!")
- ADD_DEFINITIONS(-DAFL_MODE)
- ENDIF()
- FILE(GLOB SERVER_SRCS src/server/*.c src/shared/*.c src/server/picohttpparser/*.c)
- ADD_EXECUTABLE(dnbd3-server ${CMAKE_BINARY_DIR}/generated/version.c ${SERVER_SRCS})
- TARGET_INCLUDE_DIRECTORIES(dnbd3-server PRIVATE ${JANSSON_INCLUDE_DIR})
- TARGET_LINK_LIBRARIES(dnbd3-server ${CMAKE_THREAD_LIBS_INIT} ${JANSSON_LIBRARIES})
- if(BUILD_SERVER_FUSE)
- TARGET_LINK_LIBRARIES(dnbd3-server ${FUSE_LIBRARIES})
- ADD_DEFINITIONS(-DBUILD_SERVER_FUSE)
- TARGET_INCLUDE_DIRECTORIES(dnbd3-server PRIVATE ${FUSE_INCLUDE_DIRS})
- endif()
- if(UNIX AND NOT APPLE)
- target_link_libraries(dnbd3-server rt)
- endif()
- ADD_DEPENDENCIES(dnbd3-server version)
- INSTALL(TARGETS dnbd3-server RUNTIME DESTINATION sbin)
+cmake_minimum_required(VERSION 3.10)
+
+# include CMake macros
+set(PROJECT_MODULES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
+ ${PROJECT_MODULES_DIR})
+
+# define root CMake project
+project(dnbd3
+ DESCRIPTION "dnbd3 Linux kernel module, server, clients and utilities"
+ LANGUAGES C)
+
+# define project options to define build configuration
+OPTION(DNBD3_KERNEL_MODULE "Build the dnbd3 Linux kernel module" ON)
+OPTION(DNBD3_SERVER_FUSE "Enable FUSE-Integration for dnbd3-server" OFF)
+OPTION(DNBD3_SERVER_AFL "Build dnbd3-server for usage with afl-fuzz" OFF)
+OPTION(DNBD3_SERVER_DEBUG_LOCKS "Add lock debugging code to dnbd3-server" OFF)
+OPTION(DNBD3_SERVER_DEBUG_THREADS "Add thread debugging code to dnbd3-server" OFF)
+OPTION(DNBD3_RELEASE_HARDENING "Harden dnbd3 programs in Release build configuration" OFF)
+
+# set supported build configurations
+set(CMAKE_CONFIGURATION_TYPES Debug Release)
+
+# set compilation in debug mode as default configuration
+if(NOT CMAKE_BUILD_TYPE)
+ set(CMAKE_BUILD_TYPE Debug)
+ message(STATUS "Build type is not set. Defaulting to ${CMAKE_BUILD_TYPE} build!")
endif()
-
-
-################################################################################
-# FUSE #
-################################################################################
-
-if(BUILD_FUSE_CLIENT)
- FILE(GLOB FUSE_SRCS src/fuse/*.c src/shared/*.c)
- ADD_EXECUTABLE(dnbd3-fuse ${FUSE_SRCS})
- TARGET_INCLUDE_DIRECTORIES(dnbd3-fuse PRIVATE ${FUSE_INCLUDE_DIRS})
- TARGET_LINK_LIBRARIES(dnbd3-fuse ${CMAKE_THREAD_LIBS_INIT} ${FUSE_LIBRARIES})
- ADD_DEPENDENCIES(dnbd3-fuse version)
- INSTALL(TARGETS dnbd3-fuse RUNTIME DESTINATION bin)
-endif()
-
-################################################################################
-# STRESSTEST #
-################################################################################
-
-if(BUILD_STRESSTEST)
- FILE(GLOB BENCH_SRCS src/bench/*.c src/shared/*.c)
- ADD_EXECUTABLE(dnbd3-bench ${BENCH_SRCS})
- TARGET_LINK_LIBRARIES(dnbd3-bench ${CMAKE_THREAD_LIBS_INIT})
- ADD_DEPENDENCIES(dnbd3-bench version)
- INSTALL(TARGETS dnbd3-bench RUNTIME DESTINATION bin)
-endif()
-
-################################################################################
-# MODULE #
-################################################################################
-
-IF(BUILD_KERNEL_MODULE)
- SET(MODULE_NAME dnbd3)
- SET(MODULE_FILE ${MODULE_NAME}.ko)
- FILE(GLOB MODULE_SOURCE_FILES src/kernel/*.c src/serialize.c)
- FILE(GLOB MODULE_HEADER_FILES src/kernel/*.h src/*.h)
-
- SET(KERNEL_DIR "" CACHE PATH "Path to kernel sources to compile against")
- IF(KERNEL_DIR STREQUAL "")
- SET(KERNEL_DIR "/lib/modules/${CMAKE_SYSTEM_VERSION}/build")
- ENDIF()
-
- SET(KERNEL_C_FLAGS "" CACHE STRING "Additional C flags to be used for building the kernel module")
- IF(CMAKE_BUILD_TYPE MATCHES Debug)
- SET(KERNEL_C_FLAGS "-g -DDEBUG")
- ENDIF()
-
- SET(KBUILD_COMMAND ${CMAKE_MAKE_PROGRAM} -C ${KERNEL_DIR}
- M=${CMAKE_BINARY_DIR} modules
- EXTRA_CFLAGS=${KERNEL_C_FLAGS}
- )
-
- CONFIGURE_FILE(Kbuild.in ${CMAKE_BINARY_DIR}/Kbuild)
-
- FOREACH(MODULE_SOURCE_FILE ${MODULE_SOURCE_FILES})
- CONFIGURE_FILE(${MODULE_SOURCE_FILE} ${CMAKE_BINARY_DIR} COPYONLY)
- ENDFOREACH( MODULE_SOURCE_FILE )
-
- FOREACH(MODULE_HEADER_FILE ${MODULE_HEADER_FILES})
- CONFIGURE_FILE(${MODULE_HEADER_FILE} ${CMAKE_BINARY_DIR} COPYONLY)
- ENDFOREACH( MODULE_HEADER_FILE )
-
- ADD_CUSTOM_COMMAND(
- OUTPUT ${CMAKE_BINARY_DIR}/${MODULE_FILE}
- COMMAND ${KBUILD_COMMAND}
- WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
- DEPENDS ${MODULE_SOURCE_FILES} Kbuild.in
- VERBATIM
- )
-
- ADD_CUSTOM_TARGET(${MODULE_NAME} ALL DEPENDS ${CMAKE_BINARY_DIR}/${MODULE_FILE})
-
- INSTALL(FILES ${CMAKE_BINARY_DIR}/${MODULE_NAME}.ko
- DESTINATION /lib/modules/${CMAKE_SYSTEM_VERSION}/kernel/drivers/block
- PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
- )
-
- INSTALL(CODE "EXECUTE_PROCESS(COMMAND depmod -a)")
-ENDIF()
-
-
-#
-# Other install files
-#
-
-FILE(GLOB conf_files "${CMAKE_CURRENT_SOURCE_DIR}/conf/*")
-INSTALL(FILES ${conf_files} DESTINATION /etc/dnbd3-server/sample/)
-
+# disable build of the dnbd3 Linux kernel module on a BSD system
+if(CMAKE_SYSTEM_NAME MATCHES "BSD")
+ message(STATUS "Detected BSD System: Disable build of the dnbd3 Linux kernel module")
+ set(DNBD3_KERNEL_MODULE OFF)
+endif(CMAKE_SYSTEM_NAME MATCHES "BSD")
+
+# set Linux kernel directories
+set(KERNEL_BUILD_DIR "/lib/modules/${CMAKE_SYSTEM_VERSION}/build"
+ CACHE PATH "Path to Linux kernel modules to compile against")
+set(KERNEL_INSTALL_DIR "/lib/modules/${CMAKE_SYSTEM_VERSION}/extra"
+ CACHE PATH "Path to install Linux kernel modules")
+
+# search for required packages
+find_package(Git)
+find_package(Threads)
+find_package(Fuse)
+
+# abort if a required package is not available
+if(NOT GIT_FOUND)
+ message(FATAL_ERROR "No Git found, can't determine dnbd3 project version number!")
+endif(NOT GIT_FOUND)
+if(NOT THREADS_FOUND)
+ message(FATAL_ERROR "No threads found, can't build dnbd3 project!")
+endif(NOT THREADS_FOUND)
+if(NOT FUSE_FOUND)
+ message(FATAL_ERROR "No Fuse found, can't build dnbd3 project!")
+endif(NOT FUSE_FOUND)
+
+# include project version related macros
+include(KernelVersion)
+# include project version related macros
+include(ProjectVersion)
+
+# get Linux kernel version from path
+get_kernel_version(LINUX_KERNEL_VERSION ${KERNEL_BUILD_DIR})
+
+# set include directories
+set(PROJECT_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/inc)
+set(PROJECT_INCLUDE_TMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/inc)
+include_directories(${PROJECT_INCLUDE_DIR})
+
+# generate project version C header file from template
+# exposes dnbd3-generate-version and dnbd3-version target
+gen_project_version(${PROJECT_INCLUDE_DIR}/dnbd3/version.h.in ${PROJECT_INCLUDE_TMP_DIR}/dnbd3/version.h)
+
+# print configured settings
+message(STATUS "Path to Linux kernel modules to compile against is " ${KERNEL_BUILD_DIR})
+message(STATUS "Path to install Linux kernel modules is " ${KERNEL_INSTALL_DIR})
+
+# add compile option to handle files greater than 2GB on a 32bit system
+add_definitions(-D_FILE_OFFSET_BITS=64)
+
+# define global C flags for compilation
+set(CMAKE_C_FLAGS "-std=c11")
+
+# enable all error warnings in Debug build configuration
+set(CMAKE_C_FLAGS_DEBUG "-Wall -Wextra -Wpedantic -Wconversion -Wformat -Wformat-security -Werror=format-security")
+set(CMAKE_C_FLAGS_RELEASE "-Wno-error")
+
+# set compilation optimization
+set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -Og -DDEBUG")
+set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -DNDEBUG")
+
+if(DNBD3_RELEASE_HARDENING)
+ # harden Release builds with specific C flags
+ set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -D_FORTIFY_SOURCE=2 -fstack-protector-strong -fstack-clash-protection -mfunction-return=thunk -mindirect-branch=thunk")
+endif(DNBD3_RELEASE_HARDENING)
+
+# add all dnbd3 related projects from the source code directory
+add_subdirectory(src)
+
+# install config files into sample directory
+file(GLOB DNBD3_CONF_FILES "${CMAKE_CURRENT_SOURCE_DIR}/conf/*")
+install(FILES ${DNBD3_CONF_FILES} DESTINATION /etc/dnbd3-server/sample)
diff --git a/Kbuild.in b/Kbuild.in
deleted file mode 100644
index 667cee0..0000000
--- a/Kbuild.in
+++ /dev/null
@@ -1,2 +0,0 @@
-obj-m := ${MODULE_NAME}.o
-${MODULE_NAME}-objs += core.o blk.o net.o sysfs.o utils.o serialize_kmod.o \ No newline at end of file
diff --git a/build.sh b/build.sh
deleted file mode 100755
index 6726a86..0000000
--- a/build.sh
+++ /dev/null
@@ -1,6 +0,0 @@
-#!/bin/sh
-
-mkdir -p build
-cd build/
-cmake ..
-make
diff --git a/cmake/GenerateVersion.cmake b/cmake/GenerateVersion.cmake
new file mode 100644
index 0000000..e336489
--- /dev/null
+++ b/cmake/GenerateVersion.cmake
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Copyright (C) 2020 Manuel Bentele <development@manuel-bentele.de>
+#
+
+# get Git short hash and tag of latest repository commit
+execute_process(COMMAND git describe HEAD
+ OUTPUT_VARIABLE DNBD3_VERSION
+ OUTPUT_STRIP_TRAILING_WHITESPACE)
+if(DNBD3_VERSION STREQUAL "")
+ set(DNBD3_VERSION "unknown")
+endif(DNBD3_VERSION STREQUAL "")
+
+# write dnbd3 version into a new C source file based on the specified version template
+configure_file(${VERSION_INPUT_FILE} ${VERSION_OUTPUT_FILE})
diff --git a/cmake/Kernel.cmake b/cmake/Kernel.cmake
new file mode 100644
index 0000000..232c298
--- /dev/null
+++ b/cmake/Kernel.cmake
@@ -0,0 +1,57 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# CMake macros to build and install Linux kernel modules
+# Copyright (C) 2020 Manuel Bentele <development@manuel-bentele.de>
+#
+
+# macro to define kernel module targets
+macro(add_kernel_module MODULE_NAME KERNEL_BUILD_DIR KERNEL_INSTALL_DIR MODULE_MACRO MODULE_SOURCE_FILES MODULE_HEADER_FILES BUILD_SOURCE_FILE)
+ # create directory for kernel module
+ file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME})
+ # copy build source file
+ get_filename_component(BUILD_SOURCE_FILENAME ${BUILD_SOURCE_FILE} NAME)
+ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}/${BUILD_SOURCE_FILENAME}
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different ${BUILD_SOURCE_FILE} ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}
+ DEPENDS ${BUILD_SOURCE_FILE})
+ set(BUILD_SOURCE_FILE_PREPARED ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}/${BUILD_SOURCE_FILENAME})
+ # copy source files
+ foreach(MODULE_SOURCE_FILE ${MODULE_SOURCE_FILES})
+ get_filename_component(MODULE_SOURCE_FILENAME ${MODULE_SOURCE_FILE} NAME)
+ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}/${MODULE_SOURCE_FILENAME}
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different ${MODULE_SOURCE_FILE} ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}
+ DEPENDS ${MODULE_SOURCE_FILE})
+ set(MODULE_SOURCE_FILES_PREPARED ${MODULE_SOURCE_FILES_PREPARED}
+ ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}/${MODULE_SOURCE_FILENAME})
+ endforeach()
+ # copy header files
+ foreach(MODULE_HEADER_FILE ${MODULE_HEADER_FILES})
+ get_filename_component(MODULE_HEADER_FILENAME ${MODULE_HEADER_FILE} NAME)
+ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}/${MODULE_HEADER_FILENAME}
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different ${MODULE_HEADER_FILE} ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}
+ DEPENDS ${MODULE_HEADER_FILE})
+ set(MODULE_HEADER_FILES_PREPARED ${MODULE_HEADER_FILES_PREPARED}
+ ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}/${MODULE_HEADER_FILENAME})
+ endforeach()
+ # check if module depends on another module
+ if(NOT ${ARGV7} STREQUAL "")
+ set(MODULE_EXTRA_SYMBOLS ${CMAKE_CURRENT_BINARY_DIR}/${ARGV7}/Module.symvers)
+ endif()
+ # define build command
+ set(MODULE_BUILD_COMMAND ${CMAKE_MAKE_PROGRAM} ${MODULE_MACRO}
+ -C ${KERNEL_BUILD_DIR}
+ M=${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME} modules
+ EXTRA_CFLAGS=${KERNEL_C_FLAGS}
+ KBUILD_EXTRA_SYMBOLS=${MODULE_EXTRA_SYMBOLS})
+ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}/${MODULE_NAME}.ko
+ COMMAND ${MODULE_BUILD_COMMAND}
+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}
+ COMMENT "Build kernel module ${MODULE_NAME}"
+ DEPENDS ${BUILD_SOURCE_FILE_PREPARED} ${MODULE_HEADER_FILES_PREPARED} ${MODULE_SOURCE_FILES_PREPARED}
+ VERBATIM)
+ add_custom_target(${MODULE_NAME} ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}/${MODULE_NAME}.ko ${ARGV7})
+ # install kernel module
+ install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${MODULE_NAME}/${MODULE_NAME}.ko
+ DESTINATION ${KERNEL_INSTALL_DIR}
+ PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
+ COMPONENT kernel)
+endmacro(add_kernel_module)
diff --git a/cmake/KernelVersion.cmake b/cmake/KernelVersion.cmake
new file mode 100644
index 0000000..1ded072
--- /dev/null
+++ b/cmake/KernelVersion.cmake
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Copyright (C) 2020 Manuel Bentele <development@manuel-bentele.de>
+#
+
+# get Linux kernel version from KERNEL_BUILD_DIR string
+macro(get_kernel_version LINUX_KERNEL_VERSION KERNEL_BUILD_DIR)
+ string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" LINUX_KERNEL_VERSION ${KERNEL_BUILD_DIR})
+ if(LINUX_KERNEL_VERSION STREQUAL "")
+ set(LINUX_KERNEL_VERSION "unknown")
+ endif(LINUX_KERNEL_VERSION STREQUAL "")
+endmacro(get_kernel_version LINUX_KERNEL_VERSION KERNEL_BUILD_DIR)
diff --git a/cmake/ProjectVersion.cmake b/cmake/ProjectVersion.cmake
new file mode 100644
index 0000000..108505d
--- /dev/null
+++ b/cmake/ProjectVersion.cmake
@@ -0,0 +1,27 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Copyright (C) 2020 Manuel Bentele <development@manuel-bentele.de>
+#
+
+macro(gen_project_version VERSION_INPUT_FILE VERSION_OUTPUT_FILE)
+ get_filename_component(VERSION_OUTPUT_FILENAME ${VERSION_OUTPUT_FILE} NAME)
+ # command that will trigger a rebuild of version.c every time
+ add_custom_command(OUTPUT regenerate-version-file
+ COMMAND ${CMAKE_COMMAND} -E sleep 0
+ COMMENT "Trigger generating ${VERSION_OUTPUT_FILENAME}")
+
+ # call the GenerateVersion.cmake file to generate the version.c file
+ add_custom_command(OUTPUT ${VERSION_OUTPUT_FILE}
+ COMMAND ${CMAKE_COMMAND} -D VERSION_INPUT_FILE=${VERSION_INPUT_FILE}
+ -D VERSION_OUTPUT_FILE=${VERSION_OUTPUT_FILE}
+ -P ${PROJECT_MODULES_DIR}/GenerateVersion.cmake
+ COMMENT "Generating ${VERSION_OUTPUT_FILENAME}"
+ DEPENDS regenerate-version-file)
+ add_custom_target(dnbd3-generate-version DEPENDS ${VERSION_OUTPUT_FILE})
+
+ # create target to expose project version
+ add_library(dnbd3-version INTERFACE)
+ target_include_directories(dnbd3-version INTERFACE ${PROJECT_INCLUDE_TMP_DIR})
+ add_dependencies(dnbd3-version dnbd3-generate-version)
+
+endmacro(gen_project_version VERSION_INPUT_FILE VERSION_OUTPUT_FILE)
diff --git a/get-version.sh b/get-version.sh
deleted file mode 100755
index 49c7024..0000000
--- a/get-version.sh
+++ /dev/null
@@ -1,24 +0,0 @@
-#!/bin/sh
-
-# Always create version string for repository this script lies in,
-# not the cwd... Makes usage easier in cmake
-ARG0="$0"
-SELF="$(readlink -f "${ARG0}")"
-ROOT_DIR="$(dirname "${SELF}")"
-cd "$ROOT_DIR"
-
-if [ -d .git ]; then
- [ -n "$(git diff HEAD)" ] && MODDED='+MOD'
- name="$( git describe )"
- [ -z "$name" ] && name="$( git rev-parse --short HEAD )"
- printf "%s\n" "$name$MODDED, branch $(git rev-parse --abbrev-ref HEAD), built $(date +%Y-%m-%d)"
- exit 0
-fi
-
-if [ -f "version.txt" ]; then
- cat "version.txt"
- exit 0
-fi
-
-echo "-unknown-"
-
diff --git a/src/config.h b/inc/dnbd3/config.h
index 50336af..50336af 100644
--- a/src/config.h
+++ b/inc/dnbd3/config.h
diff --git a/src/clientconfig.h b/inc/dnbd3/config/client.h
index f35f673..f35f673 100644
--- a/src/clientconfig.h
+++ b/inc/dnbd3/config/client.h
diff --git a/src/serverconfig.h b/inc/dnbd3/config/server.h
index 31708de..b6eee2c 100644
--- a/src/serverconfig.h
+++ b/inc/dnbd3/config/server.h
@@ -1,7 +1,7 @@
#ifndef _SERVERCONFIG_H_
#define _SERVERCONFIG_H_
-#include "config.h"
+#include <dnbd3/config.h>
// +++++ Performance/memory related
#define SERVER_MAX_CLIENTS 4000
diff --git a/src/shared/crc32.h b/inc/dnbd3/shared/crc32.h
index 00b8bdd..00b8bdd 100644
--- a/src/shared/crc32.h
+++ b/inc/dnbd3/shared/crc32.h
diff --git a/src/shared/fdsignal.h b/inc/dnbd3/shared/fdsignal.h
index 960a2a9..960a2a9 100644
--- a/src/shared/fdsignal.h
+++ b/inc/dnbd3/shared/fdsignal.h
diff --git a/src/shared/log.h b/inc/dnbd3/shared/log.h
index 5b1e8f7..5b1e8f7 100644
--- a/src/shared/log.h
+++ b/inc/dnbd3/shared/log.h
diff --git a/src/shared/protocol.h b/inc/dnbd3/shared/protocol.h
index 05fd2bf..1dd47f8 100644
--- a/src/shared/protocol.h
+++ b/inc/dnbd3/shared/protocol.h
@@ -1,10 +1,9 @@
#ifndef _PROTOCOL_H_
#define _PROTOCOL_H_
-#include "sockhelper.h"
-
-#include "../types.h"
-#include "../serialize.h"
+#include <dnbd3/types.h>
+#include <dnbd3/shared/serialize.h>
+#include <dnbd3/shared/sockhelper.h>
#include <errno.h>
#include <sys/types.h>
diff --git a/src/serialize.h b/inc/dnbd3/shared/serialize.h
index 1b73531..a7a1ced 100644
--- a/src/serialize.h
+++ b/inc/dnbd3/shared/serialize.h
@@ -1,8 +1,8 @@
#ifndef SERIALIZER_H_
#define SERIALIZER_H_
-// Careful with includes - this is used in kernel module too
-#include "config.h"
+#include <dnbd3/config.h>
+#include <dnbd3/types.h>
typedef struct
{
diff --git a/src/shared/sockhelper.h b/inc/dnbd3/shared/sockhelper.h
index 8d70789..5c7d903 100644
--- a/src/shared/sockhelper.h
+++ b/inc/dnbd3/shared/sockhelper.h
@@ -6,7 +6,7 @@
* abstract from the IP version by using getaddrinfo() and thelike.
*/
-#include "../types.h"
+#include <dnbd3/types.h>
#include <stdint.h>
#include <sys/socket.h>
#include <string.h>
diff --git a/src/shared/timing.h b/inc/dnbd3/shared/timing.h
index f23bfeb..f23bfeb 100644
--- a/src/shared/timing.h
+++ b/inc/dnbd3/shared/timing.h
diff --git a/src/types.h b/inc/dnbd3/types.h
index 83416f4..c7f335b 100644
--- a/src/types.h
+++ b/inc/dnbd3/types.h
@@ -21,10 +21,15 @@
#ifndef TYPES_H_
#define TYPES_H_
-#include "config.h"
-#ifndef KERNEL_MODULE
+#include <dnbd3/config.h>
+#ifdef DNBD3_KERNEL_MODULE
+#include <linux/kernel.h>
+#include <linux/string.h>
+#else
#include <stdint.h>
#include <stdbool.h>
+#include <stddef.h>
+#include <string.h>
#endif
#ifndef MIN
@@ -65,7 +70,7 @@
#include <netinet/in.h>
#endif
-#ifdef AFL_MODE
+#ifdef DNBD3_SERVER_AFL
#define send(a,b,c,d) write(a,b,c)
#define recv(a,b,c,d) read(a,b,c)
#endif
diff --git a/inc/dnbd3/version.h.in b/inc/dnbd3/version.h.in
new file mode 100644
index 0000000..4a88cf0
--- /dev/null
+++ b/inc/dnbd3/version.h.in
@@ -0,0 +1,9 @@
+/*
+ * AUTOGENERATED: DO NOT EDIT THIS FILE
+ */
+#ifndef VERSION_H_
+#define VERSION_H_
+
+#define DNBD3_VERSION "@DNBD3_VERSION@"
+
+#endif /* VERSION_H_ */
diff --git a/pack.sh b/pack.sh
deleted file mode 100755
index 9cbe5c4..0000000
--- a/pack.sh
+++ /dev/null
@@ -1,6 +0,0 @@
-#!/bin/sh
-
-./get-version.sh > version.txt
-tar ckzf dnbd3.tar.gz src cmake CMakeLists.txt get-version.sh version.txt
-rm -- version.txt
-
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644
index 0000000..6093164
--- /dev/null
+++ b/src/CMakeLists.txt
@@ -0,0 +1,13 @@
+cmake_minimum_required(VERSION 3.10)
+
+# set the project name
+project(dnbd3-src)
+
+add_subdirectory(bench)
+add_subdirectory(client)
+add_subdirectory(fuse)
+if(DNBD3_KERNEL_MODULE)
+ add_subdirectory(kernel)
+endif(DNBD3_KERNEL_MODULE)
+add_subdirectory(server)
+add_subdirectory(shared)
diff --git a/src/bench/CMakeLists.txt b/src/bench/CMakeLists.txt
new file mode 100644
index 0000000..d8a5dc9
--- /dev/null
+++ b/src/bench/CMakeLists.txt
@@ -0,0 +1,13 @@
+cmake_minimum_required(VERSION 3.10)
+
+# set the project name
+project(dnbd3-bench)
+
+# add compile option to enable enhanced POSIX pthread features
+add_compile_options(-D_GNU_SOURCE)
+
+add_executable(dnbd3-bench ${CMAKE_CURRENT_SOURCE_DIR}/connection.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/helper.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/main.c)
+target_link_libraries(dnbd3-bench dnbd3-version dnbd3-shared ${CMAKE_THREAD_LIBS_INIT})
+install(TARGETS dnbd3-bench RUNTIME DESTINATION bin)
diff --git a/src/bench/connection.c b/src/bench/connection.c
index 65f1757..974bc8a 100644
--- a/src/bench/connection.c
+++ b/src/bench/connection.c
@@ -1,10 +1,10 @@
#include "connection.h"
#include "helper.h"
-#include "../config.h"
-#include "../shared/protocol.h"
-#include "../shared/fdsignal.h"
-#include "../shared/sockhelper.h"
-#include "../shared/log.h"
+#include <dnbd3/config.h>
+#include <dnbd3/shared/protocol.h>
+#include <dnbd3/shared/fdsignal.h>
+#include <dnbd3/shared/sockhelper.h>
+#include <dnbd3/shared/log.h>
#include <stdlib.h>
#include <pthread.h>
diff --git a/src/bench/connection.h b/src/bench/connection.h
index 770bf0d..422c93e 100644
--- a/src/bench/connection.h
+++ b/src/bench/connection.h
@@ -1,7 +1,7 @@
#ifndef _CONNECTION_H_
#define _CONNECTION_H_
-#include "../shared/fdsignal.h"
+#include <dnbd3/shared/fdsignal.h>
#include <stdbool.h>
#include <stdint.h>
#include "helper.h"
diff --git a/src/bench/helper.h b/src/bench/helper.h
index e0c0262..53f32bf 100644
--- a/src/bench/helper.h
+++ b/src/bench/helper.h
@@ -1,7 +1,7 @@
#ifndef IMAGEHELPER_H
#define IMAGEHELPER_H
-#include "../types.h"
+#include <dnbd3/types.h>
#include <netdb.h>
#include <stdbool.h>
diff --git a/src/bench/main.c b/src/bench/main.c
index f8c55c3..89c09f3 100644
--- a/src/bench/main.c
+++ b/src/bench/main.c
@@ -4,8 +4,9 @@
#include "connection.h"
#include "helper.h"
-#include "../shared/protocol.h"
-#include "../shared/log.h"
+#include <dnbd3/shared/protocol.h>
+#include <dnbd3/shared/log.h>
+#include <dnbd3/version.h>
#include <stdio.h>
#include <stdlib.h>
@@ -19,6 +20,7 @@
static void printUsage(char *argv0, int exitCode)
{
+ printf( "Version: %s\n", DNBD3_VERSION );
printf( "Usage: %s [--debug] --host <serverAddress(es)> --image <imageName> [--rid revision]\n", argv0 );
printf( "Or: %s [-d] -h <serverAddress(es)> -i <imageName> [-r revision]\n", argv0 );
printf( " -h --host List of space separated hosts to use\n" );
diff --git a/src/bench/serialize.c b/src/bench/serialize.c
deleted file mode 100644
index 4934132..0000000
--- a/src/bench/serialize.c
+++ /dev/null
@@ -1,5 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include <stdint.h>
-
-#include "../serialize.c"
diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt
new file mode 100644
index 0000000..a7c1eb4
--- /dev/null
+++ b/src/client/CMakeLists.txt
@@ -0,0 +1,14 @@
+cmake_minimum_required(VERSION 3.10)
+
+# set the project name
+project(dnbd3-client)
+
+# suppress -Wmultichar warnings
+add_compile_options(-Wno-multichar)
+
+# add compile option to enable enhanced BSD netdb features
+add_compile_options(-D_GNU_SOURCE)
+
+add_executable(dnbd3-client ${CMAKE_CURRENT_SOURCE_DIR}/client.c)
+target_link_libraries(dnbd3-client dnbd3-version dnbd3-shared)
+install(TARGETS dnbd3-client RUNTIME DESTINATION bin)
diff --git a/src/client/client.c b/src/client/client.c
index 37f0558..33737fa 100644
--- a/src/client/client.c
+++ b/src/client/client.c
@@ -18,9 +18,9 @@
*
*/
-#include "../clientconfig.h"
-#include "../types.h"
-#include "../version.h"
+#include <dnbd3/config/client.h>
+#include <dnbd3/types.h>
+#include <dnbd3/version.h>
#include <stdio.h>
#include <stdlib.h>
@@ -643,7 +643,7 @@ static int dnbd3_daemon_send(int argc, char **argv)
static void dnbd3_print_help(char *argv_0)
{
- printf( "Version: %s\n\n", VERSION_STRING );
+ printf( "Version: %s\n", DNBD3_VERSION );
printf( "\nUsage: %s\n"
"\t-h <host> -i <image name> [-r <rid>] -d <device> [-a <KB>] || -c -d <device>\n\n", argv_0 );
printf( "Start the DNBD3 client.\n" );
@@ -665,6 +665,6 @@ static void dnbd3_print_help(char *argv_0)
void dnbd3_print_version()
{
- printf( "Version: %s\n", VERSION_STRING );
+ printf( "dnbd3-client version: %s\n", DNBD3_VERSION );
exit( EXIT_SUCCESS );
}
diff --git a/src/fuse/CMakeLists.txt b/src/fuse/CMakeLists.txt
new file mode 100644
index 0000000..367356a
--- /dev/null
+++ b/src/fuse/CMakeLists.txt
@@ -0,0 +1,14 @@
+cmake_minimum_required(VERSION 3.10)
+
+# set the project name
+project(dnbd3-fuse)
+
+# add compile option to enable enhanced POSIX pthread features
+add_compile_options(-D_GNU_SOURCE)
+
+add_executable(dnbd3-fuse ${CMAKE_CURRENT_SOURCE_DIR}/connection.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/helper.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/main.c)
+target_include_directories(dnbd3-fuse PRIVATE ${FUSE_INCLUDE_DIRS})
+target_link_libraries(dnbd3-fuse dnbd3-version dnbd3-shared ${FUSE_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
+install(TARGETS dnbd3-fuse RUNTIME DESTINATION bin)
diff --git a/src/fuse/connection.c b/src/fuse/connection.c
index 7bd8018..25ea219 100644
--- a/src/fuse/connection.c
+++ b/src/fuse/connection.c
@@ -1,10 +1,10 @@
#include "connection.h"
#include "helper.h"
-#include "../clientconfig.h"
-#include "../shared/protocol.h"
-#include "../shared/fdsignal.h"
-#include "../shared/sockhelper.h"
-#include "../shared/log.h"
+#include <dnbd3/config/client.h>
+#include <dnbd3/shared/protocol.h>
+#include <dnbd3/shared/fdsignal.h>
+#include <dnbd3/shared/sockhelper.h>
+#include <dnbd3/shared/log.h>
#include <stdlib.h>
#include <pthread.h>
diff --git a/src/fuse/connection.h b/src/fuse/connection.h
index be0115e..dde6da9 100644
--- a/src/fuse/connection.h
+++ b/src/fuse/connection.h
@@ -1,8 +1,8 @@
#ifndef _CONNECTION_H_
#define _CONNECTION_H_
-#include "../shared/fdsignal.h"
-#include "../shared/timing.h"
+#include <dnbd3/shared/fdsignal.h>
+#include <dnbd3/shared/timing.h>
#include <stdatomic.h>
#include <stddef.h>
#include <stdbool.h>
diff --git a/src/fuse/helper.h b/src/fuse/helper.h
index 65cca2c..b1fa513 100644
--- a/src/fuse/helper.h
+++ b/src/fuse/helper.h
@@ -1,7 +1,7 @@
#ifndef IMAGEHELPER_H
#define IMAGEHELPER_H
-#include "../types.h"
+#include <dnbd3/types.h>
#include <netdb.h>
#include <stdbool.h>
diff --git a/src/fuse/main.c b/src/fuse/main.c
index f303c9c..cf9efbe 100644
--- a/src/fuse/main.c
+++ b/src/fuse/main.c
@@ -10,11 +10,12 @@
#include "connection.h"
#include "helper.h"
-#include "../shared/protocol.h"
-#include "../shared/log.h"
+#include <dnbd3/version.h>
+#include <dnbd3/shared/protocol.h>
+#include <dnbd3/shared/log.h>
#define FUSE_USE_VERSION 30
-#include "../config.h"
+#include <dnbd3/config.h>
#include <fuse_lowlevel.h>
#include <stdio.h>
#include <stdlib.h>
@@ -272,7 +273,8 @@ static struct fuse_lowlevel_ops image_oper = {
static void printVersion()
{
char *arg[] = { "foo", "-V" };
- printf( "DNBD3-Fuse Version 1.2.3.4, protocol version %d\n", (int)PROTOCOL_VERSION );
+ printf( "dnbd3-fuse version: %s\n", DNBD3_VERSION );
+ printf( "Protocol version: %d\n", (int)PROTOCOL_VERSION );
struct fuse_args args = FUSE_ARGS_INIT( 2, arg );
fuse_parse_cmdline( &args, NULL, NULL, NULL );
exit( 0 );
diff --git a/src/fuse/serialize.c b/src/fuse/serialize.c
deleted file mode 100644
index 4934132..0000000
--- a/src/fuse/serialize.c
+++ /dev/null
@@ -1,5 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include <stdint.h>
-
-#include "../serialize.c"
diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt
new file mode 100644
index 0000000..bf1eb3a
--- /dev/null
+++ b/src/kernel/CMakeLists.txt
@@ -0,0 +1,48 @@
+cmake_minimum_required(VERSION 3.10)
+
+# set the project name
+project(dnbd3-kernel-module)
+
+# include macros to define Linux kernel build targets
+include(Kernel)
+
+# set C flags for a Linux kernel module
+set(KERNEL_C_FLAGS "-DDNBD3_KERNEL_MODULE -I ${PROJECT_INCLUDE_TMP_DIR}"
+ CACHE STRING "C flags to be used for building the dnbd3 kernel module")
+# set C flags for the debug mode of a Linux kernel module
+set(KERNEL_C_FLAGS_DEBUG "-g -DDEBUG"
+ CACHE STRING "Additional C flags to be used for building the dnbd3 kernel module in debug mode")
+
+# append include directories to the C flags
+get_property(KERNEL_INCLUDE_DIRS DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
+foreach(KERNEL_INCLUDE_DIR ${KERNEL_INCLUDE_DIRS})
+ set(KERNEL_C_FLAGS "${KERNEL_C_FLAGS} -I ${KERNEL_INCLUDE_DIR}")
+endforeach(KERNEL_INCLUDE_DIR ${KERNEL_INCLUDE_DIRS})
+
+# append debug C flags if debug mode is enabled
+if(CMAKE_BUILD_TYPE MATCHES Debug)
+ set(KERNEL_C_FLAGS "${KERNEL_C_FLAGS} ${KERNEL_C_FLAGS_DEBUG}")
+endif(CMAKE_BUILD_TYPE MATCHES Debug)
+
+# dnbd3 Linux kernel module
+set(KERNEL_MODULE_DNBD3_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/blk.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/dnbd3_main.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/net.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/serialize.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/sysfs.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/utils.c
+ ${PROJECT_VERSION_FILE})
+set(KERNEL_MODULE_DNBD3_HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/blk.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/dnbd3_main.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/net.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/sysfs.h
+ ${CMAKE_CURRENT_SOURCE_DIR}/utils.h)
+add_kernel_module(dnbd3 "${KERNEL_BUILD_DIR}"
+ "${KERNEL_INSTALL_DIR}"
+ "CONFIG_BLK_DEV_DNBD3=m"
+ "${KERNEL_MODULE_DNBD3_SOURCE_FILES}"
+ "${KERNEL_MODULE_DNBD3_HEADER_FILES}"
+ ${CMAKE_CURRENT_SOURCE_DIR}/Kbuild)
+
+# add dependency to generate ${PROJECT_VERSION_FILE} before dnbd3.ko is built
+add_dependencies(dnbd3 dnbd3-generate-version)
diff --git a/src/kernel/Kbuild b/src/kernel/Kbuild
new file mode 100644
index 0000000..385a5ff
--- /dev/null
+++ b/src/kernel/Kbuild
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0
+
+# Linux kernel module dnbd3
+obj-$(CONFIG_BLK_DEV_DNBD3) := dnbd3.o
+dnbd3-y += dnbd3_main.o blk.o net.o serialize.o sysfs.o utils.o
diff --git a/src/kernel/blk.c b/src/kernel/blk.c
index 2c46b67..00c3f8f 100644
--- a/src/kernel/blk.c
+++ b/src/kernel/blk.c
@@ -18,7 +18,7 @@
*
*/
-#include "clientconfig.h"
+#include <dnbd3/config/client.h>
#include "blk.h"
#include "net.h"
#include "sysfs.h"
diff --git a/src/kernel/blk.h b/src/kernel/blk.h
index 3377406..5f7f2db 100644
--- a/src/kernel/blk.h
+++ b/src/kernel/blk.h
@@ -21,7 +21,7 @@
#ifndef BLK_H_
#define BLK_H_
-#include "dnbd3.h"
+#include "dnbd3_main.h"
#define REQ_TYPE_SPECIAL REQ_TYPE_DRV_PRIV
diff --git a/src/kernel/core.c b/src/kernel/dnbd3_main.c
index 6e7d052..27e8eed 100644
--- a/src/kernel/core.c
+++ b/src/kernel/dnbd3_main.c
@@ -20,8 +20,9 @@
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
-#include "clientconfig.h"
-#include "dnbd3.h"
+#include <dnbd3/config/client.h>
+#include <dnbd3/version.h>
+#include "dnbd3_main.h"
#include "blk.h"
int major;
@@ -48,7 +49,7 @@ static int __init dnbd3_init(void)
return -EIO;
}
- pr_info("kernel module loaded\n");
+ pr_info("kernel module in version %s loaded\n", DNBD3_VERSION);
pr_debug("machine type " ENDIAN_MODE "\n");
// add MAX_NUMBER_DEVICES devices
@@ -86,6 +87,7 @@ module_exit(dnbd3_exit);
MODULE_DESCRIPTION("Distributed Network Block Device 3");
MODULE_LICENSE("GPL");
+MODULE_VERSION(DNBD3_VERSION);
module_param(max_devs, int, 0444);
MODULE_PARM_DESC(max_devs, "number of network block devices to initialize (default: 8)");
diff --git a/src/kernel/dnbd3.h b/src/kernel/dnbd3_main.h
index 4e06d70..124426b 100644
--- a/src/kernel/dnbd3.h
+++ b/src/kernel/dnbd3_main.h
@@ -28,10 +28,9 @@
#include <linux/blk-mq.h>
#include <net/sock.h>
-#define KERNEL_MODULE
-#include "config.h"
-#include "types.h"
-#include "serialize.h"
+#include <dnbd3/config.h>
+#include <dnbd3/types.h>
+#include <dnbd3/shared/serialize.h>
extern int major;
diff --git a/src/kernel/net.c b/src/kernel/net.c
index b387c1d..46c369a 100644
--- a/src/kernel/net.c
+++ b/src/kernel/net.c
@@ -18,12 +18,12 @@
*
*/
-#include "clientconfig.h"
+#include <dnbd3/config/client.h>
#include "net.h"
#include "blk.h"
#include "utils.h"
-#include "serialize.h"
+#include <dnbd3/shared/serialize.h>
#include <linux/time.h>
#include <linux/ktime.h>
diff --git a/src/kernel/net.h b/src/kernel/net.h
index 8a2bc22..e7accd0 100644
--- a/src/kernel/net.h
+++ b/src/kernel/net.h
@@ -21,7 +21,7 @@
#ifndef NET_H_
#define NET_H_
-#include "dnbd3.h"
+#include "dnbd3_main.h"
#define init_msghdr(h) do { \
h.msg_name = NULL; \
diff --git a/src/kernel/serialize.c b/src/kernel/serialize.c
new file mode 120000
index 0000000..5a4e4ac
--- /dev/null
+++ b/src/kernel/serialize.c
@@ -0,0 +1 @@
+../shared/serialize.c \ No newline at end of file
diff --git a/src/kernel/serialize_kmod.c b/src/kernel/serialize_kmod.c
deleted file mode 100644
index 50746df..0000000
--- a/src/kernel/serialize_kmod.c
+++ /dev/null
@@ -1,5 +0,0 @@
-#include <linux/kernel.h>
-#include <linux/string.h>
-
-#define KERNEL_MODULE
-#include "serialize.c"
diff --git a/src/kernel/sysfs.h b/src/kernel/sysfs.h
index 0a747a5..a90840b 100644
--- a/src/kernel/sysfs.h
+++ b/src/kernel/sysfs.h
@@ -21,7 +21,7 @@
#ifndef SYSFS_H_
#define SYSFS_H_
-#include "dnbd3.h"
+#include "dnbd3_main.h"
void dnbd3_sysfs_init(dnbd3_device_t *dev);
diff --git a/src/server/CMakeLists.txt b/src/server/CMakeLists.txt
new file mode 100644
index 0000000..47dda53
--- /dev/null
+++ b/src/server/CMakeLists.txt
@@ -0,0 +1,66 @@
+cmake_minimum_required(VERSION 3.10)
+
+# set the project name
+project(dnbd3-server)
+
+# find Jansson package required by the dnbd3-server
+find_package(Jansson)
+if(NOT JANSSON_FOUND)
+ message(FATAL_ERROR "*** No jansson lib found, can't build dnbd3-server!")
+endif(NOT JANSSON_FOUND)
+
+# add compile option to enable enhanced POSIX features
+add_definitions(-D_GNU_SOURCE)
+
+# export project build type to source code
+add_definitions(-DBUILD_TYPE=${CMAKE_BUILD_TYPE})
+
+if(DNBD3_SERVER_AFL)
+ # build dnbd3-server with AFL support
+ message(STATUS "Building dnbd3-server with AFL support")
+ add_definitions(-DDNBD3_SERVER_AFL)
+endif(DNBD3_SERVER_AFL)
+
+add_executable(dnbd3-server ${CMAKE_CURRENT_SOURCE_DIR}/altservers.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/fileutil.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/fuse.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/globals.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/helper.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/image.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/ini.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/integrity.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/locks.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/net.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/picohttpparser/picohttpparser.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/reference.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/rpc.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/server.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/threadpool.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/uplink.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/urldecode.c)
+target_include_directories(dnbd3-server PRIVATE ${JANSSON_INCLUDE_DIR})
+target_link_libraries(dnbd3-server dnbd3-version dnbd3-shared ${CMAKE_THREAD_LIBS_INIT} ${JANSSON_LIBRARIES})
+
+if(DNBD3_SERVER_FUSE)
+ # include Fuse headers and link with Fuse library
+ target_compile_options(dnbd3-server PRIVATE DNBD3_SERVER_FUSE)
+ target_include_directories(dnbd3-server PRIVATE ${FUSE_INCLUDE_DIRS})
+ target_link_libraries(dnbd3-server ${FUSE_LIBRARIES})
+endif(DNBD3_SERVER_FUSE)
+
+if(UNIX AND NOT APPLE)
+ # link dnbd3-server with librt if server is compiled for a Unix system
+ target_link_libraries(dnbd3-server rt)
+endif(UNIX AND NOT APPLE)
+
+if(DNBD3_SERVER_DEBUG_LOCKS)
+ # enable debugging of locks used in the dnbd3-server
+ target_compile_options(dnbd3-server DNBD3_SERVER_DEBUG_LOCKS)
+endif(DNBD3_SERVER_DEBUG_LOCKS)
+
+if(DNBD3_SERVER_DEBUG_THREADS)
+ # enable debugging of threads used in the dnbd3-server
+ target_compile_options(dnbd3-server DNBD3_SERVER_DEBUG_THREADS)
+endif(DNBD3_SERVER_DEBUG_THREADS)
+
+install(TARGETS dnbd3-server RUNTIME DESTINATION bin)
diff --git a/src/server/altservers.c b/src/server/altservers.c
index 838a475..4b87c3e 100644
--- a/src/server/altservers.c
+++ b/src/server/altservers.c
@@ -5,9 +5,9 @@
#include "helper.h"
#include "image.h"
#include "fileutil.h"
-#include "../shared/protocol.h"
-#include "../shared/timing.h"
-#include "../serverconfig.h"
+#include <dnbd3/shared/protocol.h>
+#include <dnbd3/shared/timing.h>
+#include <dnbd3/config/server.h>
#include "reference.h"
#include <assert.h>
diff --git a/src/server/fuse.c b/src/server/fuse.c
index e2ebf23..12913a6 100644
--- a/src/server/fuse.c
+++ b/src/server/fuse.c
@@ -1,8 +1,8 @@
#include "fuse.h"
-#include "../types.h"
-#include "../shared/log.h"
+#include <dnbd3/types.h>
+#include <dnbd3/shared/log.h>
-#ifndef BUILD_SERVER_FUSE
+#ifndef DNBD3_SERVER_FUSE
//
bool dfuse_init(const char *opts UNUSED, const char *dir UNUSED)
@@ -23,7 +23,7 @@ static char nullbytes[DNBD3_BLOCK_SIZE];
// FUSE ENABLED
#define FUSE_USE_VERSION 30
//
-#include "../config.h"
+#include <dnbd3/config.h>
#include "locks.h"
#include "threadpool.h"
#include "image.h"
@@ -658,4 +658,4 @@ static void cleanupFuse()
fuseChannel = NULL;
}
-#endif
+#endif // DNBD3_SERVER_FUSE
diff --git a/src/server/globals.c b/src/server/globals.c
index 9914f89..f689705 100644
--- a/src/server/globals.c
+++ b/src/server/globals.c
@@ -1,7 +1,7 @@
#include "globals.h"
#include "ini.h"
#include "locks.h"
-#include "../shared/log.h"
+#include <dnbd3/shared/log.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
diff --git a/src/server/globals.h b/src/server/globals.h
index 8807019..12805ed 100644
--- a/src/server/globals.h
+++ b/src/server/globals.h
@@ -1,9 +1,9 @@
#ifndef _GLOBALS_H_
#define _GLOBALS_H_
-#include "../types.h"
-#include "../shared/fdsignal.h"
-#include "../serverconfig.h"
+#include <dnbd3/types.h>
+#include <dnbd3/shared/fdsignal.h>
+#include <dnbd3/config/server.h>
#include <stdint.h>
#include <stdatomic.h>
#include <time.h>
@@ -36,7 +36,7 @@ typedef struct _dnbd3_queue_entry
uint64_t from; // First byte offset of requested block (ie. 4096)
uint64_t to; // Last byte + 1 of requested block (ie. 8192, if request len is 4096, resulting in bytes 4096-8191)
dnbd3_queue_client_t *clients;
-#ifdef _DEBUG
+#ifdef DEBUG
ticks entered; // When this request entered the queue (for debugging)
#endif
uint8_t hopCount; // How many hops this request has already taken across proxies
diff --git a/src/server/helper.h b/src/server/helper.h
index 102cb36..3e1b661 100644
--- a/src/server/helper.h
+++ b/src/server/helper.h
@@ -2,8 +2,8 @@
#define HELPER_H_
#include "server.h"
-#include "../shared/log.h"
-#include "../types.h"
+#include <dnbd3/shared/log.h>
+#include <dnbd3/types.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
diff --git a/src/server/image.c b/src/server/image.c
index 7a6e3f4..de6217f 100644
--- a/src/server/image.c
+++ b/src/server/image.c
@@ -5,9 +5,9 @@
#include "locks.h"
#include "integrity.h"
#include "altservers.h"
-#include "../shared/protocol.h"
-#include "../shared/timing.h"
-#include "../shared/crc32.h"
+#include <dnbd3/shared/protocol.h>
+#include <dnbd3/shared/timing.h>
+#include <dnbd3/shared/crc32.h>
#include "reference.h"
#include <assert.h>
diff --git a/src/server/locks.c b/src/server/locks.c
index 4d9cde6..3be73b3 100644
--- a/src/server/locks.c
+++ b/src/server/locks.c
@@ -7,9 +7,9 @@
#include "locks.h"
#include "helper.h"
-#include "../shared/timing.h"
+#include <dnbd3/shared/timing.h>
-#ifdef DEBUG_LOCKS
+#ifdef DNBD3_SERVER_DEBUG_LOCKS
#define MAXLOCKS (SERVER_MAX_CLIENTS * 2 + SERVER_MAX_ALTS + 200 + SERVER_MAX_IMAGES)
#define MAXTHREADS (SERVER_MAX_CLIENTS + 100)
#define MAXLPT 20
diff --git a/src/server/locks.h b/src/server/locks.h
index ff27a33..f495f7b 100644
--- a/src/server/locks.h
+++ b/src/server/locks.h
@@ -28,7 +28,7 @@
//
-#ifdef DEBUG_LOCKS
+#ifdef DNBD3_SERVER_DEBUG_LOCKS
#define mutex_init( lock, prio ) debug_mutex_init( #lock, __FILE__, __LINE__, lock, prio)
#define mutex_lock( lock ) debug_mutex_lock( #lock, __FILE__, __LINE__, lock, false)
@@ -57,7 +57,7 @@ void debug_dump_lock_stats();
#endif
-#ifdef DEBUG_THREADS
+#ifdef DNBD3_SERVER_DEBUG_THREADS
extern int debugThreadCount;
#define thread_create(thread,attr,routine,arg) (logadd( LOG_THREAD CREATE, "%d @ %s:%d\n", debugThreadCount, __FILE__, (int)__LINE__), debug_thread_create(thread, attr, routine, arg))
@@ -101,6 +101,6 @@ static inline int debug_thread_join(pthread_t thread, void **value_ptr)
#define thread_detach(thread) pthread_detach( thread )
#define thread_join(thread,value) pthread_join( thread, value )
-#endif
+#endif /* DNBD3_SERVER_DEBUG_THREADS */
#endif /* LOCKS_H_ */
diff --git a/src/server/net.c b/src/server/net.c
index c33a12e..4cd9773 100644
--- a/src/server/net.c
+++ b/src/server/net.c
@@ -26,10 +26,10 @@
#include "altservers.h"
#include "reference.h"
-#include "../shared/sockhelper.h"
-#include "../shared/timing.h"
-#include "../shared/protocol.h"
-#include "../serialize.h"
+#include <dnbd3/shared/sockhelper.h>
+#include <dnbd3/shared/timing.h>
+#include <dnbd3/shared/protocol.h>
+#include <dnbd3/shared/serialize.h>
#include <assert.h>
@@ -63,7 +63,7 @@ static void uplinkCallback(void *data, uint64_t handle, uint64_t start, uint32_t
static inline bool recv_request_header(int sock, dnbd3_request_t *request)
{
ssize_t ret, fails = 0;
-#ifdef AFL_MODE
+#ifdef DNBD3_SERVER_AFL
sock = 0;
#endif
// Read request header from socket
@@ -90,7 +90,7 @@ static inline bool recv_request_header(int sock, dnbd3_request_t *request)
static inline bool recv_request_payload(int sock, uint32_t size, serialized_buffer_t *payload)
{
-#ifdef AFL_MODE
+#ifdef DNBD3_SERVER_AFL
sock = 0;
#endif
if ( size == 0 ) {
@@ -160,7 +160,7 @@ void* net_handleNewConnection(void *clientPtr)
// Await data from client. Since this is a fresh connection, we expect data right away
sock_setTimeout( client->sock, _clientTimeout );
do {
-#ifdef AFL_MODE
+#ifdef DNBD3_SERVER_AFL
const int ret = (int)recv( 0, &request, sizeof(request), MSG_WAITALL );
#else
const int ret = (int)recv( client->sock, &request, sizeof(request), MSG_WAITALL );
@@ -396,7 +396,7 @@ void* net_handleNewConnection(void *clientPtr)
// TODO: Should we consider EOPNOTSUPP on BSD for sendfile and fallback to read/write?
// Linux would set EINVAL or ENOSYS instead, which it unfortunately also does for a couple of other failures :/
// read/write would kill performance anyways so a fallback would probably be of little use either way.
-#ifdef AFL_MODE
+#ifdef DNBD3_SERVER_AFL
char buf[1000];
size_t cnt = realBytes - done;
if ( cnt > 1000 ) {
diff --git a/src/server/rpc.c b/src/server/rpc.c
index 0002661..ab08395 100644
--- a/src/server/rpc.c
+++ b/src/server/rpc.c
@@ -5,8 +5,8 @@
#include "locks.h"
#include "image.h"
#include "altservers.h"
-#include "../shared/sockhelper.h"
-#include "../version.h"
+#include <dnbd3/shared/sockhelper.h>
+#include <dnbd3/version.h>
#include "fileutil.h"
#include "picohttpparser/picohttpparser.h"
#include "urldecode.h"
@@ -175,7 +175,7 @@ void rpc_sendStatsJson(int sock, dnbd3_host_t* host, const void* data, const int
// Reaching here means partial request or parse error
if ( pret == -2 ) { // Partial, keep reading
prevLen = hoff;
-#ifdef AFL_MODE
+#ifdef DNBD3_SERVER_AFL
ssize_t ret = recv( 0, headerBuf + hoff, sizeof(headerBuf) - hoff, 0 );
#else
ssize_t ret = recv( sock, headerBuf + hoff, sizeof(headerBuf) - hoff, 0 );
@@ -311,7 +311,7 @@ static bool handleStatus(int sock, int permissions, struct field *fields, size_t
"runId", randomRunId );
}
if ( version ) {
- json_object_set_new( statisticsJson, "version", json_string( VERSION_STRING ) );
+ json_object_set_new( statisticsJson, "version", json_string( DNBD3_VERSION ) );
json_object_set_new( statisticsJson, "build", json_string( TOSTRING( BUILD_TYPE ) ) );
}
if ( space ) {
@@ -411,7 +411,7 @@ static bool sendReply(int sock, const char *status, const char *ctype, const cha
if ( keepAlive == HTTP_CLOSE ) {
// Wait for flush
shutdown( sock, SHUT_WR );
-#ifdef AFL_MODE
+#ifdef DNBD3_SERVER_AFL
sock = 0;
#endif
// Don't wait too long in case other side ignores the shutdown
@@ -459,7 +459,7 @@ static int getacl(dnbd3_host_t *host)
if ( aclRules[i].bitMask != 0 && aclRules[i].host[aclRules[i].bytes] != ( host->addr[aclRules[i].bytes] & aclRules[i].bitMask ) ) continue;
return aclRules[i].permissions;
}
-#ifdef AFL_MODE
+#ifdef DNBD3_SERVER_AFL
return 0x7fffff;
#else
return 0;
diff --git a/src/server/serialize.c b/src/server/serialize.c
deleted file mode 100644
index 4934132..0000000
--- a/src/server/serialize.c
+++ /dev/null
@@ -1,5 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include <stdint.h>
-
-#include "../serialize.c"
diff --git a/src/server/server.c b/src/server/server.c
index 9dd9f81..8aefece 100644
--- a/src/server/server.c
+++ b/src/server/server.c
@@ -31,9 +31,9 @@
#include "rpc.h"
#include "fuse.h"
-#include "../version.h"
-#include "../shared/sockhelper.h"
-#include "../shared/timing.h"
+#include <dnbd3/version.h>
+#include <dnbd3/shared/sockhelper.h>
+#include <dnbd3/shared/timing.h>
#include <signal.h>
#include <getopt.h>
@@ -105,11 +105,11 @@ static void queueJobInternal(job_t *job);
*/
void dnbd3_printHelp(char *argv_0)
{
- printf( "Version: %s\n\n", VERSION_STRING );
+ printf( "Version: %s\n\n", DNBD3_VERSION );
printf( "Usage: %s [OPTIONS]...\n", argv_0 );
printf( "Start the DNBD3 server\n" );
printf( "-c or --config Configuration directory (default /etc/dnbd3-server/)\n" );
-#ifdef BUILD_SERVER_FUSE
+#ifdef DNBD3_SERVER_WITH_FUSE
printf( "-m or --mount FUSE mount point\n ");
#endif
printf( "-n or --nodaemon Start server in foreground\n" );
@@ -130,7 +130,7 @@ void dnbd3_printHelp(char *argv_0)
*/
void dnbd3_printVersion()
{
- printf( "Version: %s\n", VERSION_STRING );
+ printf( "dnbd3-server version: %s\n", DNBD3_VERSION );
exit( 0 );
}
@@ -227,7 +227,7 @@ int main(int argc, char *argv[])
_configDir = strdup( optarg );
break;
case 'm':
-#ifndef BUILD_SERVER_FUSE
+#ifndef DNBD3_SERVER_WITH_FUSE
fprintf( stderr, "FUSE support not enabled at build time.\n" );
return 8;
#endif
@@ -290,7 +290,7 @@ int main(int argc, char *argv[])
timing_setBase();
timing_get( &startupTime );
-#ifdef AFL_MODE
+#ifdef DNBD3_SERVER_AFL
// ###### AFL
//
image_serverStartup();
@@ -365,7 +365,7 @@ int main(int argc, char *argv[])
logadd( LOG_INFO, "DNBD3 server starting...." );
logadd( LOG_INFO, "Machine type: " ENDIAN_MODE );
logadd( LOG_INFO, "Build Type: " TOSTRING( BUILD_TYPE ) );
- logadd( LOG_INFO, "Version: %s", VERSION_STRING );
+ logadd( LOG_INFO, "Version: %s", DNBD3_VERSION );
if ( altservers_load() < 0 ) {
logadd( LOG_WARNING, "Could not load alt-servers. Does the file exist in %s?", _configDir );
diff --git a/src/server/server.h b/src/server/server.h
index a026eb6..93ab925 100644
--- a/src/server/server.h
+++ b/src/server/server.h
@@ -22,7 +22,7 @@
#define SERVER_H_
#include "globals.h"
-#include "../types.h"
+#include <dnbd3/types.h>
uint32_t dnbd3_serverUptime();
void server_addJob(void *(*startRoutine)(void *), void *arg, int delaySecs, int intervalSecs);
diff --git a/src/server/threadpool.c b/src/server/threadpool.c
index 4ebefcb..cf39d5c 100644
--- a/src/server/threadpool.c
+++ b/src/server/threadpool.c
@@ -63,7 +63,7 @@ bool threadpool_run(void *(*startRoutine)(void *), void *arg, const char *name)
logadd( LOG_MINOR, "Cannot submit work to threadpool while shutting down!" );
return false;
}
-#ifdef _DEBUG
+#ifdef DEBUG
if ( unlikely( startRoutine == NULL ) ) {
logadd( LOG_ERROR, "Trying to queue work for thread pool with NULL startRoutine" );
return false; // Or bail out!?
@@ -123,7 +123,7 @@ keep_going:;
logadd( LOG_DEBUG1, "Unexpected return value %d for signal_wait in threadpool worker!", ret );
continue;
}
-#ifdef _DEBUG
+#ifdef DEBUG
if ( entry->startRoutine == NULL ) {
logadd( LOG_ERROR, "Worker woke up but has no work to do!" );
exit( 1 );
diff --git a/src/server/threadpool.h b/src/server/threadpool.h
index d8a526e..c30d44f 100644
--- a/src/server/threadpool.h
+++ b/src/server/threadpool.h
@@ -1,7 +1,7 @@
#ifndef _THREADPOOL_H_
#define _THREADPOOL_H_
-#include "../types.h"
+#include <dnbd3/types.h>
/**
* Initialize the thread pool. This must be called before using
diff --git a/src/server/uplink.c b/src/server/uplink.c
index cb4f956..1fded60 100644
--- a/src/server/uplink.c
+++ b/src/server/uplink.c
@@ -4,10 +4,10 @@
#include "image.h"
#include "altservers.h"
#include "net.h"
-#include "../shared/sockhelper.h"
-#include "../shared/protocol.h"
-#include "../shared/timing.h"
-#include "../shared/crc32.h"
+#include <dnbd3/shared/sockhelper.h>
+#include <dnbd3/shared/protocol.h>
+#include <dnbd3/shared/timing.h>
+#include <dnbd3/shared/crc32.h>
#include "threadpool.h"
#include "reference.h"
@@ -358,7 +358,7 @@ static bool uplink_requestInternal(dnbd3_uplink_t *uplink, void *data, uplink_ca
request->handle = ++uplink->queueId;
request->from = start & ~(uint64_t)(DNBD3_BLOCK_SIZE - 1);
request->to = (end + DNBD3_BLOCK_SIZE - 1) & ~(uint64_t)(DNBD3_BLOCK_SIZE - 1);
-#ifdef _DEBUG
+#ifdef DEBUG
timing_get( &request->entered );
#endif
request->hopCount = hops;
@@ -650,7 +650,7 @@ static void* uplink_mainloop(void *data)
}
timing_set( &nextAltCheck, &now, (discoverFailCount < SERVER_RTT_MAX_UNREACH) ? altCheckInterval : SERVER_RTT_INTERVAL_FAILED );
}
-#ifdef _DEBUG
+#ifdef DEBUG
if ( uplink->current.fd != -1 && !uplink->shutdown ) {
bool resend = false;
ticks deadline;
@@ -662,7 +662,7 @@ static void* uplink_mainloop(void *data)
" (from %" PRIu64 " to %" PRIu64 ", sent: %d) %s:%d",
it->from, it->to, (int)it->sent, PIMG(uplink->image) );
it->entered = now;
-#ifdef _DEBUG_RESEND_STARVING
+#ifdef DEBUG_RESEND_STARVING
it->sent = false;
resend = true;
#endif
diff --git a/src/server/uplink.h b/src/server/uplink.h
index 6cd69ea..b6037d6 100644
--- a/src/server/uplink.h
+++ b/src/server/uplink.h
@@ -2,7 +2,7 @@
#define _UPLINK_H_
#include "globals.h"
-#include "../types.h"
+#include <dnbd3/types.h>
void uplink_globalsInit();
diff --git a/src/shared/CMakeLists.txt b/src/shared/CMakeLists.txt
new file mode 100644
index 0000000..804d27c
--- /dev/null
+++ b/src/shared/CMakeLists.txt
@@ -0,0 +1,15 @@
+cmake_minimum_required(VERSION 3.10)
+
+# set the project name
+project(dnbd3-shared)
+
+# add compile option to get POLLRDHUP support for signals
+add_compile_options(-D_GNU_SOURCE)
+
+add_library(dnbd3-shared STATIC ${CMAKE_CURRENT_SOURCE_DIR}/crc32.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/fdsignal.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/log.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/serialize.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/sockhelper.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/timing.c)
+target_include_directories(dnbd3-shared PUBLIC ${PROJECT_INCLUDE_DIR})
diff --git a/src/shared/crc32.c b/src/shared/crc32.c
index c3e566f..6cf9a18 100644
--- a/src/shared/crc32.c
+++ b/src/shared/crc32.c
@@ -38,7 +38,7 @@
*/
-#include "../types.h"
+#include <dnbd3/types.h>
#include <stddef.h>
#if defined(__x86_64__) || defined(__amd64__)
diff --git a/src/shared/fdsignal.c b/src/shared/fdsignal.c
index 087b6f1..1db59bd 100644
--- a/src/shared/fdsignal.c
+++ b/src/shared/fdsignal.c
@@ -1,4 +1,4 @@
-#include "fdsignal.h"
+#include <dnbd3/shared/fdsignal.h>
#if defined(__linux__)
//#warning "Using eventfd based signalling"
diff --git a/src/shared/log.c b/src/shared/log.c
index 055acb4..f2cab85 100644
--- a/src/shared/log.c
+++ b/src/shared/log.c
@@ -18,7 +18,7 @@
*
*/
-#include "log.h"
+#include <dnbd3/shared/log.h>
#include <stdarg.h>
#include <pthread.h>
#include <stdlib.h>
@@ -136,7 +136,7 @@ void logadd(const logmask_t mask, const char *fmt, ...)
}
if ( toStdout ) {
if ( consoleTimestamps ) stdoutLine = buffer;
-#ifdef AFL_MODE
+#ifdef DNBD3_SERVER_AFL
fputs( stdoutLine, stderr );
fflush( stderr );
#else
diff --git a/src/serialize.c b/src/shared/serialize.c
index 0bc0dcd..648b719 100644
--- a/src/serialize.c
+++ b/src/shared/serialize.c
@@ -1,6 +1,5 @@
-#include "serialize.h"
-#include "types.h"
-
+#include <dnbd3/shared/serialize.h>
+#include <dnbd3/types.h>
void serializer_reset_read(serialized_buffer_t *buffer, size_t data_len)
{
diff --git a/src/shared/sockhelper.c b/src/shared/sockhelper.c
index 4ff93a6..f218d5a 100644
--- a/src/shared/sockhelper.c
+++ b/src/shared/sockhelper.c
@@ -1,5 +1,6 @@
-#include "sockhelper.h"
-#include "log.h"
+#include <dnbd3/shared/sockhelper.h>
+#include <dnbd3/shared/log.h>
+#include <dnbd3/types.h>
#include <arpa/inet.h> // inet_ntop
#include <netdb.h>
#include <stdio.h>
@@ -19,8 +20,7 @@ struct _poll_list {
int sock_connect(const dnbd3_host_t * const addr, const int connect_ms, const int rw_ms)
{
// TODO: Move out of here, this unit should contain general socket functions
- // TODO: Abstract away from sockaddr_in* like the rest of the functions here do,
- // so WITH_IPV6 can finally be removed as everything is transparent. b- but how?
+ // TODO: Abstract away from sockaddr_in* like the rest of the functions here
struct sockaddr_storage ss;
int proto, addrlen;
memset( &ss, 0, sizeof ss );
@@ -32,9 +32,7 @@ int sock_connect(const dnbd3_host_t * const addr, const int connect_ms, const in
addr4->sin_port = addr->port;
proto = PF_INET;
addrlen = sizeof *addr4;
- }
-#ifdef WITH_IPV6
- else if ( addr->type == HOST_IP6 ) {
+ } else if ( addr->type == HOST_IP6 ) {
// Set host (IPv6)
struct sockaddr_in6 *addr6 = (struct sockaddr_in6*)&ss;
addr6->sin6_family = AF_INET6;
@@ -42,9 +40,7 @@ int sock_connect(const dnbd3_host_t * const addr, const int connect_ms, const in
addr6->sin6_port = addr->port;
proto = PF_INET6;
addrlen = sizeof *addr6;
- }
-#endif
- else {
+ } else {
logadd( LOG_DEBUG1, "Unsupported address type: %d\n", (int)addr->type );
errno = EAFNOSUPPORT;
return -1;
@@ -165,7 +161,7 @@ bool sock_sockaddrToDnbd3(struct sockaddr* sa, dnbd3_host_t *host)
memcpy( host->addr, &addr4->sin_addr, 4 );
return true;
}
-#ifdef WITH_IPV6
+
if ( sa->sa_family == AF_INET6 ) {
// Set host (IPv6)
struct sockaddr_in6 *addr6 = (struct sockaddr_in6*)sa;
@@ -174,7 +170,7 @@ bool sock_sockaddrToDnbd3(struct sockaddr* sa, dnbd3_host_t *host)
memcpy( host->addr, &addr6->sin6_addr, 16 );
return true;
}
-#endif
+
return false;
}
diff --git a/src/shared/timing.c b/src/shared/timing.c
index 4ca1002..bdb8388 100644
--- a/src/shared/timing.c
+++ b/src/shared/timing.c
@@ -1,4 +1,4 @@
-#include "timing.h"
+#include <dnbd3/shared/timing.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
diff --git a/src/version.c.in b/src/version.c.in
deleted file mode 100644
index ab937a2..0000000
--- a/src/version.c.in
+++ /dev/null
@@ -1,3 +0,0 @@
-
-const char * VERSION_STRING = "@VERSION@";
-
diff --git a/src/version.h b/src/version.h
deleted file mode 100644
index 1c17442..0000000
--- a/src/version.h
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * This file is part of the Distributed Network Block Device 3
- *
- * Copyright(c) 2011-2012 Johann Latocha <johann@latocha.de>
- *
- * This file may be licensed under the terms of of the
- * GNU General Public License Version 2 (the ``GPL'').
- *
- * Software distributed under the License is distributed
- * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
- * express or implied. See the GPL for the specific language
- * governing rights and limitations.
- *
- * You should have received a copy of the GPL along with this
- * program. If not, go to http://www.gnu.org/licenses/gpl.html
- * or write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- */
-
-#ifndef VERSION_H_
-#define VERSION_H_
-
-extern const char *VERSION_STRING;
-
-#endif /* VERSION_H_ */