summaryrefslogtreecommitdiffstats
path: root/src/server/altservers.c
diff options
context:
space:
mode:
authorSimon Rettberg2013-08-01 18:14:15 +0200
committerSimon Rettberg2013-08-01 18:14:15 +0200
commit96b3a0feb963466447ca8dbc571bc5f670d533cc (patch)
treee546179f4d6d5cbb23dae202a8bf59b9f2ce95bb /src/server/altservers.c
parent[SERVER] Add inih (http://code.google.com/p/inih/) for *.ini parsing (diff)
downloaddnbd3-96b3a0feb963466447ca8dbc571bc5f670d533cc.tar.gz
dnbd3-96b3a0feb963466447ca8dbc571bc5f670d533cc.tar.xz
dnbd3-96b3a0feb963466447ca8dbc571bc5f670d533cc.zip
[SERVER] Add command line options to create empty image of certain size with empty cache map (so it needs an uplink server)
Diffstat (limited to 'src/server/altservers.c')
-rw-r--r--src/server/altservers.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/server/altservers.c b/src/server/altservers.c
index 6301c32..44061a3 100644
--- a/src/server/altservers.c
+++ b/src/server/altservers.c
@@ -4,6 +4,7 @@
#include "sockhelper.h"
#include "memlog.h"
#include "helper.h"
+#include "globals.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/epoll.h>
@@ -12,6 +13,7 @@
#include <assert.h>
#include <inttypes.h>
#include <time.h>
+#include <stdio.h>
#include "../serialize.h"
static dnbd3_connection_t *pending[SERVER_MAX_PENDING_ALT_CHECKS];
@@ -36,6 +38,52 @@ void altserver_init()
}
}
+int altservers_load()
+{
+ int count = 0;
+ char *name = NULL, *space;
+ char line[1000];
+ dnbd3_host_t host;
+ asprintf( &name, "%s/%s", _configDir, "alt-servers" );
+ if ( name == NULL ) return -1;
+ FILE *fp = fopen( name, "r" );
+ free( name );
+ if ( fp == NULL ) return -1;
+ while ( !feof( fp ) ) {
+ if ( fgets( line, 1000, fp ) == NULL ) break;
+ trim_right( line );
+ space = strchr( line, ' ' );
+ if ( space != NULL ) *space++ = '\0';
+ if ( !parse_address( line, &host ) ) {
+ if ( space != NULL ) *--space = ' ';
+ memlogf( "[WARNING] Invalid entry in alt-servers file ignored: '%s'", line );
+ continue;
+ }
+ if ( altservers_add( &host, space ) ) ++count;
+ }
+ fclose( fp );
+ return count;
+}
+
+int altservers_add(dnbd3_host_t *host, const char *comment)
+{
+ int i, freeSlot = -1;
+ spin_lock( &_alts_lock );
+ for (i = 0; i < _num_alts; ++i) {
+ if ( is_same_server( &_alt_servers[i].host, host ) ) {
+ spin_unlock( &_alts_lock );
+ return FALSE;
+ } else if ( freeSlot == -1 && _alt_servers[i].host.type == 0 ) {
+ freeSlot = i;
+ }
+ }
+ if ( freeSlot == -1 ) freeSlot = _num_alts++;
+ _alt_servers[freeSlot].host = *host;
+ if ( comment != NULL ) snprintf( _alt_servers[freeSlot].comment, COMMENT_LENGTH, "%s", comment );
+ spin_unlock( &_alts_lock );
+ return TRUE;
+}
+
/**
* ONLY called from the passed uplink's main thread
*/