xref: /titanic_53/usr/src/cmd/auditd/doorway.c (revision 1a578a15d3f76161f037cd99883a1f54a9eda785)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1a578a15Spaulson  * Common Development and Distribution License (the "License").
6*1a578a15Spaulson  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*1a578a15Spaulson  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Threads:
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * auditd is thread 0 and does signal handling
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * input() is a door server that receives binary audit records and
337c478bd9Sstevel@tonic-gate  * queues them for handling by an instance of process() for conversion to syslog
347c478bd9Sstevel@tonic-gate  * message(s).  There is one process thread per plugin.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * Queues:
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * Each plugin has a buffer pool and and queue for feeding the
397c478bd9Sstevel@tonic-gate  * the process threads.  The input thread moves buffers from the pool
407c478bd9Sstevel@tonic-gate  * to the queue and the process thread puts them back.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * Another pool, b_pool, contains buffers referenced by each of the
437c478bd9Sstevel@tonic-gate  * process queues; this is to minimize the number of buffer copies
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
487c478bd9Sstevel@tonic-gate #include <assert.h>
497c478bd9Sstevel@tonic-gate #include <bsm/adt.h>
507c478bd9Sstevel@tonic-gate #include <dlfcn.h>
517c478bd9Sstevel@tonic-gate #include <errno.h>
527c478bd9Sstevel@tonic-gate #include <fcntl.h>
537c478bd9Sstevel@tonic-gate #include <libintl.h>
547c478bd9Sstevel@tonic-gate #include <pthread.h>
557c478bd9Sstevel@tonic-gate #include <secdb.h>
567c478bd9Sstevel@tonic-gate #include <security/auditd.h>
577c478bd9Sstevel@tonic-gate #include <signal.h>
587c478bd9Sstevel@tonic-gate #include <stdio.h>
597c478bd9Sstevel@tonic-gate #include <stdlib.h>
607c478bd9Sstevel@tonic-gate #include <string.h>
617c478bd9Sstevel@tonic-gate #include <syslog.h>
627c478bd9Sstevel@tonic-gate #include <sys/socket.h>
637c478bd9Sstevel@tonic-gate #include <sys/types.h>
647c478bd9Sstevel@tonic-gate #include <sys/stat.h>
657c478bd9Sstevel@tonic-gate #include <unistd.h>
667c478bd9Sstevel@tonic-gate #include <audit_plugin.h>	/* libbsm */
677c478bd9Sstevel@tonic-gate #include "plugin.h"
687c478bd9Sstevel@tonic-gate #include <bsm/audit_door_infc.h>
697c478bd9Sstevel@tonic-gate #include "audit_sig_infc.h"
707c478bd9Sstevel@tonic-gate #include "queue.h"
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	DEBUG		0
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #if DEBUG
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static FILE *dbfp;
777c478bd9Sstevel@tonic-gate #define	DUMP(w, x, y, z) dump_state(w, x, y, z)
787c478bd9Sstevel@tonic-gate #define	DPRINT(x) {(void) fprintf x; }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #else
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define	DUMP(w, x, y, z)
837c478bd9Sstevel@tonic-gate #define	DPRINT(x)
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #endif
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define	FATAL_MESSAGE_LEN	256
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #define	MIN_RECORD_SIZE	(size_t)25
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate #define	INPUT_MIN		2
927c478bd9Sstevel@tonic-gate #define	THRESHOLD_PCT		75
937c478bd9Sstevel@tonic-gate #define	DEFAULT_BUF_SZ		(size_t)250
947c478bd9Sstevel@tonic-gate #define	BASE_PRIORITY		10	/* 0 - 20 valid for user, time share */
957c478bd9Sstevel@tonic-gate #define	HIGH_PRIORITY		BASE_PRIORITY - 1
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static thr_data_t	in_thr;		/* input thread locks and data */
987c478bd9Sstevel@tonic-gate static int		doorfd = -1;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static int		largest_queue = INPUT_MIN;
1017c478bd9Sstevel@tonic-gate static au_queue_t	b_pool;
1027c478bd9Sstevel@tonic-gate static int		b_allocated = 0;
1037c478bd9Sstevel@tonic-gate static pthread_mutex_t	b_alloc_lock;
1047c478bd9Sstevel@tonic-gate static pthread_mutex_t	b_refcnt_lock;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static void		input(void *, void *, int, door_desc_t *, int);
1077c478bd9Sstevel@tonic-gate static void		process(plugin_t *);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate static audit_q_t	*qpool_withdraw(plugin_t *);
1107c478bd9Sstevel@tonic-gate static void		qpool_init(plugin_t *, int);
1117c478bd9Sstevel@tonic-gate static void		qpool_return(plugin_t *, audit_q_t *);
1127c478bd9Sstevel@tonic-gate static void		qpool_close(plugin_t *);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static audit_rec_t	*bpool_withdraw(char *, size_t, size_t);
1157c478bd9Sstevel@tonic-gate static void		bpool_init();
1167c478bd9Sstevel@tonic-gate static void		bpool_return(audit_rec_t *);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate  * warn_or_fatal() -- log daemon error and (optionally) exit
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate static void
1227c478bd9Sstevel@tonic-gate warn_or_fatal(int fatal, char *parting_shot)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	char	*severity;
1257c478bd9Sstevel@tonic-gate 	char	message[512];
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (fatal)
1287c478bd9Sstevel@tonic-gate 		severity = gettext("fatal error");
1297c478bd9Sstevel@tonic-gate 	else
1307c478bd9Sstevel@tonic-gate 		severity = gettext("warning");
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	(void) snprintf(message, 512, "%s:  %s", severity, parting_shot);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	__audit_syslog("auditd", LOG_PID | LOG_ODELAY | LOG_CONS,
1357c478bd9Sstevel@tonic-gate 	    LOG_DAEMON, LOG_ALERT, message);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "auditd warn_or_fatal %s: %s\n", severity, parting_shot));
1387c478bd9Sstevel@tonic-gate 	if (fatal)
1397c478bd9Sstevel@tonic-gate 		auditd_exit(1);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /* Internal to doorway.c errors... */
1437c478bd9Sstevel@tonic-gate #define	INTERNAL_LOAD_ERROR	-1
1447c478bd9Sstevel@tonic-gate #define	INTERNAL_SYS_ERROR	-2
1457c478bd9Sstevel@tonic-gate #define	INTERNAL_CONFIG_ERROR	-3
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * report_error -- handle errors returned by plugin
1497c478bd9Sstevel@tonic-gate  *
1507c478bd9Sstevel@tonic-gate  * rc is plugin's return code if it is a non-negative value,
1517c478bd9Sstevel@tonic-gate  * otherwise it is a doorway.c code about a plugin.
1527c478bd9Sstevel@tonic-gate  */
1537c478bd9Sstevel@tonic-gate static void
1547c478bd9Sstevel@tonic-gate report_error(auditd_rc_t rc, char *error_text, char *plugin_path)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	int		warn = 0;
1577c478bd9Sstevel@tonic-gate 	char		rcbuf[100]; /* short error name string */
1587c478bd9Sstevel@tonic-gate 	char		message[FATAL_MESSAGE_LEN];
1597c478bd9Sstevel@tonic-gate 	int		bad_count = 0;
1607c478bd9Sstevel@tonic-gate 	char		*name;
1617c478bd9Sstevel@tonic-gate 	char		empty[] = "..";
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	static int	no_plug = 0;
1647c478bd9Sstevel@tonic-gate 	static int	no_load = 0;
1657c478bd9Sstevel@tonic-gate 	static int	no_thread;
1667c478bd9Sstevel@tonic-gate 	static int	no_memory = 0;
1677c478bd9Sstevel@tonic-gate 	static int	invalid = 0;
1687c478bd9Sstevel@tonic-gate 	static int	retry = 0;
1697c478bd9Sstevel@tonic-gate 	static int	fail = 0;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	name = plugin_path;
1727c478bd9Sstevel@tonic-gate 	if (error_text == NULL)
1737c478bd9Sstevel@tonic-gate 		error_text = empty;
1747c478bd9Sstevel@tonic-gate 	if (name == NULL)
1757c478bd9Sstevel@tonic-gate 		name = empty;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	switch (rc) {
1787c478bd9Sstevel@tonic-gate 	case INTERNAL_LOAD_ERROR:
1797c478bd9Sstevel@tonic-gate 		warn = 1;
1807c478bd9Sstevel@tonic-gate 		bad_count = ++no_load;
1817c478bd9Sstevel@tonic-gate 		(void) strcpy(rcbuf, "load_error");
1827c478bd9Sstevel@tonic-gate 		break;
1837c478bd9Sstevel@tonic-gate 	case INTERNAL_SYS_ERROR:
1847c478bd9Sstevel@tonic-gate 		warn = 1;
1857c478bd9Sstevel@tonic-gate 		bad_count = ++no_thread;
1867c478bd9Sstevel@tonic-gate 		(void) strcpy(rcbuf, "sys_error");
1877c478bd9Sstevel@tonic-gate 		break;
1887c478bd9Sstevel@tonic-gate 	case INTERNAL_CONFIG_ERROR:
1897c478bd9Sstevel@tonic-gate 		warn = 1;
1907c478bd9Sstevel@tonic-gate 		bad_count = ++no_plug;
1917c478bd9Sstevel@tonic-gate 		(void) strcpy(rcbuf, "config_error");
1927c478bd9Sstevel@tonic-gate 		name = strdup("--");
1937c478bd9Sstevel@tonic-gate 		break;
1947c478bd9Sstevel@tonic-gate 	case AUDITD_SUCCESS:
1957c478bd9Sstevel@tonic-gate 		break;
1967c478bd9Sstevel@tonic-gate 	case AUDITD_NO_MEMORY:	/* no_memory */
1977c478bd9Sstevel@tonic-gate 		warn = 1;
1987c478bd9Sstevel@tonic-gate 		bad_count = ++no_memory;
1997c478bd9Sstevel@tonic-gate 		(void) strcpy(rcbuf, "no_memory");
2007c478bd9Sstevel@tonic-gate 		break;
2017c478bd9Sstevel@tonic-gate 	case AUDITD_INVALID:	/* invalid */
2027c478bd9Sstevel@tonic-gate 		warn = 1;
2037c478bd9Sstevel@tonic-gate 		bad_count = ++invalid;
2047c478bd9Sstevel@tonic-gate 		(void) strcpy(rcbuf, "invalid");
2057c478bd9Sstevel@tonic-gate 		break;
2067c478bd9Sstevel@tonic-gate 	case AUDITD_RETRY:
2077c478bd9Sstevel@tonic-gate 		warn = 1;
2087c478bd9Sstevel@tonic-gate 		bad_count = ++retry;
2097c478bd9Sstevel@tonic-gate 		(void) strcpy(rcbuf, "retry");
2107c478bd9Sstevel@tonic-gate 		break;
2117c478bd9Sstevel@tonic-gate 	case AUDITD_COMM_FAIL:	/* comm_fail */
2127c478bd9Sstevel@tonic-gate 		(void) strcpy(rcbuf, "comm_fail");
2137c478bd9Sstevel@tonic-gate 		break;
2147c478bd9Sstevel@tonic-gate 	case AUDITD_FATAL:	/* failure */
2157c478bd9Sstevel@tonic-gate 		warn = 1;
2167c478bd9Sstevel@tonic-gate 		bad_count = ++fail;
2177c478bd9Sstevel@tonic-gate 		(void) strcpy(rcbuf, "failure");
2187c478bd9Sstevel@tonic-gate 		break;
2197c478bd9Sstevel@tonic-gate 	default:
2207c478bd9Sstevel@tonic-gate 		(void) strcpy(rcbuf, "error");
2217c478bd9Sstevel@tonic-gate 		break;
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "report_error(%d - %s): %s\n\t%s\n",
2247c478bd9Sstevel@tonic-gate 	    bad_count, name, rcbuf, error_text));
2257c478bd9Sstevel@tonic-gate 	if (warn)
2267c478bd9Sstevel@tonic-gate 		__audit_dowarn2("plugin", name, rcbuf, error_text, bad_count);
2277c478bd9Sstevel@tonic-gate 	else {
2287c478bd9Sstevel@tonic-gate 		(void) snprintf(message, FATAL_MESSAGE_LEN,
2297c478bd9Sstevel@tonic-gate 		    gettext("audit plugin %s reported error = \"%s\": %s\n"),
2307c478bd9Sstevel@tonic-gate 		    name, rcbuf, error_text);
2317c478bd9Sstevel@tonic-gate 		warn_or_fatal(0, message);
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate static size_t
2367c478bd9Sstevel@tonic-gate getlen(char *buf)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	adr_t		adr;
2397c478bd9Sstevel@tonic-gate 	char		tokenid;
2407c478bd9Sstevel@tonic-gate 	uint32_t	len;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	adr.adr_now = buf;
2437c478bd9Sstevel@tonic-gate 	adr.adr_stream = buf;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	adrm_char(&adr, &tokenid, 1);
2467c478bd9Sstevel@tonic-gate 	if ((tokenid == AUT_OHEADER) || (tokenid == AUT_HEADER32) ||
2477c478bd9Sstevel@tonic-gate 	    (tokenid == AUT_HEADER32_EX) || (tokenid == AUT_HEADER64) ||
2487c478bd9Sstevel@tonic-gate 	    (tokenid == AUT_HEADER64_EX)) {
2497c478bd9Sstevel@tonic-gate 		adrm_u_int32(&adr, &len, 1);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 		return (len);
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "getlen() is not looking at a header token\n"));
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	return (0);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  * load_function - call dlsym() to resolve the function address
2607c478bd9Sstevel@tonic-gate  */
2617c478bd9Sstevel@tonic-gate static int
2627c478bd9Sstevel@tonic-gate load_function(plugin_t *p, char *name, auditd_rc_t (**func)())
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	*func = (auditd_rc_t (*)())dlsym(p->plg_dlptr, name);
2657c478bd9Sstevel@tonic-gate 	if (*func == NULL) {
2667c478bd9Sstevel@tonic-gate 		char message[FATAL_MESSAGE_LEN];
2677c478bd9Sstevel@tonic-gate 		char *errmsg = dlerror();
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 		(void) snprintf(message, FATAL_MESSAGE_LEN,
2707c478bd9Sstevel@tonic-gate 		    gettext("dlsym failed %s: error %s"),
2717c478bd9Sstevel@tonic-gate 		    name, errmsg != NULL ? errmsg : gettext("Unknown error\n"));
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 		warn_or_fatal(0, message);
2747c478bd9Sstevel@tonic-gate 		return (-1);
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 	return (0);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate  * load the auditd plug in
2817c478bd9Sstevel@tonic-gate  */
2827c478bd9Sstevel@tonic-gate static int
2837c478bd9Sstevel@tonic-gate load_plugin(plugin_t *p)
2847c478bd9Sstevel@tonic-gate {
2857c478bd9Sstevel@tonic-gate 	struct stat64	stat;
2867c478bd9Sstevel@tonic-gate 	int		fd;
2877c478bd9Sstevel@tonic-gate 	int		fail = 0;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/*
2907c478bd9Sstevel@tonic-gate 	 * Stat the file so we can check modes and ownerships
2917c478bd9Sstevel@tonic-gate 	 */
2927c478bd9Sstevel@tonic-gate 	if ((fd = open(p->plg_path, O_NONBLOCK | O_RDONLY)) != -1) {
2937c478bd9Sstevel@tonic-gate 		if ((fstat64(fd, &stat) == -1) || (!S_ISREG(stat.st_mode)))
2947c478bd9Sstevel@tonic-gate 			fail = 1;
2957c478bd9Sstevel@tonic-gate 	} else
2967c478bd9Sstevel@tonic-gate 		fail = 1;
2977c478bd9Sstevel@tonic-gate 	if (fail) {
2987c478bd9Sstevel@tonic-gate 		char message[FATAL_MESSAGE_LEN];
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 		(void) snprintf(message, FATAL_MESSAGE_LEN,
3017c478bd9Sstevel@tonic-gate 		    gettext("auditd plugin: stat(%s) failed: %s\n"),
3027c478bd9Sstevel@tonic-gate 		    p->plg_path, strerror(errno));
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 		warn_or_fatal(0, message);
3057c478bd9Sstevel@tonic-gate 		return (-1);
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 	/*
3087c478bd9Sstevel@tonic-gate 	 * Check the ownership of the file
3097c478bd9Sstevel@tonic-gate 	 */
3107c478bd9Sstevel@tonic-gate 	if (stat.st_uid != (uid_t)0) {
3117c478bd9Sstevel@tonic-gate 		char message[FATAL_MESSAGE_LEN];
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 		(void) snprintf(message, FATAL_MESSAGE_LEN,
3147c478bd9Sstevel@tonic-gate 		    gettext(
3157c478bd9Sstevel@tonic-gate 		    "auditd plugin: Owner of the module %s is not root\n"),
3167c478bd9Sstevel@tonic-gate 		    p->plg_path);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 		warn_or_fatal(0, message);
3197c478bd9Sstevel@tonic-gate 		return (-1);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 	/*
3227c478bd9Sstevel@tonic-gate 	 * Check the modes on the file
3237c478bd9Sstevel@tonic-gate 	 */
3247c478bd9Sstevel@tonic-gate 	if (stat.st_mode&S_IWGRP) {
3257c478bd9Sstevel@tonic-gate 		char message[FATAL_MESSAGE_LEN];
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 		(void) snprintf(message, FATAL_MESSAGE_LEN,
3287c478bd9Sstevel@tonic-gate 		    gettext("auditd plugin: module %s writable by group\n"),
3297c478bd9Sstevel@tonic-gate 		    p->plg_path);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		warn_or_fatal(0, message);
3327c478bd9Sstevel@tonic-gate 		return (-1);
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 	if (stat.st_mode&S_IWOTH) {
3357c478bd9Sstevel@tonic-gate 		char message[FATAL_MESSAGE_LEN];
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		(void) snprintf(message, FATAL_MESSAGE_LEN,
3387c478bd9Sstevel@tonic-gate 		    gettext("auditd plugin: module %s writable by world\n"),
3397c478bd9Sstevel@tonic-gate 		    p->plg_path);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		warn_or_fatal(0, message);
3427c478bd9Sstevel@tonic-gate 		return (-1);
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 	/*
3457c478bd9Sstevel@tonic-gate 	 * Open the plugin
3467c478bd9Sstevel@tonic-gate 	 */
3477c478bd9Sstevel@tonic-gate 	p->plg_dlptr = dlopen(p->plg_path, RTLD_LAZY);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	if (p->plg_dlptr == NULL) {
3507c478bd9Sstevel@tonic-gate 		char message[FATAL_MESSAGE_LEN];
3517c478bd9Sstevel@tonic-gate 		char *errmsg = dlerror();
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 		(void) snprintf(message, FATAL_MESSAGE_LEN,
3547c478bd9Sstevel@tonic-gate 		    gettext("plugin load %s failed: %s\n"),
3557c478bd9Sstevel@tonic-gate 		    p->plg_path, errmsg != NULL ? errmsg :
3567c478bd9Sstevel@tonic-gate 		    gettext("Unknown error\n"));
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 		warn_or_fatal(0, message);
3597c478bd9Sstevel@tonic-gate 		return (-1);
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 	if (load_function(p, "auditd_plugin", &(p->plg_fplugin)))
3627c478bd9Sstevel@tonic-gate 		return (-1);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	if (load_function(p, "auditd_plugin_open", &(p->plg_fplugin_open)))
3657c478bd9Sstevel@tonic-gate 		return (-1);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	if (load_function(p, "auditd_plugin_close", &(p->plg_fplugin_close)))
3687c478bd9Sstevel@tonic-gate 		return (-1);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	return (0);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate /*
3747c478bd9Sstevel@tonic-gate  * unload_plugin() unlinks and frees the plugin_t structure after
3757c478bd9Sstevel@tonic-gate  * freeing buffers and structures that hang off it.  It also dlcloses
3767c478bd9Sstevel@tonic-gate  * the referenced plugin.  The return is the next entry, which may be NULL
3777c478bd9Sstevel@tonic-gate  *
3787c478bd9Sstevel@tonic-gate  * hold plugin_mutex for this call
3797c478bd9Sstevel@tonic-gate  */
3807c478bd9Sstevel@tonic-gate static plugin_t *
3817c478bd9Sstevel@tonic-gate unload_plugin(plugin_t *p)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	plugin_t	*q, **r;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	assert(pthread_mutex_trylock(&plugin_mutex) != 0);
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "unload_plugin: removing %s\n", p->plg_path));
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	_kva_free(p->plg_kvlist);	/* _kva_free accepts NULL */
3907c478bd9Sstevel@tonic-gate 	qpool_close(p);		/* qpool_close accepts NULL pool, queue */
3917c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "unload_plugin: %s structure removed\n", p->plg_path));
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	(void) dlclose(p->plg_dlptr);
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "unload_plugin: %s dlclosed\n", p->plg_path));
3967c478bd9Sstevel@tonic-gate 	free(p->plg_path);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_destroy(&(p->plg_mutex));
3997c478bd9Sstevel@tonic-gate 	(void) pthread_cond_destroy(&(p->plg_cv));
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	q = plugin_head;
4027c478bd9Sstevel@tonic-gate 	r = &plugin_head;
4037c478bd9Sstevel@tonic-gate 	while (q != NULL) {
4047c478bd9Sstevel@tonic-gate 		if (q == p) {
4057c478bd9Sstevel@tonic-gate 			*r = p->plg_next;
4067c478bd9Sstevel@tonic-gate 			free(p);
4077c478bd9Sstevel@tonic-gate 			break;
4087c478bd9Sstevel@tonic-gate 		}
4097c478bd9Sstevel@tonic-gate 		r = &(q->plg_next);
4107c478bd9Sstevel@tonic-gate 		q = q->plg_next;
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 	return (*r);
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate  * process return values from plugin_open
4177c478bd9Sstevel@tonic-gate  *
4187c478bd9Sstevel@tonic-gate  * presently no attribute is defined.
4197c478bd9Sstevel@tonic-gate  */
4207c478bd9Sstevel@tonic-gate /* ARGSUSED */
4217c478bd9Sstevel@tonic-gate static void
4227c478bd9Sstevel@tonic-gate open_return(plugin_t *p, char *attrval)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate /*
4277c478bd9Sstevel@tonic-gate  * auditd_thread_init
4287c478bd9Sstevel@tonic-gate  *	- create threads
4297c478bd9Sstevel@tonic-gate  *	- load plugins
4307c478bd9Sstevel@tonic-gate  *
4317c478bd9Sstevel@tonic-gate  * auditd_thread_init is called at auditd startup with an initial list
4327c478bd9Sstevel@tonic-gate  * of plugins and again each time audit catches a AU_SIG_READ_CONTROL
4337c478bd9Sstevel@tonic-gate  * or AU_SIG_NEXT_DIR.
4347c478bd9Sstevel@tonic-gate  *
4357c478bd9Sstevel@tonic-gate  */
4367c478bd9Sstevel@tonic-gate int
4377c478bd9Sstevel@tonic-gate auditd_thread_init()
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	int		threshold;
4407c478bd9Sstevel@tonic-gate 	auditd_rc_t	rc;
4417c478bd9Sstevel@tonic-gate 	plugin_t	*p;
4427c478bd9Sstevel@tonic-gate 	char		*open_params;
4437c478bd9Sstevel@tonic-gate 	char		*error_string;
4447c478bd9Sstevel@tonic-gate 	int		plugin_count = 0;
4457c478bd9Sstevel@tonic-gate 	static int	threads_ready = 0;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	if (!threads_ready) {
4487c478bd9Sstevel@tonic-gate 		struct sched_param	param;
4497c478bd9Sstevel@tonic-gate #if DEBUG
4507c478bd9Sstevel@tonic-gate 		dbfp = __auditd_debug_file_open();
4517c478bd9Sstevel@tonic-gate #endif
4527c478bd9Sstevel@tonic-gate 		doorfd = door_create((void(*)())input, 0,
4537c478bd9Sstevel@tonic-gate 		    DOOR_REFUSE_DESC | DOOR_NO_CANCEL);
4547c478bd9Sstevel@tonic-gate 		if (doorfd < 0)
4557c478bd9Sstevel@tonic-gate 			return (1);	/* can't create door -> fatal */
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 		param.sched_priority = BASE_PRIORITY;
4587c478bd9Sstevel@tonic-gate 		(void) pthread_setschedparam(pthread_self(), SCHED_OTHER,
4597c478bd9Sstevel@tonic-gate 		    &param);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 		/* input door server */
4627c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_init(&(in_thr.thd_mutex), NULL);
4637c478bd9Sstevel@tonic-gate 		(void) pthread_cond_init(&(in_thr.thd_cv), NULL);
4647c478bd9Sstevel@tonic-gate 		in_thr.thd_waiting = 0;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		bpool_init();
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 	p = plugin_head;
4697c478bd9Sstevel@tonic-gate 	while (p != NULL) {
4707c478bd9Sstevel@tonic-gate 		if (p->plg_removed) {
4717c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "start removing %s\n", p->plg_path));
4727c478bd9Sstevel@tonic-gate 			/* tell process(p) to exit and dlclose */
4737c478bd9Sstevel@tonic-gate 			(void) pthread_cond_signal(&(p->plg_cv));
4747c478bd9Sstevel@tonic-gate 		} else if (!p->plg_initialized) {
4757c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "start initial load of %s\n",
4767c478bd9Sstevel@tonic-gate 			    p->plg_path));
4777c478bd9Sstevel@tonic-gate 			if (load_plugin(p)) {
4787c478bd9Sstevel@tonic-gate 				report_error(INTERNAL_LOAD_ERROR,
4797c478bd9Sstevel@tonic-gate 				    gettext("dynamic load failed"),
4807c478bd9Sstevel@tonic-gate 				    p->plg_path);
4817c478bd9Sstevel@tonic-gate 				p = unload_plugin(p);
4827c478bd9Sstevel@tonic-gate 				continue;
4837c478bd9Sstevel@tonic-gate 			}
4847c478bd9Sstevel@tonic-gate 			open_params = NULL;
4857c478bd9Sstevel@tonic-gate 			error_string = NULL;
4867c478bd9Sstevel@tonic-gate 			if ((rc = p->plg_fplugin_open(
4877c478bd9Sstevel@tonic-gate 			    p->plg_kvlist,
4887c478bd9Sstevel@tonic-gate 			    &open_params, &error_string)) != AUDITD_SUCCESS) {
4897c478bd9Sstevel@tonic-gate 				report_error(rc, error_string, p->plg_path);
4907c478bd9Sstevel@tonic-gate 				free(error_string);
4917c478bd9Sstevel@tonic-gate 				p = unload_plugin(p);
4927c478bd9Sstevel@tonic-gate 				continue;
4937c478bd9Sstevel@tonic-gate 			}
4947c478bd9Sstevel@tonic-gate 			open_return(p, open_params);
4957c478bd9Sstevel@tonic-gate 			p->plg_reopen = 0;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 			threshold = ((p->plg_qmax * THRESHOLD_PCT) + 99) / 100;
4987c478bd9Sstevel@tonic-gate 			p->plg_qmin = INPUT_MIN;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 			DPRINT((dbfp,
5017c478bd9Sstevel@tonic-gate 			    "calling qpool_init for %s with qmax=%d\n",
5027c478bd9Sstevel@tonic-gate 			    p->plg_path, p->plg_qmax));
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 			qpool_init(p, threshold);
5057c478bd9Sstevel@tonic-gate 			audit_queue_init(&(p->plg_queue));
5067c478bd9Sstevel@tonic-gate 			p->plg_initialized = 1;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_init(&(p->plg_mutex), NULL);
5097c478bd9Sstevel@tonic-gate 			(void) pthread_cond_init(&(p->plg_cv), NULL);
5107c478bd9Sstevel@tonic-gate 			p->plg_waiting = 0;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 			if (pthread_create(&(p->plg_tid), NULL,
5137c478bd9Sstevel@tonic-gate 			    (void *(*)(void *))process, p)) {
5147c478bd9Sstevel@tonic-gate 				report_error(INTERNAL_SYS_ERROR,
5157c478bd9Sstevel@tonic-gate 				    gettext("thread creation failed"),
5167c478bd9Sstevel@tonic-gate 				    p->plg_path);
5177c478bd9Sstevel@tonic-gate 				p = unload_plugin(p);
5187c478bd9Sstevel@tonic-gate 				continue;
5197c478bd9Sstevel@tonic-gate 			}
5207c478bd9Sstevel@tonic-gate 		} else if (p->plg_reopen) {
5217c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "reopen %s\n", p->plg_path));
5227c478bd9Sstevel@tonic-gate 			error_string = NULL;
5237c478bd9Sstevel@tonic-gate 			if ((rc = p->plg_fplugin_open(
5247c478bd9Sstevel@tonic-gate 			    p->plg_kvlist,
5257c478bd9Sstevel@tonic-gate 			    &open_params, &error_string)) != AUDITD_SUCCESS) {
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 				report_error(rc, error_string, p->plg_path);
5287c478bd9Sstevel@tonic-gate 				free(error_string);
5297c478bd9Sstevel@tonic-gate 				p = unload_plugin(p);
5307c478bd9Sstevel@tonic-gate 				continue;
5317c478bd9Sstevel@tonic-gate 			}
5327c478bd9Sstevel@tonic-gate 			open_return(p, open_params);
5337c478bd9Sstevel@tonic-gate 			p->plg_reopen = 0;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "%s qmax=%d\n",
5367c478bd9Sstevel@tonic-gate 			    p->plg_path, p->plg_qmax));
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 		}
5397c478bd9Sstevel@tonic-gate 		p->plg_q_threshold = ((p->plg_qmax * THRESHOLD_PCT) + 99) / 100;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 		p = p->plg_next;
5427c478bd9Sstevel@tonic-gate 		plugin_count++;
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 	if (plugin_count == 0) {
5457c478bd9Sstevel@tonic-gate 		report_error(INTERNAL_CONFIG_ERROR,
5467c478bd9Sstevel@tonic-gate 		    gettext("No plugins are configured"), NULL);
5477c478bd9Sstevel@tonic-gate 		return (-1);
5487c478bd9Sstevel@tonic-gate 	}
5497c478bd9Sstevel@tonic-gate 	if (!threads_ready) {
5507c478bd9Sstevel@tonic-gate 		/* unleash the kernel */
5517c478bd9Sstevel@tonic-gate 		rc = auditdoor(doorfd);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 		DPRINT((dbfp, "%d returned from auditdoor.\n",
5547c478bd9Sstevel@tonic-gate 		    rc));
5557c478bd9Sstevel@tonic-gate 		if (rc != 0)
5567c478bd9Sstevel@tonic-gate 			return (1);	/* fatal */
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 		threads_ready = 1;
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 	return (0);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate /*
5647c478bd9Sstevel@tonic-gate  * Door invocations that are in progress during a
5657c478bd9Sstevel@tonic-gate  * door_revoke() invocation are allowed to complete normally.
5667c478bd9Sstevel@tonic-gate  * -- man page for door_revoke()
5677c478bd9Sstevel@tonic-gate  */
5687c478bd9Sstevel@tonic-gate void
5697c478bd9Sstevel@tonic-gate auditd_thread_close()
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 	if (doorfd == -1)
5727c478bd9Sstevel@tonic-gate 		return;
5737c478bd9Sstevel@tonic-gate 	(void) door_revoke(doorfd);
5747c478bd9Sstevel@tonic-gate 	doorfd = -1;
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate /*
5787c478bd9Sstevel@tonic-gate  * qpool_init() sets up pool for queue entries (audit_q_t)
5797c478bd9Sstevel@tonic-gate  *
5807c478bd9Sstevel@tonic-gate  */
5817c478bd9Sstevel@tonic-gate static void
5827c478bd9Sstevel@tonic-gate qpool_init(plugin_t *p, int threshold)
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate 	int		i;
5857c478bd9Sstevel@tonic-gate 	audit_q_t	*node;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	audit_queue_init(&(p->plg_pool));
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "qpool_init(%d) max, min, threshhold = %d, %d, %d\n",
5907c478bd9Sstevel@tonic-gate 	    p->plg_tid, p->plg_qmax, p->plg_qmin, threshold));
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	if (p->plg_qmax > largest_queue)
5937c478bd9Sstevel@tonic-gate 		largest_queue = p->plg_qmax;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	p->plg_q_threshold = threshold;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	for (i = 0; i < p->plg_qmin; i++) {
5987c478bd9Sstevel@tonic-gate 		node = malloc(sizeof (audit_q_t));
5997c478bd9Sstevel@tonic-gate 		if (node == NULL)
6007c478bd9Sstevel@tonic-gate 			warn_or_fatal(1, gettext("no memory\n"));
6017c478bd9Sstevel@tonic-gate 			/* doesn't return */
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 		audit_enqueue(&p->plg_pool, node);
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate /*
6087c478bd9Sstevel@tonic-gate  * bpool_init() sets up pool and queue for record entries (audit_rec_t)
6097c478bd9Sstevel@tonic-gate  *
6107c478bd9Sstevel@tonic-gate  */
6117c478bd9Sstevel@tonic-gate static void
6127c478bd9Sstevel@tonic-gate bpool_init()
6137c478bd9Sstevel@tonic-gate {
6147c478bd9Sstevel@tonic-gate 	int		i;
6157c478bd9Sstevel@tonic-gate 	audit_rec_t	*node;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	audit_queue_init(&b_pool);
6187c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&b_alloc_lock, NULL);
6197c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&b_refcnt_lock, NULL);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	for (i = 0; i < INPUT_MIN; i++) {
6227c478bd9Sstevel@tonic-gate 		node = malloc(AUDIT_REC_HEADER + DEFAULT_BUF_SZ);
6237c478bd9Sstevel@tonic-gate 		if (node == NULL)
6247c478bd9Sstevel@tonic-gate 			warn_or_fatal(1, gettext("no memory\n"));
6257c478bd9Sstevel@tonic-gate 			/* doesn't return */
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 		node->abq_buf_len = DEFAULT_BUF_SZ;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 		node->abq_data_len = 0;
6307c478bd9Sstevel@tonic-gate 		audit_enqueue(&b_pool, node);
6317c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&b_alloc_lock);
6327c478bd9Sstevel@tonic-gate 		b_allocated++;
6337c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&b_alloc_lock);
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate /*
6387c478bd9Sstevel@tonic-gate  * qpool_close() discard queue and pool for a discontinued plugin
6397c478bd9Sstevel@tonic-gate  *
6407c478bd9Sstevel@tonic-gate  * there is no corresponding bpool_close() since it would only
6417c478bd9Sstevel@tonic-gate  * be called as auditd is going down.
6427c478bd9Sstevel@tonic-gate  */
6437c478bd9Sstevel@tonic-gate static void
6447c478bd9Sstevel@tonic-gate qpool_close(plugin_t *p) {
6457c478bd9Sstevel@tonic-gate 	audit_q_t	*q_node;
6467c478bd9Sstevel@tonic-gate 	audit_rec_t	*b_node;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	if (!p->plg_initialized)
6497c478bd9Sstevel@tonic-gate 		return;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	while (audit_dequeue(&(p->plg_pool), (void *)&q_node) == 0) {
6527c478bd9Sstevel@tonic-gate 		free(q_node);
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 	audit_queue_destroy(&(p->plg_pool));
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	while (audit_dequeue(&(p->plg_queue), (void *)&q_node) == 0) {
6577c478bd9Sstevel@tonic-gate 		b_node = audit_release(&b_refcnt_lock, q_node->aqq_data);
6587c478bd9Sstevel@tonic-gate 		if (b_node != NULL)
6597c478bd9Sstevel@tonic-gate 			audit_enqueue(&b_pool, b_node);
6607c478bd9Sstevel@tonic-gate 		free(q_node);
6617c478bd9Sstevel@tonic-gate 	}
6627c478bd9Sstevel@tonic-gate 	audit_queue_destroy(&(p->plg_queue));
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate /*
6667c478bd9Sstevel@tonic-gate  * qpool_withdraw
6677c478bd9Sstevel@tonic-gate  */
6687c478bd9Sstevel@tonic-gate static audit_q_t *
6697c478bd9Sstevel@tonic-gate qpool_withdraw(plugin_t *p)
6707c478bd9Sstevel@tonic-gate {
6717c478bd9Sstevel@tonic-gate 	audit_q_t	*node;
6727c478bd9Sstevel@tonic-gate 	int		rc;
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	/* get a buffer from the pool, if any */
6757c478bd9Sstevel@tonic-gate 	rc = audit_dequeue(&(p->plg_pool), (void *)&node);
6767c478bd9Sstevel@tonic-gate 	if (rc == 0)
6777c478bd9Sstevel@tonic-gate 		return (node);
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	/*
6807c478bd9Sstevel@tonic-gate 	 * the pool is empty: allocate a new element
6817c478bd9Sstevel@tonic-gate 	 */
6827c478bd9Sstevel@tonic-gate 	node = malloc(sizeof (audit_q_t));
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	if (node == NULL)
6857c478bd9Sstevel@tonic-gate 		warn_or_fatal(1, gettext("no memory\n"));
6867c478bd9Sstevel@tonic-gate 		/* doesn't return */
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	return (node);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate /*
6927c478bd9Sstevel@tonic-gate  * bpool_withdraw -- gets a buffer and fills it
6937c478bd9Sstevel@tonic-gate  *
6947c478bd9Sstevel@tonic-gate  */
6957c478bd9Sstevel@tonic-gate static audit_rec_t *
6967c478bd9Sstevel@tonic-gate bpool_withdraw(char *buffer, size_t buff_size, size_t request_size)
6977c478bd9Sstevel@tonic-gate {
6987c478bd9Sstevel@tonic-gate 	audit_rec_t	*node;
6997c478bd9Sstevel@tonic-gate 	int		rc;
7007c478bd9Sstevel@tonic-gate 	size_t		new_length;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	new_length = (request_size > DEFAULT_BUF_SZ) ?
7037c478bd9Sstevel@tonic-gate 	    request_size : DEFAULT_BUF_SZ;
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	/* get a buffer from the pool, if any */
7067c478bd9Sstevel@tonic-gate 	rc = audit_dequeue(&b_pool, (void *)&node);
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "bpool_withdraw buf length=%d,"
7097c478bd9Sstevel@tonic-gate 	    " requested size=%d, dequeue rc=%d\n",
7107c478bd9Sstevel@tonic-gate 	    new_length, request_size, rc));
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	if (rc == 0) {
7137c478bd9Sstevel@tonic-gate 		DPRINT((dbfp, "bpool_withdraw node=%X (pool=%d)\n", node,
7147c478bd9Sstevel@tonic-gate 		    audit_queue_size(&b_pool)));
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 		if (new_length > node->abq_buf_len) {
7177c478bd9Sstevel@tonic-gate 			node = realloc(node, AUDIT_REC_HEADER + new_length);
7187c478bd9Sstevel@tonic-gate 			if (node == NULL)
7197c478bd9Sstevel@tonic-gate 				warn_or_fatal(1, gettext("no memory\n"));
7207c478bd9Sstevel@tonic-gate 				/* no return */
7217c478bd9Sstevel@tonic-gate 		}
7227c478bd9Sstevel@tonic-gate 	} else {
7237c478bd9Sstevel@tonic-gate 		/*
7247c478bd9Sstevel@tonic-gate 		 * the pool is empty: allocate a new element
7257c478bd9Sstevel@tonic-gate 		 */
7267c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&b_alloc_lock);
7277c478bd9Sstevel@tonic-gate 		if (b_allocated >= largest_queue) {
7287c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&b_alloc_lock);
7297c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "bpool_withdraw is over max (pool=%d)\n",
7307c478bd9Sstevel@tonic-gate 			    audit_queue_size(&b_pool)));
7317c478bd9Sstevel@tonic-gate 			return (NULL);
7327c478bd9Sstevel@tonic-gate 		}
7337c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&b_alloc_lock);
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 		node = malloc(AUDIT_REC_HEADER + new_length);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 		if (node == NULL)
7387c478bd9Sstevel@tonic-gate 			warn_or_fatal(1, gettext("no memory\n"));
7397c478bd9Sstevel@tonic-gate 		/* no return */
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&b_alloc_lock);
7427c478bd9Sstevel@tonic-gate 		b_allocated++;
7437c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&b_alloc_lock);
7447c478bd9Sstevel@tonic-gate 		DPRINT((dbfp, "bpool_withdraw node=%X (alloc=%d, pool=%d)\n",
7457c478bd9Sstevel@tonic-gate 		    node, b_allocated, audit_queue_size(&b_pool)));
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 	assert(request_size <= new_length);
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	(void) memcpy(node->abq_buffer, buffer, buff_size);
7507c478bd9Sstevel@tonic-gate 	node->abq_data_len = buff_size;
7517c478bd9Sstevel@tonic-gate 	node->abq_buf_len = new_length;
7527c478bd9Sstevel@tonic-gate 	node->abq_ref_count = 0;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	return (node);
7557c478bd9Sstevel@tonic-gate }
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate /*
7587c478bd9Sstevel@tonic-gate  * qpool_return() moves queue nodes back to the pool queue.
7597c478bd9Sstevel@tonic-gate  *
7607c478bd9Sstevel@tonic-gate  * if the pool is over max, the node is discarded instead.
7617c478bd9Sstevel@tonic-gate  */
7627c478bd9Sstevel@tonic-gate static void
7637c478bd9Sstevel@tonic-gate qpool_return(plugin_t *p, audit_q_t *node)
7647c478bd9Sstevel@tonic-gate {
7657c478bd9Sstevel@tonic-gate 	int	qpool_size;
7667c478bd9Sstevel@tonic-gate 	int	q_size;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate #if DEBUG
7697c478bd9Sstevel@tonic-gate 	uint32_t	sequence = node->aqq_sequence;
7707c478bd9Sstevel@tonic-gate #endif
7717c478bd9Sstevel@tonic-gate 	qpool_size = audit_queue_size(&(p->plg_pool));
7727c478bd9Sstevel@tonic-gate 	q_size = audit_queue_size(&(p->plg_queue));
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	if (qpool_size + q_size > p->plg_qmax)
7757c478bd9Sstevel@tonic-gate 		free(node);
7767c478bd9Sstevel@tonic-gate 	else
7777c478bd9Sstevel@tonic-gate 		audit_enqueue(&(p->plg_pool), node);
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	DPRINT((dbfp,
7807c478bd9Sstevel@tonic-gate 	    "qpool_return(%d):  seq=%d, q size=%d,"
7817c478bd9Sstevel@tonic-gate 	    " pool size=%d (total alloc=%d), threshhold=%d\n",
7827c478bd9Sstevel@tonic-gate 	    p->plg_tid, sequence, q_size, qpool_size,
7837c478bd9Sstevel@tonic-gate 	    q_size + qpool_size, p->plg_q_threshold));
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate /*
7877c478bd9Sstevel@tonic-gate  * bpool_return() moves queue nodes back to the pool queue.
7887c478bd9Sstevel@tonic-gate  */
7897c478bd9Sstevel@tonic-gate static void
7907c478bd9Sstevel@tonic-gate bpool_return(audit_rec_t *node)
7917c478bd9Sstevel@tonic-gate {
7927c478bd9Sstevel@tonic-gate #if DEBUG
7937c478bd9Sstevel@tonic-gate 	audit_rec_t	*copy = node;
7947c478bd9Sstevel@tonic-gate #endif
7957c478bd9Sstevel@tonic-gate 	node = audit_release(&b_refcnt_lock, node); 	/* decrement ref cnt */
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	if (node != NULL) {	/* NULL if ref cnt is not zero */
7987c478bd9Sstevel@tonic-gate 		audit_enqueue(&b_pool, node);
7997c478bd9Sstevel@tonic-gate 		DPRINT((dbfp,
8007c478bd9Sstevel@tonic-gate 		    "bpool_return: requeue %X (allocated=%d,"
8017c478bd9Sstevel@tonic-gate 		    " pool size=%d)\n", node, b_allocated,
8027c478bd9Sstevel@tonic-gate 		    audit_queue_size(&b_pool)));
8037c478bd9Sstevel@tonic-gate 	}
8047c478bd9Sstevel@tonic-gate #if DEBUG
8057c478bd9Sstevel@tonic-gate 	else {
8067c478bd9Sstevel@tonic-gate 		DPRINT((dbfp,
8077c478bd9Sstevel@tonic-gate 		    "bpool_return: decrement count for %X (allocated=%d,"
8087c478bd9Sstevel@tonic-gate 		    " pool size=%d)\n", copy, b_allocated,
8097c478bd9Sstevel@tonic-gate 		    audit_queue_size(&b_pool)));
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate #endif
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate #if DEBUG
8157c478bd9Sstevel@tonic-gate static void
8167c478bd9Sstevel@tonic-gate dump_state(char *src, plugin_t *p, int count, char *msg)
8177c478bd9Sstevel@tonic-gate {
8187c478bd9Sstevel@tonic-gate 	struct sched_param	param;
8197c478bd9Sstevel@tonic-gate 	int			policy;
8207c478bd9Sstevel@tonic-gate /*
8217c478bd9Sstevel@tonic-gate  * count is message sequence
8227c478bd9Sstevel@tonic-gate  */
8237c478bd9Sstevel@tonic-gate 	(void) pthread_getschedparam(p->plg_tid, &policy, &param);
8247c478bd9Sstevel@tonic-gate 	(void) fprintf(dbfp, "%7s(%d/%d) %11s:"
8257c478bd9Sstevel@tonic-gate 	    " input_in_wait=%d"
8267c478bd9Sstevel@tonic-gate 	    " priority=%d"
8277c478bd9Sstevel@tonic-gate 	    " queue size=%d pool size=%d"
8287c478bd9Sstevel@tonic-gate 	    "\n\t"
8297c478bd9Sstevel@tonic-gate 	    "process wait=%d"
8307c478bd9Sstevel@tonic-gate 	    " tossed=%d"
8317c478bd9Sstevel@tonic-gate 	    " queued=%d"
8327c478bd9Sstevel@tonic-gate 	    " written=%d"
8337c478bd9Sstevel@tonic-gate 	    "\n",
8347c478bd9Sstevel@tonic-gate 	    src, p->plg_tid, count, msg,
8357c478bd9Sstevel@tonic-gate 	    in_thr.thd_waiting, param.sched_priority,
8367c478bd9Sstevel@tonic-gate 	    audit_queue_size(&(p->plg_queue)),
8377c478bd9Sstevel@tonic-gate 	    audit_queue_size(&(p->plg_pool)),
8387c478bd9Sstevel@tonic-gate 	    p->plg_waiting, p->plg_tossed,
8397c478bd9Sstevel@tonic-gate 	    p->plg_queued, p->plg_output);
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	(void) fflush(dbfp);
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate #endif
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate /*
8467c478bd9Sstevel@tonic-gate  * policy_is_block: return 1 if the continue policy is off for any active
8477c478bd9Sstevel@tonic-gate  * plugin, else 0
8487c478bd9Sstevel@tonic-gate  */
8497c478bd9Sstevel@tonic-gate static int
8507c478bd9Sstevel@tonic-gate policy_is_block()
8517c478bd9Sstevel@tonic-gate {
8527c478bd9Sstevel@tonic-gate 	plugin_t *p;
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&plugin_mutex);
8557c478bd9Sstevel@tonic-gate 	p = plugin_head;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	while (p != NULL) {
8587c478bd9Sstevel@tonic-gate 		if (p->plg_cnt == 0) {
8597c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&plugin_mutex);
8607c478bd9Sstevel@tonic-gate 			DPRINT((dbfp,
8617c478bd9Sstevel@tonic-gate 			    "policy_is_block:  policy is to block\n"));
8627c478bd9Sstevel@tonic-gate 			return (1);
8637c478bd9Sstevel@tonic-gate 		}
8647c478bd9Sstevel@tonic-gate 		p = p->plg_next;
8657c478bd9Sstevel@tonic-gate 	}
8667c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&plugin_mutex);
8677c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "policy_is_block:  policy is to continue\n"));
8687c478bd9Sstevel@tonic-gate 	return (0);
8697c478bd9Sstevel@tonic-gate }
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate /*
8727c478bd9Sstevel@tonic-gate  * policy_update() -- the kernel has received a policy change.
8737c478bd9Sstevel@tonic-gate  * Presently, the only policy auditd cares about is AUDIT_CNT
8747c478bd9Sstevel@tonic-gate  */
8757c478bd9Sstevel@tonic-gate static void
8767c478bd9Sstevel@tonic-gate policy_update(uint32_t newpolicy)
8777c478bd9Sstevel@tonic-gate {
8787c478bd9Sstevel@tonic-gate 	plugin_t *p;
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "policy change:  %X\n", newpolicy));
8817c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&plugin_mutex);
8827c478bd9Sstevel@tonic-gate 	p = plugin_head;
8837c478bd9Sstevel@tonic-gate 	while (p != NULL) {
8847c478bd9Sstevel@tonic-gate 		p->plg_cnt = (newpolicy & AUDIT_CNT) ? 1 : 0;
8857c478bd9Sstevel@tonic-gate 		(void) pthread_cond_signal(&(p->plg_cv));
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 		DPRINT((dbfp, "policy changed for thread %d\n", p->plg_tid));
8887c478bd9Sstevel@tonic-gate 		p = p->plg_next;
8897c478bd9Sstevel@tonic-gate 	}
8907c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&plugin_mutex);
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate /*
8947c478bd9Sstevel@tonic-gate  * queue_buffer() inputs a buffer and queues for each active plugin if
8957c478bd9Sstevel@tonic-gate  * it represents a complete audit record.  Otherwise it builds a
8967c478bd9Sstevel@tonic-gate  * larger buffer to hold the record and take successive buffers from
8977c478bd9Sstevel@tonic-gate  * c2audit to build a complete record; then queues it for each plugin.
8987c478bd9Sstevel@tonic-gate  *
8997c478bd9Sstevel@tonic-gate  * return 0 if data is queued (or damaged and tossed).  If resources
9007c478bd9Sstevel@tonic-gate  * are not available, return 0 if all active plugins have the cnt
9017c478bd9Sstevel@tonic-gate  * policy set, else 1.  0 is also returned if the input is a control
9027c478bd9Sstevel@tonic-gate  * message.  (aub_buf is aligned on a 64 bit boundary, so casting
9037c478bd9Sstevel@tonic-gate  * it to an integer works just fine.)
9047c478bd9Sstevel@tonic-gate  */
9057c478bd9Sstevel@tonic-gate static int
9067c478bd9Sstevel@tonic-gate queue_buffer(au_dbuf_t *kl)
9077c478bd9Sstevel@tonic-gate {
9087c478bd9Sstevel@tonic-gate 	plugin_t	*p;
9097c478bd9Sstevel@tonic-gate 	audit_rec_t	*b_copy;
9107c478bd9Sstevel@tonic-gate 	audit_q_t	*q_copy;
9117c478bd9Sstevel@tonic-gate 	boolean_t	referenced = 0;
9127c478bd9Sstevel@tonic-gate 	static char	*invalid_msg = "invalid audit record discarded";
913*1a578a15Spaulson 	static char	*invalid_control = "invalid audit control discarded";
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	static audit_rec_t	*alt_b_copy = NULL;
9167c478bd9Sstevel@tonic-gate 	static size_t		alt_length;
9177c478bd9Sstevel@tonic-gate 	static size_t		alt_offset;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	/*
9207c478bd9Sstevel@tonic-gate 	 * the buffer may be a kernel -> auditd message.  (only
9217c478bd9Sstevel@tonic-gate 	 * the policy change message exists so far.)
9227c478bd9Sstevel@tonic-gate 	 */
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	if ((kl->aub_type & AU_DBUF_NOTIFY) != 0) {
9257c478bd9Sstevel@tonic-gate 		uint32_t	control;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 		control = kl->aub_type & ~AU_DBUF_NOTIFY;
9287c478bd9Sstevel@tonic-gate 		switch (control) {
9297c478bd9Sstevel@tonic-gate 		case AU_DBUF_POLICY:
9307c478bd9Sstevel@tonic-gate 			/* LINTED */
9317c478bd9Sstevel@tonic-gate 			policy_update(*(uint32_t *)kl->aub_buf);
9327c478bd9Sstevel@tonic-gate 			break;
9337c478bd9Sstevel@tonic-gate 		case AU_DBUF_SHUTDOWN:
9347c478bd9Sstevel@tonic-gate 			(void) kill(getpid(), AU_SIG_DISABLE);
9357c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "AU_DBUF_SHUTDOWN message\n"));
9367c478bd9Sstevel@tonic-gate 			break;
9377c478bd9Sstevel@tonic-gate 		default:
9387c478bd9Sstevel@tonic-gate 			warn_or_fatal(0, gettext(invalid_control));
9397c478bd9Sstevel@tonic-gate 			break;
9407c478bd9Sstevel@tonic-gate 		}
9417c478bd9Sstevel@tonic-gate 		return (0);
9427c478bd9Sstevel@tonic-gate 	}
9437c478bd9Sstevel@tonic-gate 	/*
9447c478bd9Sstevel@tonic-gate 	 * The test for valid continuation/completion may fail. Need to
9457c478bd9Sstevel@tonic-gate 	 * assume the failure was earlier and that this buffer may
9467c478bd9Sstevel@tonic-gate 	 * be a valid first or complete buffer after discarding the
9477c478bd9Sstevel@tonic-gate 	 * incomplete record
9487c478bd9Sstevel@tonic-gate 	 */
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	if (alt_b_copy != NULL) {
9517c478bd9Sstevel@tonic-gate 		if ((kl->aub_type == AU_DBUF_FIRST) ||
9527c478bd9Sstevel@tonic-gate 		    (kl->aub_type == AU_DBUF_COMPLETE)) {
9537c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "copy is not null, partial is %d\n",
9547c478bd9Sstevel@tonic-gate 			    kl->aub_type));
9557c478bd9Sstevel@tonic-gate 			bpool_return(alt_b_copy);
9567c478bd9Sstevel@tonic-gate 			warn_or_fatal(0, gettext(invalid_msg));
9577c478bd9Sstevel@tonic-gate 			alt_b_copy = NULL;
9587c478bd9Sstevel@tonic-gate 		}
9597c478bd9Sstevel@tonic-gate 	}
9607c478bd9Sstevel@tonic-gate 	if (alt_b_copy != NULL) { /* continue collecting a long record */
9617c478bd9Sstevel@tonic-gate 		if (kl->aub_size + alt_offset > alt_length) {
9627c478bd9Sstevel@tonic-gate 			bpool_return(alt_b_copy);
9637c478bd9Sstevel@tonic-gate 			alt_b_copy = NULL;
9647c478bd9Sstevel@tonic-gate 			warn_or_fatal(0, gettext(invalid_msg));
9657c478bd9Sstevel@tonic-gate 			return (0);
9667c478bd9Sstevel@tonic-gate 		}
9677c478bd9Sstevel@tonic-gate 		(void) memcpy(alt_b_copy->abq_buffer + alt_offset, kl->aub_buf,
9687c478bd9Sstevel@tonic-gate 		    kl->aub_size);
9697c478bd9Sstevel@tonic-gate 		alt_offset += kl->aub_size;
9707c478bd9Sstevel@tonic-gate 		if (kl->aub_type == AU_DBUF_MIDDLE)
9717c478bd9Sstevel@tonic-gate 			return (0);
9727c478bd9Sstevel@tonic-gate 		b_copy = alt_b_copy;
9737c478bd9Sstevel@tonic-gate 		alt_b_copy = NULL;
9747c478bd9Sstevel@tonic-gate 		b_copy->abq_data_len = alt_length;
9757c478bd9Sstevel@tonic-gate 	} else if (kl->aub_type == AU_DBUF_FIRST) {
9767c478bd9Sstevel@tonic-gate 		/* first buffer of a multiple buffer record */
9777c478bd9Sstevel@tonic-gate 		alt_length = getlen(kl->aub_buf);
9787c478bd9Sstevel@tonic-gate 		if ((alt_length < MIN_RECORD_SIZE) ||
9797c478bd9Sstevel@tonic-gate 		    (alt_length <= kl->aub_size)) {
9807c478bd9Sstevel@tonic-gate 			warn_or_fatal(0, gettext(invalid_msg));
9817c478bd9Sstevel@tonic-gate 			return (0);
9827c478bd9Sstevel@tonic-gate 		}
9837c478bd9Sstevel@tonic-gate 		alt_b_copy = bpool_withdraw(kl->aub_buf, kl->aub_size,
9847c478bd9Sstevel@tonic-gate 		    alt_length);
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 		if (alt_b_copy == NULL)
9877c478bd9Sstevel@tonic-gate 			return (policy_is_block());
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 		alt_offset = kl->aub_size;
9907c478bd9Sstevel@tonic-gate 		return (0);
9917c478bd9Sstevel@tonic-gate 	} else { /* one buffer, one record -- the basic case */
9927c478bd9Sstevel@tonic-gate 		if (kl->aub_type != AU_DBUF_COMPLETE) {
9937c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "copy is null, partial is %d\n",
9947c478bd9Sstevel@tonic-gate 			    kl->aub_type));
9957c478bd9Sstevel@tonic-gate 			warn_or_fatal(0, gettext(invalid_msg));
9967c478bd9Sstevel@tonic-gate 			return (0);	/* tossed */
9977c478bd9Sstevel@tonic-gate 		}
9987c478bd9Sstevel@tonic-gate 		b_copy = bpool_withdraw(kl->aub_buf, kl->aub_size,
9997c478bd9Sstevel@tonic-gate 		    kl->aub_size);
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 		if (b_copy == NULL)
10027c478bd9Sstevel@tonic-gate 			return (policy_is_block());
10037c478bd9Sstevel@tonic-gate 	}
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&plugin_mutex);
10067c478bd9Sstevel@tonic-gate 	p = plugin_head;
10077c478bd9Sstevel@tonic-gate 	while (p != NULL) {
10087c478bd9Sstevel@tonic-gate 		if (!p->plg_removed) {
10097c478bd9Sstevel@tonic-gate 			/*
10107c478bd9Sstevel@tonic-gate 			 * Link the record buffer to the input queues.
10117c478bd9Sstevel@tonic-gate 			 * To avoid a race, it is necessary to wait
10127c478bd9Sstevel@tonic-gate 			 * until all reference count increments
10137c478bd9Sstevel@tonic-gate 			 * are complete before queueing q_copy.
10147c478bd9Sstevel@tonic-gate 			 */
10157c478bd9Sstevel@tonic-gate 			audit_incr_ref(&b_refcnt_lock, b_copy);
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 			q_copy = qpool_withdraw(p);
10187c478bd9Sstevel@tonic-gate 			q_copy->aqq_sequence = p->plg_sequence++;
10197c478bd9Sstevel@tonic-gate 			q_copy->aqq_data = b_copy;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 			p->plg_save_q_copy = q_copy;	/* enqueue below */
10227c478bd9Sstevel@tonic-gate 			referenced = 1;
10237c478bd9Sstevel@tonic-gate 		} else
10247c478bd9Sstevel@tonic-gate 			p->plg_save_q_copy = NULL;
10257c478bd9Sstevel@tonic-gate 		p = p->plg_next;
10267c478bd9Sstevel@tonic-gate 	}
10277c478bd9Sstevel@tonic-gate 	/*
10287c478bd9Sstevel@tonic-gate 	 * now that the reference count is updated, queue it.
10297c478bd9Sstevel@tonic-gate 	 */
10307c478bd9Sstevel@tonic-gate 	if (referenced) {
10317c478bd9Sstevel@tonic-gate 		p = plugin_head;
10327c478bd9Sstevel@tonic-gate 		while ((p != NULL) && (p->plg_save_q_copy != NULL)) {
10337c478bd9Sstevel@tonic-gate 			audit_enqueue(&(p->plg_queue), p->plg_save_q_copy);
10347c478bd9Sstevel@tonic-gate 			(void) pthread_cond_signal(&(p->plg_cv));
10357c478bd9Sstevel@tonic-gate 			p->plg_queued++;
10367c478bd9Sstevel@tonic-gate 			p = p->plg_next;
10377c478bd9Sstevel@tonic-gate 		}
10387c478bd9Sstevel@tonic-gate 	} else
10397c478bd9Sstevel@tonic-gate 		bpool_return(b_copy);
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&plugin_mutex);
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	return (0);
10447c478bd9Sstevel@tonic-gate }
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate /*
10477c478bd9Sstevel@tonic-gate  * wait_a_while() -- timed wait in the door server to allow output
10487c478bd9Sstevel@tonic-gate  * time to catch up.
10497c478bd9Sstevel@tonic-gate  */
10507c478bd9Sstevel@tonic-gate static void
10517c478bd9Sstevel@tonic-gate wait_a_while() {
10527c478bd9Sstevel@tonic-gate 	struct timespec delay = {0, 500000000};	/* 1/2 second */;
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&(in_thr.thd_mutex));
10557c478bd9Sstevel@tonic-gate 	in_thr.thd_waiting = 1;
10567c478bd9Sstevel@tonic-gate 	(void) pthread_cond_reltimedwait_np(&(in_thr.thd_cv),
10577c478bd9Sstevel@tonic-gate 	    &(in_thr.thd_mutex), &delay);
10587c478bd9Sstevel@tonic-gate 	in_thr.thd_waiting = 0;
10597c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&(in_thr.thd_mutex));
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate /*
10637c478bd9Sstevel@tonic-gate  * adjust_priority() -- check queue and pools and adjust the priority
10647c478bd9Sstevel@tonic-gate  * for process() accordingly.  If we're way ahead of output, do a
10657c478bd9Sstevel@tonic-gate  * timed wait as well.
10667c478bd9Sstevel@tonic-gate  */
10677c478bd9Sstevel@tonic-gate static void
10687c478bd9Sstevel@tonic-gate adjust_priority() {
10697c478bd9Sstevel@tonic-gate 	int		queue_near_full;
10707c478bd9Sstevel@tonic-gate 	plugin_t	*p;
10717c478bd9Sstevel@tonic-gate 	int		queue_size;
10727c478bd9Sstevel@tonic-gate 	struct sched_param	param;
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	queue_near_full = 0;
10757c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&plugin_mutex);
10767c478bd9Sstevel@tonic-gate 	p = plugin_head;
10777c478bd9Sstevel@tonic-gate 	while (p != NULL) {
10787c478bd9Sstevel@tonic-gate 		queue_size = audit_queue_size(&(p->plg_queue));
10797c478bd9Sstevel@tonic-gate 		if (queue_size > p->plg_q_threshold) {
10807c478bd9Sstevel@tonic-gate 			if (p->plg_priority != HIGH_PRIORITY) {
10817c478bd9Sstevel@tonic-gate 				p->plg_priority =
10827c478bd9Sstevel@tonic-gate 				    param.sched_priority =
10837c478bd9Sstevel@tonic-gate 				    HIGH_PRIORITY;
10847c478bd9Sstevel@tonic-gate 				(void) pthread_setschedparam(p->plg_tid,
10857c478bd9Sstevel@tonic-gate 				    SCHED_OTHER, &param);
10867c478bd9Sstevel@tonic-gate 			}
10877c478bd9Sstevel@tonic-gate 			if (queue_size > p->plg_qmax - p->plg_qmin) {
10887c478bd9Sstevel@tonic-gate 				queue_near_full = 1;
10897c478bd9Sstevel@tonic-gate 				break;
10907c478bd9Sstevel@tonic-gate 			}
10917c478bd9Sstevel@tonic-gate 		}
10927c478bd9Sstevel@tonic-gate 		p = p->plg_next;
10937c478bd9Sstevel@tonic-gate 	}
10947c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&plugin_mutex);
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	if (queue_near_full) {
10977c478bd9Sstevel@tonic-gate 		DPRINT((dbfp,
10987c478bd9Sstevel@tonic-gate 		    "adjust_priority:  input taking a short break\n"));
10997c478bd9Sstevel@tonic-gate 		wait_a_while();
11007c478bd9Sstevel@tonic-gate 		DPRINT((dbfp,
11017c478bd9Sstevel@tonic-gate 		    "adjust_priority:  input back from my break\n"));
11027c478bd9Sstevel@tonic-gate 	}
11037c478bd9Sstevel@tonic-gate }
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate /*
11067c478bd9Sstevel@tonic-gate  * input() is a door server; it blocks if any plugins have full queues
11077c478bd9Sstevel@tonic-gate  * with the continue policy off. (auditconfig -policy -cnt)
11087c478bd9Sstevel@tonic-gate  *
11097c478bd9Sstevel@tonic-gate  * input() is called synchronously from c2audit and is NOT
11107c478bd9Sstevel@tonic-gate  * reentrant due to the (unprotected) static variables in
11117c478bd9Sstevel@tonic-gate  * queue_buffer().  If multiple clients are created, a context
11127c478bd9Sstevel@tonic-gate  * structure will be required for queue_buffer.
11137c478bd9Sstevel@tonic-gate  *
11147c478bd9Sstevel@tonic-gate  * timedwait is used when input() gets too far ahead of process();
11157c478bd9Sstevel@tonic-gate  * the wait terminates either when the set time expires or when
11167c478bd9Sstevel@tonic-gate  * process() signals that it has nearly caught up.
11177c478bd9Sstevel@tonic-gate  */
11187c478bd9Sstevel@tonic-gate /* ARGSUSED */
11197c478bd9Sstevel@tonic-gate static void
11207c478bd9Sstevel@tonic-gate input(void *cookie, void *argp, int arg_size, door_desc_t *dp,
11217c478bd9Sstevel@tonic-gate     int n_descriptors)
11227c478bd9Sstevel@tonic-gate {
11237c478bd9Sstevel@tonic-gate 	int		is_blocked;
11247c478bd9Sstevel@tonic-gate 	plugin_t	*p;
11257c478bd9Sstevel@tonic-gate #if DEBUG
11267c478bd9Sstevel@tonic-gate 	int		loop_count = 0;
11277c478bd9Sstevel@tonic-gate 	static int	call_counter = 0;
11287c478bd9Sstevel@tonic-gate #endif
11297c478bd9Sstevel@tonic-gate 	if (argp == NULL) {
11307c478bd9Sstevel@tonic-gate 		warn_or_fatal(0,
11317c478bd9Sstevel@tonic-gate 		    gettext("invalid data received from c2audit\n"));
11327c478bd9Sstevel@tonic-gate 		goto input_exit;
11337c478bd9Sstevel@tonic-gate 	}
11347c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "%d input new buffer: length=%u, "
11357c478bd9Sstevel@tonic-gate 	    "partial=%u, arg_size=%d\n",
11367c478bd9Sstevel@tonic-gate 	    ++call_counter, ((au_dbuf_t *)argp)->aub_size,
11377c478bd9Sstevel@tonic-gate 	    ((au_dbuf_t *)argp)->aub_type, arg_size));
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	if (((au_dbuf_t *)argp)->aub_size < 1) {
11407c478bd9Sstevel@tonic-gate 		warn_or_fatal(0,
11417c478bd9Sstevel@tonic-gate 		    gettext("invalid data length received from c2audit\n"));
11427c478bd9Sstevel@tonic-gate 		goto input_exit;
11437c478bd9Sstevel@tonic-gate 	}
11447c478bd9Sstevel@tonic-gate 	/*
11457c478bd9Sstevel@tonic-gate 	 * is_blocked is true only if one or more plugins have "no
11467c478bd9Sstevel@tonic-gate 	 * continue" (-cnt) set and one of those has a full queue.
11477c478bd9Sstevel@tonic-gate 	 * All plugins block until success is met.
11487c478bd9Sstevel@tonic-gate 	 */
11497c478bd9Sstevel@tonic-gate 	for (;;) {
11507c478bd9Sstevel@tonic-gate 		DPRINT((dbfp, "%d input is calling queue_buffer\n",
11517c478bd9Sstevel@tonic-gate 		    call_counter));
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 		is_blocked = queue_buffer((au_dbuf_t *)argp);
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 		if (!is_blocked) {
11567c478bd9Sstevel@tonic-gate 			adjust_priority();
11577c478bd9Sstevel@tonic-gate 			break;
11587c478bd9Sstevel@tonic-gate 		} else {
11597c478bd9Sstevel@tonic-gate 			DPRINT((dbfp,
11607c478bd9Sstevel@tonic-gate 			    "%d input blocked (loop=%d)\n",
11617c478bd9Sstevel@tonic-gate 			    call_counter, loop_count));
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 			wait_a_while();
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "%d input unblocked (loop=%d)\n",
11667c478bd9Sstevel@tonic-gate 			    call_counter, loop_count));
11677c478bd9Sstevel@tonic-gate 		}
11687c478bd9Sstevel@tonic-gate #if DEBUG
11697c478bd9Sstevel@tonic-gate 		loop_count++;
11707c478bd9Sstevel@tonic-gate #endif
11717c478bd9Sstevel@tonic-gate 	}
11727c478bd9Sstevel@tonic-gate input_exit:
11737c478bd9Sstevel@tonic-gate 	p = plugin_head;
11747c478bd9Sstevel@tonic-gate 	while (p != NULL) {
11757c478bd9Sstevel@tonic-gate 		(void) pthread_cond_signal(&(p->plg_cv));
11767c478bd9Sstevel@tonic-gate 		p = p->plg_next;
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate 	((au_dbuf_t *)argp)->aub_size = 0;	/* return code */
11797c478bd9Sstevel@tonic-gate 	(void) door_return(argp, sizeof (uint64_t), NULL, 0);
11807c478bd9Sstevel@tonic-gate }
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate /*
11837c478bd9Sstevel@tonic-gate  * process() -- pass a buffer to a plugin
11847c478bd9Sstevel@tonic-gate  */
11857c478bd9Sstevel@tonic-gate static void
11867c478bd9Sstevel@tonic-gate process(plugin_t *p)
11877c478bd9Sstevel@tonic-gate {
11887c478bd9Sstevel@tonic-gate 	int			rc;
11897c478bd9Sstevel@tonic-gate 	audit_rec_t		*b_node;
11907c478bd9Sstevel@tonic-gate 	audit_q_t		*q_node;
11917c478bd9Sstevel@tonic-gate 	auditd_rc_t		plugrc;
11927c478bd9Sstevel@tonic-gate 	char			*error_string;
11937c478bd9Sstevel@tonic-gate 	struct timespec 	delay;
11947c478bd9Sstevel@tonic-gate 	int			sendsignal;
11957c478bd9Sstevel@tonic-gate 	int			queue_len;
11967c478bd9Sstevel@tonic-gate 	struct sched_param	param;
1197*1a578a15Spaulson 	static boolean_t	once = B_FALSE;
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	DPRINT((dbfp, "%s is thread %d\n", p->plg_path, p->plg_tid));
12007c478bd9Sstevel@tonic-gate 	p->plg_priority = param.sched_priority = BASE_PRIORITY;
12017c478bd9Sstevel@tonic-gate 	(void) pthread_setschedparam(p->plg_tid, SCHED_OTHER, &param);
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	delay.tv_nsec = 0;
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	for (;;) {
12067c478bd9Sstevel@tonic-gate 		while (audit_dequeue(&(p->plg_queue), (void *)&q_node) != 0) {
12077c478bd9Sstevel@tonic-gate 			DUMP("process", p, p->plg_last_seq_out, "blocked");
12087c478bd9Sstevel@tonic-gate 			(void) pthread_cond_signal(&(in_thr.thd_cv));
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_lock(&(p->plg_mutex));
12117c478bd9Sstevel@tonic-gate 			p->plg_waiting++;
12127c478bd9Sstevel@tonic-gate 			(void) pthread_cond_wait(&(p->plg_cv),
12137c478bd9Sstevel@tonic-gate 			    &(p->plg_mutex));
12147c478bd9Sstevel@tonic-gate 			p->plg_waiting--;
12157c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&(p->plg_mutex));
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 			if (p->plg_removed)
1218*1a578a15Spaulson 				goto plugin_removed;
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 			DUMP("process", p, p->plg_last_seq_out, "unblocked");
12217c478bd9Sstevel@tonic-gate 		}
12227c478bd9Sstevel@tonic-gate #if DEBUG
12237c478bd9Sstevel@tonic-gate 		if (q_node->aqq_sequence != p->plg_last_seq_out + 1)
12247c478bd9Sstevel@tonic-gate 			(void) fprintf(dbfp,
12257c478bd9Sstevel@tonic-gate 			    "process(%d): buffer sequence=%u but prev=%u\n",
12267c478bd9Sstevel@tonic-gate 			    p->plg_tid, q_node->aqq_sequence,
12277c478bd9Sstevel@tonic-gate 			    p->plg_last_seq_out);
12287c478bd9Sstevel@tonic-gate #endif
12297c478bd9Sstevel@tonic-gate 		error_string = NULL;
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 		b_node = q_node->aqq_data;
1232*1a578a15Spaulson retry_mode:
12337c478bd9Sstevel@tonic-gate 		plugrc = p->plg_fplugin(b_node->abq_buffer,
1234*1a578a15Spaulson 		    b_node->abq_data_len, q_node->aqq_sequence, &error_string);
1235*1a578a15Spaulson 
1236*1a578a15Spaulson 		if (p->plg_removed)
1237*1a578a15Spaulson 			goto plugin_removed;
12387c478bd9Sstevel@tonic-gate #if DEBUG
12397c478bd9Sstevel@tonic-gate 		p->plg_last_seq_out = q_node->aqq_sequence;
12407c478bd9Sstevel@tonic-gate #endif
12417c478bd9Sstevel@tonic-gate 		switch (plugrc) {
12427c478bd9Sstevel@tonic-gate 		case AUDITD_RETRY:
1243*1a578a15Spaulson 			if (!once) {
12447c478bd9Sstevel@tonic-gate 				report_error(plugrc, error_string, p->plg_path);
1245*1a578a15Spaulson 				once = B_TRUE;
1246*1a578a15Spaulson 			}
12477c478bd9Sstevel@tonic-gate 			free(error_string);
12487c478bd9Sstevel@tonic-gate 			error_string = NULL;
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "process(%d) AUDITD_RETRY returned."
12517c478bd9Sstevel@tonic-gate 			    " cnt=%d (if 1, enter retry)\n",
12527c478bd9Sstevel@tonic-gate 			    p->plg_tid, p->plg_cnt));
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 			if (p->plg_cnt)	/* if cnt is on, lose the buffer */
12557c478bd9Sstevel@tonic-gate 				break;
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 			delay.tv_sec = p->plg_retry_time;
12587c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_lock(&(p->plg_mutex));
12597c478bd9Sstevel@tonic-gate 			p->plg_waiting++;
12607c478bd9Sstevel@tonic-gate 			(void) pthread_cond_reltimedwait_np(&(p->plg_cv),
12617c478bd9Sstevel@tonic-gate 			    &(p->plg_mutex), &delay);
12627c478bd9Sstevel@tonic-gate 			p->plg_waiting--;
12637c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&(p->plg_mutex));
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 			DPRINT((dbfp, "left retry mode for %d\n", p->plg_tid));
12667c478bd9Sstevel@tonic-gate 			goto retry_mode;
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 		case AUDITD_SUCCESS:
12697c478bd9Sstevel@tonic-gate 			p->plg_output++;
1270*1a578a15Spaulson 			once = B_FALSE;
12717c478bd9Sstevel@tonic-gate 			break;
12727c478bd9Sstevel@tonic-gate 		default:
12737c478bd9Sstevel@tonic-gate 			report_error(plugrc, error_string, p->plg_path);
12747c478bd9Sstevel@tonic-gate 			free(error_string);
12757c478bd9Sstevel@tonic-gate 			error_string = NULL;
12767c478bd9Sstevel@tonic-gate 			break;
12777c478bd9Sstevel@tonic-gate 		}	/* end switch */
12787c478bd9Sstevel@tonic-gate 		bpool_return(b_node);
12797c478bd9Sstevel@tonic-gate 		qpool_return(p, q_node);
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 		sendsignal = 0;
12827c478bd9Sstevel@tonic-gate 		queue_len = audit_queue_size(&(p->plg_queue));
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&(in_thr.thd_mutex));
12857c478bd9Sstevel@tonic-gate 		if (in_thr.thd_waiting && (queue_len > p->plg_qmin) &&
12867c478bd9Sstevel@tonic-gate 		    (queue_len < p->plg_q_threshold))
12877c478bd9Sstevel@tonic-gate 			sendsignal = 1;
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&(in_thr.thd_mutex));
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 		if (sendsignal) {
12927c478bd9Sstevel@tonic-gate 			(void) pthread_cond_signal(&(in_thr.thd_cv));
12937c478bd9Sstevel@tonic-gate 			/*
12947c478bd9Sstevel@tonic-gate 			 * sched_yield(); does not help
12957c478bd9Sstevel@tonic-gate 			 * performance and in artificial tests
12967c478bd9Sstevel@tonic-gate 			 * (high sustained volume) appears to
12977c478bd9Sstevel@tonic-gate 			 * hurt by adding wide variability in
12987c478bd9Sstevel@tonic-gate 			 * the results.
12997c478bd9Sstevel@tonic-gate 			 */
13007c478bd9Sstevel@tonic-gate 		} else if ((p->plg_priority < BASE_PRIORITY) &&
13017c478bd9Sstevel@tonic-gate 		    (queue_len < p->plg_q_threshold)) {
13027c478bd9Sstevel@tonic-gate 			p->plg_priority = param.sched_priority =
13037c478bd9Sstevel@tonic-gate 			    BASE_PRIORITY;
13047c478bd9Sstevel@tonic-gate 			(void) pthread_setschedparam(p->plg_tid, SCHED_OTHER,
13057c478bd9Sstevel@tonic-gate 			    &param);
13067c478bd9Sstevel@tonic-gate 		}
13077c478bd9Sstevel@tonic-gate 	}	/* end for (;;) */
1308*1a578a15Spaulson plugin_removed:
13097c478bd9Sstevel@tonic-gate 	DUMP("process", p, p->plg_last_seq_out, "exit");
13107c478bd9Sstevel@tonic-gate 	error_string = NULL;
13117c478bd9Sstevel@tonic-gate 	if ((rc = p->plg_fplugin_close(&error_string)) !=
13127c478bd9Sstevel@tonic-gate 	    AUDITD_SUCCESS)
13137c478bd9Sstevel@tonic-gate 		report_error(rc, error_string, p->plg_path);
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	free(error_string);
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&plugin_mutex);
13187c478bd9Sstevel@tonic-gate 	(void) unload_plugin(p);
13197c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&plugin_mutex);
13207c478bd9Sstevel@tonic-gate }
1321