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
511129SJan.Friedel@Sun.COM * Common Development and Distribution License (the "License").
611129SJan.Friedel@Sun.COM * 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*11706SJan.Friedel@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <pthread.h>
270Sstevel@tonic-gate #include <memory.h>
280Sstevel@tonic-gate #include "queue.h"
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <assert.h>
310Sstevel@tonic-gate #include "plugin.h"
320Sstevel@tonic-gate
330Sstevel@tonic-gate #define DEBUG 0
340Sstevel@tonic-gate
350Sstevel@tonic-gate #if DEBUG
3611129SJan.Friedel@Sun.COM extern FILE *dbfp;
370Sstevel@tonic-gate extern FILE *__auditd_debug_file_open();
38*11706SJan.Friedel@Sun.COM #define DPRINT(x) { (void) fprintf x; }
390Sstevel@tonic-gate #else
400Sstevel@tonic-gate #define DPRINT(x)
410Sstevel@tonic-gate #endif
420Sstevel@tonic-gate
430Sstevel@tonic-gate void
audit_queue_init(au_queue_t * q)440Sstevel@tonic-gate audit_queue_init(au_queue_t *q)
450Sstevel@tonic-gate {
460Sstevel@tonic-gate q->auq_head = NULL;
470Sstevel@tonic-gate q->auq_tail = NULL;
480Sstevel@tonic-gate (void) pthread_mutex_init(&q->auq_lock, NULL);
490Sstevel@tonic-gate q->auq_count = 0;
500Sstevel@tonic-gate #if DEBUG
5111129SJan.Friedel@Sun.COM if (dbfp == NULL) {
5211129SJan.Friedel@Sun.COM dbfp = __auditd_debug_file_open();
5311129SJan.Friedel@Sun.COM }
540Sstevel@tonic-gate #endif
550Sstevel@tonic-gate }
560Sstevel@tonic-gate
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate * enqueue() caller creates queue entry
590Sstevel@tonic-gate */
600Sstevel@tonic-gate
610Sstevel@tonic-gate void
audit_enqueue(au_queue_t * q,void * p)620Sstevel@tonic-gate audit_enqueue(au_queue_t *q, void *p)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate (void) pthread_mutex_lock(&q->auq_lock);
650Sstevel@tonic-gate
66*11706SJan.Friedel@Sun.COM DPRINT((dbfp, "enqueue0(%p): p=%p, head=%p, tail=%p, count=%d\n",
67*11706SJan.Friedel@Sun.COM (void *)q, (void *)p, (void *)q->auq_head, (void *)q->auq_tail,
68*11706SJan.Friedel@Sun.COM q->auq_count));
690Sstevel@tonic-gate
700Sstevel@tonic-gate if (q->auq_head == NULL)
710Sstevel@tonic-gate q->auq_head = p;
720Sstevel@tonic-gate else {
73*11706SJan.Friedel@Sun.COM DPRINT((dbfp, "\tindirect tail=%p\n",
74*11706SJan.Friedel@Sun.COM (void *)&(((audit_link_t *)(q->auq_tail))->aln_next)));
750Sstevel@tonic-gate
760Sstevel@tonic-gate ((audit_link_t *)(q->auq_tail))->aln_next = p;
770Sstevel@tonic-gate }
780Sstevel@tonic-gate q->auq_tail = p;
790Sstevel@tonic-gate ((audit_link_t *)p)->aln_next = NULL;
800Sstevel@tonic-gate q->auq_count++;
810Sstevel@tonic-gate
82*11706SJan.Friedel@Sun.COM DPRINT((dbfp, "enqueue1(%p): p=%p, head=%p, tail=%p, "
83*11706SJan.Friedel@Sun.COM "count=%d, pnext=%p\n", (void *)q, (void *)p, (void *)q->auq_head,
84*11706SJan.Friedel@Sun.COM (void *)q->auq_tail, q->auq_count,
85*11706SJan.Friedel@Sun.COM (void *)((audit_link_t *)p)->aln_next));
860Sstevel@tonic-gate
870Sstevel@tonic-gate (void) pthread_mutex_unlock(&q->auq_lock);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate * audit_dequeue() returns entry; caller is responsible for free
920Sstevel@tonic-gate */
930Sstevel@tonic-gate
940Sstevel@tonic-gate int
audit_dequeue(au_queue_t * q,void ** p)950Sstevel@tonic-gate audit_dequeue(au_queue_t *q, void **p)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate (void) pthread_mutex_lock(&q->auq_lock);
980Sstevel@tonic-gate
990Sstevel@tonic-gate if ((*p = q->auq_head) == NULL) {
100*11706SJan.Friedel@Sun.COM DPRINT((dbfp, "dequeue1(%p): p=%p, head=%p, "
101*11706SJan.Friedel@Sun.COM "tail=%p, count=%d\n", (void *)q, (void *)*p,
102*11706SJan.Friedel@Sun.COM (void *)q->auq_head, (void *)q->auq_tail, q->auq_count));
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate (void) pthread_mutex_unlock(&q->auq_lock);
1050Sstevel@tonic-gate return (1);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate q->auq_count--;
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate /* if *p is the last, next is NULL */
1100Sstevel@tonic-gate q->auq_head = ((audit_link_t *)*p)->aln_next;
1110Sstevel@tonic-gate
112*11706SJan.Friedel@Sun.COM DPRINT((dbfp, "dequeue0(%p): p=%p, head=%p, tail=%p, "
113*11706SJan.Friedel@Sun.COM "count=%d, pnext=%p\n", (void *)q, (void *)*p, (void *)q->auq_head,
114*11706SJan.Friedel@Sun.COM (void *)q->auq_tail, q->auq_count,
115*11706SJan.Friedel@Sun.COM (void *)((audit_link_t *)*p)->aln_next));
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate (void) pthread_mutex_unlock(&q->auq_lock);
1180Sstevel@tonic-gate return (0);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate * increment ref count
1230Sstevel@tonic-gate */
1240Sstevel@tonic-gate void
audit_incr_ref(pthread_mutex_t * l,audit_rec_t * p)1250Sstevel@tonic-gate audit_incr_ref(pthread_mutex_t *l, audit_rec_t *p)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate (void) pthread_mutex_lock(l);
1280Sstevel@tonic-gate p->abq_ref_count++;
129*11706SJan.Friedel@Sun.COM DPRINT((dbfp, "incr_ref: p=%p, count=%d\n",
130*11706SJan.Friedel@Sun.COM (void *)p, p->abq_ref_count));
1310Sstevel@tonic-gate (void) pthread_mutex_unlock(l);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate * decrement reference count; if it reaches zero,
1350Sstevel@tonic-gate * return a pointer to it. Otherwise, return NULL.
1360Sstevel@tonic-gate */
1370Sstevel@tonic-gate audit_rec_t *
audit_release(pthread_mutex_t * l,audit_rec_t * p)1380Sstevel@tonic-gate audit_release(pthread_mutex_t *l, audit_rec_t *p)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate assert(p != NULL);
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate (void) pthread_mutex_lock(l);
1430Sstevel@tonic-gate
144*11706SJan.Friedel@Sun.COM DPRINT((dbfp, "release: p=%p , count=%d\n",
145*11706SJan.Friedel@Sun.COM (void *)p, p->abq_ref_count));
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate if (--(p->abq_ref_count) > 0) {
1480Sstevel@tonic-gate (void) pthread_mutex_unlock(l);
1490Sstevel@tonic-gate return (NULL);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate (void) pthread_mutex_unlock(l);
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate return (p);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate int
audit_queue_size(au_queue_t * q)1570Sstevel@tonic-gate audit_queue_size(au_queue_t *q)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate int size;
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate (void) pthread_mutex_lock(&q->auq_lock);
1620Sstevel@tonic-gate size = q->auq_count;
1630Sstevel@tonic-gate (void) pthread_mutex_unlock(&q->auq_lock);
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate return (size);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate void
audit_queue_destroy(au_queue_t * q)1700Sstevel@tonic-gate audit_queue_destroy(au_queue_t *q)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate (void) pthread_mutex_destroy(&q->auq_lock);
1730Sstevel@tonic-gate }
174