| 1 |
/* wti.c |
| 2 |
* |
| 3 |
* This file implements the worker thread instance (wti) class. |
| 4 |
* |
| 5 |
* File begun on 2008-01-20 by RGerhards based on functions from the |
| 6 |
* previous queue object class (the wti functions have been extracted) |
| 7 |
* |
| 8 |
* There is some in-depth documentation available in doc/dev_queue.html |
| 9 |
* (and in the web doc set on https://www.rsyslog.com/doc/). Be sure to read it |
| 10 |
* if you are getting acquainted to the object. |
| 11 |
* |
| 12 |
* Copyright 2008-2026 Adiscon GmbH. |
| 13 |
* |
| 14 |
* This file is part of the rsyslog runtime library. |
| 15 |
* |
| 16 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 17 |
* you may not use this file except in compliance with the License. |
| 18 |
* You may obtain a copy of the License at |
| 19 |
* |
| 20 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 21 |
* -or- |
| 22 |
* see COPYING.ASL20 in the source distribution |
| 23 |
* |
| 24 |
* Unless required by applicable law or agreed to in writing, software |
| 25 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 26 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 27 |
* See the License for the specific language governing permissions and |
| 28 |
* limitations under the License. |
| 29 |
*/ |
| 30 |
#include "config.h" |
| 31 |
|
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <assert.h> |
| 36 |
#include <signal.h> |
| 37 |
#include <pthread.h> |
| 38 |
#include <errno.h> |
| 39 |
#include <stdint.h> |
| 40 |
|
| 41 |
#include "rsyslog.h" |
| 42 |
#include "stringbuf.h" |
| 43 |
#include "srUtils.h" |
| 44 |
#include "errmsg.h" |
| 45 |
#include "wtp.h" |
| 46 |
#include "wti.h" |
| 47 |
#include "obj.h" |
| 48 |
#include "glbl.h" |
| 49 |
#include "action.h" |
| 50 |
#include "atomic.h" |
| 51 |
#include "rsconf.h" |
| 52 |
|
| 53 |
/* static data */ |
| 54 |
DEFobjStaticHelpers; |
| 55 |
DEFobjCurrIf(glbl) |
| 56 |
|
| 57 |
pthread_key_t thrd_wti_key; |
| 58 |
|
| 59 |
|
| 60 |
/* methods */ |
| 61 |
|
| 62 |
/* get the header for debug messages |
| 63 |
* The caller must NOT free or otherwise modify the returned string! |
| 64 |
*/ |
| 65 |
uchar *ATTR_NONNULL() wtiGetDbgHdr(const wti_t *const pThis) { |
| 66 |
ISOBJ_TYPE_assert(pThis, wti); |
| 67 |
|
| 68 |
if (pThis->pszDbgHdr == NULL) |
| 69 |
return (uchar *)"wti"; /* should not normally happen */ |
| 70 |
else |
| 71 |
return pThis->pszDbgHdr; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
/* return the current worker processing state. For the sake of |
| 76 |
* simplicity, we do not use the iRet interface. -- rgerhards, 2009-07-17 |
| 77 |
*/ |
| 78 |
int ATTR_NONNULL() wtiGetState(wti_t *pThis) { |
| 79 |
return ATOMIC_FETCH_32BIT(&pThis->bIsRunning, &pThis->mutIsRunning); |
| 80 |
} |
| 81 |
|
| 82 |
/* join terminated worker thread |
| 83 |
* This may be called in any thread state, it will be a NOP if the |
| 84 |
* thread is not to join. |
| 85 |
*/ |
| 86 |
void ATTR_NONNULL() wtiJoinThrd(wti_t *const pThis) { |
| 87 |
int r; |
| 88 |
ISOBJ_TYPE_assert(pThis, wti); |
| 89 |
if (wtiGetState(pThis) == WRKTHRD_WAIT_JOIN) { |
| 90 |
DBGPRINTF("%s: joining terminated worker\n", wtiGetDbgHdr(pThis)); |
| 91 |
if ((r = pthread_join(pThis->thrdID, NULL)) != 0) { |
| 92 |
LogMsg(r, RS_RET_INTERNAL_ERROR, LOG_WARNING, "rsyslog bug? wti cannot join terminated wrkr"); |
| 93 |
return; |
| 94 |
} |
| 95 |
DBGPRINTF("%s: worker fully terminated\n", wtiGetDbgHdr(pThis)); |
| 96 |
wtiSetState(pThis, WRKTHRD_STOPPED); |
| 97 |
if (dbgTimeoutToStderr) { |
| 98 |
fprintf(stderr, "rsyslog debug: %s: thread joined\n", wtiGetDbgHdr(pThis)); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/* Set this thread to "always running" state (can not be unset) |
| 104 |
* rgerhards, 2009-07-20 |
| 105 |
*/ |
| 106 |
rsRetVal ATTR_NONNULL() wtiSetAlwaysRunning(wti_t *pThis) { |
| 107 |
ISOBJ_TYPE_assert(pThis, wti); |
| 108 |
pThis->bAlwaysRunning = RSTRUE; |
| 109 |
return RS_RET_OK; |
| 110 |
} |
| 111 |
|
| 112 |
/* Set status (thread is running or not), actually an property of |
| 113 |
* use for wtp, but we need to have it per thread instance (thus it |
| 114 |
* is inside wti). -- rgerhards, 2009-07-17 |
| 115 |
*/ |
| 116 |
rsRetVal ATTR_NONNULL() wtiSetState(wti_t *pThis, const int newVal) { |
| 117 |
ISOBJ_TYPE_assert(pThis, wti); |
| 118 |
if (newVal == WRKTHRD_STOPPED) { |
| 119 |
ATOMIC_STORE_0_TO_INT(&pThis->bIsRunning, &pThis->mutIsRunning); |
| 120 |
} else { |
| 121 |
ATOMIC_OR_INT_TO_INT(&pThis->bIsRunning, &pThis->mutIsRunning, newVal); |
| 122 |
} |
| 123 |
return RS_RET_OK; |
| 124 |
} |
| 125 |
|
| 126 |
/* Compare and Set state (atomic). |
| 127 |
*/ |
| 128 |
rsRetVal ATTR_NONNULL() wtiCASState(wti_t *pThis, const int oldVal, const int newVal) { |
| 129 |
ISOBJ_TYPE_assert(pThis, wti); |
| 130 |
if (ATOMIC_CAS(&pThis->bIsRunning, oldVal, newVal, &pThis->mutIsRunning)) { |
| 131 |
return RS_RET_OK; |
| 132 |
} else { |
| 133 |
return RS_RET_FALSE; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
/* advise all workers to start by interrupting them. That should unblock all srSleep() |
| 139 |
* calls. |
| 140 |
*/ |
| 141 |
rsRetVal wtiWakeupThrd(wti_t *pThis) { |
| 142 |
DEFiRet; |
| 143 |
|
| 144 |
ISOBJ_TYPE_assert(pThis, wti); |
| 145 |
|
| 146 |
if (wtiGetState(pThis)) { |
| 147 |
/* we first try the cooperative "cancel" interface */ |
| 148 |
pthread_kill(pThis->thrdID, SIGTTIN); |
| 149 |
DBGPRINTF("sent SIGTTIN to worker thread %p\n", (void *)pThis->thrdID); |
| 150 |
} |
| 151 |
|
| 152 |
RETiRet; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
/* Cancel the thread. If the thread is not running. But it is save and legal to |
| 157 |
* call wtiCancelThrd() in such situations. This function only returns when the |
| 158 |
* thread has terminated. Else we may get race conditions all over the code... |
| 159 |
* Note that when waiting for the thread to terminate, we do a busy wait, checking |
| 160 |
* progress every 10ms. It is very unlikely that we will ever cancel a thread |
| 161 |
* and, if so, it will only happen at the end of the rsyslog run. So doing this |
| 162 |
* kind of non-optimal wait is considered preferable over using condition variables. |
| 163 |
* rgerhards, 2008-02-26 |
| 164 |
*/ |
| 165 |
rsRetVal ATTR_NONNULL() wtiCancelThrd(wti_t *pThis, const uchar *const cancelobj) { |
| 166 |
DEFiRet; |
| 167 |
int state; |
| 168 |
wtp_t *pWtp; |
| 169 |
pthread_t thrdID; |
| 170 |
|
| 171 |
ISOBJ_TYPE_assert(pThis, wti); |
| 172 |
pWtp = pThis->pWtp; |
| 173 |
ISOBJ_TYPE_assert(pWtp, wtp); |
| 174 |
|
| 175 |
wtiJoinThrd(pThis); |
| 176 |
|
| 177 |
/* Lock wtp mutex to safely check state and access thrdID atomically. |
| 178 |
* This prevents race where thread terminates and thrdID becomes invalid |
| 179 |
* between state check and pthread_kill/pthread_cancel calls. |
| 180 |
*/ |
| 181 |
d_pthread_mutex_lock(&pWtp->mutWtp); |
| 182 |
state = wtiGetState(pThis); |
| 183 |
if (state == WRKTHRD_RUNNING || state == WRKTHRD_INITIALIZING) { |
| 184 |
thrdID = pThis->thrdID; /* Safe copy under mutex */ |
| 185 |
d_pthread_mutex_unlock(&pWtp->mutWtp); |
| 186 |
|
| 187 |
LogMsg(0, RS_RET_ERR, LOG_WARNING, |
| 188 |
"%s: need to do cooperative cancellation " |
| 189 |
"- some data may be lost, increase timeout?", |
| 190 |
cancelobj); |
| 191 |
pthread_kill(thrdID, SIGTTIN); |
| 192 |
DBGPRINTF("sent SIGTTIN to worker thread %p, giving it a chance to terminate\n", (void *)thrdID); |
| 193 |
srSleep(0, 50000); |
| 194 |
wtiJoinThrd(pThis); |
| 195 |
} else { |
| 196 |
d_pthread_mutex_unlock(&pWtp->mutWtp); |
| 197 |
if (state == WRKTHRD_STOPPED) { |
| 198 |
FINALIZE; /* Already stopped, nothing to do */ |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/* Second attempt with hard cancellation if needed */ |
| 203 |
d_pthread_mutex_lock(&pWtp->mutWtp); |
| 204 |
state = wtiGetState(pThis); |
| 205 |
if (state == WRKTHRD_RUNNING || state == WRKTHRD_INITIALIZING) { |
| 206 |
thrdID = pThis->thrdID; /* Safe copy under mutex */ |
| 207 |
d_pthread_mutex_unlock(&pWtp->mutWtp); |
| 208 |
|
| 209 |
LogMsg(0, RS_RET_ERR, LOG_WARNING, "%s: need to do hard cancellation", cancelobj); |
| 210 |
if (dbgTimeoutToStderr) { |
| 211 |
fprintf(stderr, "rsyslog debug: %s: need to do hard cancellation\n", cancelobj); |
| 212 |
} |
| 213 |
pthread_cancel(thrdID); |
| 214 |
pthread_kill(thrdID, SIGTTIN); |
| 215 |
DBGPRINTF("cooperative worker termination failed, using cancellation...\n"); |
| 216 |
DBGOPRINT((obj_t *)pThis, "canceling worker thread\n"); |
| 217 |
|
| 218 |
/* Wait for thread to terminate */ |
| 219 |
while ((state = wtiGetState(pThis)) != WRKTHRD_STOPPED && state != WRKTHRD_WAIT_JOIN) { |
| 220 |
DBGOPRINT((obj_t *)pThis, "waiting on termination, state %d\n", state); |
| 221 |
srSleep(0, 10000); |
| 222 |
} |
| 223 |
} else { |
| 224 |
d_pthread_mutex_unlock(&pWtp->mutWtp); |
| 225 |
} |
| 226 |
|
| 227 |
wtiJoinThrd(pThis); |
| 228 |
|
| 229 |
finalize_it: |
| 230 |
RETiRet; |
| 231 |
} |
| 232 |
|
| 233 |
/* note: this function is only called once in action.c */ |
| 234 |
rsRetVal wtiNewIParam(wti_t *const pWti, action_t *const pAction, actWrkrIParams_t **piparams) { |
| 235 |
actWrkrInfo_t *const wrkrInfo = &(pWti->actWrkrInfo[pAction->iActionNbr]); |
| 236 |
actWrkrIParams_t *iparams; |
| 237 |
size_t allocCount; |
| 238 |
size_t newMax; |
| 239 |
size_t startOffset; |
| 240 |
size_t zeroCount; |
| 241 |
DEFiRet; |
| 242 |
|
| 243 |
if (wrkrInfo->p.tx.currIParam == wrkrInfo->p.tx.maxIParams) { |
| 244 |
/* we need to extend */ |
| 245 |
newMax = (wrkrInfo->p.tx.maxIParams == 0) ? CONF_IPARAMS_BUFSIZE : 2u * (size_t)wrkrInfo->p.tx.maxIParams; |
| 246 |
if ((size_t)pAction->iNumTpls > SIZE_MAX / newMax) { |
| 247 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 248 |
} |
| 249 |
allocCount = (size_t)pAction->iNumTpls * newMax; |
| 250 |
if (allocCount > SIZE_MAX / sizeof(actWrkrIParams_t)) { |
| 251 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 252 |
} |
| 253 |
CHKmalloc(iparams = realloc(wrkrInfo->p.tx.iparams, sizeof(actWrkrIParams_t) * allocCount)); |
| 254 |
startOffset = (size_t)wrkrInfo->p.tx.currIParam * (size_t)pAction->iNumTpls; |
| 255 |
zeroCount = ((size_t)newMax - (size_t)wrkrInfo->p.tx.maxIParams) * (size_t)pAction->iNumTpls; |
| 256 |
memset(iparams + startOffset, 0, sizeof(actWrkrIParams_t) * zeroCount); |
| 257 |
wrkrInfo->p.tx.iparams = iparams; |
| 258 |
wrkrInfo->p.tx.maxIParams = (int)newMax; |
| 259 |
} |
| 260 |
*piparams = wrkrInfo->p.tx.iparams + wrkrInfo->p.tx.currIParam * pAction->iNumTpls; |
| 261 |
++wrkrInfo->p.tx.currIParam; |
| 262 |
|
| 263 |
finalize_it: |
| 264 |
RETiRet; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
/* Destructor */ |
| 269 |
BEGINobjDestruct(wti) /* be sure to specify the object type also in END and CODESTART macros! */ |
| 270 |
CODESTARTobjDestruct(wti); |
| 271 |
if (wtiGetState(pThis) != WRKTHRD_STOPPED) { |
| 272 |
DBGPRINTF("%s: rsyslog bug: worker not stopped during shutdown\n", wtiGetDbgHdr(pThis)); |
| 273 |
if (dbgTimeoutToStderr) { |
| 274 |
fprintf(stderr, "RSYSLOG BUG: %s: worker not stopped during shutdown\n", wtiGetDbgHdr(pThis)); |
| 275 |
} else { |
| 276 |
assert(wtiGetState(pThis) == WRKTHRD_STOPPED); |
| 277 |
} |
| 278 |
} |
| 279 |
/* actual destruction */ |
| 280 |
batchFree(&pThis->batch); |
| 281 |
free(pThis->actWrkrInfo); |
| 282 |
pthread_cond_destroy(&pThis->pcondBusy); |
| 283 |
DESTROY_ATOMIC_HELPER_MUT(pThis->mutIsRunning); |
| 284 |
free(pThis->pszDbgHdr); |
| 285 |
ENDobjDestruct(wti) |
| 286 |
|
| 287 |
|
| 288 |
/* Standard-Constructor for the wti object |
| 289 |
*/ |
| 290 |
BEGINobjConstruct(wti) /* be sure to specify the object type also in END macro! */ |
| 291 |
INIT_ATOMIC_HELPER_MUT(pThis->mutIsRunning); |
| 292 |
pthread_cond_init(&pThis->pcondBusy, NULL); |
| 293 |
ENDobjConstruct(wti) |
| 294 |
|
| 295 |
|
| 296 |
/* Construction finalizer |
| 297 |
* rgerhards, 2008-01-17 |
| 298 |
*/ |
| 299 |
rsRetVal wtiConstructFinalize(wti_t *pThis) { |
| 300 |
DEFiRet; |
| 301 |
int iDeqBatchSize; |
| 302 |
|
| 303 |
ISOBJ_TYPE_assert(pThis, wti); |
| 304 |
|
| 305 |
DBGPRINTF("%s: finalizing construction of worker instance data (for %d actions)\n", wtiGetDbgHdr(pThis), |
| 306 |
runConf->actions.iActionNbr); |
| 307 |
|
| 308 |
/* initialize our thread instance descriptor (no concurrency here) */ |
| 309 |
pThis->bIsRunning = WRKTHRD_STOPPED; |
| 310 |
|
| 311 |
/* must use calloc as we need zero-init */ |
| 312 |
CHKmalloc(pThis->actWrkrInfo = calloc(runConf->actions.iActionNbr, sizeof(actWrkrInfo_t))); |
| 313 |
|
| 314 |
if (pThis->pWtp == NULL) { |
| 315 |
dbgprintf("wtiConstructFinalize: pWtp not set, this may be intentional\n"); |
| 316 |
FINALIZE; |
| 317 |
} |
| 318 |
|
| 319 |
/* we now alloc the array for user pointers. We obtain the max from the queue itself. */ |
| 320 |
CHKiRet(pThis->pWtp->pfGetDeqBatchSize(pThis->pWtp->pUsr, &iDeqBatchSize)); |
| 321 |
CHKiRet(batchInit(&pThis->batch, iDeqBatchSize)); |
| 322 |
|
| 323 |
finalize_it: |
| 324 |
RETiRet; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
/* cancellation cleanup handler for queueWorker () |
| 329 |
* Most importantly, it must bring back the batch into a consistent state. |
| 330 |
* Keep in mind that cancellation is disabled if we run into |
| 331 |
* the cancel cleanup handler (and have been cancelled). |
| 332 |
* rgerhards, 2008-01-16 |
| 333 |
*/ |
| 334 |
static void wtiWorkerCancelCleanup(void *arg) { |
| 335 |
wti_t *pThis = (wti_t *)arg; |
| 336 |
wtp_t *pWtp; |
| 337 |
|
| 338 |
ISOBJ_TYPE_assert(pThis, wti); |
| 339 |
pWtp = pThis->pWtp; |
| 340 |
ISOBJ_TYPE_assert(pWtp, wtp); |
| 341 |
|
| 342 |
DBGPRINTF("%s: cancellation cleanup handler called.\n", wtiGetDbgHdr(pThis)); |
| 343 |
pWtp->pfObjProcessed(pWtp->pUsr, pThis); |
| 344 |
DBGPRINTF("%s: done cancellation cleanup handler.\n", wtiGetDbgHdr(pThis)); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
/* wait for queue to become non-empty or timeout |
| 349 |
* this is introduced as helper to support queue minimum batch sizes, but may |
| 350 |
* also be used for other cases. This function waits until the queue is non-empty |
| 351 |
* or a timeout occurs. The timeout must be passed in as absolute value. |
| 352 |
* @returns 0 if timeout occurs (queue still empty), something else otherwise |
| 353 |
*/ |
| 354 |
int ATTR_NONNULL() wtiWaitNonEmpty(wti_t *const pThis, const struct timespec timeout) { |
| 355 |
wtp_t *__restrict__ const pWtp = pThis->pWtp; |
| 356 |
int r; |
| 357 |
|
| 358 |
DBGOPRINT((obj_t *)pThis, "waiting on queue to become non-empty\n"); |
| 359 |
if (d_pthread_cond_timedwait(&pThis->pcondBusy, pWtp->pmutUsr, &timeout) != 0) { |
| 360 |
r = 0; |
| 361 |
} else { |
| 362 |
r = 1; |
| 363 |
} |
| 364 |
DBGOPRINT((obj_t *)pThis, "waited on queue to become non-empty, result %d\n", r); |
| 365 |
return r; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
/* wait for queue to become non-empty or timeout |
| 370 |
* helper to wtiWorker. Note the the predicate is |
| 371 |
* re-tested by the caller, so it is OK to NOT do it here. |
| 372 |
* rgerhards, 2009-05-20 |
| 373 |
*/ |
| 374 |
static void ATTR_NONNULL() doIdleProcessing(wti_t *const pThis, wtp_t *const pWtp, int *const pbInactivityTOOccurred) { |
| 375 |
struct timespec t; |
| 376 |
|
| 377 |
DBGPRINTF("%s: worker IDLE, waiting for work.\n", wtiGetDbgHdr(pThis)); |
| 378 |
|
| 379 |
if (pThis->bAlwaysRunning) { |
| 380 |
/* never shut down any started worker */ |
| 381 |
d_pthread_cond_wait(&pThis->pcondBusy, pWtp->pmutUsr); |
| 382 |
} else { |
| 383 |
timeoutComp(&t, pWtp->toWrkShutdown); /* get absolute timeout */ |
| 384 |
if (d_pthread_cond_timedwait(&pThis->pcondBusy, pWtp->pmutUsr, &t) != 0) { |
| 385 |
DBGPRINTF("%s: inactivity timeout, worker terminating...\n", wtiGetDbgHdr(pThis)); |
| 386 |
*pbInactivityTOOccurred = 1; /* indicate we had a timeout */ |
| 387 |
} |
| 388 |
} |
| 389 |
DBGOPRINT((obj_t *)pThis, "worker awoke from idle processing\n"); |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
/* generic worker thread framework. Note that we prohibit cancellation |
| 394 |
* during almost all times, because it can have very undesired side effects. |
| 395 |
* However, we may need to cancel a thread if the consumer blocks for too |
| 396 |
* long (during shutdown). So what we do is block cancellation, and every |
| 397 |
* consumer must enable it during the periods where it is safe. |
| 398 |
*/ |
| 399 |
PRAGMA_DIAGNOSTIC_PUSH |
| 400 |
PRAGMA_IGNORE_Wempty_body rsRetVal wtiWorker(wti_t *__restrict__ const pThis) { |
| 401 |
wtp_t *__restrict__ const pWtp = pThis->pWtp; /* our worker thread pool -- shortcut */ |
| 402 |
action_t *__restrict__ pAction; |
| 403 |
rsRetVal localRet; |
| 404 |
rsRetVal terminateRet; |
| 405 |
actWrkrInfo_t *__restrict__ wrkrInfo; |
| 406 |
int iCancelStateSave; |
| 407 |
int i, j, k; |
| 408 |
DEFiRet; |
| 409 |
|
| 410 |
dbgSetThrdName(pThis->pszDbgHdr); |
| 411 |
pthread_cleanup_push(wtiWorkerCancelCleanup, pThis); |
| 412 |
int bInactivityTOOccurred = 0; |
| 413 |
/* Cancellation contract: |
| 414 |
* - default state is DISABLE in worker context |
| 415 |
* - queue consumers enable it only around explicitly cancel-safe work |
| 416 |
* - critical cleanup/state transitions run with cancellation disabled |
| 417 |
*/ |
| 418 |
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &iCancelStateSave); |
| 419 |
DBGPRINTF("wti %p: worker starting\n", pThis); |
| 420 |
/* now we have our identity, on to real processing */ |
| 421 |
|
| 422 |
/* note: in this loop, the mutex is "never" unlocked. Of course, |
| 423 |
* this is not true: it actually is unlocked when the actual processing |
| 424 |
* is done, as part of pWtp->pfDoWork() processing. Note that this |
| 425 |
* function is required to re-lock it when done. We cannot do the |
| 426 |
* lock/unlock here ourselves, as pfDoWork() needs to access queue |
| 427 |
* structures itself. |
| 428 |
* The same goes for pfRateLimiter(). While we could unlock/lock when |
| 429 |
* we call it, in practice the function is often called without any |
| 430 |
* ratelimiting actually done. Only the rate limiter itself knows |
| 431 |
* that. As such, it needs to bear the burden of doing the locking |
| 432 |
* when required. -- rgerhards, 2013-11-20 |
| 433 |
*/ |
| 434 |
d_pthread_mutex_lock(pWtp->pmutUsr); |
| 435 |
while (1) { /* loop will be broken below */ |
| 436 |
if (pWtp->pfRateLimiter != NULL) { /* call rate-limiter, if defined */ |
| 437 |
pWtp->pfRateLimiter(pWtp->pUsr); |
| 438 |
} |
| 439 |
|
| 440 |
/* first check if we are in shutdown process (but evaluate a bit later) */ |
| 441 |
terminateRet = wtpChkStopWrkr(pWtp, MUTEX_ALREADY_LOCKED); |
| 442 |
if (terminateRet == RS_RET_TERMINATE_NOW) { |
| 443 |
/* we now need to free the old batch */ |
| 444 |
localRet = pWtp->pfObjProcessed(pWtp->pUsr, pThis); |
| 445 |
DBGOPRINT((obj_t *)pThis, |
| 446 |
"terminating worker because of " |
| 447 |
"TERMINATE_NOW mode, del iRet %d\n", |
| 448 |
localRet); |
| 449 |
break; |
| 450 |
} |
| 451 |
|
| 452 |
/* try to execute and process whatever we have */ |
| 453 |
localRet = pWtp->pfDoWork(pWtp->pUsr, pThis); |
| 454 |
|
| 455 |
if (localRet == RS_RET_ERR_QUEUE_EMERGENCY) { |
| 456 |
break; /* end of loop */ |
| 457 |
} else if (localRet == RS_RET_IDLE) { |
| 458 |
if (terminateRet == RS_RET_TERMINATE_WHEN_IDLE || bInactivityTOOccurred) { |
| 459 |
DBGOPRINT((obj_t *)pThis, |
| 460 |
"terminating worker terminateRet=%d, " |
| 461 |
"bInactivityTOOccurred=%d\n", |
| 462 |
terminateRet, bInactivityTOOccurred); |
| 463 |
break; /* end of loop */ |
| 464 |
} |
| 465 |
doIdleProcessing(pThis, pWtp, &bInactivityTOOccurred); |
| 466 |
continue; /* request next iteration */ |
| 467 |
} |
| 468 |
|
| 469 |
bInactivityTOOccurred = 0; /* reset for next run */ |
| 470 |
} |
| 471 |
|
| 472 |
d_pthread_mutex_unlock(pWtp->pmutUsr); |
| 473 |
|
| 474 |
DBGPRINTF("DDDD: wti %p: worker cleanup action instances\n", pThis); |
| 475 |
for (i = 0; i < runConf->actions.iActionNbr; ++i) { |
| 476 |
wrkrInfo = &(pThis->actWrkrInfo[i]); |
| 477 |
dbgprintf("wti %p, action %d, ptr %p\n", pThis, i, wrkrInfo->actWrkrData); |
| 478 |
if (wrkrInfo->actWrkrData != NULL) { |
| 479 |
pAction = wrkrInfo->pAction; |
| 480 |
actionRemoveWorker(pAction, wrkrInfo->actWrkrData); |
| 481 |
pAction->pMod->mod.om.freeWrkrInstance(wrkrInfo->actWrkrData); |
| 482 |
if (pAction->isTransactional) { |
| 483 |
/* free iparam "cache" - we need to go through to max! */ |
| 484 |
for (j = 0; j < wrkrInfo->p.tx.maxIParams; ++j) { |
| 485 |
for (k = 0; k < pAction->iNumTpls; ++k) { |
| 486 |
free(actParam(wrkrInfo->p.tx.iparams, pAction->iNumTpls, j, k).param); |
| 487 |
} |
| 488 |
} |
| 489 |
free(wrkrInfo->p.tx.iparams); |
| 490 |
wrkrInfo->p.tx.iparams = NULL; |
| 491 |
wrkrInfo->p.tx.currIParam = 0; |
| 492 |
wrkrInfo->p.tx.maxIParams = 0; |
| 493 |
} else { |
| 494 |
releaseDoActionParams(pAction, pThis, 1); |
| 495 |
} |
| 496 |
wrkrInfo->actWrkrData = NULL; /* re-init for next activation */ |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
/* indicate termination */ |
| 501 |
pthread_cleanup_pop(0); /* remove cleanup handler */ |
| 502 |
pthread_setcancelstate(iCancelStateSave, NULL); |
| 503 |
dbgprintf("wti %p: exiting\n", pThis); |
| 504 |
|
| 505 |
RETiRet; |
| 506 |
} |
| 507 |
PRAGMA_DIAGNOSTIC_POP |
| 508 |
|
| 509 |
|
| 510 |
/* some simple object access methods */ |
| 511 |
rsRetVal wtiSetpWtp(wti_t *pThis, wtp_t *pVal) { |
| 512 |
pThis->pWtp = pVal; |
| 513 |
return RS_RET_OK; |
| 514 |
} |
| 515 |
|
| 516 |
/* set the debug header message |
| 517 |
* The passed-in string is duplicated. So if the caller does not need |
| 518 |
* it any longer, it must free it. Must be called only before object is finalized. |
| 519 |
* rgerhards, 2008-01-09 |
| 520 |
*/ |
| 521 |
rsRetVal wtiSetDbgHdr(wti_t *pThis, uchar *pszMsg, const size_t lenMsg) { |
| 522 |
DEFiRet; |
| 523 |
|
| 524 |
ISOBJ_TYPE_assert(pThis, wti); |
| 525 |
assert(pszMsg != NULL); |
| 526 |
|
| 527 |
if (lenMsg < 1) ABORT_FINALIZE(RS_RET_PARAM_ERROR); |
| 528 |
|
| 529 |
if (pThis->pszDbgHdr != NULL) { |
| 530 |
free(pThis->pszDbgHdr); |
| 531 |
} |
| 532 |
|
| 533 |
if ((pThis->pszDbgHdr = malloc(lenMsg + 1)) == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 534 |
|
| 535 |
memcpy(pThis->pszDbgHdr, pszMsg, lenMsg + 1); /* always think about the \0! */ |
| 536 |
|
| 537 |
finalize_it: |
| 538 |
RETiRet; |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
/* This function returns (and creates if necessary) a dummy wti suitable |
| 543 |
* for use by the rule engine. It is intended to be used for direct-mode |
| 544 |
* main queues (folks, don't do that!). Once created, data is stored in |
| 545 |
* thread-specific storage. |
| 546 |
* Note: we do NOT do error checking -- if this functions fails, all the |
| 547 |
* rest will fail as well... (also, it will only fail under OOM, so...). |
| 548 |
* Memleak: we leak pWti's when run in direct mode. However, this is only |
| 549 |
* a cosmetic leak, as we need them until all inputs are terminated, |
| 550 |
* what means essentially until rsyslog itself is terminated. So we |
| 551 |
* don't care -- it's just not nice in valgrind, but that's it. |
| 552 |
*/ |
| 553 |
wti_t *wtiGetDummy(void) { |
| 554 |
wti_t *pWti; |
| 555 |
|
| 556 |
pWti = (wti_t *)pthread_getspecific(thrd_wti_key); |
| 557 |
if (pWti == NULL) { |
| 558 |
wtiConstruct(&pWti); |
| 559 |
if (pWti != NULL) wtiConstructFinalize(pWti); |
| 560 |
if (pthread_setspecific(thrd_wti_key, pWti) != 0) { |
| 561 |
DBGPRINTF("wtiGetDummy: error setspecific thrd_wti_key\n"); |
| 562 |
} |
| 563 |
} |
| 564 |
return pWti; |
| 565 |
} |
| 566 |
|
| 567 |
/* dummy */ |
| 568 |
static rsRetVal wtiQueryInterface(interface_t __attribute__((unused)) * i) { |
| 569 |
return RS_RET_NOT_IMPLEMENTED; |
| 570 |
} |
| 571 |
|
| 572 |
/* exit our class |
| 573 |
*/ |
| 574 |
BEGINObjClassExit(wti, OBJ_IS_CORE_MODULE) /* CHANGE class also in END MACRO! */ |
| 575 |
CODESTARTObjClassExit(wti); |
| 576 |
/* release objects we no longer need */ |
| 577 |
objRelease(glbl, CORE_COMPONENT); |
| 578 |
pthread_key_delete(thrd_wti_key); |
| 579 |
ENDObjClassExit(wti) |
| 580 |
|
| 581 |
|
| 582 |
/* Initialize the wti class. Must be called as the very first method |
| 583 |
* before anything else is called inside this class. |
| 584 |
* rgerhards, 2008-01-09 |
| 585 |
*/ |
| 586 |
BEGINObjClassInit(wti, 1, OBJ_IS_CORE_MODULE) /* one is the object version (most important for persisting) */ |
| 587 |
int r; |
| 588 |
/* request objects we use */ |
| 589 |
CHKiRet(objUse(glbl, CORE_COMPONENT)); |
| 590 |
r = pthread_key_create(&thrd_wti_key, NULL); |
| 591 |
if (r != 0) { |
| 592 |
dbgprintf("wti.c: pthread_key_create failed\n"); |
| 593 |
ABORT_FINALIZE(RS_RET_ERR); |
| 594 |
} |
| 595 |
ENDObjClassInit(wti) |