Blame SOURCES/autofs-5.1.2-create-thread-local-ID-for-mount-attempts.patch

4d476f
autofs-5.1.2 - create thread-local ID for mount attempts
4d476f
4d476f
From: Lars R. Damerow <lars@pixar.com>
4d476f
4d476f
This patch creates key_thread_attempt_id as a pthread key and populates it
4d476f
in each new thread serving a mount attempt. This ID can be used in output logs
4d476f
as an easy string to grep for, allowing admins to quickly see a mount attempt's
4d476f
log entries without the messages from other attempts interleaved.
4d476f
4d476f
The contents of the ID are intended to be opaque, but to get a chance at
4d476f
a unique value for each mount attempt, this patch does a very simple hash
4d476f
computation of the thread's wait_queue_token, the pid of the requesting
4d476f
process, and the key for the mount. The hash is based on the public domain
4d476f
sdbm library.
4d476f
4d476f
Signed-off-by: Lars R. Damerow <lars@pixar.com>
4d476f
Signed-off-by: Ian Kent <raven@themaw.net>
4d476f
---
4d476f
 CHANGELOG           |    1 +
4d476f
 daemon/automount.c  |   24 ++++++++++++++++++++++++
4d476f
 daemon/direct.c     |   17 +++++++++++++++++
4d476f
 daemon/indirect.c   |   17 +++++++++++++++++
4d476f
 include/automount.h |    6 ++++++
4d476f
 5 files changed, 65 insertions(+)
4d476f
4d476f
--- autofs-5.0.7.orig/CHANGELOG
4d476f
+++ autofs-5.0.7/CHANGELOG
4d476f
@@ -236,6 +236,7 @@
4d476f
 - fix bogus check in expire_cleanup().
4d476f
 - delay submount exit for amd submounts.
4d476f
 - add the mount requestor's pid to pending_args.
4d476f
+- create thread-local ID for mount attempts.
4d476f
 
4d476f
 25/07/2012 autofs-5.0.7
4d476f
 =======================
4d476f
--- autofs-5.0.7.orig/daemon/automount.c
4d476f
+++ autofs-5.0.7/daemon/automount.c
4d476f
@@ -89,6 +89,7 @@ struct startup_cond suc = {
4d476f
 	PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0};
4d476f
 
4d476f
 pthread_key_t key_thread_stdenv_vars;
4d476f
+pthread_key_t key_thread_attempt_id = (pthread_key_t) 0L;
4d476f
 
4d476f
 #define MAX_OPEN_FILES		10240
4d476f
 
4d476f
@@ -98,6 +99,17 @@ static int umount_all(struct autofs_poin
4d476f
 
4d476f
 extern struct master *master_list;
4d476f
 
4d476f
+/* simple string hash based on public domain sdbm library */
4d476f
+unsigned long sdbm_hash(const char *str, unsigned long seed)
4d476f
+{
4d476f
+	unsigned long hash = seed;
4d476f
+	char c;
4d476f
+
4d476f
+	while ((c = *str++))
4d476f
+		hash = c + (hash << 6) + (hash << 16) - hash;
4d476f
+	return hash;
4d476f
+}
4d476f
+
4d476f
 static int do_mkdir(const char *parent, const char *path, mode_t mode)
4d476f
 {
4d476f
 	int status;
4d476f
@@ -2441,6 +2453,18 @@ int main(int argc, char *argv[])
4d476f
 		       program);
4d476f
 		master_kill(master_list);
4d476f
 		res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
4d476f
+		close(start_pipefd[1]);
4d476f
+		release_flag_file();
4d476f
+		macro_free_global_table();
4d476f
+		exit(1);
4d476f
+	}
4d476f
+
4d476f
+	status = pthread_key_create(&key_thread_attempt_id, free);
4d476f
+	if (status) {
4d476f
+		logerr("%s: failed to create thread data key for attempt ID!",
4d476f
+		       program);
4d476f
+		master_kill(master_list);
4d476f
+		res = write(start_pipefd[1], pst_stat, sizeof(*pst_stat));
4d476f
 		close(start_pipefd[1]);
4d476f
 		release_flag_file();
4d476f
 		macro_free_global_table();
4d476f
--- autofs-5.0.7.orig/daemon/direct.c
4d476f
+++ autofs-5.0.7/daemon/direct.c
4d476f
@@ -1210,6 +1210,8 @@ static void *do_mount_direct(void *arg)
4d476f
 	struct autofs_point *ap;
4d476f
 	struct stat st;
4d476f
 	int status, state;
4d476f
+	char attempt_id_comp[20];
4d476f
+	unsigned long *attempt_id;
4d476f
 
4d476f
 	args = (struct pending_args *) arg;
4d476f
 
4d476f
@@ -1219,6 +1221,21 @@ static void *do_mount_direct(void *arg)
4d476f
 
4d476f
 	ap = mt.ap;
4d476f
 
4d476f
+	attempt_id = pthread_getspecific(key_thread_attempt_id);
4d476f
+	if (attempt_id == NULL) {
4d476f
+		attempt_id = (unsigned long *) calloc(1, sizeof(unsigned long));
4d476f
+		if (attempt_id == NULL)
4d476f
+			fatal(ENOMEM);
4d476f
+		snprintf(attempt_id_comp, 20, "%ld", mt.wait_queue_token);
4d476f
+		*attempt_id = sdbm_hash(attempt_id_comp, 0);
4d476f
+		snprintf(attempt_id_comp, 20, "%u", mt.pid);
4d476f
+		*attempt_id = sdbm_hash(attempt_id_comp, *attempt_id);
4d476f
+		*attempt_id = sdbm_hash(mt.name, *attempt_id);
4d476f
+		status = pthread_setspecific(key_thread_attempt_id, attempt_id);
4d476f
+		if (status != 0)
4d476f
+			fatal(status);
4d476f
+	}
4d476f
+
4d476f
 	args->signaled = 1;
4d476f
 	status = pthread_cond_signal(&args->cond);
4d476f
 	if (status)
4d476f
--- autofs-5.0.7.orig/daemon/indirect.c
4d476f
+++ autofs-5.0.7/daemon/indirect.c
4d476f
@@ -726,6 +726,8 @@ static void *do_mount_indirect(void *arg
4d476f
 	char buf[PATH_MAX + 1];
4d476f
 	struct stat st;
4d476f
 	int len, status, state;
4d476f
+	char attempt_id_comp[20];
4d476f
+	unsigned long *attempt_id;
4d476f
 
4d476f
 	args = (struct pending_args *) arg;
4d476f
 
4d476f
@@ -735,6 +737,21 @@ static void *do_mount_indirect(void *arg
4d476f
 
4d476f
 	ap = mt.ap;
4d476f
 
4d476f
+	attempt_id = pthread_getspecific(key_thread_attempt_id);
4d476f
+	if (attempt_id == NULL) {
4d476f
+		attempt_id = (unsigned long *) calloc(1, sizeof(unsigned long));
4d476f
+		if (attempt_id  == NULL)
4d476f
+			fatal(ENOMEM);
4d476f
+		snprintf(attempt_id_comp, 20, "%ld", mt.wait_queue_token);
4d476f
+		*attempt_id = sdbm_hash(attempt_id_comp, 0);
4d476f
+		snprintf(attempt_id_comp, 20, "%u", mt.pid);
4d476f
+		*attempt_id = sdbm_hash(attempt_id_comp, *attempt_id);
4d476f
+		*attempt_id = sdbm_hash(mt.name, *attempt_id);
4d476f
+		status = pthread_setspecific(key_thread_attempt_id, attempt_id);
4d476f
+		if (status != 0)
4d476f
+			fatal(status);
4d476f
+	}
4d476f
+
4d476f
 	args->signaled = 1;
4d476f
 	status = pthread_cond_signal(&args->cond);
4d476f
 	if (status)
4d476f
--- autofs-5.0.7.orig/include/automount.h
4d476f
+++ autofs-5.0.7/include/automount.h
4d476f
@@ -76,6 +76,8 @@ int load_autofs4_module(void);
4d476f
 #define SMB_SUPER_MAGIC    0x0000517BL
4d476f
 #define CIFS_MAGIC_NUMBER  0xFF534D42L
4d476f
 
4d476f
+#define ATTEMPT_ID_SIZE 24
4d476f
+
4d476f
 /* This sould be enough for at least 20 host aliases */
4d476f
 #define HOST_ENT_BUF_SIZE	2048
4d476f
 
4d476f
@@ -241,6 +243,8 @@ const char **copy_argv(int argc, const c
4d476f
 int compare_argv(int argc1, const char **argv1, int argc2, const char **argv2);
4d476f
 int free_argv(int argc, const char **argv);
4d476f
 
4d476f
+unsigned long sdbm_hash(const char *str, unsigned long seed);
4d476f
+
4d476f
 void dump_core(void);
4d476f
 int aquire_lock(void);
4d476f
 void release_lock(void);
4d476f
@@ -480,6 +484,8 @@ struct thread_stdenv_vars {
4d476f
 
4d476f
 extern pthread_key_t key_thread_stdenv_vars;
4d476f
 
4d476f
+extern pthread_key_t key_thread_attempt_id;
4d476f
+
4d476f
 struct kernel_mod_version {
4d476f
 	unsigned int major;
4d476f
 	unsigned int minor;