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 2009 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 
     27 #include <c2/audit.h>
     28 #include <c2/audit_kernel.h>
     29 #include <c2/audit_record.h>
     30 #include <sys/kmem.h>
     31 #include <sys/param.h>
     32 #include <sys/systm.h>
     33 #include <sys/taskq.h>
     34 #include <sys/t_lock.h>
     35 #include <sys/thread.h>
     36 #include <sys/types.h>
     37 #include <sys/zone.h>
     38 
     39 zone_key_t au_zone_key;
     40 
     41 /*ARGSUSED*/
     42 static void *
     43 au_zone_init(zoneid_t zone)
     44 {
     45 	au_kcontext_t	*kctx = kmem_zalloc(sizeof (au_kcontext_t), KM_SLEEP);
     46 	static au_kcontext_t	*global_kctx = NULL;
     47 
     48 	/*
     49 	 * INGLOBALZONE(curproc) is invalid at this point, so check for
     50 	 * zone 0
     51 	 */
     52 
     53 	if (zone == 0) {
     54 		global_kctx = kctx;
     55 		global_zone->zone_audit_kctxt = kctx;
     56 	} else {
     57 		kctx->auk_policy = global_kctx->auk_policy;
     58 		curproc->p_zone->zone_audit_kctxt = kctx;
     59 	}
     60 	kctx->auk_valid = AUK_VALID;
     61 	kctx->auk_zid = zone;
     62 
     63 	kctx->auk_info.ai_termid.at_type = AU_IPv4;
     64 	kctx->auk_info.ai_auid = AU_NOAUDITID;
     65 	kctx->auk_auditstate = AUC_INIT_AUDIT;
     66 
     67 	/* setup defaults for audit queue flow control */
     68 	kctx->auk_queue.hiwater = AQ_HIWATER;
     69 	kctx->auk_queue.lowater = AQ_LOWATER;
     70 	kctx->auk_queue.bufsz   = AQ_BUFSZ;
     71 	kctx->auk_queue.buflen  = AQ_BUFSZ;
     72 	kctx->auk_queue.delay   = AQ_DELAY;
     73 
     74 	/* statistics per zone */
     75 	kctx->auk_statistics.as_version  = TOKEN_VERSION;
     76 	kctx->auk_statistics.as_numevent = MAX_KEVENTS;
     77 
     78 	/* door IO buffer: */
     79 	kctx->auk_dbuffer =
     80 	    kmem_alloc(AU_DBUF_HEADER + kctx->auk_queue.bufsz, KM_SLEEP);
     81 
     82 	/* locks and cv's */
     83 
     84 	mutex_init(&(kctx->auk_eagain_mutex), NULL, MUTEX_DEFAULT, NULL);
     85 	cv_init(&(kctx->auk_eagain_cv), NULL, CV_DRIVER, NULL);
     86 
     87 	mutex_init(&(kctx->auk_svc_lock), NULL, MUTEX_DEFAULT, NULL);
     88 
     89 	mutex_init(&(kctx->auk_queue.lock), NULL, MUTEX_DEFAULT, NULL);
     90 	cv_init(&(kctx->auk_queue.write_cv), NULL, CV_DRIVER, NULL);
     91 	cv_init(&(kctx->auk_queue.read_cv), NULL, CV_DRIVER, NULL);
     92 
     93 	return (kctx);
     94 }
     95 
     96 /*ARGSUSED*/
     97 static void
     98 au_zone_shutdown(zoneid_t zone, void *arg)
     99 {
    100 	au_kcontext_t	*kctx = arg;
    101 
    102 	if ((kctx->auk_zid == GLOBAL_ZONEID ||
    103 	    (audit_policy | AUDIT_PERZONE)) &&
    104 	    (kctx->auk_current_vp != NULL))
    105 		(void) au_doormsg(kctx, AU_DBUF_SHUTDOWN, NULL);
    106 
    107 	kctx->auk_valid = AUK_INVALID;
    108 
    109 	/* shutdown the output thread if it is still running */
    110 	kctx->auk_auditstate = AUC_NOAUDIT;
    111 
    112 	if (kctx->auk_output_active) {
    113 		mutex_enter(&(kctx->auk_queue.lock));
    114 		cv_broadcast(&(kctx->auk_queue.read_cv));
    115 		mutex_exit(&(kctx->auk_queue.lock));
    116 
    117 		taskq_destroy(kctx->auk_taskq);
    118 	}
    119 }
    120 
    121 /*ARGSUSED*/
    122 static void
    123 au_zone_destroy(zoneid_t zone, void *arg)
    124 {
    125 	au_kcontext_t	*kctx = arg;
    126 
    127 	ASSERT(kctx->auk_auditstate == AUC_NOAUDIT);
    128 
    129 	mutex_destroy(&(kctx->auk_eagain_mutex));
    130 	cv_destroy(&(kctx->auk_eagain_cv));
    131 
    132 	mutex_destroy(&(kctx->auk_svc_lock));
    133 
    134 	mutex_enter(&(kctx->auk_queue.lock));
    135 	if (kctx->auk_queue.head != NULL) {
    136 		au_free_rec(kctx->auk_queue.head);
    137 	}
    138 	mutex_exit(&(kctx->auk_queue.lock));
    139 
    140 	mutex_destroy(&(kctx->auk_queue.lock));
    141 
    142 	cv_destroy(&(kctx->auk_queue.write_cv));
    143 	cv_destroy(&(kctx->auk_queue.read_cv));
    144 
    145 	kmem_free(kctx->auk_dbuffer, AU_DBUF_HEADER + kctx->auk_queue.bufsz);
    146 
    147 	kmem_free(kctx, sizeof (au_kcontext_t));
    148 }
    149 
    150 void
    151 au_zone_setup()
    152 {
    153 	zone_key_create(&au_zone_key, au_zone_init, au_zone_shutdown,
    154 	    au_zone_destroy);
    155 
    156 }
    157