1
/* Definition of the batch_t data structure.
2
* I am not sure yet if this will become a full-blown object. For now, this header just
3
* includes the object definition and is not accompanied by code.
5
* Copyright 2009 by Rainer Gerhards and Adiscon GmbH.
7
* This file is part of the rsyslog runtime library.
9
* The rsyslog runtime library is free software: you can redistribute it and/or modify
10
* it under the terms of the GNU Lesser General Public License as published by
11
* the Free Software Foundation, either version 3 of the License, or
12
* (at your option) any later version.
14
* The rsyslog runtime library is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU Lesser General Public License for more details.
19
* You should have received a copy of the GNU Lesser General Public License
20
* along with the rsyslog runtime library. If not, see <http://www.gnu.org/licenses/>.
22
* A copy of the GPL can be found in the file "COPYING" in this distribution.
23
* A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution.
26
#ifndef BATCH_H_INCLUDED
27
#define BATCH_H_INCLUDED
32
/* enum for batch states. Actually, we violate a layer here, in that we assume that a batch is used
33
* for action processing. So far, this seems acceptable, the status is simply ignored inside the
34
* main message queue. But over time, it could potentially be useful to split the two.
38
BATCH_STATE_RDY = 0, /* object ready for processing */
39
BATCH_STATE_BAD = 1, /* unrecoverable failure while processing, do NOT resubmit to same action */
40
BATCH_STATE_SUB = 2, /* message submitted for processing, outcome yet unknown */
41
BATCH_STATE_COMM = 3, /* message successfully commited */
42
BATCH_STATE_DISC = 4, /* discarded - processed OK, but do not submit to any other action */
46
/* an object inside a batch, including any information (state!) needed for it to "life".
49
obj_t *pUsrp; /* pointer to user object (most often message) */
50
batch_state_t state; /* associated state */
51
/* work variables for action processing; these are reused for each action (or block of
54
sbool bFilterOK; /* work area for filter processing (per action, reused!) */
55
sbool bPrevWasSuspended;
56
/* following are caches to save allocs if not absolutely necessary */
57
uchar *staticActStrings[CONF_OMOD_NUMSTRINGS_MAXSIZE]; /**< for strings */
58
/* a cache to save malloc(), if not absolutely necessary */
59
void *staticActParams[CONF_OMOD_NUMSTRINGS_MAXSIZE]; /**< for anything else */
60
size_t staticLenStrings[CONF_OMOD_NUMSTRINGS_MAXSIZE];
61
/* and the same for the message length (if used) */
62
/* end action work variables */
66
* This object is used to dequeue multiple user pointers which are than handed over
67
* to processing. The size of elements is fixed after queue creation, but may be
68
* modified by config variables (better said: queue properties).
69
* Note that a "user pointer" in rsyslog context so far always is a message
70
* object. We stick to the more generic term because queues may potentially hold
71
* other types of objects, too.
72
* rgerhards, 2009-05-12
73
* Note that nElem is not necessarily equal to nElemDeq. This is the case when we
74
* discard some elements (because of configuration) during dequeue processing. As
75
* all Elements are only deleted when the batch is processed, we can not immediately
76
* delete them. So we need to keep their number that we can delete them when the batch
77
* is completed (else, the whole process does not work correctly).
80
int maxElem; /* maximum number of elements that this batch supports */
81
int nElem; /* actual number of element in this entry */
82
int nElemDeq; /* actual number of elements dequeued (and thus to be deleted) - see comment above! */
83
int iDoneUpTo; /* all messages below this index have state other than RDY */
84
qDeqID deqID; /* ID of dequeue operation that generated this batch */
85
int *pbShutdownImmediate;/* end processing of this batch immediately if set to 1 */
86
sbool bSingleRuleset; /* do all msgs of this batch use a single ruleset? */
87
batch_obj_t *pElem; /* batch elements */
91
/* some inline functions (we may move this off to an object .. or not) */
93
batchSetSingleRuleset(batch_t *pBatch, sbool val) {
94
pBatch->bSingleRuleset = val;
97
/* get the batches ruleset (if we have a single ruleset) */
98
static inline ruleset_t*
99
batchGetRuleset(batch_t *pBatch) {
100
return (pBatch->nElem > 0) ? ((msg_t*) pBatch->pElem[0].pUsrp)->pRuleset : NULL;
103
/* get the ruleset of a specifc element of the batch (index not verified!) */
104
static inline ruleset_t*
105
batchElemGetRuleset(batch_t *pBatch, int i) {
106
return ((msg_t*) pBatch->pElem[i].pUsrp)->pRuleset;
109
/* get number of msgs for this batch */
111
batchNumMsgs(batch_t *pBatch) {
112
return pBatch->nElem;
116
/* set the status of the i-th batch element. Note that once the status is
117
* DISC, it will never be reset. So this function can NOT be used to initialize
118
* the state table. -- rgerhards, 2010-06-10
121
batchSetElemState(batch_t *pBatch, int i, batch_state_t newState) {
122
if(pBatch->pElem[i].state != BATCH_STATE_DISC)
123
pBatch->pElem[i].state = newState;
127
/* check if an element is a valid entry. We do NOT verify if the
128
* element index is valid. -- rgerhards, 2010-06-10
131
batchIsValidElem(batch_t *pBatch, int i) {
132
return(pBatch->pElem[i].bFilterOK && pBatch->pElem[i].state != BATCH_STATE_DISC);
136
/* copy one batch element to another.
137
* This creates a complete duplicate in those cases where
138
* it is needed. Use duplication only when absolutely necessary!
139
* Note that all working fields are reset to zeros. If that were
140
* not done, we would have potential problems with invalid
141
* or double pointer frees.
142
* rgerhards, 2010-06-10
145
batchCopyElem(batch_obj_t *pDest, batch_obj_t *pSrc) {
146
memset(pDest, 0, sizeof(batch_obj_t));
147
pDest->pUsrp = pSrc->pUsrp;
148
pDest->state = pSrc->state;
152
/* free members of a batch "object". Note that we can not do the usual
153
* destruction as the object typically is allocated on the stack and so the
154
* object itself cannot be freed! -- rgerhards, 2010-06-15
157
batchFree(batch_t *pBatch) {
160
for(i = 0 ; i < pBatch->maxElem ; ++i) {
161
for(j = 0 ; j < CONF_OMOD_NUMSTRINGS_MAXSIZE ; ++j) {
162
/* staticActParams MUST be freed immediately (if required),
163
* so we do not need to do that!
165
free(pBatch->pElem[i].staticActStrings[j]);
172
/* initialiaze a batch "object". The record must already exist,
173
* we "just" initialize it. The max number of elements must be
174
* provided. -- rgerhards, 2010-06-15
176
static inline rsRetVal
177
batchInit(batch_t *pBatch, int maxElem) {
179
pBatch->iDoneUpTo = 0;
180
pBatch->maxElem = maxElem;
181
CHKmalloc(pBatch->pElem = calloc((size_t)maxElem, sizeof(batch_obj_t)));
182
// TODO: replace calloc by inidividual writes?
188
/* primarily a helper for debug purposes, get human-readble name of state */
190
batchState2String(batch_state_t state) {
192
case BATCH_STATE_RDY:
193
return "BATCH_STATE_RDY";
194
case BATCH_STATE_BAD:
195
return "BATCH_STATE_BAD";
196
case BATCH_STATE_SUB:
197
return "BATCH_STATE_SUB";
198
case BATCH_STATE_COMM:
199
return "BATCH_STATE_COMM";
200
case BATCH_STATE_DISC:
201
return "BATCH_STATE_DISC";
203
return "ERROR, batch state not known!";
205
#endif /* #ifndef BATCH_H_INCLUDED */