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