Skip to contentSkip to conversation

Xenstore minor cleanup

PATCH In review Rusty Russell 1 participant 2 messages Jun 24, 2005

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Xenstore minor cleanup

Update TODO list
Wrap opendir in talloc so it gets cleaned up on OOM.
Remove last call to system by open-coding "cp -al" to create transaction.

Index: TODO
===================================================================
RCS file: /var/cvs/xeno-unstable/tools/xenstore/TODO,v retrieving revision 1.2
retrieving revision 1.3

TODO +3 -1
diff -u -r1.2 -r1.3
@@ -4,4 +4,6 @@
 
 - Remove calls to system() from daemon
 - Timeout failed watch responses
-- Timeout blocking transactions
+- Dynamic nodes
+- Persistant storage of introductions, watches and transactions, so daemon can restart
+- Remove assumption that rename doesn't fail

Index: xenstored_core.c
===================================================================
RCS file: /var/cvs/xeno-unstable/tools/xenstore/xenstored_core.c,v retrieving revision 1.3
retrieving revision 1.6

xenstored_core.c +31 -7
diff -u -r1.3 -r1.6
@@ -246,7 +246,7 @@
 }
 
 /* Read everything from a talloc_open'ed fd. */
-static void *read_all(int *fd, unsigned int *size)
+void *read_all(int *fd, unsigned int *size)
 {
 	unsigned int max = 4;
 	int ret;
@@ -271,7 +271,7 @@
 }
 
 /* Return a pointer to an fd, self-closing and attached to this pathname. */
-static int *talloc_open(const char *pathname, int flags, int mode)
+int *talloc_open(const char *pathname, int flags, int mode)
 {
 	int *fd;
 
@@ -534,6 +534,30 @@
 	return tmppath;
 }
 
+static int destroy_opendir(void *_dir)
+{
+	DIR **dir = _dir;
+	closedir(*dir);
+	return 0;
+}
+
+/* Return a pointer to a DIR*, self-closing and attached to this pathname. */
+DIR **talloc_opendir(const char *pathname)
+{
+	DIR **dir;
+
+	dir = talloc(pathname, DIR *);
+	*dir = opendir(pathname);
+	if (!*dir) {
+		int saved_errno = errno;
+		talloc_free(dir);
+		errno = saved_errno;
+		return NULL;
+	}
+	talloc_set_destructor(dir, destroy_opendir);
+	return dir;
+}
+
 /* We assume rename() doesn't fail on moves in same dir. */
 static void commit_tempfile(const char *path)
 {
@@ -677,7 +701,7 @@
 {
 	char *path, *reply = talloc_strdup(node, "");
 	unsigned int reply_len = 0;
-	DIR *dir;
+	DIR **dir;
 	struct dirent *dirent;
 
 	node = canonicalize(conn, node);
@@ -685,11 +709,11 @@
 		return send_error(conn, errno);
 
 	path = node_dir(conn->transaction, node);
-	dir = opendir(path);
+	dir = talloc_opendir(path);
 	if (!dir)
 		return send_error(conn, errno);
 
-	while ((dirent = readdir(dir)) != NULL) {
+	while ((dirent = readdir(*dir)) != NULL) {
 		int len = strlen(dirent->d_name) + 1;
 
 		if (!valid_chars(dirent->d_name))
@@ -699,7 +723,6 @@
 		strcpy(reply + reply_len, dirent->d_name);
 		reply_len += len;
 	}
-	closedir(dir);
 
 	return send_reply(conn, XS_DIRECTORY, reply, reply_len);
 }
@@ -783,7 +806,8 @@
 		return send_error(conn, EINVAL);
 
 	node = canonicalize(conn, vec[0]);
-	if (!within_transaction(conn->transaction, node))
+	if (/*suppress error on write outside transaction*/ 0 &&
+            !within_transaction(conn->transaction, node))
 		return send_error(conn, EROFS);
 
 	if (transaction_block(conn, node))

Index: xenstored_core.h
===================================================================
RCS file: /var/cvs/xeno-unstable/tools/xenstore/xenstored_core.h,v retrieving revision 1.2
retrieving revision 1.3

xenstored_core.h +11 -0
diff -u -r1.2 -r1.3
@@ -20,6 +20,8 @@
 #ifndef _XENSTORED_CORE_H
 #define _XENSTORED_CORE_H
 
+#include <sys/types.h>
+#include <dirent.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <errno.h>
@@ -129,7 +131,16 @@
 /* Is this a valid node name? */
 bool is_valid_nodename(const char *node);
 
+/* Return a pointer to an open dir, self-closig and attached to pathname. */
+DIR **talloc_opendir(const char *pathname);
+
+/* Return a pointer to an fd, self-closing and attached to this pathname. */
+int *talloc_open(const char *pathname, int flags, int mode);
+
 /* Convenient talloc-style destructor for paths. */
 int destroy_path(void *path);
 
+/* Read entire contents of a talloced fd. */
+void *read_all(int *fd, unsigned int *size);
+
 #endif /* _XENSTORED_CORE_H */

Index: xenstored_transaction.c
===================================================================
RCS file: /var/cvs/xeno-unstable/tools/xenstore/xenstored_transaction.c,v retrieving revision 1.1
retrieving revision 1.2

xenstored_transaction.c +63 -21

129 lines not on this page · raw message

Signature · 1 line
A bad analogy is like a leaky screwdriver -- Richard Braakman
#1 lore raw
Quoted 5 lines · Rusty Russell

Update TODO list
Wrap opendir in talloc so it gets cleaned up on OOM.
Remove last call to system by open-coding "cp -al" to create transaction.

Oops, disabling EROFS outside transaction snuck in. Here's the right patch:

Update TODO list
Wrap opendir in talloc so it gets cleaned up on OOM.
Remove last call to system by open-coding "cp -al" to create transaction.

Index: TODO
===================================================================
RCS file: /var/cvs/xeno-unstable/tools/xenstore/TODO,v retrieving revision 1.2
retrieving revision 1.3

TODO +3 -1
diff -u -r1.2 -r1.3
@@ -4,4 +4,6 @@
 
 - Remove calls to system() from daemon
 - Timeout failed watch responses
-- Timeout blocking transactions
+- Dynamic nodes
+- Persistant storage of introductions, watches and transactions, so daemon can restart
+- Remove assumption that rename doesn't fail

Index: xenstored_core.c
===================================================================
RCS file: /var/cvs/xeno-unstable/tools/xenstore/xenstored_core.c,v retrieving revision 1.3
retrieving revision 1.6

xenstored_core.c +29 -6
diff -u -r1.3 -r1.6
@@ -246,7 +246,7 @@
 }
 
 /* Read everything from a talloc_open'ed fd. */
-static void *read_all(int *fd, unsigned int *size)
+void *read_all(int *fd, unsigned int *size)
 {
 	unsigned int max = 4;
 	int ret;
@@ -271,7 +271,7 @@
 }
 
 /* Return a pointer to an fd, self-closing and attached to this pathname. */
-static int *talloc_open(const char *pathname, int flags, int mode)
+int *talloc_open(const char *pathname, int flags, int mode)
 {
 	int *fd;
 
@@ -534,6 +534,30 @@
 	return tmppath;
 }
 
+static int destroy_opendir(void *_dir)
+{
+	DIR **dir = _dir;
+	closedir(*dir);
+	return 0;
+}
+
+/* Return a pointer to a DIR*, self-closing and attached to this pathname. */
+DIR **talloc_opendir(const char *pathname)
+{
+	DIR **dir;
+
+	dir = talloc(pathname, DIR *);
+	*dir = opendir(pathname);
+	if (!*dir) {
+		int saved_errno = errno;
+		talloc_free(dir);
+		errno = saved_errno;
+		return NULL;
+	}
+	talloc_set_destructor(dir, destroy_opendir);
+	return dir;
+}
+
 /* We assume rename() doesn't fail on moves in same dir. */
 static void commit_tempfile(const char *path)
 {
@@ -677,7 +701,7 @@
 {
 	char *path, *reply = talloc_strdup(node, "");
 	unsigned int reply_len = 0;
-	DIR *dir;
+	DIR **dir;
 	struct dirent *dirent;
 
 	node = canonicalize(conn, node);
@@ -685,11 +709,11 @@
 		return send_error(conn, errno);
 
 	path = node_dir(conn->transaction, node);
-	dir = opendir(path);
+	dir = talloc_opendir(path);
 	if (!dir)
 		return send_error(conn, errno);
 
-	while ((dirent = readdir(dir)) != NULL) {
+	while ((dirent = readdir(*dir)) != NULL) {
 		int len = strlen(dirent->d_name) + 1;
 
 		if (!valid_chars(dirent->d_name))
@@ -699,7 +723,6 @@
 		strcpy(reply + reply_len, dirent->d_name);
 		reply_len += len;
 	}
-	closedir(dir);
 
 	return send_reply(conn, XS_DIRECTORY, reply, reply_len);
 }

Index: xenstored_core.h
===================================================================
RCS file: /var/cvs/xeno-unstable/tools/xenstore/xenstored_core.h,v retrieving revision 1.2
retrieving revision 1.3

xenstored_core.h +11 -0
diff -u -r1.2 -r1.3
@@ -20,6 +20,8 @@
 #ifndef _XENSTORED_CORE_H
 #define _XENSTORED_CORE_H
 
+#include <sys/types.h>
+#include <dirent.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <errno.h>
@@ -129,7 +131,16 @@
 /* Is this a valid node name? */
 bool is_valid_nodename(const char *node);
 
+/* Return a pointer to an open dir, self-closig and attached to pathname. */
+DIR **talloc_opendir(const char *pathname);
+
+/* Return a pointer to an fd, self-closing and attached to this pathname. */
+int *talloc_open(const char *pathname, int flags, int mode);
+
 /* Convenient talloc-style destructor for paths. */
 int destroy_path(void *path);
 
+/* Read entire contents of a talloced fd. */
+void *read_all(int *fd, unsigned int *size);
+
 #endif /* _XENSTORED_CORE_H */

Index: xenstored_transaction.c
===================================================================
RCS file: /var/cvs/xeno-unstable/tools/xenstore/xenstored_transaction.c,v retrieving revision 1.1
retrieving revision 1.2

xenstored_transaction.c +63 -21

129 lines not on this page · raw message

Signature · 1 line
A bad analogy is like a leaky screwdriver -- Richard Braakman