Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtlogin / dpylist.c
1 /* (c) Copyright 1997 The Open Group */
2 /*                                                                      *
3  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
4  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
5  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
6  * (c) Copyright 1993, 1994 Novell, Inc.                                *
7  */
8 /*
9  * xdm - display manager daemon
10  *
11  * $TOG: dpylist.c /main/5 1997/03/14 13:44:46 barstow $
12  *
13  * Copyright 1988 Massachusetts Institute of Technology
14  *
15  * Permission to use, copy, modify, and distribute this software and its
16  * documentation for any purpose and without fee is hereby granted, provided
17  * that the above copyright notice appear in all copies and that both that
18  * copyright notice and this permission notice appear in supporting
19  * documentation, and that the name of M.I.T. not be used in advertising or
20  * publicity pertaining to distribution of the software without specific,
21  * written prior permission.  M.I.T. makes no representations about the
22  * suitability of this software for any purpose.  It is provided "as is"
23  * without express or implied warranty.
24  *
25  * Author:  Keith Packard, MIT X Consortium
26  */
27
28 /*
29  * a simple linked list of known displays
30  */
31
32 # include "dm.h"
33 # include "vgmsg.h"
34
35 struct display  *displays;
36
37
38 int 
39 AnyDisplaysLeft( void )
40 {
41         return displays != (struct display *) 0;
42 }
43
44 void
45 ForEachDisplay( void (*f)() )
46 {
47         struct display  *d, *next;
48
49         for (d = displays; d; d = next) {
50                 next = d->next;
51                 (*f) (d);
52         }
53 }
54
55 struct display * 
56 FindDisplayByName( char *name )
57 {
58         struct display  *d;
59
60         for (d = displays; d; d = d->next)
61                 if (!strcmp (name, d->name))
62                         return d;
63         return 0;
64 }
65
66 struct display *
67 FindDisplayByPid( int pid )
68 {
69         struct display  *d;
70
71         for (d = displays; d; d = d->next)
72                 if (pid == d->pid)
73                         return d;
74         return 0;
75 }
76
77 struct display * 
78 FindDisplayByServerPid( int serverPid )
79 {
80         struct display  *d;
81
82         for (d = displays; d; d = d->next)
83                 if (serverPid == d->serverPid)
84                         return d;
85         return 0;
86 }
87
88 struct display * 
89 FindDisplayBySessionID( CARD32 sessionID )
90 {
91     struct display      *d;
92
93     for (d = displays; d; d = d->next)
94         if (sessionID == d->sessionID)
95             return d;
96     return 0;
97 }
98
99 struct display * 
100 FindDisplayByAddress(struct sockaddr *addr, int addrlen,
101 #if NeedWidePrototypes
102         int displayNumber )
103 #else
104         CARD16 displayNumber )
105 #endif /* NeedWidePrototypes */
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 ((char *)d->from, d->fromlen, (char *)addr, addrlen))
113         {
114             return d;
115         }
116     return 0;
117 }
118
119 #define IfFree(x)  if (x) free ((char *) x)
120     
121 void
122 RemoveDisplay( struct display *old )
123 {
124     struct display      *d, *p;
125     char                **x;
126     int                       i;
127
128     extern int          wakeupTime;
129     
130     Debug("Removing display %s from display list.\n", old->name);
131     
132     p = 0;
133     for (d = displays; d; d = d->next) {
134         if (d == old) {
135             if (p)
136                 p->next = d->next;
137             else
138                 displays = d->next;
139             IfFree (d->name);
140             IfFree (d->class);
141             for (x = d->argv; x && *x; x++)
142                 IfFree (*x);
143             IfFree (d->argv);
144             IfFree (d->resources);
145             IfFree (d->xrdb);
146             IfFree (d->cpp);
147             IfFree (d->setup);
148             IfFree (d->startup);
149             IfFree (d->reset);
150             IfFree (d->session);
151             IfFree (d->userPath);
152             IfFree (d->systemPath);
153             IfFree (d->systemShell);
154             IfFree (d->failsafeClient);
155             IfFree (d->chooser);
156             if (d->authorizations)
157             {
158                 for (i = 0; i < d->authNum; i++)
159                     XauDisposeAuth (d->authorizations[i]);
160                 free ((char *) d->authorizations);
161             }
162             IfFree (d->clientAuthFile);
163             if (d->authFile)
164                 (void) unlink (d->authFile);
165             IfFree (d->authFile);
166             IfFree (d->userAuthDir);
167             IfFree (d->authNames);
168             IfFree (d->peer);
169             IfFree (d->from);
170             XdmcpDisposeARRAY8(&d->clientAddr);
171             IfFree (d->language);
172             IfFree (d->langList);
173             IfFree (d->utmpId);
174             IfFree (d->gettyLine);
175             IfFree (d->gettySpeed);
176             IfFree (d->environStr);
177             IfFree (d->verifyName);
178
179             /*
180              *  turn off polling if we are removing a suspended display...
181              */
182              
183             if ( d->status == suspended )
184                 wakeupTime = -1;
185
186
187             free ((char *) d);
188             break;
189         }
190         p = d;
191     }
192 }
193
194 struct display * 
195 NewDisplay( char *name, char *class )
196 {
197     struct display      *d;
198
199     d = (struct display *) malloc (sizeof (struct display));
200     if (!d) {
201         LogOutOfMem (ReadCatalog(MC_LOG_SET,MC_LOG_NEW_DPY,MC_DEF_LOG_NEW_DPY));
202         return 0;
203     }
204     d->next = displays;
205     d->name = malloc ((unsigned) (strlen (name) + 1));
206     if (!d->name) {
207         LogOutOfMem (ReadCatalog(MC_LOG_SET,MC_LOG_NEW_DPY,MC_DEF_LOG_NEW_DPY));
208         free ((char *) d);
209         return 0;
210     }
211     strcpy (d->name, name);
212     if (class)
213     {
214         d->class = malloc ((unsigned) (strlen (class) + 1));
215         if (!d->class) {
216             LogOutOfMem(
217                 ReadCatalog(MC_LOG_SET,MC_LOG_NEW_DPY,MC_DEF_LOG_NEW_DPY));
218             free (d->name);
219             free ((char *) d);
220             return 0;
221         }
222         strcpy (d->class, class);
223     }
224     else
225     {
226         d->class = (char *) 0;
227     }
228     /* initialize every field to avoid possible problems */
229     d->argv = 0;
230     d->status = notRunning;
231     d->pid = -1;
232     d->serverPid = -1;
233     d->state = NewEntry;
234     d->resources = NULL;
235     d->xrdb = NULL;
236     d->cpp = NULL;
237     d->setup = NULL;
238     d->startup = NULL;
239     d->reset = NULL;
240     d->session = NULL;
241     d->userPath = NULL;
242     d->systemPath = NULL;
243     d->systemShell = NULL;
244     d->failsafeClient = NULL;
245     d->chooser = NULL;
246     d->authorize = FALSE;
247     d->authorizations = NULL;
248     d->authNum = 0;
249     d->authNameNum = 0;
250     d->clientAuthFile = NULL;
251     d->authFile = NULL;
252     d->userAuthDir = NULL;
253     d->authNames = NULL;
254     d->authNameLens = NULL;
255     d->openDelay = 0;
256     d->openRepeat = 0;
257     d->openTimeout = 0;
258     d->startAttempts = 0;
259     d->startTries = 0;
260     d->terminateServer = 0;
261     d->grabTimeout = 0;
262     d->sessionID = 0;
263     d->peer = 0;
264     d->peerlen = 0;
265     d->from = 0;
266     d->fromlen = 0;
267     d->displayNumber = 0;
268     d->useChooser = 0;
269     d->clientAddr.data = NULL;
270     d->clientAddr.length = 0;
271     d->language = NULL;
272     d->langList = NULL;
273     d->utmpId = NULL;
274     d->gettyLine = NULL;
275     d->gettySpeed = NULL;
276     d->environStr = NULL;
277     d->dtlite = FALSE;
278     d->verifyName = NULL;
279     d->pmSearchPath = NULL;
280     d->bmSearchPath = NULL;
281     displays = d;
282     return d;
283 }
284