Mercurial > hg > dhcpcd
annotate src/logerr.h @ 5525:26b5d9bc2985 draft
privsep: Send all log messages to the privileged actioneer
If dhcpcd starts and no syslogd implementation is running then
various syscall filters could be triggered when dhcpcd wants to syslog
and it's already in a chroot.
Not all libc openlog implementations support LOG_NDELAY and
openlog does not return an error code and can also mask errno back to 0.
So we have no way of knowing if we have a syslog connection or not.
This means we cannot cache the connection at startup because syslog itself
will try and open if no connection.
As such, all logging is now directed to the dhcpcd privileged actioneer
process which will handle all the syslog and log file writing actions.
The only downside of this approach (other than an extra fd per process)
is that we no longer know which PID raised the message. While we could
put the correct PID in the logfile as we control the API, we cannot
put it into syslog as we cannot control that API.
As all privsep errors should log which function they came from this
will hopefully not be an issue as on the happy path only the master
process will log stuff.
| author | Roy Marples <roy@marples.name> |
|---|---|
| date | Fri, 30 Oct 2020 03:43:51 +0000 |
| parents | ff7c7b4799b3 |
| children | b1a3d9055662 |
| rev | line source |
|---|---|
|
4548
c7df03794de3
Add SPDX identifiers to all dhcpcd source files.
Yegor Yefremov <yegorslists@googlemail.com>
parents:
4333
diff
changeset
|
1 /* SPDX-License-Identifier: BSD-2-Clause */ |
| 4010 | 2 /* |
| 3 * logerr: errx with logging | |
| 4922 | 4 * Copyright (c) 2006-2020 Roy Marples <roy@marples.name> |
| 4010 | 5 * All rights reserved |
| 6 | |
| 7 * Redistribution and use in source and binary forms, with or without | |
| 8 * modification, are permitted provided that the following conditions | |
| 9 * are met: | |
| 10 * 1. Redistributions of source code must retain the above copyright | |
| 11 * notice, this list of conditions and the following disclaimer. | |
| 12 * 2. Redistributions in binary form must reproduce the above copyright | |
| 13 * notice, this list of conditions and the following disclaimer in the | |
| 14 * documentation and/or other materials provided with the distribution. | |
| 15 * | |
| 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
| 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
| 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 26 * SUCH DAMAGE. | |
| 27 */ | |
| 28 | |
| 29 #ifndef LOGERR_H | |
| 4012 | 30 #define LOGERR_H |
| 4010 | 31 |
| 32 #include <sys/param.h> | |
| 33 | |
| 34 #ifndef __printflike | |
| 35 #if __GNUC__ > 2 || defined(__INTEL_COMPILER) | |
| 36 #define __printflike(a, b) __attribute__((format(printf, a, b))) | |
| 37 #else | |
|
4211
7b1cf89fd666
Fix build issue when `__GNUC__ <= 2`
Chris Hathhorn <chathhorn@gmail.com>
parents:
4197
diff
changeset
|
38 #define __printflike(a, b) |
| 4010 | 39 #endif |
| 40 #endif /* !__printflike */ | |
| 41 | |
|
5064
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
42 /* Please do not call log_* functions directly, use macros below */ |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
43 __printflike(1, 2) void log_debug(const char *, ...); |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
44 __printflike(1, 2) void log_debugx(const char *, ...); |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
45 __printflike(1, 2) void log_info(const char *, ...); |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
46 __printflike(1, 2) void log_infox(const char *, ...); |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
47 __printflike(1, 2) void log_warn(const char *, ...); |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
48 __printflike(1, 2) void log_warnx(const char *, ...); |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
49 __printflike(1, 2) void log_err(const char *, ...); |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
50 __printflike(1, 2) void log_errx(const char *, ...); |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
51 #define LOGERROR logerr("%s: %d", __FILE__, __LINE__) |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
52 |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
53 __printflike(2, 3) void logmessage(int pri, const char *fmt, ...); |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
54 __printflike(2, 3) void logerrmessage(int pri, const char *fmt, ...); |
| 4010 | 55 |
|
5064
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
56 /* |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
57 * These are macros to prevent taking address of them so |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
58 * __FILE__, __LINE__, etc can easily be added. |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
59 * |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
60 * We should be using |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
61 * #define loginfox(fmt, __VA_OPT__(,) __VA_ARGS__) |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
62 * but that requires gcc-8 or clang-6 and we still have a need to support |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
63 * old OS's without modern compilers. |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
64 * |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
65 * Likewise, ##__VA_ARGS__ can't be used as that's a gcc only extension. |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
66 * |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
67 * The solution is to put fmt into __VA_ARGS__. |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
68 * It's not pretty but it's 100% portable. |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
69 */ |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
70 #define logdebug(...) log_debug(__VA_ARGS__) |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
71 #define logdebugx(...) log_debugx(__VA_ARGS__) |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
72 #define loginfo(...) log_info(__VA_ARGS__) |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
73 #define loginfox(...) log_infox(__VA_ARGS__) |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
74 #define logwarn(...) log_warn(__VA_ARGS__) |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
75 #define logwarnx(...) log_warnx(__VA_ARGS__) |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
76 #define logerr(...) log_err(__VA_ARGS__) |
|
7721231839f5
logerr: Use macros to call log functions
Sergey Nikiforov <Sergey.Nikiforov@kaspersky.com>
parents:
4922
diff
changeset
|
77 #define logerrx(...) log_errx(__VA_ARGS__) |
| 4010 | 78 |
|
5525
26b5d9bc2985
privsep: Send all log messages to the privileged actioneer
Roy Marples <roy@marples.name>
parents:
5441
diff
changeset
|
79 /* For syslog in a chroot */ |
|
26b5d9bc2985
privsep: Send all log messages to the privileged actioneer
Roy Marples <roy@marples.name>
parents:
5441
diff
changeset
|
80 int loggetsyslogfd(void); |
|
26b5d9bc2985
privsep: Send all log messages to the privileged actioneer
Roy Marples <roy@marples.name>
parents:
5441
diff
changeset
|
81 void logsetsyslogfd(int); |
|
26b5d9bc2985
privsep: Send all log messages to the privileged actioneer
Roy Marples <roy@marples.name>
parents:
5441
diff
changeset
|
82 int loghandlesyslogfd(int); |
|
26b5d9bc2985
privsep: Send all log messages to the privileged actioneer
Roy Marples <roy@marples.name>
parents:
5441
diff
changeset
|
83 |
|
5077
16919a390f4d
dhcpcd: don't report forked to background in quiet mode
Roy Marples <roy@marples.name>
parents:
5064
diff
changeset
|
84 unsigned int loggetopts(void); |
| 4010 | 85 void logsetopts(unsigned int); |
|
4021
a0e367494ce8
Over engineer the logging some more.
Roy Marples <roy@marples.name>
parents:
4019
diff
changeset
|
86 #define LOGERR_DEBUG (1U << 6) |
|
a0e367494ce8
Over engineer the logging some more.
Roy Marples <roy@marples.name>
parents:
4019
diff
changeset
|
87 #define LOGERR_QUIET (1U << 7) |
|
a0e367494ce8
Over engineer the logging some more.
Roy Marples <roy@marples.name>
parents:
4019
diff
changeset
|
88 #define LOGERR_LOG (1U << 11) |
|
a0e367494ce8
Over engineer the logging some more.
Roy Marples <roy@marples.name>
parents:
4019
diff
changeset
|
89 #define LOGERR_LOG_DATE (1U << 12) |
|
a0e367494ce8
Over engineer the logging some more.
Roy Marples <roy@marples.name>
parents:
4019
diff
changeset
|
90 #define LOGERR_LOG_HOST (1U << 13) |
|
a0e367494ce8
Over engineer the logging some more.
Roy Marples <roy@marples.name>
parents:
4019
diff
changeset
|
91 #define LOGERR_LOG_TAG (1U << 14) |
|
a0e367494ce8
Over engineer the logging some more.
Roy Marples <roy@marples.name>
parents:
4019
diff
changeset
|
92 #define LOGERR_LOG_PID (1U << 15) |
|
a0e367494ce8
Over engineer the logging some more.
Roy Marples <roy@marples.name>
parents:
4019
diff
changeset
|
93 #define LOGERR_ERR (1U << 21) |
|
a0e367494ce8
Over engineer the logging some more.
Roy Marples <roy@marples.name>
parents:
4019
diff
changeset
|
94 #define LOGERR_ERR_DATE (1U << 22) |
|
a0e367494ce8
Over engineer the logging some more.
Roy Marples <roy@marples.name>
parents:
4019
diff
changeset
|
95 #define LOGERR_ERR_HOST (1U << 23) |
|
a0e367494ce8
Over engineer the logging some more.
Roy Marples <roy@marples.name>
parents:
4019
diff
changeset
|
96 #define LOGERR_ERR_TAG (1U << 24) |
|
a0e367494ce8
Over engineer the logging some more.
Roy Marples <roy@marples.name>
parents:
4019
diff
changeset
|
97 #define LOGERR_ERR_PID (1U << 25) |
|
a0e367494ce8
Over engineer the logging some more.
Roy Marples <roy@marples.name>
parents:
4019
diff
changeset
|
98 |
|
a0e367494ce8
Over engineer the logging some more.
Roy Marples <roy@marples.name>
parents:
4019
diff
changeset
|
99 /* To build tag support or not. */ |
|
a0e367494ce8
Over engineer the logging some more.
Roy Marples <roy@marples.name>
parents:
4019
diff
changeset
|
100 //#define LOGERR_TAG |
|
a0e367494ce8
Over engineer the logging some more.
Roy Marples <roy@marples.name>
parents:
4019
diff
changeset
|
101 #if defined(LOGERR_TAG) |
|
4019
fe2f1ad133a2
Write syslog entries in logfile:
Roy Marples <roy@marples.name>
parents:
4012
diff
changeset
|
102 void logsettag(const char *); |
|
4021
a0e367494ce8
Over engineer the logging some more.
Roy Marples <roy@marples.name>
parents:
4019
diff
changeset
|
103 #endif |
| 4010 | 104 |
| 105 int logopen(const char *); | |
| 106 void logclose(void); | |
| 107 int logreopen(void); | |
| 108 | |
| 109 #endif |
