blob: 0a3a6086629a9acb1b2145db0cb1519a58f79e4f [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001UPDATERCPN ?= "${PN}"
2
Patrick Williams213cb262021-08-07 19:21:33 -05003DEPENDS:append:class-target = "${@bb.utils.contains('DISTRO_FEATURES', 'sysvinit', ' update-rc.d initscripts', '', d)}"
Patrick Williamsc0f7c042017-02-23 20:41:17 -06004
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005UPDATERCD = "update-rc.d"
Patrick Williams213cb262021-08-07 19:21:33 -05006UPDATERCD:class-cross = ""
7UPDATERCD:class-native = ""
8UPDATERCD:class-nativesdk = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009
10INITSCRIPT_PARAMS ?= "defaults"
11
12INIT_D_DIR = "${sysconfdir}/init.d"
13
Brad Bishop6e60e8b2018-02-01 10:27:11 -050014def use_updatercd(d):
15 # If the distro supports both sysvinit and systemd, and the current recipe
16 # supports systemd, only call update-rc.d on rootfs creation or if systemd
17 # is not running. That's because systemctl enable/disable will already call
18 # update-rc.d if it detects initscripts.
19 if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d) and bb.data.inherits_class('systemd', d):
20 return '[ -n "$D" -o ! -d /run/systemd/system ]'
21 return 'true'
22
Brad Bishop6e60e8b2018-02-01 10:27:11 -050023PACKAGE_WRITE_DEPS += "update-rc.d-native"
24
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025updatercd_postinst() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050026if ${@use_updatercd(d)} && type update-rc.d >/dev/null 2>/dev/null; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027 if [ -n "$D" ]; then
Brad Bishopc8f47122019-06-24 09:36:18 -040028 OPT="-r $D"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050029 else
Brad Bishopc8f47122019-06-24 09:36:18 -040030 OPT="-s"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050031 fi
32 update-rc.d $OPT ${INITSCRIPT_NAME} ${INITSCRIPT_PARAMS}
33fi
34}
35
36updatercd_prerm() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050037if ${@use_updatercd(d)} && [ -z "$D" -a -x "${INIT_D_DIR}/${INITSCRIPT_NAME}" ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -060038 ${INIT_D_DIR}/${INITSCRIPT_NAME} stop || :
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039fi
40}
41
42updatercd_postrm() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050043if ${@use_updatercd(d)} && type update-rc.d >/dev/null 2>/dev/null; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044 if [ -n "$D" ]; then
45 OPT="-f -r $D"
46 else
47 OPT="-f"
48 fi
49 update-rc.d $OPT ${INITSCRIPT_NAME} remove
50fi
51}
52
53
54def update_rc_after_parse(d):
55 if d.getVar('INITSCRIPT_PACKAGES', False) == None:
56 if d.getVar('INITSCRIPT_NAME', False) == None:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060057 bb.fatal("%s inherits update-rc.d but doesn't set INITSCRIPT_NAME" % d.getVar('FILE', False))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058 if d.getVar('INITSCRIPT_PARAMS', False) == None:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060059 bb.fatal("%s inherits update-rc.d but doesn't set INITSCRIPT_PARAMS" % d.getVar('FILE', False))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050060
61python __anonymous() {
62 update_rc_after_parse(d)
63}
64
Patrick Williams213cb262021-08-07 19:21:33 -050065PACKAGESPLITFUNCS:prepend = "${@bb.utils.contains('DISTRO_FEATURES', 'sysvinit', 'populate_packages_updatercd ', '', d)}"
66PACKAGESPLITFUNCS:remove:class-nativesdk = "populate_packages_updatercd "
Patrick Williamsc124f4f2015-09-15 14:41:29 -050067
Brad Bishopc8f47122019-06-24 09:36:18 -040068populate_packages_updatercd[vardeps] += "updatercd_prerm updatercd_postrm updatercd_postinst"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050069populate_packages_updatercd[vardepsexclude] += "OVERRIDES"
70
71python populate_packages_updatercd () {
72 def update_rcd_auto_depend(pkg):
73 import subprocess
74 import os
75 path = d.expand("${D}${INIT_D_DIR}/${INITSCRIPT_NAME}")
76 if not os.path.exists(path):
77 return
78 statement = "grep -q -w '/etc/init.d/functions' %s" % path
79 if subprocess.call(statement, shell=True) == 0:
Brad Bishop00111322018-04-01 22:23:53 -040080 mlprefix = d.getVar('MLPREFIX') or ""
Patrick Williams213cb262021-08-07 19:21:33 -050081 d.appendVar('RDEPENDS:' + pkg, ' %sinitd-functions' % (mlprefix))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082
83 def update_rcd_package(pkg):
Brad Bishopc8f47122019-06-24 09:36:18 -040084 bb.debug(1, 'adding update-rc.d calls to postinst/prerm/postrm for %s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085
86 localdata = bb.data.createCopy(d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050087 overrides = localdata.getVar("OVERRIDES")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050088 localdata.setVar("OVERRIDES", "%s:%s" % (pkg, overrides))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089
90 update_rcd_auto_depend(pkg)
91
Patrick Williams213cb262021-08-07 19:21:33 -050092 postinst = d.getVar('pkg_postinst:%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050093 if not postinst:
94 postinst = '#!/bin/sh\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -050095 postinst += localdata.getVar('updatercd_postinst')
Patrick Williams213cb262021-08-07 19:21:33 -050096 d.setVar('pkg_postinst:%s' % pkg, postinst)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097
Patrick Williams213cb262021-08-07 19:21:33 -050098 prerm = d.getVar('pkg_prerm:%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050099 if not prerm:
100 prerm = '#!/bin/sh\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500101 prerm += localdata.getVar('updatercd_prerm')
Patrick Williams213cb262021-08-07 19:21:33 -0500102 d.setVar('pkg_prerm:%s' % pkg, prerm)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103
Patrick Williams213cb262021-08-07 19:21:33 -0500104 postrm = d.getVar('pkg_postrm:%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105 if not postrm:
106 postrm = '#!/bin/sh\n'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500107 postrm += localdata.getVar('updatercd_postrm')
Patrick Williams213cb262021-08-07 19:21:33 -0500108 d.setVar('pkg_postrm:%s' % pkg, postrm)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109
Patrick Williams213cb262021-08-07 19:21:33 -0500110 d.appendVar('RRECOMMENDS:' + pkg, " ${MLPREFIX}${UPDATERCD}")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111
112 # Check that this class isn't being inhibited (generally, by
113 # systemd.bbclass) before doing any work.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500114 if not d.getVar("INHIBIT_UPDATERCD_BBCLASS"):
115 pkgs = d.getVar('INITSCRIPT_PACKAGES')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116 if pkgs == None:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500117 pkgs = d.getVar('UPDATERCPN')
118 packages = (d.getVar('PACKAGES') or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500119 if not pkgs in packages and packages != []:
120 pkgs = packages[0]
121 for pkg in pkgs.split():
122 update_rcd_package(pkg)
123}