| 1 |
/* The errmsg object. |
| 2 |
* |
| 3 |
* Module begun 2008-03-05 by Rainer Gerhards, based on some code |
| 4 |
* from syslogd.c. I converted this module to lgpl and have checked that |
| 5 |
* all contributors agreed to that step. |
| 6 |
* Now moving to ASL 2.0, and contributor checks tell that there is no need |
| 7 |
* to take further case, as the code now boils to be either my own or, a few lines, |
| 8 |
* of the original BSD-licenses sysklogd code. rgerhards, 2012-01-16 |
| 9 |
* |
| 10 |
* Copyright 2008-2018 Adiscon GmbH. |
| 11 |
* |
| 12 |
* This file is part of the rsyslog runtime library. |
| 13 |
* |
| 14 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 15 |
* you may not use this file except in compliance with the License. |
| 16 |
* You may obtain a copy of the License at |
| 17 |
* |
| 18 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 19 |
* -or- |
| 20 |
* see COPYING.ASL20 in the source distribution |
| 21 |
* |
| 22 |
* Unless required by applicable law or agreed to in writing, software |
| 23 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 24 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 |
* See the License for the specific language governing permissions and |
| 26 |
* limitations under the License. |
| 27 |
*/ |
| 28 |
|
| 29 |
#include "config.h" |
| 30 |
#include <stdio.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <stdarg.h> |
| 33 |
#include <errno.h> |
| 34 |
#include <assert.h> |
| 35 |
#include <sys/types.h> |
| 36 |
#include <sys/stat.h> |
| 37 |
#include <fcntl.h> |
| 38 |
#include <string.h> |
| 39 |
#include <unistd.h> |
| 40 |
|
| 41 |
#include "rsyslog.h" |
| 42 |
#include "obj.h" |
| 43 |
#include "msg.h" |
| 44 |
#include "errmsg.h" |
| 45 |
#include "operatingstate.h" |
| 46 |
#include "srUtils.h" |
| 47 |
#include "stringbuf.h" |
| 48 |
#include "rsconf.h" |
| 49 |
|
| 50 |
/* static data */ |
| 51 |
#ifndef O_LARGEFILE |
| 52 |
#define O_LARGEFILE 0 |
| 53 |
#endif |
| 54 |
|
| 55 |
static int bHadErrMsgs; /* indicates if we had error messages since reset of this flag |
| 56 |
* This is used to abort a run if the config is unclean. |
| 57 |
*/ |
| 58 |
|
| 59 |
static int fdOversizeMsgLog = -1; |
| 60 |
static pthread_mutex_t oversizeMsgLogMut = PTHREAD_MUTEX_INITIALIZER; |
| 61 |
|
| 62 |
/* ------------------------------ methods ------------------------------ */ |
| 63 |
|
| 64 |
/* Resets the error message flag. Must be done before processing config |
| 65 |
* files. |
| 66 |
*/ |
| 67 |
void resetErrMsgsFlag(void) { |
| 68 |
bHadErrMsgs = 0; |
| 69 |
} |
| 70 |
|
| 71 |
int hadErrMsgs(void) { |
| 72 |
return bHadErrMsgs; |
| 73 |
} |
| 74 |
|
| 75 |
/** @internal |
| 76 |
* Build and emit a single diagnostic line. |
| 77 |
* |
| 78 |
* - If iErrno != 0, append strerror(iErrno). |
| 79 |
* - If iErrCode is neither NO_ERRCODE nor RS_RET_ERR, append a help URL. |
| 80 |
* - Truncate to the configured max line length. |
| 81 |
* - Set the "had error" flag for LOG_ERR. |
| 82 |
*/ |
| 83 |
static void doLogMsg(const int iErrno, const int iErrCode, const int severity, const char *msg) { |
| 84 |
char buf[2048]; |
| 85 |
char errStr[1024]; |
| 86 |
|
| 87 |
dbgprintf("Called LogMsg, msg: %s\n", msg); |
| 88 |
osf_write(OSF_TAG_MSG, msg); |
| 89 |
|
| 90 |
if (iErrno != 0) { |
| 91 |
rs_strerror_r(iErrno, errStr, sizeof(errStr)); |
| 92 |
if (iErrCode == NO_ERRCODE || iErrCode == RS_RET_ERR) { |
| 93 |
snprintf(buf, sizeof(buf), "%s: %s [v%s]", msg, errStr, VERSION); |
| 94 |
} else { |
| 95 |
snprintf(buf, sizeof(buf), "%s: %s [v%s try https://www.rsyslog.com/e/%d ]", msg, errStr, VERSION, |
| 96 |
iErrCode * -1); |
| 97 |
} |
| 98 |
} else { |
| 99 |
if (iErrCode == NO_ERRCODE || iErrCode == RS_RET_ERR) { |
| 100 |
snprintf(buf, sizeof(buf), "%s [v%s]", msg, VERSION); |
| 101 |
} else { |
| 102 |
snprintf(buf, sizeof(buf), "%s [v%s try https://www.rsyslog.com/e/%d ]", msg, VERSION, iErrCode * -1); |
| 103 |
} |
| 104 |
} |
| 105 |
buf[sizeof(buf) - 1] = '\0'; /* just to be on the safe side... */ |
| 106 |
errno = 0; |
| 107 |
|
| 108 |
const int msglen = (int)strlen(buf); |
| 109 |
if (msglen > glblGetMaxLine(ourConf)) { |
| 110 |
/* in extreme cases, our error messages may be longer than the configured |
| 111 |
* max message size. If so, we just truncate without further indication, as |
| 112 |
* anything else would probably lead to a death loop on error messages. |
| 113 |
* Note that we do not split, as we really do not anticipate there is |
| 114 |
* much value in supporting extremely short max message sizes - we assume |
| 115 |
* it's just a testbench thing. -- rgerhards, 2018-05-11 |
| 116 |
*/ |
| 117 |
buf[glblGetMaxLine(ourConf)] = '\0'; /* space must be available! */ |
| 118 |
} |
| 119 |
|
| 120 |
glblErrLogger(severity, iErrCode, (uchar *)buf); |
| 121 |
|
| 122 |
if (severity == LOG_ERR) bHadErrMsgs = 1; |
| 123 |
} |
| 124 |
|
| 125 |
/* We now receive three parameters: one is the internal error code |
| 126 |
* which will also become the error message number, the second is |
| 127 |
* errno - if it is non-zero, the corresponding error message is included |
| 128 |
* in the text and finally the message text itself. Note that it is not |
| 129 |
* 100% clean to use the internal errcode, as it may be reached from |
| 130 |
* multiple actual error causes. However, it is much better than having |
| 131 |
* no error code at all (and in most cases, a single internal error code |
| 132 |
* maps to a specific error event). |
| 133 |
* rgerhards, 2008-06-27 |
| 134 |
*/ |
| 135 |
void __attribute__((format(printf, 3, 4))) LogError(const int iErrno, const int iErrCode, const char *fmt, ...) { |
| 136 |
va_list ap; |
| 137 |
char buf[2048]; |
| 138 |
int lenBuf; |
| 139 |
|
| 140 |
va_start(ap, fmt); |
| 141 |
lenBuf = vsnprintf(buf, sizeof(buf), fmt, ap); |
| 142 |
if (lenBuf < 0) { |
| 143 |
strncpy(buf, "error message lost due to problem with vsnprintf", sizeof(buf)); |
| 144 |
} |
| 145 |
va_end(ap); |
| 146 |
buf[sizeof(buf) - 1] = '\0'; /* just to be on the safe side... */ |
| 147 |
|
| 148 |
doLogMsg(iErrno, iErrCode, LOG_ERR, buf); |
| 149 |
} |
| 150 |
|
| 151 |
/* We now receive three parameters: one is the internal error code |
| 152 |
* which will also become the error message number, the second is |
| 153 |
* errno - if it is non-zero, the corresponding error message is included |
| 154 |
* in the text and finally the message text itself. Note that it is not |
| 155 |
* 100% clean to use the internal errcode, as it may be reached from |
| 156 |
* multiple actual error causes. However, it is much better than having |
| 157 |
* no error code at all (and in most cases, a single internal error code |
| 158 |
* maps to a specific error event). |
| 159 |
* rgerhards, 2008-06-27 |
| 160 |
*/ |
| 161 |
void __attribute__((format(printf, 4, 5))) LogMsg( |
| 162 |
const int iErrno, const int iErrCode, const int severity, const char *fmt, ...) { |
| 163 |
va_list ap; |
| 164 |
char buf[2048]; |
| 165 |
int lenBuf; |
| 166 |
|
| 167 |
va_start(ap, fmt); |
| 168 |
lenBuf = vsnprintf(buf, sizeof(buf), fmt, ap); |
| 169 |
if (lenBuf < 0) { |
| 170 |
strncpy(buf, "error message lost due to problem with vsnprintf", sizeof(buf)); |
| 171 |
} |
| 172 |
va_end(ap); |
| 173 |
buf[sizeof(buf) - 1] = '\0'; /* just to be on the safe side... */ |
| 174 |
|
| 175 |
doLogMsg(iErrno, iErrCode, severity, buf); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
/* Write an oversize message to the oversize message error log. |
| 180 |
* We do NOT handle errors during writing that log other than emitting |
| 181 |
* yet another error message. The reason is that there really is nothing |
| 182 |
* else that we could do in that case. |
| 183 |
* rgerhards, 2018-05-03 |
| 184 |
*/ |
| 185 |
rsRetVal ATTR_NONNULL() writeOversizeMessageLog(const smsg_t *const pMsg) { |
| 186 |
struct json_object *json = NULL; |
| 187 |
char *rendered = NULL; |
| 188 |
struct json_object *jval; |
| 189 |
uchar *buf; |
| 190 |
size_t toWrite; |
| 191 |
ssize_t wrRet; |
| 192 |
int dummy; |
| 193 |
int mutexLocked = 0; |
| 194 |
DEFiRet; |
| 195 |
ISOBJ_TYPE_assert(pMsg, msg); |
| 196 |
|
| 197 |
if (glblGetOversizeMsgErrorFile(runConf) == NULL) { |
| 198 |
FINALIZE; |
| 199 |
} |
| 200 |
|
| 201 |
pthread_mutex_lock(&oversizeMsgLogMut); |
| 202 |
mutexLocked = 1; |
| 203 |
|
| 204 |
if (fdOversizeMsgLog == -1) { |
| 205 |
fdOversizeMsgLog = |
| 206 |
open((char *)glblGetOversizeMsgErrorFile(runConf), O_WRONLY | O_CREAT | O_APPEND | O_LARGEFILE | O_CLOEXEC, |
| 207 |
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 208 |
if (fdOversizeMsgLog == -1) { |
| 209 |
LogError(errno, RS_RET_ERR, "error opening oversize message log file %s", |
| 210 |
glblGetOversizeMsgErrorFile(runConf)); |
| 211 |
FINALIZE; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
assert(fdOversizeMsgLog != -1); |
| 216 |
json = json_object_new_object(); |
| 217 |
if (json == NULL) { |
| 218 |
FINALIZE; |
| 219 |
} |
| 220 |
|
| 221 |
getRawMsg(pMsg, &buf, &dummy); |
| 222 |
jval = json_object_new_string((char *)buf); |
| 223 |
json_object_object_add(json, "rawmsg", jval); |
| 224 |
|
| 225 |
getInputName(pMsg, &buf, &dummy); |
| 226 |
jval = json_object_new_string((char *)buf); |
| 227 |
json_object_object_add(json, "input", jval); |
| 228 |
|
| 229 |
CHKmalloc(rendered = strdup((char *)fjson_object_to_json_string(json))); |
| 230 |
|
| 231 |
toWrite = strlen(rendered) + 1; |
| 232 |
/* Note: we overwrite the '\0' terminator with '\n' -- so we avoid |
| 233 |
* calling malloc() -- write() does NOT need '\0'! |
| 234 |
*/ |
| 235 |
rendered[toWrite - 1] = '\n'; /* NO LONGER A STRING! */ |
| 236 |
wrRet = write(fdOversizeMsgLog, rendered, toWrite); |
| 237 |
if (wrRet != (ssize_t)toWrite) { |
| 238 |
LogError(errno, RS_RET_IO_ERROR, "error writing oversize message log file %s, write returned %lld", |
| 239 |
glblGetOversizeMsgErrorFile(runConf), (long long)wrRet); |
| 240 |
} |
| 241 |
|
| 242 |
finalize_it: |
| 243 |
free(rendered); |
| 244 |
if (mutexLocked) { |
| 245 |
pthread_mutex_unlock(&oversizeMsgLogMut); |
| 246 |
} |
| 247 |
if (json != NULL) { |
| 248 |
fjson_object_put(json); |
| 249 |
} |
| 250 |
RETiRet; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
void errmsgDoHUP(void) { |
| 255 |
pthread_mutex_lock(&oversizeMsgLogMut); |
| 256 |
if (fdOversizeMsgLog != -1) { |
| 257 |
close(fdOversizeMsgLog); |
| 258 |
fdOversizeMsgLog = -1; |
| 259 |
} |
| 260 |
pthread_mutex_unlock(&oversizeMsgLogMut); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
void errmsgExit(void) { |
| 265 |
if (fdOversizeMsgLog != -1) { |
| 266 |
close(fdOversizeMsgLog); |
| 267 |
} |
| 268 |
} |