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