]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ctdb-tests: Add sock_daemon test for stale socket handling
authorMartin Schwenke <martin@meltin.net>
Fri, 3 Nov 2017 05:24:46 +0000 (16:24 +1100)
committerAmitay Isaacs <amitay@samba.org>
Tue, 7 Nov 2017 06:51:02 +0000 (07:51 +0100)
Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
Autobuild-User(master): Amitay Isaacs <amitay@samba.org>
Autobuild-Date(master): Tue Nov  7 07:51:02 CET 2017 on sn-devel-144

ctdb/tests/cunit/sock_daemon_test_001.sh
ctdb/tests/src/sock_daemon_test.c

index 58742755d0d558c5545491c2fac5b77355fd47c8..9da484a761699f485272e89042f1e2040b97ec2e 100755 (executable)
@@ -93,3 +93,13 @@ test9[PID]: Received signal 15
 test9[PID]: Shutting down
 EOF
 unit_test sock_daemon_test "$pidfile" "$sockpath" 9
+
+ok <<EOF
+test10[PID]: listening on $sockpath
+test10[PID]: daemon started, pid=PID
+test10[PID]: listening on $sockpath
+test10[PID]: daemon started, pid=PID
+test10[PID]: Received signal 15
+test10[PID]: Shutting down
+EOF
+unit_test sock_daemon_test "$pidfile" "$sockpath" 10
index bbb792753e1fd012b1979435970918f44a0241b2..bba0df26a0a8797536a788e178e4c6b4262de561 100644 (file)
@@ -1335,6 +1335,133 @@ static void test9(TALLOC_CTX *mem_ctx, const char *pidfile,
        close(fd[0]);
 }
 
+/*
+ * test10
+ *
+ * Confirm that the daemon starts successfully if there is a stale socket
+ */
+
+static void test10(TALLOC_CTX *mem_ctx, const char *pidfile,
+                 const char *sockpath)
+{
+       struct stat st;
+       int fd[2];
+       pid_t pid, pid2;
+       int ret;
+       ssize_t n;
+       int pidfile_fd;
+       char pidstr[20] = { 0 };
+
+       ret = pipe(fd);
+       assert(ret == 0);
+
+       pid = fork();
+       assert(pid != -1);
+
+       if (pid == 0) {
+               struct tevent_context *ev;
+               struct sock_daemon_context *sockd;
+
+               close(fd[0]);
+
+               ev = tevent_context_init(mem_ctx);
+               assert(ev != NULL);
+
+               ret = sock_daemon_setup(mem_ctx, "test10", "file:", "NOTICE",
+                                       &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, 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);
+
+       /* KILL will leave PID file and socket behind */
+       ret = kill (pid, SIGKILL);
+       assert(ret == 0);
+
+       ret = stat(sockpath, &st);
+       assert(ret == 0);
+
+       close(fd[0]);
+
+       ret = pipe(fd);
+       assert(ret == 0);
+
+       pid = fork();
+       assert(pid != -1);
+
+       if (pid == 0) {
+               struct tevent_context *ev;
+               struct sock_daemon_context *sockd;
+
+               close(fd[0]);
+
+               ev = tevent_context_init(mem_ctx);
+               assert(ev != NULL);
+
+               ret = sock_daemon_setup(mem_ctx, "test10", "file:", "NOTICE",
+                                       &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, 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));
+
+       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, SIGTERM);
+       assert(ret == 0);
+
+       n = read(fd[0], &ret, sizeof(ret));
+       assert(n == sizeof(ret));
+       assert(ret == 3);
+
+       pid2 = waitpid(pid, &ret, 0);
+       assert(pid2 == pid);
+       assert(WEXITSTATUS(ret) == 0);
+
+       close(fd[0]);
+
+       ret = stat(pidfile, &st);
+       assert(ret == -1);
+
+       ret = stat(sockpath, &st);
+       assert(ret == -1);
+}
+
 int main(int argc, const char **argv)
 {
        TALLOC_CTX *mem_ctx;
@@ -1390,6 +1517,10 @@ int main(int argc, const char **argv)
                test9(mem_ctx, pidfile, sockpath);
                break;
 
+       case 10:
+               test10(mem_ctx, pidfile, sockpath);
+               break;
+
        default:
                fprintf(stderr, "Unknown test number %d\n", num);
        }