source: trunk/src/sh_fifo.c@ 136

Last change on this file since 136 was 22, checked in by rainer, 19 years ago

Minor code revisions.

File size: 4.9 KB
Line 
1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 1999, 2000 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "config_xor.h"
21
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <ctype.h>
26
27#undef FIL__
28#define FIL__ _("sh.fifo.c")
29
30
31#include "samhain.h"
32#include "sh_mem.h"
33#include "sh_unix.h"
34
35#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
36#include <sys/mman.h>
37#endif
38
39#define SH_FIFO_MAX 128
40
41struct dlist {
42 /*@null@*//*@dependent@*/ struct dlist * next;
43 char * data;
44 /*@null@*//*@dependent@*/ struct dlist * prev;
45};
46
47typedef struct fifo_str {
48 /*@null@*//*@dependent@*/ struct dlist * head_ptr;
49 /*@null@*//*@dependent@*/ struct dlist * tail_ptr;
50 int fifo_cts;
51} SH_FIFO;
52
53
54
55/* push an item on the head of the list
56 */
57int push_list (SH_FIFO * fifo, char * indat)
58{
59 struct dlist * item;
60 size_t len;
61
62 SL_ENTER(_("push_list"));
63
64 if (indat == NULL || fifo == NULL)
65 {
66 SL_RETURN((-1), _("push_list"));
67 }
68
69 if (fifo->fifo_cts > SH_FIFO_MAX)
70 {
71 SL_RETURN((-1), _("push_list"));
72 }
73
74 len = sl_strlen(indat);
75
76 if (len == 0)
77 {
78 SL_RETURN((-1), _("push_list"));
79 }
80 item = SH_ALLOC(sizeof(struct dlist));
81 /*@i@*/ item->data = SH_ALLOC(len+1);
82
83 if (NULL != sl_strstr (indat, _("LOGKEY")))
84 {
85 MLOCK(item->data, (len+1));
86 ;
87 }
88
89 sl_strlcpy (item->data, indat, len+1);
90 item->data[len] = '\0';
91
92 if (fifo->tail_ptr == NULL)
93 {
94 fifo->tail_ptr = item;
95 item->prev = NULL;
96 }
97 else
98 {
99 /*@i@*/ fifo->head_ptr->prev = item;
100 item->prev = NULL;
101 }
102
103 item->next = fifo->head_ptr;
104 fifo->head_ptr = item;
105
106 ++(fifo->fifo_cts);
107
108 SL_RETURN((fifo->fifo_cts), _("push_list"));
109}
110
111/* push an item on the tail of the list
112 */
113int push_tail_list (SH_FIFO * fifo, char * indat)
114{
115 struct dlist * item;
116 size_t len;
117
118 SL_ENTER(_("push_tail_list"));
119
120 if (indat == NULL || fifo == NULL)
121 {
122 SL_RETURN((-1), _("push_tail_list"));
123 }
124
125 if (fifo->fifo_cts > SH_FIFO_MAX)
126 {
127 SL_RETURN((-1), _("push_tail_list"));
128 }
129
130 len = sl_strlen(indat);
131 if (len == 0)
132 {
133 SL_RETURN((-1), _("push_list"));
134 }
135
136 item = SH_ALLOC(sizeof(struct dlist));
137 /*@i@*/item->data = SH_ALLOC(len+1);
138
139 if (NULL != sl_strstr (indat, _("LOGKEY")))
140 {
141 MLOCK(item->data, (len+1));
142 ;
143 }
144
145 sl_strlcpy (item->data, indat, len+1);
146 item->data[len] = '\0';
147
148 if (fifo->head_ptr == NULL)
149 {
150 item->next = NULL;
151 fifo->head_ptr = item;
152 }
153 else
154 {
155 item->next = NULL;
156 fifo->tail_ptr->next = item;
157 }
158
159 item->prev = fifo->tail_ptr;
160 fifo->tail_ptr = item;
161
162 ++(fifo->fifo_cts);
163
164 SL_RETURN((0), _("push_tail_list"));
165}
166
167/* pop an item from the tail of the list
168 */
169/*@null@*/ char * pop_list (SH_FIFO * fifo)
170{
171 size_t len;
172 struct dlist * getit;
173 char * retval;
174
175 SL_ENTER(_("pop_list"));
176
177 if (fifo == NULL || fifo->tail_ptr == NULL)
178 {
179 SL_RETURN (NULL, _("pop_list"));
180 }
181
182 getit = fifo->tail_ptr;
183
184 if (getit->prev == NULL) /* last element */
185 {
186 fifo->tail_ptr = NULL;
187 fifo->head_ptr = NULL;
188 }
189 else
190 {
191 fifo->tail_ptr = getit->prev;
192 fifo->tail_ptr->next = getit->next;
193 }
194
195 len = sl_strlen(getit->data);
196 retval = SH_ALLOC(len+1);
197 sl_strlcpy (retval, getit->data, len+1);
198
199 memset(getit->data, 0, len);
200
201 if (NULL != sl_strstr (retval, _("LOGKEY")))
202 {
203 MUNLOCK(getit->data, (len+1));
204 ;
205 }
206
207 SH_FREE(getit->data);
208 SH_FREE(getit);
209
210 --(fifo->fifo_cts);
211
212 SL_RETURN (retval, _("pop_list"));
213}
214
215
216
217
Note: See TracBrowser for help on using the repository browser.