Imported Upstream version 2.4.3
[platform/upstream/audit.git] / src / ausearch-llist.c
1 /*
2 * ausearch-llist.c - Minimal linked list library
3 * Copyright (c) 2005-2008, 2011 Red Hat Inc., Durham, North Carolina.
4 * Copyright (c) 2011 IBM Corp.
5 * All Rights Reserved. 
6 *
7 * This software may be freely redistributed and/or modified under the
8 * terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * Authors:
22 *   Steve Grubb <sgrubb@redhat.com>
23 *   Marcelo Henrique Cerri <mhcerri@br.ibm.com>
24 */
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include "ausearch-llist.h"
29
30 void list_create(llist *l)
31 {
32         l->head = NULL;
33         l->cur = NULL;
34         l->cnt = 0;
35         l->e.milli = 0L;       
36         l->e.sec = 0L;         
37         l->e.serial = 0L;      
38         l->e.node = NULL;      
39         l->e.type = 0;      
40         l->s.gid = -1;          
41         l->s.egid = -1;         
42         l->s.ppid = -1;            
43         l->s.pid = -1;            
44         l->s.success = S_UNSET;
45         l->s.uid = -1;            
46         l->s.euid = -1;           
47         l->s.loginuid = -2;
48         l->s.hostname = NULL;
49         l->s.filename = NULL;
50         l->s.terminal = NULL;
51         l->s.cwd = NULL;
52         l->s.exe = NULL;
53         l->s.key = NULL;
54         l->s.comm = NULL;
55         l->s.avc = NULL;
56         l->s.acct = NULL;
57         l->s.arch = 0;
58         l->s.syscall = 0;
59         l->s.session_id = -2;
60         l->s.uuid = NULL;
61         l->s.vmname = NULL;
62         l->s.exit = 0;
63         l->s.exit_is_set = 0;
64 }
65
66 void list_last(llist *l)
67 {
68         register lnode* window;
69         
70         if (l->head == NULL)
71                 return;
72
73         window = l->head;
74         while (window->next)
75                 window = window->next;
76         l->cur = window;
77 }
78
79 lnode *list_next(llist *l)
80 {
81         if (l->cur == NULL)
82                 return NULL;
83         l->cur = l->cur->next;
84         return l->cur;
85 }
86
87 lnode *list_prev(llist *l)
88 {
89         if (l->cur == NULL)
90                 return NULL;
91
92         if (l->cur->item == 0)
93                 return NULL;
94
95         list_find_item(l, l->cur->item-1);
96         return l->cur;
97 }
98
99 void list_append(llist *l, lnode *node)
100 {
101         lnode* newnode;
102
103         newnode = malloc(sizeof(lnode));
104
105         if (node->message)
106                 newnode->message = node->message;
107         else
108                 newnode->message = NULL;
109
110         newnode->type = node->type;
111         newnode->a0 = node->a0;
112         newnode->a1 = node->a1;
113         newnode->item = l->cnt; 
114         newnode->next = NULL;
115
116         // if we are at top, fix this up
117         if (l->head == NULL)
118                 l->head = newnode;
119         else    // Otherwise add pointer to newnode
120                 l->cur->next = newnode;
121
122         // make newnode current
123         l->cur = newnode;
124         l->cnt++;
125 }
126
127 int list_find_item(llist *l, unsigned int i)
128 {
129         register lnode* window;
130                                                                                 
131         if (l->cur && (l->cur->item <= i))
132                 window = l->cur;        /* Try to use where we are */
133         else
134                 window = l->head;       /* Can't, start over */
135
136         while (window) {
137                 if (window->item == i) {
138                         l->cur = window;
139                         return 1;
140                 } else
141                         window = window->next;
142         }
143         return 0;
144 }
145
146 void list_clear(llist* l)
147 {
148         lnode* nextnode;
149         register lnode* current;
150
151         current = l->head;
152         while (current) {
153                 nextnode=current->next;
154                 free(current->message);
155                 free(current);
156                 current=nextnode;
157         }
158         l->head = NULL;
159         l->cur = NULL;
160         l->cnt = 0;
161         l->e.milli = 0L;       
162         l->e.sec = 0L;         
163         l->e.serial = 0L;      
164         free((char *)l->e.node);
165         l->e.node = NULL;
166         l->e.type = 0;         
167         l->s.gid = -1;
168         l->s.egid = -1;
169         l->s.ppid = -1;
170         l->s.pid = -1;
171         l->s.success = S_UNSET;
172         l->s.uid = -1;
173         l->s.euid = -1;
174         l->s.loginuid = -2;
175         free(l->s.hostname);
176         l->s.hostname = NULL;
177         if (l->s.filename) {
178                 slist_clear(l->s.filename);
179                 free(l->s.filename);
180                 l->s.filename = NULL;
181         }
182         free(l->s.terminal);
183         l->s.terminal = NULL;
184         free(l->s.cwd);
185         l->s.cwd = NULL;
186         free(l->s.exe);
187         l->s.exe = NULL;
188         if (l->s.key) {
189                 slist_clear(l->s.key);
190                 free(l->s.key);
191                 l->s.key = NULL;
192         }
193         free(l->s.comm);
194         l->s.comm = NULL;
195         if (l->s.avc) {
196                 alist_clear(l->s.avc);
197                 free(l->s.avc);
198                 l->s.avc = NULL;
199         }
200         free(l->s.acct);
201         l->s.acct = NULL;
202         l->s.arch = 0;
203         l->s.syscall = 0;
204         l->s.session_id = -2;
205         free(l->s.uuid);
206         l->s.uuid = NULL;
207         free(l->s.vmname);
208         l->s.vmname = NULL;
209         l->s.exit = 0;
210         l->s.exit_is_set = 0;
211 }
212
213 int list_get_event(llist* l, event *e)
214 {
215         if (l == NULL || e == NULL)
216                 return 0;
217
218         e->sec = l->e.sec;
219         e->milli = l->e.milli;
220         e->serial = l->e.serial;
221         return 1;
222 }
223
224 lnode *list_find_msg(llist *l, int i)
225 {
226         register lnode* window;
227                                                                                 
228         window = l->head;       /* start at the beginning */
229         while (window) {
230                 if (window->type == i) {
231                         l->cur = window;
232                         return window;
233                 } else
234                         window = window->next;
235         }
236         return NULL;
237 }
238
239 lnode *list_find_msg_range(llist *l, int low, int high)
240 {
241         register lnode* window;
242
243         if (high <= low)
244                 return NULL;
245
246         window = l->head;       /* Start at the beginning */
247         while (window) {
248                 if (window->type >= low && window->type <= high) {
249                         l->cur = window;
250                         return window;
251                 } else
252                         window = window->next;
253         }
254         return NULL;
255 }
256