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

« back to all changes in this revision

Viewing changes to iminternal.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2007-10-19 17:21:49 UTC
  • Revision ID: james.westby@ubuntu.com-20071019172149-ie6ej2xve33mxiu7
Tags: upstream-1.19.10
ImportĀ upstreamĀ versionĀ 1.19.10

Show diffs side-by-side

added added

removed removed

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