xref: /openbmc/openbmc/poky/meta/classes-recipe/update-rc.d.bbclass (revision 517393d903089f921915530ffa689a7a1d113420)
1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7UPDATERCPN ?= "${PN}"
8
9DEPENDS:append:class-target = "${@bb.utils.contains('DISTRO_FEATURES', 'sysvinit', ' update-rc.d initscripts', '', d)}"
10
11UPDATERCD = "update-rc.d"
12UPDATERCD:class-cross = ""
13UPDATERCD:class-native = ""
14UPDATERCD:class-nativesdk = ""
15
16INITSCRIPT_PARAMS ?= "defaults"
17
18INIT_D_DIR = "${sysconfdir}/init.d"
19
20def use_updatercd(d):
21    # If the distro supports both sysvinit and systemd, and the current recipe
22    # supports systemd, only call update-rc.d on rootfs creation or if systemd
23    # is not running. That's because systemctl enable/disable will already call
24    # update-rc.d if it detects initscripts.
25    if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d) and bb.data.inherits_class('systemd', d):
26        return '[ -n "$D" -o ! -d /run/systemd/system ]'
27    return 'true'
28
29PACKAGE_WRITE_DEPS += "update-rc.d-native"
30
31updatercd_postinst() {
32if ${@use_updatercd(d)} && type update-rc.d >/dev/null 2>/dev/null; then
33	if [ -n "$D" ]; then
34		OPT="-r $D"
35	else
36		OPT="-s"
37	fi
38	update-rc.d $OPT ${INITSCRIPT_NAME} ${INITSCRIPT_PARAMS}
39fi
40}
41
42updatercd_prerm() {
43if ${@use_updatercd(d)} && [ -z "$D" -a -x "${INIT_D_DIR}/${INITSCRIPT_NAME}" ]; then
44	${INIT_D_DIR}/${INITSCRIPT_NAME} stop || :
45fi
46}
47
48updatercd_postrm() {
49if ${@use_updatercd(d)} && type update-rc.d >/dev/null 2>/dev/null; then
50	if [ -n "$D" ]; then
51		OPT="-f -r $D"
52	else
53		OPT="-f"
54	fi
55	update-rc.d $OPT ${INITSCRIPT_NAME} remove
56fi
57}
58
59
60def update_rc_after_parse(d):
61    if d.getVar('INITSCRIPT_PACKAGES', False) == None:
62        if d.getVar('INITSCRIPT_NAME', False) == None:
63            bb.fatal("%s inherits update-rc.d but doesn't set INITSCRIPT_NAME" % d.getVar('FILE', False))
64        if d.getVar('INITSCRIPT_PARAMS', False) == None:
65            bb.fatal("%s inherits update-rc.d but doesn't set INITSCRIPT_PARAMS" % d.getVar('FILE', False))
66
67python __anonymous() {
68    update_rc_after_parse(d)
69}
70
71PACKAGESPLITFUNCS =+ "${@bb.utils.contains('DISTRO_FEATURES', 'sysvinit', 'populate_packages_updatercd', '', d)}"
72PACKAGESPLITFUNCS:remove:class-nativesdk = "populate_packages_updatercd"
73
74populate_packages_updatercd[vardeps] += "updatercd_prerm updatercd_postrm updatercd_postinst"
75populate_packages_updatercd[vardepsexclude] += "OVERRIDES"
76
77python populate_packages_updatercd () {
78    def update_rcd_auto_depend(pkg):
79        import subprocess
80        import os
81        path = d.expand("${D}${INIT_D_DIR}/${INITSCRIPT_NAME}")
82        if not os.path.exists(path):
83            return
84        statement = "grep -q -w '/etc/init.d/functions' %s" % path
85        if subprocess.call(statement, shell=True) == 0:
86            mlprefix = d.getVar('MLPREFIX') or ""
87            d.appendVar('RDEPENDS:' + pkg, ' %sinitd-functions' % (mlprefix))
88
89    def update_rcd_package(pkg):
90        bb.debug(1, 'adding update-rc.d calls to postinst/prerm/postrm for %s' % pkg)
91
92        localdata = bb.data.createCopy(d)
93        overrides = localdata.getVar("OVERRIDES")
94        localdata.setVar("OVERRIDES", "%s:%s" % (pkg, overrides))
95
96        update_rcd_auto_depend(pkg)
97
98        postinst = d.getVar('pkg_postinst:%s' % pkg)
99        if not postinst:
100            postinst = '#!/bin/sh\n'
101        postinst += localdata.getVar('updatercd_postinst')
102        d.setVar('pkg_postinst:%s' % pkg, postinst)
103
104        prerm = d.getVar('pkg_prerm:%s' % pkg)
105        if not prerm:
106            prerm = '#!/bin/sh\n'
107        prerm += localdata.getVar('updatercd_prerm')
108        d.setVar('pkg_prerm:%s' % pkg, prerm)
109
110        postrm = d.getVar('pkg_postrm:%s' % pkg)
111        if not postrm:
112                postrm = '#!/bin/sh\n'
113        postrm += localdata.getVar('updatercd_postrm')
114        d.setVar('pkg_postrm:%s' % pkg, postrm)
115
116        d.appendVar('RRECOMMENDS:' + pkg, " ${MLPREFIX}${UPDATERCD}")
117
118    # Check that this class isn't being inhibited (generally, by
119    # systemd.bbclass) before doing any work.
120    if not d.getVar("INHIBIT_UPDATERCD_BBCLASS"):
121        pkgs = d.getVar('INITSCRIPT_PACKAGES')
122        if pkgs == None:
123            pkgs = d.getVar('UPDATERCPN')
124            packages = (d.getVar('PACKAGES') or "").split()
125            if not pkgs in packages and packages != []:
126                pkgs = packages[0]
127        for pkg in pkgs.split():
128            update_rcd_package(pkg)
129}
130