diff options
Diffstat (limited to 'dm.c')
-rw-r--r-- | dm.c | 179 |
1 files changed, 104 insertions, 75 deletions
@@ -26,6 +26,7 @@ other dealings in this Software without prior written authorization from The Open Group. */ +/* $XFree86: xc/programs/xdm/dm.c,v 3.21 2002/12/07 20:31:04 herrb Exp $ */ /* * xdm - display manager daemon @@ -35,6 +36,8 @@ from The Open Group. */ # include "dm.h" +# include "dm_auth.h" +# include "dm_error.h" # include <stdio.h> #ifdef X_POSIX_C_SOURCE @@ -50,6 +53,9 @@ from The Open Group. #undef _POSIX_SOURCE #endif #endif +#ifdef __NetBSD__ +#include <sys/param.h> +#endif #ifndef sigmask #define sigmask(m) (1 << ((m - 1))) @@ -58,13 +64,7 @@ from The Open Group. # include <sys/stat.h> # include <errno.h> # include <X11/Xfuncproto.h> -#if NeedVarargsPrototypes -# include <stdarg.h> -# define Va_start(a,b) va_start(a,b) -#else -# include <varargs.h> -# define Va_start(a,b) va_start(a) -#endif +# include <stdarg.h> #ifndef F_TLOCK #ifndef X_NOT_POSIX @@ -72,23 +72,22 @@ from The Open Group. #endif #endif -#ifdef X_NOT_STDC_ENV -extern int errno; -#endif - -#ifdef SVR4 +#if defined(SVR4) && !defined(SCO) extern FILE *fdopen(); #endif -static void RescanServers (); -static void ScanServers (); +static SIGVAL StopAll (int n), RescanNotify (int n); +static void RescanServers (void); +static void RestartDisplay (struct display *d, int forceReserver); +static void ScanServers (void); +static void SetAccessFileTime (void); +static void SetConfigFileTime (void); +static void StartDisplays (void); +static void TerminateProcess (int pid, int signal); + int Rescan; static long ServersModTime, ConfigModTime, AccessFileModTime; -static SIGVAL StopAll (), RescanNotify (); -void StopDisplay (); -static void RestartDisplay (); -static void StartDisplays (); int nofork_session = 0; @@ -98,14 +97,15 @@ static int TitleLen; #endif #ifndef UNRELIABLE_SIGNALS -static SIGVAL ChildNotify (); +static SIGVAL ChildNotify (int n); #endif +static int StorePid (void); + static int parent_pid = -1; /* PID of parent xdm process */ -main (argc, argv) -int argc; -char **argv; +int +main (int argc, char **argv) { int oldpid, oldumask; char cmdbuf[1024]; @@ -153,7 +153,8 @@ char **argv; if (nofork_session == 0) { /* Clean up any old Authorization files */ - sprintf(cmdbuf, "/bin/rm -f %s/authdir/authfiles/A*", authDir); + /* AUD: all good? */ + snprintf(cmdbuf, sizeof(cmdbuf), "/bin/rm -f %s/authdir/authfiles/A*", authDir); system(cmdbuf); } @@ -207,18 +208,20 @@ char **argv; /* ARGSUSED */ static SIGVAL -RescanNotify (n) - int n; +RescanNotify (int n) { + int olderrno = errno; + Debug ("Caught SIGHUP\n"); Rescan = 1; #ifdef SIGNALS_RESET_WHEN_CAUGHT (void) Signal (SIGHUP, RescanNotify); #endif + errno = olderrno; } static void -ScanServers () +ScanServers (void) { char lineBuf[10240]; int len; @@ -260,14 +263,13 @@ ScanServers () } static void -MarkDisplay (d) -struct display *d; +MarkDisplay (struct display *d) { d->state = MissingEntry; } static void -RescanServers () +RescanServers (void) { Debug ("rescanning servers\n"); LogInfo ("Rescanning both config and servers files\n"); @@ -283,7 +285,8 @@ RescanServers () StartDisplays (); } -SetConfigFileTime () +static void +SetConfigFileTime (void) { struct stat statb; @@ -291,7 +294,8 @@ SetConfigFileTime () ConfigModTime = statb.st_mtime; } -SetAccessFileTime () +static void +SetAccessFileTime (void) { struct stat statb; @@ -299,8 +303,8 @@ SetAccessFileTime () AccessFileModTime = statb.st_mtime; } -static -RescanIfMod () +static void +RescanIfMod (void) { struct stat statb; @@ -346,9 +350,10 @@ RescanIfMod () /* ARGSUSED */ static SIGVAL -StopAll (n) - int n; +StopAll (int n) { + int olderrno = errno; + if (parent_pid != getpid()) { /* @@ -361,6 +366,7 @@ StopAll (n) Debug ("Child xdm caught SIGTERM before it remove that signal.\n"); (void) Signal (n, SIG_DFL); TerminateProcess (getpid(), SIGTERM); + errno = olderrno; return; } Debug ("Shutting down entire manager\n"); @@ -373,6 +379,7 @@ StopAll (n) (void) Signal (SIGTERM, StopAll); (void) Signal (SIGINT, StopAll); #endif + errno = olderrno; } /* @@ -385,19 +392,25 @@ int ChildReady; #ifndef UNRELIABLE_SIGNALS /* ARGSUSED */ static SIGVAL -ChildNotify (n) - int n; +ChildNotify (int n) { + int olderrno = errno; + ChildReady = 1; +#ifdef ISC + (void) Signal (SIGCHLD, ChildNotify); +#endif + errno = olderrno; } #endif -WaitForChild () +void +WaitForChild (void) { int pid; struct display *d; waitType status; -#ifndef X_NOT_POSIX +#if !defined(X_NOT_POSIX) && !defined(__UNIXOS2__) sigset_t mask, omask; #else int omask; @@ -412,10 +425,11 @@ WaitForChild () sigaddset(&mask, SIGCHLD); sigaddset(&mask, SIGHUP); sigprocmask(SIG_BLOCK, &mask, &omask); + Debug ("signals blocked\n"); #else omask = sigblock (sigmask (SIGCHLD) | sigmask (SIGHUP)); -#endif Debug ("signals blocked, mask was 0x%x\n", omask); +#endif if (!ChildReady && !Rescan) #ifndef X_NOT_POSIX sigsuspend(&omask); @@ -493,13 +507,30 @@ WaitForChild () StopDisplay(d); else RestartDisplay (d, TRUE); + { + Time_t Time; + time(&Time); + Debug("time %i %i\n",Time,d->lastCrash); + if (d->lastCrash && + ((Time - d->lastCrash) < XDM_BROKEN_INTERVAL)) { + Debug("Server crash frequency too high:" + " removing display %s\n",d->name); + LogError("Server crash rate too high:" + " removing display %s\n",d->name); + RemoveDisplay (d); + } else + d->lastCrash = Time; + } break; case waitCompose (SIGTERM,0,0): - d->startTries = 0; - Debug ("Display exited on SIGTERM\n"); - if (d->displayType.origin == FromXDMCP || d->status == zombie) + Debug ("Display exited on SIGTERM, try %d of %d\n", + d->startTries, d->startAttempts); + if (d->displayType.origin == FromXDMCP || + d->status == zombie || + ++d->startTries >= d->startAttempts) { + LogError ("Display %s is being disabled\n", d->name); StopDisplay(d); - else + } else RestartDisplay (d, TRUE); break; case REMANAGE_DISPLAY: @@ -531,13 +562,13 @@ WaitForChild () d->status = notRunning; break; case running: - Debug ("Server for display %s terminated unexpectedly, status %d\n", d->name, waitVal (status)); + Debug ("Server for display %s terminated unexpectedly, status %d %d\n", d->name, waitVal (status), status); LogError ("Server for display %s terminated unexpectedly: %d\n", d->name, waitVal (status)); if (d->pid != -1) { Debug ("Terminating session pid %d\n", d->pid); TerminateProcess (d->pid, SIGTERM); - } + } break; case notRunning: Debug ("Server exited for notRunning session on display %s\n", d->name); @@ -553,8 +584,7 @@ WaitForChild () } static void -CheckDisplayStatus (d) -struct display *d; +CheckDisplayStatus (struct display *d) { if (d->displayType.origin == FromFile) { @@ -573,14 +603,13 @@ struct display *d; } static void -StartDisplays () +StartDisplays (void) { ForEachDisplay (CheckDisplayStatus); } void -StartDisplay (d) -struct display *d; +StartDisplay (struct display *d) { int pid; @@ -651,7 +680,8 @@ struct display *d; } } -TerminateProcess (pid, signal) +static void +TerminateProcess (int pid, int signal) { kill (pid, signal); #ifdef SIGCONT @@ -664,8 +694,7 @@ TerminateProcess (pid, signal) */ void -StopDisplay (d) - struct display *d; +StopDisplay (struct display *d) { if (d->serverPid != -1) d->status = zombie; /* be careful about race conditions */ @@ -682,9 +711,7 @@ StopDisplay (d) */ static void -RestartDisplay (d, forceReserver) - struct display *d; - int forceReserver; +RestartDisplay (struct display *d, int forceReserver) { if (d->serverPid != -1 && (forceReserver || d->terminateServer)) { @@ -700,16 +727,16 @@ RestartDisplay (d, forceReserver) static FD_TYPE CloseMask; static int max; -RegisterCloseOnFork (fd) -int fd; +void +RegisterCloseOnFork (int fd) { FD_SET (fd, &CloseMask); if (fd > max) max = fd; } -ClearCloseOnFork (fd) -int fd; +void +ClearCloseOnFork (int fd) { FD_CLR (fd, &CloseMask); if (fd == max) { @@ -720,13 +747,16 @@ int fd; } } -CloseOnFork () +void +CloseOnFork (void) { int fd; for (fd = 0; fd <= max; fd++) if (FD_ISSET (fd, &CloseMask)) + { close (fd); + } FD_ZERO (&CloseMask); max = 0; } @@ -734,12 +764,13 @@ CloseOnFork () static int pidFd; static FILE *pidFilePtr; -StorePid () +static int +StorePid (void) { int oldpid; if (pidFile[0] != '\0') { - pidFd = open (pidFile, 2); + pidFd = open (pidFile, O_RDWR); if (pidFd == -1 && errno == ENOENT) pidFd = open (pidFile, O_RDWR|O_CREAT, 0666); if (pidFd == -1 || !(pidFilePtr = fdopen (pidFd, "r+"))) @@ -788,14 +819,16 @@ StorePid () #endif #endif } - fprintf (pidFilePtr, "%5d\n", getpid ()); + fprintf (pidFilePtr, "%5ld\n", (long)getpid ()); (void) fflush (pidFilePtr); RegisterCloseOnFork (pidFd); } return 0; } -UnlockPidFile () +#if 0 +void +UnlockPidFile (void) { if (lockPidFile) #ifdef F_SETLK @@ -816,15 +849,10 @@ UnlockPidFile () close (pidFd); fclose (pidFilePtr); } - -#if NeedVarargsPrototypes -SetTitle (char *name, ...) -#else -/*VARARGS*/ -SetTitle (name, va_alist) -char *name; -va_dcl #endif + +#ifndef HAS_SETPROCTITLE +void SetTitle (char *name, ...) { #ifndef NOXDMTITLE char *p = Title; @@ -832,7 +860,7 @@ va_dcl char *s; va_list args; - Va_start(args,name); + va_start(args,name); *p++ = '-'; --left; s = name; @@ -853,3 +881,4 @@ va_dcl va_end(args); #endif } +#endif |