~ubuntu-branches/ubuntu/maverick/audit/maverick

« back to all changes in this revision

Viewing changes to new_audispd/queue.c

  • Committer: Bazaar Package Importer
  • Author(s): Mathias Gug
  • Date: 2007-06-29 13:05:14 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070629130514-z798cz4lebiahj5w
Tags: 1.5.4-0ubuntu1
* New upstream version.
* debian/patches/audit-1.5.1-dist.patch:
  * update so that it applies for 1.5.4.
* debian/control:
  * update Maintainer and XSBC-Original-Maintainer fields.
* debian/rules:
  * enable apparmor support: add --with-apparmor to configure options.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* queue.c --
 
2
 * Copyright 2007 Red Hat Inc., Durham, North Carolina.
 
3
 * All Rights Reserved.
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2.1 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with this library; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 *
 
19
 * Authors:
 
20
 *      Steve Grubb <sgrubb@redhat.com>
 
21
 */
 
22
 
 
23
#include "config.h"
 
24
#include <stdlib.h>
 
25
#include <pthread.h>
 
26
#include <syslog.h>
 
27
#include "queue.h"
 
28
 
 
29
static volatile event_t **q;
 
30
static pthread_mutex_t queue_lock;
 
31
static pthread_cond_t queue_nonempty;
 
32
static unsigned int q_next, q_last, q_depth;
 
33
 
 
34
 
 
35
int init_queue(unsigned size)
 
36
{
 
37
        unsigned int i;
 
38
 
 
39
        q_next = 0;
 
40
        q_last = 0;
 
41
        q_depth = size;
 
42
        q = malloc(q_depth * sizeof(event_t *));
 
43
        if (q == NULL)
 
44
                return -1;
 
45
 
 
46
        for (i=0; i<q_depth; i++) 
 
47
                q[i] = NULL;
 
48
 
 
49
        /* Setup IPC mechanisms */
 
50
        pthread_mutex_init(&queue_lock, NULL);
 
51
        pthread_cond_init(&queue_nonempty, NULL);
 
52
 
 
53
        return 0;
 
54
}
 
55
 
 
56
void enqueue(event_t *e)
 
57
{
 
58
        unsigned int n, retry_cnt = 0;
 
59
 
 
60
retry:
 
61
        // We allow 1 retry and then its over
 
62
        if (retry_cnt > 1) {
 
63
                syslog(LOG_ERR, "queue is full - dropping event");
 
64
                return;
 
65
        }
 
66
        pthread_mutex_lock(&queue_lock);
 
67
 
 
68
        // OK, have lock add event
 
69
        n = q_next%q_depth;
 
70
        if (q[n] == NULL) {
 
71
                q[n] = e;
 
72
                q_next = (n+1) % q_depth;
 
73
                pthread_mutex_unlock(&queue_lock);
 
74
                pthread_cond_signal(&queue_nonempty);
 
75
        } else {
 
76
                pthread_mutex_unlock(&queue_lock);
 
77
                pthread_yield(); // Let dequeue thread run to clear queue
 
78
                retry_cnt++;
 
79
                goto retry;
 
80
        }
 
81
}
 
82
 
 
83
event_t *dequeue(void)
 
84
{
 
85
        event_t *e;
 
86
        unsigned int n;
 
87
 
 
88
        // Wait until its got something in it
 
89
        pthread_mutex_lock(&queue_lock);
 
90
        n = q_last%q_depth;
 
91
        if (q[n] == NULL) {
 
92
                pthread_cond_wait(&queue_nonempty, &queue_lock);
 
93
                n = q_last%q_depth;
 
94
        }
 
95
 
 
96
        // OK, grab the next event
 
97
        if (q[n] != NULL) {
 
98
                e = q[n];
 
99
                q[n] = NULL;
 
100
                q_last = (n+1) % q_depth;
 
101
        } else
 
102
                e = NULL;
 
103
 
 
104
        pthread_mutex_unlock(&queue_lock);
 
105
 
 
106
        // Process the event
 
107
        return e;
 
108
}
 
109
 
 
110
void nudge_queue(void)
 
111
{
 
112
        pthread_cond_signal(&queue_nonempty);
 
113
}
 
114
 
 
115
void destroy_queue(void)
 
116
{
 
117
        unsigned int i;
 
118
 
 
119
        for (i=0; i<q_depth; i++)
 
120
                free(q[i]);
 
121
 
 
122
        free(q);
 
123
}
 
124