summaryrefslogtreecommitdiffstats
path: root/src/server
diff options
context:
space:
mode:
authorManuel Bentele2020-10-16 17:15:49 +0200
committerManuel Bentele2020-10-16 17:15:49 +0200
commit969496f15e1e0359e26c2c6e995ad4ef82720f86 (patch)
tree358466216630eb10e8ee91b9ede490424f6db848 /src/server
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.
Diffstat (limited to 'src/server')
-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
18 files changed, 124 insertions, 63 deletions
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();