]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/audit-fd.c
util: move string_is_safe() to string-util.[ch]
[thirdparty/systemd.git] / src / core / audit-fd.c
CommitLineData
c1165f82
LP
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
c1165f82 22
ffc227c9 23#include <errno.h>
c1165f82 24#include "audit-fd.h"
c1165f82
LP
25
26#ifdef HAVE_AUDIT
27
ffc227c9 28#include <stdbool.h>
c1165f82
LP
29#include <libaudit.h>
30
ffc227c9
LP
31#include "log.h"
32#include "util.h"
3ffd4af2 33#include "fd-util.h"
ffc227c9 34
c1165f82
LP
35static bool initialized = false;
36static int audit_fd;
37
38int get_audit_fd(void) {
39
40 if (!initialized) {
41 audit_fd = audit_open();
42
43 if (audit_fd < 0) {
44 if (errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
56f64d95 45 log_error_errno(errno, "Failed to connect to audit log: %m");
c1165f82
LP
46
47 audit_fd = errno ? -errno : -EINVAL;
48 }
49
50 initialized = true;
51 }
52
53 return audit_fd;
54}
55
56void close_audit_fd(void) {
57
58 if (initialized && audit_fd >= 0)
03e334a1 59 safe_close(audit_fd);
c1165f82
LP
60
61 initialized = true;
62 audit_fd = -ECONNRESET;
63}
64
65#else
66
67int get_audit_fd(void) {
68 return -EAFNOSUPPORT;
69}
70
71void close_audit_fd(void) {
72}
73
74#endif