mirror of
https://github.com/rsyslog/rsyslog.git
synced 2025-12-20 05:00:42 +01:00
Merge pull request #1210 from rgerhards/i-1209
rsyslog.conf: add ability to set environment variables
This commit is contained in:
commit
7a930d791b
@ -37,6 +37,7 @@
|
||||
#include <ctype.h>
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "rsyslog.h"
|
||||
#include "obj.h"
|
||||
@ -49,6 +50,7 @@
|
||||
#include "action.h"
|
||||
#include "parserif.h"
|
||||
#include "rainerscript.h"
|
||||
#include "srUtils.h"
|
||||
#include "net.h"
|
||||
#include "rsconf.h"
|
||||
|
||||
@ -167,6 +169,7 @@ static struct cnfparamdescr cnfparamdescr[] = {
|
||||
{ "net.aclresolvehostname", eCmdHdlrBinary, 0 },
|
||||
{ "net.enabledns", eCmdHdlrBinary, 0 },
|
||||
{ "net.permitACLwarning", eCmdHdlrBinary, 0 },
|
||||
{ "environment", eCmdHdlrArray, 0 },
|
||||
{ "processinternalmessages", eCmdHdlrBinary, 0 }
|
||||
};
|
||||
static struct cnfparamblk paramblk =
|
||||
@ -1022,6 +1025,47 @@ qs_arrcmp_tzinfo(const void *s1, const void *s2)
|
||||
return strcmp(((tzinfo_t*)s1)->id, ((tzinfo_t*)s2)->id);
|
||||
}
|
||||
|
||||
/* set an environment variable */
|
||||
static rsRetVal
|
||||
do_setenv(const char *const var)
|
||||
{
|
||||
char varname[128];
|
||||
const char *val = var;
|
||||
size_t i;
|
||||
DEFiRet;
|
||||
|
||||
for(i = 0 ; *val != '=' ; ++i, ++val) {
|
||||
if(i == sizeof(varname)-i) {
|
||||
parser_errmsg("environment variable name too long "
|
||||
"[max %zd chars] or malformed entry: '%s'",
|
||||
sizeof(varname)-1, var);
|
||||
ABORT_FINALIZE(RS_RET_ERR_SETENV);
|
||||
}
|
||||
if(*val == '\0') {
|
||||
parser_errmsg("environment variable entry is missing "
|
||||
"equal sign (for value): '%s'", var);
|
||||
ABORT_FINALIZE(RS_RET_ERR_SETENV);
|
||||
}
|
||||
varname[i] = *val;
|
||||
}
|
||||
varname[i] = '\0';
|
||||
++val;
|
||||
DBGPRINTF("do_setenv, var '%s', val '%s'\n", varname, val);
|
||||
|
||||
if(setenv(varname, val, 1) != 0) {
|
||||
char errStr[1024];
|
||||
rs_strerror_r(errno, errStr, sizeof(errStr));
|
||||
parser_errmsg("error setting environment variable "
|
||||
"'%s' to '%s': %s", varname, val, errStr);
|
||||
ABORT_FINALIZE(RS_RET_ERR_SETENV);
|
||||
}
|
||||
|
||||
|
||||
finalize_it:
|
||||
RETiRet;
|
||||
}
|
||||
|
||||
|
||||
/* This processes the "regular" parameters which are to be set after the
|
||||
* config has been fully loaded.
|
||||
*/
|
||||
@ -1139,6 +1183,13 @@ glblDoneLoadCnf(void)
|
||||
setDisableDNS(!((int) cnfparamvals[i].val.d.n));
|
||||
} else if(!strcmp(paramblk.descr[i].name, "net.permitwarning")) {
|
||||
setOption_DisallowWarning(!((int) cnfparamvals[i].val.d.n));
|
||||
} else if(!strcmp(paramblk.descr[i].name, "environment")) {
|
||||
for(int j = 0 ; j < cnfparamvals[i].val.d.ar->nmemb ; ++j) {
|
||||
char *const var =
|
||||
es_str2cstr(cnfparamvals[i].val.d.ar->arr[j], NULL);
|
||||
do_setenv(var);
|
||||
free(var);
|
||||
}
|
||||
} else {
|
||||
dbgprintf("glblDoneLoadCnf: program error, non-handled "
|
||||
"param '%s'\n", paramblk.descr[i].name);
|
||||
|
||||
@ -460,6 +460,7 @@ enum rsRetVal_ /** return value. All methods return this if not specified oth
|
||||
RS_RET_FILE_OPEN_ERROR = -2433, /**< error other than "not found" occured during open() */
|
||||
RS_RET_FILE_CHOWN_ERROR = -2434, /**< error during chown() */
|
||||
RS_RET_RENAME_TMP_QI_ERROR = -2435, /**< renaming temporary .qi file failed */
|
||||
RS_RET_ERR_SETENV = -2436, /**< error setting an environment variable */
|
||||
|
||||
/* RainerScript error messages (range 1000.. 1999) */
|
||||
RS_RET_SYSVAR_NOT_FOUND = 1001, /**< system variable could not be found (maybe misspelled) */
|
||||
|
||||
@ -21,6 +21,10 @@ TESTS += \
|
||||
stop-msgvar.sh \
|
||||
glbl-unloadmodules.sh \
|
||||
glbl-invld-param.sh \
|
||||
glbl_setenv_2_vars.sh \
|
||||
glbl_setenv_err.sh \
|
||||
glbl_setenv_err_too_long.sh \
|
||||
glbl_setenv.sh \
|
||||
invalid_nested_include.sh \
|
||||
omfwd-keepalive.sh \
|
||||
omfile-read-only.sh \
|
||||
@ -529,6 +533,10 @@ EXTRA_DIST= \
|
||||
hostname-with-slash-dflt-slash-valid.sh \
|
||||
glbl-unloadmodules.sh \
|
||||
glbl-invld-param.sh \
|
||||
glbl_setenv_2_vars.sh \
|
||||
glbl_setenv_err.sh \
|
||||
glbl_setenv_err_too_long.sh \
|
||||
glbl_setenv.sh \
|
||||
1.rstest 2.rstest 3.rstest err1.rstest \
|
||||
invalid_nested_include.sh \
|
||||
validation-run.sh \
|
||||
|
||||
26
tests/glbl_setenv.sh
Executable file
26
tests/glbl_setenv.sh
Executable file
@ -0,0 +1,26 @@
|
||||
#!/bin/bash
|
||||
# This is part of the rsyslog testbench, licensed under ASL 2.0
|
||||
. $srcdir/diag.sh init
|
||||
. $srcdir/diag.sh generate-conf
|
||||
. $srcdir/diag.sh add-conf '
|
||||
global(environment="http_proxy=http://127.0.0.1")
|
||||
|
||||
set $!prx = getenv("http_proxy");
|
||||
|
||||
template(name="outfmt" type="string" string="%$!prx%\n")
|
||||
:msg, contains, "msgnum:" action(type="omfile" template="outfmt"
|
||||
file="rsyslog.out.log")
|
||||
'
|
||||
. $srcdir/diag.sh startup
|
||||
. $srcdir/diag.sh injectmsg 0 1
|
||||
. $srcdir/diag.sh shutdown-when-empty # shut down rsyslogd when done processing messages
|
||||
. $srcdir/diag.sh wait-shutdown # we need to wait until rsyslogd is finished!
|
||||
|
||||
echo 'http://127.0.0.1' | cmp rsyslog.out.log
|
||||
if [ ! $? -eq 0 ]; then
|
||||
echo "invalid content seen, rsyslog.out.log is:"
|
||||
cat rsyslog.out.log
|
||||
. $srcdir/diag.sh error-exit 1
|
||||
fi;
|
||||
|
||||
. $srcdir/diag.sh exit
|
||||
27
tests/glbl_setenv_2_vars.sh
Executable file
27
tests/glbl_setenv_2_vars.sh
Executable file
@ -0,0 +1,27 @@
|
||||
#!/bin/bash
|
||||
# This is part of the rsyslog testbench, licensed under ASL 2.0
|
||||
. $srcdir/diag.sh init
|
||||
. $srcdir/diag.sh generate-conf
|
||||
. $srcdir/diag.sh add-conf '
|
||||
global(environment=["http_proxy=http://127.0.0.1", "SECOND=OK OK"])
|
||||
|
||||
set $!prx = getenv("http_proxy");
|
||||
set $!second = getenv("SECOND");
|
||||
|
||||
template(name="outfmt" type="string" string="%$!prx%, %$!second%\n")
|
||||
:msg, contains, "msgnum:" action(type="omfile" template="outfmt"
|
||||
file="rsyslog.out.log")
|
||||
'
|
||||
. $srcdir/diag.sh startup
|
||||
. $srcdir/diag.sh injectmsg 0 1
|
||||
. $srcdir/diag.sh shutdown-when-empty # shut down rsyslogd when done processing messages
|
||||
. $srcdir/diag.sh wait-shutdown # we need to wait until rsyslogd is finished!
|
||||
|
||||
echo 'http://127.0.0.1, OK OK' | cmp rsyslog.out.log
|
||||
if [ ! $? -eq 0 ]; then
|
||||
echo "invalid content seen, rsyslog.out.log is:"
|
||||
cat rsyslog.out.log
|
||||
. $srcdir/diag.sh error-exit 1
|
||||
fi;
|
||||
|
||||
. $srcdir/diag.sh exit
|
||||
25
tests/glbl_setenv_err.sh
Executable file
25
tests/glbl_setenv_err.sh
Executable file
@ -0,0 +1,25 @@
|
||||
#!/bin/bash
|
||||
# This is part of the rsyslog testbench, licensed under ASL 2.0
|
||||
. $srcdir/diag.sh init
|
||||
. $srcdir/diag.sh generate-conf
|
||||
. $srcdir/diag.sh add-conf '
|
||||
# env var is missing equal sign and MUST trigger parsing error!
|
||||
global(environment="http_proxy ERROR")
|
||||
|
||||
action(type="omfile" file="rsyslog.out.log")
|
||||
'
|
||||
. $srcdir/diag.sh startup
|
||||
. $srcdir/diag.sh injectmsg 0 1
|
||||
. $srcdir/diag.sh shutdown-when-empty # shut down rsyslogd when done processing messages
|
||||
. $srcdir/diag.sh wait-shutdown # we need to wait until rsyslogd is finished!
|
||||
|
||||
grep "http_proxy ERROR" < rsyslog.out.log
|
||||
if [ ! $? -eq 0 ]; then
|
||||
echo
|
||||
echo "MESSAGE INDICATING ERROR ON ENVIRONMENT VARIABLE IS MISSING:"
|
||||
echo
|
||||
cat rsyslog.out.log
|
||||
. $srcdir/diag.sh error-exit 1
|
||||
fi;
|
||||
|
||||
. $srcdir/diag.sh exit
|
||||
25
tests/glbl_setenv_err_too_long.sh
Executable file
25
tests/glbl_setenv_err_too_long.sh
Executable file
@ -0,0 +1,25 @@
|
||||
#!/bin/bash
|
||||
# This is part of the rsyslog testbench, licensed under ASL 2.0
|
||||
. $srcdir/diag.sh init
|
||||
. $srcdir/diag.sh generate-conf
|
||||
. $srcdir/diag.sh add-conf '
|
||||
# name is 400 chars long --> too long
|
||||
global(environment="NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN=400")
|
||||
|
||||
action(type="omfile" file="rsyslog.out.log")
|
||||
'
|
||||
. $srcdir/diag.sh startup
|
||||
. $srcdir/diag.sh injectmsg 0 1
|
||||
. $srcdir/diag.sh shutdown-when-empty # shut down rsyslogd when done processing messages
|
||||
. $srcdir/diag.sh wait-shutdown # we need to wait until rsyslogd is finished!
|
||||
|
||||
grep "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN" < rsyslog.out.log > /dev/null
|
||||
if [ ! $? -eq 0 ]; then
|
||||
echo
|
||||
echo "MESSAGE INDICATING ERROR ON ENVIRONMENT VARIABLE IS MISSING:"
|
||||
echo
|
||||
cat rsyslog.out.log
|
||||
. $srcdir/diag.sh error-exit 1
|
||||
fi;
|
||||
|
||||
. $srcdir/diag.sh exit
|
||||
Loading…
x
Reference in New Issue
Block a user