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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
|
/*
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
*
* obtain configuration data
*/
#include "dm.h"
#include "dm_error.h"
#include <sys/stat.h>
static char **originalArgv;
static GProc getter;
GTalk cnftalk;
static void
OpenGetter()
{
GSet( &cnftalk );
if (!getter.pid) {
if (GOpen( &getter,
originalArgv, "_config", 0, strdup( "config reader" ),
0 ))
LogPanic( "Cannot run config reader\n" );
Debug( "getter now ready\n" );
}
}
void
CloseGetter()
{
if (getter.pid) {
GSet( &cnftalk );
(void)GClose (&getter, 0, 0);
Debug( "getter now closed\n" );
}
}
/*
* ref-counted, unique-instance strings
*/
static RcStr *strs;
/*
* make a ref-counted string of the argument. the new string will
* have a ref-count of 1. the passed string pointer is no longer valid.
*/
RcStr *
newStr( char *str )
{
RcStr *cs;
for (cs = strs; cs; cs = cs->next)
if (!strcmp( str, cs->str )) {
free( str );
cs->cnt++;
return cs;
}
if (!(cs = Malloc( sizeof(*cs) )))
return 0;
cs->cnt = 1;
cs->str = str;
cs->next = strs;
strs = cs;
return cs;
}
/*
* decrement ref-count and delete string when count drops to 0.
*/
void
delStr( RcStr *str )
{
RcStr **cs;
if (!str || --str->cnt)
return;
for (cs = &strs; *cs; cs = &((*cs)->next))
if (str == *cs) {
*cs = (*cs)->next;
free( (*cs)->str );
free( *cs );
break;
}
}
typedef struct CfgFile {
RcStr *name;
int depidx;
long deptime;
} CfgFile;
static int numCfgFiles;
static CfgFile *cfgFiles;
static int cfgMapT[] = {
GC_gGlobal,
GC_gDisplay,
#ifdef XDMCP
GC_gXaccess,
#endif
};
static int cfgMap[as(cfgMapT)];
static int
GetDeps()
{
int ncf, i, dep, ret;
CfgFile *cf;
OpenGetter();
GSendInt( GC_Files );
ncf = GRecvInt();
if (!(cf = Malloc( ncf * sizeof(*cf) ))) {
CloseGetter();
return 0;
}
for (i = 0; i < ncf; i++) {
cf[i].name = newStr( GRecvStr() );
if ((dep = cf[i].depidx = GRecvInt()) != -1)
cf[i].deptime = mTime( cf[dep].name->str );
}
if (cfgFiles) {
for (i = 0; i < numCfgFiles; i++)
delStr( cfgFiles[i].name );
free( cfgFiles );
}
ret = 1;
cfgFiles = cf;
numCfgFiles = ncf;
for (i = 0; i < as(cfgMapT); i++) {
GSendInt( cfgMapT[i] );
if ((cfgMap[i] = GRecvInt()) < 0) {
LogError( "Config reader does not support config cathegory %#x\n",
cfgMapT[i] );
ret = 0;
}
}
GSendInt( -1 );
return ret;
}
static int
checkDep( int idx )
{
int dep;
if ((dep = cfgFiles[idx].depidx) == -1)
return 0;
if (checkDep( dep ))
return 1;
return mTime( cfgFiles[dep].name->str ) != cfgFiles[idx].deptime;
}
static int
needsReScan( int what, CfgDep *dep )
{
int widx, idx;
long mt;
for (widx = 0; cfgMapT[widx] != what; widx++);
idx = cfgMap[widx];
if (checkDep( idx )) {
if (!GetDeps())
return -1;
idx = cfgMap[widx];
}
mt = mTime( cfgFiles[idx].name->str );
if (dep->name != cfgFiles[idx].name) {
if (dep->name)
delStr( dep->name );
dep->name = cfgFiles[idx].name;
dep->name->cnt++;
dep->time = mt;
return 1;
} else if (dep->time != mt) {
dep->time = mt;
return 1;
} else
return 0;
}
int
startConfig( int what, CfgDep *dep, int force )
{
int ret;
if ((ret = needsReScan( what, dep )) < 0 || (!ret && !force))
return ret;
OpenGetter();
GSendInt( GC_GetConf );
GSendInt( what );
GSendStr( dep->name->str );
return 1;
}
static void
LoadResources( CfgArr *conf )
{
char **vptr, **pptr, *cptr;
long *iptr, i, id, nu, j, nptr, nint, nchr;
if (conf->data)
free( conf->data );
conf->numCfgEnt = GRecvInt();
nptr = GRecvInt();
nint = GRecvInt();
nchr = GRecvInt();
if (!(conf->data = Malloc( conf->numCfgEnt *
(sizeof(long) +
sizeof(char *)) +
nptr * sizeof(char *) +
nint * sizeof(long) +
nchr )))
{
CloseGetter();
return;
}
vptr = (char **)conf->data;
pptr = vptr + conf->numCfgEnt;
conf->idx = (long *)(pptr + nptr);
iptr = conf->idx + conf->numCfgEnt;
cptr = (char *)(iptr + nint);
for (i = 0; i < conf->numCfgEnt; i++) {
id = GRecvInt();
conf->idx[i] = id;
switch (id & C_TYPE_MASK) {
case C_TYPE_INT:
vptr[i] = (char *)((unsigned long)GRecvInt());
break;
case C_TYPE_STR:
vptr[i] = cptr;
cptr += GRecvStrBuf( cptr );
break;
case C_TYPE_ARGV:
nu = GRecvInt();
vptr[i] = (char *)pptr;
for (j = 0; j < nu; j++) {
*pptr++ = cptr;
cptr += GRecvStrBuf( cptr );
}
*pptr++ = (char *)0;
break;
default:
LogError( "Config reader supplied unknown data type in id %#x\n",
id );
break;
}
}
}
static void
ApplyResource( int id, char **src, char **dst )
{
switch (id & C_TYPE_MASK) {
case C_TYPE_INT:
*(int *)dst = *(long *)src;
break;
case C_TYPE_STR:
case C_TYPE_ARGV:
*dst = *src;
break;
}
}
#define boffset(f) XtOffsetOf(struct display, f)
/* no global variables exported currently
struct globEnts {
int id;
char **off;
} globEnt[] = {
};
*/
/* no per-display variables exported currently
struct dpyEnts {
int id;
int off;
} dpyEnt[] = {
};
*/
CfgArr cfg;
char **
FindCfgEnt( struct display *d, int id )
{
int i;
/* no global variables exported currently
for (i = 0; i < as(globEnt); i++)
if (globEnt[i].id == id)
return globEnt[i].off;
*/
for (i = 0; i < cfg.numCfgEnt; i++)
if (cfg.idx[i] == id)
return ((char **)cfg.data) + i;
if (d) {
/* no per-display variables exported currently
for (i = 0; i < as(dpyEnt); i++)
if (dpyEnt[i].id == id)
return (char **)(((char *)d) + dpyEnt[i].off);
*/
for (i = 0; i < d->cfg.numCfgEnt; i++)
if (d->cfg.idx[i] == id)
return ((char **)d->cfg.data) + i;
}
Debug( "unknown config entry %#x requested\n", id );
return (char **)0;
}
CONF_CORE_GLOBAL_DEFS
struct globVals {
int id;
char **off;
} globVal[] = {
CONF_CORE_GLOBALS
};
int
LoadDMResources( int force )
{
int i, ret;
char **ent;
if (Setjmp( cnftalk.errjmp ))
return -1; /* may memleak, but we probably have to abort anyway */
if ((ret = startConfig( GC_gGlobal, &cfg.dep, force )) <= 0)
return ret;
LoadResources( &cfg );
/* Debug( "manager resources: %[*x\n",
cfg.numCfgEnt, ((char **)cfg.data) + cfg.numCfgEnt );*/
ret = 1;
for (i = 0; i < as(globVal); i++) {
if (!(ent = FindCfgEnt( 0, globVal[i].id )))
ret = -1;
else
ApplyResource( globVal[i].id, ent, globVal[i].off );
}
if (ret < 0)
LogError( "Internal error: config reader supplied incomplete data\n" );
return ret;
}
struct dpyVals {
int id;
int off;
} dpyVal[] = {
CONF_CORE_LOCALS
};
int
LoadDisplayResources( struct display *d )
{
int i, ret;
char **ent;
if (Setjmp( cnftalk.errjmp ))
return -1; /* may memleak */
if ((ret = startConfig( GC_gDisplay, &d->cfg.dep, FALSE )) <= 0)
return ret;
GSendStr( d->name );
GSendStr( d->class2 );
LoadResources( &d->cfg );
/* Debug( "display(%s, %s) resources: %[*x\n", d->name, d->class2,
d->cfg.numCfgEnt, ((char **)d->cfg.data) + d->cfg.numCfgEnt );*/
ret = 1;
for (i = 0; i < as(dpyVal); i++) {
if (!(ent = FindCfgEnt( d, dpyVal[i].id )))
ret = -1;
else
ApplyResource( dpyVal[i].id, ent,
(char **)(((char *)d) + dpyVal[i].off) );
}
if (ret < 0)
LogError( "Internal error: config reader supplied incomplete data\n" );
return ret;
}
int
InitResources( char **argv )
{
originalArgv = argv;
cnftalk.pipe = &getter.pipe;
if (Setjmp( cnftalk.errjmp ))
return 0; /* may memleak */
return GetDeps();
}
static void
addServers( char **srv, int bType )
{
char *name, *class2;
const char *dtx, *cls;
struct display *d;
for (; *srv; srv++) {
if ((cls = strchr( *srv, '_' ))) {
if (!StrNDup( &name, *srv, cls - *srv ))
return;
if (!StrDup( &class2, cls )) {
free( name );
return;
}
} else {
if (!StrDup( &name, *srv ))
return;
class2 = 0;
}
if ((d = FindDisplayByName( name ))) {
if (d->class2)
free( d->class2 );
dtx = "existing";
} else {
if (!(d = NewDisplay( name ))) {
free( name );
if (class2)
free( class2 );
return;
}
dtx = "new";
}
d->stillThere = 1;
d->class2 = class2;
d->displayType = (*name == ':' ? dLocal : dForeign) | bType;
if ((bType & d_lifetime) == dReserve) {
if (d->status == notRunning)
d->status = reserve;
} else {
if (d->status == reserve)
d->status = notRunning;
}
Debug( "found %s %s%s display: %s %s\n", dtx,
((d->displayType & d_location) == dLocal) ? "local" : "foreign",
((d->displayType & d_lifetime) == dReserve) ? " reserve" : "",
d->name, d->class2 );
free( name );
}
}
void
ScanServers( void )
{
Debug( "ScanServers\n" );
addServers( staticServers, dFromFile | dPermanent );
addServers( reserveServers, dFromFile | dReserve );
}
|