--- /dev/null
- Version: 241
+# "enable foo" will turn into --enable-foo or --disable-foo
+# depending "with_foo" macro
+%define enable() %{expand:%%{?with_%{1}:--enable-%{1}}%%{!?with_%{1}:--disable-%{1}}}
+
+%define WITH_RANDOMSEED 0
+%define WITH_BASH_COMPLETION 0
+%define WITH_ZSH_COMPLETION 0
+%define WITH_COREDUMP 0
+%define WITH_BACKLIGHT 0
+%define WITH_TIMEDATED 0
+%define WITH_RFKILL 0
+%define WITH_MACHINED 0
+%define WITH_DOC 0
+%define WITH_HOSTNAMED 0
+
+%define build_dir _build
+%define dbuspolicydir %{_datadir}/dbus-1
+
+# The 'meson' macro is defined in rpm macros, but it uses features from rpm 4.15 ({shrink, set_build_flags)
+# Below is a version suitable for our purposes
+%define meson \
+ CFLAGS="${CFLAGS:-%optflags}" \
+ export CFLAGS \
+ %{__meson} \\\
+ --buildtype=plain \\\
+ --prefix=%{_prefix} \\\
+ --libdir=%{_libdir} \\\
+ --libexecdir=%{_libexecdir} \\\
+ --bindir=%{_bindir} \\\
+ --sbindir=%{_sbindir} \\\
+ --includedir=%{_includedir} \\\
+ --datadir=%{_datadir} \\\
+ --mandir=%{_mandir} \\\
+ --infodir=%{_infodir} \\\
+ --localedir=%{_datadir}/locale \\\
+ --sysconfdir=%{_sysconfdir} \\\
+ --localstatedir=%{_localstatedir} \\\
+ --sharedstatedir=%{_sharedstatedir} \\\
+ --wrap-mode=%{__meson_wrap_mode} \\\
+ %{_vpath_builddir}
+# Ninja macros below are defined for ninja in e.g. fedora distro, but
+# so far they are not provided by Tizen's ninja package.
+%define __ninja %{_bindir}/ninja
+%define __ninja_common_opts -v %{?_smp_flags}
+%define ninja_build \
+ %{__ninja} %{__ninja_common_opts}
+%define ninja_install \
+ DESTDIR=%{buildroot} %{__ninja} install %{__ninja_common_opts}
+
+Name: systemd
++Version: 242
+Release: 0%{?release_flags}
+# For a breakdown of the licensing, see README
+License: LGPL-2.1+ and GPL-2.0+
+Summary: A System and Service Manager
+Url: http://www.freedesktop.org/wiki/Software/systemd
+Group: Base/Startup
+Source0: https://github.com/systemd/systemd/archive/v%{version}.tar.gz
+Source1: pamconsole-tmp.conf
+Source2: %{name}-rpmlintrc
+Source3: test-runner.c
+Source4: wait-default-target.sh
+Source5: wait-delayed-target.sh
+Source6: org.tizen.system.conf
+Source7: sysctl-tizen-override.conf
+Source1001: systemd.manifest
+BuildRequires: gperf
+BuildRequires: intltool >= 0.40.0
+BuildRequires: libacl-devel
+BuildRequires: libblkid-devel >= 2.20
+BuildRequires: libcap-devel
+BuildRequires: libgcrypt-devel
+BuildRequires: libkmod-devel >= 14
+%if %{?WITH_DOC}
+BuildRequires: xsltproc
+BuildRequires: docbook-xsl-stylesheets
+%endif
+BuildRequires: pam-devel
+BuildRequires: pkgconfig
+# BuildRequires: pkgconfig(dbus-1) # for remove circular dependency on OBS
+BuildRequires: pkgconfig(glib-2.0)
+BuildRequires: pkgconfig(liblzma)
+BuildRequires: pkgconfig(libkmod)
+BuildRequires: pkgconfig(mount)
+BuildRequires: meson
+BuildRequires: acl
+BuildRequires: python
+# Requires: dbus # for remove circular dependency on OBS
+Requires: filesystem
+Requires(post): coreutils
+Requires(pre): coreutils
+Requires(pre): /usr/bin/getent
+Requires(pre): /usr/sbin/groupadd
+Requires(post): %{_sbindir}/update-alternatives
+Requires(preun): %{_sbindir}/update-alternatives
+
+Obsoletes: SysVinit < 2.86-24
+Obsoletes: sysvinit < 2.86-24
+Provides: SysVinit = 2.86-24
+Provides: sysvinit = 2.86-24
+Provides: /bin/systemctl
+Provides: /sbin/shutdown
+Provides: udev = %{version}
+Obsoletes: udev < 183
+
+%description
+systemd is a system and service manager for Linux, compatible with
+SysV and LSB init scripts. systemd provides aggressive parallelization
+capabilities, uses socket and D-Bus activation for starting services,
+offers on-demand starting of daemons, keeps track of processes using
+Linux cgroups, supports snapshotting and restoring of the system
+state, maintains mount and automount points and implements an
+elaborate transactional dependency-based service control logic. It can
+work as a drop-in replacement for sysvinit.
+
+%package -n libsystemd
+License: LGPL-2.1+
+Summary: Systemd libraries
+Group: Base/Startup
+Obsoletes: libudev < 183
+Provides: libudev = %{version}
+Obsoletes: systemd < 185-4
+Conflicts: systemd < 185-4
+
+%description -n libsystemd
+Libraries for systemd and udev, as well as the systemd PAM module.
+
+%package devel
+License: LGPL-2.1+
+Summary: Development headers for systemd
+Requires: %{name} = %{version}
+Requires: libsystemd = %{version}
+Provides: libudev-devel = %{version}
+Obsoletes: libudev-devel < 183
+
+%description devel
+Development headers and auxiliary files for developing applications for systemd.
+
+%package analyze
+License: LGPL-2.1+
+Summary: Tool for processing systemd profiling information
+Requires: %{name} = %{version}
+Obsoletes: systemd < 38-5
+
+%description analyze
+'systemd-analyze blame' lists which systemd unit needed how much time to finish
+initialization at boot.
+'systemd-analyze plot' renders an SVG visualizing the parallel start of units
+at boot.
+
+%package tests
+License: LGPL-2.1+ and BSD-2-Clause
+Summary: Set of tests for sd-bus component
+Requires: %{name} = %{version}
+
+%description tests
+This package is part of 'dbus-integratnion-tests' framework and contains set of tests
+for sd-bus component (DBUS API C library).
+
+%package extension-kdbus
+Summary: Extension for systemd to support KDBUS in Tizen
+Requires: %{name} = %{version}-%{release}
+
+%description extension-kdbus
+This modifies systemd to support KDBUS in Tizen.
+
+%prep
+%setup -q
+
+%build
+cp %{SOURCE1001} .
+cp %{SOURCE3} .
+
+%define _vpath_srcdir .
+%define _vpath_builddir %{build_dir}
+%meson \
+ -Dkdbus=true \
+%if ! %{WITH_RANDOMSEED}
+ -Drandomseed=false \
+%endif
+%if ! %{?WITH_COREDUMP}
+ -Dcoredump=false \
+%endif
+%if ! %{?WITH_BACKLIGHT}
+ -Dbacklight=false \
+%endif
+%if ! %{?WITH_TIMEDATED}
+ -Dtimedated=false \
+%endif
+%if ! %{WITH_RFKILL}
+ -Drfkill=false \
+%endif
+ -Dhwdb=false \
+ -Dsysusers=false \
+ -Dfirstboot=false \
+ -Dpolkit=false \
+ -Dtimesyncd=false \
+ -Dresolve=false \
+ -Dnetworkd=false \
+%if ! %{?WITH_MACHINED}
+ -Dmachined=false \
+%endif
+%if ! %{?WITH_HOSTNAMED}
+ -Dhostnamed=false \
+%endif
+ -Dimportd=false \
+ -Denvironment-d=false \
+ -Dnss-systemd=false \
+ -Dgcrypt=false \
+%if ! %{?WITH_DOC}
+ -Dman=false \
+%endif
+ -Dportabled=false \
+ -Drpmmacrosdir=%{_sysconfdir}/rpm/ \
+ -Dsysvinit-path="" \
+ -Dsysvrcnd-path="" \
+ -Dsmack-run-label=System::Privileged \
+ -Dinstall-tests=true \
+ -Ddefault-hierarchy=legacy \
+ -Db_pie=true
+%meson_build
+
+# compile test-runner for 'dbus-integration-test' framework
+%__cc %{_builddir}/%{name}-%{version}/test-runner.c -o %{_builddir}/%{name}-%{version}/systemd-tests
+
+%install
+%meson_install
+%find_lang %{name}
+cat <<EOF >> systemd.lang
+%lang(be) /usr/lib/systemd/catalog/systemd.be.catalog
+%lang(be) /usr/lib/systemd/catalog/systemd.be@latin.catalog
+%lang(bg) /usr/lib/systemd/catalog/systemd.bg.catalog
+%lang(de) /usr/lib/systemd/catalog/systemd.de.catalog
+%lang(fr) /usr/lib/systemd/catalog/systemd.fr.catalog
+%lang(it) /usr/lib/systemd/catalog/systemd.it.catalog
+%lang(pl) /usr/lib/systemd/catalog/systemd.pl.catalog
+%lang(pt_BR) /usr/lib/systemd/catalog/systemd.pt_BR.catalog
+%lang(ru) /usr/lib/systemd/catalog/systemd.ru.catalog
+%lang(zh) /usr/lib/systemd/catalog/systemd.zh_CN.catalog
+%lang(zh) /usr/lib/systemd/catalog/systemd.zh_TW.catalog
+EOF
+
+# udev links
+/usr/bin/mkdir -p %{buildroot}/%{_sbindir}
+/usr/bin/ln -sf ../bin/udevadm %{buildroot}%{_sbindir}/udevadm
+/usr/bin/mkdir -p %{buildroot}%{_prefix}/lib/firmware/updates
+
+# Create SysV compatibility symlinks. systemctl/systemd are smart
+# enough to detect in which way they are called.
+/usr/bin/ln -s ../lib/systemd/systemd %{buildroot}%{_bindir}/systemd
+
+# legacy links
+/usr/bin/ln -s loginctl %{buildroot}%{_bindir}/systemd-loginctl
+
+# We create all wants links manually at installation time to make sure
+# they are not owned and hence overriden by rpm after the used deleted
+# them.
+/usr/bin/rm -r %{buildroot}%{_sysconfdir}/systemd/system/*.target.wants
+
+# Make sure these directories are properly owned
+/usr/bin/mkdir -p %{buildroot}%{_prefix}/lib/systemd/system/basic.target.wants
+/usr/bin/mkdir -p %{buildroot}%{_prefix}/lib/systemd/system/default.target.wants
+/usr/bin/mkdir -p %{buildroot}%{_prefix}/lib/systemd/system/dbus.target.wants
+/usr/bin/mkdir -p %{buildroot}%{_prefix}/lib/systemd/system/syslog.target.wants
+
+# Make sure the user generators dir exists too
+/usr/bin/mkdir -p %{buildroot}%{_prefix}/lib/systemd/system-generators
+/usr/bin/mkdir -p %{buildroot}%{_prefix}/lib/systemd/user-generators
+
+# Create new-style configuration files so that we can ghost-own them
+/usr/bin/touch %{buildroot}%{_sysconfdir}/hostname
+/usr/bin/touch %{buildroot}%{_sysconfdir}/vconsole.conf
+/usr/bin/touch %{buildroot}%{_sysconfdir}/locale.conf
+/usr/bin/touch %{buildroot}%{_sysconfdir}/machine-id
+/usr/bin/touch %{buildroot}%{_sysconfdir}/machine-info
+/usr/bin/touch %{buildroot}%{_sysconfdir}/timezone
+
+/usr/bin/mkdir -p %{buildroot}%{_prefix}/lib/systemd/system-preset/
+/usr/bin/mkdir -p %{buildroot}%{_prefix}/lib/systemd/user-preset/
+
+# Make sure the shutdown/sleep drop-in dirs exist
+/usr/bin/mkdir -p %{buildroot}%{_prefix}/lib/systemd/system-shutdown/
+/usr/bin/mkdir -p %{buildroot}%{_prefix}/lib/systemd/system-sleep/
+
+# Make sure the NTP units dir exists
+/usr/bin/mkdir -p %{buildroot}%{_prefix}/lib/systemd/ntp-units.d/
+
+# Install modprobe fragment
+/usr/bin/mkdir -p %{buildroot}%{_sysconfdir}/modprobe.d/
+
+# Fix the dangling /var/lock -> /run/lock symlink
+install -Dm644 tmpfiles.d/legacy.conf %{buildroot}%{_prefix}/lib/tmpfiles.d/legacy.conf
+
+install -m644 %{SOURCE1} %{buildroot}%{_prefix}/lib/tmpfiles.d/
+
+install -m 755 -d %{buildroot}/%{_prefix}/lib/systemd/system
+
+rm -rf %{buildroot}/%{_docdir}/%{name}
+
+# Allow replacing systemd-shutdown with tizen-specific variant
+mv %{buildroot}%{_prefix}/lib/systemd/systemd-shutdown %{buildroot}%{_prefix}/lib/systemd/systemd-shutdown-original
+
+# Disable some useless services in Tizen
+rm -rf %{buildroot}/%{_prefix}/lib/systemd/system/sysinit.target.wants/dev-hugepages.mount
+rm -rf %{buildroot}/%{_prefix}/lib/systemd/system/sysinit.target.wants/sys-fs-fuse-connections.mount
+rm -rf %{buildroot}/%{_prefix}/lib/systemd/system/sysinit.target.wants/systemd-binfmt.service
+rm -rf %{buildroot}/%{_prefix}/lib/systemd/system/sysinit.target.wants/systemd-modules-load.service
+rm -rf %{buildroot}/%{_prefix}/lib/systemd/system/sysinit.target.wants/systemd-ask-password-console.path
+rm -rf %{buildroot}/%{_prefix}/lib/systemd/system/multi-user.target.wants/systemd-ask-password-wall.path
+rm -rf %{buildroot}/%{_prefix}/lib/systemd/system/systemd-tmpfiles-clean.timer
+rm -rf %{buildroot}/%{_prefix}/lib/systemd/system/systemd-tmpfiles-clean.service
+rm -rf %{buildroot}/%{_prefix}/lib/systemd/system/timers.target.wants/systemd-tmpfiles-clean.timer
+rm -rf %{buildroot}/%{_prefix}/lib/systemd/user/systemd-tmpfiles-clean.timer
+rm -rf %{buildroot}/%{_prefix}/lib/systemd/user/systemd-tmpfiles-clean.service
+rm -rf %{buildroot}/%{_prefix}/lib/systemd/user/systemd-tmpfiles-setup.service
+
+# Exclude ELF binaries
+rm -f %{buildroot}/%{_prefix}/lib/systemd/system-generators/systemd-debug-generator
+rm -f %{buildroot}/%{_prefix}/lib/systemd/system-generators/systemd-efi-boot-generator
+rm -f %{buildroot}/%{_prefix}/lib/systemd/system-generators/systemd-gpt-auto-generator
+rm -f %{buildroot}/%{_prefix}/lib/systemd/system-generators/systemd-hibernate-resume-generator
+rm -f %{buildroot}/%{_prefix}/lib/systemd/system-generators/systemd-bless-boot-generator
+rm -f %{buildroot}/%{_prefix}/lib/systemd/system-generators/systemd-run-generator
+
+# Marker file for kdbus
+touch %{buildroot}/%{_sysconfdir}/systemd/extension-kdbus
+
+# Prepare tests for 'dbus-integration-test' framework
+install -D -m 755 %{_builddir}/%{name}-%{version}/systemd-tests %{buildroot}%{_prefix}/lib/dbus-tests/runner/systemd-tests
+mkdir -p %{buildroot}%{_prefix}/lib/dbus-tests/test-suites/systemd-tests/
+mv %{buildroot}%{_prefix}/lib/systemd/tests/test-bus-* %{buildroot}%{_prefix}/lib/dbus-tests/test-suites/systemd-tests/
+
+# Shell Completion
+%if ! %{?WITH_BASH_COMPLETION}
+rm -rf %{buildroot}/%{_datadir}/bash-completion/*
+%endif
+%if ! %{?WITH_ZSH_COMPLETION}
+rm -rf %{buildroot}/%{_datadir}/zsh/site-functions/*
+%endif
+
+mkdir -p %{buildroot}/%{_localstatedir}/log/journal
+
+ln -sf ./libsystemd.pc %{buildroot}%{_libdir}/pkgconfig/libsystemd-daemon.pc
+ln -sf ./libsystemd.pc %{buildroot}%{_libdir}/pkgconfig/libsystemd-id128.pc
+ln -sf ./libsystemd.pc %{buildroot}%{_libdir}/pkgconfig/libsystemd-journal.pc
+ln -sf ./libsystemd.pc %{buildroot}%{_libdir}/pkgconfig/libsystemd-login.pc
+
+# Delayed target
+install -m 0755 %{SOURCE4} %{buildroot}%{_bindir}/wait-default-target.sh
+install -m 0755 %{SOURCE5} %{buildroot}%{_bindir}/wait-delayed-target.sh
+install -m 0755 %{SOURCE6} %{buildroot}%{dbuspolicydir}/system.d/org.tizen.system.conf
+
+# Tizen sysctl values overriding default systemd values
+install -m 0644 %{SOURCE7} %{buildroot}%{_sysconfdir}/sysctl.d/
+
+# end of install
+%pre
+/usr/bin/getent group cdrom >/dev/null 2>&1 || /usr/sbin/groupadd -r -g 11 cdrom >/dev/null 2>&1 || :
+/usr/bin/getent group tape >/dev/null 2>&1 || /usr/sbin/groupadd -r -g 33 tape >/dev/null 2>&1 || :
+/usr/bin/getent group dialout >/dev/null 2>&1 || /usr/sbin/groupadd -r -g 18 dialout >/dev/null 2>&1 || :
+/usr/bin/getent group floppy >/dev/null 2>&1 || /usr/sbin/groupadd -r -g 19 floppy >/dev/null 2>&1 || :
+/usr/bin/systemctl stop systemd-udevd-control.socket systemd-udevd-kernel.socket systemd-udevd.service >/dev/null 2>&1 || :
+
+# Rename configuration files that changed their names
+/usr/bin/mv -n %{_sysconfdir}/systemd/systemd-logind.conf %{_sysconfdir}/systemd/logind.conf >/dev/null 2>&1 || :
+/usr/bin/mv -n %{_sysconfdir}/systemd/systemd-journald.conf %{_sysconfdir}/systemd/journald.conf >/dev/null 2>&1 || :
+
+%post
+/usr/bin/systemd-machine-id-setup > /dev/null 2>&1 || :
+%if %{WITH_RANDOMSEED}
+/usr/lib/systemd/systemd-random-seed save > /dev/null 2>&1 || :
+%endif
+/usr/bin/systemctl daemon-reexec > /dev/null 2>&1 || :
+/usr/bin/systemctl start systemd-udevd.service >/dev/null 2>&1 || :
+/usr/bin/mkdir -p /etc/systemd/network
+/usr/bin/ln -sf /dev/null /etc/systemd/network/99-default.link
+
+# Set the smack label of executable binary tools
+chsmack %{_bindir}/bootctl -a "System::Tools"
+chsmack %{_bindir}/busctl -a "System::Tools"
+chsmack %{_bindir}/kernel-install -a "System::Tools"
+%if %{?WITH_MACHINED}
+chsmack %{_bindir}/machinectl -a "System::Tools"
+%endif
+chsmack %{_bindir}/systemd-run -a "System::Tools"
+%if %{?WITH_HOSTNAMED}
+chsmack %{_bindir}/hostnamectl -a "System::Tools"
+%endif
+chsmack %{_bindir}/localectl -a "System::Tools"
+%if %{?WITH_COREDUMP}
+chsmack %{_bindir}/coredumpctl -a "System::Tools"
+%endif
+%if %{?WITH_TIMEDATED}
+chsmack %{_bindir}/timedatectl -a "System::Tools"
+%endif
+chsmack %{_bindir}/systemd -a "System::Tools"
+chsmack %{_bindir}/systemctl -a "System::Tools"
+chsmack %{_bindir}/systemd-notify -a "System::Tools"
+chsmack %{_bindir}/systemd-ask-password -a "System::Tools"
+chsmack %{_bindir}/systemd-tty-ask-password-agent -a "System::Tools"
+chsmack %{_bindir}/systemd-machine-id-setup -a "System::Tools"
+chsmack %{_bindir}/systemd-socket-activate -a "System::Tools"
+chsmack %{_bindir}/loginctl -a "System::Tools"
+chsmack %{_bindir}/systemd-loginctl -a "System::Tools"
+chsmack %{_bindir}/journalctl -a "System::Tools"
+chsmack %{_bindir}/systemd-tmpfiles -a "System::Tools"
+chsmack %{_bindir}/systemd-nspawn -a "System::Tools"
+chsmack %{_bindir}/systemd-stdio-bridge -a "System::Tools"
+chsmack %{_bindir}/systemd-cat -a "System::Tools"
+chsmack %{_bindir}/systemd-cgls -a "System::Tools"
+chsmack %{_bindir}/systemd-cgtop -a "System::Tools"
+chsmack %{_bindir}/systemd-delta -a "System::Tools"
+chsmack %{_bindir}/systemd-detect-virt -a "System::Tools"
+chsmack %{_bindir}/systemd-inhibit -a "System::Tools"
+chsmack %{_bindir}/udevadm -a "System::Tools"
+chsmack %{_bindir}/systemd-escape -a "System::Tools"
+chsmack %{_bindir}/systemd-path -a "System::Tools"
+chsmack %{_prefix}/lib/systemd/* -a "System::Tools"
+
+%postun
+if [ $1 -ge 1 ] ; then
+ /usr/bin/systemctl daemon-reload > /dev/null 2>&1 || :
+ /usr/bin/systemctl try-restart systemd-logind.service >/dev/null 2>&1 || :
+fi
+
+%preun
+if [ $1 -eq 0 ] ; then
+ /usr/bin/systemctl disable \
+ getty@.service \
+ remote-fs.target \
+ systemd-readahead-replay.service \
+ systemd-readahead-collect.service >/dev/null 2>&1 || :
+
+ update-alternatives --remove systemd-shutdown %{_prefix}/lib/systemd/systemd-shutdown-original
+fi
+
+%posttrans
+# Update alternatives after the whole transaction is completed - this is
+# necessary due to RPM ordering, which removes files from old package not
+# provided by new package after regular post scripts are run. Please refer
+# to following guideliness for explanation:
+# https://fedoraproject.org/wiki/Packaging:Scriptlets#Ordering
+if [ $1 -eq 0 ]; then
+ update-alternatives --install %{_prefix}/lib/systemd/systemd-shutdown systemd-shutdown %{_prefix}/lib/systemd/systemd-shutdown-original 100 || :
+fi
+
+%post -n libsystemd -p /sbin/ldconfig
+%postun -n libsystemd -p /sbin/ldconfig
+
+%lang_package
+
+%files
+%manifest %{name}.manifest
+%license LICENSE.LGPL2.1 LICENSE.GPL2
+%config %{_sysconfdir}/pam.d/systemd-user
+%{_bindir}/bootctl
+%{_bindir}/busctl
+%{_bindir}/kernel-install
+%if %{?WITH_MACHINED}
+%{_bindir}/machinectl
+%endif
+%{_bindir}/systemd-run
+%dir %{_prefix}/lib/kernel
+%dir %{_prefix}/lib/kernel/install.d
++%{_prefix}/lib/kernel/install.d/00-entry-directory.install
+%{_prefix}/lib/kernel/install.d/50-depmod.install
+%{_prefix}/lib/kernel/install.d/90-loaderentry.install
+%if %{?WITH_HOSTNAMED}
+%{_bindir}/hostnamectl
+%endif
+%{_bindir}/localectl
+%if %{?WITH_COREDUMP}
+%{_bindir}/coredumpctl
+%endif
+%if %{?WITH_TIMEDATED}
+%{_bindir}/timedatectl
+%endif
+%dir %{_sysconfdir}/systemd
+%dir %{_sysconfdir}/systemd/system
+%dir %{_sysconfdir}/systemd/user
+%dir %{_sysconfdir}/tmpfiles.d
+%dir %{_sysconfdir}/sysctl.d
+%dir %{_sysconfdir}/modules-load.d
+%dir %{_sysconfdir}/binfmt.d
+%if %{?WITH_BASH_COMPLETION}
+%{_datadir}/bash-completion/*
+%endif
+%if %{?WITH_ZSH_COMPLETION}
+%dir %{_datadir}/zsh/site-functions
+%{_datadir}/zsh/site-functions/*
+%endif
+%dir %{_sysconfdir}/udev
+%dir %{_sysconfdir}/udev/rules.d
+%dir %{_prefix}/lib/systemd
+%dir %{_prefix}/lib/systemd/system
+%dir %{_prefix}/lib/systemd/system-generators
+%dir %{_prefix}/lib/systemd/user-generators
+%dir %{_prefix}/lib/systemd/system-preset
+%dir %{_prefix}/lib/systemd/user-preset
+%dir %{_prefix}/lib/systemd/system-shutdown
+%dir %{_prefix}/lib/systemd/system-sleep
+%dir %{_prefix}/lib/tmpfiles.d
+%dir %{_prefix}/lib/sysctl.d
+%dir %{_prefix}/lib/modules-load.d
+%dir %{_prefix}/lib/binfmt.d
+%dir %{_prefix}/lib/firmware
+%dir %{_prefix}/lib/firmware/updates
+%dir %{_datadir}/systemd
+%config(noreplace) %{dbuspolicydir}/system.d/org.freedesktop.systemd1.conf
+%if %{?WITH_HOSTNAMED}
+%config(noreplace) %{dbuspolicydir}/system.d/org.freedesktop.hostname1.conf
+%endif
+%config(noreplace) %{dbuspolicydir}/system.d/org.freedesktop.login1.conf
+%config(noreplace) %{dbuspolicydir}/system.d/org.freedesktop.locale1.conf
+%if %{?WITH_TIMEDATED}
+%config(noreplace) %{dbuspolicydir}/system.d/org.freedesktop.timedate1.conf
+%endif
+%if %{?WITH_MACHINED}
+%config(noreplace) %{dbuspolicydir}/system.d/org.freedesktop.machine1.conf
+%endif
+%if %{?WITH_COREDUMP}
+%config(noreplace) %{_sysconfdir}/systemd/coredump.conf
+%endif
+%config(noreplace) %{_sysconfdir}/systemd/system.conf
+%config(noreplace) %{_sysconfdir}/systemd/user.conf
+%config(noreplace) %{_sysconfdir}/systemd/logind.conf
+%config(noreplace) %{_sysconfdir}/systemd/journald.conf
+%config(noreplace) %{_sysconfdir}/udev/udev.conf
+%config(noreplace) %{_sysconfdir}/sysctl.d/sysctl-tizen-override.conf
+%config(noreplace) %{_sysconfdir}/systemd/sleep.conf
+%{_sysconfdir}/xdg/systemd
+%ghost %config(noreplace) %{_sysconfdir}/hostname
+%ghost %config(noreplace) %{_sysconfdir}/vconsole.conf
+%ghost %config(noreplace) %{_sysconfdir}/locale.conf
+%ghost %config(noreplace) %{_sysconfdir}/machine-id
+%ghost %config(noreplace) %{_sysconfdir}/machine-info
+%ghost %config(noreplace) %{_sysconfdir}/timezone
+%exclude %{_sysconfdir}/X11/xinit/xinitrc.d/50-systemd-user.sh
+%{_bindir}/systemd
+%{_bindir}/systemctl
+%{_bindir}/systemd-notify
+%{_bindir}/systemd-ask-password
+%{_bindir}/systemd-tty-ask-password-agent
+%{_bindir}/systemd-machine-id-setup
+%{_bindir}/systemd-socket-activate
+%{_bindir}/loginctl
+%{_bindir}/systemd-loginctl
+%{_bindir}/journalctl
+%{_bindir}/systemd-tmpfiles
+%{_bindir}/systemd-nspawn
+%{_bindir}/systemd-stdio-bridge
+%{_bindir}/systemd-cat
+%{_bindir}/systemd-cgls
+%{_bindir}/systemd-cgtop
+%{_bindir}/systemd-delta
+%{_bindir}/systemd-detect-virt
+%{_bindir}/systemd-inhibit
+%{_bindir}/systemd-id128
+%{_bindir}/udevadm
+%{_bindir}/systemd-escape
+%{_bindir}/systemd-path
+%{_bindir}/systemd-mount
+%{_bindir}/systemd-umount
+%{_prefix}/lib/sysctl.d/*.conf
+%{_prefix}/lib/systemd/systemd
+%{_prefix}/lib/systemd/system
+
+%dir %{_prefix}/lib/systemd/system/basic.target.wants
+%dir %{_prefix}/lib/systemd/user
+%dir %{_prefix}/lib/systemd/network
+%dir %{_prefix}/lib/systemd/user/default.target.wants
+%{_prefix}/lib/systemd/user/basic.target
+%{_prefix}/lib/systemd/user/exit.target
+%{_prefix}/lib/systemd/user/printer.target
+%{_prefix}/lib/systemd/user/shutdown.target
+%{_prefix}/lib/systemd/user/sockets.target
+%{_prefix}/lib/systemd/user/sound.target
+%{_prefix}/lib/systemd/user/systemd-exit.service
+%{_prefix}/lib/systemd/user/paths.target
+%{_prefix}/lib/systemd/user/smartcard.target
+%{_prefix}/lib/systemd/user/timers.target
+%{_prefix}/lib/systemd/user/default.target
+%exclude %{_prefix}/lib/systemd/user/graphical-session.target
+%exclude %{_prefix}/lib/systemd/user/graphical-session-pre.target
+%{_prefix}/lib/systemd/network/99-default.link
+%exclude %{_prefix}/lib/systemd/system-preset/90-systemd.preset
+%exclude %{_prefix}/lib/systemd/user-preset/90-systemd.preset
+%{_prefix}/lib/systemd/user/delayed.target
+%{_prefix}/lib/systemd/user/user-delayed-target-trigger.service
+%{_prefix}/lib/systemd/user/default.target.wants/user-delayed-target-trigger.service
+%{_prefix}/lib/systemd/user/user-default-target-done.service
+%{_prefix}/lib/systemd/user/default.target.wants/user-default-target-done.service
+%{_prefix}/lib/systemd/user/user-delayed-target-done.service
+%{_prefix}/lib/systemd/user/delayed.target.wants/user-delayed-target-done.service
+%{dbuspolicydir}/system.d/org.tizen.system.conf
+%exclude %{_prefix}/lib/modprobe.d/systemd.conf
+
+%{_prefix}/lib/systemd/libsystemd-shared-%{version}.so
+%{_prefix}/lib/systemd/systemd-*
+%dir %{_prefix}/lib/systemd/catalog
+%{_prefix}/lib/systemd/catalog/systemd.catalog
+%{_prefix}/lib/udev
+%{_prefix}/lib/systemd/system-generators/systemd-getty-generator
+%{_prefix}/lib/systemd/system-generators/systemd-fstab-generator
+%{_prefix}/lib/systemd/system-generators/systemd-system-update-generator
+%{_prefix}/lib/tmpfiles.d/home.conf
+%{_prefix}/lib/tmpfiles.d/journal-nocow.conf
+%{_prefix}/lib/tmpfiles.d/legacy.conf
+%{_prefix}/lib/tmpfiles.d/pamconsole-tmp.conf
+%{_prefix}/lib/tmpfiles.d/systemd.conf
+%{_prefix}/lib/tmpfiles.d/systemd-nologin.conf
+%if %{?WITH_MACHINED}
+%{_prefix}/lib/tmpfiles.d/systemd-nspawn.conf
+%endif
+%{_prefix}/lib/tmpfiles.d/tmp.conf
+%{_prefix}/lib/tmpfiles.d/var.conf
+%{_prefix}/lib/tmpfiles.d/x11.conf
+%{_sbindir}/init
+%{_sbindir}/reboot
+%{_sbindir}/halt
+%{_sbindir}/poweroff
+%{_sbindir}/shutdown
+%{_sbindir}/telinit
+%{_sbindir}/runlevel
+%{_sbindir}/udevadm
+%{_datadir}/systemd/graphinfo.gvpr
+%{_datadir}/systemd/kbd-model-map
+%{_datadir}/systemd/language-fallback-map
+%{_datadir}/dbus-1/services/org.freedesktop.systemd1.service
+%{_datadir}/dbus-1/system-services/org.freedesktop.systemd1.service
+%if %{?WITH_HOSTNAMED}
+%{_datadir}/dbus-1/system-services/org.freedesktop.hostname1.service
+%endif
+%{_datadir}/dbus-1/system-services/org.freedesktop.login1.service
+%{_datadir}/dbus-1/system-services/org.freedesktop.locale1.service
+%exclude %{_datadir}/polkit-1/actions/org.freedesktop.systemd1.policy
+%exclude %{_datadir}/polkit-1/actions/org.freedesktop.login1.policy
+%exclude %{_datadir}/polkit-1/actions/org.freedesktop.locale1.policy
+%if %{?WITH_TIMEDATED}
+%{_datadir}/dbus-1/system-services/org.freedesktop.timedate1.service
+%endif
+%if %{?WITH_MACHINED}
+%{_datadir}/dbus-1/system-services/org.freedesktop.machine1.service
+%endif
+%dir %{_datadir}/factory/
+%dir %{_datadir}/factory/etc
+%dir %{_datadir}/factory/etc/pam.d
+%{_datadir}/factory/etc/nsswitch.conf
+%{_datadir}/factory/etc/pam.d/other
+%{_datadir}/factory/etc/pam.d/system-auth
+
+%{_localstatedir}/log/journal
+
+%{_bindir}/wait-default-target.sh
+%{_bindir}/wait-delayed-target.sh
+%exclude %{_prefix}/lib/systemd/system/runlevel0.target
+%exclude %{_prefix}/lib/systemd/system/runlevel1.target
+%exclude %{_prefix}/lib/systemd/system/runlevel2.target
+%exclude %{_prefix}/lib/systemd/system/runlevel3.target
+%exclude %{_prefix}/lib/systemd/system/runlevel4.target
+%exclude %{_prefix}/lib/systemd/system/runlevel5.target
+%exclude %{_prefix}/lib/systemd/system/runlevel6.target
+
+%files -n libsystemd
+%manifest %{name}.manifest
+%license LICENSE.LGPL2.1
+%{_libdir}/security/pam_systemd.so
+%{_libdir}/libsystemd.so.*
+%{_libdir}/libudev.so.*
+%{_libdir}/libnss_myhostname.so.2
+%if %{?WITH_MACHINED}
+%{_libdir}/libnss_mymachines.so.2
+%endif
+
+%files extension-kdbus
+%manifest %{name}.manifest
+%license LICENSE.LGPL2.1 LICENSE.GPL2
+%{_sysconfdir}/systemd/extension-kdbus
+%{_prefix}/lib/systemd/user/busnames.target
+%{_prefix}/lib/systemd/system-generators/systemd-dbus1-generator
+%{_prefix}/lib/systemd/user-generators/systemd-dbus1-generator
+
+%files devel
+%manifest %{name}.manifest
+%{_libdir}/libudev.so
+%{_libdir}/libsystemd.so
+%dir %{_includedir}/systemd
+%{_includedir}/systemd/sd-bus.h
+%{_includedir}/systemd/sd-bus-protocol.h
+%{_includedir}/systemd/sd-bus-vtable.h
+%{_includedir}/systemd/sd-event.h
+%{_includedir}/systemd/_sd-common.h
+%{_includedir}/systemd/sd-daemon.h
+%{_includedir}/systemd/sd-device.h
+%{_includedir}/systemd/sd-hwdb.h
+%{_includedir}/systemd/sd-id128.h
+%{_includedir}/systemd/sd-journal.h
+%{_includedir}/systemd/sd-login.h
+%{_includedir}/systemd/sd-messages.h
+%{_includedir}/libudev.h
+%{_libdir}/pkgconfig/libudev.pc
+%{_libdir}/pkgconfig/libsystemd.pc
+%{_datadir}/pkgconfig/systemd.pc
+%{_datadir}/pkgconfig/udev.pc
+%{_libdir}/pkgconfig/libsystemd-daemon.pc
+%{_libdir}/pkgconfig/libsystemd-id128.pc
+%{_libdir}/pkgconfig/libsystemd-journal.pc
+%{_libdir}/pkgconfig/libsystemd-login.pc
+%{_sysconfdir}/rpm/macros.systemd
+
+%files analyze
+%manifest %{name}.manifest
+%license LICENSE.LGPL2.1
+%{_bindir}/systemd-analyze
+
+%files tests
+%manifest %{name}.manifest
+%{_prefix}/lib/dbus-tests/test-suites/systemd-tests/
+%{_prefix}/lib/dbus-tests/runner/systemd-tests
+%{_prefix}/lib/systemd/tests/
+
+%if %{?WITH_DOC}
+%docs_package
+%else
+%exclude %{_docdir}
+%exclude %{_datadir}/doc/systemd
+%endif
khash.h
label.c
label.h
+ limits-util.c
+ limits-util.h
+ linux/btrfs.h
+ linux/btrfs_tree.h
+ linux/can/vxcan.h
+ linux/fib_rules.h
+ linux/fou.h
+ linux/if.h
+ linux/if_addr.h
+ linux/if_arp.h
+ linux/if_bonding.h
+ linux/if_bridge.h
+ linux/if_link.h
+ linux/if_tun.h
+ linux/if_tunnel.h
+ linux/in.h
+ linux/in6.h
+ linux/libc-compat.h
+ linux/netlink.h
+ linux/rtnetlink.h
+ linux/wireguard.h
list.h
locale-util.c
locale-util.h
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <getopt.h>
++#include <signal.h>
#include <stdio_ext.h>
- #include <hashmap.h>
-
--#include "sd-bus.h"
--
#include "alloc-util.h"
#include "bus-dump.h"
#include "bus-internal.h"
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
++#include "hashmap.h"
#include "json.h"
#include "locale-util.h"
#include "log.h"
#include "path-util.h"
#include "pretty-print.h"
#include "set.h"
++#include "sd-bus.h"
+ #include "sort-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "user-util.h"
- #include "signal.h"
+#include "util.h"
#include "verbs.h"
static enum {
static bool arg_augment_creds = true;
static bool arg_watch_bind = false;
static usec_t arg_timeout = 0;
+ static const char *arg_destination = NULL;
+static int arg_sender_pid = 0;
+static int arg_receiver_pid = 0;
+static bool arg_pid = false;
+static bool arg_dot = false;
+static bool monitor_run_condi = true;
+static bool arg_well_known_names;
STATIC_DESTRUCTOR_REGISTER(arg_matches, strv_freep);
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
"Introspect the bus.\n\n"
- " -h --help Show this help\n"
- " --version Show package version\n"
- " --no-pager Do not pipe output into a pager\n"
- " --no-legend Do not show the headers and footers\n"
- " --system Connect to system bus\n"
- " --user Connect to user bus\n"
- " -H --host=[USER@]HOST Operate on remote host\n"
- " -M --machine=CONTAINER Operate on local container\n"
- " --address=ADDRESS Connect to bus specified by address\n"
- " --show-machine Show machine ID column in list\n"
- " --unique Only show unique names\n"
- " --acquired Only show acquired names\n"
- " --activatable Only show activatable names\n"
- " --match=MATCH Only show matching messages\n"
- " --size=SIZE Maximum length of captured packet\n"
- " --list Don't show tree, but simple object path list\n"
- " -q --quiet Don't show method call reply\n"
- " --verbose Show result values in long format\n"
- " --json=MODE Output as JSON\n"
- " -j Same as --json=pretty on tty, --json=short otherwise\n"
- " --expect-reply=BOOL Expect a method call reply\n"
- " --auto-start=BOOL Auto-start destination service\n"
+ " -h --help Show this help\n"
+ " --version Show package version\n"
+ " --no-pager Do not pipe output into a pager\n"
+ " --no-legend Do not show the headers and footers\n"
+ " --system Connect to system bus\n"
+ " --user Connect to user bus\n"
+ " -H --host=[USER@]HOST Operate on remote host\n"
+ " -M --machine=CONTAINER Operate on local container\n"
+ " --address=ADDRESS Connect to bus specified by address\n"
+ " --show-machine Show machine ID column in list\n"
+ " --unique Only show unique names\n"
+ " --acquired Only show acquired names\n"
+ " --activatable Only show activatable names\n"
+ " --match=MATCH Only show matching messages\n"
+ " --size=SIZE Maximum length of captured packet\n"
+ " --list Don't show tree, but simple object path list\n"
+ " -q --quiet Don't show method call reply\n"
+ " --verbose Show result values in long format\n"
+ " --json=MODE Output as JSON\n"
+ " -j Same as --json=pretty on tty, --json=short otherwise\n"
+ " --expect-reply=BOOL Expect a method call reply\n"
+ " --auto-start=BOOL Auto-start destination service\n"
" --allow-interactive-authorization=BOOL\n"
- " Allow interactive authorization for operation\n"
- " --timeout=SECS Maximum time to wait for method call completion\n"
- " --augment-creds=BOOL Extend credential data with data read from /proc/$PID\n"
- " --watch-bind=BOOL Wait for bus AF_UNIX socket to be bound in the file\n"
- " system\n\n"
+ " Allow interactive authorization for operation\n"
+ " --timeout=SECS Maximum time to wait for method call completion\n"
+ " --augment-creds=BOOL Extend credential data with data read from /proc/$PID\n"
+ " --watch-bind=BOOL Wait for bus AF_UNIX socket to be bound in the file\n"
+ " system\n"
+ " --destination=SERVICE Destination service of a signal\n"
+ "\n"
+ " --pid=PID Only show messages with pid equals PID\n"
+ " --sender-pid=SENDER_PID\n"
+ " Only show message with sender pid equals SENDER_PID\n"
+ " --receiver-pid=RECEIVER_PID\n"
+ " Only show message with receiver pid equals RECEIVER_PID\n"
+ " --well-known-names=BOOL \n"
+ " Show well know names connected to unique names on graph\n"
- "Commands:\n"
- " list List bus names\n"
- " status [SERVICE] Show bus service, process or bus owner credentials\n"
- " monitor [SERVICE...] Show bus traffic\n"
+ "\nCommands:\n"
+ " list List bus names\n"
+ " status [SERVICE] Show bus service, process or bus owner credentials\n"
+ " monitor [SERVICE...] Show bus traffic\n"
+ " dot [SERVICE...] Generate bus traffic graph\n"
- " capture [SERVICE...] Capture bus traffic as pcap\n"
- " tree [SERVICE...] Show object tree of service\n"
+ " capture [SERVICE...] Capture bus traffic as pcap\n"
+ " tree [SERVICE...] Show object tree of service\n"
" introspect SERVICE OBJECT [INTERFACE]\n"
" call SERVICE OBJECT INTERFACE METHOD [SIGNATURE [ARGUMENT...]]\n"
- " Call a method\n"
+ " Call a method\n"
+ " emit OBJECT INTERFACE SIGNAL [SIGNATURE [ARGUMENT...]]\n"
+ " Emit a signal\n"
" get-property SERVICE OBJECT INTERFACE PROPERTY...\n"
- " Get property value\n"
+ " Get property value\n"
" set-property SERVICE OBJECT INTERFACE PROPERTY SIGNATURE ARGUMENT...\n"
- " Set property value\n"
- " help Show this help\n"
+ " Set property value\n"
+ " help Show this help\n"
"\nSee the %s for details.\n"
, program_invocation_short_name
, link
ARG_AUGMENT_CREDS,
ARG_WATCH_BIND,
ARG_JSON,
+ ARG_DESTINATION,
+ ARG_PID,
+ ARG_SENDER_PID,
+ ARG_RECEIVER_PID,
+ ARG_WELL_KNOWN_NAMES,
};
static const struct option options[] = {
{ "augment-creds", required_argument, NULL, ARG_AUGMENT_CREDS },
{ "watch-bind", required_argument, NULL, ARG_WATCH_BIND },
{ "json", required_argument, NULL, ARG_JSON },
+ { "destination", required_argument, NULL, ARG_DESTINATION },
+ { "pid", required_argument, NULL, ARG_PID},
+ { "sender-pid", required_argument, NULL, ARG_SENDER_PID},
+ { "receiver-pid", required_argument, NULL, ARG_RECEIVER_PID},
+ { "well-known-names", required_argument, NULL, ARG_WELL_KNOWN_NAMES},
{},
};
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Unknown JSON out mode: %s",
optarg);
+ break;
+
+ case ARG_PID:
+ arg_pid = true;
+ r = arg_parse_pid(optarg, true, true);
+ if (r < 0)
+ return r;
+ break;
+
+ case ARG_SENDER_PID:
+ r = arg_parse_pid(optarg, true, false);
+ if (r < 0)
+ return 0;
+ break;
+
+ case ARG_RECEIVER_PID:
+ r = arg_parse_pid(optarg, false, true);
+ if (r < 0)
+ return 0;
+ break;
+ case ARG_WELL_KNOWN_NAMES:
+ arg_well_known_names = parse_boolean(optarg);
break;
+ case ARG_DESTINATION:
+ arg_destination = optarg;
+ break;
+
case '?':
return -EINVAL;
--- /dev/null
- r = unit_watch_pid(UNIT(n), n->control_pid);
+/***
+ This file is part of systemd.
+
+ Copyright 2013 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <linux/kdbus.h>
+#include <sys/mman.h>
+#include <sys/ioctl.h>
+
+#include "alloc-util.h"
+#include "bus-internal.h"
+#include "bus-kernel.h"
+#include "bus-policy.h"
+#include "bus-util.h"
+#include "busname.h"
+#include "dbus-busname.h"
+#include "fd-util.h"
+#include "format-util.h"
++#include "memory-util.h"
+#include "parse-util.h"
+#include "process-util.h"
+#include "service.h"
+#include "serialize.h"
+#include "signal-util.h"
+#include "special.h"
+#include "string-table.h"
+#include "string-util.h"
+
+static const UnitActiveState state_translation_table[_BUSNAME_STATE_MAX] = {
+ [BUSNAME_DEAD] = UNIT_INACTIVE,
+ [BUSNAME_MAKING] = UNIT_ACTIVATING,
+ [BUSNAME_REGISTERED] = UNIT_ACTIVE,
+ [BUSNAME_LISTENING] = UNIT_ACTIVE,
+ [BUSNAME_RUNNING] = UNIT_ACTIVE,
+ [BUSNAME_SIGTERM] = UNIT_DEACTIVATING,
+ [BUSNAME_SIGKILL] = UNIT_DEACTIVATING,
+ [BUSNAME_FAILED] = UNIT_FAILED
+};
+
+static int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
+static int busname_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
+
+static void busname_init(Unit *u) {
+ BusName *n = BUSNAME(u);
+
+ assert(u);
+ assert(u->load_state == UNIT_STUB);
+
+ n->starter_fd = -1;
+ n->accept_fd = true;
+ n->activating = true;
+
+ n->timeout_usec = u->manager->default_timeout_start_usec;
+}
+
+static void busname_unwatch_control_pid(BusName *n) {
+ assert(n);
+
+ if (n->control_pid <= 0)
+ return;
+
+ unit_unwatch_pid(UNIT(n), n->control_pid);
+ n->control_pid = 0;
+}
+
+static void busname_free_policy(BusName *n) {
+ BusNamePolicy *p;
+
+ assert(n);
+
+ while ((p = n->policy)) {
+ LIST_REMOVE(policy, n->policy, p);
+
+ free(p->name);
+ free(p);
+ }
+}
+
+static void busname_close_fd(BusName *n) {
+ assert(n);
+
+ n->starter_event_source = sd_event_source_unref(n->starter_event_source);
+ n->starter_fd = safe_close(n->starter_fd);
+}
+
+static void busname_done(Unit *u) {
+ BusName *n = BUSNAME(u);
+
+ assert(n);
+
+ n->name = mfree(n->name);
+
+ busname_free_policy(n);
+ busname_unwatch_control_pid(n);
+ busname_close_fd(n);
+
+ unit_ref_unset(&n->service);
+
+ n->timer_event_source = sd_event_source_unref(n->timer_event_source);
+}
+
+static int busname_arm_timer(BusName *n, usec_t usec) {
+ int r;
+
+ assert(n);
+
+ if (n->timer_event_source) {
+ r = sd_event_source_set_time(n->timer_event_source, usec);
+ if (r < 0)
+ return r;
+
+ return sd_event_source_set_enabled(n->timer_event_source, SD_EVENT_ONESHOT);
+ }
+
+ if (usec == USEC_INFINITY)
+ return 0;
+
+ r = sd_event_add_time(
+ UNIT(n)->manager->event,
+ &n->timer_event_source,
+ CLOCK_MONOTONIC,
+ usec, 0,
+ busname_dispatch_timer, n);
+ if (r < 0)
+ return r;
+
+ (void) sd_event_source_set_description(n->timer_event_source, "busname-timer");
+
+ return 0;
+}
+
+static int busname_add_default_default_dependencies(BusName *n) {
+ int r;
+
+ assert(n);
+
+ r = unit_add_dependency_by_name(UNIT(n), UNIT_BEFORE, SPECIAL_BUSNAMES_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
+ if (r < 0)
+ return r;
+
+ if (MANAGER_IS_SYSTEM(UNIT(n)->manager)) {
+ r = unit_add_two_dependencies_by_name(UNIT(n), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
+ if (r < 0)
+ return r;
+ }
+
+ return unit_add_two_dependencies_by_name(UNIT(n), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
+}
+
+static int busname_add_extras(BusName *n) {
+ Unit *u = UNIT(n);
+ int r;
+
+ assert(n);
+
+ if (!n->name) {
+ r = unit_name_to_prefix(u->id, &n->name);
+ if (r < 0)
+ return r;
+ }
+
+ if (!u->description) {
+ r = unit_set_description(u, n->name);
+ if (r < 0)
+ return r;
+ }
+
+ if (n->activating) {
+ if (!UNIT_DEREF(n->service)) {
+ Unit *x;
+
+ r = unit_load_related_unit(u, ".service", &x);
+ if (r < 0)
+ return r;
+
+ unit_ref_set(&n->service, u, x);
+ }
+
+ r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(n->service), true, UNIT_DEPENDENCY_IMPLICIT);
+ if (r < 0)
+ return r;
+ }
+
+ if (u->default_dependencies) {
+ r = busname_add_default_default_dependencies(n);
+ if (r < 0)
+ return r;
+ }
+
+ return 0;
+}
+
+static int busname_verify(BusName *n) {
+ char *e;
+
+ assert(n);
+
+ if (UNIT(n)->load_state != UNIT_LOADED)
+ return 0;
+
+ if (!service_name_is_valid(n->name)) {
+ log_unit_error(UNIT(n), "Name= setting is not a valid service name Refusing.");
+ return -EINVAL;
+ }
+
+ e = strjoina(n->name, ".busname");
+ if (!unit_has_name(UNIT(n), e)) {
+ log_unit_error(UNIT(n), "Name= setting doesn't match unit name. Refusing.");
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static int busname_load(Unit *u) {
+ BusName *n = BUSNAME(u);
+ int r;
+
+ assert(u);
+ assert(u->load_state == UNIT_STUB);
+
+ r = unit_load_fragment_and_dropin(u);
+ if (r < 0)
+ return r;
+
+ if (u->load_state == UNIT_LOADED) {
+ /* This is a new unit? Then let's add in some extras */
+ r = busname_add_extras(n);
+ if (r < 0)
+ return r;
+ }
+
+ return busname_verify(n);
+}
+
+static void busname_dump(Unit *u, FILE *f, const char *prefix) {
+ BusName *n = BUSNAME(u);
+
+ assert(n);
+ assert(f);
+
+ fprintf(f,
+ "%sBus Name State: %s\n"
+ "%sResult: %s\n"
+ "%sName: %s\n"
+ "%sActivating: %s\n"
+ "%sAccept FD: %s\n",
+ prefix, busname_state_to_string(n->state),
+ prefix, busname_result_to_string(n->result),
+ prefix, n->name,
+ prefix, yes_no(n->activating),
+ prefix, yes_no(n->accept_fd));
+
+ if (n->control_pid > 0)
+ fprintf(f,
+ "%sControl PID: "PID_FMT"\n",
+ prefix, n->control_pid);
+}
+
+static void busname_unwatch_fd(BusName *n) {
+ int r;
+
+ assert(n);
+
+ if (!n->starter_event_source)
+ return;
+
+ r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_OFF);
+ if (r < 0)
+ log_unit_debug_errno(UNIT(n), r, "Failed to disable event source: %m");
+}
+
+static int busname_watch_fd(BusName *n) {
+ int r;
+
+ assert(n);
+
+ if (n->starter_fd < 0)
+ return 0;
+
+ if (n->starter_event_source) {
+ r = sd_event_source_set_enabled(n->starter_event_source, SD_EVENT_ON);
+ if (r < 0)
+ goto fail;
+ } else {
+ r = sd_event_add_io(UNIT(n)->manager->event, &n->starter_event_source, n->starter_fd, EPOLLIN, busname_dispatch_io, n);
+ if (r < 0)
+ goto fail;
+
+ (void) sd_event_source_set_description(n->starter_event_source, "busname-starter");
+ }
+
+ return 0;
+
+fail:
+ log_unit_warning_errno(UNIT(n), r, "Failed to watch starter fd: %m");
+ busname_unwatch_fd(n);
+ return r;
+}
+
+static int busname_open_fd(BusName *n) {
+ _cleanup_free_ char *path = NULL;
+ const char *mode;
+
+ assert(n);
+
+ if (n->starter_fd >= 0)
+ return 0;
+
+ mode = MANAGER_IS_SYSTEM(UNIT(n)->manager) ? "system" : "user";
+ n->starter_fd = bus_kernel_open_bus_fd(mode, &path);
+ if (n->starter_fd < 0)
+ return log_unit_warning_errno(UNIT(n), n->starter_fd, "Failed to open %s: %m", path ?: "kdbus");
+
+ return 0;
+}
+
+static void busname_set_state(BusName *n, BusNameState state) {
+ BusNameState old_state;
+ assert(n);
+
+ old_state = n->state;
+ n->state = state;
+
+ if (!IN_SET(state, BUSNAME_MAKING, BUSNAME_SIGTERM, BUSNAME_SIGKILL)) {
+ n->timer_event_source = sd_event_source_unref(n->timer_event_source);
+ busname_unwatch_control_pid(n);
+ }
+
+ if (state != BUSNAME_LISTENING)
+ busname_unwatch_fd(n);
+
+ if (!IN_SET(state, BUSNAME_LISTENING, BUSNAME_MAKING, BUSNAME_REGISTERED, BUSNAME_RUNNING))
+ busname_close_fd(n);
+
+ if (state != old_state)
+ log_unit_debug(UNIT(n), "Changed %s -> %s", busname_state_to_string(old_state), busname_state_to_string(state));
+
+ unit_notify(UNIT(n), state_translation_table[old_state], state_translation_table[state], true);
+}
+
+static int busname_coldplug(Unit *u) {
+ BusName *n = BUSNAME(u);
+ int r;
+
+ assert(n);
+ assert(n->state == BUSNAME_DEAD);
+
+ if (n->deserialized_state == n->state)
+ return 0;
+
+ if (n->control_pid > 0 &&
+ pid_is_unwaited(n->control_pid) &&
+ IN_SET(n->deserialized_state, BUSNAME_MAKING, BUSNAME_SIGTERM, BUSNAME_SIGKILL)) {
+
- r = unit_watch_pid(UNIT(n), pid);
++ r = unit_watch_pid(UNIT(n), n->control_pid, false);
+ if (r < 0)
+ return r;
+
+ r = busname_arm_timer(n, usec_add(u->state_change_timestamp.monotonic, n->timeout_usec));
+ if (r < 0)
+ return r;
+ }
+
+ if (IN_SET(n->deserialized_state, BUSNAME_MAKING, BUSNAME_LISTENING, BUSNAME_REGISTERED, BUSNAME_RUNNING)) {
+ r = busname_open_fd(n);
+ if (r < 0)
+ return r;
+ }
+
+ if (n->deserialized_state == BUSNAME_LISTENING) {
+ r = busname_watch_fd(n);
+ if (r < 0)
+ return r;
+ }
+
+ busname_set_state(n, n->deserialized_state);
+ return 0;
+}
+
+static int busname_make_starter(BusName *n, pid_t *_pid) {
+ pid_t pid;
+ int r;
+
+ r = busname_arm_timer(n, usec_add(now(CLOCK_MONOTONIC), n->timeout_usec));
+ if (r < 0)
+ goto fail;
+
+ /* We have to resolve the user/group names out-of-process,
+ * hence let's fork here. It's messy, but well, what can we
+ * do? */
+
+ pid = fork();
+ if (pid < 0)
+ return -errno;
+
+ if (pid == 0) {
+ int ret;
+
+ (void) default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE, -1);
+ (void) ignore_signals(SIGPIPE, -1);
+ log_forget_fds();
+
+ r = bus_kernel_make_starter(n->starter_fd, n->name, n->activating, n->accept_fd, n->policy, n->policy_world);
+ if (r < 0) {
+ ret = EXIT_MAKE_STARTER;
+ goto fail_child;
+ }
+
+ _exit(0);
+
+ fail_child:
+ log_open();
+ log_error_errno(r, "Failed to create starter connection at step %s: %m", exit_status_to_string(ret, EXIT_STATUS_SYSTEMD));
+
+ _exit(ret);
+ }
+
- r = manager_add_job(UNIT(n)->manager, JOB_START, UNIT_DEREF(n->service), JOB_REPLACE, &error, NULL);
++ r = unit_watch_pid(UNIT(n), pid, true);
+ if (r < 0)
+ goto fail;
+
+ *_pid = pid;
+ return 0;
+
+fail:
+ n->timer_event_source = sd_event_source_unref(n->timer_event_source);
+ return r;
+}
+
+static void busname_enter_dead(BusName *n, BusNameResult f) {
+ assert(n);
+
+ if (n->result == BUSNAME_SUCCESS)
+ n->result = f;
+
+ busname_set_state(n, n->result != BUSNAME_SUCCESS ? BUSNAME_FAILED : BUSNAME_DEAD);
+}
+
+static void busname_enter_signal(BusName *n, BusNameState state, BusNameResult f) {
+ KillContext kill_context = {};
+ int r;
+
+ assert(n);
+
+ if (n->result == BUSNAME_SUCCESS)
+ n->result = f;
+
+ kill_context_init(&kill_context);
+
+ r = unit_kill_context(UNIT(n),
+ &kill_context,
+ state != BUSNAME_SIGTERM ? KILL_KILL : KILL_TERMINATE,
+ -1,
+ n->control_pid,
+ false);
+ if (r < 0) {
+ log_unit_warning_errno(UNIT(n), r, "Failed to kill control process: %m");
+ goto fail;
+ }
+
+ if (r > 0) {
+ r = busname_arm_timer(n, usec_add(now(CLOCK_MONOTONIC), n->timeout_usec));
+ if (r < 0) {
+ log_unit_warning_errno(UNIT(n), r, "Failed to arm timer: %m");
+ goto fail;
+ }
+
+ busname_set_state(n, state);
+ } else if (state == BUSNAME_SIGTERM)
+ busname_enter_signal(n, BUSNAME_SIGKILL, BUSNAME_SUCCESS);
+ else
+ busname_enter_dead(n, BUSNAME_SUCCESS);
+
+ return;
+
+fail:
+ busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
+}
+
+static void busname_enter_listening(BusName *n) {
+ int r;
+
+ assert(n);
+
+ if (n->activating) {
+ r = busname_watch_fd(n);
+ if (r < 0) {
+ log_unit_warning_errno(UNIT(n), r, "Failed to watch names: %m");
+ goto fail;
+ }
+
+ busname_set_state(n, BUSNAME_LISTENING);
+ } else
+ busname_set_state(n, BUSNAME_REGISTERED);
+
+ return;
+
+fail:
+ busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_FAILURE_RESOURCES);
+}
+
+static void busname_enter_making(BusName *n) {
+ int r;
+
+ assert(n);
+
+ r = busname_open_fd(n);
+ if (r < 0)
+ goto fail;
+
+ if (n->policy) {
+ /* If there is a policy, we need to resolve user/group
+ * names, which we can't do from PID1, hence let's
+ * fork. */
+ busname_unwatch_control_pid(n);
+
+ r = busname_make_starter(n, &n->control_pid);
+ if (r < 0) {
+ log_unit_warning_errno(UNIT(n), r, "Failed to fork 'making' task: %m");
+ goto fail;
+ }
+
+ busname_set_state(n, BUSNAME_MAKING);
+ } else {
+ /* If there is no policy, we can do everything
+ * directly from PID 1, hence do so. */
+
+ r = bus_kernel_make_starter(n->starter_fd, n->name, n->activating, n->accept_fd, NULL, n->policy_world);
+ if (r < 0) {
+ log_unit_warning_errno(UNIT(n), r, "Failed to make starter: %m");
+ goto fail;
+ }
+
+ busname_enter_listening(n);
+ }
+
+ return;
+
+fail:
+ busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
+}
+
+static void busname_enter_running(BusName *n) {
+ _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
+ bool pending = false;
+ Unit *other;
+ Iterator i;
+ int r;
+ void *v;
+
+ assert(n);
+
+ if (!n->activating)
+ return;
+
+ /* We don't take connections anymore if we are supposed to
+ * shut down anyway */
+
+ if (unit_stop_pending(UNIT(n))) {
+ log_unit_debug(UNIT(n), "Suppressing activation request since unit stop is scheduled.");
+
+ /* Flush all queued activation reqeuest by closing and reopening the connection */
+ bus_kernel_drop_one(n->starter_fd);
+
+ busname_enter_listening(n);
+ return;
+ }
+
+ /* If there's already a start pending don't bother to do
+ * anything */
+ HASHMAP_FOREACH_KEY(v, other, UNIT(n)->dependencies[UNIT_TRIGGERS], i)
+ if (unit_active_or_pending(other)) {
+ pending = true;
+ break;
+ }
+
+ if (!pending) {
+ if (!UNIT_ISSET(n->service)) {
+ log_unit_error(UNIT(n), "Service to activate vanished, refusing activation.");
+ r = -ENOENT;
+ goto fail;
+ }
+
- r = unit_start_limit_test(u);
++ r = manager_add_job(UNIT(n)->manager, JOB_START, UNIT_DEREF(n->service), JOB_REPLACE, NULL, &error, NULL);
+ if (r < 0)
+ goto fail;
+ }
+
+ busname_set_state(n, BUSNAME_RUNNING);
+ return;
+
+fail:
+ log_unit_warning(UNIT(n), "Failed to queue service startup job: %s", bus_error_message(&error, r));
+ busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
+}
+
+static int busname_start(Unit *u) {
+ BusName *n = BUSNAME(u);
+ int r;
+
+ assert(n);
+
+ /* We cannot fulfill this request right now, try again later
+ * please! */
+ if (IN_SET(n->state, BUSNAME_SIGTERM, BUSNAME_SIGKILL))
+ return -EAGAIN;
+
+ /* Already on it! */
+ if (n->state == BUSNAME_MAKING)
+ return 0;
+
+ if (n->activating && UNIT_ISSET(n->service)) {
+ Service *service;
+
+ service = SERVICE(UNIT_DEREF(n->service));
+
+ if (UNIT(service)->load_state != UNIT_LOADED) {
+ log_unit_error(u, "Bus service %s not loaded, refusing.", UNIT(service)->id);
+ return -ENOENT;
+ }
+ }
+
+ assert(IN_SET(n->state, BUSNAME_DEAD, BUSNAME_FAILED));
+
++ r = unit_test_start_limit(u);
+ if (r < 0) {
+ busname_enter_dead(n, BUSNAME_FAILURE_START_LIMIT_HIT);
+ return r;
+ }
+
+ r = unit_acquire_invocation_id(u);
+ if (r < 0)
+ return r;
+
+ n->result = BUSNAME_SUCCESS;
+ busname_enter_making(n);
+
+ return 1;
+}
+
+static int busname_stop(Unit *u) {
+ BusName *n = BUSNAME(u);
+
+ assert(n);
+
+ /* Already on it */
+ if (IN_SET(n->state, BUSNAME_SIGTERM, BUSNAME_SIGKILL))
+ return 0;
+
+ /* If there's already something running, we go directly into
+ * kill mode. */
+
+ if (n->state == BUSNAME_MAKING) {
+ busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_SUCCESS);
+ return -EAGAIN;
+ }
+
+ assert(IN_SET(n->state, BUSNAME_REGISTERED, BUSNAME_LISTENING, BUSNAME_RUNNING));
+
+ busname_enter_dead(n, BUSNAME_SUCCESS);
+ return 1;
+}
+
+static int busname_serialize(Unit *u, FILE *f, FDSet *fds) {
+ BusName *n = BUSNAME(u);
+ int r;
+
+ assert(n);
+ assert(f);
+ assert(fds);
+
+ (void) serialize_item(f, "state", busname_state_to_string(n->state));
+ (void) serialize_item(f, "result", busname_result_to_string(n->result));
+
+ if (n->control_pid > 0)
+ (void) serialize_item_format(f, "control-pid", PID_FMT, n->control_pid);
+
+ r = serialize_fd(f, fds, "starter-fd", n->starter_fd);
+ if (r < 0)
+ return r;
+
+ return 0;
+}
+
+static int busname_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
+ BusName *n = BUSNAME(u);
+
+ assert(n);
+ assert(key);
+ assert(value);
+
+ if (streq(key, "state")) {
+ BusNameState state;
+
+ state = busname_state_from_string(value);
+ if (state < 0)
+ log_unit_debug(u, "Failed to parse state value: %s", value);
+ else
+ n->deserialized_state = state;
+
+ } else if (streq(key, "result")) {
+ BusNameResult f;
+
+ f = busname_result_from_string(value);
+ if (f < 0)
+ log_unit_debug(u, "Failed to parse result value: %s", value);
+ else if (f != BUSNAME_SUCCESS)
+ n->result = f;
+
+ } else if (streq(key, "control-pid")) {
+ pid_t pid;
+
+ if (parse_pid(value, &pid) < 0)
+ log_unit_debug(u, "Failed to parse control-pid value: %s", value);
+ else
+ n->control_pid = pid;
+ } else if (streq(key, "starter-fd")) {
+ int fd;
+
+ if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
+ log_unit_debug(u, "Failed to parse starter fd value: %s", value);
+ else {
+ safe_close(n->starter_fd);
+ n->starter_fd = fdset_remove(fds, fd);
+ }
+ } else
+ log_unit_debug(u, "Unknown serialization key: %s", key);
+
+ return 0;
+}
+
+_pure_ static UnitActiveState busname_active_state(Unit *u) {
+ assert(u);
+
+ return state_translation_table[BUSNAME(u)->state];
+}
+
+_pure_ static const char *busname_sub_state_to_string(Unit *u) {
+ assert(u);
+
+ return busname_state_to_string(BUSNAME(u)->state);
+}
+
+static int busname_peek_message(BusName *n) {
+ struct kdbus_cmd_recv cmd_recv = {
+ .size = sizeof(cmd_recv),
+ .flags = KDBUS_RECV_PEEK,
+ };
+ struct kdbus_cmd_free cmd_free = {
+ .size = sizeof(cmd_free),
+ };
+ const char *comm = NULL;
+ struct kdbus_item *d;
+ struct kdbus_msg *k;
+ size_t start, ps, sz, delta;
+ void *p = MAP_FAILED;
+ pid_t pid = 0;
+ int r;
+
+ /* Generate a friendly debug log message about which process
+ * caused triggering of this bus name. This simply peeks the
+ * metadata of the first queued message and logs it. */
+
+ assert(n);
+
+ /* Let's shortcut things a bit, if debug logging is turned off
+ * anyway. */
+
+ if (log_get_max_level() < LOG_DEBUG)
+ return 0;
+
+ r = ioctl(n->starter_fd, KDBUS_CMD_RECV, &cmd_recv);
+ if (r < 0) {
+ if (errno == EINTR || errno == EAGAIN)
+ return 0;
+
+ return log_unit_error_errno(UNIT(n), errno, "Failed to query activation message: %m");
+ }
+
+ /* We map as late as possible, and unmap imemdiately after
+ * use. On 32bit address space is scarce and we want to be
+ * able to handle a lot of activator connections at the same
+ * time, and hence shouldn't keep the mmap()s around for
+ * longer than necessary. */
+
+ ps = page_size();
+ start = (cmd_recv.msg.offset / ps) * ps;
+ delta = cmd_recv.msg.offset - start;
+ sz = PAGE_ALIGN(delta + cmd_recv.msg.msg_size);
+
+ p = mmap(NULL, sz, PROT_READ, MAP_SHARED, n->starter_fd, start);
+ if (p == MAP_FAILED) {
+ r = log_unit_error_errno(UNIT(n), errno, "Failed to map activation message: %m");
+ goto finish;
+ }
+
+ k = (struct kdbus_msg *) ((uint8_t *) p + delta);
+ KDBUS_ITEM_FOREACH(d, k, items) {
+ switch (d->type) {
+
+ case KDBUS_ITEM_PIDS:
+ pid = d->pids.pid;
+ break;
+
+ case KDBUS_ITEM_PID_COMM:
+ comm = d->str;
+ break;
+ }
+ }
+
+ if (pid > 0)
+ log_unit_debug(UNIT(n), "Activation triggered by process " PID_FMT " (%s)", pid, strna(comm));
+
+ r = 0;
+
+finish:
+ if (p != MAP_FAILED)
+ (void) munmap(p, sz);
+
+ cmd_free.offset = cmd_recv.msg.offset;
+ if (ioctl(n->starter_fd, KDBUS_CMD_FREE, &cmd_free) < 0)
+ log_unit_warning(UNIT(n), "Failed to free peeked message, ignoring: %m");
+
+ return r;
+}
+
+static int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
+ BusName *n = userdata;
+
+ assert(n);
+ assert(fd >= 0);
+
+ if (n->state != BUSNAME_LISTENING)
+ return 0;
+
+ log_unit_debug(UNIT(n), "Activation request");
+
+ if (revents != EPOLLIN) {
+ log_unit_error(UNIT(n), "Got unexpected poll event (0x%x) on starter fd.", revents);
+ goto fail;
+ }
+
+ busname_peek_message(n);
+ busname_enter_running(n);
+ return 0;
+fail:
+
+ busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES);
+ return 0;
+}
+
+static void busname_sigchld_event(Unit *u, pid_t pid, int code, int status) {
+ BusName *n = BUSNAME(u);
+ BusNameResult f;
+
+ assert(n);
+ assert(pid >= 0);
+
+ if (pid != n->control_pid)
+ return;
+
+ n->control_pid = 0;
+
+ if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
+ f = BUSNAME_SUCCESS;
+ else if (code == CLD_EXITED)
+ f = BUSNAME_FAILURE_EXIT_CODE;
+ else if (code == CLD_KILLED)
+ f = BUSNAME_FAILURE_SIGNAL;
+ else if (code == CLD_DUMPED)
+ f = BUSNAME_FAILURE_CORE_DUMP;
+ else
+ assert_not_reached("Unknown sigchld code");
+
+ log_unit_full(u, f == BUSNAME_SUCCESS ? LOG_DEBUG : LOG_NOTICE, 0,
+ "Control process exited, code=%s status=%i", sigchld_code_to_string(code), status);
+
+ if (n->result == BUSNAME_SUCCESS)
+ n->result = f;
+
+ switch (n->state) {
+
+ case BUSNAME_MAKING:
+ if (f == BUSNAME_SUCCESS)
+ busname_enter_listening(n);
+ else
+ busname_enter_signal(n, BUSNAME_SIGTERM, f);
+ break;
+
+ case BUSNAME_SIGTERM:
+ case BUSNAME_SIGKILL:
+ busname_enter_dead(n, f);
+ break;
+
+ default:
+ assert_not_reached("Uh, control process died at wrong time.");
+ }
+
+ /* Notify clients about changed exit status */
+ unit_add_to_dbus_queue(u);
+}
+
+static int busname_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
+ BusName *n = BUSNAME(userdata);
+
+ assert(n);
+ assert(n->timer_event_source == source);
+
+ switch (n->state) {
+
+ case BUSNAME_MAKING:
+ log_unit_warning(UNIT(n), "Making timed out. Terminating.");
+ busname_enter_signal(n, BUSNAME_SIGTERM, BUSNAME_FAILURE_TIMEOUT);
+ break;
+
+ case BUSNAME_SIGTERM:
+ log_unit_warning(UNIT(n), "Stopping timed out. Killing.");
+ busname_enter_signal(n, BUSNAME_SIGKILL, BUSNAME_FAILURE_TIMEOUT);
+ break;
+
+ case BUSNAME_SIGKILL:
+ log_unit_warning(UNIT(n), "Processes still around after SIGKILL. Ignoring.");
+ busname_enter_dead(n, BUSNAME_FAILURE_TIMEOUT);
+ break;
+
+ default:
+ assert_not_reached("Timeout at wrong time.");
+ }
+
+ return 0;
+}
+
+static void busname_reset_failed(Unit *u) {
+ BusName *n = BUSNAME(u);
+
+ assert(n);
+
+ if (n->state == BUSNAME_FAILED)
+ busname_set_state(n, BUSNAME_DEAD);
+
+ n->result = BUSNAME_SUCCESS;
+}
+
+static void busname_trigger_notify(Unit *u, Unit *other) {
+ BusName *n = BUSNAME(u);
+
+ assert(n);
+ assert(other);
+
+ if (!IN_SET(n->state, BUSNAME_RUNNING, BUSNAME_LISTENING))
+ return;
+
+ if (other->start_limit_hit) {
+ busname_enter_dead(n, BUSNAME_FAILURE_SERVICE_START_LIMIT_HIT);
+ return;
+ }
+
+ if (other->load_state != UNIT_LOADED || other->type != UNIT_SERVICE)
+ return;
+
+ if (IN_SET(SERVICE(other)->state,
+ SERVICE_DEAD, SERVICE_FAILED,
+ SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
+ SERVICE_AUTO_RESTART))
+ busname_enter_listening(n);
+
+ if (SERVICE(other)->state == SERVICE_RUNNING)
+ busname_set_state(n, BUSNAME_RUNNING);
+}
+
+static int busname_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
+ return unit_kill_common(u, who, signo, -1, BUSNAME(u)->control_pid, error);
+}
+
+static int busname_get_timeout(Unit *u, usec_t *timeout) {
+ BusName *n = BUSNAME(u);
+ usec_t t;
+ int r;
+
+ if (!n->timer_event_source)
+ return 0;
+
+ r = sd_event_source_get_time(n->timer_event_source, &t);
+ if (r < 0)
+ return r;
+ if (t == USEC_INFINITY)
+ return 0;
+
+ *timeout = t;
+ return 1;
+}
+
+static bool busname_supported(void) {
+ return is_kdbus_available();
+}
+
+static int busname_control_pid(Unit *u) {
+ BusName *n = BUSNAME(u);
+
+ assert(n);
+
+ return n->control_pid;
+}
+
+static const char* const busname_result_table[_BUSNAME_RESULT_MAX] = {
+ [BUSNAME_SUCCESS] = "success",
+ [BUSNAME_FAILURE_RESOURCES] = "resources",
+ [BUSNAME_FAILURE_TIMEOUT] = "timeout",
+ [BUSNAME_FAILURE_EXIT_CODE] = "exit-code",
+ [BUSNAME_FAILURE_SIGNAL] = "signal",
+ [BUSNAME_FAILURE_CORE_DUMP] = "core-dump",
+ [BUSNAME_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
+ [BUSNAME_FAILURE_SERVICE_START_LIMIT_HIT] = "service-start-limit-hit",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(busname_result, BusNameResult);
+
+const UnitVTable busname_vtable = {
+ .object_size = sizeof(BusName),
+
+ .sections =
+ "Unit\0"
+ "BusName\0"
+ "Install\0",
+ .private_section = "BusName",
+
+ .init = busname_init,
+ .done = busname_done,
+ .load = busname_load,
+
+ .coldplug = busname_coldplug,
+
+ .dump = busname_dump,
+
+ .start = busname_start,
+ .stop = busname_stop,
+
+ .kill = busname_kill,
+
+ .get_timeout = busname_get_timeout,
+
+ .serialize = busname_serialize,
+ .deserialize_item = busname_deserialize_item,
+
+ .active_state = busname_active_state,
+ .sub_state_to_string = busname_sub_state_to_string,
+
+ .sigchld_event = busname_sigchld_event,
+
+ .trigger_notify = busname_trigger_notify,
+
+ .reset_failed = busname_reset_failed,
+
+ .supported = busname_supported,
+
+ .control_pid = busname_control_pid,
+
+ .bus_vtable = bus_busname_vtable,
+
+ .status_message_formats = {
+ .finished_start_job = {
+ [JOB_DONE] = "Listening on %s.",
+ [JOB_FAILED] = "Failed to listen on %s.",
+ },
+ .finished_stop_job = {
+ [JOB_DONE] = "Closed %s.",
+ [JOB_FAILED] = "Failed stopping %s.",
+ },
+ },
+};
#include "stdio-util.h"
#include "string-table.h"
#include "string-util.h"
+#include "umask-util.h"
+#include "smack-util.h"
#include "virt.h"
- #define CGROUP_CPU_QUOTA_PERIOD_USEC ((usec_t) 100 * USEC_PER_MSEC)
+ #define CGROUP_CPU_QUOTA_DEFAULT_PERIOD_USEC ((usec_t) 100 * USEC_PER_MSEC)
/* Returns the log level to use when cgroup attribute writes fail. When an attribute is missing or we have access
* problems we downgrade to LOG_DEBUG. This is supposed to be nice to container managers and kernels which want to mask
SD_BUS_PROPERTY("TemporaryFileSystem", "a(ss)", property_get_temporary_filesystems, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("MountAPIVFS", "b", bus_property_get_bool, offsetof(ExecContext, mount_apivfs), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("KeyringMode", "s", property_get_exec_keyring_mode, offsetof(ExecContext, keyring_mode), SD_BUS_VTABLE_PROPERTY_CONST),
+ SD_BUS_PROPERTY("ProtectHostname", "b", bus_property_get_bool, offsetof(ExecContext, protect_hostname), SD_BUS_VTABLE_PROPERTY_CONST),
+ SD_BUS_PROPERTY("NetworkNamespacePath", "s", NULL, offsetof(ExecContext, network_namespace_path), SD_BUS_VTABLE_PROPERTY_CONST),
/* Obsolete/redundant properties: */
- SD_BUS_PROPERTY("Capabilities", "s", property_get_empty_string, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
+ SD_BUS_PROPERTY("Capabilities", "s", property_get_capabilities, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("ReadWriteDirectories", "as", NULL, offsetof(ExecContext, read_write_paths), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
SD_BUS_PROPERTY("ReadOnlyDirectories", "as", NULL, offsetof(ExecContext, read_only_paths), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
SD_BUS_PROPERTY("InaccessibleDirectories", "as", NULL, offsetof(ExecContext, inaccessible_paths), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
%{nil}
%binfmt_apply() \
- @rootlibexecdir@/systemd-binfmt %{?*} >/dev/null 2>&1 || : \
+ %{expand:%%{?__systemd_someargs_%#}} \
+ [ -x @rootlibexecdir@/systemd-binfmt ] && @rootlibexecdir@/systemd-binfmt %{?*} || : \
%{nil}
+
+
+%install_service() \
+mkdir -p %{buildroot}/%{_unitdir}/%{1} \
+ln -s ../%{2} %{buildroot}/%{_unitdir}/%{1}/%{2} \
+%{nil}
load_fragment_gperf_c,
load_fragment_gperf_nulstr_c,
include_directories : includes,
- dependencies : [threads,
+ link_whole : libcore_shared,
+ dependencies : [versiondep,
+ threads,
+ libcap,
librt,
libseccomp,
libpam,
--- /dev/null
+/***
+ This file is part of systemd.
+
+ Copyright 2013 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
++#include <unistd.h>
++#include <sys/types.h>
++
+#include "alloc-util.h"
+#include "bus-internal.h"
+#include "bus-util.h"
+#include "cgroup-util.h"
+#include "conf-parser.h"
+#include "dirent-util.h"
+#include "fd-util.h"
+#include "fileio.h"
+#include "mkdir.h"
+#include "special.h"
+#include "unit-name.h"
+#include "util.h"
+
+static const char *arg_dest_late = "/tmp", *arg_dest = "/tmp";
+
+static int create_dbus_files(
+ const char *path,
+ const char *name,
+ const char *service,
+ const char *exec,
+ const char *user,
+ const char *type) {
+
+ _cleanup_free_ char *b = NULL, *s = NULL, *lnk = NULL;
+ _cleanup_fclose_ FILE *f = NULL;
+ int r;
+
+ assert(path);
+ assert(name);
+ assert(service || exec);
+
+ if (!service) {
+ _cleanup_free_ char *a = NULL;
+
+ s = strjoin("dbus-", name, ".service", NULL);
+ if (!s)
+ return log_oom();
+
+ a = strjoin(arg_dest_late, "/", s, NULL);
+ if (!a)
+ return log_oom();
+
+ f = fopen(a, "wxe");
+ if (!f)
+ return log_error_errno(errno, "Failed to create %s: %m", a);
+
+ fprintf(f,
+ "# Automatically generated by systemd-dbus1-generator\n\n"
+ "[Unit]\n"
+ "SourcePath=%s\n"
+ "Description=DBUS1: %s\n"
+ "Documentation=man:systemd-dbus1-generator(8)\n\n"
+ "[Service]\n"
+ "ExecStart=%s\n"
+ "Type=dbus\n"
+ "BusName=%s\n",
+ path,
+ name,
+ exec,
+ name);
+
+ if (user)
+ fprintf(f, "User=%s\n", user);
+
+
+ if (type) {
+ fprintf(f, "Environment=DBUS_STARTER_BUS_TYPE=%s\n", type);
+
+ if (streq(type, "system"))
+ fprintf(f, "Environment=DBUS_STARTER_ADDRESS=" DEFAULT_SYSTEM_BUS_ADDRESS "\n");
+ else if (streq(type, "session")) {
+ char *run;
+
+ run = getenv("XDG_RUNTIME_DIR");
+ if (!run) {
+ log_error("XDG_RUNTIME_DIR not set.");
+ return -EINVAL;
+ }
+
+ fprintf(f, "Environment=DBUS_STARTER_ADDRESS="KERNEL_USER_BUS_ADDRESS_FMT ";" UNIX_USER_BUS_ADDRESS_FMT "\n",
+ getuid(), run);
+ }
+ }
+
+ r = fflush_and_check(f);
+ if (r < 0)
+ return log_error_errno(r, "Failed to write %s: %m", a);
+
+ f = safe_fclose(f);
+
+ service = s;
+ }
+
+ b = strjoin(arg_dest_late, "/", name, ".busname", NULL);
+ if (!b)
+ return log_oom();
+
+ f = fopen(b, "wxe");
+ if (!f)
+ return log_error_errno(errno, "Failed to create %s: %m", b);
+
+ fprintf(f,
+ "# Automatically generated by systemd-dbus1-generator\n\n"
+ "[Unit]\n"
+ "SourcePath=%s\n"
+ "Description=DBUS1: %s\n"
+ "Documentation=man:systemd-dbus1-generator(8)\n\n"
+ "[BusName]\n"
+ "Name=%s\n"
+ "Service=%s\n"
+ "AllowWorld=talk\n",
+ path,
+ name,
+ name,
+ service);
+
+ r = fflush_and_check(f);
+ if (r < 0)
+ return log_error_errno(r, "Failed to write %s: %m", b);
+
+ lnk = strjoin(arg_dest_late, "/" SPECIAL_BUSNAMES_TARGET ".wants/", name, ".busname", NULL);
+ if (!lnk)
+ return log_oom();
+
+ mkdir_parents_label(lnk, 0755);
+ if (symlink(b, lnk))
+ return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
+
+ return 0;
+}
+
+static int add_dbus(const char *path, const char *fname, const char *type) {
+ _cleanup_free_ char *name = NULL, *exec = NULL, *user = NULL, *service = NULL;
+
+ const ConfigTableItem table[] = {
+ { "D-BUS Service", "Name", config_parse_string, 0, &name },
+ { "D-BUS Service", "Exec", config_parse_string, 0, &exec },
+ { "D-BUS Service", "User", config_parse_string, 0, &user },
+ { "D-BUS Service", "SystemdService", config_parse_string, 0, &service },
+ { },
+ };
+
+ char *p;
+ int r;
+
+ assert(path);
+ assert(fname);
+
+ p = strjoina(path, "/", fname);
+ r = config_parse(NULL, p, NULL,
+ "D-BUS Service\0",
+ config_item_table_lookup, table,
+ CONFIG_PARSE_RELAXED|CONFIG_PARSE_WARN, NULL);
+ if (r < 0)
+ return r;
+
+ if (!name) {
+ log_warning("Activation file %s lacks name setting, ignoring.", p);
+ return 0;
+ }
+
+ if (!service_name_is_valid(name)) {
+ log_warning("Bus service name %s is not valid, ignoring.", name);
+ return 0;
+ }
+
+ if (streq(name, "org.freedesktop.systemd1")) {
+ log_debug("Skipping %s, identified as systemd.", p);
+ return 0;
+ }
+
+ if (service) {
+ if (!unit_name_is_valid(service, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) {
+ log_warning("Unit name %s is not valid, ignoring.", service);
+ return 0;
+ }
+ if (!endswith(service, ".service")) {
+ log_warning("Bus names can only activate services, ignoring %s.", p);
+ return 0;
+ }
+ } else {
+ if (!exec || streq(exec, "/bin/false")) {
+ log_warning("Neither service name nor binary path specified, ignoring %s.", p);
+ return 0;
+ }
+
+ if (exec[0] != '/') {
+ log_warning("Exec= in %s does not start with an absolute path, ignoring.", p);
+ return 0;
+ }
+ }
+
+ return create_dbus_files(p, name, service, exec, user, type);
+}
+
+static int parse_dbus_fragments(const char *path, const char *type) {
+ _cleanup_closedir_ DIR *d = NULL;
+ struct dirent *de;
+ int r;
+
+ assert(path);
+ assert(type);
+
+ d = opendir(path);
+ if (!d) {
+ if (errno == -ENOENT)
+ return 0;
+
+ return log_error_errno(errno, "Failed to enumerate D-Bus activated services: %m");
+ }
+
+ r = 0;
+ FOREACH_DIRENT(de, d, goto fail) {
+ int q;
+
+ if (!endswith(de->d_name, ".service"))
+ continue;
+
+ q = add_dbus(path, de->d_name, type);
+ if (q < 0)
+ r = q;
+ }
+
+ return r;
+
+fail:
+ return log_error_errno(errno, "Failed to read D-Bus services directory: %m");
+}
+
+static int link_busnames_target(const char *units) {
+ const char *f, *t;
+
+ f = strjoina(units, "/" SPECIAL_BUSNAMES_TARGET);
+ t = strjoina(arg_dest, "/" SPECIAL_BASIC_TARGET ".wants/" SPECIAL_BUSNAMES_TARGET);
+
+ mkdir_parents_label(t, 0755);
+ if (symlink(f, t) < 0)
+ return log_error_errno(errno, "Failed to create symlink %s: %m", t);
+
+ return 0;
+}
+
+static int create_compatibility(const char *units, const char *type) {
+ const char *t;
+ _cleanup_fclose_ FILE *f = NULL;
+ int r;
+
+ t = strjoina(arg_dest, "/" SPECIAL_DBUS_SERVICE);
+ if (!t)
+ return log_oom();
+
+ f = fopen(t, "wxe");
+ if (!f)
+ return log_error_errno(errno, "Failed to create %s: %m", t);
+
+ fprintf(f,
+ "# Automatically generated by systemd-dbus1-generator\n\n"
+ "[Unit]\n"
+ "Description=DBUS1: dbus.service for kdbus compatibility\n"
+ "[Service]\n"
+ "ExecStart=/bin/true\n"
+ "RemainAfterExit=yes\n");
+
+ r = fflush_and_check(f);
+ if (r < 0)
+ return log_error_errno(r, "Failed to write %s: %m", t);
+
+ f = safe_fclose(f);
+
+
+ t = strjoina(arg_dest, "/" SPECIAL_DBUS_SOCKET);
+ if (!t)
+ return log_oom();
+
+ f = fopen(t, "wxe");
+ if (!f)
+ return log_error_errno(errno, "Failed to create %s: %m", t);
+
+ fprintf(f,
+ "# Automatically generated by systemd-dbus1-generator\n\n"
+ "[Unit]\n"
+ "Description=DBUS1: dbus.socket for kdbus compatibility\n"
+ "[Socket]\n"
+ "SmackLabelIPIn=^\n"
+ "ListenStream=@/tmp/.kdbus_bus_%s_%d\n", type, getuid());
+
+
+ r = fflush_and_check(f);
+ if (r < 0)
+ return log_error_errno(r, "Failed to write %s: %m", t);
+
+ f = safe_fclose(f);
+
+ return 0;
+}
+
+int main(int argc, char *argv[]) {
+ const char *path, *type, *units;
+ int r, q;
+
+ if (argc > 1 && argc != 4) {
+ log_error("This program takes three or no arguments.");
+ return EXIT_FAILURE;
+ }
+
+ if (argc > 1) {
+ arg_dest = argv[1];
+ arg_dest_late = argv[3];
+ }
+
+ log_set_prohibit_ipc(true);
+ log_set_target(LOG_TARGET_AUTO);
+ log_parse_environment();
+ log_open();
+
+ umask(0022);
+
+ if (!is_kdbus_available())
+ return 0;
+
+ r = cg_pid_get_owner_uid(0, NULL);
+ if (r >= 0) {
+ path = "/usr/share/dbus-1/services";
+ type = "session";
+ units = USER_DATA_UNIT_PATH;
+ } else if (r == -ENXIO) {
+ path = "/usr/share/dbus-1/system-services";
+ type = "system";
+ units = SYSTEM_DATA_UNIT_PATH;
+ } else
+ return log_error_errno(r, "Failed to determine whether we are running as user or system instance: %m");
+
+ r = parse_dbus_fragments(path, type);
+
+ /* FIXME: One day this should just be pulled in statically from basic.target */
+ q = link_busnames_target(units);
+ if (q < 0)
+ r = q;
+
+ q = create_compatibility(units, type);
+ if (q < 0)
+ r = q;
+
+ return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+}
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh
- [[ $1 == "add" ]] || exit 0
- [[ $2 ]] || exit 1
+PATH=/bin:/usr/bin:/sbin:/usr/sbin
+
+ COMMAND="$1"
+ KERNEL_VERSION="$2"
+ ENTRY_DIR_ABS="$3"
+ KERNEL_IMAGE="$4"
+ INITRD_OPTIONS_START="5"
- case "$1" in
+ [[ $KERNEL_VERSION ]] || exit 1
+
+ case "$COMMAND" in
add)
- [[ -d /lib/modules/"$2"/kernel ]] || exit 0
- exec depmod -a "$2"
+ [[ -d "/lib/modules/${KERNEL_VERSION}/kernel" ]] || exit 0
+ [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
+ echo "Running depmod -a ${KERNEL_VERSION}"
+ exec depmod -a "${KERNEL_VERSION}"
;;
remove)
- exec rm -f /lib/modules/"$2"/modules.{alias{,.bin},builtin.bin,dep{,.bin},devname,softdep,symbols{,.bin}}
+ [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
+ echo "Removing /lib/modules/${KERNEL_VERSION}/modules.dep and associated files"
+ exec rm -f /lib/modules/"${KERNEL_VERSION}"/modules.{alias{,.bin},builtin.bin,dep{,.bin},devname,softdep,symbols{,.bin}}
;;
*)
exit 0
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh
+PATH=/bin:/usr/bin:/sbin:/usr/sbin
+
COMMAND="$1"
KERNEL_VERSION="$2"
- BOOT_DIR_ABS="$3"
+ ENTRY_DIR_ABS="$3"
KERNEL_IMAGE="$4"
INITRD_OPTIONS_START="5"
--- /dev/null
- bus->rqueue[bus->rqueue_size++] = m;
+/* SPDX-License-Identifier: LGPL-2.1+ */
+/***
+ This file is part of systemd.
+
+ Copyright 2013 Lennart Poettering
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#if HAVE_VALGRIND_MEMCHECK_H
+#include <valgrind/memcheck.h>
+#endif
+
+#include <errno.h>
+#include <stddef.h>
+#include <sys/ioctl.h>
+
+#include "sd-bus.h"
+
+#include "alloc-util.h"
+#include "bus-bloom.h"
+#include "bus-control-kernel.h"
+#include "bus-internal.h"
+#include "bus-message.h"
+#include "bus-util.h"
+#include "bus-slot.h"
+#include "capability-util.h"
+#include "process-util.h"
+#include "stdio-util.h"
+#include "string-util.h"
+#include "strv.h"
+#include "user-util.h"
+
+int bus_request_name_kernel(sd_bus *bus, const char *name, uint64_t flags) {
+ struct kdbus_cmd *n;
+ size_t size, l;
+ int r;
+
+ assert(bus);
+ assert(name);
+
+ l = strlen(name) + 1;
+ size = offsetof(struct kdbus_cmd, items) + KDBUS_ITEM_SIZE(l);
+ n = alloca0_align(size, 8);
+ n->size = size;
+ n->flags = request_name_flags_to_kdbus(flags);
+
+ n->items[0].size = KDBUS_ITEM_HEADER_SIZE + l;
+ n->items[0].type = KDBUS_ITEM_NAME;
+ memcpy(n->items[0].str, name, l);
+
+#if HAVE_VALGRIND_MEMCHECK_H
+ VALGRIND_MAKE_MEM_DEFINED(n, n->size);
+#endif
+
+ r = ioctl(bus->input_fd, KDBUS_CMD_NAME_ACQUIRE, n);
+ if (r < 0)
+ return -errno;
+
+ if (n->return_flags & KDBUS_NAME_IN_QUEUE)
+ return 0;
+
+ return 1;
+}
+
+/* This is copied static func from sd-bus.c. But we want to keep kdbus-specific changes in this file as much as possible.
+ */
+static usec_t calc_elapse(sd_bus *bus, uint64_t usec) {
+ assert(bus);
+
+ if (usec == (uint64_t) -1)
+ return 0;
+
+ /* We start all timeouts the instant we enter BUS_HELLO/BUS_RUNNING state, so that the don't run in parallel
+ * with any connection setup states. Hence, if a method callback is started earlier than that we just store the
+ * relative timestamp, and afterwards the absolute one. */
+
+ if (IN_SET(bus->state, BUS_WATCH_BIND, BUS_OPENING, BUS_AUTHENTICATING))
+ return usec;
+ else
+ return now(CLOCK_MONOTONIC) + usec;
+}
+
+/* This is copied static func from sd-bus.c. But we want to keep kdbus-specific changes in this file as much as possible.
+ */
+static int timeout_compare(const void *a, const void *b) {
+ const struct reply_callback *x = a, *y = b;
+
+ if (x->timeout_usec != 0 && y->timeout_usec == 0)
+ return -1;
+
+ if (x->timeout_usec == 0 && y->timeout_usec != 0)
+ return 1;
+
+ if (x->timeout_usec < y->timeout_usec)
+ return -1;
+
+ if (x->timeout_usec > y->timeout_usec)
+ return 1;
+
+ return 0;
+}
+
+static int prepare_for_kernel_request(sd_bus *bus)
+{
+ int ret;
+ /* we want room for the return message */
+ ret = bus_rqueue_make_room(bus);
+ if (ret < 0)
+ return ret;
+
+ ret = ordered_hashmap_ensure_allocated(&bus->reply_callbacks, &uint64_hash_ops);
+ if (ret < 0)
+ return ret;
+
+ ret = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+
+static int prepare_int_reply(sd_bus *bus,
+ sd_bus_message **ret_m,
+ int reply,
+ const char *error_failed_message)
+{
+ int ret;
+ _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
+ uint64_t cookie;
+
+ cookie = ++bus->cookie;
+ if (reply < 0) {
+ _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
+
+ sd_bus_error_setf(&error, SD_BUS_ERROR_FAILED, "%s", error_failed_message);
+ ret = bus_message_new_synthetic_error(bus, cookie, &error, &m);
+ if (ret < 0)
+ return ret;
+ } else {
+ ret = bus_message_new_synthetic_reply(bus, cookie, &m);
+ if (ret < 0)
+ return ret;
+ sd_bus_message_append(m, "i", reply);
+ }
+
+ ret = bus_seal_synthetic_message(bus, m);
+ if (ret < 0)
+ return ret;
+
+ *ret_m = m;
+ m = NULL;
+ return 0;
+}
+
+static int enqueue_kernel_reply(
+ sd_bus *bus,
+ sd_bus_slot **ret_slot,
+ sd_bus_message *m,
+ sd_bus_message_handler_t callback,
+ void *userdata)
+{
+ int ret;
+ _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *s = NULL;
+
+ if (ret_slot || callback) {
+ s = bus_slot_allocate(bus, !ret_slot, BUS_REPLY_CALLBACK, sizeof(struct reply_callback), userdata);
+ if (!s)
+ return -ENOMEM;
+
+ s->reply_callback.callback = callback;
+
+ s->reply_callback.cookie = m->reply_cookie;
+ ret = ordered_hashmap_put(bus->reply_callbacks, &s->reply_callback.cookie, &s->reply_callback);
+ if (ret < 0) {
+ s->reply_callback.cookie = 0;
+ return ret;
+ }
+
+ s->reply_callback.timeout_usec = calc_elapse(bus, BUS_DEFAULT_TIMEOUT);
+ if (s->reply_callback.timeout_usec != 0) {
+ ret = prioq_put(bus->reply_callbacks_prioq, &s->reply_callback, &s->reply_callback.prioq_idx);
+ if (ret < 0) {
+ s->reply_callback.timeout_usec = 0;
+ return ret;
+ }
+ }
+ }
+
++ bus->rqueue[bus->rqueue_size++] = bus_message_ref_queued(m, bus);
+
+ if (ret_slot)
+ *ret_slot = s;
+ s = NULL;
+
+ return 0;
+}
+
+static int enqueue_int_kernel_reply(
+ sd_bus *bus,
+ sd_bus_slot **ret_slot,
+ int reply,
+ const char *error_string,
+ sd_bus_message_handler_t callback,
+ void *userdata) {
+ int ret;
+ _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
+
+ ret = prepare_int_reply(bus, &m, reply, error_string);
+ if (ret < 0)
+ return ret;
+
+ ret = enqueue_kernel_reply(bus, ret_slot, m, callback, userdata);
+ if (ret < 0)
+ return ret;
+
+ m = NULL;
+ return 1;
+}
+
+int bus_request_name_kernel_async(sd_bus *bus,
+ sd_bus_slot **ret_slot,
+ const char *name,
+ uint64_t flags,
+ sd_bus_message_handler_t callback,
+ void *userdata) {
+ int ret;
+
+ ret = prepare_for_kernel_request(bus);
+ if (ret < 0)
+ return ret;
+
+ return enqueue_int_kernel_reply(bus,
+ ret_slot,
+ bus_request_name_kernel(bus, name, flags),
+ "Request name failed",
+ callback,
+ userdata);
+}
+
+int bus_release_name_kernel(sd_bus *bus, const char *name) {
+ struct kdbus_cmd *n;
+ size_t size, l;
+ int r;
+
+ assert(bus);
+ assert(name);
+
+ l = strlen(name) + 1;
+ size = offsetof(struct kdbus_cmd, items) + KDBUS_ITEM_SIZE(l);
+ n = alloca0_align(size, 8);
+ n->size = size;
+
+ n->items[0].size = KDBUS_ITEM_HEADER_SIZE + l;
+ n->items[0].type = KDBUS_ITEM_NAME;
+ memcpy(n->items[0].str, name, l);
+
+#if HAVE_VALGRIND_MEMCHECK_H
+ VALGRIND_MAKE_MEM_DEFINED(n, n->size);
+#endif
+ r = ioctl(bus->input_fd, KDBUS_CMD_NAME_RELEASE, n);
+ if (r < 0)
+ return -errno;
+
+ return 0;
+}
+
+int bus_release_name_kernel_async(
+ sd_bus *bus,
+ sd_bus_slot **ret_slot,
+ const char *name,
+ sd_bus_message_handler_t callback,
+ void *userdata) {
+ int ret;
+
+ ret = prepare_for_kernel_request(bus);
+ if (ret < 0)
+ return ret;
+
+ return enqueue_int_kernel_reply(bus,
+ ret_slot,
+ bus_release_name_kernel(bus, name),
+ "Release name failed",
+ callback,
+ userdata);
+}
+
+static int kernel_get_list(sd_bus *bus, uint64_t flags, char ***x) {
+ struct kdbus_cmd_list cmd = {
+ .size = sizeof(cmd),
+ .flags = flags,
+ };
+ struct kdbus_info *name_list, *name;
+ uint64_t previous_id = 0;
+ int r;
+
+ /* Caller will free half-constructed list on failure... */
+
+ r = ioctl(bus->input_fd, KDBUS_CMD_LIST, &cmd);
+ if (r < 0)
+ return -errno;
+
+ name_list = (struct kdbus_info *) ((uint8_t *) bus->kdbus_buffer + cmd.offset);
+
+ KDBUS_FOREACH(name, name_list, cmd.list_size) {
+ struct kdbus_item *item;
+
+ if ((flags & KDBUS_LIST_UNIQUE) && name->id != previous_id && !(name->flags & KDBUS_HELLO_ACTIVATOR)) {
+ char *n;
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat"
+ if (asprintf(&n, ":1.%llu", name->id) < 0) {
+ r = -ENOMEM;
+ goto fail;
+ }
+#pragma GCC diagnostic pop
+
+ r = strv_consume(x, n);
+ if (r < 0)
+ goto fail;
+
+ previous_id = name->id;
+ }
+
+ KDBUS_ITEM_FOREACH(item, name, items) {
+ if (item->type == KDBUS_ITEM_OWNED_NAME) {
+ if (service_name_is_valid(item->name.name)) {
+ r = strv_extend(x, item->name.name);
+ if (r < 0) {
+ r = -ENOMEM;
+ goto fail;
+ }
+ }
+ }
+ }
+ }
+
+ r = 0;
+
+fail:
+ bus_kernel_cmd_free(bus, cmd.offset);
+ return r;
+}
+
+int bus_list_names_kernel(sd_bus *bus, char ***acquired, char ***activatable) {
+ _cleanup_strv_free_ char **x = NULL, **y = NULL;
+ int r;
+
+ if (acquired) {
+ r = kernel_get_list(bus, KDBUS_LIST_UNIQUE | KDBUS_LIST_NAMES, &x);
+ if (r < 0)
+ return r;
+ }
+
+ if (activatable) {
+ r = kernel_get_list(bus, KDBUS_LIST_ACTIVATORS, &y);
+ if (r < 0)
+ return r;
+
+ *activatable = y;
+ y = NULL;
+ }
+
+ if (acquired) {
+ *acquired = x;
+ x = NULL;
+ }
+
+ return 0;
+}
+
+static int bus_populate_creds_from_items(
+ sd_bus *bus,
+ struct kdbus_info *info,
+ uint64_t mask,
+ sd_bus_creds *c) {
+
+ struct kdbus_item *item;
+ uint64_t m;
+ int r;
+
+ assert(bus);
+ assert(info);
+ assert(c);
+
+ KDBUS_ITEM_FOREACH(item, info, items) {
+
+ switch (item->type) {
+
+ case KDBUS_ITEM_PIDS:
+
+ if (mask & SD_BUS_CREDS_PID && item->pids.pid > 0) {
+ c->pid = (pid_t) item->pids.pid;
+ c->mask |= SD_BUS_CREDS_PID;
+ }
+
+ if (mask & SD_BUS_CREDS_TID && item->pids.tid > 0) {
+ c->tid = (pid_t) item->pids.tid;
+ c->mask |= SD_BUS_CREDS_TID;
+ }
+
+ if (mask & SD_BUS_CREDS_PPID) {
+ if (item->pids.ppid > 0) {
+ c->ppid = (pid_t) item->pids.ppid;
+ c->mask |= SD_BUS_CREDS_PPID;
+ } else if (item->pids.pid == 1) {
+ /* The structure doesn't
+ * really distinguish the case
+ * where a process has no
+ * parent and where we don't
+ * know it because it could
+ * not be translated due to
+ * namespaces. However, we
+ * know that PID 1 has no
+ * parent process, hence let's
+ * patch that in, manually. */
+ c->ppid = 0;
+ c->mask |= SD_BUS_CREDS_PPID;
+ }
+ }
+
+ break;
+
+ case KDBUS_ITEM_CREDS:
+
+ if (mask & SD_BUS_CREDS_UID && (uid_t) item->creds.uid != UID_INVALID) {
+ c->uid = (uid_t) item->creds.uid;
+ c->mask |= SD_BUS_CREDS_UID;
+ }
+
+ if (mask & SD_BUS_CREDS_EUID && (uid_t) item->creds.euid != UID_INVALID) {
+ c->euid = (uid_t) item->creds.euid;
+ c->mask |= SD_BUS_CREDS_EUID;
+ }
+
+ if (mask & SD_BUS_CREDS_SUID && (uid_t) item->creds.suid != UID_INVALID) {
+ c->suid = (uid_t) item->creds.suid;
+ c->mask |= SD_BUS_CREDS_SUID;
+ }
+
+ if (mask & SD_BUS_CREDS_FSUID && (uid_t) item->creds.fsuid != UID_INVALID) {
+ c->fsuid = (uid_t) item->creds.fsuid;
+ c->mask |= SD_BUS_CREDS_FSUID;
+ }
+
+ if (mask & SD_BUS_CREDS_GID && (gid_t) item->creds.gid != GID_INVALID) {
+ c->gid = (gid_t) item->creds.gid;
+ c->mask |= SD_BUS_CREDS_GID;
+ }
+
+ if (mask & SD_BUS_CREDS_EGID && (gid_t) item->creds.egid != GID_INVALID) {
+ c->egid = (gid_t) item->creds.egid;
+ c->mask |= SD_BUS_CREDS_EGID;
+ }
+
+ if (mask & SD_BUS_CREDS_SGID && (gid_t) item->creds.sgid != GID_INVALID) {
+ c->sgid = (gid_t) item->creds.sgid;
+ c->mask |= SD_BUS_CREDS_SGID;
+ }
+
+ if (mask & SD_BUS_CREDS_FSGID && (gid_t) item->creds.fsgid != GID_INVALID) {
+ c->fsgid = (gid_t) item->creds.fsgid;
+ c->mask |= SD_BUS_CREDS_FSGID;
+ }
+
+ break;
+
+ case KDBUS_ITEM_PID_COMM:
+ if (mask & SD_BUS_CREDS_COMM) {
+ r = free_and_strdup(&c->comm, item->str);
+ if (r < 0)
+ return r;
+
+ c->mask |= SD_BUS_CREDS_COMM;
+ }
+ break;
+
+ case KDBUS_ITEM_TID_COMM:
+ if (mask & SD_BUS_CREDS_TID_COMM) {
+ r = free_and_strdup(&c->tid_comm, item->str);
+ if (r < 0)
+ return r;
+
+ c->mask |= SD_BUS_CREDS_TID_COMM;
+ }
+ break;
+
+ case KDBUS_ITEM_EXE:
+ if (mask & SD_BUS_CREDS_EXE) {
+ r = free_and_strdup(&c->exe, item->str);
+ if (r < 0)
+ return r;
+
+ c->mask |= SD_BUS_CREDS_EXE;
+ }
+ break;
+
+ case KDBUS_ITEM_CMDLINE:
+ if (mask & SD_BUS_CREDS_CMDLINE) {
+ c->cmdline_size = item->size - offsetof(struct kdbus_item, data);
+ c->cmdline = memdup(item->data, c->cmdline_size);
+ if (!c->cmdline)
+ return -ENOMEM;
+
+ c->mask |= SD_BUS_CREDS_CMDLINE;
+ }
+ break;
+
+ case KDBUS_ITEM_CGROUP:
+ m = (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) & mask;
+
+ if (m) {
+ r = free_and_strdup(&c->cgroup, item->str);
+ if (r < 0)
+ return r;
+
+ r = bus_get_root_path(bus);
+ if (r < 0)
+ return r;
+
+ r = free_and_strdup(&c->cgroup_root, bus->cgroup_root);
+ if (r < 0)
+ return r;
+
+ c->mask |= m;
+ }
+ break;
+
+ case KDBUS_ITEM_CAPS:
+ m = (SD_BUS_CREDS_EFFECTIVE_CAPS | SD_BUS_CREDS_PERMITTED_CAPS |
+ SD_BUS_CREDS_INHERITABLE_CAPS | SD_BUS_CREDS_BOUNDING_CAPS) & mask;
+
+ if (m) {
+ if (item->caps.last_cap != cap_last_cap() ||
+ item->size - offsetof(struct kdbus_item, caps.caps) < DIV_ROUND_UP(item->caps.last_cap, 32U) * 4 * 4)
+ return -EBADMSG;
+
+ c->capability = memdup(item->caps.caps, item->size - offsetof(struct kdbus_item, caps.caps));
+ if (!c->capability)
+ return -ENOMEM;
+
+ c->mask |= m;
+ }
+ break;
+
+ case KDBUS_ITEM_SECLABEL:
+ if (mask & SD_BUS_CREDS_SELINUX_CONTEXT) {
+ r = free_and_strdup(&c->label, item->str);
+ if (r < 0)
+ return r;
+
+ c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
+ }
+ break;
+
+ case KDBUS_ITEM_AUDIT:
+ if (mask & SD_BUS_CREDS_AUDIT_SESSION_ID) {
+ c->audit_session_id = (uint32_t) item->audit.sessionid;
+ c->mask |= SD_BUS_CREDS_AUDIT_SESSION_ID;
+ }
+
+ if (mask & SD_BUS_CREDS_AUDIT_LOGIN_UID) {
+ c->audit_login_uid = (uid_t) item->audit.loginuid;
+ c->mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID;
+ }
+ break;
+
+ case KDBUS_ITEM_OWNED_NAME:
+ if ((mask & SD_BUS_CREDS_WELL_KNOWN_NAMES) && service_name_is_valid(item->name.name)) {
+ r = strv_extend(&c->well_known_names, item->name.name);
+ if (r < 0)
+ return r;
+
+ c->mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES;
+ }
+ break;
+
+ case KDBUS_ITEM_CONN_DESCRIPTION:
+ if (mask & SD_BUS_CREDS_DESCRIPTION) {
+ r = free_and_strdup(&c->description, item->str);
+ if (r < 0)
+ return r;
+
+ c->mask |= SD_BUS_CREDS_DESCRIPTION;
+ }
+ break;
+
+ case KDBUS_ITEM_AUXGROUPS:
+ if (mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS) {
+ size_t i, n;
+ uid_t *g;
+
+ n = (item->size - offsetof(struct kdbus_item, data64)) / sizeof(uint64_t);
+ g = new(gid_t, n);
+ if (!g)
+ return -ENOMEM;
+
+ for (i = 0; i < n; i++)
+ g[i] = item->data64[i];
+
+ free(c->supplementary_gids);
+ c->supplementary_gids = g;
+ c->n_supplementary_gids = n;
+
+ c->mask |= SD_BUS_CREDS_SUPPLEMENTARY_GIDS;
+ }
+ break;
+ }
+ }
+
+ return 0;
+}
+
+uint64_t attach_flags_to_kdbus(uint64_t mask) {
+ uint64_t m = 0;
+
+ if (mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
+ SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID))
+ m |= KDBUS_ATTACH_CREDS;
+
+ if (mask & (SD_BUS_CREDS_PID|SD_BUS_CREDS_TID|SD_BUS_CREDS_PPID))
+ m |= KDBUS_ATTACH_PIDS;
+
+ if (mask & SD_BUS_CREDS_COMM)
+ m |= KDBUS_ATTACH_PID_COMM;
+
+ if (mask & SD_BUS_CREDS_TID_COMM)
+ m |= KDBUS_ATTACH_TID_COMM;
+
+ if (mask & SD_BUS_CREDS_EXE)
+ m |= KDBUS_ATTACH_EXE;
+
+ if (mask & SD_BUS_CREDS_CMDLINE)
+ m |= KDBUS_ATTACH_CMDLINE;
+
+ 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))
+ m |= KDBUS_ATTACH_CGROUP;
+
+ if (mask & (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS))
+ m |= KDBUS_ATTACH_CAPS;
+
+ if (mask & SD_BUS_CREDS_SELINUX_CONTEXT)
+ m |= KDBUS_ATTACH_SECLABEL;
+
+ if (mask & (SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID))
+ m |= KDBUS_ATTACH_AUDIT;
+
+ if (mask & SD_BUS_CREDS_WELL_KNOWN_NAMES)
+ m |= KDBUS_ATTACH_NAMES;
+
+ if (mask & SD_BUS_CREDS_DESCRIPTION)
+ m |= KDBUS_ATTACH_CONN_DESCRIPTION;
+
+ if (mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS)
+ m |= KDBUS_ATTACH_AUXGROUPS;
+
+ return m;
+}
+
+int bus_get_name_creds_kdbus(
+ sd_bus *bus,
+ const char *name,
+ uint64_t mask,
+ bool allow_activator,
+ sd_bus_creds **creds) {
+
+ _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *c = NULL;
+ struct kdbus_cmd_info *cmd;
+ struct kdbus_info *conn_info;
+ size_t size, l;
+ uint64_t id;
+ int r;
+
+ if (streq(name, "org.freedesktop.DBus"))
+ return -EOPNOTSUPP;
+
+ r = bus_kernel_parse_unique_name(name, &id);
+ if (r < 0)
+ return r;
+ if (r > 0) {
+ size = offsetof(struct kdbus_cmd_info, items);
+ cmd = alloca0_align(size, 8);
+ cmd->id = id;
+ } else {
+ l = strlen(name) + 1;
+ size = offsetof(struct kdbus_cmd_info, items) + KDBUS_ITEM_SIZE(l);
+ cmd = alloca0_align(size, 8);
+ cmd->items[0].size = KDBUS_ITEM_HEADER_SIZE + l;
+ cmd->items[0].type = KDBUS_ITEM_NAME;
+ memcpy(cmd->items[0].str, name, l);
+ }
+
+ /* If augmentation is on, and the bus didn't provide us
+ * the bits we want, then ask for the PID/TID so that we
+ * can read the rest from /proc. */
+ if ((mask & SD_BUS_CREDS_AUGMENT) &&
+ (mask & (SD_BUS_CREDS_PPID|
+ SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
+ SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID|
+ SD_BUS_CREDS_SUPPLEMENTARY_GIDS|
+ SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM|SD_BUS_CREDS_EXE|SD_BUS_CREDS_CMDLINE|
+ 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|
+ SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS|
+ SD_BUS_CREDS_SELINUX_CONTEXT|
+ SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID)))
+ mask |= SD_BUS_CREDS_PID;
+
+ cmd->size = size;
+ cmd->attach_flags = attach_flags_to_kdbus(mask);
+
+ r = ioctl(bus->input_fd, KDBUS_CMD_CONN_INFO, cmd);
+ if (r < 0)
+ return -errno;
+
+ conn_info = (struct kdbus_info *) ((uint8_t *) bus->kdbus_buffer + cmd->offset);
+
+ /* Non-activated names are considered not available */
+ if (!allow_activator && (conn_info->flags & KDBUS_HELLO_ACTIVATOR)) {
+ if (name[0] == ':')
+ r = -ENXIO;
+ else
+ r = -ESRCH;
+ goto fail;
+ }
+
+ c = bus_creds_new();
+ if (!c) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ if (mask & SD_BUS_CREDS_UNIQUE_NAME) {
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat"
+ if (asprintf(&c->unique_name, ":1.%llu", conn_info->id) < 0) {
+ r = -ENOMEM;
+ goto fail;
+ }
+#pragma GCC diagnostic pop
+
+ c->mask |= SD_BUS_CREDS_UNIQUE_NAME;
+ }
+
+ /* If KDBUS_ITEM_OWNED_NAME is requested then we'll get 0 of
+ them in case the service has no names. This does not mean
+ however that the list of owned names could not be
+ acquired. Hence, let's explicitly clarify that the data is
+ complete. */
+ c->mask |= mask & SD_BUS_CREDS_WELL_KNOWN_NAMES;
+
+ r = bus_populate_creds_from_items(bus, conn_info, mask, c);
+ if (r < 0)
+ goto fail;
+
+ r = bus_creds_add_more(c, mask, 0, 0);
+ if (r < 0)
+ goto fail;
+
+ if (creds) {
+ *creds = c;
+ c = NULL;
+ }
+
+ r = 0;
+
+fail:
+ bus_kernel_cmd_free(bus, cmd->offset);
+ return r;
+}
+
+int bus_get_owner_creds_kdbus(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
+ _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *c = NULL;
+ struct kdbus_cmd_info cmd = {
+ .size = sizeof(struct kdbus_cmd_info),
+ };
+ struct kdbus_info *creator_info;
+ pid_t pid = 0;
+ int r;
+
+ c = bus_creds_new();
+ if (!c)
+ return -ENOMEM;
+
+ /* If augmentation is on, and the bus doesn't didn't allow us
+ * to get the bits we want, then ask for the PID/TID so that we
+ * can read the rest from /proc. */
+ if ((mask & SD_BUS_CREDS_AUGMENT) &&
+ (mask & (SD_BUS_CREDS_PPID|
+ SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
+ SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID|
+ SD_BUS_CREDS_SUPPLEMENTARY_GIDS|
+ SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM|SD_BUS_CREDS_EXE|SD_BUS_CREDS_CMDLINE|
+ 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|
+ SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS|
+ SD_BUS_CREDS_SELINUX_CONTEXT|
+ SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID)))
+ mask |= SD_BUS_CREDS_PID;
+
+ cmd.attach_flags = attach_flags_to_kdbus(mask);
+
+ r = ioctl(bus->input_fd, KDBUS_CMD_BUS_CREATOR_INFO, &cmd);
+ if (r < 0)
+ return -errno;
+
+ creator_info = (struct kdbus_info *) ((uint8_t *) bus->kdbus_buffer + cmd.offset);
+
+ r = bus_populate_creds_from_items(bus, creator_info, mask, c);
+ bus_kernel_cmd_free(bus, cmd.offset);
+ if (r < 0)
+ return r;
+
+ r = bus_creds_add_more(c, mask, pid, 0);
+ if (r < 0)
+ return r;
+
+ *ret = c;
+ c = NULL;
+ return 0;
+}
+
+static int add_name_change_match(sd_bus *bus,
+ uint64_t cookie,
+ const char *name,
+ const char *old_owner,
+ const char *new_owner) {
+
+ uint64_t name_id = KDBUS_MATCH_ID_ANY, old_owner_id = 0, new_owner_id = 0;
+ int is_name_id = -1, r;
+ struct kdbus_item *item;
+
+ assert(bus);
+
+ /* If we encounter a match that could match against
+ * NameOwnerChanged messages, then we need to create
+ * KDBUS_ITEM_NAME_{ADD,REMOVE,CHANGE} and
+ * KDBUS_ITEM_ID_{ADD,REMOVE} matches for it, possibly
+ * multiple if the match is underspecified.
+ *
+ * The NameOwnerChanged signals take three parameters with
+ * unique or well-known names, but only some forms actually
+ * exist:
+ *
+ * WELLKNOWN, "", UNIQUE → KDBUS_ITEM_NAME_ADD
+ * WELLKNOWN, UNIQUE, "" → KDBUS_ITEM_NAME_REMOVE
+ * WELLKNOWN, UNIQUE, UNIQUE → KDBUS_ITEM_NAME_CHANGE
+ * UNIQUE, "", UNIQUE → KDBUS_ITEM_ID_ADD
+ * UNIQUE, UNIQUE, "" → KDBUS_ITEM_ID_REMOVE
+ *
+ * For the latter two the two unique names must be identical.
+ *
+ * */
+
+ if (name) {
+ is_name_id = bus_kernel_parse_unique_name(name, &name_id);
+ if (is_name_id < 0)
+ return 0;
+ }
+
+ if (!isempty(old_owner)) {
+ r = bus_kernel_parse_unique_name(old_owner, &old_owner_id);
+ if (r < 0)
+ return 0;
+ if (r == 0)
+ return 0;
+ if (is_name_id > 0 && old_owner_id != name_id)
+ return 0;
+ } else
+ old_owner_id = KDBUS_MATCH_ID_ANY;
+
+ if (!isempty(new_owner)) {
+ r = bus_kernel_parse_unique_name(new_owner, &new_owner_id);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return 0;
+ if (is_name_id > 0 && new_owner_id != name_id)
+ return 0;
+ } else
+ new_owner_id = KDBUS_MATCH_ID_ANY;
+
+ if (is_name_id <= 0) {
+ struct kdbus_cmd_match *m;
+ size_t sz, l;
+
+ /* If the name argument is missing or is a well-known
+ * name, then add KDBUS_ITEM_NAME_{ADD,REMOVE,CHANGE}
+ * matches for it */
+
+ l = name ? strlen(name) + 1 : 0;
+
+ sz = ALIGN8(offsetof(struct kdbus_cmd_match, items) +
+ offsetof(struct kdbus_item, name_change) +
+ offsetof(struct kdbus_notify_name_change, name) +
+ l);
+
+ m = alloca0_align(sz, 8);
+ m->size = sz;
+ m->cookie = cookie;
+
+ item = m->items;
+ item->size =
+ offsetof(struct kdbus_item, name_change) +
+ offsetof(struct kdbus_notify_name_change, name) +
+ l;
+
+ item->name_change.old_id.id = old_owner_id;
+ item->name_change.new_id.id = new_owner_id;
+
+ memcpy_safe(item->name_change.name, name, l);
+
+ /* If the old name is unset or empty, then
+ * this can match against added names */
+ if (isempty(old_owner)) {
+ item->type = KDBUS_ITEM_NAME_ADD;
+
+ r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
+ if (r < 0)
+ return -errno;
+ }
+
+ /* If the new name is unset or empty, then
+ * this can match against removed names */
+ if (isempty(new_owner)) {
+ item->type = KDBUS_ITEM_NAME_REMOVE;
+
+ r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
+ if (r < 0)
+ return -errno;
+ }
+
+ /* The CHANGE match we need in either case, because
+ * what is reported as a name change by the kernel
+ * might just be an owner change between starter and
+ * normal clients. For userspace such a change should
+ * be considered a removal/addition, hence let's
+ * subscribe to this unconditionally. */
+ item->type = KDBUS_ITEM_NAME_CHANGE;
+ r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
+ if (r < 0)
+ return -errno;
+ }
+
+ if (is_name_id != 0) {
+ struct kdbus_cmd_match *m;
+ uint64_t sz;
+
+ /* If the name argument is missing or is a unique
+ * name, then add KDBUS_ITEM_ID_{ADD,REMOVE} matches
+ * for it */
+
+ sz = ALIGN8(offsetof(struct kdbus_cmd_match, items) +
+ offsetof(struct kdbus_item, id_change) +
+ sizeof(struct kdbus_notify_id_change));
+
+ m = alloca0_align(sz, 8);
+ m->size = sz;
+ m->cookie = cookie;
+
+ item = m->items;
+ item->size =
+ offsetof(struct kdbus_item, id_change) +
+ sizeof(struct kdbus_notify_id_change);
+ item->id_change.id = name_id;
+
+ /* If the old name is unset or empty, then this can
+ * match against added ids */
+ if (isempty(old_owner)) {
+ item->type = KDBUS_ITEM_ID_ADD;
+ if (!isempty(new_owner))
+ item->id_change.id = new_owner_id;
+
+ r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
+ if (r < 0)
+ return -errno;
+ }
+
+ /* If thew new name is unset or empty, then this can
+ * match against removed ids */
+ if (isempty(new_owner)) {
+ item->type = KDBUS_ITEM_ID_REMOVE;
+ if (!isempty(old_owner))
+ item->id_change.id = old_owner_id;
+
+ r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
+ if (r < 0)
+ return -errno;
+ }
+ }
+
+ return 0;
+}
+
+int bus_add_match_internal_kernel(
+ sd_bus *bus,
+ struct bus_match_component *components,
+ unsigned n_components,
+ uint64_t cookie) {
+
+ struct kdbus_cmd_match *m;
+ struct kdbus_item *item;
+ uint64_t *bloom;
+ size_t sz;
+ const char *sender = NULL;
+ size_t sender_length = 0;
+ uint64_t src_id = KDBUS_MATCH_ID_ANY, dst_id = KDBUS_MATCH_ID_ANY;
+ bool using_bloom = false;
+ unsigned i;
+ bool matches_name_change = true;
+ const char *name_change_arg[3] = {};
+ int r;
+
+ assert(bus);
+
+ /* Monitor streams don't support matches, make this a NOP */
+ if (bus->is_monitor)
+ return 0;
+
+ bloom = alloca0(bus->bloom_size);
+
+ sz = ALIGN8(offsetof(struct kdbus_cmd_match, items));
+
+ for (i = 0; i < n_components; i++) {
+ struct bus_match_component *c = &components[i];
+
+ switch (c->type) {
+
+ case BUS_MATCH_SENDER:
+ if (!streq(c->value_str, "org.freedesktop.DBus"))
+ matches_name_change = false;
+
+ r = bus_kernel_parse_unique_name(c->value_str, &src_id);
+ if (r < 0)
+ return r;
+ else if (r > 0)
+ sz += ALIGN8(offsetof(struct kdbus_item, id) + sizeof(uint64_t));
+ else {
+ sender = c->value_str;
+ sender_length = strlen(sender);
+ sz += ALIGN8(offsetof(struct kdbus_item, str) + sender_length + 1);
+ }
+
+ break;
+
+ case BUS_MATCH_MESSAGE_TYPE:
+ if (c->value_u8 != SD_BUS_MESSAGE_SIGNAL)
+ matches_name_change = false;
+
+ bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, "message-type", bus_message_type_to_string(c->value_u8));
+ using_bloom = true;
+ break;
+
+ case BUS_MATCH_INTERFACE:
+ if (!streq(c->value_str, "org.freedesktop.DBus"))
+ matches_name_change = false;
+
+ bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, "interface", c->value_str);
+ using_bloom = true;
+ break;
+
+ case BUS_MATCH_MEMBER:
+ if (!streq(c->value_str, "NameOwnerChanged"))
+ matches_name_change = false;
+
+ bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, "member", c->value_str);
+ using_bloom = true;
+ break;
+
+ case BUS_MATCH_PATH:
+ if (!streq(c->value_str, "/org/freedesktop/DBus"))
+ matches_name_change = false;
+
+ bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, "path", c->value_str);
+ using_bloom = true;
+ break;
+
+ case BUS_MATCH_PATH_NAMESPACE:
+ bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, "path-slash-prefix", c->value_str);
+ using_bloom = true;
+ break;
+
+ case BUS_MATCH_ARG...BUS_MATCH_ARG_LAST: {
+ char buf[sizeof("arg")-1 + 2 + 1];
+
+ if (c->type - BUS_MATCH_ARG < 3)
+ name_change_arg[c->type - BUS_MATCH_ARG] = c->value_str;
+
+ xsprintf(buf, "arg%i", c->type - BUS_MATCH_ARG);
+ bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, buf, c->value_str);
+ using_bloom = true;
+ break;
+ }
+
+ case BUS_MATCH_ARG_HAS...BUS_MATCH_ARG_HAS_LAST: {
+ char buf[sizeof("arg")-1 + 2 + sizeof("-has")];
+
+ xsprintf(buf, "arg%i-has", c->type - BUS_MATCH_ARG_HAS);
+ bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, buf, c->value_str);
+ using_bloom = true;
+ break;
+ }
+
+ case BUS_MATCH_ARG_PATH...BUS_MATCH_ARG_PATH_LAST:
+ /*
+ * XXX: DBus spec defines arg[0..63]path= matching to be
+ * a two-way glob. That is, if either string is a prefix
+ * of the other, it matches.
+ * This is really hard to realize in bloom-filters, as
+ * we would have to create a bloom-match for each prefix
+ * of @c->value_str. This is excessive, hence we just
+ * ignore all those matches and accept everything from
+ * the kernel. People should really avoid those matches.
+ * If they're used in real-life some day, we will have
+ * to properly support multiple-matches here.
+ */
+ break;
+
+ case BUS_MATCH_ARG_NAMESPACE...BUS_MATCH_ARG_NAMESPACE_LAST: {
+ char buf[sizeof("arg")-1 + 2 + sizeof("-dot-prefix")];
+
+ xsprintf(buf, "arg%i-dot-prefix", c->type - BUS_MATCH_ARG_NAMESPACE);
+ bloom_add_pair(bloom, bus->bloom_size, bus->bloom_n_hash, buf, c->value_str);
+ using_bloom = true;
+ break;
+ }
+
+ case BUS_MATCH_DESTINATION:
+ /*
+ * Kernel only supports matching on destination IDs, but
+ * not on destination names. So just skip the
+ * destination name restriction and verify it in
+ * user-space on retrieval.
+ */
+ r = bus_kernel_parse_unique_name(c->value_str, &dst_id);
+ if (r < 0)
+ return r;
+ else if (r > 0)
+ sz += ALIGN8(offsetof(struct kdbus_item, id) + sizeof(uint64_t));
+
+ /* if not a broadcast, it cannot be a name-change */
+ if (r <= 0 || dst_id != KDBUS_DST_ID_BROADCAST)
+ matches_name_change = false;
+
+ break;
+
+ case BUS_MATCH_ROOT:
+ case BUS_MATCH_VALUE:
+ case BUS_MATCH_LEAF:
+ case _BUS_MATCH_NODE_TYPE_MAX:
+ case _BUS_MATCH_NODE_TYPE_INVALID:
+ assert_not_reached("Invalid match type?");
+ }
+ }
+
+ if (using_bloom)
+ sz += ALIGN8(offsetof(struct kdbus_item, data64) + bus->bloom_size);
+
+ m = alloca0_align(sz, 8);
+ m->size = sz;
+ m->cookie = cookie;
+
+ item = m->items;
+
+ if (src_id != KDBUS_MATCH_ID_ANY) {
+ item->size = offsetof(struct kdbus_item, id) + sizeof(uint64_t);
+ item->type = KDBUS_ITEM_ID;
+ item->id = src_id;
+ item = KDBUS_ITEM_NEXT(item);
+ }
+
+ if (dst_id != KDBUS_MATCH_ID_ANY) {
+ item->size = offsetof(struct kdbus_item, id) + sizeof(uint64_t);
+ item->type = KDBUS_ITEM_DST_ID;
+ item->id = dst_id;
+ item = KDBUS_ITEM_NEXT(item);
+ }
+
+ if (using_bloom) {
+ item->size = offsetof(struct kdbus_item, data64) + bus->bloom_size;
+ item->type = KDBUS_ITEM_BLOOM_MASK;
+ memcpy(item->data64, bloom, bus->bloom_size);
+ item = KDBUS_ITEM_NEXT(item);
+ }
+
+ if (sender) {
+ item->size = offsetof(struct kdbus_item, str) + sender_length + 1;
+ item->type = KDBUS_ITEM_NAME;
+ memcpy(item->str, sender, sender_length + 1);
+ }
+
+ r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_ADD, m);
+ if (r < 0)
+ return -errno;
+
+ if (matches_name_change) {
+
+ /* If this match could theoretically match
+ * NameOwnerChanged messages, we need to
+ * install a second non-bloom filter explitly
+ * for it */
+
+ r = add_name_change_match(bus, cookie, name_change_arg[0], name_change_arg[1], name_change_arg[2]);
+ if (r < 0)
+ return r;
+ }
+
+ return 0;
+}
+
+int bus_add_match_internal_kernel_async(
+ sd_bus *bus,
+ sd_bus_slot **ret_slot,
+ const char *match,
+ struct bus_match_component *components,
+ unsigned n_components,
+ uint64_t cookie,
+ sd_bus_message_handler_t callback,
+ void *userdata)
+{
+ int ret;
+ _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
+ uint64_t reply_cookie;
+
+ ret = prepare_for_kernel_request(bus);
+ if (ret < 0)
+ return ret;
+
+ ret = bus_add_match_internal_kernel(bus, components, n_components, cookie);
+
+ reply_cookie = ++bus->cookie;
+ if (ret < 0) {
+ _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
+
+ sd_bus_error_setf(&error, SD_BUS_ERROR_FAILED, "Add match failed");
+ ret = bus_message_new_synthetic_error(bus, reply_cookie, &error, &m);
+ } else
+ ret = bus_message_new_synthetic_reply(bus, reply_cookie, &m);
+
+ if (ret < 0)
+ return ret;
+
+ ret = bus_seal_synthetic_message(bus, m);
+ if (ret < 0)
+ return ret;
+
+ ret = enqueue_kernel_reply(bus, ret_slot, m, callback, userdata);
+ m = NULL;
+
+ return ret;
+}
+
+int bus_remove_match_internal_kernel(
+ sd_bus *bus,
+ uint64_t cookie) {
+
+ struct kdbus_cmd_match m = {
+ .size = offsetof(struct kdbus_cmd_match, items),
+ .cookie = cookie,
+ };
+ int r;
+
+ assert(bus);
+
+ /* Monitor streams don't support matches, make this a NOP */
+ if (bus->is_monitor)
+ return 0;
+
+ r = ioctl(bus->input_fd, KDBUS_CMD_MATCH_REMOVE, &m);
+ if (r < 0)
+ return -errno;
+
+ return 0;
+}
#include "bus-kernel.h"
#include "bus-match.h"
#include "def.h"
++#include "format-util.h"
#include "hashmap.h"
#include "list.h"
#include "prioq.h"
- #include "refcnt.h"
#include "socket-util.h"
- #include "util.h"
+ #include "time-util.h"
+
+ /* Note that we use the new /run prefix here (instead of /var/run) since we require them to be aliases and
+ * that way we become independent of /var being mounted */
-#define DEFAULT_SYSTEM_BUS_ADDRESS "unix:path=/run/dbus/system_bus_socket"
-#define DEFAULT_USER_BUS_ADDRESS_FMT "unix:path=%s/bus"
++#define UNIX_SYSTEM_BUS_ADDRESS "unix:path=/run/dbus/system_bus_socket"
++#define KERNEL_SYSTEM_BUS_ADDRESS "kernel:path=/sys/fs/kdbus/0-system/bus"
++#define DEFAULT_SYSTEM_BUS_ADDRESS KERNEL_SYSTEM_BUS_ADDRESS ";" UNIX_SYSTEM_BUS_ADDRESS
++#define UNIX_USER_BUS_ADDRESS_FMT "unix:path=%s/bus"
++#define KERNEL_USER_BUS_ADDRESS_FMT "kernel:path=/sys/fs/kdbus/"UID_FMT"-user/bus"
++#define DEFAULT_USER_BUS_ADDRESS_FMT KERNEL_USER_BUS_ADDRESS_FMT ";" UNIX_USER_BUS_ADDRESS_FMT
struct reply_callback {
sd_bus_message_handler_t callback;
union sockaddr_union sockaddr;
socklen_t sockaddr_size;
- char *kernel;
- char *machine;
pid_t nspid;
+ char *machine;
+
++ char *kernel;
+
sd_id128_t server_id;
char *address;
char *exec_path;
char **exec_argv;
- unsigned iteration_counter;
-
++ /* kdbus forward-ported stuff */
++ uint64_t hello_flags;
++ uint64_t attach_flags;
++ uint64_t match_cookie;
+ void *kdbus_buffer;
+
/* We do locking around the memfd cache, since we want to
* allow people to process a sd_bus_message in a different
* thread then it was generated on and free it there. Since
void *current_userdata;
sd_bus **default_bus_ptr;
- pid_t tid;
+ struct kdbus_creds fake_creds;
+ struct kdbus_pids fake_pids;
+ char *fake_label;
+
+ char *cgroup_root;
+
char *description;
char *patch_sender;
#include "string-util.h"
#include "strv.h"
#include "user-util.h"
- #include "util.h"
+ #include "memory-util.h"
-void close_and_munmap(int fd, void *address, size_t size) {
+#pragma GCC diagnostic ignored "-Wformat"
+
+#define UNIQUE_NAME_MAX (3+DECIMAL_STR_MAX(uint64_t))
+
+int bus_kernel_parse_unique_name(const char *s, uint64_t *id) {
+ int r;
+
+ assert(s);
+ assert(id);
+
+ if (!startswith(s, ":1."))
+ return 0;
+
+ r = safe_atou64(s + 3, id);
+ if (r < 0)
+ return r;
+
+ return 1;
+}
+
+static void append_payload_vec(struct kdbus_item **d, const void *p, size_t sz) {
+ assert(d);
+ assert(sz > 0);
+
+ *d = ALIGN8_PTR(*d);
+
+ /* Note that p can be NULL, which encodes a region full of
+ * zeroes, which is useful to optimize certain padding
+ * conditions */
+
+ (*d)->size = offsetof(struct kdbus_item, vec) + sizeof(struct kdbus_vec);
+ (*d)->type = KDBUS_ITEM_PAYLOAD_VEC;
+ (*d)->vec.address = PTR_TO_UINT64(p);
+ (*d)->vec.size = sz;
+
+ *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
+}
+
+static void append_payload_memfd(struct kdbus_item **d, int memfd, size_t start, size_t sz) {
+ assert(d);
+ assert(memfd >= 0);
+ assert(sz > 0);
+
+ *d = ALIGN8_PTR(*d);
+ (*d)->size = offsetof(struct kdbus_item, memfd) + sizeof(struct kdbus_memfd);
+ (*d)->type = KDBUS_ITEM_PAYLOAD_MEMFD;
+ (*d)->memfd.fd = memfd;
+ (*d)->memfd.start = start;
+ (*d)->memfd.size = sz;
+
+ *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
+}
+
+static void append_destination(struct kdbus_item **d, const char *s, size_t length) {
+ assert(d);
+ assert(s);
+
+ *d = ALIGN8_PTR(*d);
+
+ (*d)->size = offsetof(struct kdbus_item, str) + length + 1;
+ (*d)->type = KDBUS_ITEM_DST_NAME;
+ memcpy((*d)->str, s, length + 1);
+
+ *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
+}
+
+static struct kdbus_bloom_filter *append_bloom(struct kdbus_item **d, size_t length) {
+ struct kdbus_item *i;
+
+ assert(d);
+
+ i = ALIGN8_PTR(*d);
+
+ i->size = offsetof(struct kdbus_item, bloom_filter) +
+ offsetof(struct kdbus_bloom_filter, data) +
+ length;
+ i->type = KDBUS_ITEM_BLOOM_FILTER;
+
+ *d = (struct kdbus_item *) ((uint8_t*) i + i->size);
+
+ return &i->bloom_filter;
+}
+
+static void append_fds(struct kdbus_item **d, const int fds[], unsigned n_fds) {
+ assert(d);
+ assert(fds);
+ assert(n_fds > 0);
+
+ *d = ALIGN8_PTR(*d);
+ (*d)->size = offsetof(struct kdbus_item, fds) + sizeof(int) * n_fds;
+ (*d)->type = KDBUS_ITEM_FDS;
+ memcpy((*d)->fds, fds, sizeof(int) * n_fds);
+
+ *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
+}
+
+static void add_bloom_arg(void *data, size_t size, unsigned n_hash, unsigned i, const char *t) {
+ char buf[sizeof("arg")-1 + 2 + sizeof("-slash-prefix")];
+ char *e;
+
+ assert(data);
+ assert(size > 0);
+ assert(i < 64);
+ assert(t);
+
+ e = stpcpy(buf, "arg");
+ if (i < 10)
+ *(e++) = '0' + (char) i;
+ else {
+ *(e++) = '0' + (char) (i / 10);
+ *(e++) = '0' + (char) (i % 10);
+ }
+
+ *e = 0;
+ bloom_add_pair(data, size, n_hash, buf, t);
+
+ strcpy(e, "-dot-prefix");
+ bloom_add_prefixes(data, size, n_hash, buf, t, '.');
+ strcpy(e, "-slash-prefix");
+ bloom_add_prefixes(data, size, n_hash, buf, t, '/');
+}
+
+static void add_bloom_arg_has(void *data, size_t size, unsigned n_hash, unsigned i, const char *t) {
+ char buf[sizeof("arg")-1 + 2 + sizeof("-has")];
+ char *e;
+
+ assert(data);
+ assert(size > 0);
+ assert(i < 64);
+ assert(t);
+
+ e = stpcpy(buf, "arg");
+ if (i < 10)
+ *(e++) = '0' + (char) i;
+ else {
+ *(e++) = '0' + (char) (i / 10);
+ *(e++) = '0' + (char) (i % 10);
+ }
+
+ strcpy(e, "-has");
+ bloom_add_pair(data, size, n_hash, buf, t);
+}
+
+static int bus_message_setup_bloom(sd_bus_message *m, struct kdbus_bloom_filter *bloom) {
+ void *data;
+ unsigned i;
+ int r;
+
+ assert(m);
+ assert(bloom);
+
+ data = bloom->data;
+ memzero(data, m->bus->bloom_size);
+ bloom->generation = 0;
+
+ bloom_add_pair(data, m->bus->bloom_size, m->bus->bloom_n_hash, "message-type", bus_message_type_to_string(m->header->type));
+
+ if (m->interface)
+ bloom_add_pair(data, m->bus->bloom_size, m->bus->bloom_n_hash, "interface", m->interface);
+ if (m->member)
+ bloom_add_pair(data, m->bus->bloom_size, m->bus->bloom_n_hash, "member", m->member);
+ if (m->path) {
+ bloom_add_pair(data, m->bus->bloom_size, m->bus->bloom_n_hash, "path", m->path);
+ bloom_add_pair(data, m->bus->bloom_size, m->bus->bloom_n_hash, "path-slash-prefix", m->path);
+ bloom_add_prefixes(data, m->bus->bloom_size, m->bus->bloom_n_hash, "path-slash-prefix", m->path, '/');
+ }
+
+ r = sd_bus_message_rewind(m, true);
+ if (r < 0)
+ return r;
+
+ for (i = 0; i < 64; i++) {
+ const char *t, *contents;
+ char type;
+
+ r = sd_bus_message_peek_type(m, &type, &contents);
+ if (r < 0)
+ return r;
+
+ if (IN_SET(type, SD_BUS_TYPE_STRING, SD_BUS_TYPE_OBJECT_PATH, SD_BUS_TYPE_SIGNATURE)) {
+
+ /* The bloom filter includes simple strings of any kind */
+ r = sd_bus_message_read_basic(m, type, &t);
+ if (r < 0)
+ return r;
+
+ add_bloom_arg(data, m->bus->bloom_size, m->bus->bloom_n_hash, i, t);
+ }
+
+ if (type == SD_BUS_TYPE_ARRAY && STR_IN_SET(contents, "s", "o", "g")) {
+
+ /* As well as array of simple strings of any kinds */
+ r = sd_bus_message_enter_container(m, type, contents);
+ if (r < 0)
+ return r;
+
+ while ((r = sd_bus_message_read_basic(m, contents[0], &t)) > 0)
+ add_bloom_arg_has(data, m->bus->bloom_size, m->bus->bloom_n_hash, i, t);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_exit_container(m);
+ if (r < 0)
+ return r;
+
+ } else
+ /* Stop adding to bloom filter as soon as we
+ * run into the first argument we cannot add
+ * to it. */
+ break;
+ }
+
+ return 0;
+}
+
+static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
+ struct bus_body_part *part;
+ struct kdbus_item *d;
+ const char *destination;
+ bool well_known = false;
+ uint64_t dst_id;
+ size_t sz, dl;
+ unsigned i;
+ int r;
+
+ assert(b);
+ assert(m);
+ assert(m->sealed);
+
+ /* We put this together only once, if this message is reused
+ * we reuse the earlier-built version */
+ if (m->kdbus)
+ return 0;
+
+ destination = m->destination ?: m->destination_ptr;
+
+ if (destination) {
+ r = bus_kernel_parse_unique_name(destination, &dst_id);
+ if (r < 0)
+ return r;
+ if (r == 0) {
+ well_known = true;
+
+ /* verify_destination_id will usually be 0, which makes the kernel
+ * driver only look at the provided well-known name. Otherwise,
+ * the kernel will make sure the provided destination id matches
+ * the owner of the provided well-known-name, and fail if they
+ * differ. Currently, this is only needed for bus-proxyd. */
+ dst_id = m->verify_destination_id;
+ }
+ } else
+ dst_id = KDBUS_DST_ID_BROADCAST;
+
+ sz = offsetof(struct kdbus_msg, items);
+
+ /* Add in fixed header, fields header and payload */
+ sz += (1 + m->n_body_parts) * ALIGN8(offsetof(struct kdbus_item, vec) +
+ MAX(sizeof(struct kdbus_vec),
+ sizeof(struct kdbus_memfd)));
+
+ /* Add space for bloom filter */
+ sz += ALIGN8(offsetof(struct kdbus_item, bloom_filter) +
+ offsetof(struct kdbus_bloom_filter, data) +
+ m->bus->bloom_size);
+
+ /* Add in well-known destination header */
+ if (well_known) {
+ dl = strlen(destination);
+ sz += ALIGN8(offsetof(struct kdbus_item, str) + dl + 1);
+ }
+
+ /* Add space for unix fds */
+ if (m->n_fds > 0)
+ sz += ALIGN8(offsetof(struct kdbus_item, fds) + sizeof(int)*m->n_fds);
+
+ m->kdbus = memalign(8, sz);
+ if (!m->kdbus) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ m->free_kdbus = true;
+ memzero(m->kdbus, sz);
+
+ m->kdbus->flags =
+ ((m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED) ? 0 : KDBUS_MSG_EXPECT_REPLY) |
+ ((m->header->flags & BUS_MESSAGE_NO_AUTO_START) ? KDBUS_MSG_NO_AUTO_START : 0) |
+ ((m->header->type == SD_BUS_MESSAGE_SIGNAL) ? KDBUS_MSG_SIGNAL : 0);
+
+ m->kdbus->dst_id = dst_id;
+ m->kdbus->payload_type = KDBUS_PAYLOAD_DBUS;
+ m->kdbus->cookie = m->header->dbus2.cookie;
+ m->kdbus->priority = m->priority;
+
+ if (m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
+ m->kdbus->cookie_reply = m->reply_cookie;
+ else {
+ struct timespec now;
+
+ assert_se(clock_gettime(CLOCK_MONOTONIC_COARSE, &now) == 0);
+ m->kdbus->timeout_ns = now.tv_sec * NSEC_PER_SEC + now.tv_nsec +
+ m->timeout * NSEC_PER_USEC;
+ }
+
+ d = m->kdbus->items;
+
+ if (well_known)
+ append_destination(&d, destination, dl);
+
+ append_payload_vec(&d, m->header, BUS_MESSAGE_BODY_BEGIN(m));
+
+ MESSAGE_FOREACH_PART(part, i, m) {
+ if (part->is_zero) {
+ /* If this is padding then simply send a
+ * vector with a NULL data pointer which the
+ * kernel will just pass through. This is the
+ * most efficient way to encode zeroes */
+
+ append_payload_vec(&d, NULL, part->size);
+ continue;
+ }
+
+ if (part->memfd >= 0 && part->sealed && destination) {
+ /* Try to send a memfd, if the part is
+ * sealed and this is not a broadcast. Since we can only */
+
+ append_payload_memfd(&d, part->memfd, part->memfd_offset, part->size);
+ continue;
+ }
+
+ /* Otherwise, let's send a vector to the actual data.
+ * For that, we need to map it first. */
+ r = bus_body_part_map(part);
+ if (r < 0)
+ goto fail;
+
+ append_payload_vec(&d, part->data, part->size);
+ }
+
+ if (m->header->type == SD_BUS_MESSAGE_SIGNAL) {
+ struct kdbus_bloom_filter *bloom;
+
+ bloom = append_bloom(&d, m->bus->bloom_size);
+ r = bus_message_setup_bloom(m, bloom);
+ if (r < 0)
+ goto fail;
+ }
+
+ if (m->n_fds > 0)
+ append_fds(&d, m->fds, m->n_fds);
+
+ m->kdbus->size = (uint8_t*) d - (uint8_t*) m->kdbus;
+ assert(m->kdbus->size <= sz);
+
+ return 0;
+
+fail:
+ m->poisoned = true;
+ return r;
+}
+
+static void unset_memfds(struct sd_bus_message *m) {
+ struct bus_body_part *part;
+ unsigned i;
+
+ assert(m);
+
+ /* Make sure the memfds are not freed twice */
+ MESSAGE_FOREACH_PART(part, i, m)
+ if (part->memfd >= 0)
+ part->memfd = -1;
+}
+
+static void message_set_timestamp(sd_bus *bus, sd_bus_message *m, const struct kdbus_timestamp *ts) {
+ assert(bus);
+ assert(m);
+
+ if (!ts)
+ return;
+
+ if (!bus->attach_timestamp)
+ return;
+
+ m->realtime = ts->realtime_ns / NSEC_PER_USEC;
+ m->monotonic = ts->monotonic_ns / NSEC_PER_USEC;
+ m->seqnum = ts->seqnum;
+}
+
+static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
+ sd_bus_message *m = NULL;
+ struct kdbus_item *d;
+ unsigned n_fds = 0;
+ _cleanup_free_ int *fds = NULL;
+ struct bus_header *header = NULL;
+ void *footer = NULL;
+ size_t header_size = 0, footer_size = 0;
+ size_t n_bytes = 0, idx = 0;
+ const char *destination = NULL, *seclabel = NULL;
+ bool last_was_memfd = false;
+ int r;
+
+ assert(bus);
+ assert(k);
+ assert(k->payload_type == KDBUS_PAYLOAD_DBUS);
+
+ KDBUS_ITEM_FOREACH(d, k, items) {
+ size_t l;
+
+ l = d->size - offsetof(struct kdbus_item, data);
+
+ switch (d->type) {
+
+ case KDBUS_ITEM_PAYLOAD_OFF:
+ if (!header) {
+ header = (struct bus_header*)((uint8_t*) k + d->vec.offset);
+ header_size = d->vec.size;
+ }
+
+ footer = (uint8_t*) k + d->vec.offset;
+ footer_size = d->vec.size;
+
+ n_bytes += d->vec.size;
+ last_was_memfd = false;
+ break;
+
+ case KDBUS_ITEM_PAYLOAD_MEMFD:
+ if (!header) /* memfd cannot be first part */
+ return -EBADMSG;
+
+ n_bytes += d->memfd.size;
+ last_was_memfd = true;
+ break;
+
+ case KDBUS_ITEM_FDS: {
+ int *f;
+ unsigned j;
+
+ j = l / sizeof(int);
+ f = realloc(fds, sizeof(int) * (n_fds + j));
+ if (!f)
+ return -ENOMEM;
+
+ fds = f;
+ memcpy(fds + n_fds, d->fds, sizeof(int) * j);
+ n_fds += j;
+ break;
+ }
+
+ case KDBUS_ITEM_SECLABEL:
+ seclabel = d->str;
+ break;
+ }
+ }
+
+ if (last_was_memfd) /* memfd cannot be last part */
+ return -EBADMSG;
+
+ if (!header)
+ return -EBADMSG;
+
+ if (header_size < sizeof(struct bus_header))
+ return -EBADMSG;
+
+ /* on kdbus we only speak native endian gvariant, never dbus1
+ * marshalling or reverse endian */
+ if (header->version != 2 ||
+ header->endian != BUS_NATIVE_ENDIAN)
+ return -EPROTOTYPE;
+
+ r = bus_message_from_header(
+ bus,
+ header, header_size,
+ footer, footer_size,
+ n_bytes,
+ fds, n_fds,
+ seclabel, 0, &m);
+ if (r < 0)
+ return r;
+
+ /* The well-known names list is different from the other
+ credentials. If we asked for it, but nothing is there, this
+ means that the list of well-known names is simply empty, not
+ that we lack any data */
+
+ m->creds.mask |= (SD_BUS_CREDS_UNIQUE_NAME|SD_BUS_CREDS_WELL_KNOWN_NAMES) & bus->creds_mask;
+
+ KDBUS_ITEM_FOREACH(d, k, items) {
+ size_t l;
+
+ l = d->size - offsetof(struct kdbus_item, data);
+
+ switch (d->type) {
+
+ case KDBUS_ITEM_PAYLOAD_OFF: {
+ size_t begin_body;
+
+ begin_body = BUS_MESSAGE_BODY_BEGIN(m);
+
+ if (idx + d->vec.size > begin_body) {
+ struct bus_body_part *part;
+
+ /* Contains body material */
+
+ part = message_append_part(m);
+ if (!part) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ /* A -1 offset is NUL padding. */
+ part->is_zero = d->vec.offset == ~0ULL;
+
+ if (idx >= begin_body) {
+ if (!part->is_zero)
+ part->data = (uint8_t* )k + d->vec.offset;
+ part->size = d->vec.size;
+ } else {
+ if (!part->is_zero)
+ part->data = (uint8_t*) k + d->vec.offset + (begin_body - idx);
+ part->size = d->vec.size - (begin_body - idx);
+ }
+
+ part->sealed = true;
+ }
+
+ idx += d->vec.size;
+ break;
+ }
+
+ case KDBUS_ITEM_PAYLOAD_MEMFD: {
+ struct bus_body_part *part;
+
+ if (idx < BUS_MESSAGE_BODY_BEGIN(m)) {
+ r = -EBADMSG;
+ goto fail;
+ }
+
+ part = message_append_part(m);
+ if (!part) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ part->memfd = d->memfd.fd;
+ part->memfd_offset = d->memfd.start;
+ part->size = d->memfd.size;
+ part->sealed = true;
+
+ idx += d->memfd.size;
+ break;
+ }
+
+ case KDBUS_ITEM_PIDS:
+
+ /* The PID/TID might be missing, when the data
+ * is faked by a bus proxy and it lacks that
+ * information about the real client (since
+ * SO_PEERCRED is used for that). Also kernel
+ * namespacing might make some of this data
+ * unavailable when untranslatable. */
+
+ if (d->pids.pid > 0) {
+ m->creds.pid = (pid_t) d->pids.pid;
+ m->creds.mask |= SD_BUS_CREDS_PID & bus->creds_mask;
+ }
+
+ if (d->pids.tid > 0) {
+ m->creds.tid = (pid_t) d->pids.tid;
+ m->creds.mask |= SD_BUS_CREDS_TID & bus->creds_mask;
+ }
+
+ if (d->pids.ppid > 0) {
+ m->creds.ppid = (pid_t) d->pids.ppid;
+ m->creds.mask |= SD_BUS_CREDS_PPID & bus->creds_mask;
+ } else if (d->pids.pid == 1) {
+ m->creds.ppid = 0;
+ m->creds.mask |= SD_BUS_CREDS_PPID & bus->creds_mask;
+ }
+
+ break;
+
+ case KDBUS_ITEM_CREDS:
+
+ /* EUID/SUID/FSUID/EGID/SGID/FSGID might be
+ * missing too (see above). */
+
+ if ((uid_t) d->creds.uid != UID_INVALID) {
+ m->creds.uid = (uid_t) d->creds.uid;
+ m->creds.mask |= SD_BUS_CREDS_UID & bus->creds_mask;
+ }
+
+ if ((uid_t) d->creds.euid != UID_INVALID) {
+ m->creds.euid = (uid_t) d->creds.euid;
+ m->creds.mask |= SD_BUS_CREDS_EUID & bus->creds_mask;
+ }
+
+ if ((uid_t) d->creds.suid != UID_INVALID) {
+ m->creds.suid = (uid_t) d->creds.suid;
+ m->creds.mask |= SD_BUS_CREDS_SUID & bus->creds_mask;
+ }
+
+ if ((uid_t) d->creds.fsuid != UID_INVALID) {
+ m->creds.fsuid = (uid_t) d->creds.fsuid;
+ m->creds.mask |= SD_BUS_CREDS_FSUID & bus->creds_mask;
+ }
+
+ if ((gid_t) d->creds.gid != GID_INVALID) {
+ m->creds.gid = (gid_t) d->creds.gid;
+ m->creds.mask |= SD_BUS_CREDS_GID & bus->creds_mask;
+ }
+
+ if ((gid_t) d->creds.egid != GID_INVALID) {
+ m->creds.egid = (gid_t) d->creds.egid;
+ m->creds.mask |= SD_BUS_CREDS_EGID & bus->creds_mask;
+ }
+
+ if ((gid_t) d->creds.sgid != GID_INVALID) {
+ m->creds.sgid = (gid_t) d->creds.sgid;
+ m->creds.mask |= SD_BUS_CREDS_SGID & bus->creds_mask;
+ }
+
+ if ((gid_t) d->creds.fsgid != GID_INVALID) {
+ m->creds.fsgid = (gid_t) d->creds.fsgid;
+ m->creds.mask |= SD_BUS_CREDS_FSGID & bus->creds_mask;
+ }
+
+ break;
+
+ case KDBUS_ITEM_TIMESTAMP:
+ message_set_timestamp(bus, m, &d->timestamp);
+ break;
+
+ case KDBUS_ITEM_PID_COMM:
+ m->creds.comm = d->str;
+ m->creds.mask |= SD_BUS_CREDS_COMM & bus->creds_mask;
+ break;
+
+ case KDBUS_ITEM_TID_COMM:
+ m->creds.tid_comm = d->str;
+ m->creds.mask |= SD_BUS_CREDS_TID_COMM & bus->creds_mask;
+ break;
+
+ case KDBUS_ITEM_EXE:
+ m->creds.exe = d->str;
+ m->creds.mask |= SD_BUS_CREDS_EXE & bus->creds_mask;
+ break;
+
+ case KDBUS_ITEM_CMDLINE:
+ m->creds.cmdline = d->str;
+ m->creds.cmdline_size = l;
+ m->creds.mask |= SD_BUS_CREDS_CMDLINE & bus->creds_mask;
+ break;
+
+ case KDBUS_ITEM_CGROUP:
+ m->creds.cgroup = d->str;
+ m->creds.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) & bus->creds_mask;
+
+ r = bus_get_root_path(bus);
+ if (r < 0)
+ goto fail;
+
+ m->creds.cgroup_root = bus->cgroup_root;
+ break;
+
+ case KDBUS_ITEM_AUDIT:
+ m->creds.audit_session_id = (uint32_t) d->audit.sessionid;
+ m->creds.mask |= SD_BUS_CREDS_AUDIT_SESSION_ID & bus->creds_mask;
+
+ m->creds.audit_login_uid = (uid_t) d->audit.loginuid;
+ m->creds.mask |= SD_BUS_CREDS_AUDIT_LOGIN_UID & bus->creds_mask;
+ break;
+
+ case KDBUS_ITEM_CAPS:
+ if (d->caps.last_cap != cap_last_cap() ||
+ d->size - offsetof(struct kdbus_item, caps.caps) < DIV_ROUND_UP(d->caps.last_cap, 32U) * 4 * 4) {
+ r = -EBADMSG;
+ goto fail;
+ }
+
+ m->creds.capability = d->caps.caps;
+ m->creds.mask |= (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS) & bus->creds_mask;
+ break;
+
+ case KDBUS_ITEM_DST_NAME:
+ if (!service_name_is_valid(d->str)) {
+ r = -EBADMSG;
+ goto fail;
+ }
+
+ destination = d->str;
+ break;
+
+ case KDBUS_ITEM_OWNED_NAME:
+ if (!service_name_is_valid(d->name.name)) {
+ r = -EBADMSG;
+ goto fail;
+ }
+
+ if (bus->creds_mask & SD_BUS_CREDS_WELL_KNOWN_NAMES) {
+ char **wkn;
+ size_t n;
+
+ /* We just extend the array here, but
+ * do not allocate the strings inside
+ * of it, instead we just point to our
+ * buffer directly. */
+ n = strv_length(m->creds.well_known_names);
+ wkn = realloc(m->creds.well_known_names, (n + 2) * sizeof(char*));
+ if (!wkn) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ wkn[n] = d->name.name;
+ wkn[n+1] = NULL;
+ m->creds.well_known_names = wkn;
+
+ m->creds.mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES;
+ }
+ break;
+
+ case KDBUS_ITEM_CONN_DESCRIPTION:
+ m->creds.description = d->str;
+ m->creds.mask |= SD_BUS_CREDS_DESCRIPTION & bus->creds_mask;
+ break;
+
+ case KDBUS_ITEM_AUXGROUPS:
+
+ if (bus->creds_mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS) {
+ size_t i, n;
+ gid_t *g;
+
+ n = (d->size - offsetof(struct kdbus_item, data64)) / sizeof(uint64_t);
+ g = new(gid_t, n);
+ if (!g) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ for (i = 0; i < n; i++)
+ g[i] = d->data64[i];
+
+ m->creds.supplementary_gids = g;
+ m->creds.n_supplementary_gids = n;
+ m->creds.mask |= SD_BUS_CREDS_SUPPLEMENTARY_GIDS;
+ }
+
+ break;
+
+ case KDBUS_ITEM_FDS:
+ case KDBUS_ITEM_SECLABEL:
+ case KDBUS_ITEM_BLOOM_FILTER:
+ break;
+
+ default:
+ log_debug("Got unknown field from kernel %llu", d->type);
+ }
+ }
+
+ /* If we requested the list of well-known names to be appended
+ * and the sender had none no item for it will be
+ * attached. However, this does *not* mean that the kernel
+ * didn't want to provide this information to us. Hence, let's
+ * explicitly mark this information as available if it was
+ * requested. */
+ m->creds.mask |= bus->creds_mask & SD_BUS_CREDS_WELL_KNOWN_NAMES;
+
+ r = bus_message_parse_fields(m);
+ if (r < 0)
+ goto fail;
+
+ /* Refuse messages if kdbus and dbus1 cookie doesn't match up */
+ if ((uint64_t) m->header->dbus2.cookie != k->cookie) {
+ r = -EBADMSG;
+ goto fail;
+ }
+
+ /* Refuse messages where the reply flag doesn't match up */
+ if (!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED) != !!(k->flags & KDBUS_MSG_EXPECT_REPLY)) {
+ r = -EBADMSG;
+ goto fail;
+ }
+
+ /* Refuse reply messages where the reply cookie doesn't match up */
+ if ((m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED) && m->reply_cookie != k->cookie_reply) {
+ r = -EBADMSG;
+ goto fail;
+ }
+
+ /* Refuse messages where the autostart flag doesn't match up */
+ if (!(m->header->flags & BUS_MESSAGE_NO_AUTO_START) != !(k->flags & KDBUS_MSG_NO_AUTO_START)) {
+ r = -EBADMSG;
+ goto fail;
+ }
+
+ /* Override information from the user header with data from the kernel */
+ if (k->src_id == KDBUS_SRC_ID_KERNEL)
+ bus_message_set_sender_driver(bus, m);
+ else {
+ xsprintf(m->sender_buffer, ":1.%llu", k->src_id);
+ m->sender = m->creds.unique_name = m->sender_buffer;
+ }
+
+ if (destination)
+ m->destination = destination;
+ else if (k->dst_id == KDBUS_DST_ID_BROADCAST)
+ m->destination = NULL;
+ else if (k->dst_id == KDBUS_DST_ID_NAME)
+ m->destination = bus->unique_name; /* fill in unique name if the well-known name is missing */
+ else {
+ xsprintf(m->destination_buffer, ":1.%llu", k->dst_id);
+ m->destination = m->destination_buffer;
+ }
+
+ /* We take possession of the kmsg struct now */
+ m->kdbus = k;
+ m->release_kdbus = true;
+ m->free_fds = true;
+ fds = NULL;
+
- bus->rqueue[bus->rqueue_size++] = m;
++ bus->rqueue[bus->rqueue_size++] = bus_message_ref_queued(m, bus);
+
+ return 1;
+
+fail:
+ unset_memfds(m);
+ sd_bus_message_unref(m);
+
+ return r;
+}
+
+int bus_kernel_take_fd(sd_bus *b) {
+ struct kdbus_bloom_parameter *bloom = NULL;
+ struct kdbus_item *items, *item;
+ struct kdbus_cmd_hello *hello;
+ _cleanup_free_ char *g = NULL;
+ const char *name;
+ size_t l = 0, m = 0, sz;
+ int r;
+
+ assert(b);
+
+ if (b->is_server)
+ return -EINVAL;
+
+ b->use_memfd = 1;
+
+ if (b->description) {
+ g = bus_label_escape(b->description);
+ if (!g)
+ return -ENOMEM;
+
+ name = g;
+ } else {
+ char pr[17] = {};
+
+ /* If no name is explicitly set, we'll include a hint
+ * indicating the library implementation, a hint which
+ * kind of bus this is and the thread name */
+
+ assert_se(prctl(PR_GET_NAME, (unsigned long) pr) >= 0);
+
+ if (isempty(pr)) {
+ name = b->is_system ? "sd-system" :
+ b->is_user ? "sd-user" : "sd";
+ } else {
+ _cleanup_free_ char *e = NULL;
+
+ e = bus_label_escape(pr);
+ if (!e)
+ return -ENOMEM;
+
+ g = strappend(b->is_system ? "sd-system-" :
+ b->is_user ? "sd-user-" : "sd-",
+ e);
+ if (!g)
+ return -ENOMEM;
+
+ name = g;
+ }
+
+ b->description = bus_label_unescape(name);
+ if (!b->description)
+ return -ENOMEM;
+ }
+
+ m = strlen(name);
+
+ sz = ALIGN8(offsetof(struct kdbus_cmd_hello, items)) +
+ ALIGN8(offsetof(struct kdbus_item, str) + m + 1);
+
+ if (b->fake_creds_valid)
+ sz += ALIGN8(offsetof(struct kdbus_item, creds) + sizeof(struct kdbus_creds));
+
+ if (b->fake_pids_valid)
+ sz += ALIGN8(offsetof(struct kdbus_item, pids) + sizeof(struct kdbus_pids));
+
+ if (b->fake_label) {
+ l = strlen(b->fake_label);
+ sz += ALIGN8(offsetof(struct kdbus_item, str) + l + 1);
+ }
+
+ hello = alloca0_align(sz, 8);
+ hello->size = sz;
+ hello->flags = b->hello_flags;
+ hello->attach_flags_send = _KDBUS_ATTACH_ANY;
+ hello->attach_flags_recv = b->attach_flags;
+ hello->pool_size = KDBUS_POOL_SIZE;
+
+ item = hello->items;
+
+ item->size = offsetof(struct kdbus_item, str) + m + 1;
+ item->type = KDBUS_ITEM_CONN_DESCRIPTION;
+ memcpy(item->str, name, m + 1);
+ item = KDBUS_ITEM_NEXT(item);
+
+ if (b->fake_creds_valid) {
+ item->size = offsetof(struct kdbus_item, creds) + sizeof(struct kdbus_creds);
+ item->type = KDBUS_ITEM_CREDS;
+ item->creds = b->fake_creds;
+
+ item = KDBUS_ITEM_NEXT(item);
+ }
+
+ if (b->fake_pids_valid) {
+ item->size = offsetof(struct kdbus_item, pids) + sizeof(struct kdbus_pids);
+ item->type = KDBUS_ITEM_PIDS;
+ item->pids = b->fake_pids;
+
+ item = KDBUS_ITEM_NEXT(item);
+ }
+
+ if (b->fake_label) {
+ item->size = offsetof(struct kdbus_item, str) + l + 1;
+ item->type = KDBUS_ITEM_SECLABEL;
+ memcpy(item->str, b->fake_label, l+1);
+ }
+
+ r = ioctl(b->input_fd, KDBUS_CMD_HELLO, hello);
+ if (r < 0) {
+ if (errno == ENOTTY)
+ /* If the ioctl is not supported we assume that the
+ * API version changed in a major incompatible way,
+ * let's indicate an API incompatibility in this
+ * case. */
+ return -ESOCKTNOSUPPORT;
+
+ return -errno;
+ }
+
+ if (!b->kdbus_buffer) {
+ b->kdbus_buffer = mmap(NULL, KDBUS_POOL_SIZE, PROT_READ, MAP_SHARED, b->input_fd, 0);
+ if (b->kdbus_buffer == MAP_FAILED) {
+ b->kdbus_buffer = NULL;
+ r = -errno;
+ goto fail;
+ }
+ }
+
+ /* The higher 32bit of the bus_flags fields are considered
+ * 'incompatible flags'. Refuse them all for now. */
+ if (hello->bus_flags > 0xFFFFFFFFULL) {
+ r = -ESOCKTNOSUPPORT;
+ goto fail;
+ }
+
+ /* extract bloom parameters from items */
+ items = (void*)((uint8_t*)b->kdbus_buffer + hello->offset);
+ KDBUS_FOREACH(item, items, hello->items_size) {
+ switch (item->type) {
+ case KDBUS_ITEM_BLOOM_PARAMETER:
+ bloom = &item->bloom_parameter;
+ break;
+ }
+ }
+
+ if (!bloom || !bloom_validate_parameters((size_t) bloom->size, (unsigned) bloom->n_hash)) {
+ r = -EOPNOTSUPP;
+ goto fail;
+ }
+
+ b->bloom_size = (size_t) bloom->size;
+ b->bloom_n_hash = (unsigned) bloom->n_hash;
+
+ if (asprintf(&b->unique_name, ":1.%llu", hello->id) < 0) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ b->unique_id = hello->id;
+
+ b->is_kernel = true;
+ b->bus_client = true;
+ b->can_fds = !!(hello->flags & KDBUS_HELLO_ACCEPT_FD);
+ b->message_version = 2;
+ b->message_endian = BUS_NATIVE_ENDIAN;
+
+ /* the kernel told us the UUID of the underlying bus */
+ memcpy(b->server_id.bytes, hello->id128, sizeof(b->server_id.bytes));
+
+ /* free returned items */
+ (void) bus_kernel_cmd_free(b, hello->offset);
+ return bus_start_running(b);
+
+fail:
+ (void) bus_kernel_cmd_free(b, hello->offset);
+ return r;
+}
+
+int bus_kernel_connect(sd_bus *b) {
+ assert(b);
+ assert(b->input_fd < 0);
+ assert(b->output_fd < 0);
+ assert(b->kernel);
+
+ if (b->is_server)
+ return -EINVAL;
+
+ b->input_fd = open(b->kernel, O_RDWR|O_NOCTTY|O_CLOEXEC);
+ if (b->input_fd < 0)
+ return -errno;
+
+ b->output_fd = b->input_fd;
+
+ return bus_kernel_take_fd(b);
+}
+
+int bus_kernel_cmd_free(sd_bus *bus, uint64_t offset) {
+ struct kdbus_cmd_free cmd = {
+ .size = sizeof(cmd),
+ .offset = offset,
+ };
+ int r;
+
+ assert(bus);
+ assert(bus->is_kernel);
+
+ r = ioctl(bus->input_fd, KDBUS_CMD_FREE, &cmd);
+ if (r < 0)
+ return -errno;
+
+ return 0;
+}
+
+static void close_kdbus_msg(sd_bus *bus, struct kdbus_msg *k) {
+ struct kdbus_item *d;
+
+ assert(bus);
+ assert(k);
+
+ KDBUS_ITEM_FOREACH(d, k, items) {
+ if (d->type == KDBUS_ITEM_FDS)
+ close_many(d->fds, (d->size - offsetof(struct kdbus_item, fds)) / sizeof(int));
+ else if (d->type == KDBUS_ITEM_PAYLOAD_MEMFD)
+ safe_close(d->memfd.fd);
+ }
+
+ bus_kernel_cmd_free(bus, (uint8_t*) k - (uint8_t*) bus->kdbus_buffer);
+}
+
+int bus_kernel_write_message(sd_bus *bus, sd_bus_message *m, bool hint_sync_call) {
+ struct kdbus_cmd_send cmd = { };
+ int r;
+
+ assert(bus);
+ assert(m);
+ assert(bus->state == BUS_RUNNING);
+
+ /* If we can't deliver, we want room for the error message */
+ r = bus_rqueue_make_room(bus);
+ if (r < 0)
+ return r;
+
+ r = bus_message_setup_kmsg(bus, m);
+ if (r < 0)
+ return r;
+
+ cmd.size = sizeof(cmd);
+ cmd.msg_address = (uintptr_t)m->kdbus;
+
+ /* If this is a synchronous method call, then let's tell the
+ * kernel, so that it can pass CPU time/scheduling to the
+ * destination for the time, if it wants to. If we
+ * synchronously wait for the result anyway, we won't need CPU
+ * anyway. */
+ if (hint_sync_call) {
+ m->kdbus->flags |= KDBUS_MSG_EXPECT_REPLY;
+ cmd.flags |= KDBUS_SEND_SYNC_REPLY;
+ }
+
+ r = ioctl(bus->output_fd, KDBUS_CMD_SEND, &cmd);
+ if (r < 0) {
+ _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
+ sd_bus_message *reply;
+
+ if (errno == EAGAIN || errno == EINTR)
+ return 0;
+ else if (errno == ENXIO || errno == ESRCH) {
+
+ /* ENXIO: unique name not known
+ * ESRCH: well-known name not known */
+
+ if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL)
+ sd_bus_error_setf(&error, SD_BUS_ERROR_SERVICE_UNKNOWN, "Destination %s not known", m->destination);
+ else {
+ log_debug("Could not deliver message to %s as destination is not known. Ignoring.", m->destination);
+ return 0;
+ }
+
+ } else if (errno == EADDRNOTAVAIL) {
+
+ /* EADDRNOTAVAIL: activation is possible, but turned off in request flags */
+
+ if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL)
+ sd_bus_error_setf(&error, SD_BUS_ERROR_SERVICE_UNKNOWN, "Activation of %s not requested", m->destination);
+ else {
+ log_debug("Could not deliver message to %s as destination is not activated. Ignoring.", m->destination);
+ return 0;
+ }
+ } else
+ return -errno;
+
+ r = bus_message_new_synthetic_error(
+ bus,
+ BUS_MESSAGE_COOKIE(m),
+ &error,
+ &reply);
+
+ if (r < 0)
+ return r;
+
+ r = bus_seal_synthetic_message(bus, reply);
+ if (r < 0)
+ return r;
+
- bus->rqueue[bus->rqueue_size++] = reply;
++ bus->rqueue[bus->rqueue_size++] = bus_message_ref_queued(reply, bus);
+
+ } else if (hint_sync_call) {
+ struct kdbus_msg *k;
+
+ k = (struct kdbus_msg *)((uint8_t *)bus->kdbus_buffer + cmd.reply.offset);
+ assert(k);
+
+ if (k->payload_type == KDBUS_PAYLOAD_DBUS) {
+
+ r = bus_kernel_make_message(bus, k);
+ if (r < 0) {
+ close_kdbus_msg(bus, k);
+
+ /* Anybody can send us invalid messages, let's just drop them. */
+ if (r == -EBADMSG || r == -EPROTOTYPE)
+ log_debug_errno(r, "Ignoring invalid synchronous reply: %m");
+ else
+ return r;
+ }
+ } else {
+ log_debug("Ignoring message with unknown payload type %llu.", k->payload_type);
+ close_kdbus_msg(bus, k);
+ }
+ }
+
+ return 1;
+}
+
+static int push_name_owner_changed(
+ sd_bus *bus,
+ const char *name,
+ const char *old_owner,
+ const char *new_owner,
+ const struct kdbus_timestamp *ts) {
+
+ _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
+ int r;
+
+ assert(bus);
+
+ r = sd_bus_message_new_signal(
+ bus,
+ &m,
+ "/org/freedesktop/DBus",
+ "org.freedesktop.DBus",
+ "NameOwnerChanged");
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_append(m, "sss", name, old_owner, new_owner);
+ if (r < 0)
+ return r;
+
+ bus_message_set_sender_driver(bus, m);
+ message_set_timestamp(bus, m, ts);
+
+ r = bus_seal_synthetic_message(bus, m);
+ if (r < 0)
+ return r;
+
- bus->rqueue[bus->rqueue_size++] = m;
++ bus->rqueue[bus->rqueue_size++] = bus_message_ref_queued(m, bus);
+ m = NULL;
+
+ return 1;
+}
+
+static int translate_name_change(
+ sd_bus *bus,
+ const struct kdbus_msg *k,
+ const struct kdbus_item *d,
+ const struct kdbus_timestamp *ts) {
+
+ char new_owner[UNIQUE_NAME_MAX], old_owner[UNIQUE_NAME_MAX];
+
+ assert(bus);
+ assert(k);
+ assert(d);
+
+ if (d->type == KDBUS_ITEM_NAME_ADD || (d->name_change.old_id.flags & (KDBUS_NAME_IN_QUEUE|KDBUS_NAME_ACTIVATOR)))
+ old_owner[0] = 0;
+ else
+ sprintf(old_owner, ":1.%llu", d->name_change.old_id.id);
+
+ if (d->type == KDBUS_ITEM_NAME_REMOVE || (d->name_change.new_id.flags & (KDBUS_NAME_IN_QUEUE|KDBUS_NAME_ACTIVATOR))) {
+
+ if (isempty(old_owner))
+ return 0;
+
+ new_owner[0] = 0;
+ } else
+ sprintf(new_owner, ":1.%llu", d->name_change.new_id.id);
+
+ return push_name_owner_changed(bus, d->name_change.name, old_owner, new_owner, ts);
+}
+
+static int translate_id_change(
+ sd_bus *bus,
+ const struct kdbus_msg *k,
+ const struct kdbus_item *d,
+ const struct kdbus_timestamp *ts) {
+
+ char owner[UNIQUE_NAME_MAX];
+
+ assert(bus);
+ assert(k);
+ assert(d);
+
+ sprintf(owner, ":1.%llu", d->id_change.id);
+
+ return push_name_owner_changed(
+ bus, owner,
+ d->type == KDBUS_ITEM_ID_ADD ? NULL : owner,
+ d->type == KDBUS_ITEM_ID_ADD ? owner : NULL,
+ ts);
+}
+
+static int translate_reply(
+ sd_bus *bus,
+ const struct kdbus_msg *k,
+ const struct kdbus_item *d,
+ const struct kdbus_timestamp *ts) {
+
+ _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
+ int r;
+
+ assert(bus);
+ assert(k);
+ assert(d);
+
+ r = bus_message_new_synthetic_error(
+ bus,
+ k->cookie_reply,
+ d->type == KDBUS_ITEM_REPLY_TIMEOUT ?
+ &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out") :
+ &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call peer died"),
+ &m);
+ if (r < 0)
+ return r;
+
+ message_set_timestamp(bus, m, ts);
+
+ r = bus_seal_synthetic_message(bus, m);
+ if (r < 0)
+ return r;
+
- bus->rqueue[bus->rqueue_size++] = m;
++ bus->rqueue[bus->rqueue_size++] = bus_message_ref_queued(m, bus);
+ m = NULL;
+
+ return 1;
+}
+
+static int bus_kernel_translate_message(sd_bus *bus, struct kdbus_msg *k) {
+ static int (* const translate[])(sd_bus *bus, const struct kdbus_msg *k, const struct kdbus_item *d, const struct kdbus_timestamp *ts) = {
+ [KDBUS_ITEM_NAME_ADD - _KDBUS_ITEM_KERNEL_BASE] = translate_name_change,
+ [KDBUS_ITEM_NAME_REMOVE - _KDBUS_ITEM_KERNEL_BASE] = translate_name_change,
+ [KDBUS_ITEM_NAME_CHANGE - _KDBUS_ITEM_KERNEL_BASE] = translate_name_change,
+
+ [KDBUS_ITEM_ID_ADD - _KDBUS_ITEM_KERNEL_BASE] = translate_id_change,
+ [KDBUS_ITEM_ID_REMOVE - _KDBUS_ITEM_KERNEL_BASE] = translate_id_change,
+
+ [KDBUS_ITEM_REPLY_TIMEOUT - _KDBUS_ITEM_KERNEL_BASE] = translate_reply,
+ [KDBUS_ITEM_REPLY_DEAD - _KDBUS_ITEM_KERNEL_BASE] = translate_reply,
+ };
+
+ struct kdbus_item *d, *found = NULL;
+ struct kdbus_timestamp *ts = NULL;
+
+ assert(bus);
+ assert(k);
+ assert(k->payload_type == KDBUS_PAYLOAD_KERNEL);
+
+ KDBUS_ITEM_FOREACH(d, k, items) {
+ if (d->type == KDBUS_ITEM_TIMESTAMP)
+ ts = &d->timestamp;
+ else if (d->type >= _KDBUS_ITEM_KERNEL_BASE && d->type < _KDBUS_ITEM_KERNEL_BASE + ELEMENTSOF(translate)) {
+ if (found)
+ return -EBADMSG;
+ found = d;
+ } else
+ log_debug("Got unknown field from kernel %llu", d->type);
+ }
+
+ if (!found) {
+ log_debug("Didn't find a kernel message to translate.");
+ return 0;
+ }
+
+ return translate[found->type - _KDBUS_ITEM_KERNEL_BASE](bus, k, found, ts);
+}
+
+int bus_kernel_read_message(sd_bus *bus, bool hint_priority, int64_t priority) {
+ struct kdbus_cmd_recv recv = { .size = sizeof(recv) };
+ struct kdbus_msg *k;
+ int r;
+
+ assert(bus);
+
+ r = bus_rqueue_make_room(bus);
+ if (r < 0)
+ return r;
+
+ if (hint_priority) {
+ recv.flags |= KDBUS_RECV_USE_PRIORITY;
+ recv.priority = priority;
+ }
+
+ r = ioctl(bus->input_fd, KDBUS_CMD_RECV, &recv);
+ if (recv.return_flags & KDBUS_RECV_RETURN_DROPPED_MSGS)
+ log_debug("%s: kdbus reports %" PRIu64 " dropped broadcast messages, ignoring.", strna(bus->description), (uint64_t) recv.dropped_msgs);
+ if (r < 0) {
+ if (errno == EAGAIN)
+ return 0;
+
+ return -errno;
+ }
+
+ k = (struct kdbus_msg *)((uint8_t *)bus->kdbus_buffer + recv.msg.offset);
+ if (k->payload_type == KDBUS_PAYLOAD_DBUS) {
+ r = bus_kernel_make_message(bus, k);
+
+ /* Anybody can send us invalid messages, let's just drop them. */
+ if (r == -EBADMSG || r == -EPROTOTYPE) {
+ log_debug_errno(r, "Ignoring invalid message: %m");
+ r = 0;
+ }
+
+ if (r <= 0)
+ close_kdbus_msg(bus, k);
+ } else if (k->payload_type == KDBUS_PAYLOAD_KERNEL) {
+ r = bus_kernel_translate_message(bus, k);
+ close_kdbus_msg(bus, k);
+ } else {
+ log_debug("Ignoring message with unknown payload type %llu.", k->payload_type);
+ r = 0;
+ close_kdbus_msg(bus, k);
+ }
+
+ return r < 0 ? r : 1;
+}
+
+int bus_kernel_pop_memfd(sd_bus *bus, void **address, size_t *mapped, size_t *allocated) {
+ struct memfd_cache *c;
+ int fd;
+
+ assert(address);
+ assert(mapped);
+ assert(allocated);
+
+ if (!bus || !bus->is_kernel)
+ return -EOPNOTSUPP;
+
+ assert_se(pthread_mutex_lock(&bus->memfd_cache_mutex) == 0);
+
+ if (bus->n_memfd_cache <= 0) {
+ int r;
+
+ assert_se(pthread_mutex_unlock(&bus->memfd_cache_mutex) == 0);
+
+ r = memfd_new(bus->description);
+ if (r < 0)
+ return r;
+
+ *address = NULL;
+ *mapped = 0;
+ *allocated = 0;
+ return r;
+ }
+
+ c = &bus->memfd_cache[--bus->n_memfd_cache];
+
+ assert(c->fd >= 0);
+ assert(c->mapped == 0 || c->address);
+
+ *address = c->address;
+ *mapped = c->mapped;
+ *allocated = c->allocated;
+ fd = c->fd;
+
+ assert_se(pthread_mutex_unlock(&bus->memfd_cache_mutex) == 0);
+
+ return fd;
+}
+
+static void close_and_munmap(int fd, void *address, size_t size) {
if (size > 0)
assert_se(munmap(address, PAGE_ALIGN(size)) >= 0);
message_reset_parts(m);
- sd_bus_unref(m->bus);
+ if (m->release_kdbus)
+ bus_kernel_cmd_free(m->bus, (uint8_t *) m->kdbus - (uint8_t *) m->bus->kdbus_buffer);
+
+ if (m->free_kdbus)
+ free(m->kdbus);
+
+ /* Note that we don't unref m->bus here. That's already done by sd_bus_message_unref() as each user
+ * reference to the bus message also is considered a reference to the bus connection itself. */
if (m->free_fds) {
close_many(m->fds, m->n_fds);
return 0;
}
+int bus_message_new_synthetic_reply(
+ sd_bus *bus,
+ uint64_t cookie,
+ sd_bus_message **m) {
+
+ sd_bus_message *t;
+ int r;
+
+ assert(bus);
+ assert(m);
+
+ r = sd_bus_message_new(bus, &t, SD_BUS_MESSAGE_METHOD_RETURN);
+ if (r < 0)
+ return -ENOMEM;
+
+ assert(t);
+
+ t->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
+ t->reply_cookie = cookie;
+
+ r = message_append_reply_cookie(t, t->reply_cookie);
+ if (r < 0)
+ goto fail;
+
+ if (bus && bus->unique_name) {
+ r = message_append_field_string(t, BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, bus->unique_name, &t->destination);
+ if (r < 0)
+ goto fail;
+ }
+
+ bus_message_set_sender_driver(bus, t);
+
+ *m = t;
+ return 0;
+
+fail:
+ message_free(t);
+ return r;
+}
- DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_bus_message, sd_bus_message, message_free);
+ _public_ sd_bus_message* sd_bus_message_ref(sd_bus_message *m) {
+ if (!m)
+ return NULL;
+
+ /* We are fine if this message so far was either explicitly reffed or not reffed but queued into at
+ * least one bus connection object. */
+ assert(m->n_ref > 0 || m->n_queued > 0);
+
+ m->n_ref++;
+
+ /* Each user reference to a bus message shall also be considered a ref on the bus */
+ sd_bus_ref(m->bus);
+ return m;
+ }
+
+ _public_ sd_bus_message* sd_bus_message_unref(sd_bus_message *m) {
+ if (!m)
+ return NULL;
+
+ assert(m->n_ref > 0);
+
+ sd_bus_unref(m->bus); /* Each regular ref is also a ref on the bus connection. Let's hence drop it
+ * here. Note we have to do this before decrementing our own n_ref here, since
+ * otherwise, if this message is currently queued sd_bus_unref() might call
+ * bus_message_unref_queued() for this which might then destroy the message
+ * while we are still processing it. */
+ m->n_ref--;
+
+ if (m->n_ref > 0 || m->n_queued > 0)
+ return NULL;
+
+ /* Unset the bus field if neither the user has a reference nor this message is queued. We are careful
+ * to reset the field only after the last reference to the bus is dropped, after all we might keep
+ * multiple references to the bus, once for each reference kept on outselves. */
+ m->bus = NULL;
+
+ return message_free(m);
+ }
+
+ sd_bus_message* bus_message_ref_queued(sd_bus_message *m, sd_bus *bus) {
+ if (!m)
+ return NULL;
+
+ /* If this is a different bus than the message is associated with, then implicitly turn this into a
+ * regular reference. This means that you can create a memory leak by enqueuing a message generated
+ * on one bus onto another at the same time as enqueueing a message from the second one on the first,
+ * as we'll not detect the cyclic references there. */
+ if (bus != m->bus)
+ return sd_bus_message_ref(m);
+
+ assert(m->n_ref > 0 || m->n_queued > 0);
+ m->n_queued++;
+
+ return m;
+ }
+
+ sd_bus_message* bus_message_unref_queued(sd_bus_message *m, sd_bus *bus) {
+ if (!m)
+ return NULL;
+
+ if (bus != m->bus)
+ return sd_bus_message_unref(m);
+
+ assert(m->n_queued > 0);
+ m->n_queued--;
+
+ if (m->n_ref > 0 || m->n_queued > 0)
+ return NULL;
+
+ m->bus = NULL;
+
+ return message_free(m);
+ }
_public_ int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type) {
assert_return(m, -EINVAL);
if (IN_SET(bus->state, BUS_RUNNING, BUS_HELLO) && bus->wqueue_size <= 0) {
size_t idx = 0;
- r = bus_write_message(bus, m, &idx);
+ r = bus_write_message(bus, m, hint_sync_call, &idx);
if (r < 0) {
- if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
+ if (ERRNO_IS_DISCONNECT(r)) {
bus_enter_closing(bus);
return -ECONNRESET;
}
#include <linux/if_bridge.h>
#include <linux/if_link.h>
#include <linux/if_tunnel.h>
+ #include <linux/l2tp.h>
#include <linux/veth.h>
-
-#if HAVE_LINUX_FOU_H
-#include <linux/fou.h>
-#endif
-
-#if HAVE_LINUX_CAN_VXCAN_H
-#include <linux/can/vxcan.h>
-#endif
+#include <linux/wireguard.h>
#include "macro.h"
#include "missing.h"
#include "in-addr-util.h"
#include "netdev.h"
#include "socket-util.h"
-#include "wireguard-netlink.h"
- #ifndef IFNAMSIZ
- #define IFNAMSIZ 16
- #endif
-
typedef struct WireguardIPmask {
uint16_t family;
union in_addr_union ip;
json-internal.h
json.c
json.h
+ libmount-util.h
+ linux/auto_dev-ioctl.h
+ linux/bpf.h
+ linux/bpf_common.h
+ linux/bpf_insn.h
+ linux/dm-ioctl.h
+ linux/ethtool.h
+ linux/netdevice.h
lockfile-util.c
lockfile-util.h
+ log-link.h
logs-show.c
logs-show.h
loop-util.c
#include "escape.h"
#include "fd-util.h"
#include "fstab-util.h"
-#include "linux-3.13/dm-ioctl.h"
+ #include "libmount-util.h"
#include "mount-setup.h"
#include "mount-util.h"
#include "mountpoint-util.h"
#include "alloc-util.h"
#include "fd-util.h"
+#include "hexdecoct.h"
+#include "io-util.h"
#include "macro.h"
+#include "path-util.h"
+#include "rm-rf.h"
#include "missing.h"
#include "mountpoint-util.h"
+ #include "namespace-util.h"
#include "path-util.h"
#include "stat-util.h"
+#include "stdio-util.h"
+#include "string-util.h"
#include "tmpfile-util.h"
static void test_files_same(void) {
#include "fd-util.h"
#include "link-config.h"
#include "log.h"
-#include "missing_network.h"
+ #include "memory-util.h"
#include "naming-scheme.h"
#include "netlink-util.h"
#include "network-internal.h"
PATH_TO_INIT=$ROOTLIBDIR/systemd
- BASICTOOLS="test sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe sed cmp tee rm true false chmod chown ln xargs"
- DEBUGTOOLS="df free ls stty cat ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort hostname find"
+ BASICTOOLS="test sh bash setsid loadkeys setfont login sulogin gzip sleep echo head tail cat mount umount cryptsetup date dmsetup modprobe sed cmp tee rm true false chmod chown ln xargs"
+ DEBUGTOOLS="df free ls stty ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort hostname find vi mv"
-STATEDIR="${BUILD_DIR:-.}/test/$(basename $(dirname $(realpath $0)))"
+STATEDIR="${BUILD_DIR:-.}/test/$(basename $(dirname $(readlink -f $0)))"
STATEFILE="$STATEDIR/.testdir"
TESTLOG="$STATEDIR/test.log"
'sockets.target.wants/'],
['systemd-udevd-kernel.socket', '',
'sockets.target.wants/'],
+ ['time-set.target', ''],
['time-sync.target', ''],
['timers.target', ''],
- ['tmp.mount', '',
- 'local-fs.target.wants/'],
['umount.target', ''],
['user.slice', ''],
['var-lib-machines.mount', 'ENABLE_MACHINED',
['console-getty.service', ''],
['container-getty@.service', ''],
['getty@.service', '',
- 'autovt@.service ' +
- join_paths(pkgsysconfdir, 'system/getty.target.wants/getty@tty1.service')],
+ 'autovt@.service '],
['serial-getty@.service', ''],
+ ['tmp.mount', '',
+ 'local-fs.target.wants/'],
]
foreach tuple : in_units
RestrictAddressFamilies=AF_UNIX
RestrictNamespaces=yes
RestrictRealtime=yes
+ RestrictSUIDSGID=yes
+SmackProcessLabel=System
SystemCallArchitectures=native
SystemCallErrorNumber=EPERM
SystemCallFilter=@system-service sethostname
RestrictAddressFamilies=AF_UNIX AF_NETLINK
RestrictNamespaces=yes
RestrictRealtime=yes
+ RestrictSUIDSGID=yes
+SmackProcessLabel=System
Sockets=systemd-journald.socket systemd-journald-dev-log.socket systemd-journald-audit.socket
StandardOutput=null
SystemCallArchitectures=native
RestrictAddressFamilies=AF_UNIX
RestrictNamespaces=yes
RestrictRealtime=yes
+ RestrictSUIDSGID=yes
+SmackProcessLabel=System
SystemCallArchitectures=native
SystemCallErrorNumber=EPERM
SystemCallFilter=@system-service
RestrictAddressFamilies=AF_UNIX AF_NETLINK
RestrictNamespaces=yes
RestrictRealtime=yes
+ RestrictSUIDSGID=yes
+ RuntimeDirectory=systemd/sessions systemd/seats systemd/users systemd/inhibit systemd/shutdown
+ RuntimeDirectoryPreserve=yes
+SmackProcessLabel=System::Privileged
SystemCallArchitectures=native
SystemCallErrorNumber=EPERM
SystemCallFilter=@system-service
LockPersonality=yes
MemoryDenyWriteExecute=yes
NoNewPrivileges=yes
+ ProtectHostname=yes
RestrictAddressFamilies=AF_UNIX AF_NETLINK AF_INET AF_INET6
RestrictRealtime=yes
+SmackProcessLabel=System
SystemCallArchitectures=native
SystemCallErrorNumber=EPERM
SystemCallFilter=@system-service @mount
KillMode=mixed
WatchdogSec=3min
TasksMax=infinity
+SmackProcessLabel=System::Privileged
PrivateMounts=yes
+ ProtectHostname=yes
MemoryDenyWriteExecute=yes
- RestrictRealtime=yes
RestrictAddressFamilies=AF_UNIX AF_NETLINK AF_INET AF_INET6
+ RestrictRealtime=yes
+ RestrictSUIDSGID=yes
SystemCallFilter=@system-service @module @raw-io
SystemCallErrorNumber=EPERM
SystemCallArchitectures=native
KillMode=mixed
Delegate=pids memory
TasksMax=infinity
+Environment=DBUS_SESSION_BUS_ADDRESS=kernel:path=/sys/fs/kdbus/%i-user/bus;unix:path=/run/user/%i/bus
+Environment=XDG_RUNTIME_DIR=/run/user/%i
+Capabilities=cap_sys_admin,cap_mac_admin,cap_setgid,cap_dac_override=i
+SecureBits=keep-caps
+TimeoutStartSec=infinity
TimeoutStopSec=120s
+ KeyringMode=inherit