]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/audit-fd.c
tree-wide: sort includes
[thirdparty/systemd.git] / src / core / audit-fd.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2012 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22
23 #include <errno.h>
24
25 #include "audit-fd.h"
26
27 #ifdef HAVE_AUDIT
28
29 #include <libaudit.h>
30 #include <stdbool.h>
31
32 #include "fd-util.h"
33 #include "log.h"
34 #include "util.h"
35
36 static bool initialized = false;
37 static int audit_fd;
38
39 int get_audit_fd(void) {
40
41 if (!initialized) {
42 audit_fd = audit_open();
43
44 if (audit_fd < 0) {
45 if (errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
46 log_error_errno(errno, "Failed to connect to audit log: %m");
47
48 audit_fd = errno ? -errno : -EINVAL;
49 }
50
51 initialized = true;
52 }
53
54 return audit_fd;
55 }
56
57 void close_audit_fd(void) {
58
59 if (initialized && audit_fd >= 0)
60 safe_close(audit_fd);
61
62 initialized = true;
63 audit_fd = -ECONNRESET;
64 }
65
66 #else
67
68 int get_audit_fd(void) {
69 return -EAFNOSUPPORT;
70 }
71
72 void close_audit_fd(void) {
73 }
74
75 #endif