]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/audit.c
shared: add process-util.[ch]
[thirdparty/systemd.git] / src / shared / audit.c
CommitLineData
d7832d2c
KS
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
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
d7832d2c
KS
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
5430f7f2 16 Lesser General Public License for more details.
d7832d2c 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
d7832d2c
KS
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
d7832d2c 22#include <errno.h>
d7832d2c 23#include <stdio.h>
d7832d2c
KS
24
25#include "macro.h"
26#include "audit.h"
27#include "util.h"
0b452006 28#include "process-util.h"
a5c32cff 29#include "fileio.h"
d7832d2c
KS
30
31int audit_session_from_pid(pid_t pid, uint32_t *id) {
5b12334d
LP
32 _cleanup_free_ char *s = NULL;
33 const char *p;
d7832d2c
KS
34 uint32_t u;
35 int r;
36
37 assert(id);
38
b68fa010 39 p = procfs_file_alloca(pid, "sessionid");
d7832d2c 40
5b12334d 41 r = read_one_line_file(p, &s);
d7832d2c
KS
42 if (r < 0)
43 return r;
44
45 r = safe_atou32(s, &u);
d7832d2c
KS
46 if (r < 0)
47 return r;
48
49 if (u == (uint32_t) -1 || u <= 0)
5b12334d 50 return -ENXIO;
d7832d2c
KS
51
52 *id = u;
53 return 0;
54}
55
56int audit_loginuid_from_pid(pid_t pid, uid_t *uid) {
5b12334d
LP
57 _cleanup_free_ char *s = NULL;
58 const char *p;
d7832d2c
KS
59 uid_t u;
60 int r;
61
62 assert(uid);
63
b68fa010 64 p = procfs_file_alloca(pid, "loginuid");
d7832d2c 65
5b12334d 66 r = read_one_line_file(p, &s);
d7832d2c
KS
67 if (r < 0)
68 return r;
69
70 r = parse_uid(s, &u);
d7832d2c
KS
71 if (r < 0)
72 return r;
73
d7832d2c
KS
74 *uid = (uid_t) u;
75 return 0;
76}
cfb1f5df
LP
77
78bool use_audit(void) {
79 static int cached_use = -1;
80
81 if (cached_use < 0) {
82 int fd;
83
84 fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_AUDIT);
85 if (fd < 0)
86 cached_use = errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT;
87 else {
88 cached_use = true;
89 safe_close(fd);
90 }
91 }
92
93 return cached_use;
94}