summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsr2012-09-25 19:56:04 +0200
committersr2012-09-25 19:56:04 +0200
commit5a329ef4cba5688842d7d945e5c5956109797e60 (patch)
tree720c56b41892e0e9772590d5e86fb3628fe2441f
parent[SERVER] IPC: Add calls to add/remove trusted namespaces and servers (diff)
downloaddnbd3-5a329ef4cba5688842d7d945e5c5956109797e60.tar.gz
dnbd3-5a329ef4cba5688842d7d945e5c5956109797e60.tar.xz
dnbd3-5a329ef4cba5688842d7d945e5c5956109797e60.zip
[SERVER] Fix return code for IPC_IMAGE_ADD
[SERVER] Check if image exists on IPC_IMAGE_ADD
-rw-r--r--src/server/helper.c26
-rw-r--r--src/server/helper.h2
-rw-r--r--src/server/ipc.c44
-rw-r--r--src/server/job.c7
-rw-r--r--src/server/saveload.h1
5 files changed, 64 insertions, 16 deletions
diff --git a/src/server/helper.c b/src/server/helper.c
index c86fcb6..e5f520a 100644
--- a/src/server/helper.c
+++ b/src/server/helper.c
@@ -3,6 +3,7 @@
#include <string.h>
#include <stdlib.h>
#include <glib/gmacros.h>
+#include <fcntl.h>
#include "../config.h"
/**
@@ -167,3 +168,28 @@ void remove_trailing_slash(char *string)
while (ptr >= string && *ptr == '/')
*ptr-- = '\0';
}
+
+int file_exists(char *file)
+{
+ int fd = open(file, O_RDONLY);
+ if (fd < 0)
+ return FALSE;
+ close(fd);
+ return TRUE;
+}
+
+int file_writable(char *file)
+{
+ int fd = open(file, O_WRONLY);
+ if (fd >= 0)
+ {
+ close(fd);
+ return TRUE;
+ }
+ fd = open(file, O_WRONLY | O_CREAT, 0600);
+ if (fd < 0)
+ return FALSE;
+ close(fd);
+ unlink(file);
+ return TRUE;
+}
diff --git a/src/server/helper.h b/src/server/helper.h
index 8a50f90..5145905 100644
--- a/src/server/helper.h
+++ b/src/server/helper.h
@@ -13,6 +13,8 @@ char is_valid_namespace(char *namespace);
char is_valid_imagename(char *namespace);
void strtolower(char *string);
void remove_trailing_slash(char *string);
+int file_exists(char *file);
+int file_writable(char *file);
static inline int is_same_server(const dnbd3_host_t *const a, const dnbd3_host_t *const b)
{
diff --git a/src/server/ipc.c b/src/server/ipc.c
index 76c3ff5..5ee1194 100644
--- a/src/server/ipc.c
+++ b/src/server/ipc.c
@@ -361,7 +361,7 @@ static int ipc_receive(int client_sock)
goto get_info_reply_cleanup;
xmlDocSetRootElement(docReply, root_node);
- xmlNewTextChild(root_node, NULL, BAD_CAST "namespace", BAD_CAST _local_namespace);
+ xmlNewTextChild(root_node, NULL, BAD_CAST "defaultns", BAD_CAST _local_namespace);
// Images
parent_node = xmlNewNode(NULL, BAD_CAST "images");
@@ -441,10 +441,11 @@ static int ipc_receive(int client_sock)
tmp_node = xmlNewNode(NULL, BAD_CAST "server");
if (tmp_node == NULL)
goto get_info_reply_cleanup;
+ xmlNodePtr namespace_root = xmlNewNode(NULL, BAD_CAST "namespaces");
+ if (namespace_root == NULL)
+ goto get_info_reply_cleanup;
host_to_string(&server->host, strbuffer, STRBUFLEN);
- xmlNewProp(tmp_node, BAD_CAST "ip", BAD_CAST strbuffer);
- sprintf(strbuffer, "%d", (int)ntohs(server->host.port));
- xmlNewProp(tmp_node, BAD_CAST "port", BAD_CAST strbuffer);
+ xmlNewProp(tmp_node, BAD_CAST "address", BAD_CAST strbuffer);
if (server->comment)
xmlNewProp(tmp_node, BAD_CAST "comment", BAD_CAST server->comment);
for (iterator2 = server->namespaces; iterator2; iterator2 = iterator2->next)
@@ -453,7 +454,7 @@ static int ipc_receive(int client_sock)
server_node = xmlNewNode(NULL, BAD_CAST "namespace");
if (server_node == NULL)
goto get_info_reply_cleanup;
- xmlAddChild(tmp_node, server_node);
+ xmlAddChild(namespace_root, server_node);
xmlNewProp(server_node, BAD_CAST "name", BAD_CAST ns->name);
if (ns->auto_replicate)
xmlNewProp(server_node, BAD_CAST "replicate", BAD_CAST "1");
@@ -461,6 +462,7 @@ static int ipc_receive(int client_sock)
xmlNewProp(server_node, BAD_CAST "recursive", BAD_CAST "1");
}
xmlAddChild(parent_node, tmp_node);
+ xmlAddChild(tmp_node, namespace_root);
}
}
pthread_spin_unlock(&_spinlock);
@@ -522,16 +524,27 @@ get_info_reply_cleanup:
char *rid_str = (char *)XML_GETPROP(cur, "rid");
image.file = (char *)XML_GETPROP(cur, "file");
image.cache_file = (char *)XML_GETPROP(cur, "cache");
- if (image.config_group && rid_str && image.file && image.cache_file)
+ if (image.file && !file_exists(image.file))
{
- image.rid = atoi(rid_str);
- if (cmd == IPC_ADDIMG)
- header.error = htonl(dnbd3_add_image(&image));
- else
- header.error = htonl(dnbd3_del_image(&image));
+ header.error = htonl(ERROR_FILE_NOT_FOUND);
+ }
+ else if (image.cache_file && !file_writable(image.cache_file))
+ {
+ header.error = htonl(ERROR_NOT_WRITABLE);
}
else
- header.error = htonl(ERROR_MISSING_ARGUMENT);
+ {
+ if (image.config_group && rid_str)
+ {
+ image.rid = atoi(rid_str);
+ if (cmd == IPC_ADDIMG)
+ header.error = htonl(dnbd3_add_image(&image));
+ else
+ header.error = htonl(dnbd3_del_image(&image));
+ }
+ else
+ header.error = htonl(ERROR_MISSING_ARGUMENT);
+ }
FREE_POINTERLIST;
} END_FOR_EACH;
if (count == 0)
@@ -541,6 +554,7 @@ get_info_reply_cleanup:
header.error = htonl(ERROR_INVALID_XML);
header.size = htonl(0);
+ printf("Code: %d\n", (int)ntohl(header.error));
return_value = send_data(client_sock, &header, sizeof(header));
break;
@@ -765,12 +779,12 @@ void dnbd3_ipc_send(int cmd)
continue;
NEW_POINTERLIST;
++count;
- char *ip = XML_GETPROP(cur, "ip");
+ char *address = XML_GETPROP(cur, "address");
char *comment = XML_GETPROP(cur, "comment");
if (comment)
- printf("%-30s (%s)\n", ip, comment);
+ printf("%-30s (%s)\n", address, comment);
else
- printf("%-30s\n", ip);
+ printf("%-30s\n", address);
for (childit = cur->children; childit; childit = childit->next)
{
if (childit->type != XML_ELEMENT_NODE || childit->name == NULL || strcmp((const char*)childit->name, "namespace") != 0)
diff --git a/src/server/job.c b/src/server/job.c
index 7612573..5ac0721 100644
--- a/src/server/job.c
+++ b/src/server/job.c
@@ -210,7 +210,12 @@ static void connect_proxy_images()
pthread_spin_unlock(&_spinlock);
int dh = open(devname, O_RDWR);
if (dh < 0)
+ {
+ pthread_spin_lock(&_spinlock);
+ return_free_device(devname);
+ pthread_spin_unlock(&_spinlock);
continue;
+ }
for (s = 0; s < NUMBER_SERVERS; ++s)
{
if (servers[s].host.type == 0)
@@ -449,7 +454,7 @@ static void query_servers()
goto communication_error;
}
// Data seems ok
- char *ns = getTextFromPath(doc, "/data/namespace");
+ char *ns = getTextFromPath(doc, "/data/defaultns");
if (ns && *ns == '\0')
{
xmlFree(ns);
diff --git a/src/server/saveload.h b/src/server/saveload.h
index e45a41a..77528e4 100644
--- a/src/server/saveload.h
+++ b/src/server/saveload.h
@@ -40,6 +40,7 @@
#define ERROR_UNKNOWN_COMMAND 10
#define ERROR_SEE_LOG 11
#define ERROR_WRONG_PASSWORD 12
+#define ERROR_NOT_WRITABLE 13
void dnbd3_load_config();
int dnbd3_save_config();