summaryrefslogtreecommitdiffstats
path: root/tdm/backend/dpylist.c
blob: 0119c12cb81abdd8a25f25287bd3aa4b4f9534e1 (plain)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*

Copyright 1988, 1998  The Open Group
Copyright 2000-2005 Oswald Buddenhagen <ossi@kde.org>

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 COPYRIGHT HOLDERS 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 a copyright holder 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 copyright holder.

*/

/*
 * xdm - display manager daemon
 * Author: Keith Packard, MIT X Consortium
 *
 * a simple linked list of known displays
 */

#include "dm.h"
#include "dm_error.h"

struct display *displays;
static struct disphist *disphist;

int
AnyDisplaysLeft( void )
{
	return displays != (struct display *)0;
}

int
AnyActiveDisplays( void )
{
	struct display *d;

	for (d = displays; d; d = d->next)
		if (d->status == remoteLogin || d->userSess >= 0)
			return 1;
	return 0;
}

int
AnyRunningDisplays( void )
{
	struct display *d;

	for (d = displays; d; d = d->next)
		switch (d->status) {
			case notRunning:
			case textMode:
			case reserve:
				break;
			default:
				return 1;
		}
	return 0;
}

int
AnyReserveDisplays( void )
{
	struct display *d;

	for (d = displays; d; d = d->next) {
		if ((d->displayType & d_lifetime) == dReserve) {
			return 1;
		}
	}
	return 0;
}

int
idleReserveDisplays( void )
{
	struct display *d;
	int cnt = 0;

	for (d = displays; d; d = d->next) {
		if (d->status == reserve) {
			cnt++;
		}
	}
	return cnt;
}

int
StartReserveDisplay( int lt )
{
	struct display *d, *rd;

	for (rd = 0, d = displays; d; d = d->next) {
		if (d->status == reserve) {
			rd = d;
		}
	}
	if (rd) {
		rd->idleTimeout = lt;
		rd->status = notRunning;
		return 1;
	}
	return 0;
}

void
ForEachDisplay( void (*f)( struct display * ) )
{
	struct display *d, *next;

	for (d = displays; d; d = next) {
		next = d->next;
		(*f)( d );
	}
}

#ifdef HAVE_VTS
static void
_forEachDisplayRev( struct display *d, void (*f)( struct display * ) )
{
	if (d) {
		if (d->next) {
			_forEachDisplayRev(d->next, f);
		}
		(*f)( d );
	}
}

void
ForEachDisplayRev( void (*f)( struct display * ) )
{
	_forEachDisplayRev( displays, f );
}
#endif

struct display *
FindDisplayByName( const char *name )
{
	struct display *d;

	for (d = displays; d; d = d->next) {
		if (!strcmp( name, d->name )) {
			return d;
		}
	}
	return 0;
}

struct display *
FindDisplayByPid( int pid )
{
	struct display *d;

	for (d = displays; d; d = d->next) {
		if (pid == d->pid) {
			return d;
		}
	}
	return 0;
}

struct display *
FindDisplayByServerPid( int serverPid )
{
	struct display *d;

	for (d = displays; d; d = d->next) {
		if (serverPid == d->serverPid) {
			return d;
		}
	}
	return 0;
}

#ifdef XDMCP

struct display *
FindDisplayBySessionID( CARD32 sessionID )
{
	struct display *d;

	for (d = displays; d; d = d->next) {
		if (sessionID == d->sessionID) {
			return d;
		}
	}
	return 0;
}

struct display *
FindDisplayByAddress( XdmcpNetaddr addr, int addrlen, CARD16 displayNumber )
{
	struct display *d;

	for (d = displays; d; d = d->next) {
		if ((d->displayType & d_origin) == dFromXDMCP &&
		    d->displayNumber == displayNumber &&
		    addressEqual( (XdmcpNetaddr)d->from.data, d->from.length,
		                  addr, addrlen )) {
			return d;
		}
	}
	return 0;
}

#endif /* XDMCP */

#define IfFree(x)  if (x) free( (char *)x )

void
RemoveDisplay(struct display *old)
{
	struct display *d, **dp;
	int i;

	for (dp = &displays; (d = *dp); dp = &(*dp)->next) {
		if (d == old) {
			Debug("Removing display %s\n", d->name);
			*dp = d->next;
			IfFree(d->class2);
			IfFree(d->cfg.data);
			delStr(d->cfg.dep.name);
#ifdef XDMCP
			IfFree(d->remoteHost);
#endif
			if (d->authorizations) {
				for (i = 0; i < d->authNum; i++) {
					XauDisposeAuth(d->authorizations[i]);
				}
				free((char *)d->authorizations);
			}
			if (d->authFile) {
				(void)unlink(d->authFile);
				free(d->authFile);
			}
			IfFree(d->authNameLens);
#ifdef XDMCP
			XdmcpDisposeARRAY8(&d->peer);
			XdmcpDisposeARRAY8(&d->from);
			XdmcpDisposeARRAY8(&d->clientAddr);
#endif
			free((char *)d);
			break;
		}
	}
}

static struct disphist *
FindHist( const char *name )
{
	struct disphist *hstent;

	for (hstent = disphist; hstent; hstent = hstent->next)
		if (!strcmp( hstent->name, name ))
			return hstent;
	return 0;
}

struct display *
NewDisplay( const char *name )
{
	struct display *d;
	struct disphist *hstent;

	if (!(hstent = FindHist( name ))) {
		if (!(hstent = Calloc( 1, sizeof(*hstent) )))
			return 0;
		if (!StrDup( &hstent->name, name )) {
			free( hstent );
			return 0;
		}
		hstent->next = disphist; disphist = hstent;
	}

	if (!(d = (struct display *)Calloc( 1, sizeof(*d) ))) {
		return 0;
	}
	d->next = displays;
	d->hstent = hstent;
	d->name = hstent->name;
	/* initialize fields (others are 0) */
	d->pid = -1;
	d->serverPid = -1;
	d->ctrl.fd = -1;
	d->ctrl.fifo.fd = -1;
	d->pipe.rfd = -1;
	d->pipe.wfd = -1;
	d->gpipe.rfd = -1;
	d->gpipe.wfd = -1;
	d->userSess = -1;
#ifdef XDMCP
	d->xdmcpFd = -1;
#endif
	displays = d;
	Debug("created new display %s\n", d->name);
	return d;
}