1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-23 17:34:34 +03:00

ctdb-tests: Add sock_daemon test for stale socket handling

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
This commit is contained in:
Martin Schwenke 2017-11-03 16:24:46 +11:00 committed by Amitay Isaacs
parent ad8d72091e
commit 5c354e10ac
2 changed files with 141 additions and 0 deletions

View File

@ -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

View 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);
}