[PATCH 09/10] add support for logfile rotation

Julien Cristau jcristau at debian.org
Tue Jan 5 10:18:23 PST 2010


From: Branden Robinson <branden at debian.org>

- Implement new ReopenLogFile() and ReopenLogFileNotify() functions.
  ReopenLogFileNotify() is a signal handler for SIGUSR2.
- Move the definition of the WRITES() macro from error.c to dm_error.h
  so that dm.c can use it as well.
- Document xdm's signal handling in its manpage.

Forward-ported by Eugene Konev and Julien Cristau.
---
 dm.c        |   40 +++++++++++++++++++++++++++++++++++++++-
 dm_error.h  |    2 ++
 xdm.man.cpp |   31 +++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 1 deletions(-)

diff --git a/dm.c b/dm.c
index e8eab0c..b60acc9 100644
--- a/dm.c
+++ b/dm.c
@@ -80,8 +80,10 @@ from The Open Group.
 extern FILE    *fdopen();
 #endif
 
-static SIGVAL	StopAll (int n), RescanNotify (int n);
+static SIGVAL	StopAll (int n), RescanNotify (int n),
+				ReopenLogFileNotify (int n);
 static void	RescanServers (void);
+static void ReopenLogFile (void);
 static void	RestartDisplay (struct display *d, int forceReserver);
 static void	ScanServers (void);
 static void	SetAccessFileTime (void);
@@ -90,6 +92,7 @@ static void	StartDisplays (void);
 static void	TerminateProcess (pid_t pid, int signal);
 
 volatile int	Rescan;
+volatile int	Reopen;
 static long	ServersModTime, ConfigModTime, AccessFileModTime;
 
 int nofork_session = 0;
@@ -199,6 +202,7 @@ main (int argc, char **argv)
     AddOtherEntropy();
 #endif
     (void) Signal (SIGHUP, RescanNotify);
+    (void) Signal (SIGUSR2, ReopenLogFileNotify);
 #ifndef UNRELIABLE_SIGNALS
     (void) Signal (SIGCHLD, ChildNotify);
 #endif
@@ -209,6 +213,11 @@ main (int argc, char **argv)
 #endif
 	   AnyDisplaysLeft ())
     {
+	if (Reopen)
+	{
+	    ReopenLogFile ();
+		Reopen = 0;
+	}
 	if (Rescan)
 	{
 	    RescanServers ();
@@ -233,6 +242,7 @@ RescanNotify (int n)
     int olderrno = errno;
 
     Debug ("Caught SIGHUP\n");
+    Reopen = 1;
     Rescan = 1;
 #ifdef SIGNALS_RESET_WHEN_CAUGHT
     (void) Signal (SIGHUP, RescanNotify);
@@ -240,6 +250,26 @@ RescanNotify (int n)
     errno = olderrno;
 }
 
+/*
+ * Handle a SIGUSR2: set variable that will instruct the main loop to
+ * reopen the log file.
+ */
+static void
+ReopenLogFileNotify (int n)
+{
+#ifdef SIGNALS_RESET_WHEN_CAUGHT
+    int olderrno = errno;
+#endif
+
+    /* Debug() is not safe inside a signal handler. */
+    WRITES(STDERR_FILENO, "ReopenLogFileNotify handling SIGUSR2\n");
+    Reopen = 1;
+#ifdef SIGNALS_RESET_WHEN_CAUGHT
+    (void) Signal (SIGUSR2, ReopenLogFileNotify);
+    errno = olderrno;
+#endif
+}
+
 static void
 ScanServers (void)
 {
@@ -307,6 +337,14 @@ RescanServers (void)
 }
 
 static void
+ReopenLogFile (void)
+{
+    Debug ("closing standard error file descriptor %d\n", STDERR_FILENO);
+    close (STDERR_FILENO);
+    InitErrorLog ();
+}
+
+static void
 SetConfigFileTime (void)
 {
     struct stat	statb;
diff --git a/dm_error.h b/dm_error.h
index 739da7b..adb5cae 100644
--- a/dm_error.h
+++ b/dm_error.h
@@ -40,6 +40,8 @@ authorization.
 #  define GCC_PRINTFLIKE(fmt,var) /*nothing*/
 # endif
 
+#define WRITES(fd, buf) write(fd, buf, strlen(buf))
+
 extern void Debug        (const char * fmt, ...) GCC_PRINTFLIKE(1,2);
 extern void InitErrorLog (void);
 extern void LogAppend    (const char * fmt, ...) GCC_PRINTFLIKE(1,2);
diff --git a/xdm.man.cpp b/xdm.man.cpp
index 0d9a8b4..1916788 100644
--- a/xdm.man.cpp
+++ b/xdm.man.cpp
@@ -1403,6 +1403,37 @@ isn't very good at doing is coexisting with other window systems.  To use
 multiple window systems on the same hardware, you'll probably be more
 interested in
 .I xinit.
+.SH "ASYNCHRONOUS EVENTS"
+.B xdm
+uses
+.B SIGALRM
+and
+.B SIGUSR1
+for its own inter-process communication purposes, managing the relationship
+between the parent
+.B xdm
+process and its children.
+Sending these signals to any
+.B xdm
+process may result in unexpected behavior.
+.TP
+.B SIGHUP
+causes
+.B xdm
+to rescan its configuration files and reopen its log file.
+.TP
+.B SIGTERM
+causes
+.B xdm
+to terminate its children and shut down.
+.TP
+.B SIGUSR2
+causes
+.B xdm
+to reopen its log file.
+This is useful if log rotation is desired, but
+.B SIGHUP
+is too disruptive.
 .SH FILES
 .TP 20
 .I XDMDIR/xdm-config
-- 
1.6.5.7



More information about the xorg-devel mailing list