Git init
[framework/uifw/xorg/lib/libice.git] / src / error.c
1 /******************************************************************************
2
3
4 Copyright 1993, 1998  The Open Group
5
6 Permission to use, copy, modify, distribute, and sell this software and its
7 documentation for any purpose is hereby granted without fee, provided that
8 the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
10 documentation.
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the name of The Open Group shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from The Open Group.
25
26 Author: Ralph Mor, X Consortium
27 ******************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32 #include <X11/ICE/ICElib.h>
33 #include "ICElibint.h"
34 #include <stdio.h>
35
36 #include <errno.h>
37
38
39 void
40 _IceErrorBadMinor (
41         IceConn iceConn,
42         int     majorOpcode,
43         int     offendingMinor,
44         int     severity
45 )
46 {
47     IceErrorHeader (iceConn,
48         majorOpcode, offendingMinor,
49         iceConn->receive_sequence,
50         severity,
51         IceBadMinor,
52         0);
53
54     IceFlush (iceConn);
55 }
56
57
58 void
59 _IceErrorBadState (
60         IceConn iceConn,
61         int     majorOpcode,
62         int     offendingMinor,
63         int     severity
64 )
65 {
66     IceErrorHeader (iceConn,
67         majorOpcode, offendingMinor,
68         iceConn->receive_sequence,
69         severity,
70         IceBadState,
71         0);
72
73     IceFlush (iceConn);
74 }
75
76
77 void
78 _IceErrorBadLength (
79         IceConn iceConn,
80         int     majorOpcode,
81         int     offendingMinor,
82         int     severity
83 )
84 {
85     IceErrorHeader (iceConn,
86         majorOpcode, offendingMinor,
87         iceConn->receive_sequence,
88         severity,
89         IceBadLength,
90         0);
91
92     IceFlush (iceConn);
93 }
94
95
96 void
97 _IceErrorBadValue (
98         IceConn         iceConn,
99         int             majorOpcode,
100         int             offendingMinor,
101         int             offset,
102         int             length,         /* in bytes */
103         IcePointer      value
104 )
105 {
106     IceErrorHeader (iceConn,
107         majorOpcode, offendingMinor,
108         iceConn->receive_sequence,
109         IceCanContinue,
110         IceBadValue,
111         WORD64COUNT (8 + length));
112
113     IceWriteData32 (iceConn, 4, &offset);
114     IceWriteData32 (iceConn, 4, &length);
115     IceWriteData (iceConn, length, (char *) value);
116
117     if (PAD64 (length))
118         IceWritePad (iceConn, PAD64 (length));
119     
120     IceFlush (iceConn);
121 }
122
123
124 void
125 _IceErrorNoAuthentication (
126         IceConn iceConn,
127         int     offendingMinor
128 )
129 {
130     int severity = (offendingMinor == ICE_ConnectionSetup) ?
131         IceFatalToConnection : IceFatalToProtocol;
132
133     IceErrorHeader (iceConn,
134         0, offendingMinor,
135         iceConn->receive_sequence,
136         severity,
137         IceNoAuth,
138         0);
139
140     IceFlush (iceConn);
141 }
142
143
144 void
145 _IceErrorNoVersion (
146         IceConn iceConn,
147         int     offendingMinor
148 )
149 {
150     int severity = (offendingMinor == ICE_ConnectionSetup) ?
151         IceFatalToConnection : IceFatalToProtocol;
152
153     IceErrorHeader (iceConn,
154         0, offendingMinor,
155         iceConn->receive_sequence,
156         severity,
157         IceNoVersion,
158         0);
159
160     IceFlush (iceConn);
161 }
162
163
164 void
165 _IceErrorSetupFailed (
166         IceConn iceConn,
167         int     offendingMinor,
168         char    *reason
169 )
170 {
171     char *pBuf, *pStart;
172     int bytes;
173     int severity = (offendingMinor == ICE_ConnectionSetup) ?
174         IceFatalToConnection : IceFatalToProtocol;
175
176     if (!reason)
177         reason = "";
178     bytes = STRING_BYTES (reason);
179
180     IceErrorHeader (iceConn,
181         0, offendingMinor,
182         iceConn->receive_sequence,
183         severity,
184         IceSetupFailed,
185         WORD64COUNT (bytes));
186
187     pBuf = pStart = IceAllocScratch (iceConn, PADDED_BYTES64 (bytes));
188     STORE_STRING (pBuf, reason);
189
190     IceWriteData (iceConn, PADDED_BYTES64 (bytes), pStart);
191     IceFlush (iceConn);
192 }
193
194
195 void
196 _IceErrorAuthenticationRejected (
197         IceConn iceConn,
198         int     offendingMinor,
199         char    *reason
200 )
201 {
202     char *pBuf, *pStart;
203     int bytes;
204
205     if (!reason)
206         reason = "";
207     bytes = STRING_BYTES (reason);
208
209     IceErrorHeader (iceConn,
210         0, offendingMinor,
211         iceConn->receive_sequence,
212         IceFatalToProtocol,
213         IceAuthRejected,
214         WORD64COUNT (bytes));
215
216     pBuf = pStart = IceAllocScratch (iceConn, PADDED_BYTES64 (bytes));
217     STORE_STRING (pBuf, reason);
218
219     IceWriteData (iceConn, PADDED_BYTES64 (bytes), pStart);
220     IceFlush (iceConn);
221 }
222
223
224 void
225 _IceErrorAuthenticationFailed (
226         IceConn iceConn,
227         int     offendingMinor,
228         char    *reason
229 )
230 {
231     char *pBuf, *pStart;
232     int bytes;
233
234     if (!reason)
235         reason = "";
236     bytes = STRING_BYTES (reason);
237
238     IceErrorHeader (iceConn,
239         0, offendingMinor,
240         iceConn->receive_sequence,
241         IceFatalToProtocol,
242         IceAuthFailed,
243         WORD64COUNT (bytes));
244
245     pBuf = pStart = IceAllocScratch (iceConn, PADDED_BYTES64 (bytes));
246     STORE_STRING (pBuf, reason);
247
248     IceWriteData (iceConn, PADDED_BYTES64 (bytes), pStart);
249     IceFlush (iceConn);
250 }
251
252
253 void
254 _IceErrorProtocolDuplicate (
255         IceConn iceConn,
256         char    *protocolName
257 )
258 {
259     char *pBuf, *pStart;
260     int bytes;
261
262     if (!protocolName)
263         protocolName = "";
264     bytes = STRING_BYTES (protocolName);
265
266     IceErrorHeader (iceConn,
267         0, ICE_ProtocolSetup,
268         iceConn->receive_sequence,
269         IceFatalToProtocol,
270         IceProtocolDuplicate,
271         WORD64COUNT (bytes));
272
273     pBuf = pStart = IceAllocScratch (iceConn, PADDED_BYTES64 (bytes));
274     STORE_STRING (pBuf, protocolName);
275
276     IceWriteData (iceConn, PADDED_BYTES64 (bytes), pStart);
277     IceFlush (iceConn);
278 }
279
280
281 void
282 _IceErrorMajorOpcodeDuplicate (
283         IceConn iceConn,
284         int     majorOpcode
285 )
286 {
287     char mOp = (char) majorOpcode;
288
289     IceErrorHeader (iceConn,
290         0, ICE_ProtocolSetup,
291         iceConn->receive_sequence,
292         IceFatalToProtocol,
293         IceMajorOpcodeDuplicate,
294         1 /* length */);
295
296     IceWriteData (iceConn, 8, &mOp);
297     IceFlush (iceConn);
298 }
299
300
301 void
302 _IceErrorUnknownProtocol (
303         IceConn iceConn,
304         char    *protocolName
305 )
306 {
307     char *pBuf, *pStart;
308     int bytes;
309
310     if (!protocolName)
311         protocolName = "";
312     bytes = STRING_BYTES (protocolName);
313
314     IceErrorHeader (iceConn,
315         0, ICE_ProtocolSetup,
316         iceConn->receive_sequence,
317         IceFatalToProtocol,
318         IceUnknownProtocol,
319         WORD64COUNT (bytes));
320
321     pBuf = pStart = IceAllocScratch (iceConn, PADDED_BYTES64 (bytes));
322     STORE_STRING (pBuf, protocolName);
323
324     IceWriteData (iceConn, PADDED_BYTES64 (bytes), pStart);
325     IceFlush (iceConn);
326 }
327
328
329 void
330 _IceErrorBadMajor (
331         IceConn iceConn,
332         int     offendingMajor,
333         int     offendingMinor,
334         int     severity
335 )
336 {
337     char maj = (char) offendingMajor;
338
339     IceErrorHeader (iceConn,
340         0, offendingMinor,
341         iceConn->receive_sequence,
342         severity,
343         IceBadMajor,
344         1 /* length */);
345
346     IceWriteData (iceConn, 8, &maj);
347     IceFlush (iceConn);
348 }
349
350
351 \f
352 /*
353  * Default error handler.
354  */
355
356 static void
357 _IceDefaultErrorHandler (
358         IceConn         iceConn,
359         Bool            swap,
360         int             offendingMinorOpcode,
361         unsigned long   offendingSequence,
362         int             errorClass,
363         int             severity,
364         IcePointer      values
365 )
366 {
367     char *str;
368     char *pData = (char *) values;
369
370     switch (offendingMinorOpcode)
371     {
372         case ICE_ConnectionSetup:
373             str = "ConnectionSetup";
374             break;
375         case ICE_AuthRequired:
376             str = "AuthRequired";
377             break;
378         case ICE_AuthReply:
379             str = "AuthReply";
380             break;
381         case ICE_AuthNextPhase:
382             str = "AuthNextPhase";
383             break;
384         case ICE_ConnectionReply:
385             str = "ConnectionReply";
386             break;
387         case ICE_ProtocolSetup:
388             str = "ProtocolSetup";
389             break;
390         case ICE_ProtocolReply:
391             str = "ProtocolReply";
392             break;
393         case ICE_Ping:
394             str = "Ping";
395             break;
396         case ICE_PingReply:
397             str = "PingReply";
398             break;
399         case ICE_WantToClose:
400             str = "WantToClose";
401             break;
402         case ICE_NoClose:
403             str = "NoClose";
404             break;
405         default:
406             str = "";
407         }
408
409     fprintf (stderr, "\n");
410
411     fprintf (stderr, "ICE error:  Offending minor opcode    = %d (%s)\n",
412         offendingMinorOpcode, str);
413
414     fprintf (stderr, "            Offending sequence number = %lu\n",
415         offendingSequence);
416
417     switch (errorClass)
418     {
419         case IceBadMinor:
420             str = "BadMinor";
421             break;
422         case IceBadState:
423             str = "BadState";
424             break;
425         case IceBadLength:
426             str = "BadLength";
427             break;
428         case IceBadValue:
429             str = "BadValue";
430             break;
431         case IceBadMajor:
432             str = "BadMajor";
433             break;
434         case IceNoAuth:
435             str = "NoAuthentication";
436             break;
437         case IceNoVersion:
438             str = "NoVersion";
439             break;
440         case IceSetupFailed:
441             str = "SetupFailed";
442             break;
443         case IceAuthRejected:
444             str = "AuthenticationRejected";
445             break;
446         case IceAuthFailed:
447             str = "AuthenticationFailed";
448             break;
449         case IceProtocolDuplicate:
450             str = "ProtocolDuplicate";
451             break;
452         case IceMajorOpcodeDuplicate:
453             str = "MajorOpcodeDuplicate";
454             break;
455         case IceUnknownProtocol:
456             str = "UnknownProtocol";
457             break;
458         default:
459             str = "???";
460     }
461
462     fprintf (stderr, "            Error class               = %s\n", str);
463
464     if (severity == IceCanContinue)
465         str = "CanContinue";
466     else if (severity == IceFatalToProtocol)
467         str = "FatalToProtocol";
468     else if (severity == IceFatalToConnection)
469         str = "FatalToConnection";
470     else
471         str = "???";
472
473     fprintf (stderr, "            Severity                  = %s\n", str);
474
475     switch (errorClass)
476     {
477         case IceBadValue:
478         {
479             int offset, length, val;
480
481             EXTRACT_CARD32 (pData, swap, offset);
482             EXTRACT_CARD32 (pData, swap, length);
483
484             fprintf (stderr,
485                 "            BadValue Offset           = %d\n", offset);
486             fprintf (stderr,
487                 "            BadValue Length           = %d\n", length);
488
489             if (length <= 4)
490             {
491                 if (length == 1)
492                     val = (int) *pData;
493                 else if (length == 2)
494                 {
495                     EXTRACT_CARD16 (pData, swap, val);
496                 }
497                 else
498                 {
499                     EXTRACT_CARD32 (pData, swap, val);
500                 }
501
502                 fprintf (stderr,
503                     "            BadValue                  = %d\n", val);
504             }
505             break;
506         }
507
508         case IceBadMajor:
509
510             fprintf (stderr, "Major opcode : %d\n", (int) *pData);
511             break;
512
513         case IceSetupFailed:
514
515             EXTRACT_STRING (pData, swap, str);
516             fprintf (stderr, "Reason : %s\n", str);
517             free(str);
518             break;
519
520         case IceAuthRejected:
521
522             EXTRACT_STRING (pData, swap, str);
523             fprintf (stderr, "Reason : %s\n", str);
524             free(str);
525             break;
526
527         case IceAuthFailed:
528
529             EXTRACT_STRING (pData, swap, str);
530             fprintf (stderr, "Reason : %s\n", str);
531             free(str);
532             break;
533
534         case IceProtocolDuplicate:
535
536             EXTRACT_STRING (pData, swap, str);
537             fprintf (stderr, "Protocol name : %s\n", str);
538             free(str);
539             break;
540
541         case IceMajorOpcodeDuplicate:
542
543             fprintf (stderr, "Major opcode : %d\n", (int) *pData);
544             break;
545
546         case IceUnknownProtocol:
547
548             EXTRACT_STRING (pData, swap, str);
549             fprintf (stderr, "Protocol name : %s\n", str);
550             free(str);
551             break;
552
553         default:
554             break;
555     }
556
557     fprintf (stderr, "\n");
558
559     if (severity != IceCanContinue)
560         exit (1);
561 }
562
563 IceErrorHandler   _IceErrorHandler   = _IceDefaultErrorHandler;
564
565 \f
566 /* 
567  * This procedure sets the ICE error handler to be the specified
568  * routine.  If NULL is passed in the default error handler is restored.
569  * The function's return value is the previous error handler.
570  */
571  
572 IceErrorHandler
573 IceSetErrorHandler (
574         IceErrorHandler handler
575 )
576 {
577     IceErrorHandler oldHandler = _IceErrorHandler;
578
579     if (handler != NULL)
580         _IceErrorHandler = handler;
581     else
582         _IceErrorHandler = _IceDefaultErrorHandler;
583
584     return (oldHandler);
585 }
586
587
588 \f
589 /*
590  * Default IO error handler.
591  */
592
593 static void
594 _IceDefaultIOErrorHandler (
595         IceConn         iceConn
596 )
597 {
598     fprintf (stderr,
599         "ICE default IO error handler doing an exit(), pid = %ld, errno = %d\n",
600         (long)getpid(), errno);
601
602     exit (1);
603 }
604
605 IceIOErrorHandler _IceIOErrorHandler = _IceDefaultIOErrorHandler;
606
607 \f
608 /* 
609  * This procedure sets the ICE fatal I/O error handler to be the
610  * specified routine.  If NULL is passed in the default error
611  * handler is restored.   The function's return value is the
612  * previous error handler.
613  */
614  
615 IceIOErrorHandler
616 IceSetIOErrorHandler (
617         IceIOErrorHandler handler
618 )
619 {
620     IceIOErrorHandler oldHandler = _IceIOErrorHandler;
621
622     if (handler != NULL)
623         _IceIOErrorHandler = handler;
624     else
625         _IceIOErrorHandler = _IceDefaultIOErrorHandler;
626
627     return (oldHandler);
628 }