xref: /freebsd/contrib/sendmail/libmilter/handler.c (revision 94c01205742119acdf56dbbd22fc274e5e38cfd4)
106f25ae9SGregory Neil Shapiro /*
294c01205SGregory Neil Shapiro  *  Copyright (c) 1999-2002 Sendmail, Inc. and its suppliers.
306f25ae9SGregory Neil Shapiro  *	All rights reserved.
406f25ae9SGregory Neil Shapiro  *
506f25ae9SGregory Neil Shapiro  * By using this file, you agree to the terms and conditions set
606f25ae9SGregory Neil Shapiro  * forth in the LICENSE file which can be found at the top level of
706f25ae9SGregory Neil Shapiro  * the sendmail distribution.
806f25ae9SGregory Neil Shapiro  *
906f25ae9SGregory Neil Shapiro  */
1006f25ae9SGregory Neil Shapiro 
1140266059SGregory Neil Shapiro #include <sm/gen.h>
1294c01205SGregory Neil Shapiro SM_RCSID("@(#)$Id: handler.c,v 8.30 2002/04/29 15:06:48 ca Exp $")
1306f25ae9SGregory Neil Shapiro 
1406f25ae9SGregory Neil Shapiro #include "libmilter.h"
1506f25ae9SGregory Neil Shapiro 
1606f25ae9SGregory Neil Shapiro 
1706f25ae9SGregory Neil Shapiro /*
1806f25ae9SGregory Neil Shapiro **  HANDLE_SESSION -- Handle a connected session in its own context
1906f25ae9SGregory Neil Shapiro **
2006f25ae9SGregory Neil Shapiro **	Parameters:
2106f25ae9SGregory Neil Shapiro **		ctx -- context structure
2206f25ae9SGregory Neil Shapiro **
2306f25ae9SGregory Neil Shapiro **	Returns:
2406f25ae9SGregory Neil Shapiro **		MI_SUCCESS/MI_FAILURE
2506f25ae9SGregory Neil Shapiro */
2606f25ae9SGregory Neil Shapiro 
2706f25ae9SGregory Neil Shapiro int
2806f25ae9SGregory Neil Shapiro mi_handle_session(ctx)
2906f25ae9SGregory Neil Shapiro 	SMFICTX_PTR ctx;
3006f25ae9SGregory Neil Shapiro {
3106f25ae9SGregory Neil Shapiro 	int ret;
3206f25ae9SGregory Neil Shapiro 
3306f25ae9SGregory Neil Shapiro 	if (ctx == NULL)
3406f25ae9SGregory Neil Shapiro 		return MI_FAILURE;
3506f25ae9SGregory Neil Shapiro 	ctx->ctx_id = (sthread_t) sthread_get_id();
3606f25ae9SGregory Neil Shapiro 
3706f25ae9SGregory Neil Shapiro 	/*
3806f25ae9SGregory Neil Shapiro 	**  detach so resources are free when the thread returns
3906f25ae9SGregory Neil Shapiro 	**  if we ever "wait" for threads, this call must be removed
4006f25ae9SGregory Neil Shapiro 	*/
4194c01205SGregory Neil Shapiro 
4206f25ae9SGregory Neil Shapiro 	if (pthread_detach(ctx->ctx_id) != 0)
4394c01205SGregory Neil Shapiro 		ret = MI_FAILURE;
4494c01205SGregory Neil Shapiro 	else
4506f25ae9SGregory Neil Shapiro 		ret = mi_engine(ctx);
4606f25ae9SGregory Neil Shapiro 	if (ValidSocket(ctx->ctx_sd))
47193538b7SGregory Neil Shapiro 	{
4840266059SGregory Neil Shapiro 		(void) closesocket(ctx->ctx_sd);
49193538b7SGregory Neil Shapiro 		ctx->ctx_sd = INVALID_SOCKET;
50193538b7SGregory Neil Shapiro 	}
5106f25ae9SGregory Neil Shapiro 	if (ctx->ctx_reply != NULL)
52193538b7SGregory Neil Shapiro 	{
5306f25ae9SGregory Neil Shapiro 		free(ctx->ctx_reply);
54193538b7SGregory Neil Shapiro 		ctx->ctx_reply = NULL;
55193538b7SGregory Neil Shapiro 	}
5606f25ae9SGregory Neil Shapiro 	if (ctx->ctx_privdata != NULL)
5706f25ae9SGregory Neil Shapiro 	{
5806f25ae9SGregory Neil Shapiro 		smi_log(SMI_LOG_WARN,
5906f25ae9SGregory Neil Shapiro 			"%s: private data not NULL",
6006f25ae9SGregory Neil Shapiro 			ctx->ctx_smfi->xxfi_name);
6106f25ae9SGregory Neil Shapiro 	}
6206f25ae9SGregory Neil Shapiro 	mi_clr_macros(ctx, 0);
6306f25ae9SGregory Neil Shapiro 	free(ctx);
6406f25ae9SGregory Neil Shapiro 	ctx = NULL;
6506f25ae9SGregory Neil Shapiro 	return ret;
6606f25ae9SGregory Neil Shapiro }
67