Rev 582: make sure we still run events when waiting for ctdb_event_script() in http://samba.org/~tridge/ctdb

tridge at samba.org tridge at samba.org
Thu Jul 19 03:36:01 GMT 2007


------------------------------------------------------------
revno: 582
revision-id: tridge at samba.org-20070719033600-x0n3k9dq1dbr4e14
parent: tridge at samba.org-20070718101357-gv3ugzegujz4f5vu
committer: Andrew Tridgell <tridge at samba.org>
branch nick: tridge
timestamp: Thu 2007-07-19 13:36:00 +1000
message:
  make sure we still run events when waiting for ctdb_event_script()
modified:
  server/eventscript.c           eventscript.c-20070704074533-95f10rsay8um8wrr-1
=== modified file 'server/eventscript.c'
--- a/server/eventscript.c	2007-07-12 23:35:46 +0000
+++ b/server/eventscript.c	2007-07-19 03:36:00 +0000
@@ -58,22 +58,6 @@
 	return ret;
 }
 
-/*
-  run the event script
- */
-int ctdb_event_script(struct ctdb_context *ctdb, const char *fmt, ...)
-{
-	va_list ap;
-	int ret;
-
-	va_start(ap, fmt);
-	ret = ctdb_event_script_v(ctdb, fmt, ap);
-	va_end(ap);
-
-	return ret;
-}
-
-
 struct ctdb_event_script_state {
 	struct ctdb_context *ctdb;
 	pid_t child;
@@ -131,15 +115,14 @@
   run the event script in the background, calling the callback when 
   finished
  */
-int ctdb_event_script_callback(struct ctdb_context *ctdb, 
-			       struct timeval timeout,
-			       TALLOC_CTX *mem_ctx,
-			       void (*callback)(struct ctdb_context *, int, void *),
-			       void *private_data,
-			       const char *fmt, ...)
+static int ctdb_event_script_callback_v(struct ctdb_context *ctdb, 
+					struct timeval timeout,
+					TALLOC_CTX *mem_ctx,
+					void (*callback)(struct ctdb_context *, int, void *),
+					void *private_data,
+					const char *fmt, va_list ap)
 {
 	struct ctdb_event_script_state *state;
-	va_list ap;
 	int ret;
 
 	state = talloc(mem_ctx, struct ctdb_event_script_state);
@@ -170,9 +153,7 @@
 			ctdb_restore_scheduler(ctdb);
 		}
 		set_close_on_exec(state->fd[1]);
-		va_start(ap, fmt);
 		ret = ctdb_event_script_v(ctdb, fmt, ap);
-		va_end(ap);
 		_exit(ret);
 	}
 
@@ -191,3 +172,69 @@
 }
 
 
+/*
+  run the event script in the background, calling the callback when 
+  finished
+ */
+int ctdb_event_script_callback(struct ctdb_context *ctdb, 
+			       struct timeval timeout,
+			       TALLOC_CTX *mem_ctx,
+			       void (*callback)(struct ctdb_context *, int, void *),
+			       void *private_data,
+			       const char *fmt, ...)
+{
+	va_list ap;
+	int ret;
+
+	va_start(ap, fmt);
+	ret = ctdb_event_script_callback_v(ctdb, timeout, mem_ctx, callback, private_data, fmt, ap);
+	va_end(ap);
+
+	return ret;
+}
+
+
+struct callback_status {
+	bool done;
+	int status;
+};
+
+/*
+  called when ctdb_event_script() finishes
+ */
+static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
+{
+	struct callback_status *s = (struct callback_status *)private_data;
+	s->done = true;
+	s->status = status;
+}
+
+/*
+  run the event script, waiting for it to complete. Used when the caller doesn't want to 
+  continue till the event script has finished.
+ */
+int ctdb_event_script(struct ctdb_context *ctdb, const char *fmt, ...)
+{
+	va_list ap;
+	int ret;
+	TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
+	struct callback_status status;
+
+	va_start(ap, fmt);
+	ret = ctdb_event_script_callback_v(ctdb, timeval_zero(), tmp_ctx, event_script_callback, &status, fmt, ap);
+	va_end(ap);
+
+	if (ret != 0) {
+		talloc_free(tmp_ctx);
+		return ret;
+	}
+
+	status.status = -1;
+	status.done = false;
+
+	while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
+
+	talloc_free(tmp_ctx);
+
+	return status.status;
+}



More information about the samba-cvs mailing list