diff options
Diffstat (limited to 'src/server/server.c')
-rw-r--r-- | src/server/server.c | 84 |
1 files changed, 61 insertions, 23 deletions
diff --git a/src/server/server.c b/src/server/server.c index 0dddea7..0f75935 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -3,7 +3,7 @@ * * Copyright(c) 2011-2012 Johann Latocha <johann@latocha.de> * - * This file may be licensed under the terms of of the + * This file may be licensed under the terms of the * GNU General Public License Version 2 (the ``GPL''). * * Software distributed under the License is distributed @@ -29,10 +29,12 @@ #include "integrity.h" #include "threadpool.h" #include "rpc.h" +#include "fuse.h" -#include "../version.h" -#include "../shared/sockhelper.h" -#include "../shared/timing.h" +#include <dnbd3/version.h> +#include <dnbd3/build.h> +#include <dnbd3/shared/sockhelper.h> +#include <dnbd3/shared/timing.h> #include <signal.h> #include <getopt.h> @@ -104,10 +106,14 @@ 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_LONG ); + printf( "Built: %s\n", DNBD3_BUILD_DATE ); 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 DNBD3_SERVER_FUSE + printf( "-m or --mount FUSE mount point\n"); +#endif printf( "-n or --nodaemon Start server in foreground\n" ); printf( "-b or --bind Local Address to bind to\n" ); printf( "-h or --help Show this help text and quit\n" ); @@ -126,7 +132,8 @@ void dnbd3_printHelp(char *argv_0) */ void dnbd3_printVersion() { - printf( "Version: %s\n", VERSION_STRING ); + printf( "dnbd3-server version: %s\n", DNBD3_VERSION_LONG ); + printf( "Built: %s\n", DNBD3_BUILD_DATE ); exit( 0 ); } @@ -140,6 +147,8 @@ _Noreturn static void dnbd3_cleanup() _shutdown = true; logadd( LOG_INFO, "Cleanup..." ); + dfuse_shutdown(); + if ( hasTimerThread ) { pthread_kill( timerThread, SIGINT ); thread_join( timerThread, NULL ); @@ -190,11 +199,13 @@ int main(int argc, char *argv[]) char *paramCreate = NULL; char *bindAddress = NULL; char *errorMsg = NULL; + char *mountDir = NULL; int64_t paramSize = -1; int paramRevision = -1; - static const char *optString = "b:c:d:hnv?"; + static const char *optString = "b:c:m:d:hnv?"; static const struct option longOpts[] = { { "config", required_argument, NULL, 'c' }, + { "mount", required_argument, NULL, 'm' }, { "nodaemon", no_argument, NULL, 'n' }, { "reload", no_argument, NULL, 'r' }, { "help", no_argument, NULL, 'h' }, @@ -209,6 +220,16 @@ int main(int argc, char *argv[]) { 0, 0, 0, 0 } }; + log_init(); + + /* set proper output stream for AFL */ +#ifdef DNBD3_SERVER_AFL + if ( log_setConsoleOutputStream(stderr) < 0 ) { + logadd( LOG_ERROR, "Failed to set output stream for AFL to stderr" ); + exit( EXIT_FAILURE ); + } +#endif + mainPid = getpid(); mainThread = pthread_self(); opt = getopt_long( argc, argv, optString, longOpts, &longIndex ); @@ -218,6 +239,13 @@ int main(int argc, char *argv[]) case 'c': _configDir = strdup( optarg ); break; + case 'm': +#ifndef DNBD3_SERVER_FUSE + fprintf( stderr, "FUSE support not enabled at build time.\n" ); + return 8; +#endif + mountDir = strdup( optarg ); + break; case 'n': demonize = 0; break; @@ -263,6 +291,7 @@ int main(int argc, char *argv[]) opt = getopt_long( argc, argv, optString, longOpts, &longIndex ); } + // Load general config if ( _configDir == NULL ) _configDir = strdup( "/etc/dnbd3-server" ); @@ -275,9 +304,7 @@ int main(int argc, char *argv[]) timing_setBase(); timing_get( &startupTime ); -#ifdef AFL_MODE - // ###### AFL - // +#ifdef DNBD3_SERVER_AFL image_serverStartup(); net_init(); uplink_globalsInit(); @@ -301,9 +328,7 @@ int main(int argc, char *argv[]) net_handleNewConnection( dnbd3_client ); exit( 0 ); } - // - // ###### AFL END -#endif +#endif /* DNBD3_SERVER_AFL */ // One-shots first: @@ -315,7 +340,10 @@ int main(int argc, char *argv[]) // No one-shot detected, normal server operation or errormsg serving if ( demonize ) { logadd( LOG_INFO, "Forking into background, see log file for further information" ); - daemon( 1, 0 ); + if ( daemon( 0, 0 ) == -1 ) { + logadd( LOG_ERROR, "Could not daemon(): errno=%d", errno ); + exit( 1 ); + } } if ( errorMsg != NULL ) { setupNetwork( bindAddress ); @@ -339,7 +367,15 @@ int main(int argc, char *argv[]) net_init(); uplink_globalsInit(); rpc_init(); - logadd( LOG_INFO, "DNBD3 server starting.... Machine type: " ENDIAN_MODE ); + if ( mountDir != NULL && !dfuse_init( "-oallow_other", mountDir ) ) { + logadd( LOG_ERROR, "Cannot mount fuse directory to %s", mountDir ); + dnbd3_cleanup(); + return EXIT_FAILURE; + } + logadd( LOG_INFO, "DNBD3 server starting...." ); + logadd( LOG_INFO, "Machine type: " DNBD3_ENDIAN_MODE ); + logadd( LOG_INFO, "Build Type: %s", DNBD3_BUILD ); + logadd( LOG_INFO, "Version: %s, built %s", DNBD3_VERSION_LONG, DNBD3_BUILD_DATE ); if ( altservers_load() < 0 ) { logadd( LOG_WARNING, "Could not load alt-servers. Does the file exist in %s?", _configDir ); @@ -379,10 +415,11 @@ int main(int argc, char *argv[]) // Initialize thread pool if ( !threadpool_init( 8 ) ) { logadd( LOG_ERROR, "Could not init thread pool!\n" ); + dnbd3_cleanup(); exit( EXIT_FAILURE ); } - logadd( LOG_INFO, "Server is ready. (%s)", VERSION_STRING ); + logadd( LOG_INFO, "Server is ready." ); if ( thread_create( &timerThread, NULL, &timerMainloop, NULL ) == 0 ) { hasTimerThread = true; @@ -398,7 +435,7 @@ int main(int argc, char *argv[]) if ( sigReload ) { sigReload = false; logadd( LOG_INFO, "SIGHUP received, re-scanning image directory" ); - threadpool_run( &server_asyncImageListLoad, NULL ); + threadpool_run( &server_asyncImageListLoad, NULL, "IMAGE_RELOAD" ); } if ( sigLogCycle ) { sigLogCycle = false; @@ -425,7 +462,7 @@ int main(int argc, char *argv[]) continue; } - if ( !threadpool_run( &net_handleNewConnection, (void *)dnbd3_client ) ) { + if ( !threadpool_run( &net_handleNewConnection, (void *)dnbd3_client, "CLIENT" ) ) { logadd( LOG_ERROR, "Could not start thread for new connection." ); free( dnbd3_client ); continue; @@ -520,10 +557,11 @@ static void dnbd3_handleSignal2(int signum, siginfo_t *info, void *data UNUSED) if ( info->si_pid != 0 && !pthread_equal( pthread_self(), mainThread ) ) { pthread_kill( mainThread, info->si_signo ); // And relay signal if we're not the main thread } - } - if ( pthread_equal( pthread_self(), mainThread ) ) { - // Signal received by main thread -- handle - dnbd3_handleSignal( signum ); + // Source is not this process -- only then do we honor signals + if ( pthread_equal( pthread_self(), mainThread ) ) { + // Signal received by main thread -- handle + dnbd3_handleSignal( signum ); + } } } @@ -568,7 +606,7 @@ static int handlePendingJobs(void) jobHead = *temp; // Make it list head *temp = NULL; // Split off part before that while ( todo != NULL ) { - threadpool_run( todo->startRoutine, todo->arg ); + threadpool_run( todo->startRoutine, todo->arg, "TIMER_TASK" ); old = todo; todo = todo->next; if ( old->intervalSecs == 0 ) { |