1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: systemd.eclass
8 # @BLURB: helper functions to install systemd units
10 # This eclass provides a set of functions to install unit files for
11 # sys-apps/systemd within ebuilds.
21 # "$(systemd_with_unitdir)"
24 # econf "${myeconfargs[@]}"
28 inherit eutils toolchain-funcs
32 *) die "${ECLASS}.eclass API in EAPI ${EAPI} not yet established."
35 DEPEND="virtual/pkgconfig"
37 # @FUNCTION: _systemd_get_dir
38 # @USAGE: <variable-name> <fallback-directory>
41 # Try to obtain the <variable-name> variable from systemd.pc.
42 # If pkg-config or systemd is not installed, return <fallback-directory>
45 [[ ${#} -eq 2 ]] || die "Usage: ${FUNCNAME} <variable-name> <fallback-directory>"
46 local variable=${1} fallback=${2} d
48 if $(tc-getPKG_CONFIG) --exists systemd; then
49 d=$($(tc-getPKG_CONFIG) --variable="${variable}" systemd) || die
57 # @FUNCTION: _systemd_get_unitdir
60 # Get unprefixed unitdir.
61 _systemd_get_unitdir() {
62 _systemd_get_dir systemdsystemunitdir /usr/lib/systemd/system
65 # @FUNCTION: systemd_get_unitdir
67 # Output the path for the systemd unit directory (not including ${D}).
68 # This function always succeeds, even if systemd is not installed.
69 systemd_get_unitdir() {
70 has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX=
71 debug-print-function ${FUNCNAME} "${@}"
73 echo "${EPREFIX}$(_systemd_get_unitdir)"
76 # @FUNCTION: _systemd_get_userunitdir
79 # Get unprefixed userunitdir.
80 _systemd_get_userunitdir() {
81 _systemd_get_dir systemduserunitdir /usr/lib/systemd/user
84 # @FUNCTION: systemd_get_userunitdir
86 # Output the path for the systemd user unit directory (not including
87 # ${D}). This function always succeeds, even if systemd is not
89 systemd_get_userunitdir() {
90 has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX=
91 debug-print-function ${FUNCNAME} "${@}"
93 echo "${EPREFIX}$(_systemd_get_userunitdir)"
96 # @FUNCTION: _systemd_get_utildir
99 # Get unprefixed utildir.
100 _systemd_get_utildir() {
101 _systemd_get_dir systemdutildir /usr/lib/systemd
104 # @FUNCTION: systemd_get_utildir
106 # Output the path for the systemd utility directory (not including
107 # ${D}). This function always succeeds, even if systemd is not
109 systemd_get_utildir() {
110 has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX=
111 debug-print-function ${FUNCNAME} "${@}"
113 echo "${EPREFIX}$(_systemd_get_utildir)"
116 # @FUNCTION: systemd_dounit
119 # Install systemd unit(s). Uses doins, thus it is fatal in EAPI 4
120 # and non-fatal in earlier EAPIs.
122 debug-print-function ${FUNCNAME} "${@}"
125 insinto "$(_systemd_get_unitdir)"
130 # @FUNCTION: systemd_newunit
131 # @USAGE: <old-name> <new-name>
133 # Install systemd unit with a new name. Uses newins, thus it is fatal
134 # in EAPI 4 and non-fatal in earlier EAPIs.
136 debug-print-function ${FUNCNAME} "${@}"
139 insinto "$(_systemd_get_unitdir)"
144 # @FUNCTION: systemd_douserunit
147 # Install systemd user unit(s). Uses doins, thus it is fatal in EAPI 4
148 # and non-fatal in earlier EAPIs.
149 systemd_douserunit() {
150 debug-print-function ${FUNCNAME} "${@}"
153 insinto "$(_systemd_get_userunitdir)"
158 # @FUNCTION: systemd_newuserunit
159 # @USAGE: <old-name> <new-name>
161 # Install systemd user unit with a new name. Uses newins, thus it
162 # is fatal in EAPI 4 and non-fatal in earlier EAPIs.
163 systemd_newuserunit() {
164 debug-print-function ${FUNCNAME} "${@}"
167 insinto "$(_systemd_get_userunitdir)"
172 # @FUNCTION: systemd_install_serviced
173 # @USAGE: <conf-file> [<service.d>]
175 # Install the file <conf-file> as service.d/00gentoo.conf template.
176 # The <service.d> argument specifies the configured service name.
177 # If not specified, the configuration file name will be used with .conf
178 # suffix stripped (e.g. foo.service.conf -> foo.service).
179 systemd_install_serviced() {
180 debug-print-function ${FUNCNAME} "${@}"
185 [[ ${src} ]] || die "No file specified"
187 if [[ ! ${service} ]]; then
188 [[ ${src} == *.conf ]] || die "Source file needs .conf suffix"
190 service=${service%.conf}
192 # avoid potentially common mistake
193 [[ ${service} == *.d ]] && die "Service must not have .d suffix"
196 insinto /etc/systemd/system/"${service}".d
197 newins "${src}" 00gentoo.conf
201 # @FUNCTION: systemd_dotmpfilesd
202 # @USAGE: <tmpfilesd>...
204 # Install systemd tmpfiles.d files. Uses doins, thus it is fatal
205 # in EAPI 4 and non-fatal in earlier EAPIs.
206 systemd_dotmpfilesd() {
207 debug-print-function ${FUNCNAME} "${@}"
210 [[ ${f} == *.conf ]] \
211 || die 'tmpfiles.d files need to have .conf suffix.'
215 insinto /usr/lib/tmpfiles.d/
220 # @FUNCTION: systemd_newtmpfilesd
221 # @USAGE: <old-name> <new-name>.conf
223 # Install systemd tmpfiles.d file under a new name. Uses newins, thus it
224 # is fatal in EAPI 4 and non-fatal in earlier EAPIs.
225 systemd_newtmpfilesd() {
226 debug-print-function ${FUNCNAME} "${@}"
228 [[ ${2} == *.conf ]] \
229 || die 'tmpfiles.d files need to have .conf suffix.'
232 insinto /usr/lib/tmpfiles.d/
237 # @FUNCTION: systemd_enable_service
238 # @USAGE: <target> <service>
240 # Enable service in desired target, e.g. install a symlink for it.
241 # Uses dosym, thus it is fatal in EAPI 4 and non-fatal in earlier
243 systemd_enable_service() {
244 debug-print-function ${FUNCNAME} "${@}"
246 [[ ${#} -eq 2 ]] || die "Synopsis: systemd_enable_service target service"
250 local ud=$(_systemd_get_unitdir)
251 local destname=${service##*/}
253 dodir "${ud}"/"${target}".wants && \
254 dosym ../"${service}" "${ud}"/"${target}".wants/"${destname}"
257 # @FUNCTION: systemd_enable_ntpunit
258 # @USAGE: <NN-name> <service>...
260 # Add an NTP service provider to the list of implementations
261 # in timedated. <NN-name> defines the newly-created ntp-units.d priority
262 # and name, while the remaining arguments list service units that will
263 # be added to that file.
265 # Uses doins, thus it is fatal in EAPI 4 and non-fatal in earlier
268 # Doc: http://www.freedesktop.org/wiki/Software/systemd/timedated/
269 systemd_enable_ntpunit() {
270 debug-print-function ${FUNCNAME} "${@}"
271 if [[ ${#} -lt 2 ]]; then
272 die "Usage: systemd_enable_ntpunit <NN-name> <service>..."
275 local ntpunit_name=${1}
276 local services=( "${@:2}" )
278 if [[ ${ntpunit_name} != [0-9][0-9]-* ]]; then
279 die "ntpunit.d file must be named NN-name where NN are digits."
280 elif [[ ${ntpunit_name} == *.list ]]; then
281 die "The .list suffix is appended implicitly to ntpunit.d name."
284 local unitdir=$(systemd_get_unitdir)
286 for s in "${services[@]}"; do
287 if [[ ! -f "${D}${unitdir}/${s}" ]]; then
288 die "ntp-units.d provider ${s} not installed (yet?) in \${D}."
290 echo "${s}" >> "${T}"/${ntpunit_name}.list
294 insinto "$(_systemd_get_utildir)"/ntp-units.d
295 doins "${T}"/${ntpunit_name}.list
299 rm "${T}"/${ntpunit_name}.list || die
304 # @FUNCTION: systemd_with_unitdir
305 # @USAGE: [<configure-option-name>]
307 # Output '--with-systemdsystemunitdir' as expected by systemd-aware configure
308 # scripts. This function always succeeds. Its output may be quoted in order
309 # to preserve whitespace in paths. systemd_to_myeconfargs() is preferred over
312 # If upstream does use invalid configure option to handle installing systemd
313 # units (e.g. `--with-systemdunitdir'), you can pass the 'suffix' as an optional
314 # argument to this function (`$(systemd_with_unitdir systemdunitdir)'). Please
315 # remember to report a bug upstream as well.
316 systemd_with_unitdir() {
317 debug-print-function ${FUNCNAME} "${@}"
318 local optname=${1:-systemdsystemunitdir}
320 echo --with-${optname}="$(systemd_get_unitdir)"
323 # @FUNCTION: systemd_with_utildir
325 # Output '--with-systemdsystemutildir' as used by some packages to install
326 # systemd helpers. This function always succeeds. Its output may be quoted
327 # in order to preserve whitespace in paths.
328 systemd_with_utildir() {
329 debug-print-function ${FUNCNAME} "${@}"
331 echo --with-systemdutildir="$(systemd_get_utildir)"
334 # @FUNCTION: systemd_to_myeconfargs
336 # Add '--with-systemdsystemunitdir' as expected by systemd-aware configure
337 # scripts to the myeconfargs variable used by autotools-utils eclass. Handles
338 # quoting automatically.
339 systemd_to_myeconfargs() {
340 debug-print-function ${FUNCNAME} "${@}"
342 eqawarn 'systemd_to_myeconfargs() is deprecated and will be removed on 2013-10-11.'
343 eqawarn 'Please use $(systemd_with_unitdir) instead.'
347 --with-systemdsystemunitdir="$(systemd_get_unitdir)"
351 # @FUNCTION: systemd_update_catalog
353 # Update the journald catalog. This needs to be called after installing
354 # or removing catalog files.
356 # If systemd is not installed, no operation will be done. The catalog
357 # will be (re)built once systemd is installed.
359 # See: http://www.freedesktop.org/wiki/Software/systemd/catalog
360 systemd_update_catalog() {
361 debug-print-function ${FUNCNAME} "${@}"
363 # Make sure to work on the correct system.
365 local journalctl=${EPREFIX}/usr/bin/journalctl
366 if [[ -x ${journalctl} ]]; then
367 ebegin "Updating systemd journal catalogs"
368 journalctl --update-catalog
371 debug-print "${FUNCNAME}: journalctl not found."
375 # @FUNCTION: systemd_is_booted
377 # Check whether the system was booted using systemd.
379 # This should be used purely for informational purposes, e.g. warning
380 # user that he needs to use systemd. Installed files or application
381 # behavior *must not* rely on this. Please remember to check MERGE_TYPE
382 # to not trigger the check on binary package build hosts!
384 # Returns 0 if systemd is used to boot the system, 1 otherwise.
387 systemd_is_booted() {
388 debug-print-function ${FUNCNAME} "${@}"
390 [[ -d /run/systemd/system ]]
393 debug-print "${FUNCNAME}: [[ -d /run/systemd/system ]] -> ${ret}"