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 
     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_fstat_lock), NULL, MUTEX_DEFAULT, NULL);
     85 
     86 	mutex_init(&(kctx->auk_eagain_mutex), NULL, MUTEX_DEFAULT, NULL);
     87 	cv_init(&(kctx->auk_eagain_cv), NULL, CV_DRIVER, NULL);
     88 
     89 	mutex_init(&(kctx->auk_svc_lock), NULL, MUTEX_DEFAULT, NULL);
     90 
     91 	mutex_init(&(kctx->auk_queue.lock), NULL, MUTEX_DEFAULT, NULL);
     92 	cv_init(&(kctx->auk_queue.write_cv), NULL, CV_DRIVER, NULL);
     93 	cv_init(&(kctx->auk_queue.read_cv), NULL, CV_DRIVER, NULL);
     94 
     95 	return (kctx);
     96 }
     97 
     98 /*ARGSUSED*/
     99 static void
    100 au_zone_shutdown(zoneid_t zone, void *arg)
    101 {
    102 	au_kcontext_t	*kctx = arg;
    103 
    104 	if ((kctx->auk_zid == GLOBAL_ZONEID ||
    105 	    (audit_policy | AUDIT_PERZONE)) &&
    106 	    (kctx->auk_current_vp != NULL))
    107 		(void) au_doormsg(kctx, AU_DBUF_SHUTDOWN, NULL);
    108 
    109 	kctx->auk_valid = AUK_INVALID;
    110 
    111 	/* shutdown the output thread if it is still running */
    112 	kctx->auk_auditstate = AUC_NOAUDIT;
    113 
    114 	if (kctx->auk_output_active) {
    115 		mutex_enter(&(kctx->auk_queue.lock));
    116 		cv_broadcast(&(kctx->auk_queue.read_cv));
    117 		mutex_exit(&(kctx->auk_queue.lock));
    118 
    119 		taskq_destroy(kctx->auk_taskq);
    120 	}
    121 }
    122 
    123 /*ARGSUSED*/
    124 static void
    125 au_zone_destroy(zoneid_t zone, void *arg)
    126 {
    127 	au_kcontext_t	*kctx = arg;
    128 
    129 	ASSERT(kctx->auk_auditstate == AUC_NOAUDIT);
    130 
    131 	mutex_destroy(&(kctx->auk_fstat_lock));
    132 
    133 	mutex_destroy(&(kctx->auk_eagain_mutex));
    134 	cv_destroy(&(kctx->auk_eagain_cv));
    135 
    136 	mutex_destroy(&(kctx->auk_svc_lock));
    137 
    138 	mutex_enter(&(kctx->auk_queue.lock));
    139 	if (kctx->auk_queue.head != NULL) {
    140 		au_free_rec(kctx->auk_queue.head);
    141 	}
    142 	mutex_exit(&(kctx->auk_queue.lock));
    143 
    144 	mutex_destroy(&(kctx->auk_queue.lock));
    145 
    146 	cv_destroy(&(kctx->auk_queue.write_cv));
    147 	cv_destroy(&(kctx->auk_queue.read_cv));
    148 
    149 	kmem_free(kctx->auk_dbuffer, AU_DBUF_HEADER + kctx->auk_queue.bufsz);
    150 
    151 	kmem_free(kctx, sizeof (au_kcontext_t));
    152 }
    153 
    154 void
    155 au_zone_setup()
    156 {
    157 	zone_key_create(&au_zone_key, au_zone_init, au_zone_shutdown,
    158 	    au_zone_destroy);
    159 
    160 }
    161