annotate src/logerr.h @ 5526:b1a3d9055662 draft

privsep: Allow logfile reopening in a chroot Now that only the privileged actioneer does the actual logging we can safely reopen the file we are logging to. This also closes and re-opens the syslog connection.
author Roy Marples <roy@marples.name>
date Fri, 30 Oct 2020 14:19:16 +0000
parents 26b5d9bc2985
children 071a9ea18363
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
2 /*
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
3 * logerr: errx with logging
4922
555d7d1a4939 Welcome to 2020!
Roy Marples <roy@marples.name>
parents: 4548
diff changeset
4 * Copyright (c) 2006-2020 Roy Marples <roy@marples.name>
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
5 * All rights reserved
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
6
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
7 * Redistribution and use in source and binary forms, with or without
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
8 * modification, are permitted provided that the following conditions
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
9 * are met:
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
10 * 1. Redistributions of source code must retain the above copyright
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer.
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
15 *
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
26 * SUCH DAMAGE.
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
27 */
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
28
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
29 #ifndef LOGERR_H
4012
75a3e4964dfb Fix define.
Roy Marples <roy@marples.name>
parents: 4010
diff changeset
30 #define LOGERR_H
4010
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
31
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
32 #include <sys/param.h>
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
33
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
34 #ifndef __printflike
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
35 #if __GNUC__ > 2 || defined(__INTEL_COMPILER)
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
36 #define __printflike(a, b) __attribute__((format(printf, a, b)))
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
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
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
39 #endif
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
40 #endif /* !__printflike */
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
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
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
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
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
78
5526
b1a3d9055662 privsep: Allow logfile reopening in a chroot
Roy Marples <roy@marples.name>
parents: 5525
diff changeset
79 /* For logging in a chroot */
b1a3d9055662 privsep: Allow logfile reopening in a chroot
Roy Marples <roy@marples.name>
parents: 5525
diff changeset
80 int loggetfd(void);
b1a3d9055662 privsep: Allow logfile reopening in a chroot
Roy Marples <roy@marples.name>
parents: 5525
diff changeset
81 void logsetfd(int);
b1a3d9055662 privsep: Allow logfile reopening in a chroot
Roy Marples <roy@marples.name>
parents: 5525
diff changeset
82 int logreadfd(int);
5525
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
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
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
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
104
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
105 int logopen(const char *);
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
106 void logclose(void);
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
107 int logreopen(void);
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
108
746684127581 logerr: errx with logging
Roy Marples <roy@marples.name>
parents:
diff changeset
109 #endif