summaryrefslogtreecommitdiff
path: root/gnu/usr.sbin/sendmail/libsm/debug.c
blob: 5a201967563f303676304567e1a01ac11da468f3 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * 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("@(#)$Sendmail: debug.c,v 1.25 2001/09/04 22:41:27 ca Exp $")

/*
**  libsm debugging and tracing
**  For documentation, see debug.html.
*/

#include <ctype.h>
#include <stdlib.h>
#include <setjmp.h>
#include <sm/io.h>
#include <sm/assert.h>
#include <sm/conf.h>
#include <sm/debug.h>
#include <sm/string.h>
#include <sm/varargs.h>
#include <sm/heap.h>

/*
**  Abstractions for printing trace messages.
*/

/*
**  The output file to which trace output is directed.
**  There is a controversy over whether this variable
**  should be process global or thread local.
**  To make the interface more abstract, we've hidden the
**  variable behind access functions.
*/

static SM_FILE_T *SmDebugOutput = smioout;

/*
**  SM_DEBUG_FILE -- Returns current debug file pointer.
**
**	Parameters:
**		none.
**
**	Returns:
**		current debug file pointer.
*/

SM_FILE_T *
sm_debug_file()
{
	return SmDebugOutput;
}

/*
**  SM_DEBUG_SETFILE -- Sets debug file pointer.
**
**	Parameters:
**		fp -- new debug file pointer.
**
**	Returns:
**		none.
**
**	Side Effects:
**		Sets SmDebugOutput.
*/

void
sm_debug_setfile(fp)
	SM_FILE_T *fp;
{
	SmDebugOutput = fp;
}

/*
**  SM_DPRINTF -- printf() for debug output.
**
**	Parameters:
**		fmt -- format for printf()
**
**	Returns:
**		none.
*/

void
#if SM_VA_STD
sm_dprintf(char *fmt, ...)
#else /* SM_VA_STD */
sm_dprintf(fmt, va_alist)
	char *fmt;
	va_dcl
#endif /* SM_VA_STD */
{
	SM_VA_LOCAL_DECL

	SM_VA_START(ap, fmt);
	sm_io_vfprintf(SmDebugOutput, SmDebugOutput->f_timeout, fmt, ap);
	SM_VA_END(ap);
}

/*
**  SM_DFLUSH -- Flush debug output.
**
**	Parameters:
**		none.
**
**	Returns:
**		none.
*/

void
sm_dflush()
{
	sm_io_flush(SmDebugOutput, SM_TIME_DEFAULT);
}

/*
**  This is the internal database of debug settings.
**  The semantics of looking up a setting in the settings database
**  are that the *last* setting specified in a -d option on the sendmail
**  command line that matches a given SM_DEBUG structure is the one that is
**  used.  That is necessary to conform to the existing semantics of
**  the sendmail -d option.  We store the settings as a linked list in
**  reverse order, so when we do a lookup, we take the *first* entry
**  that matches.
*/

typedef struct sm_debug_setting SM_DEBUG_SETTING_T;
struct sm_debug_setting
{
	const char		*ds_pattern;
	unsigned int		ds_level;
	SM_DEBUG_SETTING_T	*ds_next;
};
SM_DEBUG_SETTING_T *SmDebugSettings = NULL;

/*
**  We keep a linked list of SM_DEBUG structures that have been initialized,
**  for use by sm_debug_reset.
*/

SM_DEBUG_T *SmDebugInitialized = NULL;

const char SmDebugMagic[] = "sm_debug";

/*
**  SM_DEBUG_RESET -- Reset SM_DEBUG structures.
**
**	Reset all SM_DEBUG structures back to the uninitialized state.
**	This is used by sm_debug_addsetting to ensure that references to
**	SM_DEBUG structures that occur before sendmail processes its -d flags
**	do not cause those structures to be permanently forced to level 0.
**
**	Parameters:
**		none.
**
**	Returns:
**		none.
*/

void
sm_debug_reset()
{
	SM_DEBUG_T *debug;

	for (debug = SmDebugInitialized;
	     debug != NULL;
	     debug = debug->debug_next)
	{
		debug->debug_level = SM_DEBUG_UNKNOWN;
	}
	SmDebugInitialized = NULL;
}

/*
**  SM_DEBUG_ADDSETTING_X -- add an entry to the database of debug settings
**
**	Parameters:
**		pattern -- a shell-style glob pattern (see sm_match).
**			WARNING: the storage for 'pattern' will be owned by
**			the debug package, so it should either be a string
**			literal or the result of a call to sm_strdup_x.
**		level -- a non-negative integer.
**
**	Returns:
**		none.
**
**	Exceptions:
**		F:sm_heap -- out of memory
*/

void
sm_debug_addsetting_x(pattern, level)
	const char *pattern;
	int level;
{
	SM_DEBUG_SETTING_T *s;

	SM_REQUIRE(pattern != NULL);
	SM_REQUIRE(level >= 0);
	s = sm_malloc_x(sizeof(SM_DEBUG_SETTING_T));
	s->ds_pattern = pattern;
	s->ds_level = (unsigned int) level;
	s->ds_next = SmDebugSettings;
	SmDebugSettings = s;
	sm_debug_reset();
}

/*
**  PARSE_NAMED_SETTING_X -- process a symbolic debug setting
**
**	Parameters:
**		s -- Points to a non-empty \0 or , terminated string,
**		     of which the initial character is not a digit.
**
**	Returns:
**		pointer to terminating \0 or , character.
**
**	Exceptions:
**		F:sm.heap -- out of memory.
**
**	Side Effects:
**		adds the setting to the database.
*/

static const char *
parse_named_setting_x(s)
	register const char *s;
{
	const char *pat, *endpat;
	int level;

	pat = s;
	while (*s != '\0' && *s != ',' && *s != '.')
		++s;
	endpat = s;
	if (*s == '.')
	{
		++s;
		level = 0;
		while (isascii(*s) && isdigit(*s))
		{
			level = level * 10 + (*s - '0');
			++s;
		}
		if (level < 0)
			level = 0;
	}
	else
		level = 1;

	sm_debug_addsetting_x(sm_strndup_x(pat, endpat - pat), level);

	/* skip trailing junk */
	while (*s != '\0' && *s != ',')
		++s;

	return s;
}

/*
**  SM_DEBUG_ADDSETTINGS_X -- process a list of debug options
**
**	Parameters:
**		s -- a list of debug settings, eg the argument to the
**		     sendmail -d option.
**
**		The syntax of the string s is as follows:
**
**		<settings> ::= <setting> | <settings> "," <setting>
**		<setting> ::= <categories> | <categories> "." <level>
**		<categories> ::= [a-zA-Z_*?][a-zA-Z0-9_*?]*
**
**		However, note that we skip over anything we don't
**		understand, rather than report an error.
**
**	Returns:
**		none.
**
**	Exceptions:
**		F:sm.heap -- out of memory
**
**	Side Effects:
**		updates the database of debug settings.
*/

void
sm_debug_addsettings_x(s)
	register const char *s;
{
	for (;;)
	{
		if (*s == '\0')
			return;
		if (*s == ',')
		{
			++s;
			continue;
		}
		s = parse_named_setting_x(s);
	}
}

/*
**  SM_DEBUG_LOADLEVEL -- Get activation level of the specified debug object.
**
**	Parameters:
**		debug -- debug object.
**
**	Returns:
**		Activation level of the specified debug object.
**
**	Side Effects:
**		Ensures that the debug object is initialized.
*/

int
sm_debug_loadlevel(debug)
	SM_DEBUG_T *debug;
{
	if (debug->debug_level == SM_DEBUG_UNKNOWN)
	{
		SM_DEBUG_SETTING_T *s;

		for (s = SmDebugSettings; s != NULL; s = s->ds_next)
		{
			if (sm_match(debug->debug_name, s->ds_pattern))
			{
				debug->debug_level = s->ds_level;
				goto initialized;
			}
		}
		debug->debug_level = 0;
	initialized:
		debug->debug_next = SmDebugInitialized;
		SmDebugInitialized = debug;
	}
	return (int) debug->debug_level;
}

/*
**  SM_DEBUG_LOADACTIVE -- Activation level reached?
**
**	Parameters:
**		debug -- debug object.
**		level -- level to check.
**
**	Returns:
**		true iff the activation level of the specified debug
**			object >= level.
**
**	Side Effects:
**		Ensures that the debug object is initialized.
*/

bool
sm_debug_loadactive(debug, level)
	SM_DEBUG_T *debug;
	int level;
{
	return sm_debug_loadlevel(debug) >= level;
}