proj/gentoo: Initial commit
[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_unitdir
38 # @INTERNAL
39 # @DESCRIPTION:
40 # Get unprefixed unitdir.
41 _systemd_get_unitdir() {
42         if $(tc-getPKG_CONFIG) --exists systemd; then
43                 echo "$($(tc-getPKG_CONFIG) --variable=systemdsystemunitdir systemd)"
44         else
45                 echo /usr/lib/systemd/system
46         fi
47 }
48
49 # @FUNCTION: systemd_get_unitdir
50 # @DESCRIPTION:
51 # Output the path for the systemd unit directory (not including ${D}).
52 # This function always succeeds, even if systemd is not installed.
53 systemd_get_unitdir() {
54         has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX=
55         debug-print-function ${FUNCNAME} "${@}"
56
57         echo "${EPREFIX}$(_systemd_get_unitdir)"
58 }
59
60 # @FUNCTION: _systemd_get_userunitdir
61 # @INTERNAL
62 # @DESCRIPTION:
63 # Get unprefixed userunitdir.
64 _systemd_get_userunitdir() {
65         if $(tc-getPKG_CONFIG) --exists systemd; then
66                 echo "$($(tc-getPKG_CONFIG) --variable=systemduserunitdir systemd)"
67         else
68                 echo /usr/lib/systemd/user
69         fi
70 }
71
72 # @FUNCTION: systemd_get_userunitdir
73 # @DESCRIPTION:
74 # Output the path for the systemd user unit directory (not including
75 # ${D}). This function always succeeds, even if systemd is not
76 # installed.
77 systemd_get_userunitdir() {
78         has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX=
79         debug-print-function ${FUNCNAME} "${@}"
80
81         echo "${EPREFIX}$(_systemd_get_userunitdir)"
82 }
83
84 # @FUNCTION: _systemd_get_utildir
85 # @INTERNAL
86 # @DESCRIPTION:
87 # Get unprefixed utildir.
88 _systemd_get_utildir() {
89         if $(tc-getPKG_CONFIG) --exists systemd; then
90                 echo "$($(tc-getPKG_CONFIG) --variable=systemdutildir systemd)"
91         else
92                 echo /usr/lib/systemd
93         fi
94 }
95
96 # @FUNCTION: systemd_get_utildir
97 # @DESCRIPTION:
98 # Output the path for the systemd utility directory (not including
99 # ${D}). This function always succeeds, even if systemd is not
100 # installed.
101 systemd_get_utildir() {
102         has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX=
103         debug-print-function ${FUNCNAME} "${@}"
104
105         echo "${EPREFIX}$(_systemd_get_utildir)"
106 }
107
108 # @FUNCTION: systemd_dounit
109 # @USAGE: <unit>...
110 # @DESCRIPTION:
111 # Install systemd unit(s). Uses doins, thus it is fatal in EAPI 4
112 # and non-fatal in earlier EAPIs.
113 systemd_dounit() {
114         debug-print-function ${FUNCNAME} "${@}"
115
116         (
117                 insinto "$(_systemd_get_unitdir)"
118                 doins "${@}"
119         )
120 }
121
122 # @FUNCTION: systemd_newunit
123 # @USAGE: <old-name> <new-name>
124 # @DESCRIPTION:
125 # Install systemd unit with a new name. Uses newins, thus it is fatal
126 # in EAPI 4 and non-fatal in earlier EAPIs.
127 systemd_newunit() {
128         debug-print-function ${FUNCNAME} "${@}"
129
130         (
131                 insinto "$(_systemd_get_unitdir)"
132                 newins "${@}"
133         )
134 }
135
136 # @FUNCTION: systemd_douserunit
137 # @USAGE: <unit>...
138 # @DESCRIPTION:
139 # Install systemd user unit(s). Uses doins, thus it is fatal in EAPI 4
140 # and non-fatal in earlier EAPIs.
141 systemd_douserunit() {
142         debug-print-function ${FUNCNAME} "${@}"
143
144         (
145                 insinto "$(_systemd_get_userunitdir)"
146                 doins "${@}"
147         )
148 }
149
150 # @FUNCTION: systemd_newuserunit
151 # @USAGE: <old-name> <new-name>
152 # @DESCRIPTION:
153 # Install systemd user unit with a new name. Uses newins, thus it
154 # is fatal in EAPI 4 and non-fatal in earlier EAPIs.
155 systemd_newuserunit() {
156         debug-print-function ${FUNCNAME} "${@}"
157
158         (
159                 insinto "$(_systemd_get_userunitdir)"
160                 newins "${@}"
161         )
162 }
163
164 # @FUNCTION: systemd_install_serviced
165 # @USAGE: <conf-file> [<service.d>]
166 # @DESCRIPTION:
167 # Install the file <conf-file> as service.d/00gentoo.conf template.
168 # The <service.d> argument specifies the configured service name.
169 # If not specified, the configuration file name will be used with .conf
170 # suffix stripped (e.g. foo.service.conf -> foo.service).
171 systemd_install_serviced() {
172         debug-print-function ${FUNCNAME} "${@}"
173
174         local src=${1}
175         local service=${2}
176
177         [[ ${src} ]] || die "No file specified"
178
179         if [[ ! ${service} ]]; then
180                 [[ ${src} == *.conf ]] || die "Source file needs .conf suffix"
181                 service=${src##*/}
182                 service=${service%.conf}
183         fi
184         # avoid potentially common mistake
185         [[ ${service} == *.d ]] && die "Service must not have .d suffix"
186
187         (
188                 insinto /etc/systemd/system/"${service}".d
189                 newins "${src}" 00gentoo.conf
190         )
191 }
192
193 # @FUNCTION: systemd_dotmpfilesd
194 # @USAGE: <tmpfilesd>...
195 # @DESCRIPTION:
196 # Install systemd tmpfiles.d files. Uses doins, thus it is fatal
197 # in EAPI 4 and non-fatal in earlier EAPIs.
198 systemd_dotmpfilesd() {
199         debug-print-function ${FUNCNAME} "${@}"
200
201         for f; do
202                 [[ ${f} == *.conf ]] \
203                         || die 'tmpfiles.d files need to have .conf suffix.'
204         done
205
206         (
207                 insinto /usr/lib/tmpfiles.d/
208                 doins "${@}"
209         )
210 }
211
212 # @FUNCTION: systemd_newtmpfilesd
213 # @USAGE: <old-name> <new-name>.conf
214 # @DESCRIPTION:
215 # Install systemd tmpfiles.d file under a new name. Uses newins, thus it
216 # is fatal in EAPI 4 and non-fatal in earlier EAPIs.
217 systemd_newtmpfilesd() {
218         debug-print-function ${FUNCNAME} "${@}"
219
220         [[ ${2} == *.conf ]] \
221                 || die 'tmpfiles.d files need to have .conf suffix.'
222
223         (
224                 insinto /usr/lib/tmpfiles.d/
225                 newins "${@}"
226         )
227 }
228
229 # @FUNCTION: systemd_enable_service
230 # @USAGE: <target> <service>
231 # @DESCRIPTION:
232 # Enable service in desired target, e.g. install a symlink for it.
233 # Uses dosym, thus it is fatal in EAPI 4 and non-fatal in earlier
234 # EAPIs.
235 systemd_enable_service() {
236         debug-print-function ${FUNCNAME} "${@}"
237
238         [[ ${#} -eq 2 ]] || die "Synopsis: systemd_enable_service target service"
239
240         local target=${1}
241         local service=${2}
242         local ud=$(_systemd_get_unitdir)
243         local destname=${service##*/}
244
245         dodir "${ud}"/"${target}".wants && \
246         dosym ../"${service}" "${ud}"/"${target}".wants/"${destname}"
247 }
248
249 # @FUNCTION: systemd_enable_ntpunit
250 # @USAGE: <NN-name> <service>...
251 # @DESCRIPTION:
252 # Add an NTP service provider to the list of implementations
253 # in timedated. <NN-name> defines the newly-created ntp-units.d priority
254 # and name, while the remaining arguments list service units that will
255 # be added to that file.
256 #
257 # Uses doins, thus it is fatal in EAPI 4 and non-fatal in earlier
258 # EAPIs.
259 #
260 # Doc: http://www.freedesktop.org/wiki/Software/systemd/timedated/
261 systemd_enable_ntpunit() {
262         debug-print-function ${FUNCNAME} "${@}"
263         if [[ ${#} -lt 2 ]]; then
264                 die "Usage: systemd_enable_ntpunit <NN-name> <service>..."
265         fi
266
267         local ntpunit_name=${1}
268         local services=( "${@:2}" )
269
270         if [[ ${ntpunit_name} != [0-9][0-9]-* ]]; then
271                 die "ntpunit.d file must be named NN-name where NN are digits."
272         elif [[ ${ntpunit_name} == *.list ]]; then
273                 die "The .list suffix is appended implicitly to ntpunit.d name."
274         fi
275
276         local unitdir=$(systemd_get_unitdir)
277         local s
278         for s in "${services[@]}"; do
279                 if [[ ! -f "${D}${unitdir}/${s}" ]]; then
280                         die "ntp-units.d provider ${s} not installed (yet?) in \${D}."
281                 fi
282                 echo "${s}" >> "${T}"/${ntpunit_name}.list
283         done
284
285         (
286                 insinto "$(_systemd_get_utildir)"/ntp-units.d
287                 doins "${T}"/${ntpunit_name}.list
288         )
289         local ret=${?}
290
291         rm "${T}"/${ntpunit_name}.list || die
292
293         return ${ret}
294 }
295
296 # @FUNCTION: systemd_with_unitdir
297 # @USAGE: [<configure-option-name>]
298 # @DESCRIPTION:
299 # Output '--with-systemdsystemunitdir' as expected by systemd-aware configure
300 # scripts. This function always succeeds. Its output may be quoted in order
301 # to preserve whitespace in paths. systemd_to_myeconfargs() is preferred over
302 # this function.
303 #
304 # If upstream does use invalid configure option to handle installing systemd
305 # units (e.g. `--with-systemdunitdir'), you can pass the 'suffix' as an optional
306 # argument to this function (`$(systemd_with_unitdir systemdunitdir)'). Please
307 # remember to report a bug upstream as well.
308 systemd_with_unitdir() {
309         debug-print-function ${FUNCNAME} "${@}"
310         local optname=${1:-systemdsystemunitdir}
311
312         echo --with-${optname}="$(systemd_get_unitdir)"
313 }
314
315 # @FUNCTION: systemd_with_utildir
316 # @DESCRIPTION:
317 # Output '--with-systemdsystemutildir' as used by some packages to install
318 # systemd helpers. This function always succeeds. Its output may be quoted
319 # in order to preserve whitespace in paths.
320 systemd_with_utildir() {
321         debug-print-function ${FUNCNAME} "${@}"
322
323         echo --with-systemdutildir="$(systemd_get_utildir)"
324 }
325
326 # @FUNCTION: systemd_to_myeconfargs
327 # @DESCRIPTION:
328 # Add '--with-systemdsystemunitdir' as expected by systemd-aware configure
329 # scripts to the myeconfargs variable used by autotools-utils eclass. Handles
330 # quoting automatically.
331 systemd_to_myeconfargs() {
332         debug-print-function ${FUNCNAME} "${@}"
333
334         eqawarn 'systemd_to_myeconfargs() is deprecated and will be removed on 2013-10-11.'
335         eqawarn 'Please use $(systemd_with_unitdir) instead.'
336
337         myeconfargs=(
338                 "${myeconfargs[@]}"
339                 --with-systemdsystemunitdir="$(systemd_get_unitdir)"
340         )
341 }
342
343 # @FUNCTION: systemd_update_catalog
344 # @DESCRIPTION:
345 # Update the journald catalog. This needs to be called after installing
346 # or removing catalog files.
347 #
348 # If systemd is not installed, no operation will be done. The catalog
349 # will be (re)built once systemd is installed.
350 #
351 # See: http://www.freedesktop.org/wiki/Software/systemd/catalog
352 systemd_update_catalog() {
353         debug-print-function ${FUNCNAME} "${@}"
354
355         # Make sure to work on the correct system.
356
357         local journalctl=${EPREFIX}/usr/bin/journalctl
358         if [[ -x ${journalctl} ]]; then
359                 ebegin "Updating systemd journal catalogs"
360                 journalctl --update-catalog
361                 eend $?
362         else
363                 debug-print "${FUNCNAME}: journalctl not found."
364         fi
365 }
366
367 # @FUNCTION: systemd_is_booted
368 # @DESCRIPTION:
369 # Check whether the system was booted using systemd.
370 #
371 # This should be used purely for informational purposes, e.g. warning
372 # user that he needs to use systemd. Installed files or application
373 # behavior *must not* rely on this. Please remember to check MERGE_TYPE
374 # to not trigger the check on binary package build hosts!
375 #
376 # Returns 0 if systemd is used to boot the system, 1 otherwise.
377 #
378 # See: man sd_booted
379 systemd_is_booted() {
380         debug-print-function ${FUNCNAME} "${@}"
381
382         [[ -d /run/systemd/system ]]
383         local ret=${?}
384
385         debug-print "${FUNCNAME}: [[ -d /run/systemd/system ]] -> ${ret}"
386         return ${ret}
387 }