Debian Codegraph @

runtime/obj-types.h

raw
1 /* Some type definitions and macros for the obj object.
2 * I needed to move them out of the main obj.h, because obj.h's
3 * prototypes use other data types. However, their .h's rely
4 * on some of the obj.h data types and macros. So I needed to break
5 * that loop somehow and I've done that by moving the typedefs
6 * into this file here.
7 *
8 * Copyright 2008-2026 Rainer Gerhards and Adiscon GmbH.
9 *
10 * This file is part of the rsyslog runtime library.
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 * -or-
18 * see COPYING.ASL20 in the source distribution
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 */
26
27 #ifndef OBJ_TYPES_H_INCLUDED
28 #define OBJ_TYPES_H_INCLUDED
29
30 #include "stringbuf.h"
31 #include "syslogd-types.h"
32 #include "atomic.h"
33
34 /* property types for obj[De]Serialize() */
35 typedef enum {
36 PROPTYPE_NONE = 0, /* currently no value set */
37 PROPTYPE_PSZ = 1,
38 PROPTYPE_SHORT = 2,
39 PROPTYPE_INT = 3,
40 PROPTYPE_LONG = 4,
41 PROPTYPE_INT64 = 5,
42 PROPTYPE_CSTR = 6,
43 PROPTYPE_SYSLOGTIME = 7
44 } propType_t;
45
46 typedef unsigned objID_t;
47
48 typedef enum { /* IDs of base methods supported by all objects - used for jump table, so
49 * they must start at zero and be incremented. -- rgerhards, 2008-01-04
50 */
51 objMethod_CONSTRUCT = 0,
52 objMethod_DESTRUCT = 1,
53 objMethod_SERIALIZE = 2,
54 objMethod_DESERIALIZE = 3,
55 objMethod_SETPROPERTY = 4,
56 objMethod_CONSTRUCTION_FINALIZER = 5,
57 objMethod_GETSEVERITY = 6,
58 objMethod_DEBUGPRINT = 7
59 } objMethod_t;
60 #define OBJ_NUM_METHODS 8 /* must be updated to contain the max number of methods supported */
61
62
63 /* the base data type for interfaces
64 * This MUST be in sync with the ifBEGIN macro
65 */
66 struct interface_s {
67 int ifVersion; /* must be set to version requested */
68 int ifIsLoaded;
69 /* is the interface loaded? (0-no, 1-yes, 2-load failed; if not 1, functions can NOT be called! */
70 };
71
72
73 struct objInfo_s {
74 uchar *pszID; /* the object ID as a string */
75 size_t lenID; /* length of the ID string */
76 int iObjVers;
77 uchar *pszName;
78 rsRetVal (*objMethods[OBJ_NUM_METHODS])(void *, ...);
79 rsRetVal (*QueryIF)(interface_t *);
80 struct modInfo_s *pModInfo;
81 };
82
83
84 struct obj_s { /* the dummy struct that each derived class can be casted to */
85 objInfo_t *pObjInfo;
86 #ifndef NDEBUG /* this means if debug... */
87 unsigned int iObjCooCKiE; /* must always be 0xBADEFEE for a valid object */
88 #endif
89 uchar *pszName; /* the name of *this* specific object instance */
90 };
91
92
93 /* macros which must be gloablly-visible (because they are used during definition of
94 * other objects.
95 */
96 #ifndef NDEBUG /* this means if debug... */
97 #include <string.h>
98 #define BEGINobjInstance obj_t objData
99 #define ISOBJ_assert(pObj) \
100 do { \
101 assert((pObj) != NULL); \
102 assert((unsigned)((obj_t *)(pObj))->iObjCooCKiE == (unsigned)0xBADEFEE); \
103 } while (0);
104 #define ISOBJ_TYPE_assert(pObj, objType) \
105 do { \
106 assert(pObj != NULL); \
107 if (strcmp((char *)(((obj_t *)pObj)->pObjInfo->pszID), #objType)) { \
108 dbgprintf( \
109 "%s:%d ISOBJ assert failure: invalid object type, expected '%s' " \
110 "actual '%s', cookie: %X\n", \
111 __FILE__, __LINE__, #objType, (((obj_t *)pObj)->pObjInfo->pszID), ((obj_t *)(pObj))->iObjCooCKiE); \
112 fprintf(stderr, \
113 "%s:%d ISOBJ assert failure: invalid object type, expected '%s' " \
114 "actual '%s', cookie: %X\n", \
115 __FILE__, __LINE__, #objType, (((obj_t *)pObj)->pObjInfo->pszID), \
116 ((obj_t *)(pObj))->iObjCooCKiE); \
117 fflush(stderr); \
118 assert(!strcmp((char *)(((obj_t *)pObj)->pObjInfo->pszID), #objType)); \
119 } \
120 assert((unsigned)((obj_t *)(pObj))->iObjCooCKiE == (unsigned)0xBADEFEE); \
121 } while (0)
122 /* now the same for pointers to "regular" objects (like wrkrInstanceData) */
123 #define PTR_ASSERT_DEF unsigned int _Assert_type;
124 #define PTR_ASSERT_SET_TYPE(_ptr, _type) _ptr->_Assert_type = _type
125 #define PTR_ASSERT_CHK(_ptr, _type) \
126 do { \
127 assert(_ptr != NULL); \
128 if (_ptr->_Assert_type != _type) { \
129 dbgprintf( \
130 "%s:%d PTR_ASSERT_CHECK failure: invalid pointer type %x, " \
131 "expected %x\n", \
132 __FILE__, __LINE__, _ptr->_Assert_type, _type); \
133 fprintf(stderr, \
134 "%s:%d PTR_ASSERT_CHECK failure: invalid pointer type %x, " \
135 "expected %x\n", \
136 __FILE__, __LINE__, _ptr->_Assert_type, _type); \
137 assert(_ptr->_Assert_type == _type); \
138 } \
139 } while (0)
140 #else /* non-debug mode, no checks but much faster */
141 #define BEGINobjInstance obj_t objData
142 #define ISOBJ_TYPE_assert(pObj, objType)
143 #define ISOBJ_assert(pObj)
144
145 #define PTR_ASSERT_DEF
146 #define PTR_ASSERT_SET_TYPE(_ptr, _type)
147 #define PTR_ASSERT_CHK(_ptr, _type)
148 #endif
149
150 /* a set method for *very simple* object accesses. Note that this does
151 * NOT conform to the standard calling conventions and should be
152 * used only if actually nothing can go wrong! -- rgerhards, 2008-04-17
153 */
154 #define DEFpropGetMeth(obj, prop, dataType) \
155 dataType obj##Get##prop(void) { \
156 return pThis->prop = pVal; \
157 }
158
159 #define DEFpropSetMethPTR(obj, prop, dataType) \
160 rsRetVal obj##Set##prop(obj##_t *pThis, dataType *pVal) { \
161 pThis->prop = pVal; \
162 return RS_RET_OK; \
163 }
164 #define PROTOTYPEpropSetMethPTR(obj, prop, dataType) rsRetVal obj##Set##prop(obj##_t *pThis, dataType *)
165 #define DEFpropSetMethFP(obj, prop, dataType) \
166 rsRetVal obj##Set##prop(obj##_t *pThis, dataType) { \
167 pThis->prop = pVal; \
168 return RS_RET_OK; \
169 }
170 #define PROTOTYPEpropSetMethFP(obj, prop, dataType) rsRetVal obj##Set##prop(obj##_t *pThis, dataType)
171 #define DEFpropSetMeth(obj, prop, dataType) \
172 rsRetVal obj##Set##prop(obj##_t *pThis, dataType pVal) { \
173 pThis->prop = pVal; \
174 return RS_RET_OK; \
175 }
176 #define PROTOTYPEpropSetMeth(obj, prop, dataType) rsRetVal obj##Set##prop(obj##_t *pThis, dataType pVal)
177 #define INTERFACEpropSetMeth(obj, prop, dataType) rsRetVal (*Set##prop)(obj##_t * pThis, dataType)
178 /* class initializer */
179 #define PROTOTYPEObjClassInit(objName) rsRetVal objName##ClassInit(struct modInfo_s *)
180 /* below: objName must be the object name (e.g. vm, strm, ...) and ISCORE must be
181 * 1 if the module is a statically linked core module and 0 if it is a
182 * dynamically loaded one. -- rgerhards, 2008-02-29
183 */
184 #define OBJ_IS_CORE_MODULE 1 /* This should better be renamed to something like "OBJ_IS_NOT_LIBHEAD" or so... ;) */
185 #define OBJ_IS_LOADABLE_MODULE 0
186 #define BEGINObjClassInit(objName, objVers, objType) \
187 rsRetVal objName##ClassInit(struct modInfo_s *pModInfo) { \
188 DEFiRet; \
189 if (objType == OBJ_IS_CORE_MODULE) { /* are we a core module? */ \
190 CHKiRet(objGetObjInterface(&obj)); /* this provides the root pointer for all other queries */ \
191 } \
192 CHKiRet(obj.InfoConstruct(&pObjInfoOBJ, (uchar *)#objName, objVers, (rsRetVal(*)(void *))objName##Construct, \
193 (rsRetVal(*)(void *))objName##Destruct, \
194 (rsRetVal(*)(interface_t *))objName##QueryInterface, pModInfo));
195
196 #define ENDObjClassInit(objName) \
197 iRet = obj.RegisterObj((uchar *)#objName, pObjInfoOBJ); \
198 finalize_it: \
199 RETiRet; \
200 }
201
202 /* ... and now the same for abstract classes.
203 * TODO: consolidate the two -- rgerhards, 2008-02-29
204 */
205 #define BEGINAbstractObjClassInit(objName, objVers, objType) \
206 rsRetVal objName##ClassInit(struct modInfo_s *pModInfo) { \
207 DEFiRet; \
208 if (objType == OBJ_IS_CORE_MODULE) { /* are we a core module? */ \
209 CHKiRet(objGetObjInterface(&obj)); /* this provides the root pointer for all other queries */ \
210 } \
211 CHKiRet(obj.InfoConstruct(&pObjInfoOBJ, (uchar *)#objName, objVers, NULL, NULL, \
212 (rsRetVal(*)(interface_t *))objName##QueryInterface, pModInfo));
213
214 #define ENDObjClassInit(objName) \
215 iRet = obj.RegisterObj((uchar *)#objName, pObjInfoOBJ); \
216 finalize_it: \
217 RETiRet; \
218 }
219
220
221 /* now come the class exit. This is to be called immediately before the class is
222 * unloaded (actual unload for plugins, program termination for core modules)
223 * gerhards, 2008-03-10
224 */
225 #define PROTOTYPEObjClassExit(objName) rsRetVal objName##ClassExit(void)
226 #define BEGINObjClassExit(objName, objType) \
227 rsRetVal objName##ClassExit(void) { \
228 DEFiRet;
229
230 #define CODESTARTObjClassExit(objName)
231
232 #define ENDObjClassExit(objName) \
233 iRet = obj.UnregisterObj((uchar *)#objName); \
234 RETiRet; \
235 }
236
237 /* this defines both the constructor and initializer
238 * rgerhards, 2008-01-10
239 */
240 #define PROTOTYPEobjConstruct(obj) rsRetVal obj##Construct(obj##_t **ppThis)
241 #define BEGINobjConstruct(obj) \
242 static rsRetVal obj##Initialize(obj##_t __attribute__((unused)) * pThis) { \
243 DEFiRet;
244
245 #define ENDobjConstruct(obj) \
246 /* use finalize_it: before calling the macro (if you need it)! */ \
247 RETiRet; \
248 } \
249 rsRetVal obj##Construct(obj##_t **ppThis) { \
250 DEFiRet; \
251 obj##_t *pThis; \
252 \
253 assert(ppThis != NULL); \
254 \
255 if ((pThis = (obj##_t *)calloc(1, sizeof(obj##_t))) == NULL) { \
256 ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); \
257 } \
258 objConstructSetObjInfo(pThis); \
259 \
260 obj##Initialize(pThis); \
261 \
262 finalize_it: \
263 OBJCONSTRUCT_CHECK_SUCCESS_AND_CLEANUP \
264 RETiRet; \
265 }
266
267
268 /* this defines the destructor. The important point is that the base object
269 * destructor is called. The upper-level class shall destruct all of its
270 * properties, but not the instance itself. This is freed here by the
271 * framework (we need an intact pointer because we need to free the
272 * obj_t structures inside it). A pointer to the object pointer must be
273 * parse, because it is re-set to NULL (this, for example, is important in
274 * cancellation handlers). The object pointer is always named pThis.
275 * The object is always freed, even if there is some error while
276 * Cancellation is blocked during destructors, as this could have fatal
277 * side-effects. However, this also means the upper-level object should
278 * not perform any lengthy processing.
279 * IMPORTANT: if the upper level object requires some situations where the
280 * object shall not be destructed (e.g. via reference counting), then
281 * it shall set pThis to NULL, which prevents destruction of the
282 * object.
283 * processing.
284 * rgerhards, 2008-01-30
285 */
286 #define PROTOTYPEobjDestruct(OBJ) rsRetVal OBJ##Destruct(OBJ##_t __attribute__((unused)) * *ppThis)
287 /* note: we generate a prototype in any case, as this does not hurt but
288 * many modules otherwise seem to miss one, which generates compiler
289 * warnings.
290 */
291 #define BEGINobjDestruct(OBJ) \
292 rsRetVal OBJ##Destruct(OBJ##_t __attribute__((unused)) * *ppThis) { \
293 DEFiRet; \
294 OBJ##_t *pThis;
295
296 #define CODESTARTobjDestruct(OBJ) \
297 assert(ppThis != NULL); \
298 pThis = *ppThis; \
299 ISOBJ_TYPE_assert(pThis, OBJ);
300
301 /* note: there was a long-time bug in the macro below that lead to *ppThis = NULL
302 * only when the object was actually destructed. I discovered this issue during
303 * introduction of the pRcvFrom property in smsg_t, but it potentially had other
304 * effects, too. I am not sure if some experienced instability resulted from this
305 * bug OR if its fix will cause harm to so-far "correctly" running code. The later
306 * may very well be. Thus I will change it only for the current branch and also
307 * the beta, but not in all old builds. Let's see how things evolve.
308 * rgerhards, 2009-06-30
309 */
310 #define ENDobjDestruct(OBJ) \
311 goto finalize_it; /* prevent compiler warning ;) */ \
312 /* no more code here! */ \
313 finalize_it: \
314 if (pThis != NULL) { \
315 obj.DestructObjSelf((obj_t *)pThis); \
316 free(pThis); \
317 } \
318 *ppThis = NULL; \
319 RETiRet; \
320 }
321
322
323 /* this defines the debug print entry point. DebugPrint is optional. If
324 * it is provided, the object should output some meaningful information
325 * via the debug system.
326 * rgerhards, 2008-02-20
327 */
328 #define PROTOTYPEObjDebugPrint(obj) rsRetVal obj##DebugPrint(obj##_t *pThis)
329 #define INTERFACEObjDebugPrint(obj) rsRetVal (*DebugPrint)(obj##_t * pThis)
330 #define BEGINobjDebugPrint(obj) \
331 rsRetVal obj##DebugPrint(obj##_t __attribute__((unused)) * pThis) { \
332 DEFiRet;
333
334 #define CODESTARTobjDebugPrint(obj) \
335 assert(pThis != NULL); \
336 ISOBJ_TYPE_assert(pThis, obj);
337
338 #define ENDobjDebugPrint(obj) \
339 RETiRet; \
340 }
341
342 /* ------------------------------ object loader system ------------------------------ *
343 * The following code builds a dynamic object loader system. The
344 * root idea is that all objects are dynamically loadable,
345 * which is necessary to get a clean plug-in interface where every plugin can access
346 * rsyslog's rich object model via simple and quite portable methods.
347 *
348 * To do so, each object defines one or more interfaces. They are essentially structures
349 * with function (method) pointers. Anyone interested in calling an object must first
350 * obtain the interface and can then call through it.
351 *
352 * The interface data type must always be called <obj>_if_t, as this is expected
353 * by the macros. Having consitent naming is also easier for the programmer. By default,
354 * macros create a static variable named like the object in each calling objects
355 * static data block.
356 *
357 * rgerhards, 2008-02-21 (initial implementation), 2008-04-17 (update of this note)
358 */
359
360 /* this defines the QueryInterface print entry point. Over time, it should be
361 * present in all objects.
362 */
363 #define BEGINobjQueryInterface(obj) \
364 rsRetVal obj##QueryInterface(obj##_if_t *pIf) { \
365 DEFiRet;
366
367 #define CODESTARTobjQueryInterface(obj) assert(pIf != NULL);
368
369 #define ENDobjQueryInterface(obj) \
370 RETiRet; \
371 }
372
373 #define PROTOTYPEObjQueryInterface(obj) rsRetVal obj##QueryInterface(obj##_if_t *pIf)
374
375
376 /* the following macros should be used to define interfaces inside the
377 * header files.
378 */
379 #define BEGINinterface(obj) \
380 typedef struct obj##_if_s { \
381 ifBEGIN /* This MUST always be the first interface member */
382 #define ENDinterface(obj) \
383 } \
384 obj##_if_t;
385
386 /* the following macro is used to get access to an object (not an instance,
387 * just the class itself!). It must be called before any of the object's
388 * methods can be accessed. The MYLIB part is the name of my library, or NULL if
389 * the caller is a core module. Using the right value here is important to get
390 * the reference counting correct (object accesses from the same library must
391 * not be counted because that would cause a library plugin to never unload, as
392 * its ClassExit() entry points are only called if no object is referenced, which
393 * would never happen as the library references itself.
394 * rgerhards, 2008-03-11
395 */
396 #define CORE_COMPONENT NULL /* use this to indicate this is a core component */
397 #define DONT_LOAD_LIB NULL /* do not load a library to obtain object interface (currently same as CORE_COMPONENT) */
398 #define objUse(objName, FILENAME) obj.UseObj(__FILE__, (uchar *)#objName, (uchar *)FILENAME, (void *)&objName)
399 #define objRelease(objName, FILENAME) obj.ReleaseObj(__FILE__, (uchar *)#objName, (uchar *)FILENAME, (void *)&objName)
400
401 /* defines data that must always be present at the very begin of the interface structure */
402 #define ifBEGIN \
403 int ifVersion; /* must be set to version requested */ \
404 int ifIsLoaded; /* is the interface loaded? (0-no, 1-yes; if no, functions can NOT be called! */
405
406
407 /* use the following define some place in your static data (suggested right at
408 * the beginning
409 */
410 #define DEFobjCurrIf(obj) static obj##_if_t obj = {.ifVersion = obj##CURR_IF_VERSION, .ifIsLoaded = 0};
411
412 /* define the prototypes for a class - keep signatures behind PROTOTYPE*
413 * macros so interface changes are localized in this file.
414 */
415 #define PROTOTYPEObjBasic(obj) \
416 PROTOTYPEObjClassInit(obj); \
417 PROTOTYPEObjClassExit(obj); \
418 PROTOTYPEObjQueryInterface(obj)
419
420 /* use where typed object ctor/dtor declarations are valid in scope */
421 #define PROTOTYPEObjFull(obj) \
422 PROTOTYPEObjBasic(obj); \
423 PROTOTYPEobjConstruct(obj); \
424 PROTOTYPEobjDestruct(obj)
425
426 /* legacy alias kept for broad in-tree compatibility */
427 #define PROTOTYPEObj(obj) PROTOTYPEObjBasic(obj)
428
429 /* ------------------------------ end object loader system ------------------------------ */
430
431
432 #include "modules.h"
433
434 #endif /* #ifndef OBJ_TYPES_H_INCLUDED */