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

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