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 * a simple linked list of known displays
34 */
35
36 #include "dm.h"
37 #include "dm_error.h"
38
39 static struct display *displays;
40
41 int
AnyDisplaysLeft(void)42 AnyDisplaysLeft (void)
43 {
44 return displays != (struct display *) 0;
45 }
46
47 void
ForEachDisplay(void (* f)(struct display *))48 ForEachDisplay (void (*f)(struct display *))
49 {
50 struct display *d, *next;
51
52 for (d = displays; d; d = next) {
53 next = d->next;
54 (*f) (d);
55 }
56 }
57
58 struct display *
FindDisplayByName(char * name)59 FindDisplayByName (char *name)
60 {
61 struct display *d;
62
63 for (d = displays; d; d = d->next)
64 if (!strcmp (name, d->name))
65 return d;
66 return NULL;
67 }
68
69 struct display *
FindDisplayByPid(pid_t pid)70 FindDisplayByPid (pid_t pid)
71 {
72 struct display *d;
73
74 for (d = displays; d; d = d->next)
75 if (pid == d->pid)
76 return d;
77 return NULL;
78 }
79
80 struct display *
FindDisplayByServerPid(pid_t serverPid)81 FindDisplayByServerPid (pid_t serverPid)
82 {
83 struct display *d;
84
85 for (d = displays; d; d = d->next)
86 if (serverPid == d->serverPid)
87 return d;
88 return NULL;
89 }
90
91 #ifdef XDMCP
92
93 struct display *
FindDisplayBySessionID(CARD32 sessionID)94 FindDisplayBySessionID (CARD32 sessionID)
95 {
96 struct display *d;
97
98 for (d = displays; d; d = d->next)
99 if (sessionID == d->sessionID)
100 return d;
101 return NULL;
102 }
103
104 struct display *
FindDisplayByAddress(XdmcpNetaddr addr,int addrlen,CARD16 displayNumber)105 FindDisplayByAddress (XdmcpNetaddr addr, int addrlen, CARD16 displayNumber)
106 {
107 struct display *d;
108
109 for (d = displays; d; d = d->next)
110 if (d->displayType.origin == FromXDMCP &&
111 d->displayNumber == displayNumber &&
112 addressEqual (d->from, d->fromlen, addr, addrlen))
113 {
114 return d;
115 }
116 return NULL;
117 }
118
119 #endif /* XDMCP */
120
121 void
RemoveDisplay(struct display * old)122 RemoveDisplay (struct display *old)
123 {
124 struct display *d, *p;
125 char **x;
126 int i;
127
128 p = NULL;
129 for (d = displays; d; d = d->next) {
130 if (d == old) {
131 if (p)
132 p->next = d->next;
133 else
134 displays = d->next;
135 free (d->name);
136 free (d->class);
137 for (x = d->argv; x && *x; x++)
138 free (*x);
139 free (d->argv);
140 free (d->resources);
141 free (d->xrdb);
142 free (d->setup);
143 free (d->startup);
144 free (d->reset);
145 free (d->session);
146 free (d->userPath);
147 free (d->systemPath);
148 free (d->systemShell);
149 free (d->failsafeClient);
150 free (d->chooser);
151 if (d->authorizations)
152 {
153 for (i = 0; i < d->authNum; i++)
154 XauDisposeAuth (d->authorizations[i]);
155 free (d->authorizations);
156 }
157 free (d->clientAuthFile);
158 if (d->authFile)
159 (void) unlink (d->authFile);
160 free (d->authFile);
161 free (d->userAuthDir);
162 for (x = d->authNames; x && *x; x++)
163 free (*x);
164 free (d->authNames);
165 free (d->authNameLens);
166 #ifdef XDMCP
167 free (d->peer);
168 free (d->from);
169 XdmcpDisposeARRAY8 (&d->clientAddr);
170 #endif
171 free (d->windowPath);
172 free (d);
173 break;
174 }
175 p = d;
176 }
177 }
178
179 struct display *
NewDisplay(char * name,char * class)180 NewDisplay (char *name, char *class)
181 {
182 struct display *d;
183
184 d = calloc (1, sizeof (struct display));
185 if (!d) {
186 LogOutOfMem ("NewDisplay");
187 return NULL;
188 }
189 d->next = displays;
190 d->name = strdup (name);
191 if (!d->name) {
192 LogOutOfMem ("NewDisplay");
193 free (d);
194 return NULL;
195 }
196 if (class)
197 {
198 d->class = strdup (class);
199 if (!d->class) {
200 LogOutOfMem ("NewDisplay");
201 free (d->name);
202 free (d);
203 return NULL;
204 }
205 }
206 else
207 {
208 d->class = NULL;
209 }
210 /* initialize every field to avoid possible problems */
211 d->argv = NULL;
212 d->status = notRunning;
213 d->pid = -1;
214 d->serverPid = -1;
215 d->state = NewEntry;
216 d->resources = NULL;
217 d->xrdb = NULL;
218 d->setup = NULL;
219 d->startup = NULL;
220 d->reset = NULL;
221 d->session = NULL;
222 d->userPath = NULL;
223 d->systemPath = NULL;
224 d->systemShell = NULL;
225 d->failsafeClient = NULL;
226 d->chooser = NULL;
227 d->authorize = FALSE;
228 d->authorizations = NULL;
229 d->authNum = 0;
230 d->authNameNum = 0;
231 d->clientAuthFile = NULL;
232 d->authFile = NULL;
233 d->userAuthDir = NULL;
234 d->authNames = NULL;
235 d->authNameLens = NULL;
236 d->authComplain = 1;
237 d->openDelay = 0;
238 d->openRepeat = 0;
239 d->openTimeout = 0;
240 d->startAttempts = 0;
241 d->startTries = 0;
242 d->lastReserv = 0;
243 d->reservAttempts = 0;
244 d->reservTries = 0;
245 d->terminateServer = 0;
246 d->grabTimeout = 0;
247 #ifdef XDMCP
248 d->sessionID = 0;
249 d->peer = NULL;
250 d->peerlen = 0;
251 d->from = NULL;
252 d->fromlen = 0;
253 d->displayNumber = 0;
254 d->useChooser = 0;
255 d->clientAddr.data = NULL;
256 d->clientAddr.length = 0;
257 d->connectionType = 0;
258 d->xdmcpFd = -1;
259 #endif
260 d->version = 1; /* registered with The Open Group */
261 d->willing = NULL;
262 d->dpy = NULL;
263 d->windowPath = NULL;
264 displays = d;
265 return d;
266 }
267