xref: /titanic_52/usr/src/uts/common/os/audit_memory.c (revision 1a5e258f5471356ca102c7176637cdce45bac147)
1005d3febSMarek Pospisil /*
2005d3febSMarek Pospisil  * CDDL HEADER START
3005d3febSMarek Pospisil  *
4005d3febSMarek Pospisil  * The contents of this file are subject to the terms of the
5005d3febSMarek Pospisil  * Common Development and Distribution License (the "License").
6005d3febSMarek Pospisil  * You may not use this file except in compliance with the License.
7005d3febSMarek Pospisil  *
8005d3febSMarek Pospisil  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9005d3febSMarek Pospisil  * or http://www.opensolaris.org/os/licensing.
10005d3febSMarek Pospisil  * See the License for the specific language governing permissions
11005d3febSMarek Pospisil  * and limitations under the License.
12005d3febSMarek Pospisil  *
13005d3febSMarek Pospisil  * When distributing Covered Code, include this CDDL HEADER in each
14005d3febSMarek Pospisil  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15005d3febSMarek Pospisil  * If applicable, add the following below this CDDL HEADER, with the
16005d3febSMarek Pospisil  * fields enclosed by brackets "[]" replaced with your own identifying
17005d3febSMarek Pospisil  * information: Portions Copyright [yyyy] [name of copyright owner]
18005d3febSMarek Pospisil  *
19005d3febSMarek Pospisil  * CDDL HEADER END
20005d3febSMarek Pospisil  */
21005d3febSMarek Pospisil /*
22005d3febSMarek Pospisil  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23005d3febSMarek Pospisil  * Use is subject to license terms.
24005d3febSMarek Pospisil  */
25005d3febSMarek Pospisil 
26005d3febSMarek Pospisil #include <sys/param.h>
27005d3febSMarek Pospisil #include <sys/types.h>
28005d3febSMarek Pospisil #include <sys/kmem.h>
29005d3febSMarek Pospisil #include <c2/audit.h>
30005d3febSMarek Pospisil #include <c2/audit_kernel.h>
31005d3febSMarek Pospisil 
32005d3febSMarek Pospisil 
33005d3febSMarek Pospisil /* process audit data (pad) cache */
34005d3febSMarek Pospisil kmem_cache_t *au_pad_cache;
35005d3febSMarek Pospisil 
36005d3febSMarek Pospisil /*
37005d3febSMarek Pospisil  * increment audit path reference count
38005d3febSMarek Pospisil  */
39005d3febSMarek Pospisil void
40005d3febSMarek Pospisil au_pathhold(struct audit_path *app)
41005d3febSMarek Pospisil {
42*1a5e258fSJosef 'Jeff' Sipek 	atomic_inc_32(&app->audp_ref);
43005d3febSMarek Pospisil }
44005d3febSMarek Pospisil 
45005d3febSMarek Pospisil /*
46005d3febSMarek Pospisil  * decrement audit path reference count
47005d3febSMarek Pospisil  */
48005d3febSMarek Pospisil void
49005d3febSMarek Pospisil au_pathrele(struct audit_path *app)
50005d3febSMarek Pospisil {
51*1a5e258fSJosef 'Jeff' Sipek 	if (atomic_dec_32_nv(&app->audp_ref) > 0)
52005d3febSMarek Pospisil 		return;
53005d3febSMarek Pospisil 	kmem_free(app, app->audp_size);
54005d3febSMarek Pospisil }
55005d3febSMarek Pospisil 
56005d3febSMarek Pospisil /*
57005d3febSMarek Pospisil  * allocate a new auditpath
58005d3febSMarek Pospisil  *	newsect = increment sections count,
59005d3febSMarek Pospisil  *	charincr = change in strings storage
60005d3febSMarek Pospisil  */
61005d3febSMarek Pospisil 
62005d3febSMarek Pospisil struct audit_path *
63005d3febSMarek Pospisil au_pathdup(const struct audit_path *oldapp, int newsect, int charincr)
64005d3febSMarek Pospisil {
65005d3febSMarek Pospisil 	struct audit_path	*newapp;
66005d3febSMarek Pospisil 	int	i, alloc_size, oldlen;
67005d3febSMarek Pospisil 	char	*oldcp, *newcp;
68005d3febSMarek Pospisil 
69005d3febSMarek Pospisil 	newsect = (newsect != 0);
70005d3febSMarek Pospisil 	oldcp = oldapp->audp_sect[0];
71005d3febSMarek Pospisil 	oldlen = (oldapp->audp_sect[oldapp->audp_cnt] - oldcp);
72005d3febSMarek Pospisil 	alloc_size = sizeof (struct audit_path) +
73005d3febSMarek Pospisil 	    (oldapp->audp_cnt + newsect) * sizeof (char *) +
74005d3febSMarek Pospisil 	    oldlen + charincr;
75005d3febSMarek Pospisil 
76005d3febSMarek Pospisil 	newapp = kmem_alloc(alloc_size, KM_SLEEP);
77005d3febSMarek Pospisil 	newapp->audp_ref = 1;
78005d3febSMarek Pospisil 	newapp->audp_size = alloc_size;
79005d3febSMarek Pospisil 
80005d3febSMarek Pospisil 	newapp->audp_cnt = oldapp->audp_cnt + newsect;
81005d3febSMarek Pospisil 	newcp = (char *)(&newapp->audp_sect[newapp->audp_cnt + 1]);
82005d3febSMarek Pospisil 	for (i = 0; i <= oldapp->audp_cnt; i++) {
83005d3febSMarek Pospisil 		newapp->audp_sect[i] = newcp +
84005d3febSMarek Pospisil 		    (oldapp->audp_sect[i] - oldcp);
85005d3febSMarek Pospisil 	}
86005d3febSMarek Pospisil 	/*
87005d3febSMarek Pospisil 	 * if this is a new section, set its end
88005d3febSMarek Pospisil 	 * if this is an extended section, reset its end
89005d3febSMarek Pospisil 	 */
90005d3febSMarek Pospisil 	newapp->audp_sect[newapp->audp_cnt] = newcp + oldlen + charincr;
91005d3febSMarek Pospisil 	/* copy all of the old strings */
92005d3febSMarek Pospisil 	bcopy(oldcp, newcp, oldlen);
93005d3febSMarek Pospisil 
94005d3febSMarek Pospisil 	return (newapp);
95005d3febSMarek Pospisil }
96005d3febSMarek Pospisil 
97005d3febSMarek Pospisil /*ARGSUSED1*/
98005d3febSMarek Pospisil static int
99005d3febSMarek Pospisil au_pad_const(void *vpad, void *priv, int flags)
100005d3febSMarek Pospisil {
101005d3febSMarek Pospisil 	p_audit_data_t *pad = vpad;
102005d3febSMarek Pospisil 
103005d3febSMarek Pospisil 	mutex_init(&pad->pad_lock, NULL, MUTEX_DEFAULT, NULL);
104005d3febSMarek Pospisil 
105005d3febSMarek Pospisil 	return (0);
106005d3febSMarek Pospisil }
107005d3febSMarek Pospisil 
108005d3febSMarek Pospisil /*ARGSUSED1*/
109005d3febSMarek Pospisil static void
110005d3febSMarek Pospisil au_pad_destr(void *vpad, void *priv)
111005d3febSMarek Pospisil {
112005d3febSMarek Pospisil 	p_audit_data_t *pad = vpad;
113005d3febSMarek Pospisil 
114005d3febSMarek Pospisil 	mutex_destroy(&pad->pad_lock);
115005d3febSMarek Pospisil }
116005d3febSMarek Pospisil 
117005d3febSMarek Pospisil void
118005d3febSMarek Pospisil au_pad_init()
119005d3febSMarek Pospisil {
120005d3febSMarek Pospisil 	au_pad_cache = kmem_cache_create("audit_proc",
121005d3febSMarek Pospisil 	    sizeof (p_audit_data_t), 0, au_pad_const, au_pad_destr,
122005d3febSMarek Pospisil 	    NULL, NULL, NULL, 0);
123005d3febSMarek Pospisil }
124