systemd.eclass: Introduce a common getter for systemd directories
[gentoo.git] / eclass / systemd.eclass
1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: systemd.eclass
6 # @MAINTAINER:
7 # systemd@gentoo.org
8 # @BLURB: helper functions to install systemd units
9 # @DESCRIPTION:
10 # This eclass provides a set of functions to install unit files for
11 # sys-apps/systemd within ebuilds.
12 # @EXAMPLE:
13 #
14 # @CODE
15 # inherit systemd
16 #
17 # src_configure() {
18 #       local myeconfargs=(
19 #               --enable-foo
20 #               --disable-bar
21 #               "$(systemd_with_unitdir)"
22 #       )
23 #
24 #       econf "${myeconfargs[@]}"
25 # }
26 # @CODE
27
28 inherit eutils toolchain-funcs
29
30 case ${EAPI:-0} in
31         0|1|2|3|4|5) ;;
32         *) die "${ECLASS}.eclass API in EAPI ${EAPI} not yet established."
33 esac
34
35 DEPEND="virtual/pkgconfig"
36
37 # @FUNCTION: _systemd_get_dir
38 # @USAGE: <variable-name> <fallback-directory>
39 # @INTERNAL
40 # @DESCRIPTION:
41 # Try to obtain the <variable-name> variable from systemd.pc.
42 # If pkg-config or systemd is not installed, return <fallback-directory>
43 # instead.
44 _systemd_get_dir() {
45         [[ ${#} -eq 2 ]] || die "Usage: ${FUNCNAME} <variable-name> <fallback-directory>"
46         local variable=${1} fallback=${2} d
47
48         if $(tc-getPKG_CONFIG) --exists systemd; then
49                 d=$($(tc-getPKG_CONFIG) --variable="${variable}" systemd) || die
50         else
51                 d=${fallback}
52         fi
53
54         echo "${d}"
55 }
56
57 # @FUNCTION: _systemd_get_unitdir
58 # @INTERNAL
59 # @DESCRIPTION:
60 # Get unprefixed unitdir.
61 _systemd_get_unitdir() {
62         _systemd_get_dir systemdsystemunitdir /usr/lib/systemd/system
63 }
64
65 # @FUNCTION: systemd_get_unitdir
66 # @DESCRIPTION:
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} "${@}"
72
73         echo "${EPREFIX}$(_systemd_get_unitdir)"
74 }
75
76 # @FUNCTION: _systemd_get_userunitdir
77 # @INTERNAL
78 # @DESCRIPTION:
79 # Get unprefixed userunitdir.
80 _systemd_get_userunitdir() {
81         _systemd_get_dir systemduserunitdir /usr/lib/systemd/user
82 }
83
84 # @FUNCTION: systemd_get_userunitdir
85 # @DESCRIPTION:
86 # Output the path for the systemd user unit directory (not including
87 # ${D}). This function always succeeds, even if systemd is not
88 # installed.
89 systemd_get_userunitdir() {
90         has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX=
91         debug-print-function ${FUNCNAME} "${@}"
92
93         echo "${EPREFIX}$(_systemd_get_userunitdir)"
94 }
95
96 # @FUNCTION: _systemd_get_utildir
97 # @INTERNAL
98 # @DESCRIPTION:
99 # Get unprefixed utildir.
100 _systemd_get_utildir() {
101         _systemd_get_dir systemdutildir /usr/lib/systemd
102 }
103
104 # @FUNCTION: systemd_get_utildir
105 # @DESCRIPTION:
106 # Output the path for the systemd utility directory (not including
107 # ${D}). This function always succeeds, even if systemd is not
108 # installed.
109 systemd_get_utildir() {
110         has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX=
111         debug-print-function ${FUNCNAME} "${@}"
112
113         echo "${EPREFIX}$(_systemd_get_utildir)"
114 }
115
116 # @FUNCTION: systemd_dounit
117 # @USAGE: <unit>...
118 # @DESCRIPTION:
119 # Install systemd unit(s). Uses doins, thus it is fatal in EAPI 4
120 # and non-fatal in earlier EAPIs.
121 systemd_dounit() {
122         debug-print-function ${FUNCNAME} "${@}"
123
124         (
125                 insinto "$(_systemd_get_unitdir)"
126                 doins "${@}"
127         )
128 }
129
130 # @FUNCTION: systemd_newunit
131 # @USAGE: <old-name> <new-name>
132 # @DESCRIPTION:
133 # Install systemd unit with a new name. Uses newins, thus it is fatal
134 # in EAPI 4 and non-fatal in earlier EAPIs.
135 systemd_newunit() {
136         debug-print-function ${FUNCNAME} "${@}"
137
138         (
139                 insinto "$(_systemd_get_unitdir)"
140                 newins "${@}"
141         )
142 }
143
144 # @FUNCTION: systemd_douserunit
145 # @USAGE: <unit>...
146 # @DESCRIPTION:
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} "${@}"
151
152         (
153                 insinto "$(_systemd_get_userunitdir)"
154                 doins "${@}"
155         )
156 }
157
158 # @FUNCTION: systemd_newuserunit
159 # @USAGE: <old-name> <new-name>
160 # @DESCRIPTION:
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} "${@}"
165
166         (
167                 insinto "$(_systemd_get_userunitdir)"
168                 newins "${@}"
169         )
170 }
171
172 # @FUNCTION: systemd_install_serviced
173 # @USAGE: <conf-file> [<service.d>]
174 # @DESCRIPTION:
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} "${@}"
181
182         local src=${1}
183         local service=${2}
184
185         [[ ${src} ]] || die "No file specified"
186
187         if [[ ! ${service} ]]; then
188                 [[ ${src} == *.conf ]] || die "Source file needs .conf suffix"
189                 service=${src##*/}
190                 service=${service%.conf}
191         fi
192         # avoid potentially common mistake
193         [[ ${service} == *.d ]] && die "Service must not have .d suffix"
194
195         (
196                 insinto /etc/systemd/system/"${service}".d
197                 newins "${src}" 00gentoo.conf
198         )
199 }
200
201 # @FUNCTION: systemd_dotmpfilesd
202 # @USAGE: <tmpfilesd>...
203 # @DESCRIPTION:
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} "${@}"
208
209         for f; do
210                 [[ ${f} == *.conf ]] \
211                         || die 'tmpfiles.d files need to have .conf suffix.'
212         done
213
214         (
215                 insinto /usr/lib/tmpfiles.d/
216                 doins "${@}"
217         )
218 }
219
220 # @FUNCTION: systemd_newtmpfilesd
221 # @USAGE: <old-name> <new-name>.conf
222 # @DESCRIPTION:
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} "${@}"
227
228         [[ ${2} == *.conf ]] \
229                 || die 'tmpfiles.d files need to have .conf suffix.'
230
231         (
232                 insinto /usr/lib/tmpfiles.d/
233                 newins "${@}"
234         )
235 }
236
237 # @FUNCTION: systemd_enable_service
238 # @USAGE: <target> <service>
239 # @DESCRIPTION:
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
242 # EAPIs.
243 systemd_enable_service() {
244         debug-print-function ${FUNCNAME} "${@}"
245
246         [[ ${#} -eq 2 ]] || die "Synopsis: systemd_enable_service target service"
247
248         local target=${1}
249         local service=${2}
250         local ud=$(_systemd_get_unitdir)
251         local destname=${service##*/}
252
253         dodir "${ud}"/"${target}".wants && \
254         dosym ../"${service}" "${ud}"/"${target}".wants/"${destname}"
255 }
256
257 # @FUNCTION: systemd_enable_ntpunit
258 # @USAGE: <NN-name> <service>...
259 # @DESCRIPTION:
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.
264 #
265 # Uses doins, thus it is fatal in EAPI 4 and non-fatal in earlier
266 # EAPIs.
267 #
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>..."
273         fi
274
275         local ntpunit_name=${1}
276         local services=( "${@:2}" )
277
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."
282         fi
283
284         local unitdir=$(systemd_get_unitdir)
285         local s
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}."
289                 fi
290                 echo "${s}" >> "${T}"/${ntpunit_name}.list
291         done
292
293         (
294                 insinto "$(_systemd_get_utildir)"/ntp-units.d
295                 doins "${T}"/${ntpunit_name}.list
296         )
297         local ret=${?}
298
299         rm "${T}"/${ntpunit_name}.list || die
300
301         return ${ret}
302 }
303
304 # @FUNCTION: systemd_with_unitdir
305 # @USAGE: [<configure-option-name>]
306 # @DESCRIPTION:
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
310 # this function.
311 #
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}
319
320         echo --with-${optname}="$(systemd_get_unitdir)"
321 }
322
323 # @FUNCTION: systemd_with_utildir
324 # @DESCRIPTION:
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} "${@}"
330
331         echo --with-systemdutildir="$(systemd_get_utildir)"
332 }
333
334 # @FUNCTION: systemd_to_myeconfargs
335 # @DESCRIPTION:
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} "${@}"
341
342         eqawarn 'systemd_to_myeconfargs() is deprecated and will be removed on 2013-10-11.'
343         eqawarn 'Please use $(systemd_with_unitdir) instead.'
344
345         myeconfargs=(
346                 "${myeconfargs[@]}"
347                 --with-systemdsystemunitdir="$(systemd_get_unitdir)"
348         )
349 }
350
351 # @FUNCTION: systemd_update_catalog
352 # @DESCRIPTION:
353 # Update the journald catalog. This needs to be called after installing
354 # or removing catalog files.
355 #
356 # If systemd is not installed, no operation will be done. The catalog
357 # will be (re)built once systemd is installed.
358 #
359 # See: http://www.freedesktop.org/wiki/Software/systemd/catalog
360 systemd_update_catalog() {
361         debug-print-function ${FUNCNAME} "${@}"
362
363         # Make sure to work on the correct system.
364
365         local journalctl=${EPREFIX}/usr/bin/journalctl
366         if [[ -x ${journalctl} ]]; then
367                 ebegin "Updating systemd journal catalogs"
368                 journalctl --update-catalog
369                 eend $?
370         else
371                 debug-print "${FUNCNAME}: journalctl not found."
372         fi
373 }
374
375 # @FUNCTION: systemd_is_booted
376 # @DESCRIPTION:
377 # Check whether the system was booted using systemd.
378 #
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!
383 #
384 # Returns 0 if systemd is used to boot the system, 1 otherwise.
385 #
386 # See: man sd_booted
387 systemd_is_booted() {
388         debug-print-function ${FUNCNAME} "${@}"
389
390         [[ -d /run/systemd/system ]]
391         local ret=${?}
392
393         debug-print "${FUNCNAME}: [[ -d /run/systemd/system ]] -> ${ret}"
394         return ${ret}
395 }