]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/audit-fd.c
basic/terminal-util: add support for $NO_COLOR
[thirdparty/systemd.git] / src / core / audit-fd.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4
5 #include "audit-fd.h"
6
7 #if HAVE_AUDIT
8
9 #include <libaudit.h>
10 #include <stdbool.h>
11
12 #include "capability-util.h"
13 #include "fd-util.h"
14 #include "log.h"
15 #include "util.h"
16
17 static bool initialized = false;
18 static int audit_fd;
19
20 int get_audit_fd(void) {
21
22 if (!initialized) {
23 if (have_effective_cap(CAP_AUDIT_WRITE) == 0) {
24 audit_fd = -EPERM;
25 initialized = true;
26
27 return audit_fd;
28 }
29
30 audit_fd = audit_open();
31
32 if (audit_fd < 0) {
33 if (!IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT))
34 log_error_errno(errno, "Failed to connect to audit log: %m");
35
36 audit_fd = errno ? -errno : -EINVAL;
37 }
38
39 initialized = true;
40 }
41
42 return audit_fd;
43 }
44
45 void close_audit_fd(void) {
46
47 if (initialized && audit_fd >= 0)
48 safe_close(audit_fd);
49
50 initialized = true;
51 audit_fd = -ECONNRESET;
52 }
53
54 #else
55
56 int get_audit_fd(void) {
57 return -EAFNOSUPPORT;
58 }
59
60 void close_audit_fd(void) {
61 }
62
63 #endif