[SCM] Samba Shared Repository - branch v4-4-test updated

Karolin Seeger kseeger at samba.org
Fri Sep 23 13:25:03 UTC 2016


The branch, v4-4-test has been updated
       via  816c764 ctdb-daemon: Log when removing stale Unix domain socket
       via  c3bddc1 ctdb-daemon: Drop attempt to connect to Unix domain socket
       via  1c13a4c ctdb-daemon: Don't try to reopen TDB files
       via  87632d2 ctdb-daemon: Bind to Unix domain socket after PID file creation
       via  df236e0 ctdb-daemon: Use PID file abstraction
       via  a6a439c ctdb-common: Add routines to manage PID file
       via  514e6c7 s3-spoolss: fix winreg_printer_ver_to_qword
       via  368170c nsswitch: Also set h_errnop for nss_wins functions
       via  c849e59 nsswitch: Add missing arguments to wins gethostbyname*
       via  01fa691 s3/smbd: set FILE_ATTRIBUTE_DIRECTORY as necessary
       via  5793362 gencache: Bail out of stabilize if we can not get the allrecord lock
      from  d1d5716 VERSION: VERSION: Bump version up to 4.4.7...

https://git.samba.org/?p=samba.git;a=shortlog;h=v4-4-test


- Log -----------------------------------------------------------------
commit 816c764a78db286118c25d0679ab0c05b9f661e3
Author: Martin Schwenke <martin at meltin.net>
Date:   Thu Sep 22 14:52:55 2016 +1000

    ctdb-daemon: Log when removing stale Unix domain socket
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12287
    
    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): Thu Sep 22 12:28:12 CEST 2016 on sn-devel-144
    
    (cherry picked from commit 0ec01826d32019b06dd10bb9b6ea5232786d5699)
    
    Autobuild-User(v4-4-test): Karolin Seeger <kseeger at samba.org>
    Autobuild-Date(v4-4-test): Fri Sep 23 15:24:11 CEST 2016 on sn-devel-144

commit c3bddc182fb076c2bb620081290cccc8608fd25c
Author: Martin Schwenke <martin at meltin.net>
Date:   Thu Sep 22 14:47:02 2016 +1000

    ctdb-daemon: Drop attempt to connect to Unix domain socket
    
    This was a weak attempt at exclusivity.  PID file creation now does
    that properly.
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12287
    
    Signed-off-by: Martin Schwenke <martin at meltin.net>
    Reviewed-by: Amitay Isaacs <amitay at gmail.com>
    (cherry picked from commit 8eff9e96037627b1e4adf3ccc8da94ef8f0bad2a)

commit 1c13a4cbbe005cc3d9c1932adc835d233bf43916
Author: Martin Schwenke <martin at meltin.net>
Date:   Thu Sep 22 14:46:12 2016 +1000

    ctdb-daemon: Don't try to reopen TDB files
    
    There aren't any open at this stage.
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12287
    
    Signed-off-by: Martin Schwenke <martin at meltin.net>
    Reviewed-by: Amitay Isaacs <amitay at gmail.com>
    (cherry picked from commit d719a87fe021b0c704fc4b12ddfc0345fe3af146)

commit 87632d231af6398fa1517c7de0753431537fadbd
Author: Martin Schwenke <martin at meltin.net>
Date:   Thu Sep 22 14:43:58 2016 +1000

    ctdb-daemon: Bind to Unix domain socket after PID file creation
    
    No use touching the socket if PID file creation fails.
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12287
    
    Signed-off-by: Martin Schwenke <martin at meltin.net>
    Reviewed-by: Amitay Isaacs <amitay at gmail.com>
    (cherry picked from commit 1e501c77492d25b760c7b10849460ee6490f39dc)

commit df236e0ac6254535009ae23ecfd806db4f1c7c83
Author: Martin Schwenke <martin at meltin.net>
Date:   Thu Sep 22 14:35:03 2016 +1000

    ctdb-daemon: Use PID file abstraction
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12287
    
    Signed-off-by: Martin Schwenke <martin at meltin.net>
    Reviewed-by: Amitay Isaacs <amitay at gmail.com>
    (cherry picked from commit 5148e02adb7b2ea34da9c826a682c1387773402b)

commit a6a439c2f685deb19b57826a280e6924889fa75b
Author: Amitay Isaacs <amitay at gmail.com>
Date:   Mon Sep 19 16:30:12 2016 +1000

    ctdb-common: Add routines to manage PID file
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12287
    
    Signed-off-by: Amitay Isaacs <amitay at gmail.com>
    Reviewed-by: Martin Schwenke <martin at meltin.net>
    (cherry picked from commit 97b6ac7f662d8de316ed520e038779e79bcdb7bc)

commit 514e6c7e7afd99b3b27a9c71322171d0c711465a
Author: Günther Deschner <gd at samba.org>
Date:   Mon Sep 12 17:55:37 2016 +0200

    s3-spoolss: fix winreg_printer_ver_to_qword
    
    Bug: https://bugzilla.samba.org/show_bug.cgi?id=12285
    
    We were reporting the OS minor number as the driver version number in all
    GetDriver/EnumDriver calls.
    
    Guenther
    
    Signed-off-by: Guenther Deschner <gd at samba.org>
    Reviewed-by: Jeremy Allison <jra at samba.org>
    (cherry picked from commit a9a1a16cc8b87a84cdfa049ebd26bf4eac1b3618)

commit 368170cd60ebbae24c5f6138a58472582f46a7e9
Author: Andreas Schneider <asn at samba.org>
Date:   Tue Sep 20 13:26:52 2016 +0200

    nsswitch: Also set h_errnop for nss_wins functions
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12269
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Jim McDonough <jmcd at samba.org>
    
    (cherry picked from commit 382345126c56e26d3dbc319f1c7c1dae3c4fafc9)

commit c849e59cd57a7a45125133dd6f3016950eb2aef3
Author: Andreas Schneider <asn at samba.org>
Date:   Mon Sep 19 16:17:11 2016 +0200

    nsswitch: Add missing arguments to wins gethostbyname*
    
    The errno pointer argument is missing.
    
    BUG: https://bugzilla.samba.org/show_bug.cgi?id=12269
    
    Signed-off-by: Andreas Schneider <asn at samba.org>
    Reviewed-by: Jeremy Allison <jra at samba.org>
    Reviewed-by: Jim McDonough <jmcd at samba.org>
    (cherry picked from commit 124ae4e861f048fe015bff32ace4abff4d3e6c62)

commit 01fa691cc3b4544e27500a169788f389db57f1aa
Author: Ralph Boehme <slow at samba.org>
Date:   Wed Sep 14 12:52:48 2016 +0200

    s3/smbd: set FILE_ATTRIBUTE_DIRECTORY as necessary
    
    Some VFS modules like GPFS will always return success from
    SMB_VFS_GET_DOS_ATTRIBUTES() but only set a subset of the attributes. It
    neither sets FILE_ATTRIBUTE_NORMAL nor FILE_ATTRIBUTE_DIRECTORY.
    
    We already handle the case that the VFS stack returns with result==0 and
    then add the FILE_ATTRIBUTE_NORMAL, regardless of the type of the
    filesystem object. If we want to handle result==0 situation in the
    SMB_VFS_GET_DOS_ATTRIBUTES() caller, then do it right by either setting
    FILE_ATTRIBUTE_NORMAL or FILE_ATTRIBUTE_DIRECTORY.
    
    Bug: https://bugzilla.samba.org/show_bug.cgi?id=12261
    
    Signed-off-by: Ralph Boehme <slow at samba.org>
    Reviewed-by: Jeremy Allison <jra at samba.org>
    
    Autobuild-User(master): Jeremy Allison <jra at samba.org>
    Autobuild-Date(master): Fri Sep 16 00:34:43 CEST 2016 on sn-devel-144
    
    (cherry picked from commit 2a2ac63975b8ff41ede8e93ef2b33148c89f185f)

commit 5793362ccdcbf7721a03f3c6389b0251ba52deba
Author: Volker Lendecke <vl at samba.org>
Date:   Mon Sep 19 14:29:21 2016 -0700

    gencache: Bail out of stabilize if we can not get the allrecord lock
    
    Bug: https://bugzilla.samba.org/show_bug.cgi?id=12045
    
    Signed-off-by: Volker Lendecke <vl at samba.org>
    Reviewed-by: Jeremy Allison <jra at samba.org>
    
    Autobuild-User(master): Jeremy Allison <jra at samba.org>
    Autobuild-Date(master): Tue Sep 20 04:09:33 CEST 2016 on sn-devel-144
    
    (cherry picked from commit b208499960eefef02d305a3bd59b03a7c2aafcac)

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

Summary of changes:
 ctdb/common/pidfile.c                   | 143 +++++++++++++++++++
 ctdb/common/pidfile.h                   |  51 +++++++
 ctdb/server/ctdb_daemon.c               |  67 ++++-----
 ctdb/tests/cunit/pidfile_test_001.sh    |   8 ++
 ctdb/tests/src/ctdbd_test.c             |   1 +
 ctdb/tests/src/pidfile_test.c           | 241 ++++++++++++++++++++++++++++++++
 ctdb/wscript                            |   3 +-
 nsswitch/wins.c                         |  60 ++++++--
 source3/lib/gencache.c                  |   2 +-
 source3/rpc_client/cli_winreg_spoolss.c |   7 +-
 source3/smbd/dosmode.c                  |   6 +-
 11 files changed, 534 insertions(+), 55 deletions(-)
 create mode 100644 ctdb/common/pidfile.c
 create mode 100644 ctdb/common/pidfile.h
 create mode 100755 ctdb/tests/cunit/pidfile_test_001.sh
 create mode 100644 ctdb/tests/src/pidfile_test.c


Changeset truncated at 500 lines:

diff --git a/ctdb/common/pidfile.c b/ctdb/common/pidfile.c
new file mode 100644
index 0000000..b3f29e3
--- /dev/null
+++ b/ctdb/common/pidfile.c
@@ -0,0 +1,143 @@
+/*
+   Create and remove pidfile
+
+   Copyright (C) Amitay Isaacs  2016
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "replace.h"
+#include "system/filesys.h"
+
+#include <talloc.h>
+
+#include "common/pidfile.h"
+
+struct pidfile_context {
+	const char *pidfile;
+	int fd;
+	pid_t pid;
+};
+
+static int pidfile_context_destructor(struct pidfile_context *pid_ctx);
+
+int pidfile_create(TALLOC_CTX *mem_ctx, const char *pidfile,
+		   struct pidfile_context **result)
+{
+	struct pidfile_context *pid_ctx;
+	struct flock lck;
+	char tmp[64];
+	int fd, ret = 0;
+	int len;
+	ssize_t nwritten;
+
+	pid_ctx = talloc_zero(mem_ctx, struct pidfile_context);
+	if (pid_ctx == NULL) {
+		return ENOMEM;
+	}
+
+	pid_ctx->pidfile = talloc_strdup(pid_ctx, pidfile);
+	if (pid_ctx->pidfile == NULL) {
+		ret = ENOMEM;
+		goto fail;
+	}
+
+	pid_ctx->pid = getpid();
+
+	fd = open(pidfile, O_CREAT|O_WRONLY|O_NONBLOCK, 0644);
+	if (fd == -1) {
+		ret = errno;
+		goto fail;
+	}
+
+	pid_ctx->fd = fd;
+
+	lck = (struct flock) {
+		.l_type = F_WRLCK,
+		.l_whence = SEEK_SET,
+	};
+
+	do {
+		ret = fcntl(fd, F_SETLK, &lck);
+	} while ((ret == -1) && (errno == EINTR));
+
+	if (ret != 0) {
+		ret = errno;
+		goto fail;
+	}
+
+	do {
+		ret = ftruncate(fd, 0);
+	} while ((ret == -1) && (errno == EINTR));
+
+	if (ret == -1) {
+		ret = EIO;
+		goto fail_unlink;
+	}
+
+	len = snprintf(tmp, sizeof(tmp), "%u\n", pid_ctx->pid);
+	if (len < 0) {
+		ret = EIO;
+		goto fail_unlink;
+	}
+
+	do {
+		nwritten = write(fd, tmp, len);
+	} while ((nwritten == -1) && (errno == EINTR));
+
+	if ((nwritten == -1) || (nwritten != len)) {
+		ret = EIO;
+		goto fail_unlink;
+	}
+
+	talloc_set_destructor(pid_ctx, pidfile_context_destructor);
+
+	*result = pid_ctx;
+	return 0;
+
+fail_unlink:
+	unlink(pidfile);
+	close(fd);
+
+fail:
+	talloc_free(pid_ctx);
+	return ret;
+}
+
+static int pidfile_context_destructor(struct pidfile_context *pid_ctx)
+{
+	struct flock lck;
+	int ret;
+
+	if (getpid() != pid_ctx->pid) {
+		return 0;
+	}
+
+	lck = (struct flock) {
+		.l_type = F_UNLCK,
+		.l_whence = SEEK_SET,
+	};
+
+	(void) unlink(pid_ctx->pidfile);
+
+	do {
+		ret = fcntl(pid_ctx->fd, F_SETLK, &lck);
+	} while ((ret == -1) && (errno == EINTR));
+
+	do {
+		ret = close(pid_ctx->fd);
+	} while ((ret == -1) && (errno == EINTR));
+
+	return 0;
+}
diff --git a/ctdb/common/pidfile.h b/ctdb/common/pidfile.h
new file mode 100644
index 0000000..1450134
--- /dev/null
+++ b/ctdb/common/pidfile.h
@@ -0,0 +1,51 @@
+/*
+   Create and remove pidfile
+
+   Copyright (C) Amitay Isaacs  2016
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __CTDB_PIDFILE_H__
+#define __CTDB_PIDFILE_H__
+
+#include <talloc.h>
+
+/**
+ * @file pidfile.h
+ *
+ * @brief Routines to manage PID file
+ */
+
+/**
+ * @brief Abstract struct to store pidfile details
+ */
+struct pidfile_context;
+
+/**
+ * @brief Create a PID file
+ *
+ * This creates a PID file, locks it, and writes PID.
+ *
+ * @param[in] mem_ctx Talloc memory context
+ * @param[in] pidfile Path of PID file
+ * @param[out] result Pidfile context
+ * @return 0 on success, errno on failure
+ *
+ * Freeing the pidfile_context, will delete the pidfile.
+ */
+int pidfile_create(TALLOC_CTX *mem_ctx, const char *pidfile,
+		   struct pidfile_context **result);
+
+#endif /* __CTDB_PIDFILE_H__ */
diff --git a/ctdb/server/ctdb_daemon.c b/ctdb/server/ctdb_daemon.c
index 1fe792c..1cc74a0 100644
--- a/ctdb/server/ctdb_daemon.c
+++ b/ctdb/server/ctdb_daemon.c
@@ -43,6 +43,7 @@
 #include "common/system.h"
 #include "common/common.h"
 #include "common/logging.h"
+#include "common/pidfile.h"
 
 struct ctdb_client_pid_list {
 	struct ctdb_client_pid_list *next, *prev;
@@ -52,6 +53,7 @@ struct ctdb_client_pid_list {
 };
 
 const char *ctdbd_pidfile = NULL;
+static struct pidfile_context *ctdbd_pidfile_ctx = NULL;
 
 static void daemon_incoming_packet(void *, struct ctdb_req_header *);
 
@@ -983,6 +985,7 @@ static void ctdb_accept_client(struct tevent_context *ev,
 static int ux_socket_bind(struct ctdb_context *ctdb)
 {
 	struct sockaddr_un addr;
+	int ret;
 
 	ctdb->daemon.sd = socket(AF_UNIX, SOCK_STREAM, 0);
 	if (ctdb->daemon.sd == -1) {
@@ -993,17 +996,16 @@ static int ux_socket_bind(struct ctdb_context *ctdb)
 	addr.sun_family = AF_UNIX;
 	strncpy(addr.sun_path, ctdb->daemon.name, sizeof(addr.sun_path)-1);
 
-	/* First check if an old ctdbd might be running */
-	if (connect(ctdb->daemon.sd,
-		    (struct sockaddr *)&addr, sizeof(addr)) == 0) {
-		DEBUG(DEBUG_CRIT,
-		      ("Something is already listening on ctdb socket '%s'\n",
-		       ctdb->daemon.name));
-		goto failed;
-	}
-
 	/* Remove any old socket */
-	unlink(ctdb->daemon.name);
+	ret = unlink(ctdb->daemon.name);
+	if (ret == 0) {
+		DEBUG(DEBUG_WARNING,
+		      ("Removed stale socket %s\n", ctdb->daemon.name));
+	} else if (errno != ENOENT) {
+		DEBUG(DEBUG_ERR,
+		      ("Failed to remove stale socket %s\n", ctdb->daemon.name));
+		return -1;
+	}
 
 	set_close_on_exec(ctdb->daemon.sd);
 	set_nonblocking(ctdb->daemon.sd);
@@ -1127,32 +1129,21 @@ static void ctdb_tevent_trace(enum tevent_trace_point tp,
 
 static void ctdb_remove_pidfile(void)
 {
-	/* Only the main ctdbd's PID matches the SID */
-	if (ctdbd_pidfile != NULL && getsid(0) == getpid()) {
-		if (unlink(ctdbd_pidfile) == 0) {
-			DEBUG(DEBUG_NOTICE, ("Removed PID file %s\n",
-					     ctdbd_pidfile));
-		} else {
-			DEBUG(DEBUG_WARNING, ("Failed to Remove PID file %s\n",
-					      ctdbd_pidfile));
-		}
-	}
+	TALLOC_FREE(ctdbd_pidfile_ctx);
 }
 
-static void ctdb_create_pidfile(pid_t pid)
+static void ctdb_create_pidfile(TALLOC_CTX *mem_ctx)
 {
 	if (ctdbd_pidfile != NULL) {
-		FILE *fp;
-
-		fp = fopen(ctdbd_pidfile, "w");
-		if (fp == NULL) {
-			DEBUG(DEBUG_ALERT,
-			      ("Failed to open PID file %s\n", ctdbd_pidfile));
+		int ret = pidfile_create(mem_ctx, ctdbd_pidfile,
+					 &ctdbd_pidfile_ctx);
+		if (ret != 0) {
+			DEBUG(DEBUG_ERR,
+			      ("Failed to create PID file %s\n",
+			       ctdbd_pidfile));
 			exit(11);
 		}
 
-		fprintf(fp, "%d\n", pid);
-		fclose(fp);
 		DEBUG(DEBUG_NOTICE, ("Created PID file %s\n", ctdbd_pidfile));
 		atexit(ctdb_remove_pidfile);
 	}
@@ -1214,19 +1205,10 @@ int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork)
 	int res, ret = -1;
 	struct tevent_fd *fde;
 
-	/* create a unix domain stream socket to listen to */
-	res = ux_socket_bind(ctdb);
-	if (res!=0) {
-		DEBUG(DEBUG_ALERT,("Cannot continue.  Exiting!\n"));
-		exit(10);
-	}
-
 	if (do_fork && fork()) {
 		return 0;
 	}
 
-	tdb_reopen_all(false);
-
 	if (do_fork) {
 		if (setsid() == -1) {
 			ctdb_die(ctdb, "Failed to setsid()\n");
@@ -1243,7 +1225,14 @@ int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork)
 	ctdb->ctdbd_pid = getpid();
 	DEBUG(DEBUG_ERR, ("Starting CTDBD (Version %s) as PID: %u\n",
 			  CTDB_VERSION_STRING, ctdb->ctdbd_pid));
-	ctdb_create_pidfile(ctdb->ctdbd_pid);
+	ctdb_create_pidfile(ctdb);
+
+	/* create a unix domain stream socket to listen to */
+	res = ux_socket_bind(ctdb);
+	if (res!=0) {
+		DEBUG(DEBUG_ALERT,("Cannot continue.  Exiting!\n"));
+		exit(10);
+	}
 
 	/* Make sure we log something when the daemon terminates.
 	 * This must be the first exit handler to run (so the last to
diff --git a/ctdb/tests/cunit/pidfile_test_001.sh b/ctdb/tests/cunit/pidfile_test_001.sh
new file mode 100755
index 0000000..620682e
--- /dev/null
+++ b/ctdb/tests/cunit/pidfile_test_001.sh
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+. "${TEST_SCRIPTS_DIR}/unit.sh"
+
+pidfile=$(mktemp --tmpdir="$TEST_VAR_DIR")
+
+ok_null
+unit_test pidfile_test $pidfile
diff --git a/ctdb/tests/src/ctdbd_test.c b/ctdb/tests/src/ctdbd_test.c
index 018aa2a..e66a29b 100644
--- a/ctdb/tests/src/ctdbd_test.c
+++ b/ctdb/tests/src/ctdbd_test.c
@@ -47,6 +47,7 @@ bool fast_start;
 #include "common/rb_tree.c"
 #include "common/reqid.c"
 #include "common/logging.c"
+#include "common/pidfile.c"
 
 /* CTDB_SERVER_OBJ */
 #include "server/ctdb_daemon.c"
diff --git a/ctdb/tests/src/pidfile_test.c b/ctdb/tests/src/pidfile_test.c
new file mode 100644
index 0000000..ad8bf14
--- /dev/null
+++ b/ctdb/tests/src/pidfile_test.c
@@ -0,0 +1,241 @@
+/*
+   pidfile tests
+
+   Copyright (C) Amitay Isaacs  2016
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "replace.h"
+#include "system/wait.h"
+
+#include <assert.h>
+
+#include "common/pidfile.c"
+
+
+/* create pid file, check pid file exists, check pid and remove pid file */
+static void test1(const char *pidfile)
+{
+	struct pidfile_context *pid_ctx;
+	int ret;
+	struct stat st;
+	FILE *fp;
+	pid_t pid;
+
+	ret = pidfile_create(NULL, pidfile, &pid_ctx);
+	assert(ret == 0);
+	assert(pid_ctx != NULL);
+
+	ret = stat(pidfile, &st);
+	assert(ret == 0);
+	assert(S_ISREG(st.st_mode));
+
+	fp = fopen(pidfile, "r");
+	ret = fscanf(fp, "%d", &pid);
+	assert(ret == 1);
+	assert(pid == getpid());
+	fclose(fp);
+
+	TALLOC_FREE(pid_ctx);
+
+	ret = stat(pidfile, &st);
+	assert(ret == -1);
+}
+
+/* create pid file in two processes */
+static void test2(const char *pidfile)
+{
+	struct pidfile_context *pid_ctx;
+	pid_t pid, pid2;
+	int fd[2];
+	int ret;
+	size_t nread;
+	FILE *fp;
+	struct stat st;
+
+	ret = pipe(fd);
+	assert(ret == 0);
+
+	pid = fork();
+	assert(pid != -1);
+
+	if (pid == 0) {
+		ssize_t nwritten;
+
+		close(fd[0]);
+
+		ret = pidfile_create(NULL, pidfile, &pid_ctx);
+		assert(ret == 0);
+		assert(pid_ctx != NULL);
+
+		nwritten = write(fd[1], &ret, sizeof(ret));
+		assert(nwritten == sizeof(ret));
+
+		sleep(10);
+
+		TALLOC_FREE(pid_ctx);
+
+		nwritten = write(fd[1], &ret, sizeof(ret));
+		assert(nwritten == sizeof(ret));
+
+		exit(1);
+	}
+
+	close(fd[1]);
+
+	nread = read(fd[0], &ret, sizeof(ret));
+	assert(nread == sizeof(ret));
+	assert(ret == 0);
+
+	fp = fopen(pidfile, "r");
+	assert(fp != NULL);
+	ret = fscanf(fp, "%d", &pid2);
+	assert(ret == 1);
+	assert(pid == pid2);
+	fclose(fp);
+
+	ret = pidfile_create(NULL, pidfile, &pid_ctx);
+	assert(ret != 0);
+
+	nread = read(fd[0], &ret, sizeof(ret));
+	assert(nread == sizeof(ret));
+	assert(ret == 0);
+
+	ret = pidfile_create(NULL, pidfile, &pid_ctx);
+	assert(ret == 0);
+	assert(pid_ctx != NULL);
+
+	TALLOC_FREE(pid_ctx);
+
+	ret = stat(pidfile, &st);
+	assert(ret == -1);
+}
+
+/* create pid file, fork, try to remove pid file in separate process */
+static void test3(const char *pidfile)
+{
+	struct pidfile_context *pid_ctx;
+	pid_t pid;


-- 
Samba Shared Repository



More information about the samba-cvs mailing list