10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
55007Spaulson * Common Development and Distribution License (the "License").
65007Spaulson * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*12918SJan.Friedel@Sun.COM * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
2411704SJan.Friedel@Sun.COM
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate * Threads:
270Sstevel@tonic-gate *
280Sstevel@tonic-gate * auditd is thread 0 and does signal handling
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * input() is a door server that receives binary audit records and
310Sstevel@tonic-gate * queues them for handling by an instance of process() for conversion to syslog
320Sstevel@tonic-gate * message(s). There is one process thread per plugin.
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * Queues:
350Sstevel@tonic-gate *
360Sstevel@tonic-gate * Each plugin has a buffer pool and and queue for feeding the
370Sstevel@tonic-gate * the process threads. The input thread moves buffers from the pool
380Sstevel@tonic-gate * to the queue and the process thread puts them back.
390Sstevel@tonic-gate *
400Sstevel@tonic-gate * Another pool, b_pool, contains buffers referenced by each of the
410Sstevel@tonic-gate * process queues; this is to minimize the number of buffer copies
420Sstevel@tonic-gate *
430Sstevel@tonic-gate */
440Sstevel@tonic-gate
450Sstevel@tonic-gate #include <arpa/inet.h>
460Sstevel@tonic-gate #include <assert.h>
470Sstevel@tonic-gate #include <bsm/adt.h>
480Sstevel@tonic-gate #include <dlfcn.h>
490Sstevel@tonic-gate #include <errno.h>
500Sstevel@tonic-gate #include <fcntl.h>
510Sstevel@tonic-gate #include <libintl.h>
520Sstevel@tonic-gate #include <pthread.h>
530Sstevel@tonic-gate #include <secdb.h>
540Sstevel@tonic-gate #include <security/auditd.h>
550Sstevel@tonic-gate #include <signal.h>
560Sstevel@tonic-gate #include <stdio.h>
570Sstevel@tonic-gate #include <stdlib.h>
580Sstevel@tonic-gate #include <string.h>
590Sstevel@tonic-gate #include <syslog.h>
600Sstevel@tonic-gate #include <sys/socket.h>
610Sstevel@tonic-gate #include <sys/types.h>
620Sstevel@tonic-gate #include <sys/stat.h>
630Sstevel@tonic-gate #include <unistd.h>
640Sstevel@tonic-gate #include <audit_plugin.h> /* libbsm */
650Sstevel@tonic-gate #include "plugin.h"
660Sstevel@tonic-gate #include <bsm/audit_door_infc.h>
670Sstevel@tonic-gate #include "queue.h"
680Sstevel@tonic-gate
690Sstevel@tonic-gate #define DEBUG 0
700Sstevel@tonic-gate
7111706SJan.Friedel@Sun.COM /* gettext() obfuscation routine for lint */
7211706SJan.Friedel@Sun.COM #ifdef __lint
7311706SJan.Friedel@Sun.COM #define gettext(x) x
7411706SJan.Friedel@Sun.COM #endif
7511706SJan.Friedel@Sun.COM
760Sstevel@tonic-gate #if DEBUG
770Sstevel@tonic-gate static FILE *dbfp;
780Sstevel@tonic-gate #define DUMP(w, x, y, z) dump_state(w, x, y, z)
7911706SJan.Friedel@Sun.COM #define DPRINT(x) { (void) fprintf x; }
800Sstevel@tonic-gate #else
810Sstevel@tonic-gate #define DUMP(w, x, y, z)
820Sstevel@tonic-gate #define DPRINT(x)
830Sstevel@tonic-gate #endif
840Sstevel@tonic-gate
850Sstevel@tonic-gate #define FATAL_MESSAGE_LEN 256
860Sstevel@tonic-gate
870Sstevel@tonic-gate #define MIN_RECORD_SIZE (size_t)25
880Sstevel@tonic-gate
890Sstevel@tonic-gate #define INPUT_MIN 2
900Sstevel@tonic-gate #define THRESHOLD_PCT 75
910Sstevel@tonic-gate #define DEFAULT_BUF_SZ (size_t)250
920Sstevel@tonic-gate #define BASE_PRIORITY 10 /* 0 - 20 valid for user, time share */
930Sstevel@tonic-gate #define HIGH_PRIORITY BASE_PRIORITY - 1
940Sstevel@tonic-gate
950Sstevel@tonic-gate static thr_data_t in_thr; /* input thread locks and data */
960Sstevel@tonic-gate static int doorfd = -1;
970Sstevel@tonic-gate
980Sstevel@tonic-gate static int largest_queue = INPUT_MIN;
990Sstevel@tonic-gate static au_queue_t b_pool;
1000Sstevel@tonic-gate static int b_allocated = 0;
1010Sstevel@tonic-gate static pthread_mutex_t b_alloc_lock;
1020Sstevel@tonic-gate static pthread_mutex_t b_refcnt_lock;
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate static void input(void *, void *, int, door_desc_t *, int);
1050Sstevel@tonic-gate static void process(plugin_t *);
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate static audit_q_t *qpool_withdraw(plugin_t *);
1080Sstevel@tonic-gate static void qpool_init(plugin_t *, int);
1090Sstevel@tonic-gate static void qpool_return(plugin_t *, audit_q_t *);
1100Sstevel@tonic-gate static void qpool_close(plugin_t *);
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate static audit_rec_t *bpool_withdraw(char *, size_t, size_t);
1130Sstevel@tonic-gate static void bpool_init();
1140Sstevel@tonic-gate static void bpool_return(audit_rec_t *);
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate * warn_or_fatal() -- log daemon error and (optionally) exit
1180Sstevel@tonic-gate */
1190Sstevel@tonic-gate static void
warn_or_fatal(int fatal,char * parting_shot)1200Sstevel@tonic-gate warn_or_fatal(int fatal, char *parting_shot)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate char *severity;
1230Sstevel@tonic-gate char message[512];
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate if (fatal)
1260Sstevel@tonic-gate severity = gettext("fatal error");
1270Sstevel@tonic-gate else
1280Sstevel@tonic-gate severity = gettext("warning");
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate (void) snprintf(message, 512, "%s: %s", severity, parting_shot);
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate __audit_syslog("auditd", LOG_PID | LOG_ODELAY | LOG_CONS,
1330Sstevel@tonic-gate LOG_DAEMON, LOG_ALERT, message);
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate DPRINT((dbfp, "auditd warn_or_fatal %s: %s\n", severity, parting_shot));
1360Sstevel@tonic-gate if (fatal)
1370Sstevel@tonic-gate auditd_exit(1);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate /* Internal to doorway.c errors... */
1410Sstevel@tonic-gate #define INTERNAL_LOAD_ERROR -1
1420Sstevel@tonic-gate #define INTERNAL_SYS_ERROR -2
1430Sstevel@tonic-gate #define INTERNAL_CONFIG_ERROR -3
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate * report_error -- handle errors returned by plugin
1470Sstevel@tonic-gate *
1480Sstevel@tonic-gate * rc is plugin's return code if it is a non-negative value,
1490Sstevel@tonic-gate * otherwise it is a doorway.c code about a plugin.
1500Sstevel@tonic-gate */
1510Sstevel@tonic-gate static void
report_error(auditd_rc_t rc,char * error_text,char * plugin_path)1520Sstevel@tonic-gate report_error(auditd_rc_t rc, char *error_text, char *plugin_path)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate int warn = 0;
1550Sstevel@tonic-gate char rcbuf[100]; /* short error name string */
1560Sstevel@tonic-gate char message[FATAL_MESSAGE_LEN];
1570Sstevel@tonic-gate int bad_count = 0;
1580Sstevel@tonic-gate char *name;
1590Sstevel@tonic-gate char empty[] = "..";
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate static int no_plug = 0;
1620Sstevel@tonic-gate static int no_load = 0;
1630Sstevel@tonic-gate static int no_thread;
1640Sstevel@tonic-gate static int no_memory = 0;
1650Sstevel@tonic-gate static int invalid = 0;
1660Sstevel@tonic-gate static int retry = 0;
1670Sstevel@tonic-gate static int fail = 0;
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate name = plugin_path;
1700Sstevel@tonic-gate if (error_text == NULL)
1710Sstevel@tonic-gate error_text = empty;
1720Sstevel@tonic-gate if (name == NULL)
1730Sstevel@tonic-gate name = empty;
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate switch (rc) {
1760Sstevel@tonic-gate case INTERNAL_LOAD_ERROR:
1770Sstevel@tonic-gate warn = 1;
1780Sstevel@tonic-gate bad_count = ++no_load;
1790Sstevel@tonic-gate (void) strcpy(rcbuf, "load_error");
1800Sstevel@tonic-gate break;
1810Sstevel@tonic-gate case INTERNAL_SYS_ERROR:
1820Sstevel@tonic-gate warn = 1;
1830Sstevel@tonic-gate bad_count = ++no_thread;
1840Sstevel@tonic-gate (void) strcpy(rcbuf, "sys_error");
1850Sstevel@tonic-gate break;
1860Sstevel@tonic-gate case INTERNAL_CONFIG_ERROR:
1870Sstevel@tonic-gate warn = 1;
1880Sstevel@tonic-gate bad_count = ++no_plug;
1890Sstevel@tonic-gate (void) strcpy(rcbuf, "config_error");
1900Sstevel@tonic-gate name = strdup("--");
1910Sstevel@tonic-gate break;
1920Sstevel@tonic-gate case AUDITD_SUCCESS:
1930Sstevel@tonic-gate break;
1940Sstevel@tonic-gate case AUDITD_NO_MEMORY: /* no_memory */
1950Sstevel@tonic-gate warn = 1;
1960Sstevel@tonic-gate bad_count = ++no_memory;
1970Sstevel@tonic-gate (void) strcpy(rcbuf, "no_memory");
1980Sstevel@tonic-gate break;
1990Sstevel@tonic-gate case AUDITD_INVALID: /* invalid */
2000Sstevel@tonic-gate warn = 1;
2010Sstevel@tonic-gate bad_count = ++invalid;
2020Sstevel@tonic-gate (void) strcpy(rcbuf, "invalid");
2030Sstevel@tonic-gate break;
2040Sstevel@tonic-gate case AUDITD_RETRY:
2050Sstevel@tonic-gate warn = 1;
2060Sstevel@tonic-gate bad_count = ++retry;
2070Sstevel@tonic-gate (void) strcpy(rcbuf, "retry");
2080Sstevel@tonic-gate break;
2090Sstevel@tonic-gate case AUDITD_COMM_FAIL: /* comm_fail */
2100Sstevel@tonic-gate (void) strcpy(rcbuf, "comm_fail");
2110Sstevel@tonic-gate break;
2120Sstevel@tonic-gate case AUDITD_FATAL: /* failure */
2130Sstevel@tonic-gate warn = 1;
2140Sstevel@tonic-gate bad_count = ++fail;
2150Sstevel@tonic-gate (void) strcpy(rcbuf, "failure");
2160Sstevel@tonic-gate break;
2170Sstevel@tonic-gate default:
2180Sstevel@tonic-gate (void) strcpy(rcbuf, "error");
2190Sstevel@tonic-gate break;
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate DPRINT((dbfp, "report_error(%d - %s): %s\n\t%s\n",
2220Sstevel@tonic-gate bad_count, name, rcbuf, error_text));
2230Sstevel@tonic-gate if (warn)
2240Sstevel@tonic-gate __audit_dowarn2("plugin", name, rcbuf, error_text, bad_count);
2250Sstevel@tonic-gate else {
2260Sstevel@tonic-gate (void) snprintf(message, FATAL_MESSAGE_LEN,
2270Sstevel@tonic-gate gettext("audit plugin %s reported error = \"%s\": %s\n"),
2280Sstevel@tonic-gate name, rcbuf, error_text);
2290Sstevel@tonic-gate warn_or_fatal(0, message);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate static size_t
getlen(char * buf)2340Sstevel@tonic-gate getlen(char *buf)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate adr_t adr;
2370Sstevel@tonic-gate char tokenid;
2380Sstevel@tonic-gate uint32_t len;
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate adr.adr_now = buf;
2410Sstevel@tonic-gate adr.adr_stream = buf;
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate adrm_char(&adr, &tokenid, 1);
2440Sstevel@tonic-gate if ((tokenid == AUT_OHEADER) || (tokenid == AUT_HEADER32) ||
2450Sstevel@tonic-gate (tokenid == AUT_HEADER32_EX) || (tokenid == AUT_HEADER64) ||
2460Sstevel@tonic-gate (tokenid == AUT_HEADER64_EX)) {
2470Sstevel@tonic-gate adrm_u_int32(&adr, &len, 1);
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate return (len);
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate DPRINT((dbfp, "getlen() is not looking at a header token\n"));
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate return (0);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate /*
2570Sstevel@tonic-gate * load_function - call dlsym() to resolve the function address
2580Sstevel@tonic-gate */
2590Sstevel@tonic-gate static int
load_function(plugin_t * p,char * name,auditd_rc_t (** func)())2600Sstevel@tonic-gate load_function(plugin_t *p, char *name, auditd_rc_t (**func)())
2610Sstevel@tonic-gate {
2620Sstevel@tonic-gate *func = (auditd_rc_t (*)())dlsym(p->plg_dlptr, name);
2630Sstevel@tonic-gate if (*func == NULL) {
2640Sstevel@tonic-gate char message[FATAL_MESSAGE_LEN];
2650Sstevel@tonic-gate char *errmsg = dlerror();
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate (void) snprintf(message, FATAL_MESSAGE_LEN,
2680Sstevel@tonic-gate gettext("dlsym failed %s: error %s"),
2690Sstevel@tonic-gate name, errmsg != NULL ? errmsg : gettext("Unknown error\n"));
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate warn_or_fatal(0, message);
2720Sstevel@tonic-gate return (-1);
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate return (0);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate /*
2780Sstevel@tonic-gate * load the auditd plug in
2790Sstevel@tonic-gate */
2800Sstevel@tonic-gate static int
load_plugin(plugin_t * p)2810Sstevel@tonic-gate load_plugin(plugin_t *p)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate struct stat64 stat;
2840Sstevel@tonic-gate int fd;
2850Sstevel@tonic-gate int fail = 0;
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate * Stat the file so we can check modes and ownerships
2890Sstevel@tonic-gate */
2900Sstevel@tonic-gate if ((fd = open(p->plg_path, O_NONBLOCK | O_RDONLY)) != -1) {
2910Sstevel@tonic-gate if ((fstat64(fd, &stat) == -1) || (!S_ISREG(stat.st_mode)))
2920Sstevel@tonic-gate fail = 1;
2930Sstevel@tonic-gate } else
2940Sstevel@tonic-gate fail = 1;
2950Sstevel@tonic-gate if (fail) {
2960Sstevel@tonic-gate char message[FATAL_MESSAGE_LEN];
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate (void) snprintf(message, FATAL_MESSAGE_LEN,
2990Sstevel@tonic-gate gettext("auditd plugin: stat(%s) failed: %s\n"),
3000Sstevel@tonic-gate p->plg_path, strerror(errno));
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate warn_or_fatal(0, message);
3030Sstevel@tonic-gate return (-1);
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate /*
3060Sstevel@tonic-gate * Check the ownership of the file
3070Sstevel@tonic-gate */
3080Sstevel@tonic-gate if (stat.st_uid != (uid_t)0) {
3090Sstevel@tonic-gate char message[FATAL_MESSAGE_LEN];
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate (void) snprintf(message, FATAL_MESSAGE_LEN,
3120Sstevel@tonic-gate gettext(
3130Sstevel@tonic-gate "auditd plugin: Owner of the module %s is not root\n"),
3140Sstevel@tonic-gate p->plg_path);
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate warn_or_fatal(0, message);
3170Sstevel@tonic-gate return (-1);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate /*
3200Sstevel@tonic-gate * Check the modes on the file
3210Sstevel@tonic-gate */
3220Sstevel@tonic-gate if (stat.st_mode&S_IWGRP) {
3230Sstevel@tonic-gate char message[FATAL_MESSAGE_LEN];
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate (void) snprintf(message, FATAL_MESSAGE_LEN,
3260Sstevel@tonic-gate gettext("auditd plugin: module %s writable by group\n"),
3270Sstevel@tonic-gate p->plg_path);
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate warn_or_fatal(0, message);
3300Sstevel@tonic-gate return (-1);
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate if (stat.st_mode&S_IWOTH) {
3330Sstevel@tonic-gate char message[FATAL_MESSAGE_LEN];
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate (void) snprintf(message, FATAL_MESSAGE_LEN,
3360Sstevel@tonic-gate gettext("auditd plugin: module %s writable by world\n"),
3370Sstevel@tonic-gate p->plg_path);
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate warn_or_fatal(0, message);
3400Sstevel@tonic-gate return (-1);
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate /*
3430Sstevel@tonic-gate * Open the plugin
3440Sstevel@tonic-gate */
3450Sstevel@tonic-gate p->plg_dlptr = dlopen(p->plg_path, RTLD_LAZY);
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate if (p->plg_dlptr == NULL) {
3480Sstevel@tonic-gate char message[FATAL_MESSAGE_LEN];
3490Sstevel@tonic-gate char *errmsg = dlerror();
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate (void) snprintf(message, FATAL_MESSAGE_LEN,
3520Sstevel@tonic-gate gettext("plugin load %s failed: %s\n"),
3530Sstevel@tonic-gate p->plg_path, errmsg != NULL ? errmsg :
3540Sstevel@tonic-gate gettext("Unknown error\n"));
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate warn_or_fatal(0, message);
3570Sstevel@tonic-gate return (-1);
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate if (load_function(p, "auditd_plugin", &(p->plg_fplugin)))
3600Sstevel@tonic-gate return (-1);
3610Sstevel@tonic-gate
3620Sstevel@tonic-gate if (load_function(p, "auditd_plugin_open", &(p->plg_fplugin_open)))
3630Sstevel@tonic-gate return (-1);
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate if (load_function(p, "auditd_plugin_close", &(p->plg_fplugin_close)))
3660Sstevel@tonic-gate return (-1);
3670Sstevel@tonic-gate
3680Sstevel@tonic-gate return (0);
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate
3710Sstevel@tonic-gate /*
3720Sstevel@tonic-gate * unload_plugin() unlinks and frees the plugin_t structure after
3730Sstevel@tonic-gate * freeing buffers and structures that hang off it. It also dlcloses
3740Sstevel@tonic-gate * the referenced plugin. The return is the next entry, which may be NULL
3750Sstevel@tonic-gate *
3760Sstevel@tonic-gate * hold plugin_mutex for this call
3770Sstevel@tonic-gate */
3780Sstevel@tonic-gate static plugin_t *
unload_plugin(plugin_t * p)3790Sstevel@tonic-gate unload_plugin(plugin_t *p)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate plugin_t *q, **r;
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate assert(pthread_mutex_trylock(&plugin_mutex) != 0);
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate DPRINT((dbfp, "unload_plugin: removing %s\n", p->plg_path));
3860Sstevel@tonic-gate
3870Sstevel@tonic-gate _kva_free(p->plg_kvlist); /* _kva_free accepts NULL */
3880Sstevel@tonic-gate qpool_close(p); /* qpool_close accepts NULL pool, queue */
3890Sstevel@tonic-gate DPRINT((dbfp, "unload_plugin: %s structure removed\n", p->plg_path));
3900Sstevel@tonic-gate
3910Sstevel@tonic-gate (void) dlclose(p->plg_dlptr);
3920Sstevel@tonic-gate
3930Sstevel@tonic-gate DPRINT((dbfp, "unload_plugin: %s dlclosed\n", p->plg_path));
3940Sstevel@tonic-gate free(p->plg_path);
3950Sstevel@tonic-gate
3960Sstevel@tonic-gate (void) pthread_mutex_destroy(&(p->plg_mutex));
3970Sstevel@tonic-gate (void) pthread_cond_destroy(&(p->plg_cv));
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate q = plugin_head;
4000Sstevel@tonic-gate r = &plugin_head;
4010Sstevel@tonic-gate while (q != NULL) {
4020Sstevel@tonic-gate if (q == p) {
4030Sstevel@tonic-gate *r = p->plg_next;
4040Sstevel@tonic-gate free(p);
4050Sstevel@tonic-gate break;
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate r = &(q->plg_next);
4080Sstevel@tonic-gate q = q->plg_next;
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate return (*r);
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate /*
4140Sstevel@tonic-gate * process return values from plugin_open
4150Sstevel@tonic-gate *
4160Sstevel@tonic-gate * presently no attribute is defined.
4170Sstevel@tonic-gate */
4180Sstevel@tonic-gate /* ARGSUSED */
4190Sstevel@tonic-gate static void
open_return(plugin_t * p,char * attrval)4200Sstevel@tonic-gate open_return(plugin_t *p, char *attrval)
4210Sstevel@tonic-gate {
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate
4240Sstevel@tonic-gate /*
4250Sstevel@tonic-gate * auditd_thread_init
4260Sstevel@tonic-gate * - create threads
4270Sstevel@tonic-gate * - load plugins
4280Sstevel@tonic-gate *
4290Sstevel@tonic-gate * auditd_thread_init is called at auditd startup with an initial list
430*12918SJan.Friedel@Sun.COM * of plugins and again each time audit catches a SIGHUP or SIGUSR1.
4310Sstevel@tonic-gate */
4320Sstevel@tonic-gate int
auditd_thread_init()4330Sstevel@tonic-gate auditd_thread_init()
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate int threshold;
4360Sstevel@tonic-gate auditd_rc_t rc;
4370Sstevel@tonic-gate plugin_t *p;
4380Sstevel@tonic-gate char *open_params;
4390Sstevel@tonic-gate char *error_string;
4400Sstevel@tonic-gate int plugin_count = 0;
4410Sstevel@tonic-gate static int threads_ready = 0;
4420Sstevel@tonic-gate
4430Sstevel@tonic-gate if (!threads_ready) {
4440Sstevel@tonic-gate struct sched_param param;
4450Sstevel@tonic-gate #if DEBUG
4460Sstevel@tonic-gate dbfp = __auditd_debug_file_open();
4470Sstevel@tonic-gate #endif
4480Sstevel@tonic-gate doorfd = door_create((void(*)())input, 0,
4490Sstevel@tonic-gate DOOR_REFUSE_DESC | DOOR_NO_CANCEL);
4500Sstevel@tonic-gate if (doorfd < 0)
4510Sstevel@tonic-gate return (1); /* can't create door -> fatal */
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate param.sched_priority = BASE_PRIORITY;
4540Sstevel@tonic-gate (void) pthread_setschedparam(pthread_self(), SCHED_OTHER,
4550Sstevel@tonic-gate ¶m);
4560Sstevel@tonic-gate
4570Sstevel@tonic-gate /* input door server */
4580Sstevel@tonic-gate (void) pthread_mutex_init(&(in_thr.thd_mutex), NULL);
4590Sstevel@tonic-gate (void) pthread_cond_init(&(in_thr.thd_cv), NULL);
4600Sstevel@tonic-gate in_thr.thd_waiting = 0;
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate bpool_init();
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate p = plugin_head;
4650Sstevel@tonic-gate while (p != NULL) {
4660Sstevel@tonic-gate if (p->plg_removed) {
4670Sstevel@tonic-gate DPRINT((dbfp, "start removing %s\n", p->plg_path));
4680Sstevel@tonic-gate /* tell process(p) to exit and dlclose */
4690Sstevel@tonic-gate (void) pthread_cond_signal(&(p->plg_cv));
4700Sstevel@tonic-gate } else if (!p->plg_initialized) {
4710Sstevel@tonic-gate DPRINT((dbfp, "start initial load of %s\n",
4720Sstevel@tonic-gate p->plg_path));
4730Sstevel@tonic-gate if (load_plugin(p)) {
4740Sstevel@tonic-gate report_error(INTERNAL_LOAD_ERROR,
4750Sstevel@tonic-gate gettext("dynamic load failed"),
4760Sstevel@tonic-gate p->plg_path);
4770Sstevel@tonic-gate p = unload_plugin(p);
4780Sstevel@tonic-gate continue;
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate open_params = NULL;
4810Sstevel@tonic-gate error_string = NULL;
4820Sstevel@tonic-gate if ((rc = p->plg_fplugin_open(
4830Sstevel@tonic-gate p->plg_kvlist,
4840Sstevel@tonic-gate &open_params, &error_string)) != AUDITD_SUCCESS) {
4850Sstevel@tonic-gate report_error(rc, error_string, p->plg_path);
4860Sstevel@tonic-gate free(error_string);
4870Sstevel@tonic-gate p = unload_plugin(p);
4880Sstevel@tonic-gate continue;
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate open_return(p, open_params);
4910Sstevel@tonic-gate p->plg_reopen = 0;
4920Sstevel@tonic-gate
4930Sstevel@tonic-gate threshold = ((p->plg_qmax * THRESHOLD_PCT) + 99) / 100;
4940Sstevel@tonic-gate p->plg_qmin = INPUT_MIN;
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate DPRINT((dbfp,
4970Sstevel@tonic-gate "calling qpool_init for %s with qmax=%d\n",
4980Sstevel@tonic-gate p->plg_path, p->plg_qmax));
4990Sstevel@tonic-gate
5000Sstevel@tonic-gate qpool_init(p, threshold);
5010Sstevel@tonic-gate audit_queue_init(&(p->plg_queue));
5020Sstevel@tonic-gate p->plg_initialized = 1;
5030Sstevel@tonic-gate
5040Sstevel@tonic-gate (void) pthread_mutex_init(&(p->plg_mutex), NULL);
5050Sstevel@tonic-gate (void) pthread_cond_init(&(p->plg_cv), NULL);
5060Sstevel@tonic-gate p->plg_waiting = 0;
5070Sstevel@tonic-gate
5080Sstevel@tonic-gate if (pthread_create(&(p->plg_tid), NULL,
5090Sstevel@tonic-gate (void *(*)(void *))process, p)) {
5100Sstevel@tonic-gate report_error(INTERNAL_SYS_ERROR,
5110Sstevel@tonic-gate gettext("thread creation failed"),
5120Sstevel@tonic-gate p->plg_path);
5130Sstevel@tonic-gate p = unload_plugin(p);
5140Sstevel@tonic-gate continue;
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate } else if (p->plg_reopen) {
5170Sstevel@tonic-gate DPRINT((dbfp, "reopen %s\n", p->plg_path));
5180Sstevel@tonic-gate error_string = NULL;
519*12918SJan.Friedel@Sun.COM if ((rc = p->plg_fplugin_open(p->plg_kvlist,
5200Sstevel@tonic-gate &open_params, &error_string)) != AUDITD_SUCCESS) {
5210Sstevel@tonic-gate report_error(rc, error_string, p->plg_path);
5220Sstevel@tonic-gate free(error_string);
5230Sstevel@tonic-gate p = unload_plugin(p);
5240Sstevel@tonic-gate continue;
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate open_return(p, open_params);
5270Sstevel@tonic-gate p->plg_reopen = 0;
5280Sstevel@tonic-gate
5290Sstevel@tonic-gate DPRINT((dbfp, "%s qmax=%d\n",
5300Sstevel@tonic-gate p->plg_path, p->plg_qmax));
5310Sstevel@tonic-gate
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate p->plg_q_threshold = ((p->plg_qmax * THRESHOLD_PCT) + 99) / 100;
5340Sstevel@tonic-gate
5350Sstevel@tonic-gate p = p->plg_next;
5360Sstevel@tonic-gate plugin_count++;
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate if (plugin_count == 0) {
5390Sstevel@tonic-gate report_error(INTERNAL_CONFIG_ERROR,
5400Sstevel@tonic-gate gettext("No plugins are configured"), NULL);
5410Sstevel@tonic-gate return (-1);
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate if (!threads_ready) {
5440Sstevel@tonic-gate /* unleash the kernel */
5450Sstevel@tonic-gate rc = auditdoor(doorfd);
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate DPRINT((dbfp, "%d returned from auditdoor.\n",
5480Sstevel@tonic-gate rc));
5490Sstevel@tonic-gate if (rc != 0)
5500Sstevel@tonic-gate return (1); /* fatal */
5510Sstevel@tonic-gate
5520Sstevel@tonic-gate threads_ready = 1;
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate return (0);
5550Sstevel@tonic-gate }
5560Sstevel@tonic-gate
5570Sstevel@tonic-gate /*
5580Sstevel@tonic-gate * Door invocations that are in progress during a
5590Sstevel@tonic-gate * door_revoke() invocation are allowed to complete normally.
5600Sstevel@tonic-gate * -- man page for door_revoke()
5610Sstevel@tonic-gate */
5620Sstevel@tonic-gate void
auditd_thread_close()5630Sstevel@tonic-gate auditd_thread_close()
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate if (doorfd == -1)
5660Sstevel@tonic-gate return;
5670Sstevel@tonic-gate (void) door_revoke(doorfd);
5680Sstevel@tonic-gate doorfd = -1;
5690Sstevel@tonic-gate }
5700Sstevel@tonic-gate
5710Sstevel@tonic-gate /*
5720Sstevel@tonic-gate * qpool_init() sets up pool for queue entries (audit_q_t)
5730Sstevel@tonic-gate *
5740Sstevel@tonic-gate */
5750Sstevel@tonic-gate static void
qpool_init(plugin_t * p,int threshold)5760Sstevel@tonic-gate qpool_init(plugin_t *p, int threshold)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate int i;
5790Sstevel@tonic-gate audit_q_t *node;
5800Sstevel@tonic-gate
5810Sstevel@tonic-gate audit_queue_init(&(p->plg_pool));
5820Sstevel@tonic-gate
5830Sstevel@tonic-gate DPRINT((dbfp, "qpool_init(%d) max, min, threshhold = %d, %d, %d\n",
5845007Spaulson p->plg_tid, p->plg_qmax, p->plg_qmin, threshold));
5850Sstevel@tonic-gate
5860Sstevel@tonic-gate if (p->plg_qmax > largest_queue)
5870Sstevel@tonic-gate largest_queue = p->plg_qmax;
5880Sstevel@tonic-gate
5890Sstevel@tonic-gate p->plg_q_threshold = threshold;
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate for (i = 0; i < p->plg_qmin; i++) {
5920Sstevel@tonic-gate node = malloc(sizeof (audit_q_t));
5930Sstevel@tonic-gate if (node == NULL)
5940Sstevel@tonic-gate warn_or_fatal(1, gettext("no memory\n"));
5950Sstevel@tonic-gate /* doesn't return */
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate audit_enqueue(&p->plg_pool, node);
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate
6010Sstevel@tonic-gate /*
6020Sstevel@tonic-gate * bpool_init() sets up pool and queue for record entries (audit_rec_t)
6030Sstevel@tonic-gate *
6040Sstevel@tonic-gate */
6050Sstevel@tonic-gate static void
bpool_init()6060Sstevel@tonic-gate bpool_init()
6070Sstevel@tonic-gate {
6080Sstevel@tonic-gate int i;
6090Sstevel@tonic-gate audit_rec_t *node;
6100Sstevel@tonic-gate
6110Sstevel@tonic-gate audit_queue_init(&b_pool);
6120Sstevel@tonic-gate (void) pthread_mutex_init(&b_alloc_lock, NULL);
6130Sstevel@tonic-gate (void) pthread_mutex_init(&b_refcnt_lock, NULL);
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate for (i = 0; i < INPUT_MIN; i++) {
6160Sstevel@tonic-gate node = malloc(AUDIT_REC_HEADER + DEFAULT_BUF_SZ);
6170Sstevel@tonic-gate if (node == NULL)
6180Sstevel@tonic-gate warn_or_fatal(1, gettext("no memory\n"));
6190Sstevel@tonic-gate /* doesn't return */
6200Sstevel@tonic-gate
6210Sstevel@tonic-gate node->abq_buf_len = DEFAULT_BUF_SZ;
6220Sstevel@tonic-gate
6230Sstevel@tonic-gate node->abq_data_len = 0;
6240Sstevel@tonic-gate audit_enqueue(&b_pool, node);
6250Sstevel@tonic-gate (void) pthread_mutex_lock(&b_alloc_lock);
6260Sstevel@tonic-gate b_allocated++;
6270Sstevel@tonic-gate (void) pthread_mutex_unlock(&b_alloc_lock);
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate
6310Sstevel@tonic-gate /*
6320Sstevel@tonic-gate * qpool_close() discard queue and pool for a discontinued plugin
6330Sstevel@tonic-gate *
6340Sstevel@tonic-gate * there is no corresponding bpool_close() since it would only
6350Sstevel@tonic-gate * be called as auditd is going down.
6360Sstevel@tonic-gate */
6370Sstevel@tonic-gate static void
qpool_close(plugin_t * p)6380Sstevel@tonic-gate qpool_close(plugin_t *p) {
6390Sstevel@tonic-gate audit_q_t *q_node;
6400Sstevel@tonic-gate audit_rec_t *b_node;
6410Sstevel@tonic-gate
6420Sstevel@tonic-gate if (!p->plg_initialized)
6430Sstevel@tonic-gate return;
6440Sstevel@tonic-gate
6450Sstevel@tonic-gate while (audit_dequeue(&(p->plg_pool), (void *)&q_node) == 0) {
6460Sstevel@tonic-gate free(q_node);
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate audit_queue_destroy(&(p->plg_pool));
6490Sstevel@tonic-gate
6500Sstevel@tonic-gate while (audit_dequeue(&(p->plg_queue), (void *)&q_node) == 0) {
6510Sstevel@tonic-gate b_node = audit_release(&b_refcnt_lock, q_node->aqq_data);
6520Sstevel@tonic-gate if (b_node != NULL)
6530Sstevel@tonic-gate audit_enqueue(&b_pool, b_node);
6540Sstevel@tonic-gate free(q_node);
6550Sstevel@tonic-gate }
6560Sstevel@tonic-gate audit_queue_destroy(&(p->plg_queue));
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate
6590Sstevel@tonic-gate /*
6600Sstevel@tonic-gate * qpool_withdraw
6610Sstevel@tonic-gate */
6620Sstevel@tonic-gate static audit_q_t *
qpool_withdraw(plugin_t * p)6630Sstevel@tonic-gate qpool_withdraw(plugin_t *p)
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate audit_q_t *node;
6660Sstevel@tonic-gate int rc;
6670Sstevel@tonic-gate
6680Sstevel@tonic-gate /* get a buffer from the pool, if any */
6690Sstevel@tonic-gate rc = audit_dequeue(&(p->plg_pool), (void *)&node);
6700Sstevel@tonic-gate if (rc == 0)
6710Sstevel@tonic-gate return (node);
6720Sstevel@tonic-gate
6730Sstevel@tonic-gate /*
6740Sstevel@tonic-gate * the pool is empty: allocate a new element
6750Sstevel@tonic-gate */
6760Sstevel@tonic-gate node = malloc(sizeof (audit_q_t));
6770Sstevel@tonic-gate
6780Sstevel@tonic-gate if (node == NULL)
6790Sstevel@tonic-gate warn_or_fatal(1, gettext("no memory\n"));
6800Sstevel@tonic-gate /* doesn't return */
6810Sstevel@tonic-gate
6820Sstevel@tonic-gate return (node);
6830Sstevel@tonic-gate }
6840Sstevel@tonic-gate
6850Sstevel@tonic-gate /*
6860Sstevel@tonic-gate * bpool_withdraw -- gets a buffer and fills it
6870Sstevel@tonic-gate *
6880Sstevel@tonic-gate */
6890Sstevel@tonic-gate static audit_rec_t *
bpool_withdraw(char * buffer,size_t buff_size,size_t request_size)6900Sstevel@tonic-gate bpool_withdraw(char *buffer, size_t buff_size, size_t request_size)
6910Sstevel@tonic-gate {
6920Sstevel@tonic-gate audit_rec_t *node;
6930Sstevel@tonic-gate int rc;
6940Sstevel@tonic-gate size_t new_length;
6950Sstevel@tonic-gate
6960Sstevel@tonic-gate new_length = (request_size > DEFAULT_BUF_SZ) ?
6970Sstevel@tonic-gate request_size : DEFAULT_BUF_SZ;
6980Sstevel@tonic-gate
6990Sstevel@tonic-gate /* get a buffer from the pool, if any */
7000Sstevel@tonic-gate rc = audit_dequeue(&b_pool, (void *)&node);
7010Sstevel@tonic-gate
7020Sstevel@tonic-gate DPRINT((dbfp, "bpool_withdraw buf length=%d,"
7035007Spaulson " requested size=%d, dequeue rc=%d\n",
7045007Spaulson new_length, request_size, rc));
7050Sstevel@tonic-gate
7060Sstevel@tonic-gate if (rc == 0) {
70711706SJan.Friedel@Sun.COM DPRINT((dbfp, "bpool_withdraw node=%p (pool=%d)\n",
70811706SJan.Friedel@Sun.COM (void *)node, audit_queue_size(&b_pool)));
7090Sstevel@tonic-gate
7100Sstevel@tonic-gate if (new_length > node->abq_buf_len) {
7110Sstevel@tonic-gate node = realloc(node, AUDIT_REC_HEADER + new_length);
7120Sstevel@tonic-gate if (node == NULL)
7130Sstevel@tonic-gate warn_or_fatal(1, gettext("no memory\n"));
7140Sstevel@tonic-gate /* no return */
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate } else {
7170Sstevel@tonic-gate /*
7180Sstevel@tonic-gate * the pool is empty: allocate a new element
7190Sstevel@tonic-gate */
7200Sstevel@tonic-gate (void) pthread_mutex_lock(&b_alloc_lock);
7210Sstevel@tonic-gate if (b_allocated >= largest_queue) {
7220Sstevel@tonic-gate (void) pthread_mutex_unlock(&b_alloc_lock);
7230Sstevel@tonic-gate DPRINT((dbfp, "bpool_withdraw is over max (pool=%d)\n",
7240Sstevel@tonic-gate audit_queue_size(&b_pool)));
7250Sstevel@tonic-gate return (NULL);
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate (void) pthread_mutex_unlock(&b_alloc_lock);
7280Sstevel@tonic-gate
7290Sstevel@tonic-gate node = malloc(AUDIT_REC_HEADER + new_length);
7300Sstevel@tonic-gate
7310Sstevel@tonic-gate if (node == NULL)
7320Sstevel@tonic-gate warn_or_fatal(1, gettext("no memory\n"));
7330Sstevel@tonic-gate /* no return */
7340Sstevel@tonic-gate
7350Sstevel@tonic-gate (void) pthread_mutex_lock(&b_alloc_lock);
7360Sstevel@tonic-gate b_allocated++;
7370Sstevel@tonic-gate (void) pthread_mutex_unlock(&b_alloc_lock);
73811706SJan.Friedel@Sun.COM DPRINT((dbfp, "bpool_withdraw node=%p (alloc=%d, pool=%d)\n",
73911706SJan.Friedel@Sun.COM (void *)node, b_allocated, audit_queue_size(&b_pool)));
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate assert(request_size <= new_length);
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate (void) memcpy(node->abq_buffer, buffer, buff_size);
7440Sstevel@tonic-gate node->abq_data_len = buff_size;
7450Sstevel@tonic-gate node->abq_buf_len = new_length;
7460Sstevel@tonic-gate node->abq_ref_count = 0;
7470Sstevel@tonic-gate
7480Sstevel@tonic-gate return (node);
7490Sstevel@tonic-gate }
7500Sstevel@tonic-gate
7510Sstevel@tonic-gate /*
7520Sstevel@tonic-gate * qpool_return() moves queue nodes back to the pool queue.
7530Sstevel@tonic-gate *
7540Sstevel@tonic-gate * if the pool is over max, the node is discarded instead.
7550Sstevel@tonic-gate */
7560Sstevel@tonic-gate static void
qpool_return(plugin_t * p,audit_q_t * node)7570Sstevel@tonic-gate qpool_return(plugin_t *p, audit_q_t *node)
7580Sstevel@tonic-gate {
7590Sstevel@tonic-gate int qpool_size;
7600Sstevel@tonic-gate int q_size;
7610Sstevel@tonic-gate
7620Sstevel@tonic-gate #if DEBUG
76311704SJan.Friedel@Sun.COM uint64_t sequence = node->aqq_sequence;
7640Sstevel@tonic-gate #endif
7650Sstevel@tonic-gate qpool_size = audit_queue_size(&(p->plg_pool));
7660Sstevel@tonic-gate q_size = audit_queue_size(&(p->plg_queue));
7670Sstevel@tonic-gate
7680Sstevel@tonic-gate if (qpool_size + q_size > p->plg_qmax)
7690Sstevel@tonic-gate free(node);
7700Sstevel@tonic-gate else
7710Sstevel@tonic-gate audit_enqueue(&(p->plg_pool), node);
7720Sstevel@tonic-gate
7730Sstevel@tonic-gate DPRINT((dbfp,
77411704SJan.Friedel@Sun.COM "qpool_return(%d): seq=%llu, q size=%d,"
7750Sstevel@tonic-gate " pool size=%d (total alloc=%d), threshhold=%d\n",
7760Sstevel@tonic-gate p->plg_tid, sequence, q_size, qpool_size,
7770Sstevel@tonic-gate q_size + qpool_size, p->plg_q_threshold));
7780Sstevel@tonic-gate }
7790Sstevel@tonic-gate
7800Sstevel@tonic-gate /*
7810Sstevel@tonic-gate * bpool_return() moves queue nodes back to the pool queue.
7820Sstevel@tonic-gate */
7830Sstevel@tonic-gate static void
bpool_return(audit_rec_t * node)7840Sstevel@tonic-gate bpool_return(audit_rec_t *node)
7850Sstevel@tonic-gate {
7860Sstevel@tonic-gate #if DEBUG
7870Sstevel@tonic-gate audit_rec_t *copy = node;
7880Sstevel@tonic-gate #endif
7890Sstevel@tonic-gate node = audit_release(&b_refcnt_lock, node); /* decrement ref cnt */
7900Sstevel@tonic-gate
7910Sstevel@tonic-gate if (node != NULL) { /* NULL if ref cnt is not zero */
7920Sstevel@tonic-gate audit_enqueue(&b_pool, node);
7930Sstevel@tonic-gate DPRINT((dbfp,
79411706SJan.Friedel@Sun.COM "bpool_return: requeue %p (allocated=%d,"
79511706SJan.Friedel@Sun.COM " pool size=%d)\n", (void *)node, b_allocated,
7960Sstevel@tonic-gate audit_queue_size(&b_pool)));
7970Sstevel@tonic-gate }
7980Sstevel@tonic-gate #if DEBUG
7990Sstevel@tonic-gate else {
8000Sstevel@tonic-gate DPRINT((dbfp,
80111706SJan.Friedel@Sun.COM "bpool_return: decrement count for %p (allocated=%d,"
80211706SJan.Friedel@Sun.COM " pool size=%d)\n", (void *)copy, b_allocated,
8030Sstevel@tonic-gate audit_queue_size(&b_pool)));
8040Sstevel@tonic-gate }
8050Sstevel@tonic-gate #endif
8060Sstevel@tonic-gate }
8070Sstevel@tonic-gate
8080Sstevel@tonic-gate #if DEBUG
8090Sstevel@tonic-gate static void
dump_state(char * src,plugin_t * p,uint64_t count,char * msg)81011704SJan.Friedel@Sun.COM dump_state(char *src, plugin_t *p, uint64_t count, char *msg)
8110Sstevel@tonic-gate {
8120Sstevel@tonic-gate struct sched_param param;
8130Sstevel@tonic-gate int policy;
8140Sstevel@tonic-gate /*
8150Sstevel@tonic-gate * count is message sequence
8160Sstevel@tonic-gate */
8170Sstevel@tonic-gate (void) pthread_getschedparam(p->plg_tid, &policy, ¶m);
81811704SJan.Friedel@Sun.COM (void) fprintf(dbfp, "%7s(%d/%llu) %11s:"
8190Sstevel@tonic-gate " input_in_wait=%d"
8200Sstevel@tonic-gate " priority=%d"
8210Sstevel@tonic-gate " queue size=%d pool size=%d"
8220Sstevel@tonic-gate "\n\t"
8230Sstevel@tonic-gate "process wait=%d"
8240Sstevel@tonic-gate " tossed=%d"
8250Sstevel@tonic-gate " queued=%d"
8260Sstevel@tonic-gate " written=%d"
8270Sstevel@tonic-gate "\n",
8280Sstevel@tonic-gate src, p->plg_tid, count, msg,
8290Sstevel@tonic-gate in_thr.thd_waiting, param.sched_priority,
8300Sstevel@tonic-gate audit_queue_size(&(p->plg_queue)),
8310Sstevel@tonic-gate audit_queue_size(&(p->plg_pool)),
8320Sstevel@tonic-gate p->plg_waiting, p->plg_tossed,
8330Sstevel@tonic-gate p->plg_queued, p->plg_output);
8340Sstevel@tonic-gate
8350Sstevel@tonic-gate (void) fflush(dbfp);
8360Sstevel@tonic-gate }
8370Sstevel@tonic-gate #endif
8380Sstevel@tonic-gate
8390Sstevel@tonic-gate /*
8400Sstevel@tonic-gate * policy_is_block: return 1 if the continue policy is off for any active
8410Sstevel@tonic-gate * plugin, else 0
8420Sstevel@tonic-gate */
8430Sstevel@tonic-gate static int
policy_is_block()8440Sstevel@tonic-gate policy_is_block()
8450Sstevel@tonic-gate {
8460Sstevel@tonic-gate plugin_t *p;
8470Sstevel@tonic-gate
8480Sstevel@tonic-gate (void) pthread_mutex_lock(&plugin_mutex);
8490Sstevel@tonic-gate p = plugin_head;
8500Sstevel@tonic-gate
8510Sstevel@tonic-gate while (p != NULL) {
8520Sstevel@tonic-gate if (p->plg_cnt == 0) {
8530Sstevel@tonic-gate (void) pthread_mutex_unlock(&plugin_mutex);
8540Sstevel@tonic-gate DPRINT((dbfp,
8550Sstevel@tonic-gate "policy_is_block: policy is to block\n"));
8560Sstevel@tonic-gate return (1);
8570Sstevel@tonic-gate }
8580Sstevel@tonic-gate p = p->plg_next;
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate (void) pthread_mutex_unlock(&plugin_mutex);
8610Sstevel@tonic-gate DPRINT((dbfp, "policy_is_block: policy is to continue\n"));
8620Sstevel@tonic-gate return (0);
8630Sstevel@tonic-gate }
8640Sstevel@tonic-gate
8650Sstevel@tonic-gate /*
8660Sstevel@tonic-gate * policy_update() -- the kernel has received a policy change.
8670Sstevel@tonic-gate * Presently, the only policy auditd cares about is AUDIT_CNT
8680Sstevel@tonic-gate */
8690Sstevel@tonic-gate static void
policy_update(uint32_t newpolicy)8700Sstevel@tonic-gate policy_update(uint32_t newpolicy)
8710Sstevel@tonic-gate {
8720Sstevel@tonic-gate plugin_t *p;
8730Sstevel@tonic-gate
87411706SJan.Friedel@Sun.COM DPRINT((dbfp, "policy change: %X\n", newpolicy));
8750Sstevel@tonic-gate (void) pthread_mutex_lock(&plugin_mutex);
8760Sstevel@tonic-gate p = plugin_head;
8770Sstevel@tonic-gate while (p != NULL) {
8780Sstevel@tonic-gate p->plg_cnt = (newpolicy & AUDIT_CNT) ? 1 : 0;
8790Sstevel@tonic-gate (void) pthread_cond_signal(&(p->plg_cv));
8800Sstevel@tonic-gate
8810Sstevel@tonic-gate DPRINT((dbfp, "policy changed for thread %d\n", p->plg_tid));
8820Sstevel@tonic-gate p = p->plg_next;
8830Sstevel@tonic-gate }
8840Sstevel@tonic-gate (void) pthread_mutex_unlock(&plugin_mutex);
8850Sstevel@tonic-gate }
8860Sstevel@tonic-gate
8870Sstevel@tonic-gate /*
8880Sstevel@tonic-gate * queue_buffer() inputs a buffer and queues for each active plugin if
8890Sstevel@tonic-gate * it represents a complete audit record. Otherwise it builds a
8900Sstevel@tonic-gate * larger buffer to hold the record and take successive buffers from
8910Sstevel@tonic-gate * c2audit to build a complete record; then queues it for each plugin.
8920Sstevel@tonic-gate *
8930Sstevel@tonic-gate * return 0 if data is queued (or damaged and tossed). If resources
8940Sstevel@tonic-gate * are not available, return 0 if all active plugins have the cnt
8950Sstevel@tonic-gate * policy set, else 1. 0 is also returned if the input is a control
8960Sstevel@tonic-gate * message. (aub_buf is aligned on a 64 bit boundary, so casting
8970Sstevel@tonic-gate * it to an integer works just fine.)
8980Sstevel@tonic-gate */
8990Sstevel@tonic-gate static int
queue_buffer(au_dbuf_t * kl)9000Sstevel@tonic-gate queue_buffer(au_dbuf_t *kl)
9010Sstevel@tonic-gate {
9020Sstevel@tonic-gate plugin_t *p;
9030Sstevel@tonic-gate audit_rec_t *b_copy;
9040Sstevel@tonic-gate audit_q_t *q_copy;
9050Sstevel@tonic-gate boolean_t referenced = 0;
9060Sstevel@tonic-gate static char *invalid_msg = "invalid audit record discarded";
9075007Spaulson static char *invalid_control = "invalid audit control discarded";
9080Sstevel@tonic-gate
9090Sstevel@tonic-gate static audit_rec_t *alt_b_copy = NULL;
9100Sstevel@tonic-gate static size_t alt_length;
9110Sstevel@tonic-gate static size_t alt_offset;
9120Sstevel@tonic-gate
9130Sstevel@tonic-gate /*
9140Sstevel@tonic-gate * the buffer may be a kernel -> auditd message. (only
9150Sstevel@tonic-gate * the policy change message exists so far.)
9160Sstevel@tonic-gate */
9170Sstevel@tonic-gate
9180Sstevel@tonic-gate if ((kl->aub_type & AU_DBUF_NOTIFY) != 0) {
9190Sstevel@tonic-gate uint32_t control;
9200Sstevel@tonic-gate
9210Sstevel@tonic-gate control = kl->aub_type & ~AU_DBUF_NOTIFY;
9220Sstevel@tonic-gate switch (control) {
9230Sstevel@tonic-gate case AU_DBUF_POLICY:
9240Sstevel@tonic-gate /* LINTED */
9250Sstevel@tonic-gate policy_update(*(uint32_t *)kl->aub_buf);
9260Sstevel@tonic-gate break;
9270Sstevel@tonic-gate case AU_DBUF_SHUTDOWN:
928*12918SJan.Friedel@Sun.COM (void) kill(getpid(), SIGTERM);
9290Sstevel@tonic-gate DPRINT((dbfp, "AU_DBUF_SHUTDOWN message\n"));
9300Sstevel@tonic-gate break;
9310Sstevel@tonic-gate default:
9320Sstevel@tonic-gate warn_or_fatal(0, gettext(invalid_control));
9330Sstevel@tonic-gate break;
9340Sstevel@tonic-gate }
9350Sstevel@tonic-gate return (0);
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate /*
9380Sstevel@tonic-gate * The test for valid continuation/completion may fail. Need to
9390Sstevel@tonic-gate * assume the failure was earlier and that this buffer may
9400Sstevel@tonic-gate * be a valid first or complete buffer after discarding the
9410Sstevel@tonic-gate * incomplete record
9420Sstevel@tonic-gate */
9430Sstevel@tonic-gate
9440Sstevel@tonic-gate if (alt_b_copy != NULL) {
9450Sstevel@tonic-gate if ((kl->aub_type == AU_DBUF_FIRST) ||
9460Sstevel@tonic-gate (kl->aub_type == AU_DBUF_COMPLETE)) {
9470Sstevel@tonic-gate DPRINT((dbfp, "copy is not null, partial is %d\n",
9480Sstevel@tonic-gate kl->aub_type));
9490Sstevel@tonic-gate bpool_return(alt_b_copy);
9500Sstevel@tonic-gate warn_or_fatal(0, gettext(invalid_msg));
9510Sstevel@tonic-gate alt_b_copy = NULL;
9520Sstevel@tonic-gate }
9530Sstevel@tonic-gate }
9540Sstevel@tonic-gate if (alt_b_copy != NULL) { /* continue collecting a long record */
9550Sstevel@tonic-gate if (kl->aub_size + alt_offset > alt_length) {
9560Sstevel@tonic-gate bpool_return(alt_b_copy);
9570Sstevel@tonic-gate alt_b_copy = NULL;
9580Sstevel@tonic-gate warn_or_fatal(0, gettext(invalid_msg));
9590Sstevel@tonic-gate return (0);
9600Sstevel@tonic-gate }
9610Sstevel@tonic-gate (void) memcpy(alt_b_copy->abq_buffer + alt_offset, kl->aub_buf,
9620Sstevel@tonic-gate kl->aub_size);
9630Sstevel@tonic-gate alt_offset += kl->aub_size;
9640Sstevel@tonic-gate if (kl->aub_type == AU_DBUF_MIDDLE)
9650Sstevel@tonic-gate return (0);
9660Sstevel@tonic-gate b_copy = alt_b_copy;
9670Sstevel@tonic-gate alt_b_copy = NULL;
9680Sstevel@tonic-gate b_copy->abq_data_len = alt_length;
9690Sstevel@tonic-gate } else if (kl->aub_type == AU_DBUF_FIRST) {
9700Sstevel@tonic-gate /* first buffer of a multiple buffer record */
9710Sstevel@tonic-gate alt_length = getlen(kl->aub_buf);
9720Sstevel@tonic-gate if ((alt_length < MIN_RECORD_SIZE) ||
9730Sstevel@tonic-gate (alt_length <= kl->aub_size)) {
9740Sstevel@tonic-gate warn_or_fatal(0, gettext(invalid_msg));
9750Sstevel@tonic-gate return (0);
9760Sstevel@tonic-gate }
9770Sstevel@tonic-gate alt_b_copy = bpool_withdraw(kl->aub_buf, kl->aub_size,
9780Sstevel@tonic-gate alt_length);
9790Sstevel@tonic-gate
9800Sstevel@tonic-gate if (alt_b_copy == NULL)
9810Sstevel@tonic-gate return (policy_is_block());
9820Sstevel@tonic-gate
9830Sstevel@tonic-gate alt_offset = kl->aub_size;
9840Sstevel@tonic-gate return (0);
9850Sstevel@tonic-gate } else { /* one buffer, one record -- the basic case */
9860Sstevel@tonic-gate if (kl->aub_type != AU_DBUF_COMPLETE) {
9870Sstevel@tonic-gate DPRINT((dbfp, "copy is null, partial is %d\n",
9880Sstevel@tonic-gate kl->aub_type));
9890Sstevel@tonic-gate warn_or_fatal(0, gettext(invalid_msg));
9900Sstevel@tonic-gate return (0); /* tossed */
9910Sstevel@tonic-gate }
9920Sstevel@tonic-gate b_copy = bpool_withdraw(kl->aub_buf, kl->aub_size,
9930Sstevel@tonic-gate kl->aub_size);
9940Sstevel@tonic-gate
9950Sstevel@tonic-gate if (b_copy == NULL)
9960Sstevel@tonic-gate return (policy_is_block());
9970Sstevel@tonic-gate }
9980Sstevel@tonic-gate
9990Sstevel@tonic-gate (void) pthread_mutex_lock(&plugin_mutex);
10000Sstevel@tonic-gate p = plugin_head;
10010Sstevel@tonic-gate while (p != NULL) {
10020Sstevel@tonic-gate if (!p->plg_removed) {
10030Sstevel@tonic-gate /*
10040Sstevel@tonic-gate * Link the record buffer to the input queues.
10050Sstevel@tonic-gate * To avoid a race, it is necessary to wait
10060Sstevel@tonic-gate * until all reference count increments
10070Sstevel@tonic-gate * are complete before queueing q_copy.
10080Sstevel@tonic-gate */
10090Sstevel@tonic-gate audit_incr_ref(&b_refcnt_lock, b_copy);
10100Sstevel@tonic-gate
10110Sstevel@tonic-gate q_copy = qpool_withdraw(p);
10120Sstevel@tonic-gate q_copy->aqq_sequence = p->plg_sequence++;
10130Sstevel@tonic-gate q_copy->aqq_data = b_copy;
10140Sstevel@tonic-gate
10150Sstevel@tonic-gate p->plg_save_q_copy = q_copy; /* enqueue below */
10160Sstevel@tonic-gate referenced = 1;
10170Sstevel@tonic-gate } else
10180Sstevel@tonic-gate p->plg_save_q_copy = NULL;
10190Sstevel@tonic-gate p = p->plg_next;
10200Sstevel@tonic-gate }
10210Sstevel@tonic-gate /*
10220Sstevel@tonic-gate * now that the reference count is updated, queue it.
10230Sstevel@tonic-gate */
10240Sstevel@tonic-gate if (referenced) {
10250Sstevel@tonic-gate p = plugin_head;
10260Sstevel@tonic-gate while ((p != NULL) && (p->plg_save_q_copy != NULL)) {
10270Sstevel@tonic-gate audit_enqueue(&(p->plg_queue), p->plg_save_q_copy);
10280Sstevel@tonic-gate (void) pthread_cond_signal(&(p->plg_cv));
10290Sstevel@tonic-gate p->plg_queued++;
10300Sstevel@tonic-gate p = p->plg_next;
10310Sstevel@tonic-gate }
10320Sstevel@tonic-gate } else
10330Sstevel@tonic-gate bpool_return(b_copy);
10340Sstevel@tonic-gate
10350Sstevel@tonic-gate (void) pthread_mutex_unlock(&plugin_mutex);
10360Sstevel@tonic-gate
10370Sstevel@tonic-gate return (0);
10380Sstevel@tonic-gate }
10390Sstevel@tonic-gate
10400Sstevel@tonic-gate /*
10410Sstevel@tonic-gate * wait_a_while() -- timed wait in the door server to allow output
10420Sstevel@tonic-gate * time to catch up.
10430Sstevel@tonic-gate */
10440Sstevel@tonic-gate static void
wait_a_while()1045*12918SJan.Friedel@Sun.COM wait_a_while()
1046*12918SJan.Friedel@Sun.COM {
10470Sstevel@tonic-gate struct timespec delay = {0, 500000000}; /* 1/2 second */;
10480Sstevel@tonic-gate
10490Sstevel@tonic-gate (void) pthread_mutex_lock(&(in_thr.thd_mutex));
10500Sstevel@tonic-gate in_thr.thd_waiting = 1;
10510Sstevel@tonic-gate (void) pthread_cond_reltimedwait_np(&(in_thr.thd_cv),
10520Sstevel@tonic-gate &(in_thr.thd_mutex), &delay);
10530Sstevel@tonic-gate in_thr.thd_waiting = 0;
10540Sstevel@tonic-gate (void) pthread_mutex_unlock(&(in_thr.thd_mutex));
10550Sstevel@tonic-gate }
10560Sstevel@tonic-gate
10570Sstevel@tonic-gate /*
10580Sstevel@tonic-gate * adjust_priority() -- check queue and pools and adjust the priority
10590Sstevel@tonic-gate * for process() accordingly. If we're way ahead of output, do a
10600Sstevel@tonic-gate * timed wait as well.
10610Sstevel@tonic-gate */
10620Sstevel@tonic-gate static void
adjust_priority()1063*12918SJan.Friedel@Sun.COM adjust_priority()
1064*12918SJan.Friedel@Sun.COM {
10650Sstevel@tonic-gate int queue_near_full;
10660Sstevel@tonic-gate plugin_t *p;
10670Sstevel@tonic-gate int queue_size;
10680Sstevel@tonic-gate struct sched_param param;
10690Sstevel@tonic-gate
10700Sstevel@tonic-gate queue_near_full = 0;
10710Sstevel@tonic-gate (void) pthread_mutex_lock(&plugin_mutex);
10720Sstevel@tonic-gate p = plugin_head;
10730Sstevel@tonic-gate while (p != NULL) {
10740Sstevel@tonic-gate queue_size = audit_queue_size(&(p->plg_queue));
10750Sstevel@tonic-gate if (queue_size > p->plg_q_threshold) {
10760Sstevel@tonic-gate if (p->plg_priority != HIGH_PRIORITY) {
10770Sstevel@tonic-gate p->plg_priority =
10780Sstevel@tonic-gate param.sched_priority =
10790Sstevel@tonic-gate HIGH_PRIORITY;
10800Sstevel@tonic-gate (void) pthread_setschedparam(p->plg_tid,
10810Sstevel@tonic-gate SCHED_OTHER, ¶m);
10820Sstevel@tonic-gate }
10830Sstevel@tonic-gate if (queue_size > p->plg_qmax - p->plg_qmin) {
10840Sstevel@tonic-gate queue_near_full = 1;
10850Sstevel@tonic-gate break;
10860Sstevel@tonic-gate }
10870Sstevel@tonic-gate }
10880Sstevel@tonic-gate p = p->plg_next;
10890Sstevel@tonic-gate }
10900Sstevel@tonic-gate (void) pthread_mutex_unlock(&plugin_mutex);
10910Sstevel@tonic-gate
10920Sstevel@tonic-gate if (queue_near_full) {
10930Sstevel@tonic-gate DPRINT((dbfp,
10940Sstevel@tonic-gate "adjust_priority: input taking a short break\n"));
10950Sstevel@tonic-gate wait_a_while();
10960Sstevel@tonic-gate DPRINT((dbfp,
10970Sstevel@tonic-gate "adjust_priority: input back from my break\n"));
10980Sstevel@tonic-gate }
10990Sstevel@tonic-gate }
11000Sstevel@tonic-gate
11010Sstevel@tonic-gate /*
11020Sstevel@tonic-gate * input() is a door server; it blocks if any plugins have full queues
1103*12918SJan.Friedel@Sun.COM * with the continue policy off. (auditconfig -setpolicy -cnt)
11040Sstevel@tonic-gate *
11050Sstevel@tonic-gate * input() is called synchronously from c2audit and is NOT
11060Sstevel@tonic-gate * reentrant due to the (unprotected) static variables in
11070Sstevel@tonic-gate * queue_buffer(). If multiple clients are created, a context
11080Sstevel@tonic-gate * structure will be required for queue_buffer.
11090Sstevel@tonic-gate *
11100Sstevel@tonic-gate * timedwait is used when input() gets too far ahead of process();
11110Sstevel@tonic-gate * the wait terminates either when the set time expires or when
11120Sstevel@tonic-gate * process() signals that it has nearly caught up.
11130Sstevel@tonic-gate */
11140Sstevel@tonic-gate /* ARGSUSED */
11150Sstevel@tonic-gate static void
input(void * cookie,void * argp,int arg_size,door_desc_t * dp,int n_descriptors)11160Sstevel@tonic-gate input(void *cookie, void *argp, int arg_size, door_desc_t *dp,
11170Sstevel@tonic-gate int n_descriptors)
11180Sstevel@tonic-gate {
11190Sstevel@tonic-gate int is_blocked;
11200Sstevel@tonic-gate plugin_t *p;
11210Sstevel@tonic-gate #if DEBUG
11220Sstevel@tonic-gate int loop_count = 0;
11230Sstevel@tonic-gate static int call_counter = 0;
11240Sstevel@tonic-gate #endif
11250Sstevel@tonic-gate if (argp == NULL) {
11260Sstevel@tonic-gate warn_or_fatal(0,
11270Sstevel@tonic-gate gettext("invalid data received from c2audit\n"));
11280Sstevel@tonic-gate goto input_exit;
11290Sstevel@tonic-gate }
11300Sstevel@tonic-gate DPRINT((dbfp, "%d input new buffer: length=%u, "
11310Sstevel@tonic-gate "partial=%u, arg_size=%d\n",
11320Sstevel@tonic-gate ++call_counter, ((au_dbuf_t *)argp)->aub_size,
11330Sstevel@tonic-gate ((au_dbuf_t *)argp)->aub_type, arg_size));
11340Sstevel@tonic-gate
11350Sstevel@tonic-gate if (((au_dbuf_t *)argp)->aub_size < 1) {
11360Sstevel@tonic-gate warn_or_fatal(0,
11370Sstevel@tonic-gate gettext("invalid data length received from c2audit\n"));
11380Sstevel@tonic-gate goto input_exit;
11390Sstevel@tonic-gate }
11400Sstevel@tonic-gate /*
11410Sstevel@tonic-gate * is_blocked is true only if one or more plugins have "no
11420Sstevel@tonic-gate * continue" (-cnt) set and one of those has a full queue.
11430Sstevel@tonic-gate * All plugins block until success is met.
11440Sstevel@tonic-gate */
11450Sstevel@tonic-gate for (;;) {
11460Sstevel@tonic-gate DPRINT((dbfp, "%d input is calling queue_buffer\n",
11470Sstevel@tonic-gate call_counter));
11480Sstevel@tonic-gate
11490Sstevel@tonic-gate is_blocked = queue_buffer((au_dbuf_t *)argp);
11500Sstevel@tonic-gate
11510Sstevel@tonic-gate if (!is_blocked) {
11520Sstevel@tonic-gate adjust_priority();
11530Sstevel@tonic-gate break;
11540Sstevel@tonic-gate } else {
11550Sstevel@tonic-gate DPRINT((dbfp,
11560Sstevel@tonic-gate "%d input blocked (loop=%d)\n",
11570Sstevel@tonic-gate call_counter, loop_count));
11580Sstevel@tonic-gate
11590Sstevel@tonic-gate wait_a_while();
11600Sstevel@tonic-gate
11610Sstevel@tonic-gate DPRINT((dbfp, "%d input unblocked (loop=%d)\n",
11620Sstevel@tonic-gate call_counter, loop_count));
11630Sstevel@tonic-gate }
11640Sstevel@tonic-gate #if DEBUG
11650Sstevel@tonic-gate loop_count++;
11660Sstevel@tonic-gate #endif
11670Sstevel@tonic-gate }
11680Sstevel@tonic-gate input_exit:
11690Sstevel@tonic-gate p = plugin_head;
11700Sstevel@tonic-gate while (p != NULL) {
11710Sstevel@tonic-gate (void) pthread_cond_signal(&(p->plg_cv));
11720Sstevel@tonic-gate p = p->plg_next;
11730Sstevel@tonic-gate }
11740Sstevel@tonic-gate ((au_dbuf_t *)argp)->aub_size = 0; /* return code */
11750Sstevel@tonic-gate (void) door_return(argp, sizeof (uint64_t), NULL, 0);
11760Sstevel@tonic-gate }
11770Sstevel@tonic-gate
11780Sstevel@tonic-gate /*
11790Sstevel@tonic-gate * process() -- pass a buffer to a plugin
11800Sstevel@tonic-gate */
11810Sstevel@tonic-gate static void
process(plugin_t * p)11820Sstevel@tonic-gate process(plugin_t *p)
11830Sstevel@tonic-gate {
11840Sstevel@tonic-gate int rc;
11850Sstevel@tonic-gate audit_rec_t *b_node;
11860Sstevel@tonic-gate audit_q_t *q_node;
11870Sstevel@tonic-gate auditd_rc_t plugrc;
11880Sstevel@tonic-gate char *error_string;
11890Sstevel@tonic-gate struct timespec delay;
11900Sstevel@tonic-gate int sendsignal;
11910Sstevel@tonic-gate int queue_len;
11920Sstevel@tonic-gate struct sched_param param;
11935007Spaulson static boolean_t once = B_FALSE;
11940Sstevel@tonic-gate
11950Sstevel@tonic-gate DPRINT((dbfp, "%s is thread %d\n", p->plg_path, p->plg_tid));
11960Sstevel@tonic-gate p->plg_priority = param.sched_priority = BASE_PRIORITY;
11970Sstevel@tonic-gate (void) pthread_setschedparam(p->plg_tid, SCHED_OTHER, ¶m);
11980Sstevel@tonic-gate
11990Sstevel@tonic-gate delay.tv_nsec = 0;
12000Sstevel@tonic-gate
12010Sstevel@tonic-gate for (;;) {
12020Sstevel@tonic-gate while (audit_dequeue(&(p->plg_queue), (void *)&q_node) != 0) {
12030Sstevel@tonic-gate DUMP("process", p, p->plg_last_seq_out, "blocked");
12040Sstevel@tonic-gate (void) pthread_cond_signal(&(in_thr.thd_cv));
12050Sstevel@tonic-gate
12060Sstevel@tonic-gate (void) pthread_mutex_lock(&(p->plg_mutex));
12070Sstevel@tonic-gate p->plg_waiting++;
12080Sstevel@tonic-gate (void) pthread_cond_wait(&(p->plg_cv),
12090Sstevel@tonic-gate &(p->plg_mutex));
12100Sstevel@tonic-gate p->plg_waiting--;
12110Sstevel@tonic-gate (void) pthread_mutex_unlock(&(p->plg_mutex));
12120Sstevel@tonic-gate
12130Sstevel@tonic-gate if (p->plg_removed)
12145007Spaulson goto plugin_removed;
12150Sstevel@tonic-gate
12160Sstevel@tonic-gate DUMP("process", p, p->plg_last_seq_out, "unblocked");
12170Sstevel@tonic-gate }
12180Sstevel@tonic-gate #if DEBUG
12190Sstevel@tonic-gate if (q_node->aqq_sequence != p->plg_last_seq_out + 1)
12200Sstevel@tonic-gate (void) fprintf(dbfp,
122111704SJan.Friedel@Sun.COM "process(%d): buffer sequence=%llu but prev=%llu\n",
12220Sstevel@tonic-gate p->plg_tid, q_node->aqq_sequence,
12230Sstevel@tonic-gate p->plg_last_seq_out);
12240Sstevel@tonic-gate #endif
12250Sstevel@tonic-gate error_string = NULL;
12260Sstevel@tonic-gate
12270Sstevel@tonic-gate b_node = q_node->aqq_data;
12285007Spaulson retry_mode:
12290Sstevel@tonic-gate plugrc = p->plg_fplugin(b_node->abq_buffer,
12305007Spaulson b_node->abq_data_len, q_node->aqq_sequence, &error_string);
12315007Spaulson
12325007Spaulson if (p->plg_removed)
12335007Spaulson goto plugin_removed;
12340Sstevel@tonic-gate #if DEBUG
12350Sstevel@tonic-gate p->plg_last_seq_out = q_node->aqq_sequence;
12360Sstevel@tonic-gate #endif
12370Sstevel@tonic-gate switch (plugrc) {
12380Sstevel@tonic-gate case AUDITD_RETRY:
12395007Spaulson if (!once) {
12405007Spaulson report_error(plugrc, error_string, p->plg_path);
12415007Spaulson once = B_TRUE;
12425007Spaulson }
12430Sstevel@tonic-gate free(error_string);
12440Sstevel@tonic-gate error_string = NULL;
12450Sstevel@tonic-gate
12460Sstevel@tonic-gate DPRINT((dbfp, "process(%d) AUDITD_RETRY returned."
12470Sstevel@tonic-gate " cnt=%d (if 1, enter retry)\n",
12480Sstevel@tonic-gate p->plg_tid, p->plg_cnt));
12490Sstevel@tonic-gate
12500Sstevel@tonic-gate if (p->plg_cnt) /* if cnt is on, lose the buffer */
12510Sstevel@tonic-gate break;
12520Sstevel@tonic-gate
12530Sstevel@tonic-gate delay.tv_sec = p->plg_retry_time;
12540Sstevel@tonic-gate (void) pthread_mutex_lock(&(p->plg_mutex));
12550Sstevel@tonic-gate p->plg_waiting++;
12560Sstevel@tonic-gate (void) pthread_cond_reltimedwait_np(&(p->plg_cv),
12575007Spaulson &(p->plg_mutex), &delay);
12580Sstevel@tonic-gate p->plg_waiting--;
12590Sstevel@tonic-gate (void) pthread_mutex_unlock(&(p->plg_mutex));
12600Sstevel@tonic-gate
12610Sstevel@tonic-gate DPRINT((dbfp, "left retry mode for %d\n", p->plg_tid));
12620Sstevel@tonic-gate goto retry_mode;
12630Sstevel@tonic-gate
12640Sstevel@tonic-gate case AUDITD_SUCCESS:
12650Sstevel@tonic-gate p->plg_output++;
12665007Spaulson once = B_FALSE;
12670Sstevel@tonic-gate break;
12680Sstevel@tonic-gate default:
12690Sstevel@tonic-gate report_error(plugrc, error_string, p->plg_path);
12700Sstevel@tonic-gate free(error_string);
12710Sstevel@tonic-gate error_string = NULL;
12720Sstevel@tonic-gate break;
12730Sstevel@tonic-gate } /* end switch */
12740Sstevel@tonic-gate bpool_return(b_node);
12750Sstevel@tonic-gate qpool_return(p, q_node);
12760Sstevel@tonic-gate
12770Sstevel@tonic-gate sendsignal = 0;
12780Sstevel@tonic-gate queue_len = audit_queue_size(&(p->plg_queue));
12790Sstevel@tonic-gate
12800Sstevel@tonic-gate (void) pthread_mutex_lock(&(in_thr.thd_mutex));
12810Sstevel@tonic-gate if (in_thr.thd_waiting && (queue_len > p->plg_qmin) &&
12820Sstevel@tonic-gate (queue_len < p->plg_q_threshold))
12830Sstevel@tonic-gate sendsignal = 1;
12840Sstevel@tonic-gate
12850Sstevel@tonic-gate (void) pthread_mutex_unlock(&(in_thr.thd_mutex));
12860Sstevel@tonic-gate
12870Sstevel@tonic-gate if (sendsignal) {
12880Sstevel@tonic-gate (void) pthread_cond_signal(&(in_thr.thd_cv));
12890Sstevel@tonic-gate /*
12900Sstevel@tonic-gate * sched_yield(); does not help
12910Sstevel@tonic-gate * performance and in artificial tests
12920Sstevel@tonic-gate * (high sustained volume) appears to
12930Sstevel@tonic-gate * hurt by adding wide variability in
12940Sstevel@tonic-gate * the results.
12950Sstevel@tonic-gate */
12960Sstevel@tonic-gate } else if ((p->plg_priority < BASE_PRIORITY) &&
12970Sstevel@tonic-gate (queue_len < p->plg_q_threshold)) {
12980Sstevel@tonic-gate p->plg_priority = param.sched_priority =
12990Sstevel@tonic-gate BASE_PRIORITY;
13000Sstevel@tonic-gate (void) pthread_setschedparam(p->plg_tid, SCHED_OTHER,
13010Sstevel@tonic-gate ¶m);
13020Sstevel@tonic-gate }
13030Sstevel@tonic-gate } /* end for (;;) */
13045007Spaulson plugin_removed:
13050Sstevel@tonic-gate DUMP("process", p, p->plg_last_seq_out, "exit");
13060Sstevel@tonic-gate error_string = NULL;
13070Sstevel@tonic-gate if ((rc = p->plg_fplugin_close(&error_string)) !=
13080Sstevel@tonic-gate AUDITD_SUCCESS)
13090Sstevel@tonic-gate report_error(rc, error_string, p->plg_path);
13100Sstevel@tonic-gate
13110Sstevel@tonic-gate free(error_string);
13120Sstevel@tonic-gate
13130Sstevel@tonic-gate (void) pthread_mutex_lock(&plugin_mutex);
13140Sstevel@tonic-gate (void) unload_plugin(p);
13150Sstevel@tonic-gate (void) pthread_mutex_unlock(&plugin_mutex);
13160Sstevel@tonic-gate }
1317