1*eeb34522Schristos /* $NetBSD: assert.c,v 1.18 2017/05/15 16:09:09 christos Exp $ */
22c4d3c4cScgd
3b55fd24aScgd /*-
4b55fd24aScgd * Copyright (c) 1992, 1993
5b55fd24aScgd * The Regents of the University of California. All rights reserved.
67fb7e975Sjtc *
77fb7e975Sjtc * Redistribution and use in source and binary forms, with or without
87fb7e975Sjtc * modification, are permitted provided that the following conditions
97fb7e975Sjtc * are met:
107fb7e975Sjtc * 1. Redistributions of source code must retain the above copyright
117fb7e975Sjtc * notice, this list of conditions and the following disclaimer.
127fb7e975Sjtc * 2. Redistributions in binary form must reproduce the above copyright
137fb7e975Sjtc * notice, this list of conditions and the following disclaimer in the
147fb7e975Sjtc * documentation and/or other materials provided with the distribution.
15eb7c1594Sagc * 3. Neither the name of the University nor the names of its contributors
16b55fd24aScgd * may be used to endorse or promote products derived from this software
17b55fd24aScgd * without specific prior written permission.
187fb7e975Sjtc *
19b55fd24aScgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20b55fd24aScgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21b55fd24aScgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22b55fd24aScgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23b55fd24aScgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24b55fd24aScgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25b55fd24aScgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26b55fd24aScgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27b55fd24aScgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28b55fd24aScgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29b55fd24aScgd * SUCH DAMAGE.
307fb7e975Sjtc */
317fb7e975Sjtc
3226cc2d4fSchristos #include <sys/cdefs.h>
33f23f94cbSmycroft #if defined(LIBC_SCCS) && !defined(lint)
342c4d3c4cScgd #if 0
352c4d3c4cScgd static char sccsid[] = "@(#)assert.c 8.1 (Berkeley) 6/4/93";
362c4d3c4cScgd #else
37*eeb34522Schristos __RCSID("$NetBSD: assert.c,v 1.18 2017/05/15 16:09:09 christos Exp $");
382c4d3c4cScgd #endif
39f23f94cbSmycroft #endif /* LIBC_SCCS and not lint */
40f23f94cbSmycroft
41fd5cb0acSkleink #include "namespace.h"
42b55fd24aScgd #include <sys/types.h>
438423dd34Slukem
44b55fd24aScgd #include <assert.h>
457fb7e975Sjtc #include <stdio.h>
467fb7e975Sjtc #include <stdlib.h>
47*eeb34522Schristos #include <unistd.h>
488423dd34Slukem #include <syslog.h>
49*eeb34522Schristos #include "extern.h"
507fb7e975Sjtc
51*eeb34522Schristos static int
fmtassert(char * buf,size_t len,const char * file,int line,const char * function,const char * failedexpr)52*eeb34522Schristos fmtassert(char *buf, size_t len, const char *file, int line,
53*eeb34522Schristos const char *function, const char *failedexpr)
54f02540ffSkleink {
55*eeb34522Schristos return snprintf_ss(buf, len,
56f02540ffSkleink "assertion \"%s\" failed: file \"%s\", line %d%s%s%s\n",
57f02540ffSkleink failedexpr, file, line,
58f02540ffSkleink function ? ", function \"" : "",
59f02540ffSkleink function ? function : "",
60f02540ffSkleink function ? "\"" : "");
61*eeb34522Schristos }
62*eeb34522Schristos
63*eeb34522Schristos void
__assert13(const char * file,int line,const char * function,const char * failedexpr)64*eeb34522Schristos __assert13(const char *file, int line, const char *function,
65*eeb34522Schristos const char *failedexpr)
66*eeb34522Schristos {
67*eeb34522Schristos char buf[1024];
68*eeb34522Schristos int l = fmtassert(buf, sizeof(buf), file, line, function, failedexpr);
69*eeb34522Schristos if (l < 0)
70*eeb34522Schristos abort();
71*eeb34522Schristos (void)write(STDERR_FILENO, buf, (size_t)l);
72f02540ffSkleink abort();
73f02540ffSkleink /* NOTREACHED */
74f02540ffSkleink }
75f02540ffSkleink
76f02540ffSkleink void
__assert(const char * file,int line,const char * failedexpr)779e66e6d7Sabs __assert(const char *file, int line, const char *failedexpr)
787fb7e975Sjtc {
79aa330a1eSlukem
80aa330a1eSlukem __assert13(file, line, NULL, failedexpr);
81b55fd24aScgd /* NOTREACHED */
827fb7e975Sjtc }
838423dd34Slukem
84aa330a1eSlukem
85aa330a1eSlukem enum {
86aa330a1eSlukem DIAGASSERT_ABORT = 1<<0,
87aa330a1eSlukem DIAGASSERT_STDERR = 1<<1,
88aa330a1eSlukem DIAGASSERT_SYSLOG = 1<<2
89aa330a1eSlukem };
90aa330a1eSlukem
91aa330a1eSlukem static int diagassert_flags = -1;
92aa330a1eSlukem
938423dd34Slukem void
__diagassert13(const char * file,int line,const char * function,const char * failedexpr)949e66e6d7Sabs __diagassert13(const char *file, int line, const char *function,
959e66e6d7Sabs const char *failedexpr)
96f02540ffSkleink {
97aa330a1eSlukem char buf[1024];
98aa330a1eSlukem
99aa330a1eSlukem if (diagassert_flags == -1) {
100aa330a1eSlukem char *p;
101aa330a1eSlukem
102aa330a1eSlukem diagassert_flags = DIAGASSERT_SYSLOG;
103aa330a1eSlukem
104aa330a1eSlukem for (p = getenv("LIBC_DIAGASSERT"); p && *p; p++) {
105aa330a1eSlukem switch (*p) {
106aa330a1eSlukem case 'a':
107aa330a1eSlukem diagassert_flags |= DIAGASSERT_ABORT;
108aa330a1eSlukem break;
109aa330a1eSlukem case 'A':
110aa330a1eSlukem diagassert_flags &= ~DIAGASSERT_ABORT;
111aa330a1eSlukem break;
112aa330a1eSlukem case 'e':
113aa330a1eSlukem diagassert_flags |= DIAGASSERT_STDERR;
114aa330a1eSlukem break;
115aa330a1eSlukem case 'E':
116aa330a1eSlukem diagassert_flags &= ~DIAGASSERT_STDERR;
117aa330a1eSlukem break;
118aa330a1eSlukem case 'l':
119aa330a1eSlukem diagassert_flags |= DIAGASSERT_SYSLOG;
120aa330a1eSlukem break;
121aa330a1eSlukem case 'L':
122aa330a1eSlukem diagassert_flags &= ~DIAGASSERT_SYSLOG;
123aa330a1eSlukem break;
124aa330a1eSlukem }
125aa330a1eSlukem }
126aa330a1eSlukem }
127aa330a1eSlukem
128*eeb34522Schristos fmtassert(buf, sizeof(buf), file, line, function, failedexpr);
129*eeb34522Schristos if (diagassert_flags & DIAGASSERT_STDERR) {
130*eeb34522Schristos char ebuf[1024];
131*eeb34522Schristos int l = snprintf_ss(ebuf, sizeof(ebuf), "%s: %s\n",
132*eeb34522Schristos getprogname(), buf);
133*eeb34522Schristos if (l == -1)
134*eeb34522Schristos abort();
135*eeb34522Schristos (void)write(STDERR_FILENO, ebuf, (size_t)l);
136*eeb34522Schristos }
137*eeb34522Schristos if (diagassert_flags & DIAGASSERT_SYSLOG) {
138*eeb34522Schristos struct syslog_data sdata = SYSLOG_DATA_INIT;
139*eeb34522Schristos syslog_ss(LOG_DEBUG | LOG_USER, &sdata, "%s", buf);
140*eeb34522Schristos }
141aa330a1eSlukem if (diagassert_flags & DIAGASSERT_ABORT)
142aa330a1eSlukem abort();
143f02540ffSkleink }
144f02540ffSkleink
145f02540ffSkleink void
__diagassert(const char * file,int line,const char * failedexpr)1469e66e6d7Sabs __diagassert(const char *file, int line, const char *failedexpr)
1478423dd34Slukem {
148aa330a1eSlukem
149aa330a1eSlukem __diagassert13(file, line, NULL, failedexpr);
1508423dd34Slukem }
151