aboutsummaryrefslogtreecommitdiff
path: root/doc/nx-X11_vs_XOrg69_patches/NXresource.c.NX.patch
blob: 3c7f45e46867119f745f3c3b6465ea9789c8659e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
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
--- ./nx-X11/programs/Xserver/hw/nxagent/X/NXresource.c.X.original	2015-02-13 14:03:44.748441432 +0100
+++ ./nx-X11/programs/Xserver/hw/nxagent/X/NXresource.c	2015-02-10 19:13:13.820685287 +0100
@@ -1,3 +1,20 @@
+/**************************************************************************/
+/*                                                                        */
+/* Copyright (c) 2001, 2011 NoMachine, http://www.nomachine.com/.         */
+/*                                                                        */
+/* NXAGENT, NX protocol compression and NX extensions to this software    */
+/* are copyright of NoMachine. Redistribution and use of the present      */
+/* software is allowed according to terms specified in the file LICENSE   */
+/* which comes in the source distribution.                                */
+/*                                                                        */
+/* Check http://www.nomachine.com/licensing.html for applicability.       */
+/*                                                                        */
+/* NX and NoMachine are trademarks of Medialogic S.p.A.                   */
+/*                                                                        */
+/* All rights reserved.                                                   */
+/*                                                                        */
+/**************************************************************************/
+
 /************************************************************
 
 Copyright 1987, 1998  The Open Group
@@ -125,6 +142,20 @@
 #endif
 #include <assert.h>
 
+#ifdef NXAGENT_SERVER
+
+#include "Agent.h"
+#include "Font.h"
+#include "Pixmaps.h"
+#include "GCs.h"
+
+#define PANIC
+#define WARNING
+#undef  TEST
+#undef  DEBUG
+
+#endif
+
 static void RebuildTable(
     int /*client*/
 );
@@ -170,6 +201,10 @@
 
 #endif
 
+#ifdef NXAGENT_SERVER
+static int nxagentResChangedFlag = 0;
+#endif
+
 RESTYPE
 CreateNewResourceType(DeleteType deleteFunc)
 {
@@ -422,13 +457,107 @@
     return id;
 }
 
+#ifdef NXAGENT_SERVER
+
+int nxagentFindClientResource(int client, RESTYPE type, pointer value)
+{
+  ResourcePtr pResource;
+  ResourcePtr *resources;
+
+  int i;
+
+  for (i = 0; i < clientTable[client].buckets; i++)
+  {
+    resources = clientTable[client].resources;
+
+    for (pResource = resources[i]; pResource; pResource = pResource -> next)
+    {
+      if (pResource -> type == type && pResource -> value == value)
+      {
+        #ifdef TEST
+        fprintf(stderr, "nxagentFindClientResource: Found resource [%p] type [%lu] "
+                    "for client [%d].\n", (void *) value,
+                        pResource -> type, client);
+        #endif
+
+        return 1;
+      }
+    }
+  }
+
+  return 0;
+}
+
+int nxagentSwitchResourceType(int client, RESTYPE type, pointer value)
+{
+  ResourcePtr pResource;
+  ResourcePtr *resources;
+
+  RESTYPE internalType = 0;
+
+  int i;
+
+  if (type == RT_PIXMAP)
+  {
+    internalType = RT_NX_PIXMAP;
+  }
+  else if (type == RT_GC)
+  {
+    internalType = RT_NX_GC;
+  }
+  else if (type == RT_FONT)
+  {
+    internalType = RT_NX_FONT;
+  }
+  else
+  {
+    return 0;
+  }
+
+  if (client == serverClient -> index)
+  {
+    #ifdef TEST
+    fprintf(stderr, "nxagentSwitchResourceType: Requesting client is [%d]. Skipping the resource switch.\n",
+                client);
+    #endif
+
+    return 0;
+  }
+
+  for (i = 0; i < clientTable[serverClient -> index].buckets; i++)
+  {
+    resources = clientTable[serverClient -> index].resources;
+
+    for (pResource = resources[i]; pResource; pResource = pResource -> next)
+    {
+      if (pResource -> type == internalType &&
+              pResource -> value == value)
+      {
+        #ifdef TEST
+        fprintf(stderr, "nxagentSwitchResourceType: Changing resource [%p] type from [%lu] to "
+                    "[%lu] for server client [%d].\n", (void *) value,
+                        (unsigned long) pResource -> type, (unsigned long) type, serverClient -> index);
+        #endif
+
+        FreeResource(pResource -> id, RT_NONE);
+
+        return 1;
+      }
+    }
+  }
+
+  return 0;
+}
+
+#endif
+
 Bool
 AddResource(XID id, RESTYPE type, pointer value)
 {
     int client;
     register ClientResourceRec *rrec;
     register ResourcePtr res, *head;
-    	
+
     client = CLIENT_ID(id);
     rrec = &clientTable[client];
     if (!rrec->buckets)
@@ -437,6 +566,18 @@
 		(unsigned long)id, type, (unsigned long)value, client);
         FatalError("client not in use\n");
     }
+
+#ifdef NXAGENT_SERVER
+
+    nxagentSwitchResourceType(client, type, value);
+
+    #ifdef TEST
+    fprintf(stderr, "AddResource: Adding resource for client [%d] type [%lu] value [%p] id [%lu].\n",
+                client, (unsigned long) type, (void *) value, (unsigned long) id);
+    #endif
+
+#endif
+
     if ((rrec->elements >= 4*rrec->buckets) &&
 	(rrec->hashsize < MAXHASHSIZE))
 	RebuildTable(client);
@@ -453,6 +594,9 @@
     res->value = value;
     *head = res;
     rrec->elements++;
+    #ifdef NXAGENT_SERVER
+    nxagentResChangedFlag = 1;
+    #endif
     if (!(id & SERVER_BIT) && (id >= rrec->expectID))
 	rrec->expectID = id + 1;
     return TRUE;
@@ -517,6 +661,14 @@
     int		elements;
     Bool	gotOne = FALSE;
 
+#ifdef NXAGENT_SERVER
+
+    #ifdef TEST
+    fprintf(stderr, "FreeResource: Freeing resource id [%lu].\n", (unsigned long) id);
+    #endif
+
+#endif
+
     if (((cid = CLIENT_ID(id)) < MAXCLIENTS) && clientTable[cid].buckets)
     {
 	head = &clientTable[cid].resources[Hash(cid, id)];
@@ -530,6 +682,9 @@
 		RESTYPE rtype = res->type;
 		*prev = res->next;
 		elements = --*eltptr;
+                #ifdef NXAGENT_SERVER
+                nxagentResChangedFlag = 1;
+                #endif
 		if (rtype & RC_CACHED)
 		    FlushClientCaches(res->id);
 		if (rtype != skipDeleteFuncType)
@@ -570,6 +725,9 @@
 	    if (res->id == id && res->type == type)
 	    {
 		*prev = res->next;
+                #ifdef NXAGENT_SERVER
+                nxagentResChangedFlag = 1;
+                #endif
 		if (type & RC_CACHED)
 		    FlushClientCaches(res->id);
 		if (!skipFree)
@@ -634,10 +792,28 @@
     int i, elements;
     register int *eltptr;
 
+    #ifdef NXAGENT_SERVER
+    register ResourcePtr **resptr;
+    #endif
+
     if (!client)
 	client = serverClient;
 
+/*
+ * If func triggers a resource table
+ * rebuild then restart the loop.
+ */
+
+#ifdef NXAGENT_SERVER
+RestartLoop:
+#endif
+
     resources = clientTable[client->index].resources;
+
+    #ifdef NXAGENT_SERVER
+    resptr = &clientTable[client->index].resources;
+    #endif
+
     eltptr = &clientTable[client->index].elements;
     for (i = 0; i < clientTable[client->index].buckets; i++) 
     {
@@ -646,8 +822,44 @@
 	    next = this->next;
 	    if (!type || this->type == type) {
 		elements = *eltptr;
+
+                /*
+                 * FIXME:
+                 * It is not safe to let a function change the resource
+                 * table we are reading!
+                 */
+
+                #ifdef NXAGENT_SERVER
+                nxagentResChangedFlag = 0;
+                #endif
 		(*func)(this->value, this->id, cdata);
+
+                /*
+                 * Avoid that a call to RebuildTable() could invalidate the
+                 * pointer. This is safe enough, because in RebuildTable()
+                 * the new pointer is allocated just before the old one is
+                 * freed, so it can't point to the same address.
+                 */
+
+                #ifdef NXAGENT_SERVER
+                if (*resptr != resources)
+                   goto RestartLoop;
+                #endif
+
+                /*
+                 * It's not enough to check if the number of elements has
+                 * changed, beacause it could happen that the number of
+                 * resources that have been added matches the number of
+                 * the freed ones.
+                 * 'nxagentResChangedFlag' is set if a resource has been
+                 * added or freed.
+                 */
+
+                #ifdef NXAGENT_SERVER
+                if (*eltptr != elements || nxagentResChangedFlag)
+                #else
 		if (*eltptr != elements)
+                #endif
 		    next = resources[i]; /* start over */
 	    }
 	}
@@ -665,10 +877,28 @@
     int i, elements;
     register int *eltptr;
 
+    #ifdef NXAGENT_SERVER
+    register ResourcePtr **resptr;
+    #endif
+
     if (!client)
         client = serverClient;
 
+/*
+ * If func triggers a resource table
+ * rebuild then restart the loop.
+ */
+
+#ifdef NXAGENT_SERVER
+RestartLoop:
+#endif
+
     resources = clientTable[client->index].resources;
+
+    #ifdef NXAGENT_SERVER
+    resptr = &clientTable[client->index].resources;
+    #endif
+
     eltptr = &clientTable[client->index].elements;
     for (i = 0; i < clientTable[client->index].buckets; i++)
     {
@@ -676,8 +906,44 @@
         {
             next = this->next;
             elements = *eltptr;
+
+            /*
+             * FIXME:
+             * It is not safe to let a function change the resource
+             * table we are reading!
+             */
+
+            #ifdef NXAGENT_SERVER
+            nxagentResChangedFlag = 0;
+            #endif
             (*func)(this->value, this->id, this->type, cdata);
+
+            /*
+             * Avoid that a call to RebuildTable() could invalidate the
+             * pointer. This is safe enough, because in RebuildTable()
+             * the new pointer is allocated just before the old one is
+             * freed, so it can't point to the same address.
+             */
+
+            #ifdef NXAGENT_SERVER
+            if (*resptr != resources)
+                goto RestartLoop;
+            #endif
+
+            /*
+             * It's not enough to check if the number of elements has
+             * changed, beacause it could happen that the number of
+             * resources that have been added matches the number of
+             * the freed ones.
+             * 'nxagentResChangedFlag' is set if a resource has been
+             * added or freed.
+             */
+
+            #ifdef NXAGENT_SERVER
+            if (*eltptr != elements || nxagentResChangedFlag)
+            #else
             if (*eltptr != elements)
+            #endif
                 next = resources[i]; /* start over */
         }
     }
@@ -695,15 +961,44 @@
     ResourcePtr this;
     int i;
 
+    #ifdef NXAGENT_SERVER
+    ResourcePtr **resptr;
+    Bool res;
+    #endif
+
     if (!client)
 	client = serverClient;
 
+/*
+ * If func triggers a resource table
+ * rebuild then restart the loop.
+ */
+
+#ifdef NXAGENT_SERVER
+RestartLoop:
+#endif
+
     resources = clientTable[client->index].resources;
+
+    #ifdef NXAGENT_SERVER
+    resptr = &clientTable[client->index].resources;
+    #endif
+
     for (i = 0; i < clientTable[client->index].buckets; i++) {
         for (this = resources[i]; this; this = this->next) {
 	    if (!type || this->type == type) {
+                #ifdef NXAGENT_SERVER
+                res = (*func)(this->value, this->id, cdata);
+
+                if (*resptr != resources)
+                    goto RestartLoop;
+
+                if (res)
+                    return this->value;
+                #else
 		if((*func)(this->value, this->id, cdata))
 		    return this->value;
+                #endif
 	    }
 	}
     }
@@ -952,3 +1247,4 @@
 }
 
 #endif /* XCSECURITY */
+