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

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