blob: 9ba3daccaf35834867c4196c0698acde4d9298c4 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001UPDATERCPN ?= "${PN}"
2
Brad Bishop6e60e8b2018-02-01 10:27:11 -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"
6UPDATERCD_class-cross = ""
7UPDATERCD_class-native = ""
8UPDATERCD_class-nativesdk = ""
9
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
Patrick Williamsc124f4f2015-09-15 14:41:29 -050023updatercd_preinst() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050024if ${@use_updatercd(d)} && [ -z "$D" -a -f "${INIT_D_DIR}/${INITSCRIPT_NAME}" ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -060025 ${INIT_D_DIR}/${INITSCRIPT_NAME} stop || :
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -050027if ${@use_updatercd(d)} && type update-rc.d >/dev/null 2>/dev/null; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028 if [ -n "$D" ]; then
29 OPT="-f -r $D"
30 else
31 OPT="-f"
32 fi
33 update-rc.d $OPT ${INITSCRIPT_NAME} remove
34fi
35}
36
Brad Bishop6e60e8b2018-02-01 10:27:11 -050037PACKAGE_WRITE_DEPS += "update-rc.d-native"
38
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039updatercd_postinst() {
Brad Bishop37a0e4d2017-12-04 01:01:44 -050040# Begin section update-rc.d
Brad Bishop6e60e8b2018-02-01 10:27:11 -050041if ${@use_updatercd(d)} && type update-rc.d >/dev/null 2>/dev/null; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050042 if [ -n "$D" ]; then
43 OPT="-r $D"
44 else
45 OPT="-s"
46 fi
47 update-rc.d $OPT ${INITSCRIPT_NAME} ${INITSCRIPT_PARAMS}
48fi
Brad Bishop37a0e4d2017-12-04 01:01:44 -050049# End section update-rc.d
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050}
51
52updatercd_prerm() {
Brad Bishop37a0e4d2017-12-04 01:01:44 -050053# Begin section update-rc.d
Brad Bishop6e60e8b2018-02-01 10:27:11 -050054if ${@use_updatercd(d)} && [ -z "$D" -a -x "${INIT_D_DIR}/${INITSCRIPT_NAME}" ]; then
Patrick Williamsc0f7c042017-02-23 20:41:17 -060055 ${INIT_D_DIR}/${INITSCRIPT_NAME} stop || :
Patrick Williamsc124f4f2015-09-15 14:41:29 -050056fi
Brad Bishop37a0e4d2017-12-04 01:01:44 -050057# End section update-rc.d
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058}
59
60updatercd_postrm() {
Brad Bishop6e60e8b2018-02-01 10:27:11 -050061if ${@use_updatercd(d)} && type update-rc.d >/dev/null 2>/dev/null; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050062 if [ -n "$D" ]; then
63 OPT="-f -r $D"
64 else
65 OPT="-f"
66 fi
67 update-rc.d $OPT ${INITSCRIPT_NAME} remove
68fi
69}
70
71
72def update_rc_after_parse(d):
73 if d.getVar('INITSCRIPT_PACKAGES', False) == None:
74 if d.getVar('INITSCRIPT_NAME', False) == None:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060075 bb.fatal("%s inherits update-rc.d but doesn't set INITSCRIPT_NAME" % d.getVar('FILE', False))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 if d.getVar('INITSCRIPT_PARAMS', False) == None:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060077 bb.fatal("%s inherits update-rc.d but doesn't set INITSCRIPT_PARAMS" % d.getVar('FILE', False))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078
79python __anonymous() {
80 update_rc_after_parse(d)
81}
82
Patrick Williamsc0f7c042017-02-23 20:41:17 -060083PACKAGESPLITFUNCS_prepend = "${@bb.utils.contains('DISTRO_FEATURES', 'sysvinit', 'populate_packages_updatercd ', '', d)}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050084PACKAGESPLITFUNCS_remove_class-nativesdk = "populate_packages_updatercd "
85
86populate_packages_updatercd[vardeps] += "updatercd_prerm updatercd_postrm updatercd_preinst updatercd_postinst"
87populate_packages_updatercd[vardepsexclude] += "OVERRIDES"
88
89python populate_packages_updatercd () {
90 def update_rcd_auto_depend(pkg):
91 import subprocess
92 import os
93 path = d.expand("${D}${INIT_D_DIR}/${INITSCRIPT_NAME}")
94 if not os.path.exists(path):
95 return
96 statement = "grep -q -w '/etc/init.d/functions' %s" % path
97 if subprocess.call(statement, shell=True) == 0:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050098 mlprefix = d.getVar('MLPREFIX') or ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050099 d.appendVar('RDEPENDS_' + pkg, ' %sinitscripts-functions' % (mlprefix))
100
101 def update_rcd_package(pkg):
102 bb.debug(1, 'adding update-rc.d calls to preinst/postinst/prerm/postrm for %s' % pkg)
103
104 localdata = bb.data.createCopy(d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500105 overrides = localdata.getVar("OVERRIDES")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106 localdata.setVar("OVERRIDES", "%s:%s" % (pkg, overrides))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107
108 update_rcd_auto_depend(pkg)
109
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500110 preinst = d.getVar('pkg_preinst_%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111 if not preinst:
112 preinst = '#!/bin/sh\n'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500113 preinst += localdata.getVar('updatercd_preinst')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114 d.setVar('pkg_preinst_%s' % pkg, preinst)
115
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500116 postinst = d.getVar('pkg_postinst_%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500117 if not postinst:
118 postinst = '#!/bin/sh\n'
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500119 postinst = postinst.splitlines(True)
120 try:
121 index = postinst.index('# End section update-alternatives\n')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500122 postinst.insert(index + 1, localdata.getVar('updatercd_postinst'))
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500123 except ValueError:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500124 postinst.append(localdata.getVar('updatercd_postinst'))
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500125 postinst = ''.join(postinst)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500126 d.setVar('pkg_postinst_%s' % pkg, postinst)
127
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500128 prerm = d.getVar('pkg_prerm_%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500129 if not prerm:
130 prerm = '#!/bin/sh\n'
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500131 prerm = prerm.splitlines(True)
132 try:
133 index = prerm.index('# Begin section update-alternatives\n')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500134 prerm.insert(index, localdata.getVar('updatercd_prerm'))
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500135 except ValueError:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500136 prerm.append(localdata.getVar('updatercd_prerm'))
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500137 prerm = ''.join(prerm)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500138 d.setVar('pkg_prerm_%s' % pkg, prerm)
139
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500140 postrm = d.getVar('pkg_postrm_%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500141 if not postrm:
142 postrm = '#!/bin/sh\n'
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500143 postrm += localdata.getVar('updatercd_postrm')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500144 d.setVar('pkg_postrm_%s' % pkg, postrm)
145
146 d.appendVar('RRECOMMENDS_' + pkg, " ${MLPREFIX}${UPDATERCD}")
147
148 # Check that this class isn't being inhibited (generally, by
149 # systemd.bbclass) before doing any work.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500150 if not d.getVar("INHIBIT_UPDATERCD_BBCLASS"):
151 pkgs = d.getVar('INITSCRIPT_PACKAGES')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500152 if pkgs == None:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500153 pkgs = d.getVar('UPDATERCPN')
154 packages = (d.getVar('PACKAGES') or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500155 if not pkgs in packages and packages != []:
156 pkgs = packages[0]
157 for pkg in pkgs.split():
158 update_rcd_package(pkg)
159}