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