summaryrefslogtreecommitdiffstats
path: root/contrib/sendmail/libsm/assert.c
blob: efad1ec14e403b9906bd34f9c4dcf250f7805031 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
 *	All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 *
 */

#include <sm/gen.h>
SM_RCSID("@(#)$Id: assert.c,v 1.25 2001/09/11 04:04:47 gshapiro Exp $")

/*
**  Abnormal program termination and assertion checking.
**  For documentation, see assert.html.
*/

#include <signal.h>
#include <stdlib.h>
#include <unistd.h>

#include <sm/assert.h>
#include <sm/exc.h>
#include <sm/io.h>
#include <sm/varargs.h>

/*
**  Debug categories that are used to guard expensive assertion checks.
*/

SM_DEBUG_T SmExpensiveAssert = SM_DEBUG_INITIALIZER("sm_check_assert",
	"@(#)$Debug: sm_check_assert - check assertions $");

SM_DEBUG_T SmExpensiveRequire = SM_DEBUG_INITIALIZER("sm_check_require",
	"@(#)$Debug: sm_check_require - check function preconditions $");

SM_DEBUG_T SmExpensiveEnsure = SM_DEBUG_INITIALIZER("sm_check_ensure",
	"@(#)$Debug: sm_check_ensure - check function postconditions $");

/*
**  Debug category: send self SIGSTOP on fatal error,
**  so that you can run a debugger on the stopped process.
*/

SM_DEBUG_T SmAbortStop = SM_DEBUG_INITIALIZER("sm_abort_stop",
	"@(#)$Debug: sm_abort_stop - stop process on fatal error $");

/*
**  SM_ABORT_DEFAULTHANDLER -- Default procedure for abnormal program
**				termination.
**
**	The goal is to display an error message without disturbing the
**	process state too much, then dump core.
**
**	Parameters:
**		filename -- filename (can be NULL).
**		lineno -- line number.
**		msg -- message.
**
**	Returns:
**		doesn't return.
*/

static void
sm_abort_defaulthandler __P((
	const char *filename,
	int lineno,
	const char *msg));

static void
sm_abort_defaulthandler(filename, lineno, msg)
	const char *filename;
	int lineno;
	const char *msg;
{
	if (filename != NULL)
		sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "%s:%d: %s\n", filename,
			      lineno, msg);
	else
		sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "%s\n", msg);
	sm_io_flush(smioerr, SM_TIME_DEFAULT);
#ifdef SIGSTOP
	if (sm_debug_active(&SmAbortStop, 1))
		kill(getpid(), SIGSTOP);
#endif /* SIGSTOP */
	abort();
}

/*
**  This is the action to be taken to cause abnormal program termination.
*/

static SM_ABORT_HANDLER_T SmAbortHandler = sm_abort_defaulthandler;

/*
**  SM_ABORT_SETHANDLER -- Set handler for SM_ABORT()
**
**	This allows you to set a handler function for causing abnormal
**	program termination; it is called when a logic bug is detected.
**
**	Parameters:
**		f -- handler.
**
**	Returns:
**		none.
*/

void
sm_abort_sethandler(f)
	SM_ABORT_HANDLER_T f;
{
	if (f == NULL)
		SmAbortHandler = sm_abort_defaulthandler;
	else
		SmAbortHandler = f;
}

/*
**  SM_ABORT -- Call it when you have detected a logic bug.
**
**	Parameters:
**		fmt -- format string.
**		... -- arguments.
**
**	Returns:
**		doesn't.
*/

void
#if SM_VA_STD
sm_abort(char *fmt, ...)
#else /* SM_VA_STD */
sm_abort(fmt, va_alist)
	char *fmt;
	va_dcl
#endif /* SM_VA_STD */
{
	char msg[128];
	SM_VA_LOCAL_DECL

	SM_VA_START(ap, fmt);
	sm_vsnprintf(msg, sizeof msg, fmt, ap);
	SM_VA_END(ap);
	sm_abort_at(NULL, 0, msg);
}

/*
**  SM_ABORT_AT -- Initiate abnormal program termination.
**
**	This is the low level function that is called to initiate abnormal
**	program termination.  It prints an error message and terminates the
**	program.  It is called by sm_abort and by the assertion macros.
**	If filename != NULL then filename and lineno specify the line of source
**	code at which the bug was detected.
**
**	Parameters:
**		filename -- filename (can be NULL).
**		lineno -- line number.
**		msg -- message.
**
**	Returns:
**		doesn't.
*/

void
sm_abort_at(filename, lineno, msg)
	const char *filename;
	int lineno;
	const char *msg;
{
	SM_TRY
		(*SmAbortHandler)(filename, lineno, msg);
	SM_EXCEPT(exc, "*")
		sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
			      "exception raised by abort handler:\n");
		sm_exc_print(exc, smioerr);
		sm_io_flush(smioerr, SM_TIME_DEFAULT);
	SM_END_TRY

	/*
	**  SmAbortHandler isn't supposed to return.
	**  Since it has, let's make sure that the program is terminated.
	*/

	abort();
}
OpenPOWER on IntegriCloud