]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
c1165f82 | 2 | /*** |
c1165f82 | 3 | Copyright 2012 Lennart Poettering |
c1165f82 LP |
4 | ***/ |
5 | ||
ffc227c9 | 6 | #include <errno.h> |
cf0fbc49 | 7 | |
c1165f82 | 8 | #include "audit-fd.h" |
c1165f82 | 9 | |
349cc4a5 | 10 | #if HAVE_AUDIT |
c1165f82 LP |
11 | |
12 | #include <libaudit.h> | |
cf0fbc49 | 13 | #include <stdbool.h> |
c1165f82 | 14 | |
b3fb3c01 | 15 | #include "capability-util.h" |
cf0fbc49 | 16 | #include "fd-util.h" |
ffc227c9 LP |
17 | #include "log.h" |
18 | #include "util.h" | |
19 | ||
c1165f82 LP |
20 | static bool initialized = false; |
21 | static int audit_fd; | |
22 | ||
23 | int get_audit_fd(void) { | |
24 | ||
25 | if (!initialized) { | |
b3fb3c01 GT |
26 | if (have_effective_cap(CAP_AUDIT_WRITE) == 0) { |
27 | audit_fd = -EPERM; | |
28 | initialized = true; | |
29 | ||
30 | return audit_fd; | |
31 | } | |
32 | ||
c1165f82 LP |
33 | audit_fd = audit_open(); |
34 | ||
35 | if (audit_fd < 0) { | |
ec2ce0c5 | 36 | if (!IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT)) |
56f64d95 | 37 | log_error_errno(errno, "Failed to connect to audit log: %m"); |
c1165f82 LP |
38 | |
39 | audit_fd = errno ? -errno : -EINVAL; | |
40 | } | |
41 | ||
42 | initialized = true; | |
43 | } | |
44 | ||
45 | return audit_fd; | |
46 | } | |
47 | ||
48 | void close_audit_fd(void) { | |
49 | ||
50 | if (initialized && audit_fd >= 0) | |
03e334a1 | 51 | safe_close(audit_fd); |
c1165f82 LP |
52 | |
53 | initialized = true; | |
54 | audit_fd = -ECONNRESET; | |
55 | } | |
56 | ||
57 | #else | |
58 | ||
59 | int get_audit_fd(void) { | |
60 | return -EAFNOSUPPORT; | |
61 | } | |
62 | ||
63 | void close_audit_fd(void) { | |
64 | } | |
65 | ||
66 | #endif |