| 1 |
/* obj.c |
| 2 |
* |
| 3 |
* This file implements a generic object "class". All other classes can |
| 4 |
* use the service of this base class here to include auto-destruction and |
| 5 |
* other capabilities in a generic manner. |
| 6 |
* |
| 7 |
* As of 2008-02-29, I (rgerhards) am adding support for dynamically loadable |
| 8 |
* objects. In essence, each object will soon be available via its interface, |
| 9 |
* only. Before any object's code is accessed (including global static methods), |
| 10 |
* the caller needs to obtain an object interface. To do so, it needs to provide |
| 11 |
* the object name and the file where the object is expected to reside in. A |
| 12 |
* file may not be given, in which case the object is expected to reside in |
| 13 |
* the rsyslog core. The caller than receives an interface pointer which can |
| 14 |
* be utilized to access all the object's methods. This method enables rsyslog |
| 15 |
* to load library modules on demand. In order to keep overhead low, callers |
| 16 |
* should request object interface only once in the object Init function and |
| 17 |
* free them when they exit. The only exception is when a caller needs to |
| 18 |
* access an object only conditional, in which case a pointer to its interface |
| 19 |
* shall be acquired as need first arises but still be released only on exit |
| 20 |
* or when there definitely is no further need. The whole idea is to limit |
| 21 |
* the very performance-intense act of dynamically loading an objects library. |
| 22 |
* Of course, it is possible to violate this suggestion, but than you should |
| 23 |
* have very good reasoning to do so. |
| 24 |
* |
| 25 |
* Please note that there is one trick we need to do. Each object queries |
| 26 |
* the object interfaces and it does so via objUse(). objUse, however, is |
| 27 |
* part of the obj object's interface (implemented via the file you are |
| 28 |
* just reading). So in order to obtain a pointer to objUse, we need to |
| 29 |
* call it - obviously not possible. One solution would be that objUse is |
| 30 |
* hardcoded into all callers. That, however, would bring us into slight |
| 31 |
* trouble with actually dynamically loaded modules, as we should NOT |
| 32 |
* rely on the OS loader to resolve symbols back to the caller (this |
| 33 |
* is a feature not universally available and highly importable). Of course, |
| 34 |
* we can solve this with a pHostQueryEtryPoint() call. It still sounds |
| 35 |
* somewhat unnatural to call a regular interface function via a special |
| 36 |
* method. So what we do instead is define a special function called |
| 37 |
* objGetObjInterface() which delivers our own interface. That function |
| 38 |
* than will be defined global and be queriable via pHostQueryEtryPoint(). |
| 39 |
* I agree, technically this is much the same, but from an architecture |
| 40 |
* point of view it looks cleaner (at least to me). |
| 41 |
* |
| 42 |
* Please note that there is another egg-hen problem: we use a linked list, |
| 43 |
* which is provided by the linkedList object. However, we need to |
| 44 |
* initialize the linked list before we can provide the UseObj() |
| 45 |
* functionality. That, in turn, would probably be required by the |
| 46 |
* linkedList object. So the solution is to use a backdoor just to |
| 47 |
* init the linked list and from then on use the usual interfaces. |
| 48 |
* |
| 49 |
* File begun on 2008-01-04 by RGerhards |
| 50 |
* |
| 51 |
* Copyright 2008-2024 Rainer Gerhards and Adiscon GmbH. |
| 52 |
* |
| 53 |
* This file is part of the rsyslog runtime library. |
| 54 |
* |
| 55 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 56 |
* you may not use this file except in compliance with the License. |
| 57 |
* You may obtain a copy of the License at |
| 58 |
* |
| 59 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 60 |
* -or- |
| 61 |
* see COPYING.ASL20 in the source distribution |
| 62 |
* |
| 63 |
* Unless required by applicable law or agreed to in writing, software |
| 64 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 65 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 66 |
* See the License for the specific language governing permissions and |
| 67 |
* limitations under the License. |
| 68 |
*/ |
| 69 |
#include "config.h" |
| 70 |
|
| 71 |
#include <stdio.h> |
| 72 |
#include <stdlib.h> |
| 73 |
#include <string.h> |
| 74 |
#include <ctype.h> |
| 75 |
#include <assert.h> |
| 76 |
#include <pthread.h> |
| 77 |
|
| 78 |
#define DEV_DEBUG 0 /* set to 1 to enable very verbose developer debugging messages */ |
| 79 |
|
| 80 |
/* how many objects are supported by rsyslogd? */ |
| 81 |
#define OBJ_NUM_IDS 100 |
| 82 |
|
| 83 |
#include "rsyslog.h" |
| 84 |
#include "syslogd-types.h" |
| 85 |
#include "srUtils.h" |
| 86 |
#include "obj.h" |
| 87 |
#include "stream.h" |
| 88 |
#include "modules.h" |
| 89 |
#include "errmsg.h" |
| 90 |
#include "cfsysline.h" |
| 91 |
#include "unicode-helper.h" |
| 92 |
#include "datetime.h" |
| 93 |
#include "atomic.h" |
| 94 |
|
| 95 |
/* static data */ |
| 96 |
DEFobjCurrIf(obj) /* we define our own interface, as this is expected by some macros! */ |
| 97 |
DEFobjCurrIf(var) DEFobjCurrIf(module) |
| 98 |
DEFobjCurrIf(strm) static objInfo_t *arrObjInfo[OBJ_NUM_IDS]; /* array with object information pointers */ |
| 99 |
pthread_mutex_t mutObjGlobalOp; /* mutex to guard global operations of the object system */ |
| 100 |
|
| 101 |
|
| 102 |
/* cookies for serialized lines */ |
| 103 |
#define COOKIE_OBJLINE '<' |
| 104 |
#define COOKIE_PROPLINE '+' |
| 105 |
#define COOKIE_ENDLINE '>' |
| 106 |
#define COOKIE_BLANKLINE '.' |
| 107 |
|
| 108 |
/* forward definitions */ |
| 109 |
static rsRetVal FindObjInfo(const char *szObjName, objInfo_t **ppInfo); |
| 110 |
|
| 111 |
/* methods */ |
| 112 |
|
| 113 |
/* This is a dummy method to be used when a standard method has not been |
| 114 |
* implemented by an object. Having it allows us to simply call via the |
| 115 |
* jump table without any NULL pointer checks - which gains quite |
| 116 |
* some performance. -- rgerhards, 2008-01-04 |
| 117 |
*/ |
| 118 |
static rsRetVal objInfoNotImplementedDummy(void __attribute__((unused)) * pThis) { |
| 119 |
return RS_RET_NOT_IMPLEMENTED; |
| 120 |
} |
| 121 |
|
| 122 |
/* and now the macro to check if something is not implemented |
| 123 |
* must be provided an objInfo_t pointer. |
| 124 |
*/ |
| 125 |
#define objInfoIsImplemented(pThis, method) \ |
| 126 |
(pThis->objMethods[method] != (rsRetVal(*)(void *, ...))objInfoNotImplementedDummy) |
| 127 |
|
| 128 |
/* construct an object Info object. Each class shall do this on init. The |
| 129 |
* resulting object shall be cached during the lifetime of the class and each |
| 130 |
* object shall receive a reference. A constructor and destructor MUST be provided for all |
| 131 |
* objects, thus they are in the parameter list. |
| 132 |
* pszID is the identifying object name and must point to constant pool memory. It is never freed. |
| 133 |
*/ |
| 134 |
static rsRetVal InfoConstruct(objInfo_t **ppThis, |
| 135 |
uchar *pszID, |
| 136 |
int iObjVers, |
| 137 |
rsRetVal (*pConstruct)(void *), |
| 138 |
rsRetVal (*pDestruct)(void *), |
| 139 |
rsRetVal (*pQueryIF)(interface_t *), |
| 140 |
modInfo_t *pModInfo) { |
| 141 |
DEFiRet; |
| 142 |
int i; |
| 143 |
objInfo_t *pThis; |
| 144 |
|
| 145 |
assert(ppThis != NULL); |
| 146 |
|
| 147 |
if ((pThis = calloc(1, sizeof(objInfo_t))) == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 148 |
|
| 149 |
pThis->pszID = pszID; |
| 150 |
pThis->lenID = ustrlen(pszID); |
| 151 |
pThis->pszName = ustrdup(pszID); /* it's OK if we have NULL ptr, GetName() will deal with that! */ |
| 152 |
pThis->iObjVers = iObjVers; |
| 153 |
pThis->QueryIF = pQueryIF; |
| 154 |
pThis->pModInfo = pModInfo; |
| 155 |
|
| 156 |
pThis->objMethods[0] = (rsRetVal(*)(void *, ...))pConstruct; |
| 157 |
pThis->objMethods[1] = (rsRetVal(*)(void *, ...))pDestruct; |
| 158 |
for (i = 2; i < OBJ_NUM_METHODS; ++i) { |
| 159 |
pThis->objMethods[i] = (rsRetVal(*)(void *, ...))objInfoNotImplementedDummy; |
| 160 |
} |
| 161 |
|
| 162 |
*ppThis = pThis; |
| 163 |
|
| 164 |
finalize_it: |
| 165 |
RETiRet; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
/* destruct the objInfo object - must be done only when no more instances exist. |
| 170 |
* rgerhards, 2008-03-10 |
| 171 |
*/ |
| 172 |
static rsRetVal InfoDestruct(objInfo_t **ppThis) { |
| 173 |
DEFiRet; |
| 174 |
objInfo_t *pThis; |
| 175 |
|
| 176 |
assert(ppThis != NULL); |
| 177 |
pThis = *ppThis; |
| 178 |
assert(pThis != NULL); |
| 179 |
|
| 180 |
free(pThis->pszName); |
| 181 |
free(pThis); |
| 182 |
*ppThis = NULL; |
| 183 |
|
| 184 |
RETiRet; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
/* set a method handler */ |
| 189 |
static rsRetVal InfoSetMethod(objInfo_t *pThis, objMethod_t objMethod, rsRetVal (*pHandler)(void *)) { |
| 190 |
pThis->objMethods[objMethod] = (rsRetVal(*)(void *, ...))pHandler; |
| 191 |
return RS_RET_OK; |
| 192 |
} |
| 193 |
|
| 194 |
/* destruct the base object properties. |
| 195 |
* rgerhards, 2008-01-29 |
| 196 |
*/ |
| 197 |
static rsRetVal DestructObjSelf(obj_t *pThis) { |
| 198 |
DEFiRet; |
| 199 |
|
| 200 |
ISOBJ_assert(pThis); |
| 201 |
free(pThis->pszName); |
| 202 |
|
| 203 |
RETiRet; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
/* --------------- object serializiation / deserialization support --------------- */ |
| 208 |
|
| 209 |
|
| 210 |
/* serialize the header of an object |
| 211 |
* pszRecType must be either "Obj" (Object) or "OPB" (Object Property Bag) |
| 212 |
*/ |
| 213 |
static rsRetVal objSerializeHeader(strm_t *pStrm, obj_t *pObj, uchar *pszRecType) { |
| 214 |
DEFiRet; |
| 215 |
|
| 216 |
ISOBJ_TYPE_assert(pStrm, strm); |
| 217 |
ISOBJ_assert(pObj); |
| 218 |
assert(!strcmp((char *)pszRecType, "Obj") || !strcmp((char *)pszRecType, "OPB")); |
| 219 |
|
| 220 |
/* object cookie and serializer version (so far always 1) */ |
| 221 |
CHKiRet(strm.WriteChar(pStrm, COOKIE_OBJLINE)); |
| 222 |
CHKiRet(strm.Write(pStrm, (uchar *)pszRecType, 3)); /* record types are always 3 octets */ |
| 223 |
CHKiRet(strm.WriteChar(pStrm, ':')); |
| 224 |
CHKiRet(strm.WriteChar(pStrm, '1')); |
| 225 |
|
| 226 |
/* object type, version and string length */ |
| 227 |
CHKiRet(strm.WriteChar(pStrm, ':')); |
| 228 |
CHKiRet(strm.Write(pStrm, pObj->pObjInfo->pszID, pObj->pObjInfo->lenID)); |
| 229 |
CHKiRet(strm.WriteChar(pStrm, ':')); |
| 230 |
CHKiRet(strm.WriteLong(pStrm, objGetVersion(pObj))); |
| 231 |
|
| 232 |
/* record trailer */ |
| 233 |
CHKiRet(strm.WriteChar(pStrm, ':')); |
| 234 |
CHKiRet(strm.WriteChar(pStrm, '\n')); |
| 235 |
|
| 236 |
finalize_it: |
| 237 |
RETiRet; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
/* begin serialization of an object |
| 242 |
* rgerhards, 2008-01-06 |
| 243 |
*/ |
| 244 |
static rsRetVal BeginSerialize(strm_t *pStrm, obj_t *pObj) { |
| 245 |
DEFiRet; |
| 246 |
|
| 247 |
ISOBJ_TYPE_assert(pStrm, strm); |
| 248 |
ISOBJ_assert(pObj); |
| 249 |
|
| 250 |
CHKiRet(strm.RecordBegin(pStrm)); |
| 251 |
CHKiRet(objSerializeHeader(pStrm, pObj, (uchar *)"Obj")); |
| 252 |
|
| 253 |
finalize_it: |
| 254 |
RETiRet; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
/* begin serialization of an object's property bag |
| 259 |
* Note: a property bag is used to serialize some of an objects |
| 260 |
* properties, but not necessarily all. A good example is the queue |
| 261 |
* object, which at some stage needs to serialize a number of its |
| 262 |
* properties, but not the queue data itself. From the object point |
| 263 |
* of view, a property bag can not be used to re-instantiate an object. |
| 264 |
* Otherwise, the serialization is exactly the same. |
| 265 |
* rgerhards, 2008-01-11 |
| 266 |
*/ |
| 267 |
static rsRetVal BeginSerializePropBag(strm_t *pStrm, obj_t *pObj) { |
| 268 |
DEFiRet; |
| 269 |
|
| 270 |
ISOBJ_TYPE_assert(pStrm, strm); |
| 271 |
ISOBJ_assert(pObj); |
| 272 |
|
| 273 |
CHKiRet(strm.RecordBegin(pStrm)); |
| 274 |
CHKiRet(objSerializeHeader(pStrm, pObj, (uchar *)"OPB")); |
| 275 |
|
| 276 |
finalize_it: |
| 277 |
RETiRet; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
/* append a property |
| 282 |
*/ |
| 283 |
static rsRetVal SerializeProp(strm_t *pStrm, uchar *pszPropName, propType_t propType, void *pUsr) { |
| 284 |
DEFiRet; |
| 285 |
uchar *pszBuf = NULL; |
| 286 |
size_t lenBuf = 0; |
| 287 |
uchar szBuf[64]; |
| 288 |
varType_t vType = VARTYPE_NONE; |
| 289 |
|
| 290 |
ISOBJ_TYPE_assert(pStrm, strm); |
| 291 |
assert(pszPropName != NULL); |
| 292 |
|
| 293 |
/*dbgprintf("objSerializeProp: strm %p, propName '%s', type %d, pUsr %p\n", |
| 294 |
pStrm, pszPropName, propType, pUsr);*/ |
| 295 |
/* if we have no user pointer, there is no need to write this property. |
| 296 |
* TODO: think if that's the righ point of view |
| 297 |
* rgerhards, 2008-01-06 |
| 298 |
*/ |
| 299 |
if (pUsr == NULL) { |
| 300 |
ABORT_FINALIZE(RS_RET_OK); |
| 301 |
} |
| 302 |
|
| 303 |
/* TODO: use the stream functions for data conversion here - should be quicker */ |
| 304 |
|
| 305 |
switch (propType) { |
| 306 |
case PROPTYPE_PSZ: |
| 307 |
pszBuf = (uchar *)pUsr; |
| 308 |
lenBuf = ustrlen(pszBuf); |
| 309 |
vType = VARTYPE_STR; |
| 310 |
break; |
| 311 |
case PROPTYPE_SHORT: |
| 312 |
CHKiRet(srUtilItoA((char *)szBuf, sizeof(szBuf), (number_t) * ((short *)pUsr))); |
| 313 |
pszBuf = szBuf; |
| 314 |
lenBuf = ustrlen(szBuf); |
| 315 |
vType = VARTYPE_NUMBER; |
| 316 |
break; |
| 317 |
case PROPTYPE_INT: |
| 318 |
CHKiRet(srUtilItoA((char *)szBuf, sizeof(szBuf), (number_t) * ((int *)pUsr))); |
| 319 |
pszBuf = szBuf; |
| 320 |
lenBuf = ustrlen(szBuf); |
| 321 |
vType = VARTYPE_NUMBER; |
| 322 |
break; |
| 323 |
case PROPTYPE_LONG: |
| 324 |
CHKiRet(srUtilItoA((char *)szBuf, sizeof(szBuf), (number_t) * ((long *)pUsr))); |
| 325 |
pszBuf = szBuf; |
| 326 |
lenBuf = ustrlen(szBuf); |
| 327 |
vType = VARTYPE_NUMBER; |
| 328 |
break; |
| 329 |
case PROPTYPE_INT64: |
| 330 |
CHKiRet(srUtilItoA((char *)szBuf, sizeof(szBuf), *((int64 *)pUsr))); |
| 331 |
pszBuf = szBuf; |
| 332 |
lenBuf = ustrlen(szBuf); |
| 333 |
vType = VARTYPE_NUMBER; |
| 334 |
break; |
| 335 |
case PROPTYPE_CSTR: |
| 336 |
pszBuf = rsCStrGetSzStrNoNULL((cstr_t *)pUsr); |
| 337 |
lenBuf = rsCStrLen((cstr_t *)pUsr); |
| 338 |
vType = VARTYPE_STR; |
| 339 |
break; |
| 340 |
case PROPTYPE_SYSLOGTIME: |
| 341 |
lenBuf = |
| 342 |
snprintf((char *)szBuf, sizeof(szBuf), "%d:%d:%d:%d:%d:%d:%d:%d:%d:%c:%d:%d", |
| 343 |
((syslogTime_t *)pUsr)->timeType, ((syslogTime_t *)pUsr)->year, ((syslogTime_t *)pUsr)->month, |
| 344 |
((syslogTime_t *)pUsr)->day, ((syslogTime_t *)pUsr)->hour, ((syslogTime_t *)pUsr)->minute, |
| 345 |
((syslogTime_t *)pUsr)->second, ((syslogTime_t *)pUsr)->secfrac, |
| 346 |
((syslogTime_t *)pUsr)->secfracPrecision, ((syslogTime_t *)pUsr)->OffsetMode, |
| 347 |
((syslogTime_t *)pUsr)->OffsetHour, ((syslogTime_t *)pUsr)->OffsetMinute); |
| 348 |
if (lenBuf > sizeof(szBuf) - 1) ABORT_FINALIZE(RS_RET_PROVIDED_BUFFER_TOO_SMALL); |
| 349 |
vType = VARTYPE_SYSLOGTIME; |
| 350 |
pszBuf = szBuf; |
| 351 |
break; |
| 352 |
case PROPTYPE_NONE: |
| 353 |
default: |
| 354 |
dbgprintf("invalid PROPTYPE %d\n", propType); |
| 355 |
break; |
| 356 |
} |
| 357 |
|
| 358 |
/* cookie */ |
| 359 |
CHKiRet(strm.WriteChar(pStrm, COOKIE_PROPLINE)); |
| 360 |
/* name */ |
| 361 |
CHKiRet(strm.Write(pStrm, pszPropName, ustrlen(pszPropName))); |
| 362 |
CHKiRet(strm.WriteChar(pStrm, ':')); |
| 363 |
/* type */ |
| 364 |
CHKiRet(strm.WriteLong(pStrm, (int)vType)); |
| 365 |
CHKiRet(strm.WriteChar(pStrm, ':')); |
| 366 |
/* length */ |
| 367 |
CHKiRet(strm.WriteLong(pStrm, lenBuf)); |
| 368 |
CHKiRet(strm.WriteChar(pStrm, ':')); |
| 369 |
|
| 370 |
/* data */ |
| 371 |
CHKiRet(strm.Write(pStrm, (uchar *)pszBuf, lenBuf)); |
| 372 |
|
| 373 |
/* trailer */ |
| 374 |
CHKiRet(strm.WriteChar(pStrm, ':')); |
| 375 |
CHKiRet(strm.WriteChar(pStrm, '\n')); |
| 376 |
|
| 377 |
finalize_it: |
| 378 |
RETiRet; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
/* end serialization of an object. The caller receives a |
| 383 |
* standard C string, which he must free when no longer needed. |
| 384 |
*/ |
| 385 |
static rsRetVal EndSerialize(strm_t *pStrm) { |
| 386 |
DEFiRet; |
| 387 |
|
| 388 |
assert(pStrm != NULL); |
| 389 |
|
| 390 |
CHKiRet(strm.WriteChar(pStrm, COOKIE_ENDLINE)); |
| 391 |
CHKiRet(strm.Write(pStrm, (uchar *)"End\n", sizeof("END\n") - 1)); |
| 392 |
CHKiRet(strm.WriteChar(pStrm, COOKIE_BLANKLINE)); |
| 393 |
CHKiRet(strm.WriteChar(pStrm, '\n')); |
| 394 |
|
| 395 |
CHKiRet(strm.RecordEnd(pStrm)); |
| 396 |
|
| 397 |
finalize_it: |
| 398 |
RETiRet; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
/* define a helper to make code below a bit cleaner (and quicker to write) */ |
| 403 |
#define NEXTC CHKiRet(strm.ReadChar(pStrm, &c)) /*;dbgprintf("c: %c\n", c)*/ |
| 404 |
|
| 405 |
|
| 406 |
/* de-serialize an embedded, non-octect-counted string. This is useful |
| 407 |
* for deserializing the object name inside the header. The string is |
| 408 |
* terminated by the first occurrence of the ':' character. |
| 409 |
* rgerhards, 2008-02-29 |
| 410 |
*/ |
| 411 |
static rsRetVal objDeserializeEmbedStr(cstr_t **ppStr, strm_t *pStrm) { |
| 412 |
DEFiRet; |
| 413 |
uchar c; |
| 414 |
cstr_t *pStr = NULL; |
| 415 |
|
| 416 |
assert(ppStr != NULL); |
| 417 |
|
| 418 |
CHKiRet(cstrConstruct(&pStr)); |
| 419 |
|
| 420 |
NEXTC; |
| 421 |
while (c != ':') { |
| 422 |
CHKiRet(cstrAppendChar(pStr, c)); |
| 423 |
NEXTC; |
| 424 |
} |
| 425 |
cstrFinalize(pStr); |
| 426 |
|
| 427 |
*ppStr = pStr; |
| 428 |
|
| 429 |
finalize_it: |
| 430 |
if (iRet != RS_RET_OK && pStr != NULL) cstrDestruct(&pStr); |
| 431 |
|
| 432 |
RETiRet; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
/* de-serialize a number */ |
| 437 |
static rsRetVal objDeserializeNumber(number_t *pNum, strm_t *pStrm) { |
| 438 |
DEFiRet; |
| 439 |
number_t i; |
| 440 |
int bIsNegative; |
| 441 |
uchar c; |
| 442 |
|
| 443 |
assert(pNum != NULL); |
| 444 |
|
| 445 |
NEXTC; |
| 446 |
if (c == '-') { |
| 447 |
bIsNegative = 1; |
| 448 |
NEXTC; |
| 449 |
} else { |
| 450 |
bIsNegative = 0; |
| 451 |
} |
| 452 |
|
| 453 |
/* we check this so that we get more meaningful error codes */ |
| 454 |
if (!isdigit(c)) ABORT_FINALIZE(RS_RET_INVALID_NUMBER); |
| 455 |
|
| 456 |
i = 0; |
| 457 |
while (isdigit(c)) { |
| 458 |
i = i * 10 + c - '0'; |
| 459 |
NEXTC; |
| 460 |
} |
| 461 |
|
| 462 |
if (c != ':') ABORT_FINALIZE(RS_RET_INVALID_DELIMITER); |
| 463 |
|
| 464 |
if (bIsNegative) i *= -1; |
| 465 |
|
| 466 |
*pNum = i; |
| 467 |
finalize_it: |
| 468 |
RETiRet; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
/* de-serialize a string, length must be provided but may be 0 */ |
| 473 |
static rsRetVal objDeserializeStr(cstr_t **ppCStr, int iLen, strm_t *pStrm) { |
| 474 |
DEFiRet; |
| 475 |
int i; |
| 476 |
uchar c; |
| 477 |
cstr_t *pCStr = NULL; |
| 478 |
|
| 479 |
assert(ppCStr != NULL); |
| 480 |
assert(iLen >= 0); |
| 481 |
|
| 482 |
CHKiRet(cstrConstruct(&pCStr)); |
| 483 |
|
| 484 |
NEXTC; |
| 485 |
for (i = 0; i < iLen; ++i) { |
| 486 |
CHKiRet(cstrAppendChar(pCStr, c)); |
| 487 |
NEXTC; |
| 488 |
} |
| 489 |
cstrFinalize(pCStr); |
| 490 |
|
| 491 |
/* check terminator */ |
| 492 |
if (c != ':') { |
| 493 |
/* Initialized to NULL */ |
| 494 |
*ppCStr = NULL; |
| 495 |
ABORT_FINALIZE(RS_RET_INVALID_DELIMITER); |
| 496 |
} |
| 497 |
|
| 498 |
*ppCStr = pCStr; |
| 499 |
|
| 500 |
finalize_it: |
| 501 |
if (iRet != RS_RET_OK && pCStr != NULL) cstrDestruct(&pCStr); |
| 502 |
|
| 503 |
RETiRet; |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
/* de-serialize a syslogTime -- rgerhards,2008-01-08 */ |
| 508 |
#define GETVAL(var) \ |
| 509 |
CHKiRet(objDeserializeNumber(&l, pStrm)); \ |
| 510 |
pTime->var = l; |
| 511 |
static rsRetVal objDeserializeSyslogTime(syslogTime_t *pTime, strm_t *pStrm) { |
| 512 |
DEFiRet; |
| 513 |
number_t l; |
| 514 |
uchar c; |
| 515 |
|
| 516 |
assert(pTime != NULL); |
| 517 |
|
| 518 |
GETVAL(timeType); |
| 519 |
GETVAL(year); |
| 520 |
GETVAL(month); |
| 521 |
GETVAL(day); |
| 522 |
GETVAL(hour); |
| 523 |
GETVAL(minute); |
| 524 |
GETVAL(second); |
| 525 |
GETVAL(secfrac); |
| 526 |
GETVAL(secfracPrecision); |
| 527 |
/* OffsetMode is a single character! */ |
| 528 |
NEXTC; |
| 529 |
pTime->OffsetMode = c; |
| 530 |
NEXTC; |
| 531 |
if (c != ':') ABORT_FINALIZE(RS_RET_INVALID_DELIMITER); |
| 532 |
GETVAL(OffsetHour); |
| 533 |
GETVAL(OffsetMinute); |
| 534 |
|
| 535 |
finalize_it: |
| 536 |
RETiRet; |
| 537 |
} |
| 538 |
#undef GETVAL |
| 539 |
|
| 540 |
/* de-serialize an object header |
| 541 |
* rgerhards, 2008-01-07 |
| 542 |
*/ |
| 543 |
static rsRetVal objDeserializeHeader(uchar *pszRecType, cstr_t **ppstrID, int *poVers, strm_t *pStrm) { |
| 544 |
DEFiRet; |
| 545 |
number_t oVers; |
| 546 |
uchar c; |
| 547 |
|
| 548 |
assert(ppstrID != NULL); |
| 549 |
assert(poVers != NULL); |
| 550 |
assert(!strcmp((char *)pszRecType, "Obj") || !strcmp((char *)pszRecType, "OPB")); |
| 551 |
|
| 552 |
/* check header cookie */ |
| 553 |
NEXTC; |
| 554 |
if (c != COOKIE_OBJLINE) ABORT_FINALIZE(RS_RET_INVALID_HEADER); |
| 555 |
NEXTC; |
| 556 |
if (c != pszRecType[0]) ABORT_FINALIZE(RS_RET_INVALID_HEADER_RECTYPE); |
| 557 |
NEXTC; |
| 558 |
if (c != pszRecType[1]) ABORT_FINALIZE(RS_RET_INVALID_HEADER_RECTYPE); |
| 559 |
NEXTC; |
| 560 |
if (c != pszRecType[2]) ABORT_FINALIZE(RS_RET_INVALID_HEADER_RECTYPE); |
| 561 |
NEXTC; |
| 562 |
if (c != ':') ABORT_FINALIZE(RS_RET_INVALID_HEADER); |
| 563 |
NEXTC; |
| 564 |
if (c != '1') ABORT_FINALIZE(RS_RET_INVALID_HEADER_VERS); |
| 565 |
NEXTC; |
| 566 |
if (c != ':') ABORT_FINALIZE(RS_RET_INVALID_HEADER_VERS); |
| 567 |
|
| 568 |
/* object type and version */ |
| 569 |
CHKiRet(objDeserializeEmbedStr(ppstrID, pStrm)); |
| 570 |
CHKiRet(objDeserializeNumber(&oVers, pStrm)); |
| 571 |
|
| 572 |
/* and now we skip over the rest until the delemiting \n */ |
| 573 |
NEXTC; |
| 574 |
while (c != '\n') { |
| 575 |
NEXTC; |
| 576 |
} |
| 577 |
|
| 578 |
*poVers = oVers; |
| 579 |
|
| 580 |
finalize_it: |
| 581 |
RETiRet; |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
/* Deserialize a single property. Pointer must be positioned at begin of line. Whole line |
| 586 |
* up until the \n is read. |
| 587 |
*/ |
| 588 |
rsRetVal objDeserializeProperty(var_t *pProp, strm_t *pStrm) { |
| 589 |
DEFiRet; |
| 590 |
number_t i; |
| 591 |
number_t iLen; |
| 592 |
uchar c; |
| 593 |
int step = 0; /* which step was successful? */ |
| 594 |
int64 offs; |
| 595 |
|
| 596 |
assert(pProp != NULL); |
| 597 |
|
| 598 |
/* check cookie */ |
| 599 |
NEXTC; |
| 600 |
if (c != COOKIE_PROPLINE) { |
| 601 |
/* oops, we've read one char that does not belong to use - unget it first */ |
| 602 |
CHKiRet(strm.UnreadChar(pStrm, c)); |
| 603 |
ABORT_FINALIZE(RS_RET_NO_PROPLINE); |
| 604 |
} |
| 605 |
|
| 606 |
/* get the property name first */ |
| 607 |
CHKiRet(cstrConstruct(&pProp->pcsName)); |
| 608 |
|
| 609 |
NEXTC; |
| 610 |
while (c != ':') { |
| 611 |
CHKiRet(cstrAppendChar(pProp->pcsName, c)); |
| 612 |
NEXTC; |
| 613 |
} |
| 614 |
cstrFinalize(pProp->pcsName); |
| 615 |
step = 1; |
| 616 |
|
| 617 |
/* property type */ |
| 618 |
CHKiRet(objDeserializeNumber(&i, pStrm)); |
| 619 |
pProp->varType = i; |
| 620 |
step = 2; |
| 621 |
|
| 622 |
/* size (needed for strings) */ |
| 623 |
CHKiRet(objDeserializeNumber(&iLen, pStrm)); |
| 624 |
step = 3; |
| 625 |
|
| 626 |
/* we now need to deserialize the value */ |
| 627 |
switch (pProp->varType) { |
| 628 |
case VARTYPE_STR: |
| 629 |
CHKiRet(objDeserializeStr(&pProp->val.pStr, iLen, pStrm)); |
| 630 |
break; |
| 631 |
case VARTYPE_NUMBER: |
| 632 |
CHKiRet(objDeserializeNumber(&pProp->val.num, pStrm)); |
| 633 |
break; |
| 634 |
case VARTYPE_SYSLOGTIME: |
| 635 |
CHKiRet(objDeserializeSyslogTime(&pProp->val.vSyslogTime, pStrm)); |
| 636 |
break; |
| 637 |
case VARTYPE_NONE: |
| 638 |
default: |
| 639 |
dbgprintf("invalid VARTYPE %d\n", pProp->varType); |
| 640 |
break; |
| 641 |
} |
| 642 |
step = 4; |
| 643 |
|
| 644 |
/* we should now be at the end of the line. So the next char must be \n */ |
| 645 |
NEXTC; |
| 646 |
if (c != '\n') ABORT_FINALIZE(RS_RET_INVALID_PROPFRAME); |
| 647 |
|
| 648 |
finalize_it: |
| 649 |
/* ensure the type of var is reset back to VARTYPE_NONE since |
| 650 |
* the deconstruct method of var might free unallocated memory |
| 651 |
*/ |
| 652 |
if (iRet != RS_RET_OK && iRet != RS_RET_NO_PROPLINE) { |
| 653 |
if (pProp->varType == VARTYPE_STR && step < 4) { |
| 654 |
pProp->val.pStr = NULL; |
| 655 |
} |
| 656 |
if (step <= 2) { |
| 657 |
pProp->varType = VARTYPE_NONE; |
| 658 |
} |
| 659 |
} |
| 660 |
if (Debug && iRet != RS_RET_OK && iRet != RS_RET_NO_PROPLINE) { |
| 661 |
strm.GetCurrOffset(pStrm, &offs); |
| 662 |
dbgprintf("error %d deserializing property name, offset %lld, step %d\n", iRet, offs, step); |
| 663 |
strmDebugOutBuf(pStrm); |
| 664 |
if (step >= 1) { |
| 665 |
dbgprintf("error property name: '%s'\n", rsCStrGetSzStrNoNULL(pProp->pcsName)); |
| 666 |
} |
| 667 |
if (step >= 2) { |
| 668 |
dbgprintf("error var type: '%d'\n", pProp->varType); |
| 669 |
} |
| 670 |
if (step >= 3) { |
| 671 |
dbgprintf("error len: '%d'\n", (int)iLen); |
| 672 |
} |
| 673 |
if (step >= 4) { |
| 674 |
switch (pProp->varType) { |
| 675 |
case VARTYPE_STR: |
| 676 |
dbgprintf("error data string: '%s'\n", rsCStrGetSzStrNoNULL(pProp->val.pStr)); |
| 677 |
break; |
| 678 |
case VARTYPE_NUMBER: |
| 679 |
dbgprintf("error number: %d\n", (int)pProp->val.num); |
| 680 |
break; |
| 681 |
case VARTYPE_SYSLOGTIME: |
| 682 |
dbgprintf( |
| 683 |
"syslog time was successfully parsed (but " |
| 684 |
"is not displayed\n"); |
| 685 |
break; |
| 686 |
case VARTYPE_NONE: |
| 687 |
default: |
| 688 |
break; |
| 689 |
} |
| 690 |
} |
| 691 |
} |
| 692 |
RETiRet; |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
/* de-serialize an object trailer. This does not get any data but checks if the |
| 697 |
* format is ok. |
| 698 |
* rgerhards, 2008-01-07 |
| 699 |
*/ |
| 700 |
static rsRetVal objDeserializeTrailer(strm_t *pStrm) { |
| 701 |
DEFiRet; |
| 702 |
uchar c; |
| 703 |
|
| 704 |
/* check header cookie */ |
| 705 |
NEXTC; |
| 706 |
if (c != COOKIE_ENDLINE) ABORT_FINALIZE(RS_RET_INVALID_TRAILER); |
| 707 |
NEXTC; |
| 708 |
if (c != 'E') ABORT_FINALIZE(RS_RET_INVALID_TRAILER); |
| 709 |
NEXTC; |
| 710 |
if (c != 'n') ABORT_FINALIZE(RS_RET_INVALID_TRAILER); |
| 711 |
NEXTC; |
| 712 |
if (c != 'd') ABORT_FINALIZE(RS_RET_INVALID_TRAILER); |
| 713 |
NEXTC; |
| 714 |
if (c != '\n') ABORT_FINALIZE(RS_RET_INVALID_TRAILER); |
| 715 |
NEXTC; |
| 716 |
if (c != COOKIE_BLANKLINE) ABORT_FINALIZE(RS_RET_INVALID_TRAILER); |
| 717 |
NEXTC; |
| 718 |
if (c != '\n') ABORT_FINALIZE(RS_RET_INVALID_TRAILER); |
| 719 |
|
| 720 |
finalize_it: |
| 721 |
if (Debug && iRet != RS_RET_OK) { |
| 722 |
dbgprintf("objDeserializeTrailer fails with %d\n", iRet); |
| 723 |
} |
| 724 |
|
| 725 |
RETiRet; |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
/* This method tries to recover a serial store if it got out of sync. |
| 730 |
* To do so, it scans the line beginning cookies and waits for the object |
| 731 |
* cookie. If that is found, control is returned. If the store is exhausted, |
| 732 |
* we will receive an RS_RET_EOF error as part of NEXTC, which will also |
| 733 |
* terminate this function. So we may either return with somehting that |
| 734 |
* looks like a valid object or end of store. |
| 735 |
* rgerhards, 2008-01-07 |
| 736 |
*/ |
| 737 |
static rsRetVal objDeserializeTryRecover(strm_t *pStrm) { |
| 738 |
DEFiRet; |
| 739 |
uchar c; |
| 740 |
int bWasNL; |
| 741 |
int bRun; |
| 742 |
|
| 743 |
assert(pStrm != NULL); |
| 744 |
bRun = 1; |
| 745 |
bWasNL = 0; |
| 746 |
|
| 747 |
while (bRun) { |
| 748 |
NEXTC; |
| 749 |
if (c == '\n') |
| 750 |
bWasNL = 1; |
| 751 |
else { |
| 752 |
if (bWasNL == 1 && c == COOKIE_OBJLINE) |
| 753 |
bRun = 0; /* we found it! */ |
| 754 |
else |
| 755 |
bWasNL = 0; |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
CHKiRet(strm.UnreadChar(pStrm, c)); |
| 760 |
|
| 761 |
finalize_it: |
| 762 |
dbgprintf("deserializer has possibly been able to re-sync and recover, state %d\n", iRet); |
| 763 |
RETiRet; |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
static void varResetState(var_t *pVar) { |
| 768 |
rsCStrDestruct(&pVar->pcsName); /* no longer needed */ |
| 769 |
if (pVar->varType == VARTYPE_STR) { |
| 770 |
rsCStrDestruct(&pVar->val.pStr); |
| 771 |
} |
| 772 |
memset(&pVar->val, 0, sizeof(pVar->val)); |
| 773 |
pVar->varType = VARTYPE_NONE; |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
/* De-serialize the properties of an object. This includes processing |
| 778 |
* of the trailer. Header must already have been processed. |
| 779 |
* rgerhards, 2008-01-11 |
| 780 |
*/ |
| 781 |
static rsRetVal objDeserializeProperties(obj_t *pObj, rsRetVal (*objSetProperty)(void *, ...), strm_t *pStrm) { |
| 782 |
DEFiRet; |
| 783 |
var_t *pVar = NULL; |
| 784 |
|
| 785 |
ISOBJ_assert(pObj); |
| 786 |
ISOBJ_TYPE_assert(pStrm, strm); |
| 787 |
|
| 788 |
CHKiRet(var.Construct(&pVar)); |
| 789 |
CHKiRet(var.ConstructFinalize(pVar)); |
| 790 |
|
| 791 |
iRet = objDeserializeProperty(pVar, pStrm); |
| 792 |
while (iRet == RS_RET_OK) { |
| 793 |
CHKiRet(objSetProperty(pObj, pVar)); |
| 794 |
varResetState(pVar); |
| 795 |
iRet = objDeserializeProperty(pVar, pStrm); |
| 796 |
} |
| 797 |
|
| 798 |
if (iRet != RS_RET_NO_PROPLINE) FINALIZE; |
| 799 |
|
| 800 |
CHKiRet(objDeserializeTrailer(pStrm)); /* do trailer checks */ |
| 801 |
finalize_it: |
| 802 |
if (pVar != NULL) var.Destruct(&pVar); |
| 803 |
|
| 804 |
RETiRet; |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
/* De-Serialize an object. |
| 809 |
* Params: Pointer to object Pointer (pObj) (like a obj_t**, but can not do that due to compiler warning) |
| 810 |
* expected object ID (to check against), a fixup function that can modify the object before it is finalized |
| 811 |
* and a user pointer that is to be passed to that function in addition to the object. The fixup function |
| 812 |
* pointer may be NULL, in which case none is called. |
| 813 |
* The caller must destruct the created object. |
| 814 |
* rgerhards, 2008-01-07 |
| 815 |
*/ |
| 816 |
static rsRetVal Deserialize( |
| 817 |
void *ppObj, uchar *pszTypeExpected, strm_t *pStrm, rsRetVal (*fFixup)(obj_t *, void *), void *pUsr) { |
| 818 |
DEFiRet; |
| 819 |
rsRetVal iRetLocal; |
| 820 |
obj_t *pObj = NULL; |
| 821 |
int oVers = 0; /* keep compiler happy, but it is totally useless but takes up some execution time... */ |
| 822 |
cstr_t *pstrID = NULL; |
| 823 |
objInfo_t *pObjInfo; |
| 824 |
|
| 825 |
assert(ppObj != NULL); |
| 826 |
assert(pszTypeExpected != NULL); |
| 827 |
ISOBJ_TYPE_assert(pStrm, strm); |
| 828 |
|
| 829 |
/* we de-serialize the header. if all goes well, we are happy. However, if |
| 830 |
* we experience a problem, we try to recover. We do this by skipping to |
| 831 |
* the next object header. This is defined via the line-start cookies. In |
| 832 |
* worst case, we exhaust the queue, but then we receive EOF return state, |
| 833 |
* from objDeserializeTryRecover(), what will cause us to ultimately give up. |
| 834 |
* rgerhards, 2008-07-08 |
| 835 |
*/ |
| 836 |
do { |
| 837 |
iRetLocal = objDeserializeHeader((uchar *)"Obj", &pstrID, &oVers, pStrm); |
| 838 |
if (iRetLocal != RS_RET_OK) { |
| 839 |
dbgprintf("objDeserialize error %d during header processing - trying to recover\n", iRetLocal); |
| 840 |
CHKiRet(objDeserializeTryRecover(pStrm)); |
| 841 |
} |
| 842 |
} while (iRetLocal != RS_RET_OK); |
| 843 |
|
| 844 |
if (rsCStrSzStrCmp(pstrID, pszTypeExpected, ustrlen(pszTypeExpected))) ABORT_FINALIZE(RS_RET_INVALID_OID); |
| 845 |
|
| 846 |
CHKiRet(FindObjInfo((char *)cstrGetSzStrNoNULL(pstrID), &pObjInfo)); |
| 847 |
|
| 848 |
CHKiRet(pObjInfo->objMethods[objMethod_CONSTRUCT](&pObj)); |
| 849 |
|
| 850 |
/* we got the object, now we need to fill the properties */ |
| 851 |
CHKiRet( |
| 852 |
objDeserializeProperties(pObj, (rsRetVal(*)(void *, ...))pObjInfo->objMethods[objMethod_SETPROPERTY], pStrm)); |
| 853 |
|
| 854 |
/* check if we need to call a fixup function that modifies the object |
| 855 |
* before it is finalized. -- rgerhards, 2008-01-13 |
| 856 |
*/ |
| 857 |
if (fFixup != NULL) CHKiRet(fFixup(pObj, pUsr)); |
| 858 |
|
| 859 |
/* we have a valid object, let's finalize our work and return */ |
| 860 |
if (objInfoIsImplemented(pObjInfo, objMethod_CONSTRUCTION_FINALIZER)) |
| 861 |
CHKiRet(pObjInfo->objMethods[objMethod_CONSTRUCTION_FINALIZER](pObj)); |
| 862 |
|
| 863 |
*((obj_t **)ppObj) = pObj; |
| 864 |
|
| 865 |
finalize_it: |
| 866 |
if (iRet != RS_RET_OK && pObj != NULL) free(pObj); /* TODO: check if we can call destructor 2008-01-13 rger */ |
| 867 |
|
| 868 |
if (pstrID != NULL) rsCStrDestruct(&pstrID); |
| 869 |
|
| 870 |
RETiRet; |
| 871 |
} |
| 872 |
|
| 873 |
|
| 874 |
/* De-Serialize an object, with known constructur and destructor. Params like Deserialize(). |
| 875 |
* Note: this is for the queue subsystem, and optimized for its use. |
| 876 |
* rgerhards, 2012-11-03 |
| 877 |
*/ |
| 878 |
rsRetVal objDeserializeWithMethods(void *ppObj, |
| 879 |
uchar *pszTypeExpected, |
| 880 |
int lenTypeExpected, |
| 881 |
strm_t *pStrm, |
| 882 |
rsRetVal (*fFixup)(obj_t *, void *), |
| 883 |
void *pUsr, |
| 884 |
rsRetVal (*objConstruct)(void **), |
| 885 |
rsRetVal (*objConstructFinalize)(void *), |
| 886 |
rsRetVal (*objDeserialize)(void *, strm_t *)) { |
| 887 |
DEFiRet; |
| 888 |
rsRetVal iRetLocal; |
| 889 |
obj_t *pObj = NULL; |
| 890 |
int oVers = 0; /* keep compiler happy, but it is totally useless but takes up some execution time... */ |
| 891 |
cstr_t *pstrID = NULL; |
| 892 |
|
| 893 |
assert(ppObj != NULL); |
| 894 |
assert(pszTypeExpected != NULL); |
| 895 |
ISOBJ_TYPE_assert(pStrm, strm); |
| 896 |
|
| 897 |
/* we de-serialize the header. if all goes well, we are happy. However, if |
| 898 |
* we experience a problem, we try to recover. We do this by skipping to |
| 899 |
* the next object header. This is defined via the line-start cookies. In |
| 900 |
* worst case, we exhaust the queue, but then we receive EOF return state, |
| 901 |
* from objDeserializeTryRecover(), what will cause us to ultimately give up. |
| 902 |
* rgerhards, 2008-07-08 |
| 903 |
*/ |
| 904 |
do { |
| 905 |
iRetLocal = objDeserializeHeader((uchar *)"Obj", &pstrID, &oVers, pStrm); |
| 906 |
if (iRetLocal != RS_RET_OK) { |
| 907 |
dbgprintf( |
| 908 |
"objDeserialize error %d during header processing - " |
| 909 |
"trying to recover\n", |
| 910 |
iRetLocal); |
| 911 |
CHKiRet(objDeserializeTryRecover(pStrm)); |
| 912 |
} |
| 913 |
} while (iRetLocal != RS_RET_OK); |
| 914 |
|
| 915 |
if (rsCStrSzStrCmp(pstrID, pszTypeExpected, lenTypeExpected)) ABORT_FINALIZE(RS_RET_INVALID_OID); |
| 916 |
|
| 917 |
CHKiRet(objConstruct((void **)&pObj)); |
| 918 |
|
| 919 |
/* we got the object, now we need to fill the properties */ |
| 920 |
CHKiRet(objDeserialize(pObj, pStrm)); |
| 921 |
CHKiRet(objDeserializeTrailer(pStrm)); /* do trailer checks */ |
| 922 |
|
| 923 |
/* check if we need to call a fixup function that modifies the object |
| 924 |
* before it is finalized. -- rgerhards, 2008-01-13 |
| 925 |
*/ |
| 926 |
if (fFixup != NULL) CHKiRet(fFixup(pObj, pUsr)); |
| 927 |
|
| 928 |
/* we have a valid object, let's finalize our work and return */ |
| 929 |
if (objConstructFinalize != NULL) { |
| 930 |
CHKiRet(objConstructFinalize(pObj)); |
| 931 |
} |
| 932 |
|
| 933 |
*((obj_t **)ppObj) = pObj; |
| 934 |
|
| 935 |
finalize_it: |
| 936 |
if (iRet != RS_RET_OK && pObj != NULL) free(pObj); /* TODO: check if we can call destructor 2008-01-13 rger */ |
| 937 |
|
| 938 |
if (pstrID != NULL) rsCStrDestruct(&pstrID); |
| 939 |
|
| 940 |
if (Debug && iRet != RS_RET_OK) { |
| 941 |
dbgprintf("objDeserializeWithMethods fails with %d, stream state:\n", iRet); |
| 942 |
strmDebugOutBuf(pStrm); |
| 943 |
} |
| 944 |
|
| 945 |
|
| 946 |
RETiRet; |
| 947 |
} |
| 948 |
|
| 949 |
|
| 950 |
/* De-Serialize an object property bag. As a property bag contains only partial properties, |
| 951 |
* it is not instanciable. Thus, the caller must provide a pointer of an already-instanciated |
| 952 |
* object of the correct type. |
| 953 |
* Params: Pointer to object (pObj) |
| 954 |
* Pointer to be passed to the function |
| 955 |
* The caller must destruct the created object. |
| 956 |
* rgerhards, 2008-01-07 |
| 957 |
*/ |
| 958 |
static rsRetVal DeserializePropBag(obj_t *pObj, strm_t *pStrm) { |
| 959 |
DEFiRet; |
| 960 |
rsRetVal iRetLocal; |
| 961 |
cstr_t *pstrID = NULL; |
| 962 |
int oVers; |
| 963 |
objInfo_t *pObjInfo; |
| 964 |
|
| 965 |
ISOBJ_assert(pObj); |
| 966 |
ISOBJ_TYPE_assert(pStrm, strm); |
| 967 |
|
| 968 |
/* we de-serialize the header. if all goes well, we are happy. However, if |
| 969 |
* we experience a problem, we try to recover. We do this by skipping to |
| 970 |
* the next object header. This is defined via the line-start cookies. In |
| 971 |
* worst case, we exhaust the queue, but then we receive EOF return state |
| 972 |
* from objDeserializeTryRecover(), what will cause us to ultimately give up. |
| 973 |
* rgerhards, 2008-07-08 |
| 974 |
*/ |
| 975 |
do { |
| 976 |
iRetLocal = objDeserializeHeader((uchar *)"OPB", &pstrID, &oVers, pStrm); |
| 977 |
if (iRetLocal != RS_RET_OK) { |
| 978 |
dbgprintf("objDeserializePropBag error %d during header - trying to recover\n", iRetLocal); |
| 979 |
CHKiRet(objDeserializeTryRecover(pStrm)); |
| 980 |
} |
| 981 |
} while (iRetLocal != RS_RET_OK); |
| 982 |
|
| 983 |
if (rsCStrSzStrCmp(pstrID, pObj->pObjInfo->pszID, pObj->pObjInfo->lenID)) ABORT_FINALIZE(RS_RET_INVALID_OID); |
| 984 |
|
| 985 |
CHKiRet(FindObjInfo((char *)cstrGetSzStrNoNULL(pstrID), &pObjInfo)); |
| 986 |
|
| 987 |
/* we got the object, now we need to fill the properties */ |
| 988 |
CHKiRet( |
| 989 |
objDeserializeProperties(pObj, (rsRetVal(*)(void *, ...))pObjInfo->objMethods[objMethod_SETPROPERTY], pStrm)); |
| 990 |
|
| 991 |
finalize_it: |
| 992 |
if (pstrID != NULL) rsCStrDestruct(&pstrID); |
| 993 |
|
| 994 |
RETiRet; |
| 995 |
} |
| 996 |
|
| 997 |
#undef NEXTC /* undef helper macro */ |
| 998 |
|
| 999 |
|
| 1000 |
/* --------------- end object serializiation / deserialization support --------------- */ |
| 1001 |
|
| 1002 |
|
| 1003 |
/* set the object (instance) name |
| 1004 |
* rgerhards, 2008-01-29 |
| 1005 |
* TODO: change the naming to a rsCStr obj! (faster) |
| 1006 |
*/ |
| 1007 |
static rsRetVal SetName(obj_t *pThis, uchar *pszName) { |
| 1008 |
DEFiRet; |
| 1009 |
|
| 1010 |
free(pThis->pszName); |
| 1011 |
CHKmalloc(pThis->pszName = ustrdup(pszName)); |
| 1012 |
|
| 1013 |
finalize_it: |
| 1014 |
RETiRet; |
| 1015 |
} |
| 1016 |
|
| 1017 |
|
| 1018 |
/* get the object (instance) name |
| 1019 |
* Note that we use a non-standard calling convention. Thus function must never |
| 1020 |
* fail, else we run into real big problems. So it must make sure that at least someting |
| 1021 |
* is returned. |
| 1022 |
* rgerhards, 2008-01-30 |
| 1023 |
*/ |
| 1024 |
uchar *ATTR_NONNULL() objGetName(obj_t *const pThis) { |
| 1025 |
uchar *ret; |
| 1026 |
uchar szName[128]; |
| 1027 |
|
| 1028 |
ISOBJ_assert(pThis); |
| 1029 |
|
| 1030 |
if (pThis->pszName == NULL) { |
| 1031 |
snprintf((char *)szName, sizeof(szName), "%s %p", objGetClassName(pThis), pThis); |
| 1032 |
SetName(pThis, szName); |
| 1033 |
/* looks strange, but we NEED to re-check because if there was an |
| 1034 |
* error in objSetName(), the pointer may still be NULL |
| 1035 |
*/ |
| 1036 |
if (pThis->pszName == NULL) { |
| 1037 |
ret = objGetClassName(pThis); |
| 1038 |
} else { |
| 1039 |
ret = pThis->pszName; |
| 1040 |
} |
| 1041 |
} else { |
| 1042 |
ret = pThis->pszName; |
| 1043 |
} |
| 1044 |
|
| 1045 |
return ret; |
| 1046 |
} |
| 1047 |
|
| 1048 |
|
| 1049 |
/* Find the objInfo object for the current object |
| 1050 |
* rgerhards, 2008-02-29 |
| 1051 |
*/ |
| 1052 |
static rsRetVal FindObjInfo(const char *const __restrict__ strOID, objInfo_t **ppInfo) { |
| 1053 |
DEFiRet; |
| 1054 |
int bFound; |
| 1055 |
int i; |
| 1056 |
|
| 1057 |
bFound = 0; |
| 1058 |
i = 0; |
| 1059 |
while (!bFound && i < OBJ_NUM_IDS) { |
| 1060 |
if (arrObjInfo[i] != NULL && !strcmp(strOID, (const char *)arrObjInfo[i]->pszID)) { |
| 1061 |
bFound = 1; |
| 1062 |
break; |
| 1063 |
} |
| 1064 |
++i; |
| 1065 |
} |
| 1066 |
|
| 1067 |
if (!bFound) ABORT_FINALIZE(RS_RET_NOT_FOUND); |
| 1068 |
|
| 1069 |
*ppInfo = arrObjInfo[i]; |
| 1070 |
|
| 1071 |
finalize_it: |
| 1072 |
if (iRet != RS_RET_OK) { |
| 1073 |
dbgprintf("caller requested object '%s', not found (iRet %d)\n", strOID, iRet); |
| 1074 |
} |
| 1075 |
|
| 1076 |
RETiRet; |
| 1077 |
} |
| 1078 |
|
| 1079 |
|
| 1080 |
/* register a classes' info pointer, so that we can reference it later, if needed to |
| 1081 |
* (e.g. for de-serialization support). |
| 1082 |
* rgerhards, 2008-01-07 |
| 1083 |
* In this function, we look for a free space in the object table. While we do so, we |
| 1084 |
* also detect if the same object has already been registered, which is not valid. |
| 1085 |
* rgerhards, 2008-02-29 |
| 1086 |
*/ |
| 1087 |
static rsRetVal RegisterObj(uchar *pszObjName, objInfo_t *pInfo) { |
| 1088 |
DEFiRet; |
| 1089 |
int bFound; |
| 1090 |
int i; |
| 1091 |
|
| 1092 |
assert(pszObjName != NULL); |
| 1093 |
assert(pInfo != NULL); |
| 1094 |
|
| 1095 |
bFound = 0; |
| 1096 |
i = 0; |
| 1097 |
while (!bFound && i < OBJ_NUM_IDS && arrObjInfo[i] != NULL) { |
| 1098 |
if (arrObjInfo[i] != NULL && !ustrcmp(arrObjInfo[i]->pszID, pszObjName)) { |
| 1099 |
bFound = 1; |
| 1100 |
break; |
| 1101 |
} |
| 1102 |
++i; |
| 1103 |
} |
| 1104 |
|
| 1105 |
if (bFound) ABORT_FINALIZE(RS_RET_OBJ_ALREADY_REGISTERED); |
| 1106 |
if (i >= OBJ_NUM_IDS) ABORT_FINALIZE(RS_RET_OBJ_REGISTRY_OUT_OF_SPACE); |
| 1107 |
|
| 1108 |
arrObjInfo[i] = pInfo; |
| 1109 |
#if DEV_DEBUG == 1 |
| 1110 |
dbgprintf( |
| 1111 |
"object '%s' successfully registered with " |
| 1112 |
"index %d, qIF %p\n", |
| 1113 |
pszObjName, i, pInfo->QueryIF); |
| 1114 |
#endif |
| 1115 |
|
| 1116 |
finalize_it: |
| 1117 |
if (iRet != RS_RET_OK) { |
| 1118 |
LogError(0, NO_ERRCODE, "registering object '%s' failed with error code %d", pszObjName, iRet); |
| 1119 |
} |
| 1120 |
|
| 1121 |
RETiRet; |
| 1122 |
} |
| 1123 |
|
| 1124 |
|
| 1125 |
/* deregister a classes' info pointer, usually called because the class is unloaded. |
| 1126 |
* After deregistration, the class can no longer be accessed, except if it is reloaded. |
| 1127 |
* rgerhards, 2008-03-10 |
| 1128 |
*/ |
| 1129 |
static rsRetVal UnregisterObj(uchar *pszObjName) { |
| 1130 |
DEFiRet; |
| 1131 |
int bFound; |
| 1132 |
int i; |
| 1133 |
|
| 1134 |
assert(pszObjName != NULL); |
| 1135 |
|
| 1136 |
bFound = 0; |
| 1137 |
i = 0; |
| 1138 |
while (!bFound && i < OBJ_NUM_IDS) { |
| 1139 |
if (arrObjInfo[i] != NULL && !ustrcmp(arrObjInfo[i]->pszID, pszObjName)) { |
| 1140 |
bFound = 1; |
| 1141 |
break; |
| 1142 |
} |
| 1143 |
++i; |
| 1144 |
} |
| 1145 |
|
| 1146 |
if (!bFound) ABORT_FINALIZE(RS_RET_OBJ_NOT_REGISTERED); |
| 1147 |
|
| 1148 |
InfoDestruct(&arrObjInfo[i]); |
| 1149 |
#if DEV_DEBUG == 1 |
| 1150 |
dbgprintf("object '%s' successfully unregistered with index %d\n", pszObjName, i); |
| 1151 |
#endif |
| 1152 |
|
| 1153 |
finalize_it: |
| 1154 |
if (iRet != RS_RET_OK) { |
| 1155 |
dbgprintf("unregistering object '%s' failed with error code %d\n", pszObjName, iRet); |
| 1156 |
} |
| 1157 |
|
| 1158 |
RETiRet; |
| 1159 |
} |
| 1160 |
|
| 1161 |
|
| 1162 |
/* This function shall be called by anyone who would like to use an object. It will |
| 1163 |
* try to locate the object, load it into memory if not already present and return |
| 1164 |
* a pointer to the objects interface. |
| 1165 |
* rgerhards, 2008-02-29 |
| 1166 |
*/ |
| 1167 |
/* Note on atomic operations: |
| 1168 |
* ifIsLoaded uses PREFER_FETCH_32BIT for reads to prevent data races on platforms with atomics. |
| 1169 |
* Writes always occur under mutObjGlobalOp and use plain assignment. |
| 1170 |
* On platforms without atomics, PREFER_* degrades to plain operations (acceptable for 20+ years). |
| 1171 |
* The mutex provides full synchronization; atomics only improve diagnostic cleanliness (TSAN). |
| 1172 |
*/ |
| 1173 |
static ATTR_NO_SANITIZE_UNDEFINED rsRetVal UseObj(const char *srcFile, |
| 1174 |
uchar *pObjName, |
| 1175 |
uchar *pObjFile, |
| 1176 |
interface_t *pIf) { |
| 1177 |
DEFiRet; |
| 1178 |
objInfo_t *pObjInfo; |
| 1179 |
|
| 1180 |
|
| 1181 |
#if DEV_DEBUG == 1 |
| 1182 |
/* Best-effort atomic read before mutex: avoids TSAN warnings on platforms with atomics */ |
| 1183 |
dbgprintf( |
| 1184 |
"source file %s requests object '%s', " |
| 1185 |
" ifIsLoaded %d\n", |
| 1186 |
srcFile, pObjName, PREFER_FETCH_32BIT(pIf->ifIsLoaded)); |
| 1187 |
#endif |
| 1188 |
pthread_mutex_lock(&mutObjGlobalOp); |
| 1189 |
|
| 1190 |
/* Best-effort atomic reads: avoid UB/TSAN warnings from concurrent plain-write by other threads. |
| 1191 |
* Synchronization is provided by mutex; these are defensive reads only. */ |
| 1192 |
if (PREFER_FETCH_32BIT(pIf->ifIsLoaded) == 1) { |
| 1193 |
ABORT_FINALIZE(RS_RET_OK); /* we are already set */ |
| 1194 |
} |
| 1195 |
if (PREFER_FETCH_32BIT(pIf->ifIsLoaded) == 2) { |
| 1196 |
ABORT_FINALIZE(RS_RET_LOAD_ERROR); /* we had a load error and can not continue */ |
| 1197 |
} |
| 1198 |
|
| 1199 |
/* we must be careful that we do not enter in infinite loop if an error occurs during |
| 1200 |
* loading a module. ModLoad emits an error message in such cases and that potentially |
| 1201 |
* can trigger the same code here. So we initially set the module state to "load error" |
| 1202 |
* and set it to "fully initialized" when the load succeeded. It's a bit hackish, but |
| 1203 |
* looks like a good solution. -- rgerhards, 2008-03-07 |
| 1204 |
*/ |
| 1205 |
pIf->ifIsLoaded = 2; /* plain write: protected by mutObjGlobalOp */ |
| 1206 |
|
| 1207 |
iRet = FindObjInfo((const char *)pObjName, &pObjInfo); |
| 1208 |
if (iRet == RS_RET_NOT_FOUND) { |
| 1209 |
/* in this case, we need to see if we can dynamically load the object */ |
| 1210 |
if (pObjFile == NULL) { |
| 1211 |
FINALIZE; /* no chance, we have lost... */ |
| 1212 |
} else { |
| 1213 |
CHKiRet(module.Load(pObjFile, 0, NULL)); |
| 1214 |
/* NOW, we must find it or we have a problem... */ |
| 1215 |
CHKiRet(FindObjInfo((const char *)pObjName, &pObjInfo)); |
| 1216 |
} |
| 1217 |
} else if (iRet != RS_RET_OK) { |
| 1218 |
FINALIZE; /* give up */ |
| 1219 |
} |
| 1220 |
|
| 1221 |
/* if we reach this point, we have a valid pObjInfo */ |
| 1222 |
if (pObjFile != NULL) { /* NULL means core module */ |
| 1223 |
module.Use(srcFile, pObjInfo->pModInfo); /* increase refcount */ |
| 1224 |
} |
| 1225 |
|
| 1226 |
// TODO: fix interface pointer type, remove ATTR_NO_SANITIZE_UNDEFINED above |
| 1227 |
// The supression is just an interim solution until the pointer issue |
| 1228 |
// has been fully analyzed and aligned (or considered OK w/ reasoning). |
| 1229 |
CHKiRet(pObjInfo->QueryIF(pIf)); |
| 1230 |
pIf->ifIsLoaded = 1; /* plain write: protected by mutObjGlobalOp */ |
| 1231 |
|
| 1232 |
finalize_it: |
| 1233 |
pthread_mutex_unlock(&mutObjGlobalOp); |
| 1234 |
RETiRet; |
| 1235 |
} |
| 1236 |
|
| 1237 |
|
| 1238 |
/* This function shall be called when a caller is done with an object. Its primary |
| 1239 |
* purpose is to keep the reference count correct, which is highly important for |
| 1240 |
* modules residing in loadable modules. |
| 1241 |
* rgerhards, 2008-03-10 |
| 1242 |
*/ |
| 1243 |
static rsRetVal ReleaseObj(const char *srcFile, uchar *pObjName, uchar *pObjFile, interface_t *pIf) { |
| 1244 |
DEFiRet; |
| 1245 |
objInfo_t *pObjInfo; |
| 1246 |
|
| 1247 |
/* dev debug only dbgprintf("source file %s releasing object '%s', |
| 1248 |
ifIsLoaded %d\n", srcFile, pObjName, PREFER_FETCH_32BIT(pIf->ifIsLoaded)); */ |
| 1249 |
pthread_mutex_lock(&mutObjGlobalOp); |
| 1250 |
|
| 1251 |
if (pObjFile == NULL) FINALIZE; /* if it is not a lodable module, we do not need to do anything... */ |
| 1252 |
|
| 1253 |
if (PREFER_FETCH_32BIT(pIf->ifIsLoaded) == 0) { |
| 1254 |
FINALIZE; /* we are not loaded - this is perfectly OK... */ |
| 1255 |
} else if (PREFER_FETCH_32BIT(pIf->ifIsLoaded) == 2) { |
| 1256 |
/* Clean up failed load attempt */ |
| 1257 |
pIf->ifIsLoaded = 0; /* plain write: protected by mutObjGlobalOp */ |
| 1258 |
FINALIZE; /* we had a load error and can not/must not continue */ |
| 1259 |
} |
| 1260 |
|
| 1261 |
CHKiRet(FindObjInfo((const char *)pObjName, &pObjInfo)); |
| 1262 |
|
| 1263 |
/* if we reach this point, we have a valid pObjInfo */ |
| 1264 |
module.Release(srcFile, &pObjInfo->pModInfo); /* decrease refcount */ |
| 1265 |
|
| 1266 |
pIf->ifIsLoaded = 0; /* plain write: protected by mutObjGlobalOp */ |
| 1267 |
|
| 1268 |
finalize_it: |
| 1269 |
pthread_mutex_unlock(&mutObjGlobalOp); |
| 1270 |
|
| 1271 |
RETiRet; |
| 1272 |
} |
| 1273 |
|
| 1274 |
|
| 1275 |
/* queryInterface function |
| 1276 |
* rgerhards, 2008-02-29 |
| 1277 |
*/ |
| 1278 |
PROTOTYPEObjQueryInterface(obj); |
| 1279 |
BEGINobjQueryInterface(obj) |
| 1280 |
CODESTARTobjQueryInterface(obj); |
| 1281 |
if (pIf->ifVersion != objCURR_IF_VERSION) { /* check for current version, increment on each change */ |
| 1282 |
ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED); |
| 1283 |
} |
| 1284 |
|
| 1285 |
/* ok, we have the right interface, so let's fill it |
| 1286 |
* Please note that we may also do some backwards-compatibility |
| 1287 |
* work here (if we can support an older interface version - that, |
| 1288 |
* of course, also affects the "if" above). |
| 1289 |
*/ |
| 1290 |
pIf->UseObj = UseObj; |
| 1291 |
pIf->ReleaseObj = ReleaseObj; |
| 1292 |
pIf->InfoConstruct = InfoConstruct; |
| 1293 |
pIf->DestructObjSelf = DestructObjSelf; |
| 1294 |
pIf->BeginSerializePropBag = BeginSerializePropBag; |
| 1295 |
pIf->InfoSetMethod = InfoSetMethod; |
| 1296 |
pIf->BeginSerialize = BeginSerialize; |
| 1297 |
pIf->SerializeProp = SerializeProp; |
| 1298 |
pIf->EndSerialize = EndSerialize; |
| 1299 |
pIf->RegisterObj = RegisterObj; |
| 1300 |
pIf->UnregisterObj = UnregisterObj; |
| 1301 |
pIf->Deserialize = Deserialize; |
| 1302 |
pIf->DeserializePropBag = DeserializePropBag; |
| 1303 |
pIf->SetName = SetName; |
| 1304 |
pIf->GetName = objGetName; |
| 1305 |
finalize_it: |
| 1306 |
ENDobjQueryInterface(obj) |
| 1307 |
|
| 1308 |
|
| 1309 |
/* This function returns a pointer to our own interface. It is used as the |
| 1310 |
* hook that every object (including dynamically loaded ones) can use to |
| 1311 |
* obtain a pointer to our interface which than can be used to obtain |
| 1312 |
* pointers to any other interface in the system. This function must be |
| 1313 |
* externally visible because of its special nature. |
| 1314 |
* rgerhards, 2008-02-29 [nice - will have that date the next time in 4 years ;)] |
| 1315 |
*/ |
| 1316 |
rsRetVal objGetObjInterface(obj_if_t *pIf) { |
| 1317 |
DEFiRet; |
| 1318 |
assert(pIf != NULL); |
| 1319 |
objQueryInterface(pIf); |
| 1320 |
RETiRet; |
| 1321 |
} |
| 1322 |
|
| 1323 |
|
| 1324 |
/* exit our class |
| 1325 |
* rgerhards, 2008-03-11 |
| 1326 |
*/ |
| 1327 |
rsRetVal objClassExit(void) { |
| 1328 |
DEFiRet; |
| 1329 |
/* release objects we no longer need */ |
| 1330 |
objRelease(strm, CORE_COMPONENT); |
| 1331 |
objRelease(var, CORE_COMPONENT); |
| 1332 |
objRelease(module, CORE_COMPONENT); |
| 1333 |
|
| 1334 |
/* TODO: implement the class exits! */ |
| 1335 |
#if 0 |
| 1336 |
cfsyslineExit(pModInfo); |
| 1337 |
varClassExit(pModInfo); |
| 1338 |
#endif |
| 1339 |
moduleClassExit(); |
| 1340 |
RETiRet; |
| 1341 |
} |
| 1342 |
|
| 1343 |
|
| 1344 |
/* initialize our own class |
| 1345 |
* Please note that this also initializes those classes that we rely on. |
| 1346 |
* Though this is a bit dirty, we need to do it - otherwise we can't get |
| 1347 |
* around that bootstrap problem. We need to face the fact the the obj |
| 1348 |
* class is a little different from the rest of the system, as it provides |
| 1349 |
* the core class loader functionality. |
| 1350 |
* rgerhards, 2008-02-29 |
| 1351 |
*/ |
| 1352 |
rsRetVal objClassInit(modInfo_t *pModInfo) { |
| 1353 |
pthread_mutexattr_t mutAttr; |
| 1354 |
int i; |
| 1355 |
DEFiRet; |
| 1356 |
|
| 1357 |
/* first, initialize the object system itself. This must be done |
| 1358 |
* before any other object is created. |
| 1359 |
*/ |
| 1360 |
for (i = 0; i < OBJ_NUM_IDS; ++i) { |
| 1361 |
arrObjInfo[i] = NULL; |
| 1362 |
} |
| 1363 |
|
| 1364 |
/* the mutex must be recursive, because objects may call into other |
| 1365 |
* object identifiers recursively. |
| 1366 |
*/ |
| 1367 |
pthread_mutexattr_init(&mutAttr); |
| 1368 |
pthread_mutexattr_settype(&mutAttr, PTHREAD_MUTEX_RECURSIVE); |
| 1369 |
pthread_mutex_init(&mutObjGlobalOp, &mutAttr); |
| 1370 |
|
| 1371 |
/* request objects we use */ |
| 1372 |
CHKiRet(objGetObjInterface(&obj)); /* get ourselves ;) */ |
| 1373 |
|
| 1374 |
/* init classes we use (limit to as few as possible!) */ |
| 1375 |
CHKiRet(datetimeClassInit(pModInfo)); |
| 1376 |
CHKiRet(cfsyslineInit()); |
| 1377 |
CHKiRet(varClassInit(pModInfo)); |
| 1378 |
CHKiRet(moduleClassInit(pModInfo)); |
| 1379 |
CHKiRet(strmClassInit(pModInfo)); |
| 1380 |
CHKiRet(objUse(var, CORE_COMPONENT)); |
| 1381 |
CHKiRet(objUse(module, CORE_COMPONENT)); |
| 1382 |
CHKiRet(objUse(strm, CORE_COMPONENT)); |
| 1383 |
|
| 1384 |
finalize_it: |
| 1385 |
RETiRet; |
| 1386 |
} |