[SCM] Samba Shared Repository - branch master updated

Amitay Isaacs amitay at samba.org
Tue Aug 29 13:04:01 UTC 2017


The branch, master has been updated
       via  5b7bc0a ctdb-tests: Add sock daemon test for do_fork
       via  b838019 ctdb-tests: Add sock daemon test for create_session
       via  94cc5ea ctdb-common: Allow sock_daemon to daemonise during startup
       via  4cb5603 ctdb-tests: Add a sock_daemon test for PID file contention
       via  b67cc00 ctdb-common: Move PID file creation to sock_daemon_run_send()
       via  fc6fdde ctdb-tests: Add improved PID file check to test2
       via  8149c98 ctdb-tests: Add comments describing sock daemon tests
       via  710f5d9 ctdb-daemon: Use become_daemon() instead of custom code
       via  5f5762d ctdb-daemon: Narrow a #include
      from  3164c0a ldb_tdb: Rework ltdb_modify_internal() to use ltdb_search_dn1() internally

https://git.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit 5b7bc0ae13ccc8e0ec1ba0c4b57c72a7d1dec1be
Author: Martin Schwenke <martin at meltin.net>
Date:   Wed Aug 23 20:32:29 2017 +1000

    ctdb-tests: Add sock daemon test for do_fork
    
    Signed-off-by: Martin Schwenke <martin at meltin.net>
    Reviewed-by: Amitay Isaacs <amitay at gmail.com>
    
    Autobuild-User(master): Amitay Isaacs <amitay at samba.org>
    Autobuild-Date(master): Tue Aug 29 15:03:33 CEST 2017 on sn-devel-144

commit b83801979f69635b885778307b5156e90b5b8b32
Author: Martin Schwenke <martin at meltin.net>
Date:   Wed Aug 16 19:15:32 2017 +1000

    ctdb-tests: Add sock daemon test for create_session
    
    Signed-off-by: Martin Schwenke <martin at meltin.net>
    Reviewed-by: Amitay Isaacs <amitay at gmail.com>

commit 94cc5eaddc6cee3d7b24dc57b6194ef0a0a979ba
Author: Martin Schwenke <martin at meltin.net>
Date:   Wed Aug 16 13:52:08 2017 +1000

    ctdb-common: Allow sock_daemon to daemonise during startup
    
    Signed-off-by: Martin Schwenke <martin at meltin.net>
    Reviewed-by: Amitay Isaacs <amitay at gmail.com>

commit 4cb560386ad5fd10a482cf22698d224540a99ab0
Author: Martin Schwenke <martin at meltin.net>
Date:   Wed Aug 16 13:31:04 2017 +1000

    ctdb-tests: Add a sock_daemon test for PID file contention
    
    Signed-off-by: Martin Schwenke <martin at meltin.net>
    Reviewed-by: Amitay Isaacs <amitay at gmail.com>

commit b67cc00c9335d899402d28f229d2b4416dc8a674
Author: Amitay Isaacs <amitay at gmail.com>
Date:   Mon Aug 28 18:39:40 2017 +1000

    ctdb-common: Move PID file creation to sock_daemon_run_send()
    
    Only create PID file when actually starting the daemon, rather than
    when setting up the context.  This will facilitate future changes.
    
    Tweak test to confirm that PID file is no longer created during setup.
    
    Signed-off-by: Martin Schwenke <martin at meltin.net>
    Reviewed-by: Amitay Isaacs <amitay at gmail.com>

commit fc6fdde60fadf88be3ef9a116feff70fe97a052e
Author: Martin Schwenke <martin at meltin.net>
Date:   Mon Aug 28 16:00:53 2017 +1000

    ctdb-tests: Add improved PID file check to test2
    
    Signed-off-by: Martin Schwenke <martin at meltin.net>
    Reviewed-by: Amitay Isaacs <amitay at gmail.com>

commit 8149c9844e42b0dc6e29d0baa956224e33705768
Author: Martin Schwenke <martin at meltin.net>
Date:   Wed Aug 16 17:37:16 2017 +1000

    ctdb-tests: Add comments describing sock daemon tests
    
    Signed-off-by: Martin Schwenke <martin at meltin.net>
    Reviewed-by: Amitay Isaacs <amitay at gmail.com>

commit 710f5d922ded96f18d475f4143e567997f0d7567
Author: Martin Schwenke <martin at meltin.net>
Date:   Tue Aug 15 12:53:02 2017 +1000

    ctdb-daemon: Use become_daemon() instead of custom code
    
    Signed-off-by: Martin Schwenke <martin at meltin.net>
    Reviewed-by: Amitay Isaacs <amitay at gmail.com>

commit 5f5762d654f85252970ba3a8355908135d90b1d5
Author: Martin Schwenke <martin at meltin.net>
Date:   Tue Aug 15 12:51:59 2017 +1000

    ctdb-daemon: Narrow a #include
    
    Only time.h is needed here, not all of samba_util.h.
    
    Signed-off-by: Martin Schwenke <martin at meltin.net>
    Reviewed-by: Amitay Isaacs <amitay at gmail.com>

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

Summary of changes:
 ctdb/common/sock_daemon.c                |  28 ++-
 ctdb/common/sock_daemon.h                |  12 +-
 ctdb/server/ctdb_daemon.c                |  17 +-
 ctdb/server/ctdb_eventd.c                |   5 +-
 ctdb/tests/cunit/sock_daemon_test_001.sh |  27 +++
 ctdb/tests/src/sock_daemon_test.c        | 396 ++++++++++++++++++++++++++++++-
 6 files changed, 444 insertions(+), 41 deletions(-)


Changeset truncated at 500 lines:

diff --git a/ctdb/common/sock_daemon.c b/ctdb/common/sock_daemon.c
index a6d4301..56205d0 100644
--- a/ctdb/common/sock_daemon.c
+++ b/ctdb/common/sock_daemon.c
@@ -30,6 +30,7 @@
 #include "lib/util/blocking.h"
 #include "lib/util/dlinklist.h"
 #include "lib/util/tevent_unix.h"
+#include "lib/util/become_daemon.h"
 
 #include "common/logging.h"
 #include "common/reqid.h"
@@ -452,7 +453,6 @@ bool sock_socket_write_recv(struct tevent_req *req, int *perr)
 
 int sock_daemon_setup(TALLOC_CTX *mem_ctx, const char *daemon_name,
 		      const char *logging, const char *debug_level,
-		      const char *pidfile,
 		      struct sock_daemon_funcs *funcs,
 		      void *private_data,
 		      struct sock_daemon_context **out)
@@ -476,14 +476,6 @@ int sock_daemon_setup(TALLOC_CTX *mem_ctx, const char *daemon_name,
 		return ret;
 	}
 
-	if (pidfile != NULL) {
-		ret = pidfile_context_create(sockd, pidfile, &sockd->pid_ctx);
-		if (ret != 0) {
-			talloc_free(sockd);
-			return EEXIST;
-		}
-	}
-
 	*out = sockd;
 	return 0;
 }
@@ -537,6 +529,8 @@ static void sock_daemon_run_wait_done(struct tevent_req *subreq);
 struct tevent_req *sock_daemon_run_send(TALLOC_CTX *mem_ctx,
 					struct tevent_context *ev,
 					struct sock_daemon_context *sockd,
+					const char *pidfile,
+					bool do_fork, bool create_session,
 					pid_t pid_watch)
 {
 	struct tevent_req *req, *subreq;
@@ -550,6 +544,17 @@ struct tevent_req *sock_daemon_run_send(TALLOC_CTX *mem_ctx,
 		return NULL;
 	}
 
+	become_daemon(do_fork, !create_session, false);
+
+	if (pidfile != NULL) {
+		int ret = pidfile_context_create(sockd, pidfile,
+						 &sockd->pid_ctx);
+		if (ret != 0) {
+			tevent_req_error(req, EEXIST);
+			return tevent_req_post(req, ev);
+		}
+	}
+
 	state->ev = ev;
 	state->sockd = sockd;
 	state->pid_watch = pid_watch;
@@ -780,13 +785,16 @@ bool sock_daemon_run_recv(struct tevent_req *req, int *perr)
 
 int sock_daemon_run(struct tevent_context *ev,
 		    struct sock_daemon_context *sockd,
+		    const char *pidfile,
+		    bool do_fork, bool create_session,
 		    pid_t pid_watch)
 {
 	struct tevent_req *req;
 	int ret;
 	bool status;
 
-	req = sock_daemon_run_send(ev, ev, sockd, pid_watch);
+	req = sock_daemon_run_send(ev, ev, sockd,
+				   pidfile, do_fork, create_session, pid_watch);
 	if (req == NULL) {
 		return ENOMEM;
 	}
diff --git a/ctdb/common/sock_daemon.h b/ctdb/common/sock_daemon.h
index 81853f6..1821028 100644
--- a/ctdb/common/sock_daemon.h
+++ b/ctdb/common/sock_daemon.h
@@ -149,7 +149,6 @@ bool sock_socket_write_recv(struct tevent_req *req, int *perr);
  * @param[in] daemon_name Name of the daemon, used for logging
  * @param[in] logging Logging setup string
  * @param[in] debug_level Debug level to log at
- * @param[in] pidfile PID file to create, NULL if no PID file required
  * @param[in] funcs Socket daemon callback routines
  * @param[in] private_data Private data associated with callback routines
  * @param[out] result New socket daemon context
@@ -157,7 +156,6 @@ bool sock_socket_write_recv(struct tevent_req *req, int *perr);
  */
 int sock_daemon_setup(TALLOC_CTX *mem_ctx, const char *daemon_name,
 		      const char *logging, const char *debug_level,
-		      const char *pidfile,
 		      struct sock_daemon_funcs *funcs,
 		      void *private_data,
 		      struct sock_daemon_context **result);
@@ -182,12 +180,17 @@ int sock_daemon_add_unix(struct sock_daemon_context *sockd,
  * @param[in] mem_ctx Talloc memory context
  * @param[in] ev Tevent context
  * @param[in] sockd The socket daemon context
+ * @param[in] pidfile PID file to create, NULL if no PID file required
+ * @param[in] do_fork Whether the daemon should fork on startup
+ * @param[in] create_session Whether the daemon should create a new session
  * @param[in] pid_watch PID to watch. If PID goes away, shutdown.
  * @return new tevent request, NULL on failure
  */
 struct tevent_req *sock_daemon_run_send(TALLOC_CTX *mem_ctx,
 					struct tevent_context *ev,
 					struct sock_daemon_context *sockd,
+					const char *pidfile,
+					bool do_fork, bool create_session,
 					pid_t pid_watch);
 
 /**
@@ -204,6 +207,9 @@ bool sock_daemon_run_recv(struct tevent_req *req, int *perr);
  *
  * @param[in] ev Tevent context
  * @param[in] sockd The socket daemon context
+ * @param[in] pidfile PID file to create, NULL if no PID file required
+ * @param[in] do_fork Whether the daemon should fork on startup
+ * @param[in] create_session Whether the daemon should create a new session
  * @param[in] pid_watch PID to watch. If PID goes away, shutdown.
  * @return 0 on success, errno on failure
  *
@@ -211,6 +217,8 @@ bool sock_daemon_run_recv(struct tevent_req *req, int *perr);
  */
 int sock_daemon_run(struct tevent_context *ev,
 		    struct sock_daemon_context *sockd,
+		    const char *pidfile,
+		    bool do_fork, bool create_session,
 		    pid_t pid_watch);
 
 #endif /* __CTDB_SOCK_DAEMON_H__ */
diff --git a/ctdb/server/ctdb_daemon.c b/ctdb/server/ctdb_daemon.c
index 80b334b..41f042e 100644
--- a/ctdb/server/ctdb_daemon.c
+++ b/ctdb/server/ctdb_daemon.c
@@ -32,8 +32,9 @@
 #include "lib/tdb_wrap/tdb_wrap.h"
 #include "lib/util/dlinklist.h"
 #include "lib/util/debug.h"
-#include "lib/util/samba_util.h"
+#include "lib/util/time.h"
 #include "lib/util/blocking.h"
+#include "lib/util/become_daemon.h"
 
 #include "ctdb_version.h"
 #include "ctdb_private.h"
@@ -1197,20 +1198,8 @@ int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork)
 	int res, ret = -1;
 	struct tevent_fd *fde;
 
-	if (do_fork && fork()) {
-		return 0;
-	}
+	become_daemon(do_fork, false, false);
 
-	if (do_fork) {
-		if (setsid() == -1) {
-			ctdb_die(ctdb, "Failed to setsid()\n");
-		}
-		close(0);
-		if (open("/dev/null", O_RDONLY) != 0) {
-			DEBUG(DEBUG_ALERT,(__location__ " Failed to setup stdin on /dev/null\n"));
-			exit(11);
-		}
-	}
 	ignore_signal(SIGPIPE);
 	ignore_signal(SIGUSR1);
 
diff --git a/ctdb/server/ctdb_eventd.c b/ctdb/server/ctdb_eventd.c
index a6ab219..52a2e75 100644
--- a/ctdb/server/ctdb_eventd.c
+++ b/ctdb/server/ctdb_eventd.c
@@ -1197,7 +1197,7 @@ int main(int argc, const char **argv)
 	};
 
 	ret = sock_daemon_setup(mem_ctx, "ctdb-eventd", options.logging,
-				options.debug_level, options.pidfile,
+				options.debug_level,
 				&daemon_funcs, ectx, &sockd);
 	if (ret != 0) {
 		goto fail;
@@ -1215,7 +1215,8 @@ int main(int argc, const char **argv)
 		goto fail;
 	}
 
-	ret = sock_daemon_run(ev, sockd, options.pid);
+	ret = sock_daemon_run(ev, sockd,
+			      options.pidfile, false, false, options.pid);
 	if (ret == EINTR) {
 		ret = 0;
 	}
diff --git a/ctdb/tests/cunit/sock_daemon_test_001.sh b/ctdb/tests/cunit/sock_daemon_test_001.sh
index 72e5532..1d2607f 100755
--- a/ctdb/tests/cunit/sock_daemon_test_001.sh
+++ b/ctdb/tests/cunit/sock_daemon_test_001.sh
@@ -65,3 +65,30 @@ test6[PID]: daemon started, pid=PID
 test6[PID]: Shutting down
 EOF
 unit_test sock_daemon_test "$pidfile" "$sockpath" 6
+
+ok <<EOF
+test7[PID]: daemon started, pid=PID
+test7[PID]: Received signal 15
+test7[PID]: Shutting down
+EOF
+unit_test sock_daemon_test "$pidfile" "$sockpath" 7
+
+ok <<EOF
+test8[PID]: daemon started, pid=PID
+test8[PID]: Received signal 15
+test8[PID]: Shutting down
+test8[PID]: daemon started, pid=PID
+test8[PID]: Received signal 15
+test8[PID]: Shutting down
+EOF
+unit_test sock_daemon_test "$pidfile" "$sockpath" 8
+
+ok <<EOF
+test9[PID]: daemon started, pid=PID
+test9[PID]: Received signal 15
+test9[PID]: Shutting down
+test9[PID]: daemon started, pid=PID
+test9[PID]: Received signal 15
+test9[PID]: Shutting down
+EOF
+unit_test sock_daemon_test "$pidfile" "$sockpath" 9
diff --git a/ctdb/tests/src/sock_daemon_test.c b/ctdb/tests/src/sock_daemon_test.c
index 278dcab..95b0909 100644
--- a/ctdb/tests/src/sock_daemon_test.c
+++ b/ctdb/tests/src/sock_daemon_test.c
@@ -54,6 +54,12 @@ static struct sock_socket_funcs dummy_socket_funcs = {
 	.read_recv = dummy_read_recv,
 };
 
+/*
+ * test1
+ *
+ * Check setup without actually running daemon
+ */
+
 static void test1(TALLOC_CTX *mem_ctx, const char *pidfile,
 		  const char *sockpath)
 {
@@ -61,14 +67,13 @@ static void test1(TALLOC_CTX *mem_ctx, const char *pidfile,
 	struct stat st;
 	int ret;
 
-	ret = sock_daemon_setup(mem_ctx, "test1", "file:", "NOTICE", pidfile,
+	ret = sock_daemon_setup(mem_ctx, "test1", "file:", "NOTICE",
 				NULL, NULL, &sockd);
 	assert(ret == 0);
 	assert(sockd != NULL);
 
 	ret = stat(pidfile, &st);
-	assert(ret == 0);
-	assert(S_ISREG(st.st_mode));
+	assert(ret == -1);
 
 	ret = sock_daemon_add_unix(sockd, sockpath, &dummy_socket_funcs, NULL);
 	assert(ret == 0);
@@ -86,6 +91,13 @@ static void test1(TALLOC_CTX *mem_ctx, const char *pidfile,
 	assert(ret == -1);
 }
 
+/*
+ * test2
+ *
+ * Start daemon, check PID file, sock daemon functions, termination,
+ * exit code
+ */
+
 static void test2_startup(void *private_data)
 {
 	int fd = *(int *)private_data;
@@ -130,6 +142,8 @@ static void test2(TALLOC_CTX *mem_ctx, const char *pidfile,
 	pid_t pid, pid2;
 	int ret;
 	ssize_t n;
+	int pidfile_fd;
+	char pidstr[20] = { 0 };
 
 	ret = pipe(fd);
 	assert(ret == 0);
@@ -147,14 +161,14 @@ static void test2(TALLOC_CTX *mem_ctx, const char *pidfile,
 		assert(ev != NULL);
 
 		ret = sock_daemon_setup(mem_ctx, "test2", "file:", "NOTICE",
-					pidfile, &test2_funcs, &fd[1], &sockd);
+					&test2_funcs, &fd[1], &sockd);
 		assert(ret == 0);
 
 		ret = sock_daemon_add_unix(sockd, sockpath,
 					   &dummy_socket_funcs, NULL);
 		assert(ret == 0);
 
-		ret = sock_daemon_run(ev, sockd, -1);
+		ret = sock_daemon_run(ev, sockd, pidfile, false, false, -1);
 		assert(ret == EINTR);
 
 		exit(0);
@@ -166,6 +180,17 @@ static void test2(TALLOC_CTX *mem_ctx, const char *pidfile,
 	assert(n == sizeof(ret));
 	assert(ret == 1);
 
+	ret = stat(pidfile, &st);
+	assert(ret == 0);
+	assert(S_ISREG(st.st_mode));
+
+	pidfile_fd = open(pidfile, O_RDONLY, 0644);
+	assert(pidfile_fd != -1);
+	n = read(pidfile_fd, pidstr, sizeof(pidstr)-1);
+	assert(n != -1);
+	pid2 = (pid_t)atoi(pidstr);
+	assert(pid == pid2);
+
 	ret = kill(pid, SIGHUP);
 	assert(ret == 0);
 
@@ -200,6 +225,12 @@ static void test2(TALLOC_CTX *mem_ctx, const char *pidfile,
 	assert(ret == -1);
 }
 
+/*
+ * test3
+ *
+ * Start daemon, test watching of (parent) PID
+ */
+
 static void test3(TALLOC_CTX *mem_ctx, const char *pidfile,
 		  const char *sockpath)
 {
@@ -226,14 +257,14 @@ static void test3(TALLOC_CTX *mem_ctx, const char *pidfile,
 		assert(ev != NULL);
 
 		ret = sock_daemon_setup(mem_ctx, "test3", "file:", "NOTICE",
-					NULL, NULL, NULL, &sockd);
+					NULL, NULL, &sockd);
 		assert(ret == 0);
 
 		ret = sock_daemon_add_unix(sockd, sockpath,
 					   &dummy_socket_funcs, NULL);
 		assert(ret == 0);
 
-		ret = sock_daemon_run(ev, sockd, pid_watch);
+		ret = sock_daemon_run(ev, sockd, NULL, false, false, pid_watch);
 		assert(ret == ESRCH);
 
 		exit(0);
@@ -254,6 +285,12 @@ static void test3(TALLOC_CTX *mem_ctx, const char *pidfile,
 	assert(ret == -1);
 }
 
+/*
+ * test4
+ *
+ * Start daemon, test termination via wait_send function
+ */
+
 struct test4_wait_state {
 };
 
@@ -334,10 +371,10 @@ static void test4(TALLOC_CTX *mem_ctx, const char *pidfile,
 		assert(ev != NULL);
 
 		ret = sock_daemon_setup(mem_ctx, "test4", "file:", "NOTICE",
-					pidfile, &test4_funcs, NULL, &sockd);
+					&test4_funcs, NULL, &sockd);
 		assert(ret == 0);
 
-		ret = sock_daemon_run(ev, sockd, -1);
+		ret = sock_daemon_run(ev, sockd, pidfile, false, false, -1);
 		assert(ret == 0);
 
 		exit(0);
@@ -354,6 +391,12 @@ static void test4(TALLOC_CTX *mem_ctx, const char *pidfile,
 	assert(ret == -1);
 }
 
+/*
+ * test5
+ *
+ * Start daemon, multiple client connects, requests, disconnects
+ */
+
 #define TEST5_MAX_CLIENTS	10
 
 struct test5_pkt {
@@ -611,7 +654,7 @@ static void test5(TALLOC_CTX *mem_ctx, const char *pidfile,
 		assert(ev != NULL);
 
 		ret = sock_daemon_setup(mem_ctx, "test5", "file:", "NOTICE",
-					pidfile, &test5_funcs, &fd[1], &sockd);
+					&test5_funcs, &fd[1], &sockd);
 		assert(ret == 0);
 
 		state.num_clients = 0;
@@ -620,7 +663,7 @@ static void test5(TALLOC_CTX *mem_ctx, const char *pidfile,
 					   &test5_client_funcs, &state);
 		assert(ret == 0);
 
-		ret = sock_daemon_run(ev, sockd, pid);
+		ret = sock_daemon_run(ev, sockd, pidfile, false, false, pid);
 		assert(ret == EINTR);
 
 		exit(0);
@@ -652,6 +695,12 @@ static void test5(TALLOC_CTX *mem_ctx, const char *pidfile,
 	assert(ret == 0);
 }
 
+/*
+ * test6
+ *
+ * Start daemon, test client connects, requests, replies, disconnects
+ */
+
 struct test6_pkt {
 	uint32_t len;
 	uint32_t data;
@@ -910,7 +959,7 @@ static void test6(TALLOC_CTX *mem_ctx, const char *pidfile,
 		server_state.fd = fd[1];
 
 		ret = sock_daemon_setup(mem_ctx, "test6", "file:", "NOTICE",
-					pidfile, &test6_funcs, &server_state,
+					&test6_funcs, &server_state,
 					&sockd);
 		assert(ret == 0);
 
@@ -921,7 +970,7 @@ static void test6(TALLOC_CTX *mem_ctx, const char *pidfile,
 					   &test6_client_funcs, &server_state);
 		assert(ret == 0);
 
-		ret = sock_daemon_run(ev, sockd, pid);
+		ret = sock_daemon_run(ev, sockd, pidfile, false, false, pid);
 		assert(ret == 0);
 
 		exit(0);
@@ -941,6 +990,315 @@ static void test6(TALLOC_CTX *mem_ctx, const char *pidfile,
 	assert(pid != -1);
 }
 
+/*
+ * test7
+ *
+ * Start daemon twice, confirm PID file contention
+ */
+
+static void test7(TALLOC_CTX *mem_ctx, const char *pidfile,
+		  const char *sockpath)
+{
+	struct stat st;
+	int fd[2];
+	pid_t pid, pid2;
+	int ret;
+	struct tevent_context *ev;
+	struct sock_daemon_context *sockd;
+	ssize_t n;
+
+	ret = pipe(fd);
+	assert(ret == 0);
+
+	pid = fork();
+	assert(pid != -1);
+
+	if (pid == 0) {
+		close(fd[0]);
+
+		ev = tevent_context_init(mem_ctx);
+		assert(ev != NULL);
+
+		/* Reuse test2 funcs for the startup synchronisation */
+		ret = sock_daemon_setup(mem_ctx, "test7", "file:", "NOTICE",
+					&test2_funcs, &fd[1], &sockd);
+		assert(ret == 0);
+
+		ret = sock_daemon_run(ev, sockd, pidfile, false, false, -1);
+		assert(ret == EINTR);
+
+		exit(0);
+	}
+
+	close(fd[1]);
+
+	n = read(fd[0], &ret, sizeof(ret));
+	assert(n == sizeof(ret));
+	assert(ret == 1);
+
+	ret = stat(pidfile, &st);
+	assert(ret == 0);
+	assert(S_ISREG(st.st_mode));
+
+	ev = tevent_context_init(mem_ctx);
+	assert(ev != NULL);
+
+	/* Reuse test2 funcs for the startup synchronisation */


-- 
Samba Shared Repository



More information about the samba-cvs mailing list