Skip to contentSkip to conversation

fix hung automounter issues

RFC PATCH In review Jeff Moyer 2 participants 16 messages Apr 14, 2005 – Apr 24, 2005

Subsystems (3)
lore
Share & tools
lore

Jeff Moyer

author
#0 lore raw

RFC PATCH 1/1 fix hung automounter issues

Hi, Ian,

Dan Berrange did a spectacular job of diagnosing some autofs hangs he was experiencing. The automount daemon would appear hung, and an an strace of the process would show sys_futex on the call stack. Through his debugging, he figured out that the problem was autofs issuing syslog calls while in a signal handler.

So, there are a few ways to fix this that come to mind.

o We could not log in signal handlers. I don't consider this an acceptable solution, as we really need the debug messages generated there.
o We could defer logging to non-signal handler context. This is in fact what the attached pattch does. It queues the syslog messages, and flushes them upon the next syslog that occurs outside of signal handler context.
o We could open /dev/log directly. This is likely a bad idea as there is no standard for the interface there.
o We could have a separate logging process, which we write to via a pipe. I'm not keen on this as it adds yet another process, and makes shutdown that much more complicated.

Note that in all of the above cases, we still need to implement a signal-safe vsprintf. That is what the bulk of this patch is.

So, here is a rough take on implementing the second bullet point. I wholesale copied a bunch of code from the linux kernel for doing vsprintf. That bit is ugly. I'd also move the definition of the new logging routines into the vsprintf file, and rename it. In short, this is a proof of concept (shown to resolve the issues). I'm happy to clean it up, but I want to be sure that this is the direction we want to go in, first.

Limitations of this approach: we won't flush the logs that were issued in signal handler context until another syslog call is made. One improvement that could be made straight-away is to have all of the logging routines call flush_logs, even if the log priority is set low enough that they wouldn't otherwise log.

Comments encouraged. Thanks again to Dan!

-Jeff

Note for reviewers: the variable ringlen is a misnomer. I started with the idea of implementing a ring buffer, but threw it out. This is just a fixed size array of log buffers, and I believe that to be sufficient, since we shouldn't be doing too much logging in signal handlers, anyway.

daemon/automount.c +74 -0
diff -uprN --exclude=configure --exclude=rc.autofs autofs-4.1.4_beta2.orig/daemon/automount.c autofs-4.1.4_beta2/daemon/automount.c
@@ -27,6 +27,7 @@
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <string.h>
 #include <syslog.h>
 #include <unistd.h>
@@ -64,6 +65,7 @@ int do_debug = 0;		/* Enable full debug 
 sigset_t ready_sigs;		/* signals only accepted in ST_READY */
 sigset_t lock_sigs;		/* signals blocked for locking */
 sigset_t sigchld_mask;
+int in_interrupt = 0;		/* keeps us from logging in a signal handler */
 
 struct autofs_point ap;
 
@@ -79,6 +81,72 @@ static void cleanup_exit(const char *pat
 static int handle_packet_expire(const struct autofs_packet_expire *pkt);
 static int umount_all(int force);
 
+#define LOGQUEUE_LINE_LEN 128
+#define LOGQUEUE_MAX      128
+struct log_queue {
+	int  pri;
+	char buf[LOGQUEUE_LINE_LEN];
+} logbuf[LOGQUEUE_MAX];
+static int ringlen;
+
+static void block_signals(sigset_t *set)
+{
+	sigset_t allsigs;
+
+	sigfillset(&allsigs);
+	sigprocmask(SIG_BLOCK, &allsigs, set);
+}
+
+static void unblock_signals(sigset_t *set)
+{
+	sigprocmask(SIG_SETMASK, set, NULL);
+}
+
+void flush_logs(void)
+{
+	int i;
+	sigset_t set;
+
+	block_signals(&set);
+	assert(!in_interrupt);
+	for (i = 0; i < ringlen; i++)
+		syslog(logbuf[i].pri, logbuf[i].buf);
+	ringlen = 0;
+	unblock_signals(&set);
+}
+
+void queue_syslog(int priority, const char *format, ...)
+{
+	va_list args;
+	struct log_queue *rp;
+	sigset_t set;
+
+	assert(in_interrupt);
+
+	block_signals(&set);
+	if (ringlen >= LOGQUEUE_MAX) {
+#ifdef DEBUG
+		/*
+		 *  We want to know if we are exceeding the max number of
+		 *  log entrise.
+		 */
+		*(void *)0 = 0;
+#else
+		unblock_signals(&set);
+		return;
+#endif
+	}
+	rp = &logbuf[ringlen];
+	ringlen++;
+	unblock_signals(&set);
+
+	va_start(args, format);
+	vsnprintf_int(rp->buf, LOGQUEUE_LINE_LEN, format, args);
+	va_end(args);
+	rp->buf[LOGQUEUE_LINE_LEN - 1] = '\0';
+	rp->pri = priority;
+}
+
 int mkdir_path(const char *path, mode_t mode)
 {
 	char *buf = alloca(strlen(path) + 1);
@@ -490,6 +558,7 @@ static void sig_statemachine(int sig)
 	int save_errno = errno;
 	enum states next = ap.state;
 
+	in_interrupt++;
 	switch (sig) {
 	default:		/* all the "can't happen" signals */
 		error("process %d got unexpected signal %d!", getpid(), sig);
@@ -521,6 +590,7 @@ static void sig_statemachine(int sig)
 	debug("sig %d switching from %d to %d", sig, ap.state, next);
 
 	errno = save_errno;
+	in_interrupt--;
 }
 
 static int send_ready(unsigned int wait_queue_token)
@@ -661,6 +731,7 @@ static void sig_child(int sig)
 	int save_errno = errno;
 	enum states next;
 
+	in_interrupt++;
 	if (sig != SIGCHLD)
 		return;
 
@@ -669,6 +740,7 @@ static void sig_child(int sig)
 		nextstate(next);
 
 	errno = save_errno;
+	in_interrupt--;
 }
 
 static int st_ready(void)
@@ -1481,6 +1553,7 @@ static void sig_supervisor(int sig)
 {
 	int save_errno = errno;
 
+	in_interrupt++;
 	switch (sig) {
 	default:		/* all the signals not handled */
 		error("process %d got unexpected signal %d!", getpid(), sig);
@@ -1512,6 +1585,7 @@ static void sig_supervisor(int sig)
 		break;
 	}
 	errno = save_errno;
+	in_interrupt--;
 }
 
 int supervisor(char *path)
include/automount.h +42 -15

80 lines not on this page · raw message

lib/Makefile +5 -1
diff -uprN --exclude=configure --exclude=rc.autofs autofs-4.1.4_beta2.orig/lib/Makefile autofs-4.1.4_beta2/lib/Makefile
@@ -12,7 +12,7 @@ RANLIB = /usr/bin/ranlib
 SRCS = cache.c listmount.c cat_path.c rpc_subs.c mounts.c lock.c
 RPCS = mount.h mount_clnt.c mount_xdr.c
 OBJS = cache.o mount_clnt.o mount_xdr.o listmount.o \
-	cat_path.o rpc_subs.o mounts.o lock.o
+	cat_path.o rpc_subs.o mounts.o lock.o vsprintf.o
 
 LIB = autofs.a
 
@@ -48,6 +48,10 @@ listmount.o: listmount.c
 	$(CC) $(CFLAGS) -o listmount.o -c listmount.c
 	$(STRIP) listmount.o
 
+vsprintf.o: vsprintf.c
+	$(CC) $(CFLAGS) -o vsprintf.o -c vsprintf.c
+	$(STRIP) vsprintf.o
+
 install: all
 
 clean:
lib/vsprintf.c +594 -0

594 lines not on this page · raw message

Binary files autofs-4.1.4_beta2.orig/samples/autofs-ldap-auto-master and autofs-4.1.4_beta2/samples/autofs-ldap-auto-master differ Binary files autofs-4.1.4_beta2.orig/samples/autofs-ldap-auto-master.o and autofs-4.1.4_beta2/samples/autofs-ldap-auto-master.o differ

maintainer
#1 lore raw

On Thu, 14 Apr 2005, Jeff Moyer wrote:

I'm inpressed, this looks like a fine piece of work guys. It's going to take a while to work through this.

Also This is bound to make merging Denis Vlasenkos' logging patches really hard. So there's potentially a fair bit of work in this.

Quoted 7 lines

Hi, Ian,

Dan Berrange did a spectacular job of diagnosing some autofs hangs he was experiencing. The automount daemon would appear hung, and an an strace of the process would show sys_futex on the call stack. Through his debugging, he figured out that the problem was autofs issuing syslog calls while in a signal handler.

One, probably stupid question (humour me).

Shouldn't we be able use syslog in a signal handler?

Quoted 15 lines

So, there are a few ways to fix this that come to mind.

o We could not log in signal handlers. I don't consider this an acceptable solution, as we really need the debug messages generated there.
o We could defer logging to non-signal handler context. This is in fact what the attached pattch does. It queues the syslog messages, and flushes them upon the next syslog that occurs outside of signal handler context.
o We could open /dev/log directly. This is likely a bad idea as there is no standard for the interface there.
o We could have a separate logging process, which we write to via a pipe. I'm not keen on this as it adds yet another process, and makes shutdown that much more complicated.

I actually like the last option but a multi threaded autofs is not likely to happen for a long time.

Would another option be to make autofs signal safe by doing all the work in subprocesses?

Quoted 2 lines

Note that in all of the above cases, we still need to implement a signal-safe vsprintf. That is what the bulk of this patch is.

What about a signal safe syslog?
Is this something we push to the libc guys, both a signal safe vsprintf and syslog?

Quoted 8 lines

So, here is a rough take on implementing the second bullet point. I wholesale copied a bunch of code from the linux kernel for doing vsprintf. That bit is ugly. I'd also move the definition of the new logging routines into the vsprintf file, and rename it. In short, this is a proof of concept (shown to resolve the issues). I'm happy to clean it up, but I want to be sure that this is the direction we want to go in, first.

Quoted 8 lines

Limitations of this approach: we won't flush the logs that were issued in signal handler context until another syslog call is made. One improvement that could be made straight-away is to have all of the logging routines call flush_logs, even if the log priority is set low enough that they wouldn't otherwise log.

Comments encouraged. Thanks again to Dan!

Ian

author
#2 lore raw

==> Regarding Re: [patch, rfc] fix hung automounter issues; Ian Kent <[email protected]> adds:

raven> On Thu, 14 Apr 2005, Jeff Moyer wrote: I'm inpressed, this looks raven> like a fine piece of work guys. It's going to take a while to work raven> through this.

raven> Also This is bound to make merging Denis Vlasenkos' logging patches raven> really hard. So there's potentially a fair bit of work in this.

Well, you can go ahead and apply his patches, and I'll merge this stuff in afterwards. Just let me know when you have that all set in CVS.

Quoted 7 lines
Quoted 7 lines

Hi, Ian,

Dan Berrange did a spectacular job of diagnosing some autofs hangs he was experiencing. The automount daemon would appear hung, and an an strace of the process would show sys_futex on the call stack. Through his debugging, he figured out that the problem was autofs issuing syslog calls while in a signal handler.

raven> One, probably stupid question (humour me).

raven> Shouldn't we be able use syslog in a signal handler?

Nope:

http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html#tag_02_04

Quoted 13 lines
Quoted 13 lines

So, there are a few ways to fix this that come to mind.

o We could not log in signal handlers. I don't consider this an acceptable solution, as we really need the debug messages generated there. o We could defer logging to non-signal handler context. This is in fact what the attached pattch does. It queues the syslog messages, and flushes them upon the next syslog that occurs outside of signal handler context. o We could open /dev/log directly. This is likely a bad idea as there is no standard for the interface there. o We could have a separate logging process, which we write to via a pipe. I'm not keen on this as it adds yet another process, and makes shutdown that much more complicated.

raven> I actually like the last option but a multi threaded autofs is not raven> likely to happen for a long time.

It does not imply threading. You can simply fork().

raven> Would another option be to make autofs signal safe by doing all the raven> work in subprocesses?

There are a number of ways to address this by changing the control structure of the daemon. However, that seemed way to invasive at this stage of the game. The ideal solution is to do less in the sig_child handler, deferring the work just like we do for all of the other caught signals.

Quoted 2 lines
Quoted 2 lines

Note that in all of the above cases, we still need to implement a signal-safe vsprintf. That is what the bulk of this patch is.

raven> What about a signal safe syslog? Is this something we push to the raven> libc guys, both a signal safe vsprintf and syslog?

Well, we can certainly try, but this won't gain you anything in the short term.

I'll think more about having a separate logging process. If it seems easy enough to implement, I'll post a first whack at it. In the mean time, I think we should continue to discuss our alternatives.

-Jeff

#3 lore raw
Quoted 20 lines · Jeff Moyer

==> Regarding Re: [patch, rfc] fix hung automounter issues; Ian Kent <[email protected]> adds:

raven> On Thu, 14 Apr 2005, Jeff Moyer wrote: I'm inpressed, this looks raven> like a fine piece of work guys. It's going to take a while to work raven> through this.

raven> Also This is bound to make merging Denis Vlasenkos' logging patches raven> really hard. So there's potentially a fair bit of work in this.

Well, you can go ahead and apply his patches, and I'll merge this stuff in afterwards. Just let me know when you have that all set in CVS.

Quoted 7 lines
Quoted 7 lines

Hi, Ian,

Dan Berrange did a spectacular job of diagnosing some autofs hangs he was experiencing. The automount daemon would appear hung, and an an strace of the process would show sys_futex on the call stack. Through his debugging, he figured out that the problem was autofs issuing syslog calls while in a signal handler.

Quoted 7 lines

raven> One, probably stupid question (humour me).

raven> Shouldn't we be able use syslog in a signal handler?

Nope:

http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html#tag_02_04

I have to plead guilty to the screw up (at least I'm not the only guilty party) here and point out that there's also a few ioctl calls in the signal handling execution paths.

I should be able to the move the ioctl in ST_SHUTDOWN_PENDING to the st_shutdown_pending function but moving them in send_ready and send_fail will be somewhat harder.

What else have we missed?

Quoted 35 lines
Quoted 14 lines
Quoted 14 lines

So, there are a few ways to fix this that come to mind.

o We could not log in signal handlers. I don't consider this an acceptable solution, as we really need the debug messages generated there. o We could defer logging to non-signal handler context. This is in fact what the attached pattch does. It queues the syslog messages, and flushes them upon the next syslog that occurs outside of signal handler context. o We could open /dev/log directly. This is likely a bad idea as there is no standard for the interface there. o We could have a separate logging process, which we write to via a pipe. I'm not keen on this as it adds yet another process, and makes shutdown that much more complicated.

raven> I actually like the last option but a multi threaded autofs is not raven> likely to happen for a long time.

It does not imply threading. You can simply fork().

raven> Would another option be to make autofs signal safe by doing all the raven> work in subprocesses?

There are a number of ways to address this by changing the control structure of the daemon. However, that seemed way to invasive at this stage of the game. The ideal solution is to do less in the sig_child handler, deferring the work just like we do for all of the other caught signals.

Quoted 2 lines
Quoted 2 lines

Note that in all of the above cases, we still need to implement a signal-safe vsprintf. That is what the bulk of this patch is.

raven> What about a signal safe syslog? Is this something we push to the raven> libc guys, both a signal safe vsprintf and syslog?

Quoted 6 lines

Well, we can certainly try, but this won't gain you anything in the short term.

I'll think more about having a separate logging process. If it seems easy enough to implement, I'll post a first whack at it. In the mean time, I think we should continue to discuss our alternatives.

I must admit, the more I delve into this, the more the deffered logging idea grows on me.

Let me look at the patches a while longer.

Ian

#4 lore raw
In reply to Jeff Moyer
Quoted 32 lines · Jeff Moyer

Hi, Ian,

Dan Berrange did a spectacular job of diagnosing some autofs hangs he was experiencing. The automount daemon would appear hung, and an an strace of the process would show sys_futex on the call stack. Through his debugging, he figured out that the problem was autofs issuing syslog calls while in a signal handler.

So, there are a few ways to fix this that come to mind.

o We could not log in signal handlers. I don't consider this an acceptable solution, as we really need the debug messages generated there.
o We could defer logging to non-signal handler context. This is in fact what the attached pattch does. It queues the syslog messages, and flushes them upon the next syslog that occurs outside of signal handler context.
o We could open /dev/log directly. This is likely a bad idea as there is no standard for the interface there.
o We could have a separate logging process, which we write to via a pipe. I'm not keen on this as it adds yet another process, and makes shutdown that much more complicated.

Note that in all of the above cases, we still need to implement a signal-safe vsprintf. That is what the bulk of this patch is.

So, here is a rough take on implementing the second bullet point. I wholesale copied a bunch of code from the linux kernel for doing vsprintf. That bit is ugly. I'd also move the definition of the new logging routines into the vsprintf file, and rename it. In short, this is a proof of concept (shown to resolve the issues). I'm happy to clean it up, but I want to be sure that this is the direction we want to go in, first.

Quoted 7 lines

Limitations of this approach: we won't flush the logs that were issued in signal handler context until another syslog call is made. One improvement that could be made straight-away is to have all of the logging routines call flush_logs, even if the log priority is set low enough that they wouldn't otherwise log.

Comments encouraged. Thanks again to Dan!

Hi Jeff,

There were a few things missed in the patch as there's a few other places that calls to illegal routines are made.

I did a bit of work on it over the weekend.

Basically the changes I have made are:

- moved calls to signal_children out of signal handler.
- created seperate module for safe_syslog implementation.
- changed assert to use safe_syslog also.
- removed assertions from safe logging routines as it also calls (safe_)syslog.
- added attempt at last gasp message in queue_syslog.
- added some string termination logic to queue_syslog.

There's probably stuff that I've missed.
I've done some basic testing but more is needed.

Can you review this and alter as you see fit please Jeff?

This just leaves the ioctl calls.
I'm hoping we can verify they are safe. We can check the kernel control path and if we can verify that the glibc code is suitably reentrant they are probably OK. I'm having trouble finding the right code in glibc. Do you have a glibc person that could point us in the right direction Jeff?

We probably should agree on a name for the patch to avoid confusion later. I've called mine autofs-4.1.4-signal-safe-logging.patch. What name do you like?

Ian

daemon/automount.c +70 -26
diff -Nurp autofs-4.1.4.orig/daemon/automount.c autofs-4.1.4/daemon/automount.c
@@ -5,7 +5,7 @@
  *
  *   Copyright 1997 Transmeta Corporation - All Rights Reserved
  *   Copyright 1999-2000 Jeremy Fitzhardinge <[email protected]>
- *   Copyright 2001-2003 Ian Kent <[email protected]>
+ *   Copyright 2001-2005 Ian Kent <[email protected]>
  *
  *   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
@@ -39,7 +39,14 @@
 #include <linux/auto_fs4.h>
 
 #ifndef NDEBUG
-#define assert(x)	do { if (!(x)) { syslog(LOG_CRIT, __FILE__ ":%d: assertion failed: " #x, __LINE__); } } while(0)
+#define assert(x) 							   \
+	do { 								   \
+		if (!(x)) {						   \
+			safe_logger(LOG_CRIT,				   \
+				    __FILE__ ":%d: assertion failed: " #x, \
+				    __LINE__);				   \
+		}							   \
+	} while(0)
 #else
 #define assert(x)	do { } while(0)
 #endif
@@ -58,14 +65,12 @@ int kproto_sub_version = 0;	/* Kernel pr
 
 static int submount = 0;
 
-int do_verbose = 0;		/* Verbose feedback option */
-int do_debug = 0;		/* Enable full debug output */
-
 sigset_t ready_sigs;		/* signals only accepted in ST_READY */
 sigset_t lock_sigs;		/* signals blocked for locking */
 sigset_t sigchld_mask;
 
 struct autofs_point ap;
+extern volatile int in_interrupt;
 
 volatile struct pending_mount *junk_mounts = NULL;
 
@@ -490,6 +495,7 @@ static void sig_statemachine(int sig)
 	int save_errno = errno;
 	enum states next = ap.state;
 
+	in_interrupt++;
 	switch (sig) {
 	default:		/* all the "can't happen" signals */
 		error("process %d got unexpected signal %d!", getpid(), sig);
@@ -521,6 +527,7 @@ static void sig_statemachine(int sig)
 	debug("sig %d switching from %d to %d", sig, ap.state, next);
 
 	errno = save_errno;
+	in_interrupt--;
 }
 
 static int send_ready(unsigned int wait_queue_token)
@@ -565,7 +572,7 @@ static enum states handle_child(int hang
 
 		/* Check to see if expire process finished */
 		if (pid == ap.exp_process) {
-			int success, ret;
+			int success;
 
 			if (!WIFEXITED(status))
 				continue;
@@ -594,15 +601,8 @@ static enum states handle_child(int hang
 
 			case ST_SHUTDOWN_PENDING:
 				next = ST_SHUTDOWN;
-				if (success) {
-					ret = ioctl(ap.ioctlfd,
-						AUTOFS_IOC_ASKUMOUNT, &status);
-					if (!ret) {
-						if (status)
-							break;
-					} else
-						break;
-				}
+				if (success)
+					break;
 
 				/* Failed shutdown returns to ready */
 				warn("can't shutdown: filesystem %s still busy",
@@ -661,6 +661,7 @@ static void sig_child(int sig)
 	int save_errno = errno;
 	enum states next;
 
+	in_interrupt++;
 	if (sig != SIGCHLD)
 		return;
 
@@ -669,6 +670,7 @@ static void sig_child(int sig)
 		nextstate(next);
 
 	errno = save_errno;
+	in_interrupt--;
 }
 
 static int st_ready(void)
@@ -1487,6 +1489,7 @@ static void sig_supervisor(int sig)
 {
 	int save_errno = errno;
 
+	in_interrupt++;
 	switch (sig) {
 	default:		/* all the signals not handled */
 		error("process %d got unexpected signal %d!", getpid(), sig);
@@ -1495,13 +1498,11 @@ static void sig_supervisor(int sig)
 
 	case SIGTERM:
 	case SIGUSR2:
-		/* Tell everyone to finish up */
-		signal_children(sig);
+		ap.state = ST_SHUTDOWN_PENDING;
 		break;
 
 	case SIGUSR1:
-		/* Pass on the prune event and ignore self signal */
-		signal_children(sig);
+		ap.state = ST_PRUNE;
 		break;
 
 	case SIGCHLD:
@@ -1509,20 +1510,18 @@ static void sig_supervisor(int sig)
 		break;
 
 	case SIGHUP:
-		ap.lookup->lookup_ghost(ap.path, ap.ghost, 0, ap.lookup->context);
-
 		/* Pass on the reread event and ignore self signal */
-		kill(0, SIGHUP);
-		discard_pending(SIGHUP);
-
+		ap.state = ST_READMAP;
 		break;
 	}
 	errno = save_errno;
+	in_interrupt--;
 }
 
 int supervisor(char *path)
 {
 	unsigned int map = 0;
+	int ret;
 
 	ap.path = alloca(strlen(path) + 1);
 	strcpy(ap.path, path);
@@ -1538,6 +1537,37 @@ int supervisor(char *path)
 
 	setup_signals(sig_supervisor, sig_supervisor);
 
+	while (ap.state != ST_SHUTDOWN) {
+		switch (ap.state) {
+		case ST_READMAP:
+			st_readmap();
+			signal_children(SIGHUP);
+			ap.state = ST_READY;
+			break;
+		case ST_SHUTDOWN_PENDING:
+			ret = signal_children(SIGUSR2);
+			if (!ret) {
+				ap.state = ST_SHUTDOWN;
+				break;
+			}
+
+			/* Failed shutdown returns to ready */
+			warn("can't shutdown: filesystem %s still busy",
+				     ap.path);
+			ap.state = ST_READY;
+			break;
+		case ST_PRUNE:
+			/* Pass on the prune event and ignore self signal */
+			signal_children(SIGUSR1);
+			ap.state = ST_READY;
+			break;
+		default:
+			ap.state = ST_READY;
+			break;
+		}
+		sleep(1);
+	}
+
 	while (waitpid(0, NULL, 0) > 0);
 
 	return 0;
@@ -1644,8 +1674,23 @@ int handle_mounts(char *path)
 		kill(my_pid, SIGSTOP);
 
 	while (ap.state != ST_SHUTDOWN) {
-		if (handle_packet() && errno != EINTR)
-			break;
+		if (handle_packet() && errno != EINTR) {
+			int ret, status = 0;
+
+			ret = ioctl(ap.ioctlfd, AUTOFS_IOC_ASKUMOUNT, &status);
+			/* +			 * If the ioctl fails assume the kernel doesn't have
+			 * AUTOFS_IOC_ASKUMOUNT and just continue.
+			 */
+			if (!ret && status)
+				break;
+
+			/* Failed shutdown returns to ready */
+			warn("can't shutdown: filesystem %s still busy",
+				     ap.path);
+			ap.state = ST_READY;
+			alarm(ap.exp_runfreq);
+		}
 	}
 
 	/* Mop up remaining kids */
include/automount.h +48 -19

79 lines not on this page · raw message

lib/Makefile +4 -2
diff -Nurp autofs-4.1.4.orig/lib/Makefile autofs-4.1.4/lib/Makefile
@@ -9,10 +9,12 @@ include ../Makefile.rules
 RPCGEN = /usr/bin/rpcgen
 RANLIB = /usr/bin/ranlib
 
-SRCS = cache.c listmount.c cat_path.c rpc_subs.c mounts.c lock.c
+SRCS = cache.c listmount.c cat_path.c rpc_subs.c mounts.c lock.c \
+	safe_logger.c vsprintf.c
 RPCS = mount.h mount_clnt.c mount_xdr.c
 OBJS = cache.o mount_clnt.o mount_xdr.o listmount.o \
-	cat_path.o rpc_subs.o mounts.o lock.o
+	cat_path.o rpc_subs.o mounts.o lock.o \
+	safe_logger.o vsprintf.o
 
 LIB = autofs.a
 
lib/safe_logger.c +114 -0

114 lines not on this page · raw message

lib/vsprintf.c +592 -0

592 lines not on this page · raw message

#5 lore raw

On Mon, 18 Apr 2005 [email protected] wrote:

Hang on, I think I've got a problem with white space and my mailer.

If you have problems with the patch in the previous mail try this one instead (maybe just use this one).

daemon/automount.c +71 -26
diff -Nurp autofs-4.1.4.orig/daemon/automount.c autofs-4.1.4/daemon/automount.c
@@ -5,7 +5,7 @@
   *
   *   Copyright 1997 Transmeta Corporation - All Rights Reserved
   *   Copyright 1999-2000 Jeremy Fitzhardinge <[email protected]>
- *   Copyright 2001-2003 Ian Kent <[email protected]>
+ *   Copyright 2001-2005 Ian Kent <[email protected]>
   *
   *   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
@@ -39,7 +39,14 @@
  #include <linux/auto_fs4.h>
 
  #ifndef NDEBUG
-#define assert(x)	do { if (!(x)) { syslog(LOG_CRIT, __FILE__ ":%d: assertion failed: " #x, __LINE__); } } while(0)
+#define assert(x) 							   \
+	do { 								   \
+		if (!(x)) {						   \
+			safe_logger(LOG_CRIT,				   \
+				    __FILE__ ":%d: assertion failed: " #x, \
+				    __LINE__);				   \
+		}							   \
+	} while(0)
  #else
  #define assert(x)	do { } while(0)
  #endif
@@ -58,14 +65,12 @@ int kproto_sub_version = 0;	/* Kernel pr
 
  static int submount = 0;
 
-int do_verbose = 0;		/* Verbose feedback option */
-int do_debug = 0;		/* Enable full debug output */
-
  sigset_t ready_sigs;		/* signals only accepted in ST_READY */
  sigset_t lock_sigs;		/* signals blocked for locking */
  sigset_t sigchld_mask;
 
  struct autofs_point ap;
+extern volatile int in_interrupt;
 
  volatile struct pending_mount *junk_mounts = NULL;
 
@@ -490,6 +495,7 @@ static void sig_statemachine(int sig)
  	int save_errno = errno;
  	enum states next = ap.state;
 
+	in_interrupt++;
  	switch (sig) {
  	default:		/* all the "can't happen" signals */
  		error("process %d got unexpected signal %d!", getpid(), sig);
@@ -521,6 +527,7 @@ static void sig_statemachine(int sig)
  	debug("sig %d switching from %d to %d", sig, ap.state, next);
 
  	errno = save_errno;
+	in_interrupt--;
  }
 
  static int send_ready(unsigned int wait_queue_token)
@@ -565,7 +572,7 @@ static enum states handle_child(int hang
 
  		/* Check to see if expire process finished */
  		if (pid == ap.exp_process) {
-			int success, ret;
+			int success;
 
  			if (!WIFEXITED(status))
  				continue;
@@ -594,15 +601,8 @@ static enum states handle_child(int hang
 
  			case ST_SHUTDOWN_PENDING:
  				next = ST_SHUTDOWN;
-				if (success) {
-					ret = ioctl(ap.ioctlfd,
-						AUTOFS_IOC_ASKUMOUNT, &status);
-					if (!ret) {
-						if (status)
-							break;
-					} else
-						break;
-				}
+				if (success)
+					break;
 
  				/* Failed shutdown returns to ready */
  				warn("can't shutdown: filesystem %s still busy",
@@ -661,6 +661,7 @@ static void sig_child(int sig)
  	int save_errno = errno;
  	enum states next;
 
+	in_interrupt++;
  	if (sig != SIGCHLD)
  		return;
 
@@ -669,6 +670,7 @@ static void sig_child(int sig)
  		nextstate(next);
 
  	errno = save_errno;
+	in_interrupt--;
  }
 
  static int st_ready(void)
@@ -1487,6 +1489,7 @@ static void sig_supervisor(int sig)
  {
  	int save_errno = errno;
 
+	in_interrupt++;
  	switch (sig) {
  	default:		/* all the signals not handled */
  		error("process %d got unexpected signal %d!", getpid(), sig);
@@ -1495,13 +1498,11 @@ static void sig_supervisor(int sig)
 
  	case SIGTERM:
  	case SIGUSR2:
-		/* Tell everyone to finish up */
-		signal_children(sig);
+		ap.state = ST_SHUTDOWN_PENDING;
  		break;
 
  	case SIGUSR1:
-		/* Pass on the prune event and ignore self signal */
-		signal_children(sig);
+		ap.state = ST_PRUNE;
  		break;
 
  	case SIGCHLD:
@@ -1509,20 +1510,18 @@ static void sig_supervisor(int sig)
  		break;
 
  	case SIGHUP:
-		ap.lookup->lookup_ghost(ap.path, ap.ghost, 0, ap.lookup->context);
-
  		/* Pass on the reread event and ignore self signal */
-		kill(0, SIGHUP);
-		discard_pending(SIGHUP);
-
+		ap.state = ST_READMAP;
  		break;
  	}
  	errno = save_errno;
+	in_interrupt--;
  }
 
  int supervisor(char *path)
  {
  	unsigned int map = 0;
+	int ret;
 
  	ap.path = alloca(strlen(path) + 1);
  	strcpy(ap.path, path);
@@ -1538,6 +1537,37 @@ int supervisor(char *path)
 
  	setup_signals(sig_supervisor, sig_supervisor);
 
+	while (ap.state != ST_SHUTDOWN) {
+		switch (ap.state) {
+		case ST_READMAP:
+			st_readmap();
+			signal_children(SIGHUP);
+			ap.state = ST_READY;
+			break;
+		case ST_SHUTDOWN_PENDING:
+			ret = signal_children(SIGUSR2);
+			if (!ret) {
+				ap.state = ST_SHUTDOWN;
+				break;
+			}
+
+			/* Failed shutdown returns to ready */
+			warn("can't shutdown: filesystem %s still busy",
+				     ap.path);
+			ap.state = ST_READY;
+			break;
+		case ST_PRUNE:
+			/* Pass on the prune event and ignore self signal */
+			signal_children(SIGUSR1);
+			ap.state = ST_READY;
+			break;
+		default:
+			ap.state = ST_READY;
+			break;
+		}
+		sleep(1);
+	}
+
  	while (waitpid(0, NULL, 0) > 0);
 
  	return 0;
@@ -1644,8 +1674,23 @@ int handle_mounts(char *path)
  		kill(my_pid, SIGSTOP);
 
  	while (ap.state != ST_SHUTDOWN) {
-		if (handle_packet() && errno != EINTR)
-			break;
+		if (handle_packet() && errno != EINTR) {
+			int ret, status = 0;
+
+			ret = ioctl(ap.ioctlfd, AUTOFS_IOC_ASKUMOUNT, &status);
+			/* 
+			 * If the ioctl fails assume the kernel doesn't have
+			 * AUTOFS_IOC_ASKUMOUNT and just continue.
+			 */
+			if (!ret && status)
+				break;
+
+			/* Failed shutdown returns to ready */
+			warn("can't shutdown: filesystem %s still busy",
+				     ap.path);
+			ap.state = ST_READY;
+			alarm(ap.exp_runfreq);
+		}
  	}
 
  	/* Mop up remaining kids */
include/automount.h +48 -19

79 lines not on this page · raw message

lib/Makefile +4 -2
diff -Nurp autofs-4.1.4.orig/lib/Makefile autofs-4.1.4/lib/Makefile
@@ -9,10 +9,12 @@ include ../Makefile.rules
  RPCGEN = /usr/bin/rpcgen
  RANLIB = /usr/bin/ranlib
 
-SRCS = cache.c listmount.c cat_path.c rpc_subs.c mounts.c lock.c
+SRCS = cache.c listmount.c cat_path.c rpc_subs.c mounts.c lock.c \
+	safe_logger.c vsprintf.c
  RPCS = mount.h mount_clnt.c mount_xdr.c
  OBJS = cache.o mount_clnt.o mount_xdr.o listmount.o \
-	cat_path.o rpc_subs.o mounts.o lock.o
+	cat_path.o rpc_subs.o mounts.o lock.o \
+	safe_logger.o vsprintf.o
 
  LIB = autofs.a
 
lib/safe_logger.c +116 -0

116 lines not on this page · raw message

lib/vsprintf.c +594 -0

594 lines not on this page · raw message

author
#6 lore raw

==> Regarding Re: [autofs] Re: [patch, rfc] fix hung automounter issues; [email protected] adds:

raven> On Mon, 18 Apr 2005 [email protected] wrote:

raven> Hang on, I think I've got a problem with white space and my mailer.

raven> If you have problems with the patch in the previous mail try this one raven> instead (maybe just use this one).

Oops, we need to disable signals when flushing the logs. The following is unsafe:

lib/safe_logger.c line 63

Quoted 9 lines
lib/safe_logger.c +9 -0
+static void flush_log(void)
+{
+	int i;
+
+	for (i = 0; i < ringlen; i++)
+		syslog(logbuf[i].pri, logbuf[i].buf);
+	ringlen = 0;
+}
+

-Jeff

#7 lore raw
Quoted 12 lines · Jeff Moyer

==> Regarding Re: [autofs] Re: [patch, rfc] fix hung automounter issues; [email protected] adds:

raven> On Mon, 18 Apr 2005 [email protected] wrote:

raven> Hang on, I think I've got a problem with white space and my mailer.

raven> If you have problems with the patch in the previous mail try this one raven> instead (maybe just use this one).

Oops, we need to disable signals when flushing the logs. The following is unsafe:

Quoted 9 lines

lib/safe_logger.c line 63

Quoted 9 lines
lib/safe_logger.c +9 -0
+static void flush_log(void)
+{
+	int i;
+
+	for (i = 0; i < ringlen; i++)
+		syslog(logbuf[i].pri, logbuf[i].buf);
+	ringlen = 0;
+}
+

But it's static and only called when signals are blocked.

Ian

author
#8 lore raw

==> Regarding Re: [autofs] Re: [patch, rfc] fix hung automounter issues; [email protected] adds:

Quoted 2 lines · Jeff Moyer
Quoted 2 lines

==> Regarding Re: [autofs] Re: [patch, rfc] fix hung automounter issues; [email protected] adds:

Quoted 1 line · raven> On Mon, 18 Apr 2005 [email protected] wrote:
Quoted 1 line

raven> Hang on, I think I've got a problem with white space and my mailer.

Quoted 1 line
Quoted 1 line

raven> If you have problems with the patch in the previous mail try this one raven> instead (maybe just use this one).

Quoted 4 lines
Quoted 4 lines

Oops, we need to disable signals when flushing the logs. The following is unsafe:

Quoted 9 lines
Quoted 9 lines

lib/safe_logger.c line 63

Quoted 9 lines
lib/safe_logger.c +9 -0
+static void flush_log(void)
+{
+	int i;
+
+	for (i = 0; i < ringlen; i++)
+		syslog(logbuf[i].pri, logbuf[i].buf);
+	ringlen = 0;
+}
+

raven> But it's static and only called when signals are blocked.

That's what I get for not going over the whole patch. Sorry about that! I'll try to review this version of the patch either today or tomorrow.

-Jeff

author
#9 lore raw
In reply to [email protected]

==> Regarding Re: [autofs] Re: [patch, rfc] fix hung automounter issues; [email protected] adds:

raven> On Mon, 18 Apr 2005 [email protected] wrote:

raven> Hang on, I think I've got a problem with white space and my mailer.

raven> If you have problems with the patch in the previous mail try this one raven> instead (maybe just use this one).

Okay, I've reviewed all parts except for the actual logging functions. I'm actually not convinced this is the right approach. The overhead of setting up and tearing down the signal mask is quite expensive. I fear that environments with a great number of mounts will fall over when debugging is turned on, and that is the very thing we are trying to save. Perhaps it is worth another look at a separate logging process.

Having said that, the rest of your patch is still relevant. I've spent a good deal of time looking it over, and here are my comments.

@@ -58,14 +65,12 @@ int kproto_sub_version = 0; /* Kernel pr

  static int submount = 0;

-int do_verbose = 0; /* Verbose feedback option */
-int do_debug = 0; /* Enable full debug output */
-
sigset_t ready_sigs; /* signals only accepted in ST_READY */
sigset_t lock_sigs; /* signals blocked for locking */
sigset_t sigchld_mask;

struct autofs_point ap;
+extern volatile int in_interrupt;

Doesn't need to be volatile, so far as I can tell. Also note that we need to be careful to reset in_interrupt if we ever fork() from a signal handler.

@@ -565,7 +572,7 @@ static enum states handle_child(int hang

/* Check to see if expire process finished */ if (pid == ap.exp_process) {
- int success, ret;
+ int success;

if (!WIFEXITED(status))
continue;
@@ -594,15 +601,8 @@ static enum states handle_child(int hang

case ST_SHUTDOWN_PENDING:
next = ST_SHUTDOWN;
- if (success) {
- ret = ioctl(ap.ioctlfd,
- AUTOFS_IOC_ASKUMOUNT, &status);
- if (!ret) {
- if (status)
- break;
- } else
- break;
- }
+ if (success)
+ break;

/* Failed shutdown returns to ready */ warn("can't shutdown: filesystem %s still busy",

This would be okay, except that the AUTOFS_IOC_ASKUMOUNT won't always be run at shutdown. See below.

@@ -1495,13 +1498,11 @@ static void sig_supervisor(int sig)

case SIGTERM:
case SIGUSR2:
- /* Tell everyone to finish up */
- signal_children(sig);
+ ap.state = ST_SHUTDOWN_PENDING;
break;

case SIGUSR1:
- /* Pass on the prune event and ignore self signal */
- signal_children(sig);
+ ap.state = ST_PRUNE;
break;

case SIGCHLD:
@@ -1509,20 +1510,18 @@ static void sig_supervisor(int sig)
break;

case SIGHUP:
- ap.lookup->lookup_ghost(ap.path, ap.ghost, 0, ap.lookup->context);
-
/* Pass on the reread event and ignore self signal */
- kill(0, SIGHUP);
- discard_pending(SIGHUP);
-
+ ap.state = ST_READMAP;
break;
}
errno = save_errno;
+ in_interrupt--;
}

int supervisor(char *path)
{
unsigned int map = 0;
+ int ret;

ap.path = alloca(strlen(path) + 1);
strcpy(ap.path, path);
@@ -1538,6 +1537,37 @@ int supervisor(char *path)

  	setup_signals(sig_supervisor, sig_supervisor);

I think we should set state to ST_READY here. That would make things a bit more clear.

+ while (ap.state != ST_SHUTDOWN) {
+ switch (ap.state) {
+ case ST_READMAP:
+ st_readmap();

No check of return value?

+ signal_children(SIGHUP);
+ ap.state = ST_READY;
+ break;
+ case ST_SHUTDOWN_PENDING:
+ ret = signal_children(SIGUSR2);
+ if (!ret) {
+ ap.state = ST_SHUTDOWN;
+ break;
+ }
+
+ /* Failed shutdown returns to ready */
+ warn("can't shutdown: filesystem %s still busy",
+ ap.path);
+ ap.state = ST_READY;
+ break;
+ case ST_PRUNE:
+ /* Pass on the prune event and ignore self signal */
+ signal_children(SIGUSR1);
+ ap.state = ST_READY;
+ break;
+ default:
+ ap.state = ST_READY;
+ break;
+ }
+ sleep(1);
+ }
+
while (waitpid(0, NULL, 0) > 0);

return 0;

Hmm, this is simply not safe, we can lose signals. You really need to either block signals and implement a queue, or use a pipe like we do with indirect maps.

@@ -1644,8 +1674,23 @@ int handle_mounts(char *path)
kill(my_pid, SIGSTOP);

while (ap.state != ST_SHUTDOWN) {
- if (handle_packet() && errno != EINTR)
- break;
+ if (handle_packet() && errno != EINTR) {

The checking of errno here is completely bogus. You can simply get rid of it, I think. (this is what I was referring to above, about the ioctl not always being run).

+ int ret, status = 0;
+
+ ret = ioctl(ap.ioctlfd, AUTOFS_IOC_ASKUMOUNT, &status);
+ /*
+ * If the ioctl fails assume the kernel doesn't have
+ * AUTOFS_IOC_ASKUMOUNT and just continue.
+ */
+ if (!ret && status)
+ break;

Consider handling EINTR for the ioctl call.

+
+ /* Failed shutdown returns to ready */
+ warn("can't shutdown: filesystem %s still busy",
+ ap.path);
+ ap.state = ST_READY;

I think we should be blocking signals when modifying ap.state.

+ alarm(ap.exp_runfreq);
+ }
}

/* Mop up remaining kids */

Okay, random tangent:

static void sig_statemachine(int sig)

{
	int save_errno = errno;
	enum states next = ap.state;

	in_interrupt++;
	switch (sig) {
	default:		/* all the "can't happen" signals */
		error("process %d got unexpected signal %d!", getpid(), sig);
		break;
		/* don't FALLTHROUGH */
	...
	}

	debug("sig %d switching from %d to %d", sig, ap.state, next);

	errno = save_errno;
	in_interrupt--;
}

So, for sigsegv, sigbus, sigfpe, etc, we are simply trying to continue? That doesn't seem right to me, and the comment in setup_signals agrees:

#ifndef DEBUG
	/* When debugging, these signals should be in the default state; when
	   in production, we want to at least attempt to catch them and shut down. */

Also note this from the sigaction man page:
According to POSIX, the behaviour of a process is undefined after it ignores a SIGFPE, SIGILL, or SIGSEGV signal that was not generated by
the kill() or the raise() functions. Integer division by zero has
undefined result. On some architectures it will generate a SIGFPE sig- nal. (Also dividing the most negative integer by -1 may generate SIGFPE.) Ignoring this signal might lead to an endless loop.

Anyway, something to fix for later, I guess.

include/automount.h +1 -1
diff -Nurp autofs-4.1.4.orig/include/automount.h autofs-4.1.4/include/automount.h
@@ -109,7 +109,7 @@ struct autofs_point {
  	volatile pid_t exp_process;		/* Process that is currently expiring */
  	volatile struct pending_mount *mounts;	/* Pending mount queue */
  	struct lookup_mod *lookup;		/* Lookup module */
-	enum states state;
+	volatile enum states state;
 

Again, why the volatile? Are you worried that the compiler is going to optimize out some code? We certainly don't have issues with multiple threads accessing the data...

-Jeff

maintainer
#10 lore raw
Quoted 8 lines · Jeff Moyer

Okay, I've reviewed all parts except for the actual logging functions. I'm actually not convinced this is the right approach. The overhead of setting up and tearing down the signal mask is quite expensive. I fear that environments with a great number of mounts will fall over when debugging is turned on, and that is the very thing we are trying to save. Perhaps it is worth another look at a separate logging process.

I'm also concerned about using an array to store the log messages. This could overflow in heavy load mount situations.

Ian

#11 lore raw
In reply to Jeff Moyer
Quoted 33 lines · Jeff Moyer

==> Regarding Re: [autofs] Re: [patch, rfc] fix hung automounter issues; [email protected] adds:

raven> On Mon, 18 Apr 2005 [email protected] wrote:

raven> Hang on, I think I've got a problem with white space and my mailer.

raven> If you have problems with the patch in the previous mail try this one raven> instead (maybe just use this one).

Okay, I've reviewed all parts except for the actual logging functions. I'm actually not convinced this is the right approach. The overhead of setting up and tearing down the signal mask is quite expensive. I fear that environments with a great number of mounts will fall over when debugging is turned on, and that is the very thing we are trying to save. Perhaps it is worth another look at a separate logging process.

Having said that, the rest of your patch is still relevant. I've spent a good deal of time looking it over, and here are my comments.

(quoted diff) +1 -3
@@ -58,14 +65,12 @@ int kproto_sub_version = 0;	/* Kernel pr
 
 static int submount = 0;
 
-int do_verbose = 0;		/* Verbose feedback option */
-int do_debug = 0;		/* Enable full debug output */
-
 sigset_t ready_sigs;		/* signals only accepted in ST_READY */
 sigset_t lock_sigs;		/* signals blocked for locking */
 sigset_t sigchld_mask;
 
 struct autofs_point ap;
+extern volatile int in_interrupt;
 
Quoted 3 lines

Doesn't need to be volatile, so far as I can tell. Also note that we need to be careful to reset in_interrupt if we ever fork() from a signal handler.

Yes. I've read a bit further and I see this is not needed for integral data types.

Quoted 131 lines
(quoted diff) +3 -10
@@ -565,7 +572,7 @@ static enum states handle_child(int hang
 
 		/* Check to see if expire process finished */
 		if (pid == ap.exp_process) {
-			int success, ret;
+			int success;
 
 			if (!WIFEXITED(status))
 				continue;
@@ -594,15 +601,8 @@ static enum states handle_child(int hang
 
 			case ST_SHUTDOWN_PENDING:
 				next = ST_SHUTDOWN;
-				if (success) {
-					ret = ioctl(ap.ioctlfd,
-						AUTOFS_IOC_ASKUMOUNT, &status);
-					if (!ret) {
-						if (status)
-							break;
-					} else
-						break;
-				}
+				if (success)
+					break;
 
 				/* Failed shutdown returns to ready */
 				warn("can't shutdown: filesystem %s still busy",

This would be okay, except that the AUTOFS_IOC_ASKUMOUNT won't always be run at shutdown. See below.

(quoted diff) +5 -9
@@ -1495,13 +1498,11 @@ static void sig_supervisor(int sig)
 
 	case SIGTERM:
 	case SIGUSR2:
-		/* Tell everyone to finish up */
-		signal_children(sig);
+		ap.state = ST_SHUTDOWN_PENDING;
 		break;
 
 	case SIGUSR1:
-		/* Pass on the prune event and ignore self signal */
-		signal_children(sig);
+		ap.state = ST_PRUNE;
 		break;
 
 	case SIGCHLD:
@@ -1509,20 +1510,18 @@ static void sig_supervisor(int sig)
 		break;
 
 	case SIGHUP:
-		ap.lookup->lookup_ghost(ap.path, ap.ghost, 0, ap.lookup->context);
-
 		/* Pass on the reread event and ignore self signal */
-		kill(0, SIGHUP);
-		discard_pending(SIGHUP);
-
+		ap.state = ST_READMAP;
 		break;
 	}
 	errno = save_errno;
+	in_interrupt--;
 }
 
 int supervisor(char *path)
 {
 	unsigned int map = 0;
+	int ret;
 
 	ap.path = alloca(strlen(path) + 1);
 	strcpy(ap.path, path);
@@ -1538,6 +1537,37 @@ int supervisor(char *path)
 
 	setup_signals(sig_supervisor, sig_supervisor);
 

I think we should set state to ST_READY here. That would make things a bit more clear.

(quoted diff) +4 -0
+	while (ap.state != ST_SHUTDOWN) {
+		switch (ap.state) {
+		case ST_READMAP:
+			st_readmap();

No check of return value?

(quoted diff) +27 -0
+			signal_children(SIGHUP);
+			ap.state = ST_READY;
+			break;
+		case ST_SHUTDOWN_PENDING:
+			ret = signal_children(SIGUSR2);
+			if (!ret) {
+				ap.state = ST_SHUTDOWN;
+				break;
+			}
+
+			/* Failed shutdown returns to ready */
+			warn("can't shutdown: filesystem %s still busy",
+				     ap.path);
+			ap.state = ST_READY;
+			break;
+		case ST_PRUNE:
+			/* Pass on the prune event and ignore self signal */
+			signal_children(SIGUSR1);
+			ap.state = ST_READY;
+			break;
+		default:
+			ap.state = ST_READY;
+			break;
+		}
+		sleep(1);
+	}
+
 	while (waitpid(0, NULL, 0) > 0);
 
 	return 0;

Hmm, this is simply not safe, we can lose signals. You really need to either block signals and implement a queue, or use a pipe like we do with indirect maps.

(quoted diff) +1 -2
@@ -1644,8 +1674,23 @@ int handle_mounts(char *path)
 		kill(my_pid, SIGSTOP);
 
 	while (ap.state != ST_SHUTDOWN) {
-		if (handle_packet() && errno != EINTR)
-			break;
+		if (handle_packet() && errno != EINTR) {
 
Quoted 3 lines

The checking of errno here is completely bogus. You can simply get rid of it, I think. (this is what I was referring to above, about the ioctl not always being run).

Yep. EINTR appears to be handled within the routines themselves.

I wonder if it was ever relavent?

Quoted 11 lines
(quoted diff) +9 -0
+			int ret, status = 0;
+
+			ret = ioctl(ap.ioctlfd, AUTOFS_IOC_ASKUMOUNT, &status);
+			/*
+			 * If the ioctl fails assume the kernel doesn't have
+			 * AUTOFS_IOC_ASKUMOUNT and just continue.
+			 */
+			if (!ret && status)
+				break;
Quoted 1 line

Consider handling EINTR for the ioctl call.

EINTR is not listed as a valid error return for this function. So shouldn't we expect this call to resume after a signal is delivered?

Quoted 29 lines
(quoted diff) +5 -0
+
+			/* Failed shutdown returns to ready */
+			warn("can't shutdown: filesystem %s still busy",
+				     ap.path);
+			ap.state = ST_READY;

I think we should be blocking signals when modifying ap.state.

(quoted diff) +2 -0
+			alarm(ap.exp_runfreq);
+		}
 	}
 
 	/* Mop up remaining kids */

Okay, random tangent:

static void sig_statemachine(int sig)

{
	int save_errno = errno;
	enum states next = ap.state;

	in_interrupt++;
	switch (sig) {
	default:		/* all the "can't happen" signals */
		error("process %d got unexpected signal %d!", getpid(), sig);
		break;
		/* don't FALLTHROUGH */
Quoted 9 lines
	...
	}

	debug("sig %d switching from %d to %d", sig, ap.state, next);

	errno = save_errno;
	in_interrupt--;
}

Ian

#12 lore raw
In reply to Jeff Moyer
Quoted 8 lines · Jeff Moyer

Okay, I've reviewed all parts except for the actual logging functions. I'm actually not convinced this is the right approach. The overhead of setting up and tearing down the signal mask is quite expensive. I fear that environments with a great number of mounts will fall over when debugging is turned on, and that is the very thing we are trying to save. Perhaps it is worth another look at a separate logging process.

I agree.

The buffering approach is not the best way to handle this.

I had a look around and found that the BSD folks have a reentrant syslog. I found several posts on lists discussing the use of non-reentrant functions, including syslog, in signal handlers. The responses claimed that their reentrant syslog would work.

I grabed the source from OpenBSD and tweeked it a bit to get it working.
I've used the kernel printf code from the existing patch and checked all the other function calls in glibc. It looks like it may well be signal safe.

I've also tried to rationalise the signal setup and take down a bit. I've also tried to address the things that you picked up previously.

So have a look at the attached patch.

Ian

daemon/automount.c +149 -64
diff -Nur autofs-4.1.4.orig/daemon/automount.c autofs-4.1.4/daemon/automount.c
@@ -5,7 +5,7 @@
  *
  *   Copyright 1997 Transmeta Corporation - All Rights Reserved
  *   Copyright 1999-2000 Jeremy Fitzhardinge <[email protected]>
- *   Copyright 2001-2003 Ian Kent <[email protected]>
+ *   Copyright 2001-2005 Ian Kent <[email protected]>
  *
  *   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
@@ -28,7 +28,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <syslog.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
 #include <sys/types.h>
@@ -38,13 +37,32 @@
 #include <sys/poll.h>
 #include <linux/auto_fs4.h>
 
+#include "automount.h"
+
 #ifndef NDEBUG
-#define assert(x)	do { if (!(x)) { syslog(LOG_CRIT, __FILE__ ":%d: assertion failed: " #x, __LINE__); } } while(0)
+#define assert(x) 						    \
+	do { 							    \
+		if (!(x)) {					    \
+			crit(__FILE__ ":%d: assertion failed: " #x, \
+				__LINE__);			    \
+		}						    \
+	} while(0)
 #else
 #define assert(x)	do { } while(0)
 #endif
 
-#include "automount.h"
+#ifndef NDEBUG
+#define assert_r(context, x) 					   \
+	do { 							   \
+		if (!(x)) {					   \
+			crit_r(context,				   \
+				__FILE__ ":%d: assertion failed: ",\
+				__LINE__);			   \
+		}						   \
+	} while(0)
+#else
+#define assert_r(context, x)	do { } while(0)
+#endif
 
 const char *program;		/* Initialized with argv[0] */
 const char *version = VERSION_STRING;	/* Program version */
@@ -57,7 +75,7 @@
 int kproto_sub_version = 0;	/* Kernel protocol version used */
 
 static int submount = 0;
-
+
 int do_verbose = 0;		/* Verbose feedback option */
 int do_debug = 0;		/* Enable full debug output */
 
@@ -66,6 +84,9 @@
 sigset_t sigchld_mask;
 
 struct autofs_point ap;
+ +/* re-entrant syslog default context data */
+#define AUTOFS_SYSLOG_CONTEXT {-1, 0, 0, LOG_PID, (const char *)0, LOG_DAEMON, 0xff};
 
 volatile struct pending_mount *junk_mounts = NULL;
 
@@ -480,19 +501,25 @@
 
 static void nextstate(enum states next)
 {
+	static struct syslog_data syslog_context = AUTOFS_SYSLOG_CONTEXT;
+	static struct syslog_data *slc = &syslog_context;
+
 	if (write(ap.state_pipe[1], &next, sizeof(next)) != sizeof(next))
-		error("nextstate: write failed %m");
+		error_r(slc, "nextstate: write failed %m");
 }
 
 /* Deal with all the signal-driven events in the state machine */
 static void sig_statemachine(int sig)
 {
+	static struct syslog_data syslog_context = AUTOFS_SYSLOG_CONTEXT;
+	static struct syslog_data *slc = &syslog_context;
 	int save_errno = errno;
 	enum states next = ap.state;
 
 	switch (sig) {
 	default:		/* all the "can't happen" signals */
-		error("process %d got unexpected signal %d!", getpid(), sig);
+		error_r(slc, "process %d got unexpected signal %d!",
+			getpid(), sig);
 		break;
 		/* don't FALLTHROUGH */
 
@@ -503,33 +530,36 @@
 		break;
 
 	case SIGUSR1:
-		assert(ap.state == ST_READY);
+		assert_r(slc, ap.state == ST_READY);
 		nextstate(next = ST_PRUNE);
 		break;
 
 	case SIGALRM:
-		assert(ap.state == ST_READY);
+		assert_r(slc, ap.state == ST_READY);
 		nextstate(next = ST_EXPIRE);
 		break;
 
 	case SIGHUP:
-		assert(ap.state == ST_READY);
+		assert_r(slc, ap.state == ST_READY);
 		nextstate(next = ST_READMAP);
 		break;
 	}
 
-	debug("sig %d switching from %d to %d", sig, ap.state, next);
+	debug_r(slc, "sig %d switching from %d to %d", sig, ap.state, next);
 
 	errno = save_errno;
 }
 
 static int send_ready(unsigned int wait_queue_token)
 {
+	static struct syslog_data syslog_context = AUTOFS_SYSLOG_CONTEXT;
+	static struct syslog_data *slc = &syslog_context;
+
 	if (wait_queue_token == 0)
 		return 0;
-	debug("send_ready: token=%d\n", wait_queue_token);
+	debug_r(slc, "send_ready: token=%d\n", wait_queue_token);
 	if (ioctl(ap.ioctlfd, AUTOFS_IOC_READY, wait_queue_token) < 0) {
-		error("AUTOFS_IOC_READY: %m");
+		error_r(slc, "AUTOFS_IOC_READY: %m");
 		return 1;
 	}
 	return 0;
@@ -537,11 +567,14 @@
 
 static int send_fail(unsigned int wait_queue_token)
 {
+	static struct syslog_data syslog_context = AUTOFS_SYSLOG_CONTEXT;
+	static struct syslog_data *slc = &syslog_context;
+
 	if (wait_queue_token == 0)
 		return 0;
-	debug("send_fail: token=%d\n", wait_queue_token);
+	debug_r(slc, "send_fail: token=%d\n", wait_queue_token);
 	if (ioctl(ap.ioctlfd, AUTOFS_IOC_FAIL, wait_queue_token) < 0) {
-		syslog(LOG_ERR, "AUTOFS_IOC_FAIL: %m");
+		error_r(slc, "AUTOFS_IOC_FAIL: %m");
 		return 1;
 	}
 	return 0;
@@ -552,6 +585,8 @@
    result.  */
 static enum states handle_child(int hang)
 {
+	static struct syslog_data syslog_context = AUTOFS_SYSLOG_CONTEXT;
+	static struct syslog_data *slc = &syslog_context;
 	pid_t pid;
 	int status;
 	enum states next = ST_INVAL;
@@ -559,13 +594,13 @@
 	while ((pid = waitpid(-1, &status, hang ? 0 : WNOHANG)) > 0) {
 		struct pending_mount volatile *mt, *volatile *mtp;
 
-		debug("handle_child: got pid %d, sig %d (%d), stat %d\n",
+		debug_r(slc, "handle_child: got pid %d, sig %d (%d), stat %d",
 			pid, WIFSIGNALED(status),
 			WTERMSIG(status), WEXITSTATUS(status));
 
 		/* Check to see if expire process finished */
 		if (pid == ap.exp_process) {
-			int success, ret;
+			int success;
 
 			if (!WIFEXITED(status))
 				continue;
@@ -594,29 +629,23 @@
 
 			case ST_SHUTDOWN_PENDING:
 				next = ST_SHUTDOWN;
-				if (success) {
-					ret = ioctl(ap.ioctlfd,
-						AUTOFS_IOC_ASKUMOUNT, &status);
-					if (!ret) {
-						if (status)
-							break;
-					} else
-						break;
-				}
+				if (success)
+					break;
 
 				/* Failed shutdown returns to ready */
-				warn("can't shutdown: filesystem %s still busy",
-				     ap.path);
+				warn_r(slc,
+			           "can't shutdown: filesystem %s still busy",
+				   ap.path);
 				alarm(ap.exp_runfreq);
 				next = ST_READY;
 				break;
 
 			default:
-				error("bad state %d", ap.state);
+				error_r(slc, "bad state %d", ap.state);
 			}
 
 			if (next != ST_INVAL)
-				debug("sigchld: exp "
+				debug_r(slc, "sigchld: exp "
 				     "%d finished, switching from %d to %d",
 				     pid, ap.state, next);
 
@@ -632,7 +661,7 @@
 			if (!WIFEXITED(status) && !WIFSIGNALED(status))
 				break;
 
-			debug("sig_child: found pending iop pid %d: "
+			debug_r(slc, "sig_child: found pending iop pid %d: "
 			     "signalled %d (sig %d), exit status %d",
 				pid, WIFSIGNALED(status),
 				WTERMSIG(status), WEXITSTATUS(status));
@@ -673,10 +702,9 @@
 
 static int st_ready(void)
 {
-	debug("st_ready(): state = %d\n", ap.state);
+	debug("st_ready(): state = %d", ap.state);
 
 	ap.state = ST_READY;
-	sigprocmask(SIG_UNBLOCK, &lock_sigs, NULL);
 
 	return 0;
 }
@@ -796,6 +824,7 @@
 	default:
 		debug("expire_proc: exp_proc=%d", f);
 		ap.exp_process = f;
+		sigprocmask(SIG_SETMASK, &old, NULL);
 		return EXP_STARTED;
 	}
 }
@@ -804,10 +833,15 @@
 {
 	int status;
 
+	assert(ap.state == ST_READY);
+	ap.state = ST_READMAP;
+
 	status = ap.lookup->lookup_ghost(ap.path, ap.ghost, 0, ap.lookup->context);
 
 	debug("st_readmap: status %d\n", status);
 
+	ap.state = ST_READY;
+
 	/* If I don't exist in the map any more then exit */
 	if (status == LKP_FAIL)
 		return 0;
@@ -822,15 +856,11 @@
 	info("prep_shutdown: state = %d\n", ap.state);
 
 	assert(ap.state == ST_READY || ap.state == ST_EXPIRE);
+	ap.state = ST_SHUTDOWN_PENDING;
 
 	/* Turn off timeouts */
 	alarm(0);
 
-	/* Prevent any new mounts */
-	sigprocmask(SIG_SETMASK, &lock_sigs, NULL);
-
-	ap.state = ST_SHUTDOWN_PENDING;
-
 	/* Where're the boss, tell everyone to finish up */
 	if (getpid() == getpgrp())
 		signal_children(SIGUSR2);
@@ -844,8 +874,9 @@
 	case EXP_ERROR:
 	case EXP_PARTIAL:
 		/* It didn't work: return to ready */
+		ap.state = ST_READY;
 		alarm(ap.exp_runfreq);
-		return st_ready();
+		return 0;
 
 	case EXP_DONE:
 		/* All expired: go straight to exit */
@@ -853,8 +884,6 @@
 		return 1;
 
 	case EXP_STARTED:
-		/* Wait until expiry process finishes */
-		sigprocmask(SIG_SETMASK, &ready_sigs, NULL);
 		return 0;
 	}
 	return 1;
@@ -865,6 +894,7 @@
 	debug("st_prune(): state = %d\n", ap.state);
 
 	assert(ap.state == ST_READY);
+	ap.state = ST_PRUNE;
 
 	/* We're the boss, pass on the prune event */
 	if (getpid() == getpgrp()) 
@@ -878,11 +908,10 @@
 
 	case EXP_ERROR:
 	case EXP_PARTIAL:
+		ap.state = ST_READY;
 		return 1;
 
 	case EXP_STARTED:
-		ap.state = ST_PRUNE;
-		sigprocmask(SIG_SETMASK, &ready_sigs, NULL);
 		return 0;
 	}
 	return 1;
@@ -893,6 +922,7 @@
 	debug("st_expire(): state = %d\n", ap.state);
 
 	assert(ap.state == ST_READY);
+	ap.state = ST_EXPIRE;
 
 	switch (expire_proc(0)) {
 	case EXP_DONE:
@@ -902,12 +932,11 @@
 
 	case EXP_ERROR:
 	case EXP_PARTIAL:
+		ap.state = ST_READY;
 		alarm(ap.exp_runfreq);
 		return 1;
 
 	case EXP_STARTED:
-		ap.state = ST_EXPIRE;
-		sigprocmask(SIG_SETMASK, &ready_sigs, NULL);
 		return 0;
 	}
 	return 1;
@@ -947,7 +976,7 @@
 		if (poll(fds, 2, -1) == -1) {
 			if (errno == EINTR)
 				continue;
-			syslog(LOG_ERR, "get_pkt: poll failed: %m");
+			error("get_pkt: poll failed: %m");
 			return -1;
 		}
 
@@ -958,8 +987,7 @@
 			if (fullread(ap.state_pipe[0], &next_state, sizeof(next_state)))
 				continue;
 
-			sigprocmask(SIG_BLOCK, &lock_sigs, &old);
-
+			sigprocmask(SIG_BLOCK, &ready_sigs, &old);
 			if (next_state != ap.state) {
 				debug("get_pkt: state %d, next %d",
 					ap.state, next_state);
@@ -999,9 +1027,7 @@
 					      next_state);
 				}
 			}
-
-			if (ret)
-				sigprocmask(SIG_SETMASK, &old, NULL);
+			sigprocmask(SIG_SETMASK, &old, NULL);
 
 			if (ap.state == ST_SHUTDOWN)
 				return -1;
@@ -1484,24 +1510,25 @@
 
 /* Deal with the signals recieved by direct mount supervisor */
 static void sig_supervisor(int sig)
-{
+{ +	static struct syslog_data syslog_context = AUTOFS_SYSLOG_CONTEXT;
+	static struct syslog_data *slc = &syslog_context;
 	int save_errno = errno;
 
 	switch (sig) {
 	default:		/* all the signals not handled */
-		error("process %d got unexpected signal %d!", getpid(), sig);
+		error_r(slc, "process %d got unexpected signal %d!",
+			getpid(), sig);
 		return;
 		/* don't FALLTHROUGH */
 
 	case SIGTERM:
 	case SIGUSR2:
-		/* Tell everyone to finish up */
-		signal_children(sig);
+		ap.state = ST_SHUTDOWN_PENDING;
 		break;
 
 	case SIGUSR1:
-		/* Pass on the prune event and ignore self signal */
-		signal_children(sig);
+		ap.state = ST_PRUNE;
 		break;
 
 	case SIGCHLD:
@@ -1509,12 +1536,8 @@
 		break;
 
 	case SIGHUP:
-		ap.lookup->lookup_ghost(ap.path, ap.ghost, 0, ap.lookup->context);
-
 		/* Pass on the reread event and ignore self signal */
-		kill(0, SIGHUP);
-		discard_pending(SIGHUP);
-
+		ap.state = ST_READMAP;
 		break;
 	}
 	errno = save_errno;
@@ -1522,7 +1545,9 @@
 
 int supervisor(char *path)
 {
+	sigset_t olds;
 	unsigned int map = 0;
+	int ret;
 
 	ap.path = alloca(strlen(path) + 1);
 	strcpy(ap.path, path);
@@ -1536,8 +1561,46 @@
 		cleanup_exit(ap.path, 1);
 	}
 
+	ap.state = ST_READY;
 	setup_signals(sig_supervisor, sig_supervisor);
 
+	sigprocmask(SIG_BLOCK, &ready_sigs, &olds);
+	while (ap.state != ST_SHUTDOWN) {
+		switch (ap.state) {
+		case ST_READMAP:
+			ret = st_readmap();
+			if (!ret)
+				/* Warn but try to continue anyway */
+				warn("failed to read map");
+			signal_children(SIGHUP);
+			ap.state = ST_READY;
+			break;
+		case ST_SHUTDOWN_PENDING:
+			ret = signal_children(SIGUSR2);
+			if (!ret) {
+				ap.state = ST_SHUTDOWN;
+				sigprocmask(SIG_SETMASK, &olds, NULL);
+				continue;
+			}
+
+			/* Failed shutdown returns to ready */
+			warn("can't shutdown: filesystem %s still busy",
+				     ap.path);
+			ap.state = ST_READY;
+			break;
+		case ST_PRUNE:
+			/* Pass on the prune event and ignore self signal */
+			signal_children(SIGUSR1);
+			ap.state = ST_READY;
+			break;
+		default:
+			ap.state = ST_READY;
+			break;
+		}
+		sigsuspend(&olds);
+	}
+	sigprocmask(SIG_UNBLOCK, &ready_sigs, NULL);
+
 	while (waitpid(0, NULL, 0) > 0);
 
 	return 0;
@@ -1644,8 +1707,32 @@
 		kill(my_pid, SIGSTOP);
 
 	while (ap.state != ST_SHUTDOWN) {
-		if (handle_packet() && errno != EINTR)
-			break;
+		if (handle_packet()) {
+			sigset_t olds;
+			int ret, status = 0;
+
+			sigprocmask(SIG_BLOCK, &lock_sigs, &olds);
+			ret = ioctl(ap.ioctlfd, AUTOFS_IOC_ASKUMOUNT, &status);
+			/*
+			 * If the ioctl fails assume the kernel doesn't have
+			 * AUTOFS_IOC_ASKUMOUNT and just continue.
+			 */
+			if (ret) {
+				sigprocmask(SIG_SETMASK, &olds, NULL);
+				break;
+			}
+			if (status) {
+				sigprocmask(SIG_SETMASK, &olds, NULL);
+				break;
+			}
+
+			/* Failed shutdown returns to ready */
+			warn("can't shutdown: filesystem %s still busy",
+					ap.path);
+			alarm(ap.exp_runfreq);
+			ap.state = ST_READY;
+			sigprocmask(SIG_SETMASK, &olds, NULL);
+		}
 	}
 
 	/* Mop up remaining kids */
daemon/module.c +0 -1
diff -Nur autofs-4.1.4.orig/daemon/module.c autofs-4.1.4/daemon/module.c
@@ -14,7 +14,6 @@
  * ----------------------------------------------------------------------- */
 
 #include <stdio.h>
-#include <syslog.h>
 #include <dlfcn.h>
 #include <string.h>
 #include <stdlib.h>
daemon/mount.c +0 -1
diff -Nur autofs-4.1.4.orig/daemon/mount.c autofs-4.1.4/daemon/mount.c
@@ -19,7 +19,6 @@
  *
  * ----------------------------------------------------------------------- */
 
-#include <syslog.h>
 #include <stdlib.h>
 #include <string.h>
 #include "automount.h"
daemon/spawn.c +0 -1
diff -Nur autofs-4.1.4.orig/daemon/spawn.c autofs-4.1.4/daemon/spawn.c
@@ -21,7 +21,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <syslog.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <time.h>
include/automount.h +73 -16

103 lines not on this page · raw message

include/syslog.h +201 -0

201 lines not on this page · raw message

lib/Makefile +4 -2
diff -Nur autofs-4.1.4.orig/lib/Makefile autofs-4.1.4/lib/Makefile
@@ -9,10 +9,12 @@
 RPCGEN = /usr/bin/rpcgen
 RANLIB = /usr/bin/ranlib
 
-SRCS = cache.c listmount.c cat_path.c rpc_subs.c mounts.c lock.c
+SRCS = cache.c listmount.c cat_path.c rpc_subs.c mounts.c lock.c \
+	syslog.c vsprintf.c
 RPCS = mount.h mount_clnt.c mount_xdr.c
 OBJS = cache.o mount_clnt.o mount_xdr.o listmount.o \
-	cat_path.o rpc_subs.o mounts.o lock.o
+	cat_path.o rpc_subs.o mounts.o lock.o \
+	syslog.o vsprintf.o
 
 LIB = autofs.a
 
lib/cache.c +0 -1
diff -Nur autofs-4.1.4.orig/lib/cache.c autofs-4.1.4/lib/cache.c
@@ -21,7 +21,6 @@
 #include <string.h>
 #include <unistd.h>
 #include <ctype.h>
-#include <syslog.h>
 #include <stdio.h>
 #include <fcntl.h>
 #include <sys/param.h>
lib/cat_path.c +0 -1
diff -Nur autofs-4.1.4.orig/lib/cat_path.c autofs-4.1.4/lib/cat_path.c
@@ -16,7 +16,6 @@
 #include <alloca.h>
 #include <string.h>
 #include <limits.h>
-#include <syslog.h>
 #include <ctype.h>
 
 /*
lib/mounts.c +0 -1
diff -Nur autofs-4.1.4.orig/lib/mounts.c autofs-4.1.4/lib/mounts.c
@@ -16,7 +16,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include <syslog.h>
 #include <mntent.h>
 #include <limits.h>
 #include <sys/types.h>
lib/syslog.c +378 -0

378 lines not on this page · raw message

lib/vsprintf.c +617 -0

617 lines not on this page · raw message

#13 lore raw
Quoted 28 lines · On Sun, 24 Apr 2005 [email protected] wrote:
Quoted 12 lines · Jeff Moyer

Okay, I've reviewed all parts except for the actual logging functions. I'm
actually not convinced this is the right approach. The overhead of setting
up and tearing down the signal mask is quite expensive. I fear that environments with a great number of mounts will fall over when debugging is
turned on, and that is the very thing we are trying to save. Perhaps it is
worth another look at a separate logging process.

I agree.

The buffering approach is not the best way to handle this.

I had a look around and found that the BSD folks have a reentrant syslog. I found several posts on lists discussing the use of non-reentrant functions, including syslog, in signal handlers. The responses claimed that their reentrant syslog would work.

I grabed the source from OpenBSD and tweeked it a bit to get it working. I've used the kernel printf code from the existing patch and checked all the other function calls in glibc. It looks like it may well be signal safe.

Quoted 6 lines

I've also tried to rationalise the signal setup and take down a bit. I've also tried to address the things that you picked up previously.

So have a look at the attached patch.

Ian

Sorry, did it again.
The patch is contaminated with white space.
Try this one instead please.

daemon/automount.c +151 -64
diff -Nur autofs-4.1.4.orig/daemon/automount.c autofs-4.1.4/daemon/automount.c
@@ -5,7 +5,7 @@
   *
   *   Copyright 1997 Transmeta Corporation - All Rights Reserved
   *   Copyright 1999-2000 Jeremy Fitzhardinge <[email protected]>
- *   Copyright 2001-2003 Ian Kent <[email protected]>
+ *   Copyright 2001-2005 Ian Kent <[email protected]>
   *
   *   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
@@ -28,7 +28,6 @@
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
-#include <syslog.h>
  #include <unistd.h>
  #include <sys/ioctl.h>
  #include <sys/types.h>
@@ -38,13 +37,32 @@
  #include <sys/poll.h>
  #include <linux/auto_fs4.h>
 
+#include "automount.h"
+
  #ifndef NDEBUG
-#define assert(x)	do { if (!(x)) { syslog(LOG_CRIT, __FILE__ ":%d: assertion failed: " #x, __LINE__); } } while(0)
+#define assert(x) 						    \
+	do { 							    \
+		if (!(x)) {					    \
+			crit(__FILE__ ":%d: assertion failed: " #x, \
+				__LINE__);			    \
+		}						    \
+	} while(0)
  #else
  #define assert(x)	do { } while(0)
  #endif
 
-#include "automount.h"
+#ifndef NDEBUG
+#define assert_r(context, x) 					   \
+	do { 							   \
+		if (!(x)) {					   \
+			crit_r(context,				   \
+				__FILE__ ":%d: assertion failed: ",\
+				__LINE__);			   \
+		}						   \
+	} while(0)
+#else
+#define assert_r(context, x)	do { } while(0)
+#endif
 
  const char *program;		/* Initialized with argv[0] */
  const char *version = VERSION_STRING;	/* Program version */
@@ -57,7 +75,7 @@
  int kproto_sub_version = 0;	/* Kernel protocol version used */
 
  static int submount = 0;
-
+
  int do_verbose = 0;		/* Verbose feedback option */
  int do_debug = 0;		/* Enable full debug output */
 
@@ -66,6 +84,9 @@
  sigset_t sigchld_mask;
 
  struct autofs_point ap;
+ 
+/* re-entrant syslog default context data */
+#define AUTOFS_SYSLOG_CONTEXT {-1, 0, 0, LOG_PID, (const char *)0, LOG_DAEMON, 0xff};
 
  volatile struct pending_mount *junk_mounts = NULL;
 
@@ -480,19 +501,25 @@
 
  static void nextstate(enum states next)
  {
+	static struct syslog_data syslog_context = AUTOFS_SYSLOG_CONTEXT;
+	static struct syslog_data *slc = &syslog_context;
+
  	if (write(ap.state_pipe[1], &next, sizeof(next)) != sizeof(next))
-		error("nextstate: write failed %m");
+		error_r(slc, "nextstate: write failed %m");
  }
 
  /* Deal with all the signal-driven events in the state machine */
  static void sig_statemachine(int sig)
  {
+	static struct syslog_data syslog_context = AUTOFS_SYSLOG_CONTEXT;
+	static struct syslog_data *slc = &syslog_context;
  	int save_errno = errno;
  	enum states next = ap.state;
 
  	switch (sig) {
  	default:		/* all the "can't happen" signals */
-		error("process %d got unexpected signal %d!", getpid(), sig);
+		error_r(slc, "process %d got unexpected signal %d!",
+			getpid(), sig);
  		break;
  		/* don't FALLTHROUGH */
 
@@ -503,33 +530,36 @@
  		break;
 
  	case SIGUSR1:
-		assert(ap.state == ST_READY);
+		assert_r(slc, ap.state == ST_READY);
  		nextstate(next = ST_PRUNE);
  		break;
 
  	case SIGALRM:
-		assert(ap.state == ST_READY);
+		assert_r(slc, ap.state == ST_READY);
  		nextstate(next = ST_EXPIRE);
  		break;
 
  	case SIGHUP:
-		assert(ap.state == ST_READY);
+		assert_r(slc, ap.state == ST_READY);
  		nextstate(next = ST_READMAP);
  		break;
  	}
 
-	debug("sig %d switching from %d to %d", sig, ap.state, next);
+	debug_r(slc, "sig %d switching from %d to %d", sig, ap.state, next);
 
  	errno = save_errno;
  }
 
  static int send_ready(unsigned int wait_queue_token)
  {
+	static struct syslog_data syslog_context = AUTOFS_SYSLOG_CONTEXT;
+	static struct syslog_data *slc = &syslog_context;
+
  	if (wait_queue_token == 0)
  		return 0;
-	debug("send_ready: token=%d\n", wait_queue_token);
+	debug_r(slc, "send_ready: token=%d\n", wait_queue_token);
  	if (ioctl(ap.ioctlfd, AUTOFS_IOC_READY, wait_queue_token) < 0) {
-		error("AUTOFS_IOC_READY: %m");
+		error_r(slc, "AUTOFS_IOC_READY: %m");
  		return 1;
  	}
  	return 0;
@@ -537,11 +567,14 @@
 
  static int send_fail(unsigned int wait_queue_token)
  {
+	static struct syslog_data syslog_context = AUTOFS_SYSLOG_CONTEXT;
+	static struct syslog_data *slc = &syslog_context;
+
  	if (wait_queue_token == 0)
  		return 0;
-	debug("send_fail: token=%d\n", wait_queue_token);
+	debug_r(slc, "send_fail: token=%d\n", wait_queue_token);
  	if (ioctl(ap.ioctlfd, AUTOFS_IOC_FAIL, wait_queue_token) < 0) {
-		syslog(LOG_ERR, "AUTOFS_IOC_FAIL: %m");
+		error_r(slc, "AUTOFS_IOC_FAIL: %m");
  		return 1;
  	}
  	return 0;
@@ -552,6 +585,8 @@
     result.  */
  static enum states handle_child(int hang)
  {
+	static struct syslog_data syslog_context = AUTOFS_SYSLOG_CONTEXT;
+	static struct syslog_data *slc = &syslog_context;
  	pid_t pid;
  	int status;
  	enum states next = ST_INVAL;
@@ -559,13 +594,13 @@
  	while ((pid = waitpid(-1, &status, hang ? 0 : WNOHANG)) > 0) {
  		struct pending_mount volatile *mt, *volatile *mtp;
 
-		debug("handle_child: got pid %d, sig %d (%d), stat %d\n",
+		debug_r(slc, "handle_child: got pid %d, sig %d (%d), stat %d",
  			pid, WIFSIGNALED(status),
  			WTERMSIG(status), WEXITSTATUS(status));
 
  		/* Check to see if expire process finished */
  		if (pid == ap.exp_process) {
-			int success, ret;
+			int success;
 
  			if (!WIFEXITED(status))
  				continue;
@@ -594,29 +629,23 @@
 
  			case ST_SHUTDOWN_PENDING:
  				next = ST_SHUTDOWN;
-				if (success) {
-					ret = ioctl(ap.ioctlfd,
-						AUTOFS_IOC_ASKUMOUNT, &status);
-					if (!ret) {
-						if (status)
-							break;
-					} else
-						break;
-				}
+				if (success)
+					break;
 
  				/* Failed shutdown returns to ready */
-				warn("can't shutdown: filesystem %s still busy",
-				     ap.path);
+				warn_r(slc,
+			           "can't shutdown: filesystem %s still busy",
+				   ap.path);
  				alarm(ap.exp_runfreq);
  				next = ST_READY;
  				break;
 
  			default:
-				error("bad state %d", ap.state);
+				error_r(slc, "bad state %d", ap.state);
  			}
 
  			if (next != ST_INVAL)
-				debug("sigchld: exp "
+				debug_r(slc, "sigchld: exp "
  				     "%d finished, switching from %d to %d",
  				     pid, ap.state, next);
 
@@ -632,7 +661,7 @@
  			if (!WIFEXITED(status) && !WIFSIGNALED(status))
  				break;
 
-			debug("sig_child: found pending iop pid %d: "
+			debug_r(slc, "sig_child: found pending iop pid %d: "
  			     "signalled %d (sig %d), exit status %d",
  				pid, WIFSIGNALED(status),
  				WTERMSIG(status), WEXITSTATUS(status));
@@ -673,10 +702,9 @@
 
  static int st_ready(void)
  {
-	debug("st_ready(): state = %d\n", ap.state);
+	debug("st_ready(): state = %d", ap.state);
 
  	ap.state = ST_READY;
-	sigprocmask(SIG_UNBLOCK, &lock_sigs, NULL);
 
  	return 0;
  }
@@ -796,6 +824,7 @@
  	default:
  		debug("expire_proc: exp_proc=%d", f);
  		ap.exp_process = f;
+		sigprocmask(SIG_SETMASK, &old, NULL);
  		return EXP_STARTED;
  	}
  }
@@ -804,10 +833,15 @@
  {
  	int status;
 
+	assert(ap.state == ST_READY);
+	ap.state = ST_READMAP;
+
  	status = ap.lookup->lookup_ghost(ap.path, ap.ghost, 0, ap.lookup->context);
 
  	debug("st_readmap: status %d\n", status);
 
+	ap.state = ST_READY;
+
  	/* If I don't exist in the map any more then exit */
  	if (status == LKP_FAIL)
  		return 0;
@@ -822,15 +856,11 @@
  	info("prep_shutdown: state = %d\n", ap.state);
 
  	assert(ap.state == ST_READY || ap.state == ST_EXPIRE);
+	ap.state = ST_SHUTDOWN_PENDING;
 
  	/* Turn off timeouts */
  	alarm(0);
 
-	/* Prevent any new mounts */
-	sigprocmask(SIG_SETMASK, &lock_sigs, NULL);
-
-	ap.state = ST_SHUTDOWN_PENDING;
-
  	/* Where're the boss, tell everyone to finish up */
  	if (getpid() == getpgrp())
  		signal_children(SIGUSR2);
@@ -844,8 +874,9 @@
  	case EXP_ERROR:
  	case EXP_PARTIAL:
  		/* It didn't work: return to ready */
+		ap.state = ST_READY;
  		alarm(ap.exp_runfreq);
-		return st_ready();
+		return 0;
 
  	case EXP_DONE:
  		/* All expired: go straight to exit */
@@ -853,8 +884,6 @@
  		return 1;
 
  	case EXP_STARTED:
-		/* Wait until expiry process finishes */
-		sigprocmask(SIG_SETMASK, &ready_sigs, NULL);
  		return 0;
  	}
  	return 1;
@@ -865,6 +894,7 @@
  	debug("st_prune(): state = %d\n", ap.state);
 
  	assert(ap.state == ST_READY);
+	ap.state = ST_PRUNE;
 
  	/* We're the boss, pass on the prune event */
  	if (getpid() == getpgrp()) 
@@ -878,11 +908,10 @@
 
  	case EXP_ERROR:
  	case EXP_PARTIAL:
+		ap.state = ST_READY;
  		return 1;
 
  	case EXP_STARTED:
-		ap.state = ST_PRUNE;
-		sigprocmask(SIG_SETMASK, &ready_sigs, NULL);
  		return 0;
  	}
  	return 1;
@@ -893,6 +922,7 @@
  	debug("st_expire(): state = %d\n", ap.state);
 
  	assert(ap.state == ST_READY);
+	ap.state = ST_EXPIRE;
 
  	switch (expire_proc(0)) {
  	case EXP_DONE:
@@ -902,12 +932,11 @@
 
  	case EXP_ERROR:
  	case EXP_PARTIAL:
+		ap.state = ST_READY;
  		alarm(ap.exp_runfreq);
  		return 1;
 
  	case EXP_STARTED:
-		ap.state = ST_EXPIRE;
-		sigprocmask(SIG_SETMASK, &ready_sigs, NULL);
  		return 0;
  	}
  	return 1;
@@ -947,7 +976,7 @@
  		if (poll(fds, 2, -1) == -1) {
  			if (errno == EINTR)
  				continue;
-			syslog(LOG_ERR, "get_pkt: poll failed: %m");
+			error("get_pkt: poll failed: %m");
  			return -1;
  		}
 
@@ -958,8 +987,7 @@
  			if (fullread(ap.state_pipe[0], &next_state, sizeof(next_state)))
  				continue;
 
-			sigprocmask(SIG_BLOCK, &lock_sigs, &old);
-
+			sigprocmask(SIG_BLOCK, &ready_sigs, &old);
  			if (next_state != ap.state) {
  				debug("get_pkt: state %d, next %d",
  					ap.state, next_state);
@@ -999,9 +1027,7 @@
  					      next_state);
  				}
  			}
-
-			if (ret)
-				sigprocmask(SIG_SETMASK, &old, NULL);
+			sigprocmask(SIG_SETMASK, &old, NULL);
 
  			if (ap.state == ST_SHUTDOWN)
  				return -1;
@@ -1484,24 +1510,25 @@
 
  /* Deal with the signals recieved by direct mount supervisor */
  static void sig_supervisor(int sig)
-{
+{ 
+	static struct syslog_data syslog_context = AUTOFS_SYSLOG_CONTEXT;
+	static struct syslog_data *slc = &syslog_context;
  	int save_errno = errno;
 
  	switch (sig) {
  	default:		/* all the signals not handled */
-		error("process %d got unexpected signal %d!", getpid(), sig);
+		error_r(slc, "process %d got unexpected signal %d!",
+			getpid(), sig);
  		return;
  		/* don't FALLTHROUGH */
 
  	case SIGTERM:
  	case SIGUSR2:
-		/* Tell everyone to finish up */
-		signal_children(sig);
+		ap.state = ST_SHUTDOWN_PENDING;
  		break;
 
  	case SIGUSR1:
-		/* Pass on the prune event and ignore self signal */
-		signal_children(sig);
+		ap.state = ST_PRUNE;
  		break;
 
  	case SIGCHLD:
@@ -1509,12 +1536,8 @@
  		break;
 
  	case SIGHUP:
-		ap.lookup->lookup_ghost(ap.path, ap.ghost, 0, ap.lookup->context);
-
  		/* Pass on the reread event and ignore self signal */
-		kill(0, SIGHUP);
-		discard_pending(SIGHUP);
-
+		ap.state = ST_READMAP;
  		break;
  	}
  	errno = save_errno;
@@ -1522,7 +1545,9 @@
 
  int supervisor(char *path)
  {
+	sigset_t olds;
  	unsigned int map = 0;
+	int ret;
 
  	ap.path = alloca(strlen(path) + 1);
  	strcpy(ap.path, path);
@@ -1536,8 +1561,46 @@
  		cleanup_exit(ap.path, 1);
  	}
 
+	ap.state = ST_READY;
  	setup_signals(sig_supervisor, sig_supervisor);
 
+	sigprocmask(SIG_BLOCK, &ready_sigs, &olds);
+	while (ap.state != ST_SHUTDOWN) {
+		switch (ap.state) {
+		case ST_READMAP:
+			ret = st_readmap();
+			if (!ret)
+				/* Warn but try to continue anyway */
+				warn("failed to read map");
+			signal_children(SIGHUP);
+			ap.state = ST_READY;
+			break;
+		case ST_SHUTDOWN_PENDING:
+			ret = signal_children(SIGUSR2);
+			if (!ret) {
+				ap.state = ST_SHUTDOWN;
+				sigprocmask(SIG_SETMASK, &olds, NULL);
+				continue;
+			}
+
+			/* Failed shutdown returns to ready */
+			warn("can't shutdown: filesystem %s still busy",
+				     ap.path);
+			ap.state = ST_READY;
+			break;
+		case ST_PRUNE:
+			/* Pass on the prune event and ignore self signal */
+			signal_children(SIGUSR1);
+			ap.state = ST_READY;
+			break;
+		default:
+			ap.state = ST_READY;
+			break;
+		}
+		sigsuspend(&olds);
+	}
+	sigprocmask(SIG_UNBLOCK, &ready_sigs, NULL);
+
  	while (waitpid(0, NULL, 0) > 0);
 
  	return 0;
@@ -1644,8 +1707,32 @@
  		kill(my_pid, SIGSTOP);
 
  	while (ap.state != ST_SHUTDOWN) {
-		if (handle_packet() && errno != EINTR)
-			break;
+		if (handle_packet()) {
+			sigset_t olds;
+			int ret, status = 0;
+
+			sigprocmask(SIG_BLOCK, &lock_sigs, &olds);
+			ret = ioctl(ap.ioctlfd, AUTOFS_IOC_ASKUMOUNT, &status);
+			/*
+			 * If the ioctl fails assume the kernel doesn't have
+			 * AUTOFS_IOC_ASKUMOUNT and just continue.
+			 */
+			if (ret) {
+				sigprocmask(SIG_SETMASK, &olds, NULL);
+				break;
+			}
+			if (status) {
+				sigprocmask(SIG_SETMASK, &olds, NULL);
+				break;
+			}
+
+			/* Failed shutdown returns to ready */
+			warn("can't shutdown: filesystem %s still busy",
+					ap.path);
+			alarm(ap.exp_runfreq);
+			ap.state = ST_READY;
+			sigprocmask(SIG_SETMASK, &olds, NULL);
+		}
  	}
 
  	/* Mop up remaining kids */
daemon/module.c +0 -1
diff -Nur autofs-4.1.4.orig/daemon/module.c autofs-4.1.4/daemon/module.c
@@ -14,7 +14,6 @@
   * ----------------------------------------------------------------------- */
 
  #include <stdio.h>
-#include <syslog.h>
  #include <dlfcn.h>
  #include <string.h>
  #include <stdlib.h>
daemon/mount.c +0 -1
diff -Nur autofs-4.1.4.orig/daemon/mount.c autofs-4.1.4/daemon/mount.c
@@ -19,7 +19,6 @@
   *
   * ----------------------------------------------------------------------- */
 
-#include <syslog.h>
  #include <stdlib.h>
  #include <string.h>
  #include "automount.h"
daemon/spawn.c +0 -1
diff -Nur autofs-4.1.4.orig/daemon/spawn.c autofs-4.1.4/daemon/spawn.c
@@ -21,7 +21,6 @@
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
-#include <syslog.h>
  #include <sys/types.h>
  #include <unistd.h>
  #include <time.h>
include/automount.h +73 -16

103 lines not on this page · raw message

include/syslog.h +201 -0

201 lines not on this page · raw message

lib/Makefile +4 -2
diff -Nur autofs-4.1.4.orig/lib/Makefile autofs-4.1.4/lib/Makefile
@@ -9,10 +9,12 @@
  RPCGEN = /usr/bin/rpcgen
  RANLIB = /usr/bin/ranlib
 
-SRCS = cache.c listmount.c cat_path.c rpc_subs.c mounts.c lock.c
+SRCS = cache.c listmount.c cat_path.c rpc_subs.c mounts.c lock.c \
+	syslog.c vsprintf.c
  RPCS = mount.h mount_clnt.c mount_xdr.c
  OBJS = cache.o mount_clnt.o mount_xdr.o listmount.o \
-	cat_path.o rpc_subs.o mounts.o lock.o
+	cat_path.o rpc_subs.o mounts.o lock.o \
+	syslog.o vsprintf.o
 
  LIB = autofs.a
 
lib/cache.c +0 -1
diff -Nur autofs-4.1.4.orig/lib/cache.c autofs-4.1.4/lib/cache.c
@@ -21,7 +21,6 @@
  #include <string.h>
  #include <unistd.h>
  #include <ctype.h>
-#include <syslog.h>
  #include <stdio.h>
  #include <fcntl.h>
  #include <sys/param.h>
lib/cat_path.c +0 -1
diff -Nur autofs-4.1.4.orig/lib/cat_path.c autofs-4.1.4/lib/cat_path.c
@@ -16,7 +16,6 @@
  #include <alloca.h>
  #include <string.h>
  #include <limits.h>
-#include <syslog.h>
  #include <ctype.h>
 
  /*
lib/mounts.c +0 -1
diff -Nur autofs-4.1.4.orig/lib/mounts.c autofs-4.1.4/lib/mounts.c
@@ -16,7 +16,6 @@
  #include <stdlib.h>
  #include <string.h>
  #include <unistd.h>
-#include <syslog.h>
  #include <mntent.h>
  #include <limits.h>
  #include <sys/types.h>
lib/syslog.c +383 -0

383 lines not on this page · raw message

lib/vsprintf.c +619 -0

619 lines not on this page · raw message

author
#14 lore raw
In reply to [email protected]

==> Regarding Re: [patch, rfc] fix hung automounter issues; [email protected] adds:

Quoted 32 lines · Jeff Moyer
Quoted 32 lines

Hi, Ian,

Dan Berrange did a spectacular job of diagnosing some autofs hangs he was experiencing. The automount daemon would appear hung, and an an strace of the process would show sys_futex on the call stack. Through his debugging, he figured out that the problem was autofs issuing syslog calls while in a signal handler.

So, there are a few ways to fix this that come to mind.

o We could not log in signal handlers. I don't consider this an acceptable solution, as we really need the debug messages generated there.
o We could defer logging to non-signal handler context. This is in fact what the attached pattch does. It queues the syslog messages, and flushes them upon the next syslog that occurs outside of signal handler context.
o We could open /dev/log directly. This is likely a bad idea as there is no standard for the interface there.
o We could have a separate logging process, which we write to via a pipe. I'm not keen on this as it adds yet another process, and makes shutdown that much more complicated.

Note that in all of the above cases, we still need to implement a signal-safe vsprintf. That is what the bulk of this patch is.

So, here is a rough take on implementing the second bullet point. I wholesale copied a bunch of code from the linux kernel for doing vsprintf. That bit is ugly. I'd also move the definition of the new logging routines into the vsprintf file, and rename it. In short, this is a proof of concept (shown to resolve the issues). I'm happy to clean it up, but I want to be sure that this is the direction we want to go in, first.

Quoted 7 lines
Quoted 7 lines

Limitations of this approach: we won't flush the logs that were issued in signal handler context until another syslog call is made. One improvement that could be made straight-away is to have all of the logging routines call flush_logs, even if the log priority is set low enough that they wouldn't otherwise log.

Comments encouraged. Thanks again to Dan!

raven> Hi Jeff,

raven> There were a few things missed in the patch as there's a few other raven> places that calls to illegal routines are made.

raven> I did a bit of work on it over the weekend.

raven> Basically the changes I have made are:

raven> - moved calls to signal_children out of signal handler. raven> - created seperate module for safe_syslog implementation. raven> - changed assert to use safe_syslog also.
raven> - removed assertions from safe logging routines as it also calls
raven> (safe_)syslog.
raven> - added attempt at last gasp message in queue_syslog. raven> - added some string termination logic to queue_syslog.

raven> There's probably stuff that I've missed. raven> I've done some basic testing but more is needed.

raven> Can you review this and alter as you see fit please Jeff?

Done, and sent in a separate message.

raven> This just leaves the ioctl calls.
raven> I'm hoping we can verify they are safe. We can check the kernel control raven> path and if we can verify that the glibc code is suitably reentrant raven> they are probably OK. I'm having trouble finding the right code in raven> glibc. Do you have a glibc person that could point us in the right raven> direction Jeff?

I think you've moved the ioctl calls from signal handler context, right? I think this is resolved.

raven> We probably should agree on a name for the patch to avoid confusion raven> later. I've called mine autofs-4.1.4-signal-safe-logging.patch. What raven> name do you like?

That's fine with me. It certainly isn't important enough to fret over. ;)

-Jeff

maintainer
#15 lore raw
Quoted 74 lines · Jeff Moyer

==> Regarding Re: [patch, rfc] fix hung automounter issues; [email protected] adds:

Quoted 32 lines · Jeff Moyer
Quoted 32 lines

Hi, Ian,

Dan Berrange did a spectacular job of diagnosing some autofs hangs he was experiencing. The automount daemon would appear hung, and an an strace of the process would show sys_futex on the call stack. Through his debugging, he figured out that the problem was autofs issuing syslog calls while in a signal handler.

So, there are a few ways to fix this that come to mind.

o We could not log in signal handlers. I don't consider this an acceptable solution, as we really need the debug messages generated there.
o We could defer logging to non-signal handler context. This is in fact what the attached pattch does. It queues the syslog messages, and flushes them upon the next syslog that occurs outside of signal handler context.
o We could open /dev/log directly. This is likely a bad idea as there is no standard for the interface there.
o We could have a separate logging process, which we write to via a pipe. I'm not keen on this as it adds yet another process, and makes shutdown that much more complicated.

Note that in all of the above cases, we still need to implement a signal-safe vsprintf. That is what the bulk of this patch is.

So, here is a rough take on implementing the second bullet point. I wholesale copied a bunch of code from the linux kernel for doing vsprintf. That bit is ugly. I'd also move the definition of the new logging routines into the vsprintf file, and rename it. In short, this is a proof of concept (shown to resolve the issues). I'm happy to clean it up, but I want to be sure that this is the direction we want to go in, first.

Quoted 7 lines
Quoted 7 lines

Limitations of this approach: we won't flush the logs that were issued in signal handler context until another syslog call is made. One improvement that could be made straight-away is to have all of the logging routines call flush_logs, even if the log priority is set low enough that they wouldn't otherwise log.

Comments encouraged. Thanks again to Dan!

raven> Hi Jeff,

raven> There were a few things missed in the patch as there's a few other raven> places that calls to illegal routines are made.

raven> I did a bit of work on it over the weekend.

raven> Basically the changes I have made are:

raven> - moved calls to signal_children out of signal handler. raven> - created seperate module for safe_syslog implementation. raven> - changed assert to use safe_syslog also.
raven> - removed assertions from safe logging routines as it also calls
raven> (safe_)syslog.
raven> - added attempt at last gasp message in queue_syslog. raven> - added some string termination logic to queue_syslog.

raven> There's probably stuff that I've missed. raven> I've done some basic testing but more is needed.

raven> Can you review this and alter as you see fit please Jeff?

Done, and sent in a separate message.

raven> This just leaves the ioctl calls.
raven> I'm hoping we can verify they are safe. We can check the kernel control raven> path and if we can verify that the glibc code is suitably reentrant raven> they are probably OK. I'm having trouble finding the right code in raven> glibc. Do you have a glibc person that could point us in the right raven> direction Jeff?

Quoted 2 lines

I think you've moved the ioctl calls from signal handler context, right? I think this is resolved.

These are the ioctls in the send_ready and send_fail functions.

Ian