10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC")
3*11038SRao.Shoaib@Sun.COM * Copyright (C) 1997, 1999, 2001 Internet Software Consortium.
40Sstevel@tonic-gate *
5*11038SRao.Shoaib@Sun.COM * Permission to use, copy, modify, and/or distribute this software for any
60Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate *
9*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*11038SRao.Shoaib@Sun.COM * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*11038SRao.Shoaib@Sun.COM * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*11038SRao.Shoaib@Sun.COM * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*11038SRao.Shoaib@Sun.COM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*11038SRao.Shoaib@Sun.COM * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*11038SRao.Shoaib@Sun.COM * PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate */
170Sstevel@tonic-gate
180Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER)
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: assertions.c,v 1.5 2008/11/14 02:36:51 marka Exp $";
200Sstevel@tonic-gate #endif
210Sstevel@tonic-gate
220Sstevel@tonic-gate #include "port_before.h"
230Sstevel@tonic-gate
240Sstevel@tonic-gate #include <errno.h>
250Sstevel@tonic-gate #include <stdio.h>
260Sstevel@tonic-gate #include <stdlib.h>
270Sstevel@tonic-gate #include <string.h>
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <isc/assertions.h>
300Sstevel@tonic-gate
310Sstevel@tonic-gate #include "port_after.h"
320Sstevel@tonic-gate
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate * Forward.
350Sstevel@tonic-gate */
360Sstevel@tonic-gate
370Sstevel@tonic-gate static void default_assertion_failed(const char *, int, assertion_type,
380Sstevel@tonic-gate const char *, int);
390Sstevel@tonic-gate
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate * Public.
420Sstevel@tonic-gate */
430Sstevel@tonic-gate
440Sstevel@tonic-gate assertion_failure_callback __assertion_failed = default_assertion_failed;
450Sstevel@tonic-gate
460Sstevel@tonic-gate void
set_assertion_failure_callback(assertion_failure_callback f)470Sstevel@tonic-gate set_assertion_failure_callback(assertion_failure_callback f) {
480Sstevel@tonic-gate if (f == NULL)
490Sstevel@tonic-gate __assertion_failed = default_assertion_failed;
500Sstevel@tonic-gate else
510Sstevel@tonic-gate __assertion_failed = f;
520Sstevel@tonic-gate }
530Sstevel@tonic-gate
540Sstevel@tonic-gate const char *
assertion_type_to_text(assertion_type type)550Sstevel@tonic-gate assertion_type_to_text(assertion_type type) {
560Sstevel@tonic-gate const char *result;
570Sstevel@tonic-gate
580Sstevel@tonic-gate switch (type) {
590Sstevel@tonic-gate case assert_require:
600Sstevel@tonic-gate result = "REQUIRE";
610Sstevel@tonic-gate break;
620Sstevel@tonic-gate case assert_ensure:
630Sstevel@tonic-gate result = "ENSURE";
640Sstevel@tonic-gate break;
650Sstevel@tonic-gate case assert_insist:
660Sstevel@tonic-gate result = "INSIST";
670Sstevel@tonic-gate break;
680Sstevel@tonic-gate case assert_invariant:
690Sstevel@tonic-gate result = "INVARIANT";
700Sstevel@tonic-gate break;
710Sstevel@tonic-gate default:
720Sstevel@tonic-gate result = NULL;
730Sstevel@tonic-gate }
740Sstevel@tonic-gate return (result);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate * Private.
790Sstevel@tonic-gate */
800Sstevel@tonic-gate
81*11038SRao.Shoaib@Sun.COM /* coverity[+kill] */
820Sstevel@tonic-gate static void
default_assertion_failed(const char * file,int line,assertion_type type,const char * cond,int print_errno)830Sstevel@tonic-gate default_assertion_failed(const char *file, int line, assertion_type type,
840Sstevel@tonic-gate const char *cond, int print_errno)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate fprintf(stderr, "%s:%d: %s(%s)%s%s failed.\n",
870Sstevel@tonic-gate file, line, assertion_type_to_text(type), cond,
880Sstevel@tonic-gate (print_errno) ? ": " : "",
890Sstevel@tonic-gate (print_errno) ? strerror(errno) : "");
900Sstevel@tonic-gate abort();
910Sstevel@tonic-gate /* NOTREACHED */
920Sstevel@tonic-gate }
93*11038SRao.Shoaib@Sun.COM
94*11038SRao.Shoaib@Sun.COM /*! \file */
95