error.c revision 266e564d
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 (iceConn, majorOpcode, offendingMinor, severity)
44
45IceConn	iceConn;
46int	majorOpcode;
47int	offendingMinor;
48int	severity;
49
50{
51    IceErrorHeader (iceConn,
52	majorOpcode, offendingMinor,
53	iceConn->receive_sequence,
54	severity,
55	IceBadMinor,
56	0);
57
58    IceFlush (iceConn);
59}
60
61
62void
63_IceErrorBadState (iceConn, majorOpcode, offendingMinor, severity)
64
65IceConn	iceConn;
66int	majorOpcode;
67int	offendingMinor;
68int	severity;
69
70{
71    IceErrorHeader (iceConn,
72	majorOpcode, offendingMinor,
73	iceConn->receive_sequence,
74	severity,
75	IceBadState,
76	0);
77
78    IceFlush (iceConn);
79}
80
81
82void
83_IceErrorBadLength (iceConn, majorOpcode, offendingMinor, severity)
84
85IceConn	iceConn;
86int	majorOpcode;
87int	offendingMinor;
88int	severity;
89
90{
91    IceErrorHeader (iceConn,
92	majorOpcode, offendingMinor,
93	iceConn->receive_sequence,
94	severity,
95	IceBadLength,
96	0);
97
98    IceFlush (iceConn);
99}
100
101
102void
103_IceErrorBadValue (iceConn, majorOpcode, offendingMinor, offset, length, value)
104
105IceConn		iceConn;
106int		majorOpcode;
107int		offendingMinor;
108int		offset;
109int		length;		/* in bytes */
110IcePointer	value;
111
112{
113    IceErrorHeader (iceConn,
114	majorOpcode, offendingMinor,
115	iceConn->receive_sequence,
116	IceCanContinue,
117	IceBadValue,
118	WORD64COUNT (8 + length));
119
120    IceWriteData32 (iceConn, 4, &offset);
121    IceWriteData32 (iceConn, 4, &length);
122    IceWriteData (iceConn, length, (char *) value);
123
124    if (PAD64 (length))
125	IceWritePad (iceConn, PAD64 (length));
126
127    IceFlush (iceConn);
128}
129
130
131void
132_IceErrorNoAuthentication (iceConn, offendingMinor)
133
134IceConn	iceConn;
135int	offendingMinor;
136
137{
138    int severity = (offendingMinor == ICE_ConnectionSetup) ?
139	IceFatalToConnection : IceFatalToProtocol;
140
141    IceErrorHeader (iceConn,
142	0, offendingMinor,
143	iceConn->receive_sequence,
144	severity,
145	IceNoAuth,
146	0);
147
148    IceFlush (iceConn);
149}
150
151
152void
153_IceErrorNoVersion (iceConn, offendingMinor)
154
155IceConn	iceConn;
156int	offendingMinor;
157
158{
159    int severity = (offendingMinor == ICE_ConnectionSetup) ?
160	IceFatalToConnection : IceFatalToProtocol;
161
162    IceErrorHeader (iceConn,
163	0, offendingMinor,
164	iceConn->receive_sequence,
165	severity,
166	IceNoVersion,
167	0);
168
169    IceFlush (iceConn);
170}
171
172
173void
174_IceErrorSetupFailed (iceConn, offendingMinor, reason)
175
176IceConn	iceConn;
177int	offendingMinor;
178char	*reason;
179
180{
181    char *pBuf, *pStart;
182    int bytes;
183    int severity = (offendingMinor == ICE_ConnectionSetup) ?
184	IceFatalToConnection : IceFatalToProtocol;
185
186    if (!reason)
187	reason = "";
188    bytes = STRING_BYTES (reason);
189
190    IceErrorHeader (iceConn,
191	0, offendingMinor,
192	iceConn->receive_sequence,
193	severity,
194	IceSetupFailed,
195	WORD64COUNT (bytes));
196
197    pBuf = pStart = IceAllocScratch (iceConn, PADDED_BYTES64 (bytes));
198    STORE_STRING (pBuf, reason);
199
200    IceWriteData (iceConn, PADDED_BYTES64 (bytes), pStart);
201    IceFlush (iceConn);
202}
203
204
205void
206_IceErrorAuthenticationRejected (iceConn, offendingMinor, reason)
207
208IceConn	iceConn;
209int	offendingMinor;
210char	*reason;
211
212{
213    char *pBuf, *pStart;
214    int bytes;
215
216    if (!reason)
217	reason = "";
218    bytes = STRING_BYTES (reason);
219
220    IceErrorHeader (iceConn,
221	0, offendingMinor,
222	iceConn->receive_sequence,
223	IceFatalToProtocol,
224	IceAuthRejected,
225	WORD64COUNT (bytes));
226
227    pBuf = pStart = IceAllocScratch (iceConn, PADDED_BYTES64 (bytes));
228    STORE_STRING (pBuf, reason);
229
230    IceWriteData (iceConn, PADDED_BYTES64 (bytes), pStart);
231    IceFlush (iceConn);
232}
233
234
235void
236_IceErrorAuthenticationFailed (iceConn, offendingMinor, reason)
237
238IceConn	iceConn;
239int	offendingMinor;
240char	*reason;
241
242{
243    char *pBuf, *pStart;
244    int bytes;
245
246    if (!reason)
247	reason = "";
248    bytes = STRING_BYTES (reason);
249
250    IceErrorHeader (iceConn,
251	0, offendingMinor,
252	iceConn->receive_sequence,
253	IceFatalToProtocol,
254	IceAuthFailed,
255	WORD64COUNT (bytes));
256
257    pBuf = pStart = IceAllocScratch (iceConn, PADDED_BYTES64 (bytes));
258    STORE_STRING (pBuf, reason);
259
260    IceWriteData (iceConn, PADDED_BYTES64 (bytes), pStart);
261    IceFlush (iceConn);
262}
263
264
265void
266_IceErrorProtocolDuplicate (iceConn, protocolName)
267
268IceConn	iceConn;
269char	*protocolName;
270
271{
272    char *pBuf, *pStart;
273    int bytes;
274
275    if (!protocolName)
276	protocolName = "";
277    bytes = STRING_BYTES (protocolName);
278
279    IceErrorHeader (iceConn,
280	0, ICE_ProtocolSetup,
281	iceConn->receive_sequence,
282	IceFatalToProtocol,
283	IceProtocolDuplicate,
284	WORD64COUNT (bytes));
285
286    pBuf = pStart = IceAllocScratch (iceConn, PADDED_BYTES64 (bytes));
287    STORE_STRING (pBuf, protocolName);
288
289    IceWriteData (iceConn, PADDED_BYTES64 (bytes), pStart);
290    IceFlush (iceConn);
291}
292
293
294void
295_IceErrorMajorOpcodeDuplicate (iceConn, majorOpcode)
296
297IceConn	iceConn;
298int	majorOpcode;
299
300{
301    char mOp = (char) majorOpcode;
302
303    IceErrorHeader (iceConn,
304	0, ICE_ProtocolSetup,
305	iceConn->receive_sequence,
306	IceFatalToProtocol,
307	IceMajorOpcodeDuplicate,
308	1 /* length */);
309
310    IceWriteData (iceConn, 8, &mOp);
311    IceFlush (iceConn);
312}
313
314
315void
316_IceErrorUnknownProtocol (iceConn, protocolName)
317
318IceConn	iceConn;
319char	*protocolName;
320
321{
322    char *pBuf, *pStart;
323    int bytes;
324
325    if (!protocolName)
326	protocolName = "";
327    bytes = STRING_BYTES (protocolName);
328
329    IceErrorHeader (iceConn,
330	0, ICE_ProtocolSetup,
331	iceConn->receive_sequence,
332	IceFatalToProtocol,
333	IceUnknownProtocol,
334	WORD64COUNT (bytes));
335
336    pBuf = pStart = IceAllocScratch (iceConn, PADDED_BYTES64 (bytes));
337    STORE_STRING (pBuf, protocolName);
338
339    IceWriteData (iceConn, PADDED_BYTES64 (bytes), pStart);
340    IceFlush (iceConn);
341}
342
343
344void
345_IceErrorBadMajor (iceConn, offendingMajor, offendingMinor, severity)
346
347IceConn	iceConn;
348int     offendingMajor;
349int     offendingMinor;
350int	severity;
351
352{
353    char maj = (char) offendingMajor;
354
355    IceErrorHeader (iceConn,
356	0, offendingMinor,
357	iceConn->receive_sequence,
358	severity,
359	IceBadMajor,
360	1 /* length */);
361
362    IceWriteData (iceConn, 8, &maj);
363    IceFlush (iceConn);
364}
365
366
367
368/*
369 * Default error handler.
370 */
371
372void
373_IceDefaultErrorHandler (iceConn, swap,
374    offendingMinorOpcode, offendingSequence, errorClass, severity, values)
375
376IceConn		iceConn;
377Bool		swap;
378int		offendingMinorOpcode;
379unsigned long	offendingSequence;
380int 		errorClass;
381int		severity;
382IcePointer	values;
383
384{
385    char *str;
386    char *pData = (char *) values;
387
388    switch (offendingMinorOpcode)
389    {
390        case ICE_ConnectionSetup:
391            str = "ConnectionSetup";
392	    break;
393        case ICE_AuthRequired:
394            str = "AuthRequired";
395	    break;
396        case ICE_AuthReply:
397            str = "AuthReply";
398	    break;
399        case ICE_AuthNextPhase:
400            str = "AuthNextPhase";
401	    break;
402        case ICE_ConnectionReply:
403            str = "ConnectionReply";
404	    break;
405        case ICE_ProtocolSetup:
406            str = "ProtocolSetup";
407	    break;
408        case ICE_ProtocolReply:
409            str = "ProtocolReply";
410	    break;
411        case ICE_Ping:
412            str = "Ping";
413	    break;
414        case ICE_PingReply:
415            str = "PingReply";
416	    break;
417        case ICE_WantToClose:
418            str = "WantToClose";
419	    break;
420        case ICE_NoClose:
421            str = "NoClose";
422	    break;
423	default:
424	    str = "";
425	}
426
427    fprintf (stderr, "\n");
428
429    fprintf (stderr, "ICE error:  Offending minor opcode    = %d (%s)\n",
430	offendingMinorOpcode, str);
431
432    fprintf (stderr, "            Offending sequence number = %lu\n",
433	offendingSequence);
434
435    switch (errorClass)
436    {
437        case IceBadMinor:
438            str = "BadMinor";
439            break;
440        case IceBadState:
441            str = "BadState";
442            break;
443        case IceBadLength:
444            str = "BadLength";
445            break;
446        case IceBadValue:
447            str = "BadValue";
448            break;
449        case IceBadMajor:
450            str = "BadMajor";
451            break;
452        case IceNoAuth:
453            str = "NoAuthentication";
454            break;
455        case IceNoVersion:
456            str = "NoVersion";
457            break;
458        case IceSetupFailed:
459            str = "SetupFailed";
460            break;
461        case IceAuthRejected:
462            str = "AuthenticationRejected";
463            break;
464        case IceAuthFailed:
465            str = "AuthenticationFailed";
466            break;
467        case IceProtocolDuplicate:
468            str = "ProtocolDuplicate";
469            break;
470        case IceMajorOpcodeDuplicate:
471            str = "MajorOpcodeDuplicate";
472            break;
473        case IceUnknownProtocol:
474            str = "UnknownProtocol";
475            break;
476	default:
477	    str = "???";
478    }
479
480    fprintf (stderr, "            Error class               = %s\n", str);
481
482    if (severity == IceCanContinue)
483	str = "CanContinue";
484    else if (severity == IceFatalToProtocol)
485	str = "FatalToProtocol";
486    else if (severity == IceFatalToConnection)
487	str = "FatalToConnection";
488    else
489	str = "???";
490
491    fprintf (stderr, "            Severity                  = %s\n", str);
492
493    switch (errorClass)
494    {
495        case IceBadValue:
496        {
497	    int offset, length, val;
498
499	    EXTRACT_CARD32 (pData, swap, offset);
500	    EXTRACT_CARD32 (pData, swap, length);
501
502	    fprintf (stderr,
503		"            BadValue Offset           = %d\n", offset);
504	    fprintf (stderr,
505		"            BadValue Length           = %d\n", length);
506
507	    if (length <= 4)
508	    {
509		if (length == 1)
510		    val = (int) *pData;
511		else if (length == 2)
512		{
513		    EXTRACT_CARD16 (pData, swap, val);
514		}
515		else
516		{
517		    EXTRACT_CARD32 (pData, swap, val);
518		}
519
520		fprintf (stderr,
521	            "            BadValue                  = %d\n", val);
522	    }
523            break;
524	}
525
526        case IceBadMajor:
527
528	    fprintf (stderr, "Major opcode : %d\n", (int) *pData);
529            break;
530
531        case IceSetupFailed:
532
533	    EXTRACT_STRING (pData, swap, str);
534	    fprintf (stderr, "Reason : %s\n", str);
535	    free(str);
536            break;
537
538        case IceAuthRejected:
539
540	    EXTRACT_STRING (pData, swap, str);
541	    fprintf (stderr, "Reason : %s\n", str);
542	    free(str);
543            break;
544
545        case IceAuthFailed:
546
547	    EXTRACT_STRING (pData, swap, str);
548	    fprintf (stderr, "Reason : %s\n", str);
549	    free(str);
550            break;
551
552        case IceProtocolDuplicate:
553
554	    EXTRACT_STRING (pData, swap, str);
555	    fprintf (stderr, "Protocol name : %s\n", str);
556	    free(str);
557            break;
558
559        case IceMajorOpcodeDuplicate:
560
561	    fprintf (stderr, "Major opcode : %d\n", (int) *pData);
562            break;
563
564        case IceUnknownProtocol:
565
566	    EXTRACT_STRING (pData, swap, str);
567	    fprintf (stderr, "Protocol name : %s\n", str);
568	    free(str);
569            break;
570
571	default:
572	    break;
573    }
574
575    fprintf (stderr, "\n");
576
577    if (severity != IceCanContinue)
578	exit (1);
579}
580
581
582
583/*
584 * This procedure sets the ICE error handler to be the specified
585 * routine.  If NULL is passed in the default error handler is restored.
586 * The function's return value is the previous error handler.
587 */
588
589IceErrorHandler
590IceSetErrorHandler (handler)
591
592IceErrorHandler handler;
593
594{
595    IceErrorHandler oldHandler = _IceErrorHandler;
596
597    if (handler != NULL)
598	_IceErrorHandler = handler;
599    else
600	_IceErrorHandler = _IceDefaultErrorHandler;
601
602    return (oldHandler);
603}
604
605
606
607/*
608 * Default IO error handler.
609 */
610
611void
612_IceDefaultIOErrorHandler (iceConn)
613
614IceConn iceConn;
615
616{
617    fprintf (stderr,
618	"ICE default IO error handler doing an exit(), pid = %ld, errno = %d\n",
619	(long)getpid(), errno);
620
621    exit (1);
622}
623
624
625
626/*
627 * This procedure sets the ICE fatal I/O error handler to be the
628 * specified routine.  If NULL is passed in the default error
629 * handler is restored.   The function's return value is the
630 * previous error handler.
631 */
632
633IceIOErrorHandler
634IceSetIOErrorHandler (handler)
635
636IceIOErrorHandler handler;
637
638{
639    IceIOErrorHandler oldHandler = _IceIOErrorHandler;
640
641    if (handler != NULL)
642	_IceIOErrorHandler = handler;
643    else
644	_IceIOErrorHandler = _IceDefaultIOErrorHandler;
645
646    return (oldHandler);
647}
648