]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/bus-kernel.c
build-sys: use #if Y instead of #ifdef Y everywhere
[thirdparty/systemd.git] / src / libsystemd / sd-bus / bus-kernel.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2013 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #if HAVE_VALGRIND_MEMCHECK_H
21 #include <valgrind/memcheck.h>
22 #endif
23
24 #include <fcntl.h>
25 #include <malloc.h>
26 #include <sys/mman.h>
27 #include <sys/prctl.h>
28
29 /* When we include libgen.h because we need dirname() we immediately
30 * undefine basename() since libgen.h defines it as a macro to the POSIX
31 * version which is really broken. We prefer GNU basename(). */
32 #include <libgen.h>
33 #undef basename
34
35 #include "alloc-util.h"
36 #include "bus-internal.h"
37 #include "bus-kernel.h"
38 #include "bus-label.h"
39 #include "bus-message.h"
40 #include "bus-util.h"
41 #include "capability-util.h"
42 #include "fd-util.h"
43 #include "fileio.h"
44 #include "format-util.h"
45 #include "memfd-util.h"
46 #include "parse-util.h"
47 #include "stdio-util.h"
48 #include "string-util.h"
49 #include "strv.h"
50 #include "user-util.h"
51 #include "util.h"
52
53 void close_and_munmap(int fd, void *address, size_t size) {
54 if (size > 0)
55 assert_se(munmap(address, PAGE_ALIGN(size)) >= 0);
56
57 safe_close(fd);
58 }
59
60 void bus_flush_memfd(sd_bus *b) {
61 unsigned i;
62
63 assert(b);
64
65 for (i = 0; i < b->n_memfd_cache; i++)
66 close_and_munmap(b->memfd_cache[i].fd, b->memfd_cache[i].address, b->memfd_cache[i].mapped);
67 }
68
69 uint64_t attach_flags_to_kdbus(uint64_t mask) {
70 uint64_t m = 0;
71
72 if (mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
73 SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID))
74 m |= KDBUS_ATTACH_CREDS;
75
76 if (mask & (SD_BUS_CREDS_PID|SD_BUS_CREDS_TID|SD_BUS_CREDS_PPID))
77 m |= KDBUS_ATTACH_PIDS;
78
79 if (mask & SD_BUS_CREDS_COMM)
80 m |= KDBUS_ATTACH_PID_COMM;
81
82 if (mask & SD_BUS_CREDS_TID_COMM)
83 m |= KDBUS_ATTACH_TID_COMM;
84
85 if (mask & SD_BUS_CREDS_EXE)
86 m |= KDBUS_ATTACH_EXE;
87
88 if (mask & SD_BUS_CREDS_CMDLINE)
89 m |= KDBUS_ATTACH_CMDLINE;
90
91 if (mask & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID))
92 m |= KDBUS_ATTACH_CGROUP;
93
94 if (mask & (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS))
95 m |= KDBUS_ATTACH_CAPS;
96
97 if (mask & SD_BUS_CREDS_SELINUX_CONTEXT)
98 m |= KDBUS_ATTACH_SECLABEL;
99
100 if (mask & (SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID))
101 m |= KDBUS_ATTACH_AUDIT;
102
103 if (mask & SD_BUS_CREDS_WELL_KNOWN_NAMES)
104 m |= KDBUS_ATTACH_NAMES;
105
106 if (mask & SD_BUS_CREDS_DESCRIPTION)
107 m |= KDBUS_ATTACH_CONN_DESCRIPTION;
108
109 if (mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS)
110 m |= KDBUS_ATTACH_AUXGROUPS;
111
112 return m;
113 }