058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * Copyright (c) 2006 Sendmail, Inc. and its suppliers.
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * All rights reserved.
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * By using this file, you agree to the terms and conditions set
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * forth in the LICENSE file which can be found at the top level of
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * the sendmail distribution.
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#pragma ident "%Z%%M% %I% %E% SMI"
7800901e60d340b6af88e94a2149805dcfcaaf56jbeckSM_RCSID("@(#)$Id: monitor.c,v 8.7 2007/04/23 16:26:28 ca Exp $")
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** Thread Monitoring
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** Todo: more error checking (return code from function calls)
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** add comments.
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck/* mutex protects Mon_cur_ctx, Mon_ctx_head, and ctx_start */
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** Current ctx to monitor.
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** Invariant:
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** Mon_cur_ctx == NULL || Mon_cur_ctx is thread which was started the longest
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** time ago.
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** Basically the entries in the list are ordered by time because new
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** entries are appended at the end. However, due to the concurrent
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** execution (multi-threaded) and no guaranteed order of wakeups
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** after a mutex_lock() attempt, the order might not be strict,
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** i.e., if the list contains e1 and e2 (in that order) then
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** the the start time of e2 can be (slightly) smaller than that of e1.
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** However, this slight inaccurracy should not matter for the proper
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** working of this algorithm.
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeckstatic smfi_hd_T Mon_ctx_head; /* head of the linked list of active contexts */
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** SMFI_SET_MAX_EXEC_TIME -- set maximum execution time for a thread
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** Parameters:
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** tm -- maximum execution time for a thread
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** MI_SUCCESS
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck unsigned int tm;
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** MI_MONITOR_THREAD -- monitoring thread
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** Parameters:
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** arg -- ignored (required by pthread_create())
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** NULL on termination.
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeckstatic void *
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck /* log an error */
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck return (void *)1;
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** NOTE: this is "flow through" code,
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** do NOT use do { } while ("break" is used here!)
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck "WARNING: monitor timeout triggered, now=%ld, end=%ld, tid=%ld, state=0x%x",\
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck if (Mon_cur_ctx != NULL && Mon_cur_ctx->ctx_start > 0)
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck if (Mon_cur_ctx != NULL && Mon_cur_ctx->ctx_start > 0)
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** MI_MONITOR_INIT -- initialize monitoring thread
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** Parameters: none
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck r = thread_create(&tid, mi_monitor_thread, (void *)NULL);
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck if (r != 0)
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** MI_MONITOR_WORK_BEGIN -- record start of thread execution
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** Parameters:
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** ctx -- session context
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** cmd -- milter command char
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck SM_TAILQ_INSERT_TAIL(&Mon_ctx_head, ctx, ctx_mon_link);
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** MI_MONITOR_WORK_END -- record end of thread execution
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** Parameters:
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** ctx -- session context
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck** cmd -- milter command char
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#endif /* _FFR_THREAD_MONITOR */