1 /*
2  *Copyright (C) 2003-2004 Harold L Hunt II All Rights Reserved.
3  *Copyright (C) Colin Harrison 2005-2008
4  *
5  *Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  *"Software"), to deal in the Software without restriction, including
8  *without limitation the rights to use, copy, modify, merge, publish,
9  *distribute, sublicense, and/or sell copies of the Software, and to
10  *permit persons to whom the Software is furnished to do so, subject to
11  *the following conditions:
12  *
13  *The above copyright notice and this permission notice shall be
14  *included in all copies or substantial portions of the Software.
15  *
16  *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  *NONINFRINGEMENT. IN NO EVENT SHALL HAROLD L HUNT II BE LIABLE FOR
20  *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21  *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  *Except as contained in this notice, the name of the copyright holder(s)
25  *and author(s) shall not be used in advertising or otherwise to promote
26  *the sale, use or other dealings in this Software without prior written
27  *authorization from the copyright holder(s) and author(s).
28  *
29  * Authors:	Harold L Hunt II
30  *              Colin Harrison
31  */
32 
33 #ifdef HAVE_XWIN_CONFIG_H
34 #include <xwin-config.h>
35 #endif
36 
37 #include "win.h"
38 #include "dixstruct.h"
39 
40 /*
41  * Local function prototypes
42  */
43 
44 DISPATCH_PROC(winProcEstablishConnection);
45 
46 /*
47  * Wrapper for internal EstablishConnection function.
48  * Initializes internal clients that must not be started until
49  * an external client has connected.
50  */
51 
52 int
winProcEstablishConnection(ClientPtr client)53 winProcEstablishConnection(ClientPtr client)
54 {
55     int iReturn;
56     static int s_iCallCount = 0;
57     static unsigned long s_ulServerGeneration = 0;
58 
59     if (s_iCallCount == 0)
60         winDebug("winProcEstablishConnection - Hello\n");
61 
62     /* Do nothing if clipboard is not enabled */
63     if (!g_fClipboard) {
64         ErrorF("winProcEstablishConnection - Clipboard is not enabled, "
65                "returning.\n");
66 
67         /* Unwrap the original function, call it, and return */
68         InitialVector[2] = winProcEstablishConnectionOrig;
69         iReturn = (*winProcEstablishConnectionOrig) (client);
70         winProcEstablishConnectionOrig = NULL;
71         return iReturn;
72     }
73 
74     /* Watch for server reset */
75     if (s_ulServerGeneration != serverGeneration) {
76         /* Save new generation number */
77         s_ulServerGeneration = serverGeneration;
78 
79         /* Reset call count */
80         s_iCallCount = 0;
81     }
82 
83     /* Increment call count */
84     ++s_iCallCount;
85 
86     /*
87      * This procedure is only used for initialization.
88      * We can unwrap the original procedure at this point
89      * so that this function is no longer called until the
90      * server resets and the function is wrapped again.
91      */
92     InitialVector[2] = winProcEstablishConnectionOrig;
93 
94     /*
95      * Call original function and bail if it fails.
96      * NOTE: We must do this first, since we need XdmcpOpenDisplay
97      * to be called before we initialize our clipboard client.
98      */
99     iReturn = (*winProcEstablishConnectionOrig) (client);
100     if (iReturn != 0) {
101         ErrorF("winProcEstablishConnection - ProcEstablishConnection "
102                "failed, bailing.\n");
103         return iReturn;
104     }
105 
106     /* Clear original function pointer */
107     winProcEstablishConnectionOrig = NULL;
108 
109     /* Startup the clipboard client if clipboard mode is being used */
110     if (g_fClipboard) {
111         /*
112          * NOTE: The clipboard client is started here for a reason:
113          * 1) Assume you are using XDMCP (e.g. XWin -query %hostname%)
114          * 2) If the clipboard client attaches during X Server startup,
115          *    then it becomes the "magic client" that causes the X Server
116          *    to reset if it exits.
117          * 3) XDMCP calls KillAllClients when it starts up.
118          * 4) The clipboard client is a client, so it is killed.
119          * 5) The clipboard client is the "magic client", so the X Server
120          *    resets itself.
121          * 6) This repeats ad infinitum.
122          * 7) We avoid this by waiting until at least one client (could
123          *    be XDM, could be another client) connects, which makes it
124          *    almost certain that the clipboard client will not connect
125          *    until after XDM when using XDMCP.
126          */
127 
128         /* Create the clipboard client thread */
129         if (!winInitClipboard()) {
130             ErrorF("winProcEstablishConnection - winClipboardInit "
131                    "failed.\n");
132             return iReturn;
133         }
134 
135         ErrorF("winProcEstablishConnection - winInitClipboard returned.\n");
136     }
137 
138     return iReturn;
139 }
140