File macros.systemd of Package systemd-rpm-macros

#  -*- mode: shell-script; indent-tabs-mode: t -*-
# RPM macros for packages installing systemd unit files
#
###
#
# When a package install systemd unit files, it should use the following macros:
#
# add %systemd_requires in the specfile
#
# %pre
# %service_add_pre demo.service demo1.service
#
# %post
# %service_add_post demo.service demo1.service
#
# %preun
# %service_del_preun demo.service
#
# %postun
# %service_del_postun demo.service
# %service_del_postun_without_restart demo.service
#
# Note: the upstream variants are also available and are aliases to their SUSE
# counterparts. However for consistency the SUSE macros should be preferred
# unless the package is intended to be portable across multiple distributions
# based on RPM.

%_systemd_util_dir /usr/lib/systemd
%_unitdir /usr/lib/systemd/system
%_userunitdir /usr/lib/systemd/user
%_presetdir /usr/lib/systemd/system-preset
%_userpresetdir /usr/lib/systemd/user-preset
%_udevhwdbdir /usr/lib/udev/hwdb.d
%_udevrulesdir /usr/lib/udev/rules.d
%_journalcatalogdir /usr/lib/systemd/catalog
%_tmpfilesdir /usr/lib/tmpfiles.d
%_user_tmpfilesdir /usr/share/user-tmpfiles.d
%_sysusersdir /usr/lib/sysusers.d
%_sysctldir /usr/lib/sysctl.d
%_ntpunitsdir /usr/lib/systemd/ntp-units.d
%_binfmtdir /usr/lib/binfmt.d
%_environmentdir /usr/lib/environment.d
%_modulesloaddir /usr/lib/modules-load.d
%_modprobedir /usr/lib/modprobe.d
%_systemdgeneratordir /usr/lib/systemd/system-generators
%_systemdusergeneratordir /usr/lib/systemd/user-generators
%_systemd_system_env_generator_dir /usr/lib/systemd/system-environment-generators
%_systemd_user_env_generator_dir /usr/lib/systemd/user-environment-generators

%systemd_requires \
Requires(pre): systemd \
Requires(post): systemd \
Requires(preun): systemd \
Requires(postun): systemd \
%{nil}

# In case you're wondering why "Suggests:" is also used: libzypp
# doesn't understand "OrderWithRequires:" yet, see bsc#1187332 for
# details.
%systemd_ordering \
OrderWithRequires(pre): systemd \
OrderWithRequires(post): systemd \
OrderWithRequires(preun): systemd \
OrderWithRequires(postun): systemd \
Suggests: systemd \
%{nil}

%__systemd_someargs_0(:) %{error:The %%%1 macro requires some arguments}
%__systemd_twoargs_2() %{nil}

# Find when presets need to be applied. This information is only recorded during
# %pre and is actually applied during %post.
#
# Presets might need to be applied during package updates too as new services
# might be introduced in this case. Note that on package installations, presets
# might have been already applied. This can happen when packages have been
# renamed or splitted into sub-packages.
#
%service_add_pre()								\
%{expand:%%{?__systemd_someargs_%#:%%__systemd_someargs_%# service_add_pre}}	\
if [ -x /usr/lib/systemd/systemd-update-helper ]; then				\
	/usr/lib/systemd/systemd-update-helper mark-install-system-units %{?*} || : \
fi										\
%{nil}

# Apply the presets if %pre told us to do so.
#
%service_add_post()								\
%{expand:%%{?__systemd_someargs_%#:%%__systemd_someargs_%# service_add_post}}	\
if [ -x /usr/lib/systemd/systemd-update-helper ]; then				\
	/usr/lib/systemd/systemd-update-helper install-system-units %{?*} || :	\
fi										\
%{nil}

# On uninstall, disable and stop services.
#
%service_del_preun()								\
%{expand:%%{?__systemd_someargs_%#:%%__systemd_someargs_%# service_del_preun}}	\
if [ $1 -eq 0 ] && [ -x /usr/lib/systemd/systemd-update-helper ]; then		\
	# Package removal, not upgrade						\
	/usr/lib/systemd/systemd-update-helper remove-system-units %{?*} || :	\
fi										\
%{nil}

# On uninstall, tell systemd to reload its unit files.
# On update, tell systemd to reload its unit files but don't restart service.
#
# It ignores the content of /etc/sysconfig/services
#
%service_del_postun_without_restart()						\
%{expand:%%{?__systemd_someargs_%#:%%__systemd_someargs_%# service_del_postun_without_restart}} \
:										\
%{nil}

# On uninstall, tell systemd to reload its unit files.
# On update, tell systemd to reload its unit files and restart service.
#
# It ignores the content of /etc/sysconfig/services
#
%service_del_postun_with_restart()						\
%{expand:%%{?__systemd_someargs_%#:%%__systemd_someargs_%# service_del_postun_with_restart}} \
if [ $1 -ge 1 ] && [ -x /usr/lib/systemd/systemd-update-helper ]; then		\
	# Package upgrade, not uninstall					\
	/usr/lib/systemd/systemd-update-helper mark-restart-system-units %{?*} || : \
fi										\
%{nil}

# On uninstall, tell systemd to reload its unit files.
# On update, tell systemd to reload and restart service unless
# DISABLE_RESTART_ON_UPDATE from /etc/sysconfig/services says otherwise.
#
%service_del_postun()								\
%{expand:%%{?__systemd_someargs_%#:%%__systemd_someargs_%# service_del_postun}}	\
if [ $1 -ge 1 ] && [ -x /usr/lib/systemd/systemd-update-helper ]; then		\
	# Package upgrade, not uninstall					\
	DISABLE_RESTART_ON_UPDATE=no						\
	[ -e /etc/sysconfig/services ] && . /etc/sysconfig/services || :	\
										\
	case "$DISABLE_RESTART_ON_UPDATE" in					\
	yes|1)	;;								\
	*)	/usr/lib/systemd/systemd-update-helper mark-restart-system-units %{?*} || : \
	esac									\
fi										\
%{nil}

#
# Upstream variants
#

%systemd_pre() %{expand::%%service_add_pre %{?**}}
%systemd_post() %{expand::%%service_add_post %{?**}}
%systemd_preun() %{expand::%%service_del_preun %{?**}}
%systemd_postun() %{expand::%%service_del_postun_without_restart %{?**}}
%systemd_postun_with_restart() %{expand::%%service_del_postun_with_restart %{?**}}

#
# Variants dealing with user units.
#

%systemd_user_pre()								\
%{expand:%%{?__systemd_someargs_%#:%%__systemd_someargs_%# systemd_user_pre}}	\
if [ -x /usr/lib/systemd/systemd-update-helper ]; then				\
	/usr/lib/systemd/systemd-update-helper mark-install-user-units %{?*} || : \
fi										\
%{nil}

%systemd_user_post()								\
%{expand:%%{?__systemd_someargs_%#:%%__systemd_someargs_%# systemd_user_post}}	\
if [ -x /usr/lib/systemd/systemd-update-helper ]; then				\
	/usr/lib/systemd/systemd-update-helper install-user-units %{?*} || :	\
fi										\
%{nil}

%systemd_user_preun()								\
%{expand:%%{?__systemd_someargs_%#:%%__systemd_someargs_%# systemd_user_preun}}	\
if [ $1 -eq 0 ] && [ -x /usr/lib/systemd/systemd-update-helper ]; then		\
	# Package removal, not upgrade						\
	/usr/lib/systemd/systemd-update-helper remove-user-units %{?*} || :	\
fi										\
%{nil}

%systemd_user_postun()								\
%{expand:%%{?__systemd_someargs_%#:%%__systemd_someargs_%# systemd_user_postun}} \
:										\
%{nil}

%systemd_user_postun_with_restart()						\
%{expand:%%{?__systemd_someargs_%#:%%__systemd_someargs_%# systemd_user_postun_with_restart}} \
if [ $1 -ge 1 ] && [ -x /usr/lib/systemd/systemd-update-helper ]; then		\
	# Package upgrade, not uninstall					\
	/usr/lib/systemd/systemd-update-helper mark-restart-user-units %{?*} || : \
fi										\
%{nil}

#
# The following macros are empty as systemd trans-file-triggers are in place for
# hwdb, journal catalog, udev rules, binfmt and sysctl.
#
# Note: 'sysctl_apply' and 'binfmt_apply' don't have any users that rely on
# their immediate effect.
#
%udev_hwdb_update() :%{nil}

%udev_rules_update() :%{nil}

%journal_catalog_update() :%{nil}

%sysctl_apply()									\
%{expand:%%{?__systemd_someargs_%#:%%__systemd_someargs_%# sysctl_apply}}	\
:										\
%{nil}

%binfmt_apply()									\
%{expand:%%{?__systemd_someargs_%#:%%__systemd_someargs_%# binfmt_apply}}	\
:										\
%{nil}

#
# This should be used when a package needs to execute the shipped udev rules
# during its installation. It automatically triggers a reload of the rule files,
# as this step is often overlooked by packages.
#
# Note1: The version of this macro shipped with SLE < 16 only supports a limited
# subset of options, due to the older RPM versions (< 4.17), which lacks support
# for disabling option processing.
#
# Note2: This is suboptimal when multiple events need to be triggered, as it
# results in multiple reloads of the rule files.
#
%udev_trigger_with_reload(-)							\
if [ -e /run/udev/control ]; then						\
	/usr/bin/udevadm control --reload && /usr/bin/udevadm trigger %{?*} || : \
fi										\
%{nil}

# The 2 following macros have also been converted into NOPs since we moved to
# file triggers. Some packages might have assumed that their effects were
# effective as soon as the macros return. However such assumption on tmpfiles
# can't work on transactional systems anyways where changes must take place on
# reboot.
#
# When a system user/group needs to be created in %%pre, so proper ownership are
# used when package's files are installed, "sysusers_create_package()" should be
# used.

%tmpfiles_create()								\
%{expand:%%{?__systemd_someargs_%#:%%__systemd_someargs_%# tmpfiles_create}}	\
:										\
%{nil}

%sysusers_create()								\
%{expand:%%{?__systemd_someargs_%#:%%__systemd_someargs_%# sysusers_create}}	\
:										\
%{nil}

# This should be used by package installation scripts which require users or
# groups to be present before the files installed by the package are present on
# disk (for example because some files are owned by those users or groups).
#
# Example:
#   Source1: %{name}-sysusers.conf
#   ...
#   %install
#   install -D %SOURCE1 %{buildroot}%{_sysusersdir}/%{name}.conf
#   %pre
#   %sysusers_create_package %{name} %SOURCE1
#   %files
#   %{_sysusersdir}/%{name}.conf
#
# Note: writing to /etc is not recommended on transactional systems but the
# sysusers must be created now since by calling this macro the package explicitly
# requests that.
#
%sysusers_create_package() \
%{expand:%%{?!__systemd_twoargs_%#:%%{error:The %%%%sysusers_create_package macro requires two arguments}}} \
[ -x /usr/bin/systemd-sysusers ] && /usr/bin/systemd-sysusers --replace=%_sysusersdir/%1.conf - <<SYSTEMD_INLINE_EOF || : \
%(cat %2) \
SYSTEMD_INLINE_EOF\
%{nil}
openSUSE Build Service is sponsored by