~ubuntu-branches/ubuntu/lucid/rsyslog/lucid

1 by Michael Biebl
Import upstream version 1.19.10
1
/* iminternal.c
2
 * This file set implements the internal messages input module for rsyslog.
3
 * Note: we currently do not have an input module spec, but
4
 * we will have one in the future. This module needs then to be
5
 * adapted.
6
 * 
7
 * File begun on 2007-08-03 by RGerhards
8
 *
9
 * Copyright 2007 Rainer Gerhards and Adiscon GmbH.
10
 *
1.1.2 by Michael Biebl
Import upstream version 3.14.2
11
 * This file is part of rsyslog.
12
 *
13
 * Rsyslog is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation, either version 3 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * Rsyslog is distributed in the hope that it will be useful,
1 by Michael Biebl
Import upstream version 1.19.10
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
1.1.2 by Michael Biebl
Import upstream version 3.14.2
24
 * along with Rsyslog.  If not, see <http://www.gnu.org/licenses/>.
1 by Michael Biebl
Import upstream version 1.19.10
25
 *
26
 * A copy of the GPL can be found in the file "COPYING" in this distribution.
27
 */
28
#include "config.h"
29
#include "rsyslog.h"
30
31
#include <stdio.h>
32
#include <stdlib.h>
33
#include <string.h>
34
#include <assert.h>
35
36
#include "syslogd.h"
37
#include "linkedlist.h"
38
#include "iminternal.h"
39
40
static linkedList_t llMsgs;
41
42
43
/* destructs an iminternal object
44
 */
45
static rsRetVal iminternalDestruct(iminternal_t *pThis)
46
{
47
	DEFiRet;
48
49
	assert(pThis != NULL);
50
51
	if(pThis->pMsg != NULL)
1.1.2 by Michael Biebl
Import upstream version 3.14.2
52
		msgDestruct(&pThis->pMsg);
1 by Michael Biebl
Import upstream version 1.19.10
53
54
	free(pThis);
55
1.1.2 by Michael Biebl
Import upstream version 3.14.2
56
	RETiRet;
1 by Michael Biebl
Import upstream version 1.19.10
57
}
58
59
60
/* Construct an iminternal object
61
 */
62
static rsRetVal iminternalConstruct(iminternal_t **ppThis)
63
{
64
	DEFiRet;
65
	iminternal_t *pThis;
66
67
	assert(ppThis != NULL);
68
69
	if((pThis = (iminternal_t*) calloc(1, sizeof(iminternal_t))) == NULL) {
70
		ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
71
	}
72
73
finalize_it:
74
	if(iRet != RS_RET_OK) {
75
		if(pThis != NULL)
76
			iminternalDestruct(pThis);
77
	}
78
79
	*ppThis = pThis;
80
1.1.2 by Michael Biebl
Import upstream version 3.14.2
81
	RETiRet;
82
}
1 by Michael Biebl
Import upstream version 1.19.10
83
84
85
/* add a message to the linked list
86
 * Note: the pMsg reference counter is not incremented. Consequently,
87
 * the caller must NOT decrement it. The caller actually hands over
88
 * full ownership of the pMsg object.
89
 * The interface of this function is modelled after syslogd/logmsg(),
90
 * for which it is an "replacement".
91
 */
92
rsRetVal iminternalAddMsg(int pri, msg_t *pMsg, int flags)
93
{
94
	DEFiRet;
95
	iminternal_t *pThis;
96
97
	assert(pMsg != NULL);
98
99
	CHKiRet(iminternalConstruct(&pThis));
100
101
	pThis->pri = pri;
102
	pThis->pMsg = pMsg;
103
	pThis->flags = flags;
104
105
	CHKiRet(llAppend(&llMsgs,  NULL, (void*) pThis));
106
107
finalize_it:
108
	if(iRet != RS_RET_OK) {
109
		dbgprintf("iminternalAddMsg() error %d - can not otherwise report this error, message lost\n", iRet);
110
		if(pThis != NULL)
111
			iminternalDestruct(pThis);
112
	}
113
1.1.2 by Michael Biebl
Import upstream version 3.14.2
114
	RETiRet;
1 by Michael Biebl
Import upstream version 1.19.10
115
}
116
117
118
/* pull the first error message from the linked list, remove it
119
 * from the list and return it to the caller. The caller is
120
 * responsible for freeing the message!
121
 */
122
rsRetVal iminternalRemoveMsg(int *pPri, msg_t **ppMsg, int *pFlags)
123
{
124
	DEFiRet;
125
	iminternal_t *pThis;
126
	linkedListCookie_t llCookie = NULL;
127
128
	assert(pPri != NULL);
129
	assert(ppMsg != NULL);
130
	assert(pFlags != NULL);
131
132
	CHKiRet(llGetNextElt(&llMsgs, &llCookie, (void*)&pThis));
133
	*pPri = pThis->pri;
134
	*pFlags = pThis->flags;
135
	*ppMsg = pThis->pMsg;
136
	pThis->pMsg = NULL; /* we do no longer own it - important for destructor */
137
138
	if(llDestroyRootElt(&llMsgs) != RS_RET_OK) {
139
		dbgprintf("Root element of iminternal linked list could not be destroyed - there is "
140
			"nothing we can do against it, we ignore it for now. Things may go wild "
141
			"from here on. This is most probably a program logic error.\n");
142
	}
143
144
finalize_it:
1.1.2 by Michael Biebl
Import upstream version 3.14.2
145
	RETiRet;
1 by Michael Biebl
Import upstream version 1.19.10
146
}
147
148
/* tell the caller if we have any messages ready for processing.
149
 * 0 means we have none, everything else means there is at least
150
 * one message ready.
151
 */
152
rsRetVal iminternalHaveMsgReady(int* pbHaveOne)
153
{
154
	assert(pbHaveOne != NULL);
155
156
	return llGetNumElts(&llMsgs, pbHaveOne);
157
}
158
159
160
/* initialize the iminternal subsystem
161
 * must be called once at the start of the program
162
 */
163
rsRetVal modInitIminternal(void)
164
{
165
	DEFiRet;
166
167
	iRet = llInit(&llMsgs, iminternalDestruct, NULL, NULL);
168
1.1.2 by Michael Biebl
Import upstream version 3.14.2
169
	RETiRet;
1 by Michael Biebl
Import upstream version 1.19.10
170
}
171
172
173
/* de-initialize the iminternal subsystem
174
 * must be called once at the end of the program
175
 * Note: the error list must have been pulled first. We do
176
 * NOT care if there are any errors left - we simply destroy
177
 * them.
178
 */
179
rsRetVal modExitIminternal(void)
180
{
181
	DEFiRet;
182
183
	iRet = llDestroy(&llMsgs);
184
1.1.2 by Michael Biebl
Import upstream version 3.14.2
185
	RETiRet;
1 by Michael Biebl
Import upstream version 1.19.10
186
}
187
188
/*
189
 * vi:set ai:
190
 */