Home | History | Annotate | Download | only in c2
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 /*
     22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
     27 
     28 #include <c2/audit.h>
     29 #include <c2/audit_kernel.h>
     30 #include <c2/audit_record.h>
     31 #include <sys/kmem.h>
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/taskq.h>
     35 #include <sys/t_lock.h>
     36 #include <sys/thread.h>
     37 #include <sys/types.h>
     38 #include <sys/zone.h>
     39 
     40 zone_key_t au_zone_key;
     41 
     42 /*ARGSUSED*/
     43 static void *
     44 au_zone_init(zoneid_t zone)
     45 {
     46 	au_kcontext_t	*kctx = kmem_zalloc(sizeof (au_kcontext_t), KM_SLEEP);
     47 	static au_kcontext_t	*global_kctx = NULL;
     48 
     49 	/*
     50 	 * INGLOBALZONE(curproc) is invalid at this point, so check for
     51 	 * zone 0
     52 	 */
     53 
     54 	if (zone == 0) {
     55 		global_kctx = kctx;
     56 		global_zone->zone_audit_kctxt = kctx;
     57 		au_naevent = sizeof (kctx->auk_ets) / sizeof (au_state_t);
     58 	} else {
     59 		kctx->auk_policy = global_kctx->auk_policy;
     60 		curproc->p_zone->zone_audit_kctxt = kctx;
     61 	}
     62 	kctx->auk_valid = AUK_VALID;
     63 	kctx->auk_zid = zone;
     64 
     65 	kctx->auk_info.ai_termid.at_type = AU_IPv4;
     66 	kctx->auk_info.ai_auid = AU_NOAUDITID;
     67 	kctx->auk_auditstate = AUC_INIT_AUDIT;
     68 
     69 	/* setup defaults for audit queue flow control */
     70 	kctx->auk_queue.hiwater = AQ_HIWATER;
     71 	kctx->auk_queue.lowater = AQ_LOWATER;
     72 	kctx->auk_queue.bufsz   = AQ_BUFSZ;
     73 	kctx->auk_queue.buflen  = AQ_BUFSZ;
     74 	kctx->auk_queue.delay   = AQ_DELAY;
     75 
     76 	/* statistics per zone */
     77 	kctx->auk_statistics.as_version  = TOKEN_VERSION;
     78 	kctx->auk_statistics.as_numevent = au_naevent;
     79 
     80 	/* door IO buffer: */
     81 	kctx->auk_dbuffer =
     82 	    kmem_alloc(AU_DBUF_HEADER + kctx->auk_queue.bufsz, KM_SLEEP);
     83 
     84 	/* locks and cv's */
     85 
     86 	mutex_init(&(kctx->auk_fstat_lock), NULL, MUTEX_DEFAULT, NULL);
     87 
     88 	mutex_init(&(kctx->auk_eagain_mutex), NULL, MUTEX_DEFAULT, NULL);
     89 	cv_init(&(kctx->auk_eagain_cv), NULL, CV_DRIVER, NULL);
     90 
     91 	mutex_init(&(kctx->auk_svc_lock), NULL, MUTEX_DEFAULT, NULL);
     92 
     93 	mutex_init(&(kctx->auk_queue.lock), NULL, MUTEX_DEFAULT, NULL);
     94 	cv_init(&(kctx->auk_queue.write_cv), NULL, CV_DRIVER, NULL);
     95 	cv_init(&(kctx->auk_queue.read_cv), NULL, CV_DRIVER, NULL);
     96 
     97 	return (kctx);
     98 }
     99 
    100 /*ARGSUSED*/
    101 static void
    102 au_zone_shutdown(zoneid_t zone, void *arg)
    103 {
    104 	au_kcontext_t	*kctx = arg;
    105 
    106 	if ((kctx->auk_zid == GLOBAL_ZONEID ||
    107 	    (audit_policy | AUDIT_PERZONE)) &&
    108 	    (kctx->auk_current_vp != NULL))
    109 		(void) au_doormsg(kctx, AU_DBUF_SHUTDOWN, NULL);
    110 
    111 	kctx->auk_valid = AUK_INVALID;
    112 
    113 	/* shutdown the output thread if it is still running */
    114 	kctx->auk_auditstate = AUC_NOAUDIT;
    115 
    116 	if (kctx->auk_output_active) {
    117 		mutex_enter(&(kctx->auk_queue.lock));
    118 		cv_broadcast(&(kctx->auk_queue.read_cv));
    119 		mutex_exit(&(kctx->auk_queue.lock));
    120 
    121 		taskq_destroy(kctx->auk_taskq);
    122 	}
    123 }
    124 
    125 /*ARGSUSED*/
    126 static void
    127 au_zone_destroy(zoneid_t zone, void *arg)
    128 {
    129 	au_kcontext_t	*kctx = arg;
    130 
    131 	ASSERT(kctx->auk_auditstate == AUC_NOAUDIT);
    132 
    133 	mutex_destroy(&(kctx->auk_fstat_lock));
    134 
    135 	mutex_destroy(&(kctx->auk_eagain_mutex));
    136 	cv_destroy(&(kctx->auk_eagain_cv));
    137 
    138 	mutex_destroy(&(kctx->auk_svc_lock));
    139 
    140 	mutex_enter(&(kctx->auk_queue.lock));
    141 	if (kctx->auk_queue.head != NULL) {
    142 		au_free_rec(kctx->auk_queue.head);
    143 	}
    144 	mutex_exit(&(kctx->auk_queue.lock));
    145 
    146 	mutex_destroy(&(kctx->auk_queue.lock));
    147 
    148 	cv_destroy(&(kctx->auk_queue.write_cv));
    149 	cv_destroy(&(kctx->auk_queue.read_cv));
    150 
    151 	kmem_free(kctx->auk_dbuffer, AU_DBUF_HEADER + kctx->auk_queue.bufsz);
    152 
    153 	kmem_free(kctx, sizeof (au_kcontext_t));
    154 }
    155 
    156 void
    157 au_zone_setup()
    158 {
    159 	zone_key_create(&au_zone_key, au_zone_init, au_zone_shutdown,
    160 	    au_zone_destroy);
    161 
    162 }
    163