Home | History | Annotate | Line # | Download | only in xdm
      1 /*
      2 
      3 Copyright 1988, 1998  The Open Group
      4 
      5 Permission to use, copy, modify, distribute, and sell this software and its
      6 documentation for any purpose is hereby granted without fee, provided that
      7 the above copyright notice appear in all copies and that both that
      8 copyright notice and this permission notice appear in supporting
      9 documentation.
     10 
     11 The above copyright notice and this permission notice shall be included
     12 in all copies or substantial portions of the Software.
     13 
     14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     16 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     17 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
     18 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     19 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     20 OTHER DEALINGS IN THE SOFTWARE.
     21 
     22 Except as contained in this notice, the name of The Open Group shall
     23 not be used in advertising or otherwise to promote the sale, use or
     24 other dealings in this Software without prior written authorization
     25 from The Open Group.
     26 
     27 */
     28 
     29 /*
     30  * xdm - display manager daemon
     31  * Author:  Keith Packard, MIT X Consortium
     32  *
     33  * pseudoReset -- pretend to reset the server by killing all clients
     34  * with windows.  It will reset the server most of the time, unless
     35  * a client remains connected with no windows.
     36  */
     37 
     38 #include	"dm.h"
     39 #include	"dm_error.h"
     40 
     41 #include	<X11/Xlib.h>
     42 #include	<signal.h>
     43 
     44 /*ARGSUSED*/
     45 static int
     46 ignoreErrors (Display *dpy, XErrorEvent *event)
     47 {
     48 	Debug ("ignoring error\n");
     49 	return 0;
     50 }
     51 
     52 /*
     53  * this is mostly bogus -- but quite useful.  I wish the protocol
     54  * had some way of enumerating and identifying clients, that way
     55  * this code wouldn't have to be this kludgy.
     56  */
     57 
     58 static void
     59 killWindows (Display *dpy, Window window)
     60 {
     61 	Window	root, parent, *children;
     62 	int	child;
     63 	unsigned int nchildren = 0;
     64 
     65 	while (XQueryTree (dpy, window, &root, &parent, &children, &nchildren)
     66 	       && nchildren > 0)
     67 	{
     68 		for (child = 0; child < nchildren; child++) {
     69 			Debug ("XKillClient 0x%lx\n", (unsigned long)children[child]);
     70 			XKillClient (dpy, children[child]);
     71 		}
     72 		XFree ((char *)children);
     73 	}
     74 }
     75 
     76 static Jmp_buf	resetJmp;
     77 
     78 /* ARGSUSED */
     79 _X_NORETURN
     80 static void
     81 abortReset (int n)
     82 {
     83 	Longjmp (resetJmp, 1);
     84 }
     85 
     86 /*
     87  * this display connection better not have any windows...
     88  */
     89 
     90 void
     91 pseudoReset (Display *dpy)
     92 {
     93 	Window	root;
     94 	int	screen;
     95 
     96 	if (Setjmp (resetJmp)) {
     97 		LogError ("pseudoReset timeout\n");
     98 	} else {
     99 		(void) Signal (SIGALRM, abortReset);
    100 		(void) alarm (30);
    101 		XSetErrorHandler (ignoreErrors);
    102 		for (screen = 0; screen < ScreenCount (dpy); screen++) {
    103 			Debug ("pseudoReset screen %d\n", screen);
    104 			root = RootWindow (dpy, screen);
    105 			killWindows (dpy, root);
    106 		}
    107 		Debug ("before XSync\n");
    108 		XSync (dpy, False);
    109 		(void) alarm (0);
    110 	}
    111 	Signal (SIGALRM, SIG_DFL);
    112 	XSetErrorHandler ((XErrorHandler)0 );
    113 	Debug ("pseudoReset done\n");
    114 }
    115