1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
/*
Copyright 1988, 1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from The Open Group.
*/
/*
* xdm - display manager daemon
* Author: Keith Packard, MIT X Consortium
*
* server.c - manage the X server
*/
#include "dm.h"
#include "dm_error.h"
#include "dm_socket.h"
#include <X11/Xlib.h>
#include <X11/Xos.h>
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <poll.h>
#include <sys/socket.h>
static int receivedUsr1;
static int serverPause (unsigned t, pid_t serverPid);
/* ARGSUSED */
static SIGVAL
CatchUsr1 (int n)
{
int olderrno = errno;
Debug ("display manager caught SIGUSR1\n");
++receivedUsr1;
errno = olderrno;
}
const char *_SysErrorMsg (int n)
{
const char *s = strerror(n);
return (s ? s : "unknown error");
}
static int
StartServerOnce (struct display *d)
{
char **f;
char **argv;
char arg[1024];
pid_t pid;
Debug ("StartServer for %s\n", d->name);
receivedUsr1 = 0;
(void) signal (SIGUSR1, CatchUsr1);
argv = d->argv;
switch (pid = fork ()) {
case 0:
CleanUpChild ();
if (d->authFile) {
snprintf (arg, sizeof(arg), "-auth %s", d->authFile);
argv = parseArgs (argv, arg);
}
if (!argv) {
LogError ("StartServer: no arguments\n");
sleep ((unsigned) d->openDelay);
exit (UNMANAGE_DISPLAY);
}
for (f = argv; *f; f++)
Debug ("'%s' ", *f);
Debug ("\n");
/*
* give the server SIGUSR1 ignored,
* it will notice that and send SIGUSR1
* when ready
*/
signal (SIGUSR1, SIG_IGN);
execv (argv[0], argv);
LogError ("server %s cannot be executed\n",
argv[0]);
sleep ((unsigned) d->openDelay);
exit (REMANAGE_DISPLAY);
case -1:
LogError ("fork failed, sleeping\n");
return 0;
default:
break;
}
Debug ("Server Started %d\n", pid);
d->serverPid = pid;
if (serverPause ((unsigned) d->openDelay, pid))
return FALSE;
return TRUE;
}
int
StartServer (struct display *d)
{
int i;
int ret = FALSE;
i = 0;
while (d->serverAttempts == 0 || i < d->serverAttempts)
{
Debug("Starting X server attempt %d of %d\n", i, d->serverAttempts);
if ((ret = StartServerOnce (d)) == TRUE)
break;
sleep (d->openDelay);
i++;
}
return ret;
}
/*
* sleep for t seconds, return 1 if the server is dead when
* the sleep finishes, 0 else
*/
static void
chldHandler(int num)
{
Debug("a child died\n");
return;
}
static int serverPauseRet;
static int
serverPause (unsigned t, pid_t serverPid)
{
struct timespec timeout;
sigset_t mask;
int result;
pid_t pid;
serverPauseRet = 0;
for (;;) {
timeout.tv_sec = t;
timeout.tv_nsec = 0;
/* setup a SIGCHLD handler, to interrupt ppoll() below */
signal(SIGCHLD, chldHandler);
/* unblock all signals*/
sigemptyset(&mask);
if (!receivedUsr1) {
result = ppoll(NULL, 0, &timeout, &mask);
if (result != -1 || errno != EINTR)
Debug("Timeout waiting for USR1\n");
} else
Debug ("Already received USR1\n");
pid = waitpid (serverPid, (int *) 0, WNOHANG);
if (pid == serverPid ||
(pid == -1 && errno == ECHILD)) {
Debug ("Server dead\n");
serverPauseRet = 1;
break;
}
if (pid == 0) {
Debug ("Server alive and kicking\n");
break;
}
}
signal(SIGCHLD, SIG_DFL);
if (serverPauseRet) {
Debug ("Server died\n");
LogError ("server unexpectedly died\n");
}
return serverPauseRet;
}
static int
openErrorHandler (Display *dpy)
{
LogError ("IO Error in XOpenDisplay\n");
exit(OPENFAILED_DISPLAY);
/*NOTREACHED*/
return(0);
}
int
WaitForServer (struct display *d)
{
static int i;
for (i = 0; i < (d->openRepeat > 0 ? d->openRepeat : 1); i++) {
Debug ("Before XOpenDisplay(%s)\n", d->name);
errno = 0;
(void) XSetIOErrorHandler (openErrorHandler);
d->dpy = XOpenDisplay (d->name);
(void) XSetIOErrorHandler ((int (*)(Display *)) 0);
Debug ("After XOpenDisplay(%s)\n", d->name);
if (d->dpy) {
RegisterCloseOnFork (ConnectionNumber (d->dpy));
(void) fcntl (ConnectionNumber (d->dpy), F_SETFD, 0);
return 1;
} else {
Debug ("OpenDisplay failed %d (%s) on \"%s\"\n",
errno, strerror (errno), d->name);
}
Debug ("waiting for server to start %d\n", i);
sleep ((unsigned) d->openDelay);
}
Debug ("giving up on server\n");
LogError ("server open failed for %s, giving up\n", d->name);
return 0;
}
void
ResetServer (struct display *d)
{
if (d->dpy && d->displayType.origin != FromXDMCP)
pseudoReset (d->dpy);
}
|