summaryrefslogtreecommitdiffstats
path: root/src/server
diff options
context:
space:
mode:
authorJohann Latocha2012-02-02 01:13:50 +0100
committerJohann Latocha2012-02-02 01:13:50 +0100
commit37e4ed696b77455a738fa948b93498c8740721bc (patch)
tree14306f376ef7132d2cac1624e3aafa8dbba035b0 /src/server
parent[SERVER] Close all client sockets on shutdown (diff)
downloaddnbd3-37e4ed696b77455a738fa948b93498c8740721bc.tar.gz
dnbd3-37e4ed696b77455a738fa948b93498c8740721bc.tar.xz
dnbd3-37e4ed696b77455a738fa948b93498c8740721bc.zip
[SERVER] Parsing config file with glib
[KERNEL] Bugfixes [CLIENT] Using vid and rid
Diffstat (limited to 'src/server')
-rw-r--r--src/server/hashtable.c78
-rw-r--r--src/server/hashtable.h32
-rw-r--r--src/server/net.c19
-rw-r--r--src/server/server.c10
-rw-r--r--src/server/server.h18
-rw-r--r--src/server/utils.c90
-rw-r--r--src/server/utils.h7
7 files changed, 95 insertions, 159 deletions
diff --git a/src/server/hashtable.c b/src/server/hashtable.c
deleted file mode 100644
index a9fb63c..0000000
--- a/src/server/hashtable.c
+++ /dev/null
@@ -1,78 +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.
- *
- */
-
-#include <stdio.h>
-#include <search.h>
-#include <string.h>
-
-#include "../config.h"
-
-char key_buf[MAX_NUMBER_IMAGES * MAX_FILE_ID];
-char value_buf[MAX_NUMBER_IMAGES * MAX_FILE_NAME];
-
-char *key_ptr = key_buf;
-char *val_ptr = value_buf;
-
-void dnbd3_ht_create()
-{
- (void) hcreate(MAX_NUMBER_IMAGES);
-}
-
-void dnbd3_ht_destroy()
-{
- key_ptr = key_buf;
- val_ptr = value_buf;
- hdestroy();
-}
-
-int dnbd3_ht_insert(char* key, char* value)
-{
- if (strlen(key) > MAX_FILE_ID)
- return -1;
- if (strlen(value) > MAX_FILE_NAME)
- return -2;
-
- strcpy(key_ptr, key);
- strcpy(val_ptr, value);
-
- ENTRY item;
- item.key = key_ptr;
- item.data = val_ptr;
-
- (void) hsearch(item, ENTER);
-
- key_ptr += strlen(key) + 1;
- val_ptr += strlen(value) + 1;
-
- return 0;
-}
-
-char* dnbd3_ht_search(char* key)
-{
- ENTRY *result;
-
- ENTRY item;
- item.key = key;
-
- if ((result = hsearch(item, FIND)) != NULL)
- return result->data;
- else
- return NULL;
-}
diff --git a/src/server/hashtable.h b/src/server/hashtable.h
deleted file mode 100644
index 3c16dff..0000000
--- a/src/server/hashtable.h
+++ /dev/null
@@ -1,32 +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 HASHTABLE_H_
-#define HASHTABLE_H_
-
-void dnbd3_ht_create();
-
-void dnbd3_ht_destroy();
-
-int dnbd3_ht_insert(char* key, char* value);
-
-char* dnbd3_ht_search(char* key);
-
-#endif /* HASHTABLE_H_ */
diff --git a/src/server/net.c b/src/server/net.c
index 2d6b9fc..4f0e5a4 100644
--- a/src/server/net.c
+++ b/src/server/net.c
@@ -24,7 +24,6 @@
#include <string.h>
#include <unistd.h>
#include <pthread.h>
-#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/sendfile.h>
@@ -32,13 +31,12 @@
#include <netinet/in.h>
#include "server.h"
-#include "hashtable.h"
+#include "utils.h"
void *dnbd3_handle_query(void *dnbd3_client)
{
dnbd3_client_t *client = (dnbd3_client_t *) (uintptr_t) dnbd3_client;
int image_file = -1;
- off_t filesize = 0;
dnbd3_request_t request;
dnbd3_reply_t reply;
uint16_t cmd;
@@ -56,21 +54,19 @@ void *dnbd3_handle_query(void *dnbd3_client)
case CMD_GET_SIZE:
pthread_spin_lock(&_spinlock); // because of reloading config
- image_file = open(dnbd3_ht_search(request.image_id), O_RDONLY);
+ dnbd3_image_t *image = dnbd3_get_image(request.vid, request.rid);
pthread_spin_unlock(&_spinlock);
- if (image_file < 0)
+ if (image)
{
- printf("ERROR: Client requested an unknown image id.\n");
- filesize = 0;
+ image_file = open(image->file, O_RDONLY);
+ reply.filesize = image->filesize;
}
else
{
- struct stat st;
- fstat(image_file, &st);
- filesize = st.st_size;
+ printf("ERROR: Client requested an unknown image id.\n");
+ reply.filesize = 0;
}
reply.cmd = request.cmd;
- reply.filesize = filesize;
send(client->sock, (char *) &reply, sizeof(dnbd3_reply_t), 0);
break;
@@ -94,6 +90,7 @@ void *dnbd3_handle_query(void *dnbd3_client)
}
close(client->sock);
+ close(image_file);
_dnbd3_clients = g_slist_remove(_dnbd3_clients, client);
free(client);
printf("INFO: Client %s exit\n", client->ip);
diff --git a/src/server/server.c b/src/server/server.c
index ad4c20b..a324682 100644
--- a/src/server/server.c
+++ b/src/server/server.c
@@ -31,15 +31,16 @@
#include "server.h"
#include "utils.h"
-#include "hashtable.h"
#include "signal.h"
#include "net.h"
int _sock;
+GSList *_dnbd3_clients = NULL;
pthread_spinlock_t _spinlock;
char *_config_file_name = DEFAULT_CONFIG_FILE;
-GSList *_dnbd3_clients = NULL;
+dnbd3_image_t *_images;
+size_t _num_images = 0;
void dnbd3_print_help(char* argv_0)
{
@@ -66,7 +67,7 @@ void dnbd3_cleanup()
GSList *iterator = NULL;
for (iterator = _dnbd3_clients; iterator; iterator = iterator->next)
{
- dnbd3_client_t *client = iterator->data;
+ dnbd3_client_t *client = iterator->data;
shutdown(client->sock, SHUT_RDWR);
pthread_join(*client->thread, NULL);
}
@@ -74,6 +75,7 @@ void dnbd3_cleanup()
g_slist_free(_dnbd3_clients);
close(_sock);
+ free(_images);
dnbd3_delete_pid_file();
exit(EXIT_SUCCESS);
}
@@ -172,7 +174,7 @@ int main(int argc, char* argv[])
dnbd3_client->sock = fd;
dnbd3_client->thread = &thread;
- _dnbd3_clients = g_slist_append (_dnbd3_clients, dnbd3_client);
+ _dnbd3_clients = g_slist_append(_dnbd3_clients, dnbd3_client);
pthread_create(&(thread), NULL, dnbd3_handle_query, (void *) (uintptr_t) dnbd3_client);
}
diff --git a/src/server/server.h b/src/server/server.h
index 9372c55..303d6ee 100644
--- a/src/server/server.h
+++ b/src/server/server.h
@@ -22,7 +22,8 @@
#define SERVER_H_
#include <stdint.h>
-#include <glib-2.0/glib.h>
+#include <stdio.h>
+#include <glib.h>
#include "config.h"
#include "../types.h"
@@ -34,9 +35,22 @@ typedef struct
pthread_t *thread;
} dnbd3_client_t;
+typedef struct
+{
+ char *file;
+ off_t filesize;
+ char **servers;
+ size_t num;
+ int vid;
+ int rid;
+} dnbd3_image_t;
+
+extern GSList *_dnbd3_clients;
extern pthread_spinlock_t _spinlock;
extern char *_config_file_name;
-extern GSList *_dnbd3_clients;
+extern dnbd3_image_t *_images;
+extern size_t _num_images;
+
void dnbd3_cleanup();
diff --git a/src/server/utils.c b/src/server/utils.c
index 3a42849..f3a4c5e 100644
--- a/src/server/utils.c
+++ b/src/server/utils.c
@@ -18,12 +18,15 @@
*
*/
+#include "server.h"
#include "utils.h"
-#include "hashtable.h"
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+
void dnbd3_write_pid_file(pid_t pid)
{
@@ -65,52 +68,81 @@ void dnbd3_delete_pid_file()
}
}
-void dnbd3_load_config(char* config_file_name)
+void dnbd3_send_signal(int signum)
+{
+ pid_t pid = dnbd3_read_pid_file();
+ if (pid != 0)
+ {
+ if (kill(pid, signum) != 0)
+ {
+ printf("ERROR: dnbd3-server is not running\n");
+ dnbd3_delete_pid_file();
+ }
+ }
+ else
+ {
+ printf("ERROR: dnbd3-server is not running\n");
+ }
+}
+
+void dnbd3_load_config(char *file)
{
- dnbd3_ht_create();
- FILE *config_file = fopen(config_file_name, "r");
+ int fd;
+ gint i;
+ GKeyFile* gkf;
- if (config_file == NULL)
+ gkf = g_key_file_new();
+ if (!g_key_file_load_from_file(gkf, file, G_KEY_FILE_NONE, NULL))
{
- printf("ERROR: Config file not found: %s\n", config_file_name);
+ printf("ERROR: Config file not found: %s\n", file);
exit(EXIT_FAILURE);
}
- char line[MAX_FILE_NAME + 1 + MAX_FILE_ID];
- char* image_name = NULL;
- char* image_id = NULL;
+ gchar **groups = NULL;
+ groups = g_key_file_get_groups(gkf, &_num_images);
+ _images = calloc(_num_images, sizeof(dnbd3_image_t));
- while (fgets(line, sizeof(line), config_file) != NULL)
+ for (i = 0; i < _num_images; i++)
{
- sscanf(line, "%as %as", &image_name, &image_id);
- if (dnbd3_ht_insert(image_id, image_name) < 0)
+ _images[i].file = g_key_file_get_string(gkf, groups[i], "file", NULL);
+ _images[i].servers = g_key_file_get_string_list(gkf, groups[i], "servers", &_images[i].num, NULL);
+ _images[i].vid = g_key_file_get_integer(gkf, groups[i], "vid", NULL);
+ _images[i].rid = g_key_file_get_integer(gkf, groups[i], "rid", NULL);
+
+ fd = open(_images[i].file, O_RDONLY);
+ if (fd > 0)
+ {
+ struct stat st;
+ fstat(fd, &st);
+ _images[i].filesize = st.st_size;
+ }
+ else
{
- printf("ERROR: Image name or ID is too big\n");
- exit(EXIT_FAILURE);
+ printf("ERROR: Image not found: %s\n", _images[i].file);
}
+ close(fd);
}
- fclose(config_file);
+
+ g_strfreev(groups);
+ g_key_file_free (gkf);
}
void dnbd3_reload_config(char* config_file_name)
{
- dnbd3_ht_destroy();
+ free(_images);
+ _num_images = 0;
dnbd3_load_config(config_file_name);
}
-void dnbd3_send_signal(int signum)
+dnbd3_image_t* dnbd3_get_image(int vid, int rid)
{
- pid_t pid = dnbd3_read_pid_file();
- if (pid != 0)
- {
- if (kill(pid, signum) != 0)
- {
- printf("ERROR: dnbd3-server is not running\n");
- dnbd3_delete_pid_file();
- }
- }
- else
- {
- printf("ERROR: dnbd3-server is not running\n");
+ // TODO: find better data structure
+ dnbd3_image_t *result = NULL;
+ int i;
+ for (i = 0; i < _num_images; ++i) {
+ if (_images[i].vid == vid && _images[i].rid == rid)
+ result = &_images[i];
+
}
+ return result;
}
diff --git a/src/server/utils.h b/src/server/utils.h
index ab8f839..6ac51ff 100644
--- a/src/server/utils.h
+++ b/src/server/utils.h
@@ -30,9 +30,10 @@ pid_t dnbd3_read_pid_file();
void dnbd3_write_pid_file(pid_t pid);
void dnbd3_delete_pid_file();
-void dnbd3_load_config(char* config_file_name);
-void dnbd3_reload_config(char* config_file_name);
-
void dnbd3_send_signal(int signum);
+void dnbd3_load_config(char *file);
+void dnbd3_reload_config(char* config_file_name);
+dnbd3_image_t* dnbd3_get_image(int vid, int rid);
+
#endif /* UTILS_H_ */