Blame SOURCES/autofs-5.1.3-serialize-calls-to-open_xxxx-functions.patch

304803
autofs-5.1.3 - serialize calls to open_xxxx() functions
304803
304803
From: Ian Kent <raven@themaw.net>
304803
304803
This patch is the second part of the change described in "move
304803
open_xxxx() functions to spawn.c" to serialize the open_xxxx()
304803
functions wrt. to fork(2).
304803
304803
Signed-off-by: Ian Kent <raven@themaw.net>
304803
---
304803
 CHANGELOG                |    1 
304803
 daemon/spawn.c           |   61 ++++++++++++++++++++++++++++++++++++++++-------
304803
 include/automount.h      |    2 +
304803
 lib/mounts.c             |    2 +
304803
 modules/lookup_program.c |    3 ++
304803
 5 files changed, 61 insertions(+), 8 deletions(-)
304803
304803
--- autofs-5.0.7.orig/CHANGELOG
304803
+++ autofs-5.0.7/CHANGELOG
304803
@@ -288,6 +288,7 @@
304803
 - update configure to check for pipe2(2).
304803
 - fix open calls not using open_xxxx() calls.
304803
 - move open_xxxx() functions to spawn.c.
304803
+- serialize calls to open_xxxx() functions.
304803
 
304803
 25/07/2012 autofs-5.0.7
304803
 =======================
304803
--- autofs-5.0.7.orig/daemon/spawn.c
304803
+++ autofs-5.0.7/daemon/spawn.c
304803
@@ -29,6 +29,7 @@
304803
 #include "automount.h"
304803
 
304803
 static pthread_mutex_t spawn_mutex = PTHREAD_MUTEX_INITIALIZER;
304803
+static pthread_mutex_t open_mutex = PTHREAD_MUTEX_INITIALIZER;
304803
 
304803
 #define SPAWN_OPT_NONE		0x0000
304803
 #define SPAWN_OPT_LOCK		0x0001
304803
@@ -48,6 +49,20 @@ void dump_core(void)
304803
 	raise(SIGSEGV);
304803
 }
304803
 
304803
+void open_mutex_lock(void)
304803
+{
304803
+	int _o_lock = pthread_mutex_lock(&open_mutex);
304803
+	if (_o_lock)
304803
+		fatal(_o_lock);
304803
+}
304803
+
304803
+void open_mutex_unlock(void)
304803
+{
304803
+	int _o_unlock = pthread_mutex_unlock(&open_mutex);
304803
+	if (_o_unlock)
304803
+		fatal(_o_unlock);
304803
+}
304803
+
304803
 /*
304803
  * Use CLOEXEC flag for open(), pipe(), fopen() (read-only case) and
304803
  * socket() if possible.
304803
@@ -71,14 +86,18 @@ int open_fd(const char *path, int flags)
304803
 {
304803
 	int fd;
304803
 
304803
+	open_mutex_lock();
304803
 #if defined(O_CLOEXEC) && defined(SOCK_CLOEXEC)
304803
 	if (cloexec_works != -1)
304803
 		flags |= O_CLOEXEC;
304803
 #endif
304803
 	fd = open(path, flags);
304803
-	if (fd == -1)
304803
+	if (fd == -1) {
304803
+		open_mutex_unlock();
304803
 		return -1;
304803
+	}
304803
 	check_cloexec(fd);
304803
+	open_mutex_unlock();
304803
 	return fd;
304803
 }
304803
 
304803
@@ -86,14 +105,18 @@ int open_fd_mode(const char *path, int f
304803
 {
304803
 	int fd;
304803
 
304803
+	open_mutex_lock();
304803
 #if defined(O_CLOEXEC) && defined(SOCK_CLOEXEC)
304803
 	if (cloexec_works != -1)
304803
 		flags |= O_CLOEXEC;
304803
 #endif
304803
 	fd = open(path, flags, mode);
304803
-	if (fd == -1)
304803
+	if (fd == -1) {
304803
+		open_mutex_unlock();
304803
 		return -1;
304803
+	}
304803
 	check_cloexec(fd);
304803
+	open_mutex_unlock();
304803
 	return fd;
304803
 }
304803
 
304803
@@ -101,35 +124,45 @@ int open_pipe(int pipefd[2])
304803
 {
304803
 	int ret;
304803
 
304803
+	open_mutex_lock();
304803
 #if defined(O_CLOEXEC) && defined(SOCK_CLOEXEC) && defined(HAVE_PIPE2)
304803
 	if (cloexec_works != -1) {
304803
 		ret = pipe2(pipefd, O_CLOEXEC);
304803
 		if (ret != -1)
304803
-			return 0;
304803
+			goto done;
304803
 		if (errno != EINVAL)
304803
-			return -1;
304803
+			goto err;
304803
 	}
304803
 #endif
304803
 	ret = pipe(pipefd);
304803
 	if (ret == -1)
304803
-		return -1;
304803
+		goto err;
304803
 	check_cloexec(pipefd[0]);
304803
 	check_cloexec(pipefd[1]);
304803
+done:
304803
+	open_mutex_unlock();
304803
 	return 0;
304803
+err:
304803
+	open_mutex_unlock();
304803
+	return -1;
304803
 }
304803
 
304803
 int open_sock(int domain, int type, int protocol)
304803
 {
304803
 	int fd;
304803
 
304803
+	open_mutex_lock();
304803
 #ifdef SOCK_CLOEXEC
304803
 	if (cloexec_works != -1)
304803
 		type |= SOCK_CLOEXEC;
304803
 #endif
304803
 	fd = socket(domain, type, protocol);
304803
-	if (fd == -1)
304803
+	if (fd == -1) {
304803
+		open_mutex_unlock();
304803
 		return -1;
304803
+	}
304803
 	check_cloexec(fd);
304803
+	open_mutex_unlock();
304803
 	return fd;
304803
 }
304803
 
304803
@@ -137,19 +170,24 @@ FILE *open_fopen_r(const char *path)
304803
 {
304803
 	FILE *f;
304803
 
304803
+	open_mutex_lock();
304803
 #if defined(O_CLOEXEC) && defined(SOCK_CLOEXEC)
304803
 	if (cloexec_works != -1) {
304803
 		f = fopen(path, "re");
304803
 		if (f != NULL) {
304803
 			check_cloexec(fileno(f));
304803
+			open_mutex_unlock();
304803
 			return f;
304803
 		}
304803
 	}
304803
 #endif
304803
 	f = fopen(path, "r");
304803
-	if (f == NULL)
304803
+	if (f == NULL) {
304803
+		open_mutex_unlock();
304803
 		return NULL;
304803
+	}
304803
 	check_cloexec(fileno(f));
304803
+	open_mutex_unlock();
304803
 	return f;
304803
 }
304803
 
304803
@@ -157,19 +195,24 @@ FILE *open_setmntent_r(const char *table
304803
 {
304803
 	FILE *tab;
304803
 
304803
+	open_mutex_lock();
304803
 #if defined(O_CLOEXEC) && defined(SOCK_CLOEXEC)
304803
 	if (cloexec_works != -1) {
304803
 		tab = setmntent(table, "re");
304803
 		if (tab != NULL) {
304803
 			check_cloexec(fileno(tab));
304803
+			open_mutex_unlock();
304803
 			return tab;
304803
 		}
304803
 	}
304803
 #endif
304803
 	tab = fopen(table, "r");
304803
-	if (tab == NULL)
304803
+	if (tab == NULL) {
304803
+		open_mutex_unlock();
304803
 		return NULL;
304803
+	}
304803
 	check_cloexec(fileno(tab));
304803
+	open_mutex_unlock();
304803
 	return tab;
304803
 }
304803
 
304803
@@ -283,6 +326,7 @@ static int do_spawn(unsigned logopt, uns
304803
 		egid = tsv->gid;
304803
 	}
304803
 
304803
+	open_mutex_lock();
304803
 	f = fork();
304803
 	if (f == 0) {
304803
 		char **pargv = (char **) argv;
304803
@@ -377,6 +421,7 @@ done:
304803
 
304803
 		sigaddset(&tmpsig, SIGCHLD);
304803
 		pthread_sigmask(SIG_SETMASK, &tmpsig, NULL);
304803
+		open_mutex_unlock();
304803
 
304803
 		close(pipefd[1]);
304803
 
304803
--- autofs-5.0.7.orig/include/automount.h
304803
+++ autofs-5.0.7/include/automount.h
304803
@@ -257,6 +257,8 @@ int spawnv(unsigned logopt, const char *
304803
 int spawn_mount(unsigned logopt, ...);
304803
 int spawn_bind_mount(unsigned logopt, ...);
304803
 int spawn_umount(unsigned logopt, ...);
304803
+void open_mutex_lock(void);
304803
+void open_mutex_unlock(void);
304803
 int open_fd(const char *, int);
304803
 int open_fd_mode(const char *, int, int);
304803
 int open_pipe(int[2]);
304803
--- autofs-5.0.7.orig/lib/mounts.c
304803
+++ autofs-5.0.7/lib/mounts.c
304803
@@ -230,6 +230,7 @@ int check_nfs_mount_version(struct nfs_m
304803
 	sigfillset(&allsigs);
304803
 	pthread_sigmask(SIG_BLOCK, &allsigs, &oldsig);
304803
 
304803
+	open_mutex_lock();
304803
 	f = fork();
304803
 	if (f == 0) {
304803
 		reset_signals();
304803
@@ -248,6 +249,7 @@ int check_nfs_mount_version(struct nfs_m
304803
 
304803
 	sigaddset(&tmpsig, SIGCHLD);
304803
 	pthread_sigmask(SIG_SETMASK, &tmpsig, NULL);
304803
+	open_mutex_unlock();
304803
 
304803
 	close(pipefd[1]);
304803
 
304803
--- autofs-5.0.7.orig/modules/lookup_program.c
304803
+++ autofs-5.0.7/modules/lookup_program.c
304803
@@ -225,6 +225,7 @@ static char *lookup_one(struct autofs_po
304803
 		goto out_error;
304803
 	}
304803
 
304803
+	open_mutex_lock();
304803
 	f = fork();
304803
 	if (f < 0) {
304803
 		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
304803
@@ -233,6 +234,7 @@ static char *lookup_one(struct autofs_po
304803
 		close(pipefd[1]);
304803
 		close(epipefd[0]);
304803
 		close(epipefd[1]);
304803
+		open_mutex_unlock();
304803
 		goto out_error;
304803
 	} else if (f == 0) {
304803
 		reset_signals();
304803
@@ -262,6 +264,7 @@ static char *lookup_one(struct autofs_po
304803
 	}
304803
 	close(pipefd[1]);
304803
 	close(epipefd[1]);
304803
+	open_mutex_unlock();
304803
 
304803
 	mapp = mapent;
304803
 	errp = errbuf;