| 1 |
/* rsyslog.c - the main entry point into rsyslog's runtime library (RTL) |
| 2 |
* |
| 3 |
* This module contains all function which work on a RTL global level. It's |
| 4 |
* name is abbreviated to "rsrt" (rsyslog runtime). |
| 5 |
* |
| 6 |
* Please note that the runtime library tends to be plugin-safe. That is, it must be |
| 7 |
* initialized by calling a global initialization function. However, that |
| 8 |
* function checks if the library is already initialized and, if so, does |
| 9 |
* nothing except incrementing a refeence count. Similarly, the deinit |
| 10 |
* function does nothing as long as there are still other users (which |
| 11 |
* is tracked via the refcount). As such, it is safe to call init and |
| 12 |
* exit multiple times, as long as this are always matching calls. This |
| 13 |
* capability is needed for a plugin system, where one plugin never |
| 14 |
* knows what the other did. HOWEVER, as of this writing, not all runtime |
| 15 |
* library objects may work cleanly without static global data (the |
| 16 |
* debug system is a very good example of this). So while we aim at the |
| 17 |
* ability to work well in a plugin environment, things may not really work |
| 18 |
* out. If you intend to use the rsyslog runtime library inside plugins, |
| 19 |
* you should investigate the situation in detail. Please note that the |
| 20 |
* rsyslog project itself does not yet need this functionality - thus you |
| 21 |
* can safely assume it is totally untested ;). |
| 22 |
* |
| 23 |
* rgerhards, 2008-04-17: I have now once again checked on the plugin-safety. |
| 24 |
* Unfortunately, there is currently no hook at all with which we could |
| 25 |
* abstract a global data instance class. As such, we can NOT make the |
| 26 |
* runtime plugin-safe in the above-described sense. As the rsyslog |
| 27 |
* project itself does not need this functionality (and it is quesationable |
| 28 |
* if someone else ever will), we do currently do not make an effort to |
| 29 |
* support it. So if you intend to use rsyslog runtime inside a non-rsyslog |
| 30 |
* plugin system, be careful! |
| 31 |
* |
| 32 |
* The rsyslog runtime library is in general reentrant and thread-safe. There |
| 33 |
* are some intentional exceptions (e.g. inside the msg object). These are |
| 34 |
* documented. Any other threading and reentrency issue can be considered a bug. |
| 35 |
* |
| 36 |
* Module begun 2008-04-16 by Rainer Gerhards |
| 37 |
* |
| 38 |
* Copyright 2008-2016 Rainer Gerhards and Adiscon GmbH. |
| 39 |
* |
| 40 |
* This file is part of the rsyslog runtime library. |
| 41 |
* |
| 42 |
* The rsyslog runtime library is free software: you can redistribute it and/or modify |
| 43 |
* it under the terms of the GNU Lesser General Public License as published by |
| 44 |
* the Free Software Foundation, either version 3 of the License, or |
| 45 |
* (at your option) any later version. |
| 46 |
* |
| 47 |
* The rsyslog runtime library is distributed in the hope that it will be useful, |
| 48 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 49 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 50 |
* GNU Lesser General Public License for more details. |
| 51 |
* |
| 52 |
* You should have received a copy of the GNU Lesser General Public License |
| 53 |
* along with the rsyslog runtime library. If not, see <http://www.gnu.org/licenses/>. |
| 54 |
* |
| 55 |
* A copy of the GPL can be found in the file "COPYING" in this distribution. |
| 56 |
* A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution. |
| 57 |
*/ |
| 58 |
#include "config.h" |
| 59 |
#include <stdio.h> |
| 60 |
#include <stdlib.h> |
| 61 |
#include <assert.h> |
| 62 |
#ifdef ENABLE_LIBLOGGING_STDLOG |
| 63 |
#include <liblogging/stdlog.h> |
| 64 |
#endif |
| 65 |
|
| 66 |
#include "rsyslog.h" |
| 67 |
#include "obj.h" |
| 68 |
#include "stringbuf.h" |
| 69 |
#include "wti.h" |
| 70 |
#include "wtp.h" |
| 71 |
#include "datetime.h" |
| 72 |
#include "queue.h" |
| 73 |
#include "conf.h" |
| 74 |
#include "rsconf.h" |
| 75 |
#include "glbl.h" |
| 76 |
#include "errmsg.h" |
| 77 |
#include "prop.h" |
| 78 |
#include "ruleset.h" |
| 79 |
#include "parser.h" |
| 80 |
#include "lookup.h" |
| 81 |
#include "rswatch.h" |
| 82 |
#include "strgen.h" |
| 83 |
#include "statsobj.h" |
| 84 |
#include "atomic.h" |
| 85 |
#include "srUtils.h" |
| 86 |
|
| 87 |
pthread_attr_t default_thread_attr; |
| 88 |
#ifdef HAVE_PTHREAD_SETSCHEDPARAM |
| 89 |
struct sched_param default_sched_param; |
| 90 |
int default_thr_sched_policy; |
| 91 |
#endif |
| 92 |
|
| 93 |
/* globally visible static data - see comment in rsyslog.h for details */ |
| 94 |
uchar *glblModPath; /* module load path */ |
| 95 |
void (*glblErrLogger)(const int, const int, const uchar *) = dfltErrLogger; |
| 96 |
/* the error logger to use by the errmsg module */ |
| 97 |
|
| 98 |
/* static data */ |
| 99 |
static int iRefCount = 0; /* our refcount - it MUST exist only once inside a process (not thread) |
| 100 |
thus it is perfectly OK to use a static. MUST be initialized to 0! */ |
| 101 |
|
| 102 |
/* This is the default instance of the error logger. It simply writes the message |
| 103 |
* to stderr. It is expected that this is replaced by the runtime user very early |
| 104 |
* during startup (at least if the default is unsuitable). However, we provide a |
| 105 |
* default so that we can log errors during the initial phase, most importantly |
| 106 |
* during initialization. -- rgerhards. 2008-04-17 |
| 107 |
*/ |
| 108 |
void dfltErrLogger(const int severity, const int iErr, const uchar *errMsg) { |
| 109 |
fprintf(stderr, "rsyslog internal message (%d,%d): %s\n", severity, iErr, errMsg); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
/* set the error log function |
| 114 |
* rgerhards, 2008-04-18 |
| 115 |
*/ |
| 116 |
void rsrtSetErrLogger(void (*errLogger)(const int, const int, const uchar *)) { |
| 117 |
assert(errLogger != NULL); |
| 118 |
glblErrLogger = errLogger; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
/* globally initialze the runtime system |
| 123 |
* NOTE: this is NOT thread safe and must not be called concurrently. If that |
| 124 |
* ever poses a problem, we may use proper mutex calls - not considered needed yet. |
| 125 |
* If ppErrObj is provided, it receives a char pointer to the name of the object that |
| 126 |
* caused the problem (if one occurred). The caller must never free this pointer. If |
| 127 |
* ppErrObj is NULL, no such information will be provided. pObjIF is the pointer to |
| 128 |
* the "obj" object interface, which may be used to query any other rsyslog objects. |
| 129 |
* rgerhards, 2008-04-16 |
| 130 |
*/ |
| 131 |
rsRetVal rsrtInit(const char **ppErrObj, obj_if_t *pObjIF) { |
| 132 |
DEFiRet; |
| 133 |
int ret; |
| 134 |
char errstr[1024]; |
| 135 |
|
| 136 |
if (iRefCount == 0) { |
| 137 |
seedRandomNumber(); |
| 138 |
/* init runtime only if not yet done */ |
| 139 |
#ifdef ENABLE_LIBLOGGING_STDLOG |
| 140 |
stdlog_init(0); |
| 141 |
#endif |
| 142 |
ret = pthread_attr_init(&default_thread_attr); |
| 143 |
if (ret != 0) { |
| 144 |
rs_strerror_r(ret, errstr, sizeof(errstr)); |
| 145 |
fprintf(stderr, |
| 146 |
"rsyslogd: pthread_attr_init failed during " |
| 147 |
"startup - can not continue. Error was %s\n", |
| 148 |
errstr); |
| 149 |
exit(1); |
| 150 |
} |
| 151 |
pthread_attr_setstacksize(&default_thread_attr, 4096 * 1024); |
| 152 |
#ifdef HAVE_PTHREAD_SETSCHEDPARAM |
| 153 |
ret = pthread_getschedparam(pthread_self(), &default_thr_sched_policy, &default_sched_param); |
| 154 |
if (ret != 0) { |
| 155 |
rs_strerror_r(ret, errstr, sizeof(errstr)); |
| 156 |
fprintf(stderr, |
| 157 |
"rsyslogd: pthread_getschedparam failed during " |
| 158 |
"startup - ignoring. Error was %s\n", |
| 159 |
errstr); |
| 160 |
default_thr_sched_policy = 0; /* should be default on all platforms */ |
| 161 |
} |
| 162 |
#if defined(_AIX) |
| 163 |
pthread_attr_setstacksize(&default_thread_attr, 4096 * 512); |
| 164 |
#endif |
| 165 |
|
| 166 |
|
| 167 |
ret = pthread_attr_setschedpolicy(&default_thread_attr, default_thr_sched_policy); |
| 168 |
if (ret != 0) { |
| 169 |
rs_strerror_r(ret, errstr, sizeof(errstr)); |
| 170 |
fprintf(stderr, |
| 171 |
"rsyslogd: pthread_attr_setschedpolicy failed during " |
| 172 |
"startup - tried to set priority %d, now using default " |
| 173 |
"priority instead. Error was: %s\n", |
| 174 |
default_thr_sched_policy, errstr); |
| 175 |
} |
| 176 |
ret = pthread_attr_setschedparam(&default_thread_attr, &default_sched_param); |
| 177 |
if (ret != 0) { |
| 178 |
rs_strerror_r(ret, errstr, sizeof(errstr)); |
| 179 |
fprintf(stderr, |
| 180 |
"rsyslogd: pthread_attr_setschedparam failed during " |
| 181 |
"startup - ignored Error was: %s\n", |
| 182 |
errstr); |
| 183 |
} |
| 184 |
ret = pthread_attr_setinheritsched(&default_thread_attr, PTHREAD_EXPLICIT_SCHED); |
| 185 |
if (ret != 0) { |
| 186 |
rs_strerror_r(ret, errstr, sizeof(errstr)); |
| 187 |
fprintf(stderr, |
| 188 |
"rsyslogd: pthread_attr_setinheritsched failed during " |
| 189 |
"startup - ignoring. Error was: %s\n", |
| 190 |
errstr); |
| 191 |
} |
| 192 |
#endif |
| 193 |
if (ppErrObj != NULL) *ppErrObj = "obj"; |
| 194 |
CHKiRet(objClassInit(NULL)); /* *THIS* *MUST* always be the first class initilizer being called! */ |
| 195 |
CHKiRet(objGetObjInterface(pObjIF)); /* this provides the root pointer for all other queries */ |
| 196 |
|
| 197 |
/* initialize core classes. We must be very careful with the order of events. Some |
| 198 |
* classes use others and if we do not initialize them in the right order, we may end |
| 199 |
* up with an invalid call. The most important thing that can happen is that an error |
| 200 |
* is detected and needs to be logged, wich in turn requires a broader number of classes |
| 201 |
* to be available. The solution is that we take care in the order of calls AND use a |
| 202 |
* class immediately after it is initialized. And, of course, we load those classes |
| 203 |
* first that we use ourselfs... -- rgerhards, 2008-03-07 |
| 204 |
*/ |
| 205 |
if (ppErrObj != NULL) *ppErrObj = "statsobj"; |
| 206 |
CHKiRet(statsobjClassInit(NULL)); |
| 207 |
if (ppErrObj != NULL) *ppErrObj = "prop"; |
| 208 |
CHKiRet(propClassInit(NULL)); |
| 209 |
if (ppErrObj != NULL) *ppErrObj = "glbl"; |
| 210 |
CHKiRet(glblClassInit(NULL)); |
| 211 |
if (ppErrObj != NULL) *ppErrObj = "msg"; |
| 212 |
CHKiRet(msgClassInit(NULL)); |
| 213 |
if (ppErrObj != NULL) *ppErrObj = "ruleset"; |
| 214 |
CHKiRet(rulesetClassInit(NULL)); |
| 215 |
if (ppErrObj != NULL) *ppErrObj = "wti"; |
| 216 |
CHKiRet(wtiClassInit(NULL)); |
| 217 |
if (ppErrObj != NULL) *ppErrObj = "wtp"; |
| 218 |
CHKiRet(wtpClassInit(NULL)); |
| 219 |
if (ppErrObj != NULL) *ppErrObj = "queue"; |
| 220 |
CHKiRet(qqueueClassInit(NULL)); |
| 221 |
if (ppErrObj != NULL) *ppErrObj = "conf"; |
| 222 |
CHKiRet(confClassInit(NULL)); |
| 223 |
if (ppErrObj != NULL) *ppErrObj = "parser"; |
| 224 |
CHKiRet(parserClassInit(NULL)); |
| 225 |
if (ppErrObj != NULL) *ppErrObj = "strgen"; |
| 226 |
CHKiRet(strgenClassInit(NULL)); |
| 227 |
if (ppErrObj != NULL) *ppErrObj = "rsconf"; |
| 228 |
CHKiRet(rsconfClassInit(NULL)); |
| 229 |
if (ppErrObj != NULL) *ppErrObj = "lookup"; |
| 230 |
CHKiRet(lookupClassInit()); |
| 231 |
if (ppErrObj != NULL) *ppErrObj = "dynstats"; |
| 232 |
CHKiRet(dynstatsClassInit()); |
| 233 |
if (ppErrObj != NULL) *ppErrObj = "perctile_stats"; |
| 234 |
CHKiRet(perctileClassInit()); |
| 235 |
|
| 236 |
/* dummy "classes" */ |
| 237 |
if (ppErrObj != NULL) *ppErrObj = "str"; |
| 238 |
CHKiRet(strInit()); |
| 239 |
} |
| 240 |
|
| 241 |
++iRefCount; |
| 242 |
dbgprintf("rsyslog runtime initialized, version %s, current users %d\n", VERSION, iRefCount); |
| 243 |
|
| 244 |
finalize_it: |
| 245 |
RETiRet; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
/* globally de-initialze the runtime system |
| 250 |
* NOTE: this is NOT thread safe and must not be called concurrently. If that |
| 251 |
* ever poses a problem, we may use proper mutex calls - not considered needed yet. |
| 252 |
* This function must be provided with the caller's obj object pointer. This is |
| 253 |
* automatically deinitialized by the runtime system. |
| 254 |
* rgerhards, 2008-04-16 |
| 255 |
*/ |
| 256 |
rsRetVal rsrtExit(void) { |
| 257 |
DEFiRet; |
| 258 |
|
| 259 |
if (iRefCount == 1) { |
| 260 |
/* do actual de-init only if we are the last runtime user */ |
| 261 |
rswatchExit(); |
| 262 |
confClassExit(); |
| 263 |
glblClassExit(); |
| 264 |
rulesetClassExit(); |
| 265 |
wtiClassExit(); |
| 266 |
wtpClassExit(); |
| 267 |
strgenClassExit(); |
| 268 |
propClassExit(); |
| 269 |
statsobjClassExit(); |
| 270 |
|
| 271 |
objClassExit(); /* *THIS* *MUST/SHOULD?* always be the first class initilizer being |
| 272 |
called (except debug)! */ |
| 273 |
} |
| 274 |
|
| 275 |
--iRefCount; |
| 276 |
/* TODO we must deinit this pointer! pObjIF = NULL; / * no longer exists for this caller */ |
| 277 |
|
| 278 |
dbgprintf("rsyslog runtime de-initialized, current users %d\n", iRefCount); |
| 279 |
|
| 280 |
RETiRet; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
/* returns 0 if the rsyslog runtime is not initialized and another value |
| 285 |
* if it is. This function is primarily meant to be used by runtime functions |
| 286 |
* itself. However, it is safe to call it before initializing the runtime. |
| 287 |
* Plugins should NOT rely on this function. The reason is that another caller |
| 288 |
* may have already initialized it but deinits it before this plugin is done. |
| 289 |
* So for plugins and like architectures, the right course of action is to |
| 290 |
* call rsrtInit() and rsrtExit(), which can be called by multiple callers. |
| 291 |
* rgerhards, 2008-04-16 |
| 292 |
*/ |
| 293 |
int rsrtIsInit(void) { |
| 294 |
return iRefCount; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
/* vim:set ai: |
| 299 |
*/ |