[SCM] CTDB repository - branch master updated - ctdb-1.0.104-6-gcb8df97

Ronnie Sahlberg sahlberg at samba.org
Sun Nov 15 21:13:10 MST 2009


The branch, master has been updated
       via  cb8df973ccd446d87fbdd9a27843e54841ba5d89 (commit)
      from  b6bde176af69354ccfb00e6a3169f6b355a59d15 (commit)

http://gitweb.samba.org/?p=sahlberg/ctdb.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit cb8df973ccd446d87fbdd9a27843e54841ba5d89
Author: Ronnie Sahlberg <ronniesahlberg at gmail.com>
Date:   Mon Nov 16 15:17:32 2009 +1100

    set up a pipe betweent he main daemon and the child we use for syslogling so that we can clean up the childprocess when we stop ctdbd

-----------------------------------------------------------------------

Summary of changes:
 include/ctdb_private.h |    1 +
 server/ctdb_logging.c  |  102 +++++++++++++++++++++++++++++++++++++----------
 2 files changed, 81 insertions(+), 22 deletions(-)


Changeset truncated at 500 lines:

diff --git a/include/ctdb_private.h b/include/ctdb_private.h
index ebb6d4a..3fcfdfa 100644
--- a/include/ctdb_private.h
+++ b/include/ctdb_private.h
@@ -440,6 +440,7 @@ struct ctdb_context {
 	const char *default_public_interface;
 	pid_t ctdbd_pid;
 	pid_t recoverd_pid;
+	pid_t syslogd_pid;
 	bool done_startup;
 	const char *node_ip;
 	struct ctdb_monitor_state *monitor;
diff --git a/server/ctdb_logging.c b/server/ctdb_logging.c
index a404cdf..ee472f1 100644
--- a/server/ctdb_logging.c
+++ b/server/ctdb_logging.c
@@ -25,8 +25,6 @@
 #include "system/time.h"
 #include "system/filesys.h"
 
-static bool syslogd_is_started;
-
 struct syslog_message {
 	uint32_t level;
 	uint32_t len;
@@ -34,30 +32,97 @@ struct syslog_message {
 };
 
 
+struct ctdb_syslog_state {
+	int syslog_fd;
+	int fd[2];
+};
+
+static int syslogd_is_started = 0;
+
+
+/* called when child is finished
+ * this is for the syslog daemon, we can not use DEBUG here
+ */
+static void ctdb_syslog_handler(struct event_context *ev, struct fd_event *fde, 
+				      uint16_t flags, void *p)
+{
+	struct ctdb_syslog_state *state = talloc_get_type(p, struct ctdb_syslog_state);
+
+	int count;
+	char str[65536];
+	struct syslog_message *msg;
+
+	if (state == NULL) {
+		return;
+	}
+
+	count = recv(state->syslog_fd, str, sizeof(str), 0);
+	if (count < sizeof(struct syslog_message)) {
+		return;
+	}
+	msg = (struct syslog_message *)str;
+
+	syslog(msg->level, "%s", msg->message);
+}
+
+
+/* called when the pipd from the main daemon has closed
+ * this is for the syslog daemon, we can not use DEBUG here
+ */
+static void ctdb_syslog_terminate_handler(struct event_context *ev, struct fd_event *fde, 
+				      uint16_t flags, void *p)
+{
+	syslog(LOG_ERR, "Shutting down SYSLOG daemon with pid:%d", (int)getpid());
+	_exit(0);
+}
+
+
 
 /*
  * this is for the syslog daemon, we can not use DEBUG here
  */
 int start_syslog_daemon(struct ctdb_context *ctdb)
 {
-	pid_t child;
-	int syslog_fd = -1;
 	struct sockaddr_in syslog_sin;
+	struct ctdb_syslog_state *state;
+
+	state = talloc(ctdb, struct ctdb_syslog_state);
+	CTDB_NO_MEMORY(ctdb, state);
 
-	child = fork();
-	if (child == (pid_t)-1) {
+	if (pipe(state->fd) != 0) {
+		printf("Failed to create syslog pipe\n");
+		talloc_free(state);
+		return -1;
+	}
+	
+	ctdb->syslogd_pid = fork();
+	if (ctdb->syslogd_pid == (pid_t)-1) {
 		printf("Failed to create syslog child process\n");
+		close(state->fd[0]);
+		close(state->fd[1]);
+		talloc_free(state);
 		return -1;
 	}
 
 	syslogd_is_started = 1;
 
-	if (child != 0) {
+	if (ctdb->syslogd_pid != 0) {
+		DEBUG(DEBUG_ERR,("Starting SYSLOG child process with pid:%d\n", (int)ctdb->syslogd_pid));
+
+		close(state->fd[1]);
+		set_close_on_exec(state->fd[0]);
+
 		return 0;
 	}
 
-	syslog_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
-	if (syslog_fd == -1) {
+	syslog(LOG_ERR, "Starting SYSLOG daemon with pid:%d", (int)getpid());
+
+	close(state->fd[0]);
+	event_add_fd(ctdb->ev, state, state->fd[1], EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
+		     ctdb_syslog_terminate_handler, state);
+
+	state->syslog_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+	if (state->syslog_fd == -1) {
 		printf("Failed to create syslog socket\n");
 		return -1;
 	}
@@ -66,7 +131,7 @@ int start_syslog_daemon(struct ctdb_context *ctdb)
 	syslog_sin.sin_port   = htons(CTDB_PORT);
 	syslog_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);	
 
-	if (bind(syslog_fd, &syslog_sin, sizeof(syslog_sin)) == -1) {
+	if (bind(state->syslog_fd, &syslog_sin, sizeof(syslog_sin)) == -1) {
 		if (errno == EADDRINUSE) {
 			/* this is ok, we already have a syslog daemon */
 			_exit(0);
@@ -75,20 +140,13 @@ int start_syslog_daemon(struct ctdb_context *ctdb)
 		_exit(10);
 	}
 
-	/* just loop over the messages */
-	while (1) {
-		int count;
-		char str[1024];
-		struct syslog_message *msg;
 
-		count = recv(syslog_fd, str, sizeof(str), 0);
-		if (count < sizeof(struct syslog_message)) {
-			continue;
-		}
-		msg = (struct syslog_message *)str;
+	event_add_fd(ctdb->ev, state, state->syslog_fd, EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
+		     ctdb_syslog_handler, state);
 
-		syslog(msg->level, "%s", msg->message);
-	}
+	event_loop_wait(ctdb->ev);
+
+	/* this should not happen */
 	_exit(10);
 }
 


-- 
CTDB repository


More information about the samba-cvs mailing list