Debian Codegraph @

runtime/errmsg.h

raw
1 /* The errmsg object. It is used to emit error message inside rsyslog.
2 *
3 * Copyright 2008-2018 Rainer Gerhards and Adiscon GmbH.
4 *
5 * This file is part of the rsyslog runtime library.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 * -or-
13 * see COPYING.ASL20 in the source distribution
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21 #ifndef INCLUDED_ERRMSG_H
22 #define INCLUDED_ERRMSG_H
23
24 #include "obj-types.h"
25
26 #define NO_ERRCODE -1
27
28 /* prototypes */
29 /**
30 * @brief Close and reset internal resources of the errmsg subsystem.
31 *
32 * Closes the oversize-message log fd if open. Intended for orderly shutdown.
33 * @note This function is not thread-safe as it accesses a global flag without locks.
34 */
35 void errmsgExit(void);
36
37 /**
38 * @brief Reopen oversize-message resources on configuration reload (HUP).
39 *
40 * Intended to be called from rsyslog's SIGHUP handler. This closes the
41 * oversize-message log file descriptor so the next write will reopen it
42 * using the current configuration. This follows the traditional syslogd
43 * pattern of reloading or reopening resources on HUP.
44 *
45 * Thread-safe.
46 */
47 void errmsgDoHUP(void);
48
49 /**
50 * @brief Reset the "had error messages" flag.
51 *
52 * Must be called before processing config files to start with a clean state.
53 * @note This function is not thread-safe as it accesses a global flag without locks.
54 */
55 void resetErrMsgsFlag(void);
56
57 /**
58 * @brief Query whether a LOG_ERR message has been logged since the last reset.
59 *
60 * @return nonzero if a LOG_ERR was emitted since resetErrMsgsFlag(), else 0.
61 *
62 * @note This function is not thread-safe as it accesses a global flag without locks.
63 */
64 int hadErrMsgs(void);
65
66 /**
67 * @brief Log an error message with errno and error code context.
68 *
69 * Formats and writes an error message to the rsyslog internal log.
70 * Use this for reporting fatal or unexpected conditions.
71 *
72 * @param iErrno The system errno value (0 if not applicable).
73 * @param iErrCode Internal rsyslog error code.
74 * @param fmt printf-style format string.
75 * @param ... Arguments matching the format string.
76 *
77 * @note Pass the errno value obtained at the point of failure.
78 * Do not pass 0 and later resolve errno via strerror() or
79 * similar functions — this leads to incorrect messages.
80 * AI AGENTS: Always use LogError(errno, ...) when reporting
81 * system errors. Do not use strerror() or rs_strerror_r().
82 */
83 void __attribute__((format(printf, 3, 4))) LogError(const int iErrno, const int iErrCode, const char *fmt, ...);
84
85 /**
86 * @brief Log a message with severity, errno, and error code context.
87 *
88 * More general form of LogError() that permits explicit severity.
89 *
90 * @param iErrno The system errno value (0 if not applicable).
91 * @param iErrCode Internal rsyslog error code.
92 * @param severity Syslog severity level (LOG_ERR, LOG_WARNING, etc.).
93 * @param fmt printf-style format string.
94 * @param ... Arguments matching the format string.
95 *
96 * @note Always pass the errno value captured immediately after the
97 * failing call. Do not pass 0 and later call strerror() or
98 * equivalent — this is a common bug.
99 * AI AGENTS: Always use LogMsg(errno, ...) when reporting
100 * system errors. Do not use strerror() or rs_strerror_r().
101 */
102 void __attribute__((format(printf, 4, 5))) LogMsg(
103 const int iErrno, const int iErrCode, const int severity, const char *fmt, ...);
104
105 /**
106 * @brief Append a JSON record about an oversize message to the oversize-message log.
107 *
108 * Behavior:
109 * - No-op if the oversize message error file is not configured.
110 * - Lazily opens the file on first use and reuses the file descriptor.
111 * - Access is serialized via oversizeMsgLogMut.
112 * - Writes one JSON line per entry with fields: "rawmsg" (original raw bytes) and "input" (input name).
113 * - On open/write errors, emits LogError(...) but returns no error; no further recovery is attempted.
114 * The reason is that there is nothing useful that could be done in that case.
115 *
116 * @param pMsg Pointer to the message object (must not be NULL).
117 * @retval RS_RET_OK on success, or when no oversize log is configured.
118 * @retval RS_RET_OUT_OF_MEMORY on memory allocation failure.
119 *
120 * @note Output is newline-terminated JSON (not NUL-terminated).
121 */
122 rsRetVal ATTR_NONNULL() writeOversizeMessageLog(const smsg_t *const pMsg);
123
124 #endif /* #ifndef INCLUDED_ERRMSG_H */