dtlogin: Resolve all -Wformat-security warnings
[oweals/cde.git] / cde / programs / dtlogin / reset.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*                                                                      *
24  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
25  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
26  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
27  * (c) Copyright 1993, 1994 Novell, Inc.                                *
28  */
29 /*
30  * xdm - display manager daemon
31  *
32  * $XConsortium: reset.c /main/4 1995/10/27 16:14:40 rswiston $
33  *
34  * Copyright 1988 Massachusetts Institute of Technology
35  *
36  * Permission to use, copy, modify, and distribute this software and its
37  * documentation for any purpose and without fee is hereby granted, provided
38  * that the above copyright notice appear in all copies and that both that
39  * copyright notice and this permission notice appear in supporting
40  * documentation, and that the name of M.I.T. not be used in advertising or
41  * publicity pertaining to distribution of the software without specific,
42  * written prior permission.  M.I.T. makes no representations about the
43  * suitability of this software for any purpose.  It is provided "as is"
44  * without express or implied warranty.
45  *
46  * Author:  Keith Packard, MIT X Consortium
47  */
48
49 /*
50  * pseudoReset -- pretend to reset the server by killing all clients
51  * with windows.  It will reset the server most of the time, unless
52  * a client remains connected with no windows.
53  */
54
55 # include       <setjmp.h>
56 # include       <sys/signal.h>
57 # include       "dm.h"
58 # include       "vgmsg.h"
59
60
61 /***************************************************************************
62  *
63  *  Local procedure declarations
64  *
65  ***************************************************************************/
66
67 static SIGVAL abortReset( int arg ) ;
68 static int ignoreErrors( Display *dpy, XErrorEvent *event) ;
69 static void killWindows( Display *dpy, Window window) ;
70
71
72
73 /***************************************************************************
74  *
75  *  
76  *
77  ***************************************************************************/
78
79 /*ARGSUSED*/
80 static int 
81 ignoreErrors( Display *dpy, XErrorEvent *event )
82 {
83         Debug ("Ignoring error...\n");
84         return 1;
85 }
86
87 /*
88  * this is mostly bogus -- but quite useful.  I wish the protocol
89  * had some way of enumerating and identifying clients, that way
90  * this code wouldn't have to be this kludgy.
91  */
92
93 static void 
94 killWindows( Display *dpy, Window window )
95 {
96         Window  root, parent, *children;
97         int     child;
98         unsigned int nchildren = 0;
99         
100         while (XQueryTree (dpy, window, &root, &parent, &children, &nchildren)
101                && nchildren > 0)
102         {
103                 for (child = 0; child < nchildren; child++) {
104                         Debug ("Calling XKillClient() for window 0x%x\n",
105                                 children[child]);
106                         XKillClient (dpy, children[child]);
107                 }
108                 XFree ((char *)children);
109         }
110 }
111
112 static jmp_buf  resetJmp;
113
114 static SIGVAL
115 abortReset( int arg )
116 {
117         longjmp (resetJmp, 1);
118 }
119
120 /*
121  * this display connection better not have any windows...
122  */
123  
124 void 
125 pseudoReset( Display *dpy )
126 {
127         Window  root;
128         int     screen;
129
130         if (setjmp (resetJmp)) {
131                 LogError(
132                   ReadCatalog(MC_LOG_SET,MC_LOG_PSEUDO,MC_DEF_LOG_PSEUDO));
133         } else {
134                 signal (SIGALRM, abortReset);
135                 alarm (30);
136                 XSetErrorHandler (ignoreErrors);
137                 for (screen = 0; screen < ScreenCount (dpy); screen++) {
138                         Debug ("Pseudo reset screen %d\n", screen);
139                         root = RootWindow (dpy, screen);
140                         killWindows (dpy, root);
141                 }
142                 Debug ("Before XSync\n");
143                 XSync (dpy, False);
144                 (void) alarm (0);
145         }
146         signal (SIGALRM, SIG_DFL);
147         XSetErrorHandler ((int (*)()) 0);
148         Debug ("pseudoReset() done\n");
149 }