udev: String substitutions can be done in ENV, too
[systemd_ALT.git] / src / core / audit-fd.c
blob6674fa8379e612909a9c6c55936c81e433891caa
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <errno.h>
5 #include "audit-fd.h"
7 #if HAVE_AUDIT
9 #include <libaudit.h>
10 #include <stdbool.h>
12 #include "capability-util.h"
13 #include "fd-util.h"
14 #include "log.h"
16 static bool initialized = false;
17 static int audit_fd;
19 int get_audit_fd(void) {
21 if (!initialized) {
22 if (have_effective_cap(CAP_AUDIT_WRITE) <= 0) {
23 audit_fd = -EPERM;
24 initialized = true;
26 return audit_fd;
29 audit_fd = audit_open();
31 if (audit_fd < 0) {
32 if (!IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT))
33 log_error_errno(errno, "Failed to connect to audit log: %m");
35 audit_fd = errno ? -errno : -EINVAL;
38 initialized = true;
41 return audit_fd;
44 void close_audit_fd(void) {
46 if (initialized && audit_fd >= 0)
47 safe_close(audit_fd);
49 initialized = true;
50 audit_fd = -ECONNRESET;
53 #else
55 int get_audit_fd(void) {
56 return -EAFNOSUPPORT;
59 void close_audit_fd(void) {
62 #endif