/[gentoo-x86]/eclass/systemd.eclass
Gentoo

Diff of /eclass/systemd.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.31

  ViewVC Help
Powered by ViewVC 1.1.20