Bug Summary

File:os/connection.c
Location:line 432, column 22
Description:Call to 'malloc' has an allocation size of 0 bytes

Annotated Source Code

1/***********************************************************
2
3Copyright 1987, 1989, 1998 The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts.
26
27 All Rights Reserved
28
29Permission to use, copy, modify, and distribute this software and its
30documentation for any purpose and without fee is hereby granted,
31provided that the above copyright notice appear in all copies and that
32both that copyright notice and this permission notice appear in
33supporting documentation, and that the name of Digital not be
34used in advertising or publicity pertaining to distribution of the
35software without specific, written prior permission.
36
37DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
43SOFTWARE.
44
45******************************************************************/
46/*****************************************************************
47 * Stuff to create connections --- OS dependent
48 *
49 * EstablishNewConnections, CreateWellKnownSockets, ResetWellKnownSockets,
50 * CloseDownConnection, CheckConnections, AddEnabledDevice,
51 * RemoveEnabledDevice, OnlyListToOneClient,
52 * ListenToAllClients,
53 *
54 * (WaitForSomething is in its own file)
55 *
56 * In this implementation, a client socket table is not kept.
57 * Instead, what would be the index into the table is just the
58 * file descriptor of the socket. This won't work for if the
59 * socket ids aren't small nums (0 - 2^8)
60 *
61 *****************************************************************/
62
63#ifdef HAVE_DIX_CONFIG_H1
64#include <dix-config.h>
65#endif
66
67#ifdef WIN32
68#include <X11/Xwinsock.h>
69#endif
70#include <X11/X.h>
71#include <X11/Xproto.h>
72#define XSERV_t
73#define TRANS_SERVER
74#define TRANS_REOPEN
75#include <X11/Xtrans/Xtrans.h>
76#include <X11/Xtrans/Xtransint.h>
77#include <errno(*__error()).h>
78#include <signal.h>
79#include <stdio.h>
80#include <stdlib.h>
81
82#ifndef WIN32
83#include <sys/socket.h>
84
85#if defined(TCPCONN1) || defined(STREAMSCONN)
86#include <netinet/in.h>
87#include <arpa/inet.h>
88#ifdef apollo
89#ifndef NO_TCP_H
90#include <netinet/tcp.h>
91#endif
92#else
93#ifdef CSRG_BASED1
94#include <sys/param.h>
95#endif
96#include <netinet/tcp.h>
97#endif
98#include <arpa/inet.h>
99#endif
100
101#include <sys/uio.h>
102
103#endif /* WIN32 */
104#include "misc.h" /* for typedef of pointer */
105#include "osdep.h"
106#include <X11/Xpoll.h>
107#include "opaque.h"
108#include "dixstruct.h"
109#include "xace.h"
110
111#define Pid_tpid_t pid_t
112
113#ifdef HAVE_GETPEERUCRED
114#include <ucred.h>
115#include <zone.h>
116#else
117#define zoneid_tint int
118#endif
119
120#include "probes.h"
121
122static int lastfdesc; /* maximum file descriptor */
123
124fd_set WellKnownConnections; /* Listener mask */
125fd_set EnabledDevices; /* mask for input devices that are on */
126fd_set AllSockets; /* select on this */
127fd_set AllClients; /* available clients */
128fd_set LastSelectMask; /* mask returned from last select call */
129fd_set ClientsWithInput; /* clients with FULL requests in buffer */
130fd_set ClientsWriteBlocked; /* clients who cannot receive output */
131fd_set OutputPending; /* clients with reply/event data ready to go */
132int MaxClients = 0;
133Bool NewOutputPending; /* not yet attempted to write some new output */
134Bool AnyClientsWriteBlocked; /* true if some client blocked on write */
135Bool NoListenAll; /* Don't establish any listening sockets */
136
137static Bool RunFromSmartParent; /* send SIGUSR1 to parent process */
138Bool RunFromSigStopParent; /* send SIGSTOP to our own process; Upstart (or
139 equivalent) will send SIGCONT back. */
140static char dynamic_display[7]; /* display name */
141Bool PartialNetwork; /* continue even if unable to bind all addrs */
142static Pid_tpid_t ParentProcess;
143
144static Bool debug_conns = FALSE0;
145
146fd_set IgnoredClientsWithInput;
147static fd_set GrabImperviousClients;
148static fd_set SavedAllClients;
149static fd_set SavedAllSockets;
150static fd_set SavedClientsWithInput;
151int GrabInProgress = 0;
152
153#if !defined(WIN32)
154int *ConnectionTranslation = NULL((void*)0);
155#else
156/*
157 * On NT fds are not between 0 and MAXSOCKS, they are unrelated, and there is
158 * not even a known maximum value, so use something quite arbitrary for now.
159 * Do storage is a hash table of size 256. Collisions are handled in a linked
160 * list.
161 */
162
163#undef MAXSOCKS256
164#define MAXSOCKS256 500
165#undef MAXSELECT(sizeof(fd_set) * 8)
166#define MAXSELECT(sizeof(fd_set) * 8) 500
167
168struct _ct_node {
169 struct _ct_node *next;
170 int key;
171 int value;
172};
173
174struct _ct_node *ct_head[256];
175
176void
177InitConnectionTranslation(void)
178{
179 memset(ct_head, 0, sizeof(ct_head))__builtin___memset_chk (ct_head, 0, sizeof(ct_head), __builtin_object_size
(ct_head, 0))
;
180}
181
182int
183GetConnectionTranslation(int conn)
184{
185 struct _ct_node *node = ct_head[conn & 0xff];
186
187 while (node != NULL((void*)0)) {
188 if (node->key == conn)
189 return node->value;
190 node = node->next;
191 }
192 return 0;
193}
194
195void
196SetConnectionTranslation(int conn, int client)
197{
198 struct _ct_node **node = ct_head + (conn & 0xff);
199
200 if (client == 0) { /* remove entry */
201 while (*node != NULL((void*)0)) {
202 if ((*node)->key == conn) {
203 struct _ct_node *temp = *node;
204
205 *node = (*node)->next;
206 free(temp);
207 return;
208 }
209 node = &((*node)->next);
210 }
211 return;
212 }
213 else {
214 while (*node != NULL((void*)0)) {
215 if ((*node)->key == conn) {
216 (*node)->value = client;
217 return;
218 }
219 node = &((*node)->next);
220 }
221 *node = malloc(sizeof(struct _ct_node));
222 (*node)->next = NULL((void*)0);
223 (*node)->key = conn;
224 (*node)->value = client;
225 return;
226 }
227}
228
229void
230ClearConnectionTranslation(void)
231{
232 unsigned i;
233
234 for (i = 0; i < 256; i++) {
235 struct _ct_node *node = ct_head[i];
236
237 while (node != NULL((void*)0)) {
238 struct _ct_node *temp = node;
239
240 node = node->next;
241 free(temp);
242 }
243 }
244}
245#endif
246
247static XtransConnInfo *ListenTransConns = NULL((void*)0);
248static int *ListenTransFds = NULL((void*)0);
249static int ListenTransCount;
250
251static void ErrorConnMax(XtransConnInfo /* trans_conn */ );
252
253static XtransConnInfo
254lookup_trans_conn(int fd)
255{
256 if (ListenTransFds) {
257 int i;
258
259 for (i = 0; i < ListenTransCount; i++)
260 if (ListenTransFds[i] == fd)
261 return ListenTransConns[i];
262 }
263
264 return NULL((void*)0);
265}
266
267/* Set MaxClients and lastfdesc, and allocate ConnectionTranslation */
268
269void
270InitConnectionLimits(void)
271{
272 lastfdesc = -1;
273
274#ifndef __CYGWIN__
275
276#if !defined(XNO_SYSCONF) && defined(_SC_OPEN_MAX5)
277 lastfdesc = sysconf(_SC_OPEN_MAX5) - 1;
278#endif
279
280#ifdef HAVE_GETDTABLESIZE1
281 if (lastfdesc < 0)
282 lastfdesc = getdtablesize() - 1;
283#endif
284
285#ifdef _NFILE
286 if (lastfdesc < 0)
287 lastfdesc = _NFILE - 1;
288#endif
289
290#endif /* __CYGWIN__ */
291
292 /* This is the fallback */
293 if (lastfdesc < 0)
294 lastfdesc = MAXSOCKS256;
295
296 if (lastfdesc > MAXSELECT(sizeof(fd_set) * 8))
297 lastfdesc = MAXSELECT(sizeof(fd_set) * 8);
298
299 if (lastfdesc > MAXCLIENTS256) {
300 lastfdesc = MAXCLIENTS256;
301 if (debug_conns)
302 ErrorF("REACHED MAXIMUM CLIENTS LIMIT %d\n", MAXCLIENTS256);
303 }
304 MaxClients = lastfdesc;
305
306#ifdef DEBUG
307 ErrorF("InitConnectionLimits: MaxClients = %d\n", MaxClients);
308#endif
309
310#if !defined(WIN32)
311 if (!ConnectionTranslation)
312 ConnectionTranslation = (int *) xnfalloc(sizeof(int) * (lastfdesc + 1))XNFalloc((unsigned long)(sizeof(int) * (lastfdesc + 1)));
313#else
314 InitConnectionTranslation();
315#endif
316}
317
318/*
319 * If SIGUSR1 was set to SIG_IGN when the server started, assume that either
320 *
321 * a- The parent process is ignoring SIGUSR1
322 *
323 * or
324 *
325 * b- The parent process is expecting a SIGUSR1
326 * when the server is ready to accept connections
327 *
328 * In the first case, the signal will be harmless, in the second case,
329 * the signal will be quite useful.
330 */
331static void
332InitParentProcess(void)
333{
334#if !defined(WIN32)
335 OsSigHandlerPtr handler;
336
337 handler = OsSignal(SIGUSR130, SIG_IGN(void (*)(int))1);
338 if (handler == SIG_IGN(void (*)(int))1)
339 RunFromSmartParent = TRUE1;
340 OsSignal(SIGUSR130, handler);
341 ParentProcess = getppid();
342#endif
343}
344
345void
346NotifyParentProcess(void)
347{
348#if !defined(WIN32)
349 if (displayfd >= 0) {
350 if (write(displayfd, display, strlen(display)) != strlen(display))
351 FatalError("Cannot write display number to fd %d\n", displayfd);
352 if (write(displayfd, "\n", 1) != 1)
353 FatalError("Cannot write display number to fd %d\n", displayfd);
354 close(displayfd);
355 displayfd = -1;
356 }
357 if (RunFromSmartParent) {
358 if (ParentProcess > 1) {
359 kill(ParentProcess, SIGUSR130);
360 }
361 }
362 if (RunFromSigStopParent)
363 raise(SIGSTOP17);
364#endif
365}
366
367static Bool
368TryCreateSocket(int num, int *partial)
369{
370 char port[20];
371
372 snprintf(port, sizeof(port), "%d", num)__builtin___snprintf_chk (port, sizeof(port), 0, __builtin_object_size
(port, 2 > 1 ? 1 : 0), "%d", num)
;
373
374 return (_XSERVTransMakeAllCOTSServerListeners(port, partial,
375 &ListenTransCount,
376 &ListenTransConns) >= 0);
377}
378
379/*****************
380 * CreateWellKnownSockets
381 * At initialization, create the sockets to listen on for new clients.
382 *****************/
383
384void
385CreateWellKnownSockets(void)
386{
387 int i;
388 int partial;
389
390 FD_ZERO(&AllSockets)__builtin_bzero(&AllSockets, sizeof(*(&AllSockets)));
391 FD_ZERO(&AllClients)__builtin_bzero(&AllClients, sizeof(*(&AllClients)));
392 FD_ZERO(&LastSelectMask)__builtin_bzero(&LastSelectMask, sizeof(*(&LastSelectMask
)))
;
393 FD_ZERO(&ClientsWithInput)__builtin_bzero(&ClientsWithInput, sizeof(*(&ClientsWithInput
)))
;
394
395#if !defined(WIN32)
396 for (i = 0; i < MaxClients; i++)
1
Assuming 'i' is >= 'MaxClients'
2
Loop condition is false. Execution continues on line 402
397 ConnectionTranslation[i] = 0;
398#else
399 ClearConnectionTranslation();
400#endif
401
402 FD_ZERO(&WellKnownConnections)__builtin_bzero(&WellKnownConnections, sizeof(*(&WellKnownConnections
)))
;
403
404 /* display is initialized to "0" by main(). It is then set to the display
405 * number if specified on the command line. */
406
407 if (NoListenAll) {
3
Assuming 'NoListenAll' is not equal to 0
4
Taking true branch
408 ListenTransCount = 0;
409 }
410 else if ((displayfd < 0) || explicit_display) {
411 if (TryCreateSocket(atoi(display), &partial) &&
412 ListenTransCount >= 1)
413 if (!PartialNetwork && partial)
414 FatalError ("Failed to establish all listening sockets");
415 }
416 else { /* -displayfd and no explicit display number */
417 Bool found = 0;
418 for (i = 0; i < 65536 - X_TCP_PORT6000; i++) {
419 if (TryCreateSocket(i, &partial) && !partial) {
420 found = 1;
421 break;
422 }
423 else
424 CloseWellKnownConnections();
425 }
426 if (!found)
427 FatalError("Failed to find a socket to listen on");
428 snprintf(dynamic_display, sizeof(dynamic_display), "%d", i)__builtin___snprintf_chk (dynamic_display, sizeof(dynamic_display
), 0, __builtin_object_size (dynamic_display, 2 > 1 ? 1 : 0
), "%d", i)
;
429 display = dynamic_display;
430 }
431
432 ListenTransFds = malloc(ListenTransCount * sizeof (int));
5
Call to 'malloc' has an allocation size of 0 bytes
433
434 for (i = 0; i < ListenTransCount; i++) {
435 int fd = _XSERVTransGetConnectionNumber(ListenTransConns[i]);
436
437 ListenTransFds[i] = fd;
438 FD_SET(fd, &WellKnownConnections)do { int __fd = (fd); ((&WellKnownConnections)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)
(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))));
} while(0)
;
439
440 if (!_XSERVTransIsLocal(ListenTransConns[i]))
441 DefineSelf (fd);
442 }
443
444 if (!XFD_ANYSET(&WellKnownConnections)((((((1024) % ((sizeof(__int32_t) * 8))) == 0) ? ((1024) / ((
sizeof(__int32_t) * 8))) : (((1024) / ((sizeof(__int32_t) * 8
))) + 1)) > 0 && ((((&WellKnownConnections)->
fds_bits))[0])) || (((((1024) % ((sizeof(__int32_t) * 8))) ==
0) ? ((1024) / ((sizeof(__int32_t) * 8))) : (((1024) / ((sizeof
(__int32_t) * 8))) + 1)) > 1 && ((((&WellKnownConnections
)->fds_bits))[1])) || (((((1024) % ((sizeof(__int32_t) * 8
))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))) : (((1024) /
((sizeof(__int32_t) * 8))) + 1)) > 2 && ((((&
WellKnownConnections)->fds_bits))[2])) || (((((1024) % ((sizeof
(__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8)
)) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)) > 3 &&
((((&WellKnownConnections)->fds_bits))[3])) || (((((1024
) % ((sizeof(__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t
) * 8))) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)) > 4
&& ((((&WellKnownConnections)->fds_bits))[4])
) || (((((1024) % ((sizeof(__int32_t) * 8))) == 0) ? ((1024) /
((sizeof(__int32_t) * 8))) : (((1024) / ((sizeof(__int32_t) *
8))) + 1)) > 5 && ((((&WellKnownConnections)->
fds_bits))[5])) || (((((1024) % ((sizeof(__int32_t) * 8))) ==
0) ? ((1024) / ((sizeof(__int32_t) * 8))) : (((1024) / ((sizeof
(__int32_t) * 8))) + 1)) > 6 && ((((&WellKnownConnections
)->fds_bits))[6])) || (((((1024) % ((sizeof(__int32_t) * 8
))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))) : (((1024) /
((sizeof(__int32_t) * 8))) + 1)) > 7 && ((((&
WellKnownConnections)->fds_bits))[7])) || (((((1024) % ((sizeof
(__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8)
)) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)) > 8 &&
((((&WellKnownConnections)->fds_bits))[8])) || (((((1024
) % ((sizeof(__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t
) * 8))) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)) > 9
&& ((((&WellKnownConnections)->fds_bits))[9])
) || (((((1024) % ((sizeof(__int32_t) * 8))) == 0) ? ((1024) /
((sizeof(__int32_t) * 8))) : (((1024) / ((sizeof(__int32_t) *
8))) + 1)) > 10 && ((((&WellKnownConnections)
->fds_bits))[10])) || (((((1024) % ((sizeof(__int32_t) * 8
))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))) : (((1024) /
((sizeof(__int32_t) * 8))) + 1)) > 11 && ((((&
WellKnownConnections)->fds_bits))[11])) || (((((1024) % ((
sizeof(__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t
) * 8))) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)) > 12
&& ((((&WellKnownConnections)->fds_bits))[12]
)) || (((((1024) % ((sizeof(__int32_t) * 8))) == 0) ? ((1024)
/ ((sizeof(__int32_t) * 8))) : (((1024) / ((sizeof(__int32_t
) * 8))) + 1)) > 13 && ((((&WellKnownConnections
)->fds_bits))[13])) || (((((1024) % ((sizeof(__int32_t) * 8
))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))) : (((1024) /
((sizeof(__int32_t) * 8))) + 1)) > 14 && ((((&
WellKnownConnections)->fds_bits))[14])) || (((((1024) % ((
sizeof(__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t
) * 8))) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)) > 15
&& ((((&WellKnownConnections)->fds_bits))[15]
)))
&& !NoListenAll)
445 FatalError
446 ("Cannot establish any listening sockets - Make sure an X server isn't already running");
447
448#if !defined(WIN32)
449 OsSignal(SIGPIPE13, SIG_IGN(void (*)(int))1);
450 OsSignal(SIGHUP1, AutoResetServer);
451#endif
452 OsSignal(SIGINT2, GiveUp);
453 OsSignal(SIGTERM15, GiveUp);
454 XFD_COPYSET(&WellKnownConnections, &AllSockets){ int __i__; for (__i__ = 0; __i__ < ((((1024) % ((sizeof(
__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))
) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)); __i__++) (((
(&AllSockets))->fds_bits))[__i__] = ((((&WellKnownConnections
))->fds_bits))[__i__]; }
;
455 ResetHosts(display);
456
457 InitParentProcess();
458
459#ifdef XDMCP1
460 XdmcpInit();
461#endif
462}
463
464void
465ResetWellKnownSockets(void)
466{
467 int i;
468
469 ResetOsBuffers();
470
471 for (i = 0; i < ListenTransCount; i++) {
472 int status = _XSERVTransResetListener(ListenTransConns[i]);
473
474 if (status != TRANS_RESET_NOOP1) {
475 if (status == TRANS_RESET_FAILURE3) {
476 /*
477 * ListenTransConns[i] freed by xtrans.
478 * Remove it from out list.
479 */
480
481 FD_CLR(ListenTransFds[i], &WellKnownConnections)do { int __fd = (ListenTransFds[i]); ((&WellKnownConnections
)->fds_bits[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &=
~((__int32_t)(1<<((unsigned long)__fd % (sizeof(__int32_t
) * 8))))); } while(0)
;
482 ListenTransFds[i] = ListenTransFds[ListenTransCount - 1];
483 ListenTransConns[i] = ListenTransConns[ListenTransCount - 1];
484 ListenTransCount -= 1;
485 i -= 1;
486 }
487 else if (status == TRANS_RESET_NEW_FD2) {
488 /*
489 * A new file descriptor was allocated (the old one was closed)
490 */
491
492 int newfd = _XSERVTransGetConnectionNumber(ListenTransConns[i]);
493
494 FD_CLR(ListenTransFds[i], &WellKnownConnections)do { int __fd = (ListenTransFds[i]); ((&WellKnownConnections
)->fds_bits[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &=
~((__int32_t)(1<<((unsigned long)__fd % (sizeof(__int32_t
) * 8))))); } while(0)
;
495 ListenTransFds[i] = newfd;
496 FD_SET(newfd, &WellKnownConnections)do { int __fd = (newfd); ((&WellKnownConnections)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)
(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))));
} while(0)
;
497 }
498 }
499 }
500
501 ResetAuthorization();
502 ResetHosts(display);
503 /*
504 * restart XDMCP
505 */
506#ifdef XDMCP1
507 XdmcpReset();
508#endif
509}
510
511void
512CloseWellKnownConnections(void)
513{
514 int i;
515
516 for (i = 0; i < ListenTransCount; i++) {
517 if (ListenTransConns[i] != NULL((void*)0)) {
518 _XSERVTransClose(ListenTransConns[i]);
519 ListenTransConns[i] = NULL((void*)0);
520 }
521 }
522 ListenTransCount = 0;
523}
524
525static void
526AuthAudit(ClientPtr client, Bool letin,
527 struct sockaddr *saddr, int len,
528 unsigned int proto_n, char *auth_proto, int auth_id)
529{
530 char addr[128];
531 char client_uid_string[64];
532 LocalClientCredRec *lcc;
533
534#ifdef XSERVER_DTRACE1
535 pid_t client_pid = -1;
536 zoneid_tint client_zid = -1;
537#endif
538
539 if (!len)
540 strlcpy(addr, "local host", sizeof(addr))__builtin___strlcpy_chk (addr, "local host", sizeof(addr), __builtin_object_size
(addr, 2 > 1 ? 1 : 0))
;
541 else
542 switch (saddr->sa_family) {
543 case AF_UNSPEC0:
544#if defined(UNIXCONN1) || defined(LOCALCONN)
545 case AF_UNIX1:
546#endif
547 strlcpy(addr, "local host", sizeof(addr))__builtin___strlcpy_chk (addr, "local host", sizeof(addr), __builtin_object_size
(addr, 2 > 1 ? 1 : 0))
;
548 break;
549#if defined(TCPCONN1) || defined(STREAMSCONN)
550 case AF_INET2:
551 snprintf(addr, sizeof(addr), "IP %s",__builtin___snprintf_chk (addr, sizeof(addr), 0, __builtin_object_size
(addr, 2 > 1 ? 1 : 0), "IP %s", inet_ntoa(((struct sockaddr_in
*) saddr)->sin_addr))
552 inet_ntoa(((struct sockaddr_in *) saddr)->sin_addr))__builtin___snprintf_chk (addr, sizeof(addr), 0, __builtin_object_size
(addr, 2 > 1 ? 1 : 0), "IP %s", inet_ntoa(((struct sockaddr_in
*) saddr)->sin_addr))
;
553 break;
554#if defined(IPv61) && defined(AF_INET630)
555 case AF_INET630:{
556 char ipaddr[INET6_ADDRSTRLEN46];
557
558 inet_ntop(AF_INET630, &((struct sockaddr_in6 *) saddr)->sin6_addr,
559 ipaddr, sizeof(ipaddr));
560 snprintf(addr, sizeof(addr), "IP %s", ipaddr)__builtin___snprintf_chk (addr, sizeof(addr), 0, __builtin_object_size
(addr, 2 > 1 ? 1 : 0), "IP %s", ipaddr)
;
561 }
562 break;
563#endif
564#endif
565 default:
566 strlcpy(addr, "unknown address", sizeof(addr))__builtin___strlcpy_chk (addr, "unknown address", sizeof(addr
), __builtin_object_size (addr, 2 > 1 ? 1 : 0))
;
567 }
568
569 if (GetLocalClientCreds(client, &lcc) != -1) {
570 int slen; /* length written to client_uid_string */
571
572 strcpy(client_uid_string, " ( ")__builtin___strcpy_chk (client_uid_string, " ( ", __builtin_object_size
(client_uid_string, 2 > 1 ? 1 : 0))
;
573 slen = 3;
574
575 if (lcc->fieldsSet & LCC_UID_SET(1 << 0)) {
576 snprintf(client_uid_string + slen,__builtin___snprintf_chk (client_uid_string + slen, sizeof(client_uid_string
) - slen, 0, __builtin_object_size (client_uid_string + slen,
2 > 1 ? 1 : 0), "uid=%ld ", (long) lcc->euid)
577 sizeof(client_uid_string) - slen,__builtin___snprintf_chk (client_uid_string + slen, sizeof(client_uid_string
) - slen, 0, __builtin_object_size (client_uid_string + slen,
2 > 1 ? 1 : 0), "uid=%ld ", (long) lcc->euid)
578 "uid=%ld ", (long) lcc->euid)__builtin___snprintf_chk (client_uid_string + slen, sizeof(client_uid_string
) - slen, 0, __builtin_object_size (client_uid_string + slen,
2 > 1 ? 1 : 0), "uid=%ld ", (long) lcc->euid)
;
579 slen = strlen(client_uid_string);
580 }
581
582 if (lcc->fieldsSet & LCC_GID_SET(1 << 1)) {
583 snprintf(client_uid_string + slen,__builtin___snprintf_chk (client_uid_string + slen, sizeof(client_uid_string
) - slen, 0, __builtin_object_size (client_uid_string + slen,
2 > 1 ? 1 : 0), "gid=%ld ", (long) lcc->egid)
584 sizeof(client_uid_string) - slen,__builtin___snprintf_chk (client_uid_string + slen, sizeof(client_uid_string
) - slen, 0, __builtin_object_size (client_uid_string + slen,
2 > 1 ? 1 : 0), "gid=%ld ", (long) lcc->egid)
585 "gid=%ld ", (long) lcc->egid)__builtin___snprintf_chk (client_uid_string + slen, sizeof(client_uid_string
) - slen, 0, __builtin_object_size (client_uid_string + slen,
2 > 1 ? 1 : 0), "gid=%ld ", (long) lcc->egid)
;
586 slen = strlen(client_uid_string);
587 }
588
589 if (lcc->fieldsSet & LCC_PID_SET(1 << 2)) {
590#ifdef XSERVER_DTRACE1
591 client_pid = lcc->pid;
592#endif
593 snprintf(client_uid_string + slen,__builtin___snprintf_chk (client_uid_string + slen, sizeof(client_uid_string
) - slen, 0, __builtin_object_size (client_uid_string + slen,
2 > 1 ? 1 : 0), "pid=%ld ", (long) lcc->pid)
594 sizeof(client_uid_string) - slen,__builtin___snprintf_chk (client_uid_string + slen, sizeof(client_uid_string
) - slen, 0, __builtin_object_size (client_uid_string + slen,
2 > 1 ? 1 : 0), "pid=%ld ", (long) lcc->pid)
595 "pid=%ld ", (long) lcc->pid)__builtin___snprintf_chk (client_uid_string + slen, sizeof(client_uid_string
) - slen, 0, __builtin_object_size (client_uid_string + slen,
2 > 1 ? 1 : 0), "pid=%ld ", (long) lcc->pid)
;
596 slen = strlen(client_uid_string);
597 }
598
599 if (lcc->fieldsSet & LCC_ZID_SET(1 << 3)) {
600#ifdef XSERVER_DTRACE1
601 client_zid = lcc->zoneid;
602#endif
603 snprintf(client_uid_string + slen,__builtin___snprintf_chk (client_uid_string + slen, sizeof(client_uid_string
) - slen, 0, __builtin_object_size (client_uid_string + slen,
2 > 1 ? 1 : 0), "zoneid=%ld ", (long) lcc->zoneid)
604 sizeof(client_uid_string) - slen,__builtin___snprintf_chk (client_uid_string + slen, sizeof(client_uid_string
) - slen, 0, __builtin_object_size (client_uid_string + slen,
2 > 1 ? 1 : 0), "zoneid=%ld ", (long) lcc->zoneid)
605 "zoneid=%ld ", (long) lcc->zoneid)__builtin___snprintf_chk (client_uid_string + slen, sizeof(client_uid_string
) - slen, 0, __builtin_object_size (client_uid_string + slen,
2 > 1 ? 1 : 0), "zoneid=%ld ", (long) lcc->zoneid)
;
606 slen = strlen(client_uid_string);
607 }
608
609 snprintf(client_uid_string + slen, sizeof(client_uid_string) - slen,__builtin___snprintf_chk (client_uid_string + slen, sizeof(client_uid_string
) - slen, 0, __builtin_object_size (client_uid_string + slen,
2 > 1 ? 1 : 0), ")")
610 ")")__builtin___snprintf_chk (client_uid_string + slen, sizeof(client_uid_string
) - slen, 0, __builtin_object_size (client_uid_string + slen,
2 > 1 ? 1 : 0), ")")
;
611 FreeLocalClientCreds(lcc);
612 }
613 else {
614 client_uid_string[0] = '\0';
615 }
616
617#ifdef XSERVER_DTRACE1
618 XSERVER_CLIENT_AUTH(client->index, addr, client_pid, client_zid)do { __asm__ volatile(".reference " "___dtrace_typedefs$Xserver$v2$636f6e73745f646f75626c655f70$636f6e73745f75696e74385f70"
); __dtrace_probe$Xserver$client__auth$v1$696e74$63686172202a$75696e7433325f74$75696e7433325f74
(client->index, addr, client_pid, client_zid); __asm__ volatile
(".reference " "___dtrace_stability$Xserver$v1$4_4_5_1_1_0_1_1_0_4_4_5_4_4_5"
); } while (0)
;
619#endif
620 if (auditTrailLevel > 1) {
621 if (proto_n)
622 AuditF("client %d %s from %s%s\n Auth name: %.*s ID: %d\n",
623 client->index, letin ? "connected" : "rejected", addr,
624 client_uid_string, (int) proto_n, auth_proto, auth_id);
625 else
626 AuditF("client %d %s from %s%s\n",
627 client->index, letin ? "connected" : "rejected", addr,
628 client_uid_string);
629
630 }
631}
632
633XID
634AuthorizationIDOfClient(ClientPtr client)
635{
636 if (client->osPrivate)
637 return ((OsCommPtr) client->osPrivate)->auth_id;
638 else
639 return None0L;
640}
641
642/*****************************************************************
643 * ClientAuthorized
644 *
645 * Sent by the client at connection setup:
646 * typedef struct _xConnClientPrefix {
647 * CARD8 byteOrder;
648 * BYTE pad;
649 * CARD16 majorVersion, minorVersion;
650 * CARD16 nbytesAuthProto;
651 * CARD16 nbytesAuthString;
652 * } xConnClientPrefix;
653 *
654 * It is hoped that eventually one protocol will be agreed upon. In the
655 * mean time, a server that implements a different protocol than the
656 * client expects, or a server that only implements the host-based
657 * mechanism, will simply ignore this information.
658 *
659 *****************************************************************/
660
661const char *
662ClientAuthorized(ClientPtr client,
663 unsigned int proto_n, char *auth_proto,
664 unsigned int string_n, char *auth_string)
665{
666 OsCommPtr priv;
667 Xtransaddr *from = NULL((void*)0);
668 int family;
669 int fromlen;
670 XID auth_id;
671 const char *reason = NULL((void*)0);
672 XtransConnInfo trans_conn;
673
674 priv = (OsCommPtr) client->osPrivate;
675 trans_conn = priv->trans_conn;
676
677 /* Allow any client to connect without authorization on a launchd socket,
678 because it is securely created -- this prevents a race condition on launch */
679 if (trans_conn->flags & TRANS_NOXAUTH(1<<6)) {
680 auth_id = (XID) 0L;
681 }
682 else {
683 auth_id =
684 CheckAuthorization(proto_n, auth_proto, string_n, auth_string,
685 client, &reason);
686 }
687
688 if (auth_id == (XID) ~0L) {
689 if (_XSERVTransGetPeerAddr(trans_conn, &family, &fromlen, &from) != -1) {
690 if (InvalidHost((struct sockaddr *) from, fromlen, client))
691 AuthAudit(client, FALSE0, (struct sockaddr *) from,
692 fromlen, proto_n, auth_proto, auth_id);
693 else {
694 auth_id = (XID) 0;
695#ifdef XSERVER_DTRACE1
696 if ((auditTrailLevel > 1) || XSERVER_CLIENT_AUTH_ENABLED()({ int _r = __dtrace_isenabled$Xserver$client__auth$v1(); __asm__
volatile(""); _r; })
)
697#else
698 if (auditTrailLevel > 1)
699#endif
700 AuthAudit(client, TRUE1,
701 (struct sockaddr *) from, fromlen,
702 proto_n, auth_proto, auth_id);
703 }
704
705 free(from);
706 }
707
708 if (auth_id == (XID) ~0L) {
709 if (reason)
710 return reason;
711 else
712 return "Client is not authorized to connect to Server";
713 }
714 }
715#ifdef XSERVER_DTRACE1
716 else if ((auditTrailLevel > 1) || XSERVER_CLIENT_AUTH_ENABLED()({ int _r = __dtrace_isenabled$Xserver$client__auth$v1(); __asm__
volatile(""); _r; })
)
717#else
718 else if (auditTrailLevel > 1)
719#endif
720 {
721 if (_XSERVTransGetPeerAddr(trans_conn, &family, &fromlen, &from) != -1) {
722 AuthAudit(client, TRUE1, (struct sockaddr *) from, fromlen,
723 proto_n, auth_proto, auth_id);
724
725 free(from);
726 }
727 }
728 priv->auth_id = auth_id;
729 priv->conn_time = 0;
730
731#ifdef XDMCP1
732 /* indicate to Xdmcp protocol that we've opened new client */
733 XdmcpOpenDisplay(priv->fd);
734#endif /* XDMCP */
735
736 XaceHook(XACE_AUTH_AVAIL13, client, auth_id);
737
738 /* At this point, if the client is authorized to change the access control
739 * list, we should getpeername() information, and add the client to
740 * the selfhosts list. It's not really the host machine, but the
741 * true purpose of the selfhosts list is to see who may change the
742 * access control list.
743 */
744 return ((char *) NULL((void*)0));
745}
746
747static ClientPtr
748AllocNewConnection(XtransConnInfo trans_conn, int fd, CARD32 conn_time)
749{
750 OsCommPtr oc;
751 ClientPtr client;
752
753 if (
754#ifndef WIN32
755 fd >= lastfdesc
756#else
757 XFD_SETCOUNT(&AllClients) >= MaxClients
758#endif
759 )
760 return NullClient((ClientPtr) 0);
761 oc = malloc(sizeof(OsCommRec));
762 if (!oc)
763 return NullClient((ClientPtr) 0);
764 oc->trans_conn = trans_conn;
765 oc->fd = fd;
766 oc->input = (ConnectionInputPtr) NULL((void*)0);
767 oc->output = (ConnectionOutputPtr) NULL((void*)0);
768 oc->auth_id = None0L;
769 oc->conn_time = conn_time;
770 if (!(client = NextAvailableClient((void *) oc))) {
771 free(oc);
772 return NullClient((ClientPtr) 0);
773 }
774 client->local = ComputeLocalClient(client);
775#if !defined(WIN32)
776 ConnectionTranslation[fd] = client->index;
777#else
778 SetConnectionTranslation(fd, client->index);
779#endif
780 if (GrabInProgress) {
781 FD_SET(fd, &SavedAllClients)do { int __fd = (fd); ((&SavedAllClients)->fds_bits[(unsigned
long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)(1<<
((unsigned long)__fd % (sizeof(__int32_t) * 8))))); } while(0
)
;
782 FD_SET(fd, &SavedAllSockets)do { int __fd = (fd); ((&SavedAllSockets)->fds_bits[(unsigned
long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)(1<<
((unsigned long)__fd % (sizeof(__int32_t) * 8))))); } while(0
)
;
783 }
784 else {
785 FD_SET(fd, &AllClients)do { int __fd = (fd); ((&AllClients)->fds_bits[(unsigned
long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)(1<<
((unsigned long)__fd % (sizeof(__int32_t) * 8))))); } while(0
)
;
786 FD_SET(fd, &AllSockets)do { int __fd = (fd); ((&AllSockets)->fds_bits[(unsigned
long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)(1<<
((unsigned long)__fd % (sizeof(__int32_t) * 8))))); } while(0
)
;
787 }
788
789#ifdef DEBUG
790 ErrorF("AllocNewConnection: client index = %d, socket fd = %d\n",
791 client->index, fd);
792#endif
793#ifdef XSERVER_DTRACE1
794 XSERVER_CLIENT_CONNECT(client->index, fd)do { __asm__ volatile(".reference " "___dtrace_typedefs$Xserver$v2$636f6e73745f646f75626c655f70$636f6e73745f75696e74385f70"
); __dtrace_probe$Xserver$client__connect$v1$696e74$696e74(client
->index, fd); __asm__ volatile(".reference " "___dtrace_stability$Xserver$v1$4_4_5_1_1_0_1_1_0_4_4_5_4_4_5"
); } while (0)
;
795#endif
796
797 return client;
798}
799
800/*****************
801 * EstablishNewConnections
802 * If anyone is waiting on listened sockets, accept them.
803 * Returns a mask with indices of new clients. Updates AllClients
804 * and AllSockets.
805 *****************/
806
807 /*ARGSUSED*/ Bool
808EstablishNewConnections(ClientPtr clientUnused, void *closure)
809{
810 fd_set readyconnections; /* set of listeners that are ready */
811 int curconn; /* fd of listener that's ready */
812 register int newconn; /* fd of new client */
813 CARD32 connect_time;
814 register int i;
815 register ClientPtr client;
816 register OsCommPtr oc;
817 fd_set tmask;
818
819 XFD_ANDSET(&tmask, (fd_set *) closure, &WellKnownConnections){ int __i__; for (__i__ = 0; __i__ < ((((1024) % ((sizeof(
__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))
) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)); __i__++) (((
(&tmask))->fds_bits))[__i__] = (((((((fd_set *) closure
))->fds_bits))[__i__]) & (((((&WellKnownConnections
))->fds_bits))[__i__])); }
;
820 XFD_COPYSET(&tmask, &readyconnections){ int __i__; for (__i__ = 0; __i__ < ((((1024) % ((sizeof(
__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))
) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)); __i__++) (((
(&readyconnections))->fds_bits))[__i__] = ((((&tmask
))->fds_bits))[__i__]; }
;
821 if (!XFD_ANYSET(&readyconnections)((((((1024) % ((sizeof(__int32_t) * 8))) == 0) ? ((1024) / ((
sizeof(__int32_t) * 8))) : (((1024) / ((sizeof(__int32_t) * 8
))) + 1)) > 0 && ((((&readyconnections)->fds_bits
))[0])) || (((((1024) % ((sizeof(__int32_t) * 8))) == 0) ? ((
1024) / ((sizeof(__int32_t) * 8))) : (((1024) / ((sizeof(__int32_t
) * 8))) + 1)) > 1 && ((((&readyconnections)->
fds_bits))[1])) || (((((1024) % ((sizeof(__int32_t) * 8))) ==
0) ? ((1024) / ((sizeof(__int32_t) * 8))) : (((1024) / ((sizeof
(__int32_t) * 8))) + 1)) > 2 && ((((&readyconnections
)->fds_bits))[2])) || (((((1024) % ((sizeof(__int32_t) * 8
))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))) : (((1024) /
((sizeof(__int32_t) * 8))) + 1)) > 3 && ((((&
readyconnections)->fds_bits))[3])) || (((((1024) % ((sizeof
(__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8)
)) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)) > 4 &&
((((&readyconnections)->fds_bits))[4])) || (((((1024)
% ((sizeof(__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t
) * 8))) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)) > 5
&& ((((&readyconnections)->fds_bits))[5])) ||
(((((1024) % ((sizeof(__int32_t) * 8))) == 0) ? ((1024) / ((
sizeof(__int32_t) * 8))) : (((1024) / ((sizeof(__int32_t) * 8
))) + 1)) > 6 && ((((&readyconnections)->fds_bits
))[6])) || (((((1024) % ((sizeof(__int32_t) * 8))) == 0) ? ((
1024) / ((sizeof(__int32_t) * 8))) : (((1024) / ((sizeof(__int32_t
) * 8))) + 1)) > 7 && ((((&readyconnections)->
fds_bits))[7])) || (((((1024) % ((sizeof(__int32_t) * 8))) ==
0) ? ((1024) / ((sizeof(__int32_t) * 8))) : (((1024) / ((sizeof
(__int32_t) * 8))) + 1)) > 8 && ((((&readyconnections
)->fds_bits))[8])) || (((((1024) % ((sizeof(__int32_t) * 8
))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))) : (((1024) /
((sizeof(__int32_t) * 8))) + 1)) > 9 && ((((&
readyconnections)->fds_bits))[9])) || (((((1024) % ((sizeof
(__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8)
)) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)) > 10 &&
((((&readyconnections)->fds_bits))[10])) || (((((1024
) % ((sizeof(__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t
) * 8))) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)) > 11
&& ((((&readyconnections)->fds_bits))[11])) ||
(((((1024) % ((sizeof(__int32_t) * 8))) == 0) ? ((1024) / ((
sizeof(__int32_t) * 8))) : (((1024) / ((sizeof(__int32_t) * 8
))) + 1)) > 12 && ((((&readyconnections)->fds_bits
))[12])) || (((((1024) % ((sizeof(__int32_t) * 8))) == 0) ? (
(1024) / ((sizeof(__int32_t) * 8))) : (((1024) / ((sizeof(__int32_t
) * 8))) + 1)) > 13 && ((((&readyconnections)->
fds_bits))[13])) || (((((1024) % ((sizeof(__int32_t) * 8))) ==
0) ? ((1024) / ((sizeof(__int32_t) * 8))) : (((1024) / ((sizeof
(__int32_t) * 8))) + 1)) > 14 && ((((&readyconnections
)->fds_bits))[14])) || (((((1024) % ((sizeof(__int32_t) * 8
))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))) : (((1024) /
((sizeof(__int32_t) * 8))) + 1)) > 15 && ((((&
readyconnections)->fds_bits))[15])))
)
822 return TRUE1;
823 connect_time = GetTimeInMillis();
824 /* kill off stragglers */
825 for (i = 1; i < currentMaxClients; i++) {
826 if ((client = clients[i])) {
827 oc = (OsCommPtr) (client->osPrivate);
828 if ((oc && (oc->conn_time != 0) &&
829 (connect_time - oc->conn_time) >= TimeOutValue) ||
830 (client->noClientException != Success0 && !client->clientGone))
831 CloseDownClient(client);
832 }
833 }
834#ifndef WIN32
835 for (i = 0; i < howmany(XFD_SETSIZE, NFDBITS)((((512) % ((sizeof(__int32_t) * 8))) == 0) ? ((512) / ((sizeof
(__int32_t) * 8))) : (((512) / ((sizeof(__int32_t) * 8))) + 1
))
; i++) {
836 while (readyconnections.fds_bits[i])
837#else
838 for (i = 0; i < XFD_SETCOUNT(&readyconnections); i++)
839#endif
840 {
841 XtransConnInfo trans_conn, new_trans_conn;
842 int status;
843
844#ifndef WIN32
845 curconn = mffs(readyconnections.fds_bits[i]) - 1;
846 readyconnections.fds_bits[i] &= ~((fd_mask) 1 << curconn);
847 curconn += (i * (sizeof(fd_mask) * 8));
848#else
849 curconn = XFD_FD(&readyconnections, i);
850#endif
851
852 if ((trans_conn = lookup_trans_conn(curconn)) == NULL((void*)0))
853 continue;
854
855 if ((new_trans_conn = _XSERVTransAccept(trans_conn, &status)) == NULL((void*)0))
856 continue;
857
858 newconn = _XSERVTransGetConnectionNumber(new_trans_conn);
859
860 if (newconn < lastfdesc) {
861 int clientid;
862
863#if !defined(WIN32)
864 clientid = ConnectionTranslation[newconn];
865#else
866 clientid = GetConnectionTranslation(newconn);
867#endif
868 if (clientid && (client = clients[clientid]))
869 CloseDownClient(client);
870 }
871
872 _XSERVTransSetOption(new_trans_conn, TRANS_NONBLOCKING1, 1);
873
874 if (trans_conn->flags & TRANS_NOXAUTH(1<<6))
875 new_trans_conn->flags = new_trans_conn->flags | TRANS_NOXAUTH(1<<6);
876
877 if (!AllocNewConnection(new_trans_conn, newconn, connect_time)) {
878 ErrorConnMax(new_trans_conn);
879 _XSERVTransClose(new_trans_conn);
880 }
881 }
882#ifndef WIN32
883}
884#endif
885return TRUE1;
886}
887
888#define NOROOM"Maximum number of clients reached" "Maximum number of clients reached"
889
890/************
891 * ErrorConnMax
892 * Fail a connection due to lack of client or file descriptor space
893 ************/
894
895#define BOTIMEOUT200 200 /* in milliseconds */
896
897static void
898ErrorConnMax(XtransConnInfo trans_conn)
899{
900 int fd = _XSERVTransGetConnectionNumber(trans_conn);
901 xConnSetupPrefix csp;
902 char pad[3] = { 0, 0, 0 };
903 struct iovec iov[3];
904 char order = 0;
905 int whichbyte = 1;
906 struct timeval waittime;
907 fd_set mask;
908
909 /* if these seems like a lot of trouble to go to, it probably is */
910 waittime.tv_sec = BOTIMEOUT200 / MILLI_PER_SECOND(1000);
911 waittime.tv_usec = (BOTIMEOUT200 % MILLI_PER_SECOND(1000)) *
912 (1000000 / MILLI_PER_SECOND(1000));
913 FD_ZERO(&mask)__builtin_bzero(&mask, sizeof(*(&mask)));
914 FD_SET(fd, &mask)do { int __fd = (fd); ((&mask)->fds_bits[(unsigned long
)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)(1<<((unsigned
long)__fd % (sizeof(__int32_t) * 8))))); } while(0)
;
915 (void) Select(fd + 1, &mask, NULL, NULL, &waittime)select(fd + 1,(fd_set*)&mask,(fd_set*)((void*)0),(fd_set*
)((void*)0),(struct timeval*)&waittime)
;
916 /* try to read the byte-order of the connection */
917 (void) _XSERVTransRead(trans_conn, &order, 1);
918 if (order == 'l' || order == 'B' || order == 'r' || order == 'R') {
919 csp.success = xFalse0;
920 csp.lengthReason = sizeof(NOROOM"Maximum number of clients reached") - 1;
921 csp.length = (sizeof(NOROOM"Maximum number of clients reached") + 2) >> 2;
922 csp.majorVersion = X_PROTOCOL11;
923 csp.minorVersion = X_PROTOCOL_REVISION0;
924 if (((*(char *) &whichbyte) && (order == 'B' || order == 'R')) ||
925 (!(*(char *) &whichbyte) && (order == 'l' || order == 'r'))) {
926 swaps(&csp.majorVersion)do { if (sizeof(*(&csp.majorVersion)) != 2) wrong_size();
if (__builtin_constant_p((uintptr_t)(&csp.majorVersion) &
1) && ((uintptr_t)(&csp.majorVersion) & 1) ==
0) *(&csp.majorVersion) = lswaps(*(&csp.majorVersion
)); else swap_uint16((uint16_t *)(&csp.majorVersion)); } while
(0)
;
927 swaps(&csp.minorVersion)do { if (sizeof(*(&csp.minorVersion)) != 2) wrong_size();
if (__builtin_constant_p((uintptr_t)(&csp.minorVersion) &
1) && ((uintptr_t)(&csp.minorVersion) & 1) ==
0) *(&csp.minorVersion) = lswaps(*(&csp.minorVersion
)); else swap_uint16((uint16_t *)(&csp.minorVersion)); } while
(0)
;
928 swaps(&csp.length)do { if (sizeof(*(&csp.length)) != 2) wrong_size(); if (__builtin_constant_p
((uintptr_t)(&csp.length) & 1) && ((uintptr_t
)(&csp.length) & 1) == 0) *(&csp.length) = lswaps
(*(&csp.length)); else swap_uint16((uint16_t *)(&csp.
length)); } while (0)
;
929 }
930 iov[0].iov_len = sz_xConnSetupPrefix8;
931 iov[0].iov_base = (char *) &csp;
932 iov[1].iov_len = csp.lengthReason;
933 iov[1].iov_base = (void *) NOROOM"Maximum number of clients reached";
934 iov[2].iov_len = (4 - (csp.lengthReason & 3)) & 3;
935 iov[2].iov_base = pad;
936 (void) _XSERVTransWritev(trans_conn, iov, 3);
937 }
938}
939
940/************
941 * CloseDownFileDescriptor:
942 * Remove this file descriptor and it's I/O buffers, etc.
943 ************/
944
945static void
946CloseDownFileDescriptor(OsCommPtr oc)
947{
948 int connection = oc->fd;
949
950 if (oc->trans_conn) {
951 _XSERVTransDisconnect(oc->trans_conn);
952 _XSERVTransClose(oc->trans_conn);
953 }
954#ifndef WIN32
955 ConnectionTranslation[connection] = 0;
956#else
957 SetConnectionTranslation(connection, 0);
958#endif
959 FD_CLR(connection, &AllSockets)do { int __fd = (connection); ((&AllSockets)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~((__int32_t
)(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))))
; } while(0)
;
960 FD_CLR(connection, &AllClients)do { int __fd = (connection); ((&AllClients)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~((__int32_t
)(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))))
; } while(0)
;
961 FD_CLR(connection, &ClientsWithInput)do { int __fd = (connection); ((&ClientsWithInput)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~((__int32_t
)(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))))
; } while(0)
;
962 FD_CLR(connection, &GrabImperviousClients)do { int __fd = (connection); ((&GrabImperviousClients)->
fds_bits[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~
((__int32_t)(1<<((unsigned long)__fd % (sizeof(__int32_t
) * 8))))); } while(0)
;
963 if (GrabInProgress) {
964 FD_CLR(connection, &SavedAllSockets)do { int __fd = (connection); ((&SavedAllSockets)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~((__int32_t
)(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))))
; } while(0)
;
965 FD_CLR(connection, &SavedAllClients)do { int __fd = (connection); ((&SavedAllClients)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~((__int32_t
)(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))))
; } while(0)
;
966 FD_CLR(connection, &SavedClientsWithInput)do { int __fd = (connection); ((&SavedClientsWithInput)->
fds_bits[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~
((__int32_t)(1<<((unsigned long)__fd % (sizeof(__int32_t
) * 8))))); } while(0)
;
967 }
968 FD_CLR(connection, &ClientsWriteBlocked)do { int __fd = (connection); ((&ClientsWriteBlocked)->
fds_bits[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~
((__int32_t)(1<<((unsigned long)__fd % (sizeof(__int32_t
) * 8))))); } while(0)
;
969 if (!XFD_ANYSET(&ClientsWriteBlocked)((((((1024) % ((sizeof(__int32_t) * 8))) == 0) ? ((1024) / ((
sizeof(__int32_t) * 8))) : (((1024) / ((sizeof(__int32_t) * 8
))) + 1)) > 0 && ((((&ClientsWriteBlocked)->
fds_bits))[0])) || (((((1024) % ((sizeof(__int32_t) * 8))) ==
0) ? ((1024) / ((sizeof(__int32_t) * 8))) : (((1024) / ((sizeof
(__int32_t) * 8))) + 1)) > 1 && ((((&ClientsWriteBlocked
)->fds_bits))[1])) || (((((1024) % ((sizeof(__int32_t) * 8
))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))) : (((1024) /
((sizeof(__int32_t) * 8))) + 1)) > 2 && ((((&
ClientsWriteBlocked)->fds_bits))[2])) || (((((1024) % ((sizeof
(__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8)
)) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)) > 3 &&
((((&ClientsWriteBlocked)->fds_bits))[3])) || (((((1024
) % ((sizeof(__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t
) * 8))) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)) > 4
&& ((((&ClientsWriteBlocked)->fds_bits))[4]))
|| (((((1024) % ((sizeof(__int32_t) * 8))) == 0) ? ((1024) /
((sizeof(__int32_t) * 8))) : (((1024) / ((sizeof(__int32_t) *
8))) + 1)) > 5 && ((((&ClientsWriteBlocked)->
fds_bits))[5])) || (((((1024) % ((sizeof(__int32_t) * 8))) ==
0) ? ((1024) / ((sizeof(__int32_t) * 8))) : (((1024) / ((sizeof
(__int32_t) * 8))) + 1)) > 6 && ((((&ClientsWriteBlocked
)->fds_bits))[6])) || (((((1024) % ((sizeof(__int32_t) * 8
))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))) : (((1024) /
((sizeof(__int32_t) * 8))) + 1)) > 7 && ((((&
ClientsWriteBlocked)->fds_bits))[7])) || (((((1024) % ((sizeof
(__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8)
)) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)) > 8 &&
((((&ClientsWriteBlocked)->fds_bits))[8])) || (((((1024
) % ((sizeof(__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t
) * 8))) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)) > 9
&& ((((&ClientsWriteBlocked)->fds_bits))[9]))
|| (((((1024) % ((sizeof(__int32_t) * 8))) == 0) ? ((1024) /
((sizeof(__int32_t) * 8))) : (((1024) / ((sizeof(__int32_t) *
8))) + 1)) > 10 && ((((&ClientsWriteBlocked)->
fds_bits))[10])) || (((((1024) % ((sizeof(__int32_t) * 8))) ==
0) ? ((1024) / ((sizeof(__int32_t) * 8))) : (((1024) / ((sizeof
(__int32_t) * 8))) + 1)) > 11 && ((((&ClientsWriteBlocked
)->fds_bits))[11])) || (((((1024) % ((sizeof(__int32_t) * 8
))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))) : (((1024) /
((sizeof(__int32_t) * 8))) + 1)) > 12 && ((((&
ClientsWriteBlocked)->fds_bits))[12])) || (((((1024) % ((sizeof
(__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8)
)) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)) > 13 &&
((((&ClientsWriteBlocked)->fds_bits))[13])) || (((((1024
) % ((sizeof(__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t
) * 8))) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)) > 14
&& ((((&ClientsWriteBlocked)->fds_bits))[14])
) || (((((1024) % ((sizeof(__int32_t) * 8))) == 0) ? ((1024) /
((sizeof(__int32_t) * 8))) : (((1024) / ((sizeof(__int32_t) *
8))) + 1)) > 15 && ((((&ClientsWriteBlocked)->
fds_bits))[15])))
)
970 AnyClientsWriteBlocked = FALSE0;
971 FD_CLR(connection, &OutputPending)do { int __fd = (connection); ((&OutputPending)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~((__int32_t
)(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))))
; } while(0)
;
972}
973
974/*****************
975 * CheckConnections
976 * Some connection has died, go find which one and shut it down
977 * The file descriptor has been closed, but is still in AllClients.
978 * If would truly be wonderful if select() would put the bogus
979 * file descriptors in the exception mask, but nooooo. So we have
980 * to check each and every socket individually.
981 *****************/
982
983void
984CheckConnections(void)
985{
986#ifndef WIN32
987 fd_mask mask;
988#endif
989 fd_set tmask;
990 int curclient, curoff;
991 int i;
992 struct timeval notime;
993 int r;
994
995#ifdef WIN32
996 fd_set savedAllClients;
997#endif
998
999 notime.tv_sec = 0;
1000 notime.tv_usec = 0;
1001
1002#ifndef WIN32
1003 for (i = 0; i < howmany(XFD_SETSIZE, NFDBITS)((((512) % ((sizeof(__int32_t) * 8))) == 0) ? ((512) / ((sizeof
(__int32_t) * 8))) : (((512) / ((sizeof(__int32_t) * 8))) + 1
))
; i++) {
1004 mask = AllClients.fds_bits[i];
1005 while (mask) {
1006 curoff = mffs(mask) - 1;
1007 curclient = curoff + (i * (sizeof(fd_mask) * 8));
1008 FD_ZERO(&tmask)__builtin_bzero(&tmask, sizeof(*(&tmask)));
1009 FD_SET(curclient, &tmask)do { int __fd = (curclient); ((&tmask)->fds_bits[(unsigned
long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)(1<<
((unsigned long)__fd % (sizeof(__int32_t) * 8))))); } while(0
)
;
1010 do {
1011 r = Select(curclient + 1, &tmask, NULL, NULL, &notime)select(curclient + 1,(fd_set*)&tmask,(fd_set*)((void*)0),
(fd_set*)((void*)0),(struct timeval*)&notime)
;
1012 } while (r < 0 && (errno(*__error()) == EINTR4 || errno(*__error()) == EAGAIN35));
1013 if (r < 0)
1014 if (ConnectionTranslation[curclient] > 0)
1015 CloseDownClient(clients[ConnectionTranslation[curclient]]);
1016 mask &= ~((fd_mask) 1 << curoff);
1017 }
1018 }
1019#else
1020 XFD_COPYSET(&AllClients, &savedAllClients){ int __i__; for (__i__ = 0; __i__ < ((((1024) % ((sizeof(
__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))
) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)); __i__++) (((
(&savedAllClients))->fds_bits))[__i__] = ((((&AllClients
))->fds_bits))[__i__]; }
;
1021 for (i = 0; i < XFD_SETCOUNT(&savedAllClients); i++) {
1022 curclient = XFD_FD(&savedAllClients, i);
1023 FD_ZERO(&tmask)__builtin_bzero(&tmask, sizeof(*(&tmask)));
1024 FD_SET(curclient, &tmask)do { int __fd = (curclient); ((&tmask)->fds_bits[(unsigned
long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)(1<<
((unsigned long)__fd % (sizeof(__int32_t) * 8))))); } while(0
)
;
1025 do {
1026 r = Select(curclient + 1, &tmask, NULL, NULL, &notime)select(curclient + 1,(fd_set*)&tmask,(fd_set*)((void*)0),
(fd_set*)((void*)0),(struct timeval*)&notime)
;
1027 } while (r < 0 && (errno(*__error()) == EINTR4 || errno(*__error()) == EAGAIN35));
1028 if (r < 0)
1029 if (GetConnectionTranslation(curclient) > 0)
1030 CloseDownClient(clients[GetConnectionTranslation(curclient)]);
1031 }
1032#endif
1033}
1034
1035/*****************
1036 * CloseDownConnection
1037 * Delete client from AllClients and free resources
1038 *****************/
1039
1040void
1041CloseDownConnection(ClientPtr client)
1042{
1043 OsCommPtr oc = (OsCommPtr) client->osPrivate;
1044
1045 if (FlushCallback)
1046 CallCallbacks(&FlushCallback, NULL((void*)0));
1047
1048 if (oc->output)
1049 FlushClient(client, oc, (char *) NULL((void*)0), 0);
1050#ifdef XDMCP1
1051 XdmcpCloseDisplay(oc->fd);
1052#endif
1053 CloseDownFileDescriptor(oc);
1054 FreeOsBuffers(oc);
1055 free(client->osPrivate);
1056 client->osPrivate = (void *) NULL((void*)0);
1057 if (auditTrailLevel > 1)
1058 AuditF("client %d disconnected\n", client->index);
1059}
1060
1061void
1062AddGeneralSocket(int fd)
1063{
1064 FD_SET(fd, &AllSockets)do { int __fd = (fd); ((&AllSockets)->fds_bits[(unsigned
long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)(1<<
((unsigned long)__fd % (sizeof(__int32_t) * 8))))); } while(0
)
;
1065 if (GrabInProgress)
1066 FD_SET(fd, &SavedAllSockets)do { int __fd = (fd); ((&SavedAllSockets)->fds_bits[(unsigned
long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)(1<<
((unsigned long)__fd % (sizeof(__int32_t) * 8))))); } while(0
)
;
1067}
1068
1069void
1070AddEnabledDevice(int fd)
1071{
1072 FD_SET(fd, &EnabledDevices)do { int __fd = (fd); ((&EnabledDevices)->fds_bits[(unsigned
long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)(1<<
((unsigned long)__fd % (sizeof(__int32_t) * 8))))); } while(0
)
;
1073 AddGeneralSocket(fd);
1074}
1075
1076void
1077RemoveGeneralSocket(int fd)
1078{
1079 FD_CLR(fd, &AllSockets)do { int __fd = (fd); ((&AllSockets)->fds_bits[(unsigned
long)__fd/(sizeof(__int32_t) * 8)] &= ~((__int32_t)(1<<
((unsigned long)__fd % (sizeof(__int32_t) * 8))))); } while(0
)
;
1080 if (GrabInProgress)
1081 FD_CLR(fd, &SavedAllSockets)do { int __fd = (fd); ((&SavedAllSockets)->fds_bits[(unsigned
long)__fd/(sizeof(__int32_t) * 8)] &= ~((__int32_t)(1<<
((unsigned long)__fd % (sizeof(__int32_t) * 8))))); } while(0
)
;
1082}
1083
1084void
1085RemoveEnabledDevice(int fd)
1086{
1087 FD_CLR(fd, &EnabledDevices)do { int __fd = (fd); ((&EnabledDevices)->fds_bits[(unsigned
long)__fd/(sizeof(__int32_t) * 8)] &= ~((__int32_t)(1<<
((unsigned long)__fd % (sizeof(__int32_t) * 8))))); } while(0
)
;
1088 RemoveGeneralSocket(fd);
1089}
1090
1091/*****************
1092 * OnlyListenToOneClient:
1093 * Only accept requests from one client. Continue to handle new
1094 * connections, but don't take any protocol requests from the new
1095 * ones. Note that if GrabInProgress is set, EstablishNewConnections
1096 * needs to put new clients into SavedAllSockets and SavedAllClients.
1097 * Note also that there is no timeout for this in the protocol.
1098 * This routine is "undone" by ListenToAllClients()
1099 *****************/
1100
1101int
1102OnlyListenToOneClient(ClientPtr client)
1103{
1104 OsCommPtr oc = (OsCommPtr) client->osPrivate;
1105 int rc, connection = oc->fd;
1106
1107 rc = XaceHook(XACE_SERVER_ACCESS9, client, DixGrabAccess(1<<17));
1108 if (rc != Success0)
1109 return rc;
1110
1111 if (!GrabInProgress) {
1112 XFD_COPYSET(&ClientsWithInput, &SavedClientsWithInput){ int __i__; for (__i__ = 0; __i__ < ((((1024) % ((sizeof(
__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))
) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)); __i__++) (((
(&SavedClientsWithInput))->fds_bits))[__i__] = ((((&
ClientsWithInput))->fds_bits))[__i__]; }
;
1113 XFD_ANDSET(&ClientsWithInput,{ int __i__; for (__i__ = 0; __i__ < ((((1024) % ((sizeof(
__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))
) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)); __i__++) (((
(&ClientsWithInput))->fds_bits))[__i__] = ((((((&ClientsWithInput
))->fds_bits))[__i__]) & (((((&GrabImperviousClients
))->fds_bits))[__i__])); }
1114 &ClientsWithInput, &GrabImperviousClients){ int __i__; for (__i__ = 0; __i__ < ((((1024) % ((sizeof(
__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))
) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)); __i__++) (((
(&ClientsWithInput))->fds_bits))[__i__] = ((((((&ClientsWithInput
))->fds_bits))[__i__]) & (((((&GrabImperviousClients
))->fds_bits))[__i__])); }
;
1115 if (FD_ISSET(connection, &SavedClientsWithInput)__darwin_fd_isset((connection), (&SavedClientsWithInput))) {
1116 FD_CLR(connection, &SavedClientsWithInput)do { int __fd = (connection); ((&SavedClientsWithInput)->
fds_bits[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~
((__int32_t)(1<<((unsigned long)__fd % (sizeof(__int32_t
) * 8))))); } while(0)
;
1117 FD_SET(connection, &ClientsWithInput)do { int __fd = (connection); ((&ClientsWithInput)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)
(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))));
} while(0)
;
1118 }
1119 XFD_UNSET(&SavedClientsWithInput, &GrabImperviousClients){ int __i__; for (__i__ = 0; __i__ < ((((1024) % ((sizeof(
__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))
) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)); __i__++) (((
(&SavedClientsWithInput))->fds_bits))[__i__] &= ~(
((((&GrabImperviousClients))->fds_bits))[__i__]); }
;
1120 XFD_COPYSET(&AllSockets, &SavedAllSockets){ int __i__; for (__i__ = 0; __i__ < ((((1024) % ((sizeof(
__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))
) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)); __i__++) (((
(&SavedAllSockets))->fds_bits))[__i__] = ((((&AllSockets
))->fds_bits))[__i__]; }
;
1121 XFD_COPYSET(&AllClients, &SavedAllClients){ int __i__; for (__i__ = 0; __i__ < ((((1024) % ((sizeof(
__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))
) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)); __i__++) (((
(&SavedAllClients))->fds_bits))[__i__] = ((((&AllClients
))->fds_bits))[__i__]; }
;
1122 XFD_UNSET(&AllSockets, &AllClients){ int __i__; for (__i__ = 0; __i__ < ((((1024) % ((sizeof(
__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))
) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)); __i__++) (((
(&AllSockets))->fds_bits))[__i__] &= ~(((((&AllClients
))->fds_bits))[__i__]); }
;
1123 XFD_ANDSET(&AllClients, &AllClients, &GrabImperviousClients){ int __i__; for (__i__ = 0; __i__ < ((((1024) % ((sizeof(
__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))
) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)); __i__++) (((
(&AllClients))->fds_bits))[__i__] = ((((((&AllClients
))->fds_bits))[__i__]) & (((((&GrabImperviousClients
))->fds_bits))[__i__])); }
;
1124 FD_SET(connection, &AllClients)do { int __fd = (connection); ((&AllClients)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)
(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))));
} while(0)
;
1125 XFD_ORSET(&AllSockets, &AllSockets, &AllClients){ int __i__; for (__i__ = 0; __i__ < ((((1024) % ((sizeof(
__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))
) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)); __i__++) (((
(&AllSockets))->fds_bits))[__i__] = ((((((&AllSockets
))->fds_bits))[__i__]) | (((((&AllClients))->fds_bits
))[__i__])); }
;
1126 GrabInProgress = client->index;
1127 }
1128 return rc;
1129}
1130
1131/****************
1132 * ListenToAllClients:
1133 * Undoes OnlyListentToOneClient()
1134 ****************/
1135
1136void
1137ListenToAllClients(void)
1138{
1139 if (GrabInProgress) {
1140 XFD_ORSET(&AllSockets, &AllSockets, &SavedAllSockets){ int __i__; for (__i__ = 0; __i__ < ((((1024) % ((sizeof(
__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))
) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)); __i__++) (((
(&AllSockets))->fds_bits))[__i__] = ((((((&AllSockets
))->fds_bits))[__i__]) | (((((&SavedAllSockets))->fds_bits
))[__i__])); }
;
1141 XFD_ORSET(&AllClients, &AllClients, &SavedAllClients){ int __i__; for (__i__ = 0; __i__ < ((((1024) % ((sizeof(
__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))
) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)); __i__++) (((
(&AllClients))->fds_bits))[__i__] = ((((((&AllClients
))->fds_bits))[__i__]) | (((((&SavedAllClients))->fds_bits
))[__i__])); }
;
1142 XFD_ORSET(&ClientsWithInput, &ClientsWithInput, &SavedClientsWithInput){ int __i__; for (__i__ = 0; __i__ < ((((1024) % ((sizeof(
__int32_t) * 8))) == 0) ? ((1024) / ((sizeof(__int32_t) * 8))
) : (((1024) / ((sizeof(__int32_t) * 8))) + 1)); __i__++) (((
(&ClientsWithInput))->fds_bits))[__i__] = ((((((&ClientsWithInput
))->fds_bits))[__i__]) | (((((&SavedClientsWithInput))
->fds_bits))[__i__])); }
;
1143 GrabInProgress = 0;
1144 }
1145}
1146
1147/****************
1148 * IgnoreClient
1149 * Removes one client from input masks.
1150 * Must have cooresponding call to AttendClient.
1151 ****************/
1152
1153void
1154IgnoreClient(ClientPtr client)
1155{
1156 OsCommPtr oc = (OsCommPtr) client->osPrivate;
1157 int connection = oc->fd;
1158
1159 client->ignoreCount++;
1160 if (client->ignoreCount > 1)
1161 return;
1162
1163 isItTimeToYield = TRUE1;
1164 if (!GrabInProgress || FD_ISSET(connection, &AllClients)__darwin_fd_isset((connection), (&AllClients))) {
1165 if (FD_ISSET(connection, &ClientsWithInput)__darwin_fd_isset((connection), (&ClientsWithInput)))
1166 FD_SET(connection, &IgnoredClientsWithInput)do { int __fd = (connection); ((&IgnoredClientsWithInput)
->fds_bits[(unsigned long)__fd/(sizeof(__int32_t) * 8)] |=
((__int32_t)(1<<((unsigned long)__fd % (sizeof(__int32_t
) * 8))))); } while(0)
;
1167 else
1168 FD_CLR(connection, &IgnoredClientsWithInput)do { int __fd = (connection); ((&IgnoredClientsWithInput)
->fds_bits[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &=
~((__int32_t)(1<<((unsigned long)__fd % (sizeof(__int32_t
) * 8))))); } while(0)
;
1169 FD_CLR(connection, &ClientsWithInput)do { int __fd = (connection); ((&ClientsWithInput)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~((__int32_t
)(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))))
; } while(0)
;
1170 FD_CLR(connection, &AllSockets)do { int __fd = (connection); ((&AllSockets)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~((__int32_t
)(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))))
; } while(0)
;
1171 FD_CLR(connection, &AllClients)do { int __fd = (connection); ((&AllClients)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~((__int32_t
)(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))))
; } while(0)
;
1172 FD_CLR(connection, &LastSelectMask)do { int __fd = (connection); ((&LastSelectMask)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~((__int32_t
)(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))))
; } while(0)
;
1173 }
1174 else {
1175 if (FD_ISSET(connection, &SavedClientsWithInput)__darwin_fd_isset((connection), (&SavedClientsWithInput)))
1176 FD_SET(connection, &IgnoredClientsWithInput)do { int __fd = (connection); ((&IgnoredClientsWithInput)
->fds_bits[(unsigned long)__fd/(sizeof(__int32_t) * 8)] |=
((__int32_t)(1<<((unsigned long)__fd % (sizeof(__int32_t
) * 8))))); } while(0)
;
1177 else
1178 FD_CLR(connection, &IgnoredClientsWithInput)do { int __fd = (connection); ((&IgnoredClientsWithInput)
->fds_bits[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &=
~((__int32_t)(1<<((unsigned long)__fd % (sizeof(__int32_t
) * 8))))); } while(0)
;
1179 FD_CLR(connection, &SavedClientsWithInput)do { int __fd = (connection); ((&SavedClientsWithInput)->
fds_bits[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~
((__int32_t)(1<<((unsigned long)__fd % (sizeof(__int32_t
) * 8))))); } while(0)
;
1180 FD_CLR(connection, &SavedAllSockets)do { int __fd = (connection); ((&SavedAllSockets)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~((__int32_t
)(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))))
; } while(0)
;
1181 FD_CLR(connection, &SavedAllClients)do { int __fd = (connection); ((&SavedAllClients)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~((__int32_t
)(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))))
; } while(0)
;
1182 }
1183}
1184
1185/****************
1186 * AttendClient
1187 * Adds one client back into the input masks.
1188 ****************/
1189
1190void
1191AttendClient(ClientPtr client)
1192{
1193 OsCommPtr oc = (OsCommPtr) client->osPrivate;
1194 int connection = oc->fd;
1195
1196 client->ignoreCount--;
1197 if (client->ignoreCount)
1198 return;
1199
1200 if (!GrabInProgress || GrabInProgress == client->index ||
1201 FD_ISSET(connection, &GrabImperviousClients)__darwin_fd_isset((connection), (&GrabImperviousClients))) {
1202 FD_SET(connection, &AllClients)do { int __fd = (connection); ((&AllClients)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)
(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))));
} while(0)
;
1203 FD_SET(connection, &AllSockets)do { int __fd = (connection); ((&AllSockets)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)
(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))));
} while(0)
;
1204 FD_SET(connection, &LastSelectMask)do { int __fd = (connection); ((&LastSelectMask)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)
(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))));
} while(0)
;
1205 if (FD_ISSET(connection, &IgnoredClientsWithInput)__darwin_fd_isset((connection), (&IgnoredClientsWithInput
))
)
1206 FD_SET(connection, &ClientsWithInput)do { int __fd = (connection); ((&ClientsWithInput)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)
(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))));
} while(0)
;
1207 }
1208 else {
1209 FD_SET(connection, &SavedAllClients)do { int __fd = (connection); ((&SavedAllClients)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)
(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))));
} while(0)
;
1210 FD_SET(connection, &SavedAllSockets)do { int __fd = (connection); ((&SavedAllSockets)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)
(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))));
} while(0)
;
1211 if (FD_ISSET(connection, &IgnoredClientsWithInput)__darwin_fd_isset((connection), (&IgnoredClientsWithInput
))
)
1212 FD_SET(connection, &SavedClientsWithInput)do { int __fd = (connection); ((&SavedClientsWithInput)->
fds_bits[(unsigned long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t
)(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))))
; } while(0)
;
1213 }
1214}
1215
1216/* make client impervious to grabs; assume only executing client calls this */
1217
1218void
1219MakeClientGrabImpervious(ClientPtr client)
1220{
1221 OsCommPtr oc = (OsCommPtr) client->osPrivate;
1222 int connection = oc->fd;
1223
1224 FD_SET(connection, &GrabImperviousClients)do { int __fd = (connection); ((&GrabImperviousClients)->
fds_bits[(unsigned long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t
)(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))))
; } while(0)
;
1225
1226 if (ServerGrabCallback) {
1227 ServerGrabInfoRec grabinfo;
1228
1229 grabinfo.client = client;
1230 grabinfo.grabstate = CLIENT_IMPERVIOUS;
1231 CallCallbacks(&ServerGrabCallback, &grabinfo);
1232 }
1233}
1234
1235/* make client pervious to grabs; assume only executing client calls this */
1236
1237void
1238MakeClientGrabPervious(ClientPtr client)
1239{
1240 OsCommPtr oc = (OsCommPtr) client->osPrivate;
1241 int connection = oc->fd;
1242
1243 FD_CLR(connection, &GrabImperviousClients)do { int __fd = (connection); ((&GrabImperviousClients)->
fds_bits[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~
((__int32_t)(1<<((unsigned long)__fd % (sizeof(__int32_t
) * 8))))); } while(0)
;
1244 if (GrabInProgress && (GrabInProgress != client->index)) {
1245 if (FD_ISSET(connection, &ClientsWithInput)__darwin_fd_isset((connection), (&ClientsWithInput))) {
1246 FD_SET(connection, &SavedClientsWithInput)do { int __fd = (connection); ((&SavedClientsWithInput)->
fds_bits[(unsigned long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t
)(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))))
; } while(0)
;
1247 FD_CLR(connection, &ClientsWithInput)do { int __fd = (connection); ((&ClientsWithInput)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~((__int32_t
)(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))))
; } while(0)
;
1248 }
1249 FD_CLR(connection, &AllSockets)do { int __fd = (connection); ((&AllSockets)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~((__int32_t
)(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))))
; } while(0)
;
1250 FD_CLR(connection, &AllClients)do { int __fd = (connection); ((&AllClients)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] &= ~((__int32_t
)(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))))
; } while(0)
;
1251 isItTimeToYield = TRUE1;
1252 }
1253
1254 if (ServerGrabCallback) {
1255 ServerGrabInfoRec grabinfo;
1256
1257 grabinfo.client = client;
1258 grabinfo.grabstate = CLIENT_PERVIOUS;
1259 CallCallbacks(&ServerGrabCallback, &grabinfo);
1260 }
1261}
1262
1263/* Add a fd (from launchd or similar) to our listeners */
1264void
1265ListenOnOpenFD(int fd, int noxauth)
1266{
1267 char port[256];
1268 XtransConnInfo ciptr;
1269 const char *display_env = getenv("DISPLAY");
1270
1271 if (display_env && (strncmp(display_env, "/tmp/launch", 11) == 0)) {
1272 /* Make the path the launchd socket if our DISPLAY is set right */
1273 strcpy(port, display_env)__builtin___strcpy_chk (port, display_env, __builtin_object_size
(port, 2 > 1 ? 1 : 0))
;
1274 }
1275 else {
1276 /* Just some default so things don't break and die. */
1277 snprintf(port, sizeof(port), ":%d", atoi(display))__builtin___snprintf_chk (port, sizeof(port), 0, __builtin_object_size
(port, 2 > 1 ? 1 : 0), ":%d", atoi(display))
;
1278 }
1279
1280 /* Make our XtransConnInfo
1281 * TRANS_SOCKET_LOCAL_INDEX = 5 from Xtrans.c
1282 */
1283 ciptr = _XSERVTransReopenCOTSServer(5, fd, port);
1284 if (ciptr == NULL((void*)0)) {
1285 ErrorF("Got NULL while trying to Reopen listen port.\n");
1286 return;
1287 }
1288
1289 if (noxauth)
1290 ciptr->flags = ciptr->flags | TRANS_NOXAUTH(1<<6);
1291
1292 /* Allocate space to store it */
1293 ListenTransFds =
1294 (int *) realloc(ListenTransFds, (ListenTransCount + 1) * sizeof(int));
1295 ListenTransConns =
1296 (XtransConnInfo *) realloc(ListenTransConns,
1297 (ListenTransCount +
1298 1) * sizeof(XtransConnInfo));
1299
1300 /* Store it */
1301 ListenTransConns[ListenTransCount] = ciptr;
1302 ListenTransFds[ListenTransCount] = fd;
1303
1304 FD_SET(fd, &WellKnownConnections)do { int __fd = (fd); ((&WellKnownConnections)->fds_bits
[(unsigned long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)
(1<<((unsigned long)__fd % (sizeof(__int32_t) * 8)))));
} while(0)
;
1305 FD_SET(fd, &AllSockets)do { int __fd = (fd); ((&AllSockets)->fds_bits[(unsigned
long)__fd/(sizeof(__int32_t) * 8)] |= ((__int32_t)(1<<
((unsigned long)__fd % (sizeof(__int32_t) * 8))))); } while(0
)
;
1306
1307 /* Increment the count */
1308 ListenTransCount++;
1309}
1310
1311/* based on TRANS(SocketUNIXAccept) (XtransConnInfo ciptr, int *status) */
1312Bool
1313AddClientOnOpenFD(int fd)
1314{
1315 XtransConnInfo ciptr;
1316 CARD32 connect_time;
1317 char port[20];
1318
1319 snprintf(port, sizeof(port), ":%d", atoi(display))__builtin___snprintf_chk (port, sizeof(port), 0, __builtin_object_size
(port, 2 > 1 ? 1 : 0), ":%d", atoi(display))
;
1320 ciptr = _XSERVTransReopenCOTSServer(5, fd, port);
1321 if (ciptr == NULL((void*)0))
1322 return FALSE0;
1323
1324 _XSERVTransSetOption(ciptr, TRANS_NONBLOCKING1, 1);
1325 ciptr->flags |= TRANS_NOXAUTH(1<<6);
1326
1327 connect_time = GetTimeInMillis();
1328
1329 if (!AllocNewConnection(ciptr, fd, connect_time)) {
1330 ErrorConnMax(ciptr);
1331 _XSERVTransClose(ciptr);
1332 return FALSE0;
1333 }
1334
1335 return TRUE1;
1336}