Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 2 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 3 | # Copyright BitBake Contributors |
| 4 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 5 | # SPDX-License-Identifier: GPL-2.0-only |
| 6 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | |
| 8 | import os |
| 9 | import sys |
| 10 | import warnings |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 11 | warnings.simplefilter("default") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 12 | sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(sys.argv[0])), 'lib')) |
| 13 | from bb import fetch2 |
| 14 | import logging |
| 15 | import bb |
| 16 | import select |
| 17 | import errno |
| 18 | import signal |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 19 | import pickle |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 20 | import traceback |
| 21 | import queue |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 22 | import shlex |
| 23 | import subprocess |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 24 | from multiprocessing import Lock |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 25 | from threading import Thread |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 27 | bb.utils.check_system_locale() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 28 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 29 | # Users shouldn't be running this code directly |
| 30 | if len(sys.argv) != 2 or not sys.argv[1].startswith("decafbad"): |
| 31 | print("bitbake-worker is meant for internal execution by bitbake itself, please don't use it standalone.") |
| 32 | sys.exit(1) |
| 33 | |
| 34 | profiling = False |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 35 | if sys.argv[1].startswith("decafbadbad"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 36 | profiling = True |
| 37 | try: |
| 38 | import cProfile as profile |
| 39 | except: |
| 40 | import profile |
| 41 | |
| 42 | # Unbuffer stdout to avoid log truncation in the event |
| 43 | # of an unorderly exit as well as to provide timely |
| 44 | # updates to log files for use with tail |
| 45 | try: |
| 46 | if sys.stdout.name == '<stdout>': |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 47 | import fcntl |
| 48 | fl = fcntl.fcntl(sys.stdout.fileno(), fcntl.F_GETFL) |
| 49 | fl |= os.O_SYNC |
| 50 | fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, fl) |
| 51 | #sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | except: |
| 53 | pass |
| 54 | |
| 55 | logger = logging.getLogger("BitBake") |
| 56 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | worker_pipe = sys.stdout.fileno() |
| 58 | bb.utils.nonblockingfd(worker_pipe) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 59 | # Need to guard against multiprocessing being used in child processes |
| 60 | # and multiple processes trying to write to the parent at the same time |
| 61 | worker_pipe_lock = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 62 | |
| 63 | handler = bb.event.LogHandler() |
| 64 | logger.addHandler(handler) |
| 65 | |
| 66 | if 0: |
| 67 | # Code to write out a log file of all events passing through the worker |
| 68 | logfilename = "/tmp/workerlogfile" |
| 69 | format_str = "%(levelname)s: %(message)s" |
| 70 | conlogformat = bb.msg.BBLogFormatter(format_str) |
| 71 | consolelog = logging.FileHandler(logfilename) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 72 | consolelog.setFormatter(conlogformat) |
| 73 | logger.addHandler(consolelog) |
| 74 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 75 | worker_queue = queue.Queue() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 76 | |
| 77 | def worker_fire(event, d): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 78 | data = b"<event>" + pickle.dumps(event) + b"</event>" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 79 | worker_fire_prepickled(data) |
| 80 | |
| 81 | def worker_fire_prepickled(event): |
| 82 | global worker_queue |
| 83 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 84 | worker_queue.put(event) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 85 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 86 | # |
| 87 | # We can end up with write contention with the cooker, it can be trying to send commands |
| 88 | # and we can be trying to send event data back. Therefore use a separate thread for writing |
| 89 | # back data to cooker. |
| 90 | # |
| 91 | worker_thread_exit = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 92 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 93 | def worker_flush(worker_queue): |
Andrew Geissler | 220dafd | 2023-10-04 10:18:08 -0500 | [diff] [blame] | 94 | worker_queue_int = bytearray() |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 95 | global worker_pipe, worker_thread_exit |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 96 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 97 | while True: |
| 98 | try: |
Andrew Geissler | 220dafd | 2023-10-04 10:18:08 -0500 | [diff] [blame] | 99 | worker_queue_int.extend(worker_queue.get(True, 1)) |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 100 | except queue.Empty: |
| 101 | pass |
| 102 | while (worker_queue_int or not worker_queue.empty()): |
| 103 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 104 | (_, ready, _) = select.select([], [worker_pipe], [], 1) |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 105 | if not worker_queue.empty(): |
Andrew Geissler | 220dafd | 2023-10-04 10:18:08 -0500 | [diff] [blame] | 106 | worker_queue_int.extend(worker_queue.get()) |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 107 | written = os.write(worker_pipe, worker_queue_int) |
| 108 | worker_queue_int = worker_queue_int[written:] |
| 109 | except (IOError, OSError) as e: |
| 110 | if e.errno != errno.EAGAIN and e.errno != errno.EPIPE: |
| 111 | raise |
| 112 | if worker_thread_exit and worker_queue.empty() and not worker_queue_int: |
| 113 | return |
| 114 | |
| 115 | worker_thread = Thread(target=worker_flush, args=(worker_queue,)) |
| 116 | worker_thread.start() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 117 | |
| 118 | def worker_child_fire(event, d): |
| 119 | global worker_pipe |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 120 | global worker_pipe_lock |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 121 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 122 | data = b"<event>" + pickle.dumps(event) + b"</event>" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 123 | try: |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 124 | with bb.utils.lock_timeout(worker_pipe_lock): |
| 125 | while(len(data)): |
| 126 | written = worker_pipe.write(data) |
| 127 | data = data[written:] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 128 | except IOError: |
| 129 | sigterm_handler(None, None) |
| 130 | raise |
| 131 | |
| 132 | bb.event.worker_fire = worker_fire |
| 133 | |
| 134 | lf = None |
| 135 | #lf = open("/tmp/workercommandlog", "w+") |
| 136 | def workerlog_write(msg): |
| 137 | if lf: |
| 138 | lf.write(msg) |
| 139 | lf.flush() |
| 140 | |
| 141 | def sigterm_handler(signum, frame): |
| 142 | signal.signal(signal.SIGTERM, signal.SIG_DFL) |
| 143 | os.killpg(0, signal.SIGTERM) |
| 144 | sys.exit() |
| 145 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 146 | def fork_off_task(cfg, data, databuilder, workerdata, extraconfigdata, runtask): |
| 147 | |
| 148 | fn = runtask['fn'] |
| 149 | task = runtask['task'] |
| 150 | taskname = runtask['taskname'] |
| 151 | taskhash = runtask['taskhash'] |
| 152 | unihash = runtask['unihash'] |
| 153 | appends = runtask['appends'] |
Patrick Williams | e760df8 | 2023-05-26 11:10:49 -0500 | [diff] [blame] | 154 | layername = runtask['layername'] |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 155 | taskdepdata = runtask['taskdepdata'] |
| 156 | quieterrors = runtask['quieterrors'] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 157 | # We need to setup the environment BEFORE the fork, since |
| 158 | # a fork() or exec*() activates PSEUDO... |
| 159 | |
| 160 | envbackup = {} |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 161 | fakeroot = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 162 | fakeenv = {} |
| 163 | umask = None |
| 164 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 165 | uid = os.getuid() |
| 166 | gid = os.getgid() |
| 167 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 168 | taskdep = runtask['taskdep'] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 169 | if 'umask' in taskdep and taskname in taskdep['umask']: |
Andrew Geissler | 9b4d8b0 | 2021-02-19 12:26:16 -0600 | [diff] [blame] | 170 | umask = taskdep['umask'][taskname] |
| 171 | elif workerdata["umask"]: |
| 172 | umask = workerdata["umask"] |
| 173 | if umask: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 174 | # umask might come in as a number or text string.. |
| 175 | try: |
Andrew Geissler | 9b4d8b0 | 2021-02-19 12:26:16 -0600 | [diff] [blame] | 176 | umask = int(umask, 8) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 177 | except TypeError: |
Andrew Geissler | 9b4d8b0 | 2021-02-19 12:26:16 -0600 | [diff] [blame] | 178 | pass |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 179 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 180 | dry_run = cfg.dry_run or runtask['dry_run'] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 181 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 182 | # We can't use the fakeroot environment in a dry run as it possibly hasn't been built |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 183 | if 'fakeroot' in taskdep and taskname in taskdep['fakeroot'] and not dry_run: |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 184 | fakeroot = True |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 185 | envvars = (runtask['fakerootenv'] or "").split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 186 | for key, value in (var.split('=') for var in envvars): |
| 187 | envbackup[key] = os.environ.get(key) |
| 188 | os.environ[key] = value |
| 189 | fakeenv[key] = value |
| 190 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 191 | fakedirs = (runtask['fakerootdirs'] or "").split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 192 | for p in fakedirs: |
| 193 | bb.utils.mkdirhier(p) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 194 | logger.debug2('Running %s:%s under fakeroot, fakedirs: %s' % |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 195 | (fn, taskname, ', '.join(fakedirs))) |
| 196 | else: |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 197 | envvars = (runtask['fakerootnoenv'] or "").split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 198 | for key, value in (var.split('=') for var in envvars): |
| 199 | envbackup[key] = os.environ.get(key) |
| 200 | os.environ[key] = value |
| 201 | fakeenv[key] = value |
| 202 | |
| 203 | sys.stdout.flush() |
| 204 | sys.stderr.flush() |
| 205 | |
| 206 | try: |
| 207 | pipein, pipeout = os.pipe() |
| 208 | pipein = os.fdopen(pipein, 'rb', 4096) |
| 209 | pipeout = os.fdopen(pipeout, 'wb', 0) |
| 210 | pid = os.fork() |
| 211 | except OSError as e: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 212 | logger.critical("fork failed: %d (%s)" % (e.errno, e.strerror)) |
| 213 | sys.exit(1) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 214 | |
| 215 | if pid == 0: |
| 216 | def child(): |
| 217 | global worker_pipe |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 218 | global worker_pipe_lock |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 219 | pipein.close() |
| 220 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 221 | bb.utils.signal_on_parent_exit("SIGTERM") |
| 222 | |
| 223 | # Save out the PID so that the event can include it the |
| 224 | # events |
| 225 | bb.event.worker_pid = os.getpid() |
| 226 | bb.event.worker_fire = worker_child_fire |
| 227 | worker_pipe = pipeout |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 228 | worker_pipe_lock = Lock() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 229 | |
| 230 | # Make the child the process group leader and ensure no |
| 231 | # child process will be controlled by the current terminal |
| 232 | # This ensures signals sent to the controlling terminal like Ctrl+C |
| 233 | # don't stop the child processes. |
| 234 | os.setsid() |
Brad Bishop | 1d80a2e | 2019-11-15 16:35:03 -0500 | [diff] [blame] | 235 | |
| 236 | signal.signal(signal.SIGTERM, sigterm_handler) |
| 237 | # Let SIGHUP exit as SIGTERM |
| 238 | signal.signal(signal.SIGHUP, sigterm_handler) |
| 239 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 240 | # No stdin |
| 241 | newsi = os.open(os.devnull, os.O_RDWR) |
| 242 | os.dup2(newsi, sys.stdin.fileno()) |
| 243 | |
| 244 | if umask: |
| 245 | os.umask(umask) |
| 246 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 247 | try: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 248 | (realfn, virtual, mc) = bb.cache.virtualfn2realfn(fn) |
| 249 | the_data = databuilder.mcdata[mc] |
| 250 | the_data.setVar("BB_WORKERCONTEXT", "1") |
| 251 | the_data.setVar("BB_TASKDEPDATA", taskdepdata) |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 252 | the_data.setVar('BB_CURRENTTASK', taskname.replace("do_", "")) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 253 | if cfg.limited_deps: |
| 254 | the_data.setVar("BB_LIMITEDDEPS", "1") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 255 | the_data.setVar("BUILDNAME", workerdata["buildname"]) |
| 256 | the_data.setVar("DATE", workerdata["date"]) |
| 257 | the_data.setVar("TIME", workerdata["time"]) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 258 | for varname, value in extraconfigdata.items(): |
| 259 | the_data.setVar(varname, value) |
| 260 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 261 | bb.parse.siggen.set_taskdata(workerdata["sigdata"]) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 262 | if "newhashes" in workerdata: |
| 263 | bb.parse.siggen.set_taskhashes(workerdata["newhashes"]) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 264 | ret = 0 |
| 265 | |
Patrick Williams | e760df8 | 2023-05-26 11:10:49 -0500 | [diff] [blame] | 266 | the_data = databuilder.parseRecipe(fn, appends, layername) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 267 | the_data.setVar('BB_TASKHASH', taskhash) |
| 268 | the_data.setVar('BB_UNIHASH', unihash) |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 269 | bb.parse.siggen.setup_datacache_from_datastore(fn, the_data) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 270 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 271 | bb.utils.set_process_name("%s:%s" % (the_data.getVar("PN"), taskname.replace("do_", ""))) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 272 | |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 273 | if not bb.utils.to_boolean(the_data.getVarFlag(taskname, 'network')): |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 274 | if bb.utils.is_local_uid(uid): |
| 275 | logger.debug("Attempting to disable network for %s" % taskname) |
| 276 | bb.utils.disable_network(uid, gid) |
| 277 | else: |
| 278 | logger.debug("Skipping disable network for %s since %s is not a local uid." % (taskname, uid)) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 279 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 280 | # exported_vars() returns a generator which *cannot* be passed to os.environ.update() |
| 281 | # successfully. We also need to unset anything from the environment which shouldn't be there |
| 282 | exports = bb.data.exported_vars(the_data) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 283 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 284 | bb.utils.empty_environment() |
| 285 | for e, v in exports: |
| 286 | os.environ[e] = v |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 287 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 288 | for e in fakeenv: |
| 289 | os.environ[e] = fakeenv[e] |
| 290 | the_data.setVar(e, fakeenv[e]) |
| 291 | the_data.setVarFlag(e, 'export', "1") |
| 292 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 293 | task_exports = the_data.getVarFlag(taskname, 'exports') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 294 | if task_exports: |
| 295 | for e in task_exports.split(): |
| 296 | the_data.setVarFlag(e, 'export', '1') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 297 | v = the_data.getVar(e) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 298 | if v is not None: |
| 299 | os.environ[e] = v |
| 300 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 301 | if quieterrors: |
| 302 | the_data.setVarFlag(taskname, "quieterrors", "1") |
| 303 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 304 | except Exception: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 305 | if not quieterrors: |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 306 | logger.critical(traceback.format_exc()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 307 | os._exit(1) |
| 308 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 309 | if dry_run: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 310 | return 0 |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 311 | try: |
| 312 | ret = bb.build.exec_task(fn, taskname, the_data, cfg.profile) |
| 313 | finally: |
| 314 | if fakeroot: |
| 315 | fakerootcmd = shlex.split(the_data.getVar("FAKEROOTCMD")) |
| 316 | subprocess.run(fakerootcmd + ['-S'], check=True, stdout=subprocess.PIPE) |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 317 | return ret |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 318 | except: |
| 319 | os._exit(1) |
| 320 | if not profiling: |
| 321 | os._exit(child()) |
| 322 | else: |
| 323 | profname = "profile-%s.log" % (fn.replace("/", "-") + "-" + taskname) |
| 324 | prof = profile.Profile() |
| 325 | try: |
| 326 | ret = profile.Profile.runcall(prof, child) |
| 327 | finally: |
| 328 | prof.dump_stats(profname) |
| 329 | bb.utils.process_profilelog(profname) |
| 330 | os._exit(ret) |
| 331 | else: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 332 | for key, value in iter(envbackup.items()): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 333 | if value is None: |
| 334 | del os.environ[key] |
| 335 | else: |
| 336 | os.environ[key] = value |
| 337 | |
| 338 | return pid, pipein, pipeout |
| 339 | |
| 340 | class runQueueWorkerPipe(): |
| 341 | """ |
| 342 | Abstraction for a pipe between a worker thread and the worker server |
| 343 | """ |
| 344 | def __init__(self, pipein, pipeout): |
| 345 | self.input = pipein |
| 346 | if pipeout: |
| 347 | pipeout.close() |
| 348 | bb.utils.nonblockingfd(self.input) |
Andrew Geissler | 220dafd | 2023-10-04 10:18:08 -0500 | [diff] [blame] | 349 | self.queue = bytearray() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 350 | |
| 351 | def read(self): |
| 352 | start = len(self.queue) |
| 353 | try: |
Andrew Geissler | 220dafd | 2023-10-04 10:18:08 -0500 | [diff] [blame] | 354 | self.queue.extend(self.input.read(102400) or b"") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 355 | except (OSError, IOError) as e: |
| 356 | if e.errno != errno.EAGAIN: |
| 357 | raise |
| 358 | |
| 359 | end = len(self.queue) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 360 | index = self.queue.find(b"</event>") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 361 | while index != -1: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 362 | msg = self.queue[:index+8] |
| 363 | assert msg.startswith(b"<event>") and msg.count(b"<event>") == 1 |
| 364 | worker_fire_prepickled(msg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 365 | self.queue = self.queue[index+8:] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 366 | index = self.queue.find(b"</event>") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 367 | return (end > start) |
| 368 | |
| 369 | def close(self): |
| 370 | while self.read(): |
| 371 | continue |
| 372 | if len(self.queue) > 0: |
| 373 | print("Warning, worker child left partial message: %s" % self.queue) |
| 374 | self.input.close() |
| 375 | |
| 376 | normalexit = False |
| 377 | |
| 378 | class BitbakeWorker(object): |
| 379 | def __init__(self, din): |
| 380 | self.input = din |
| 381 | bb.utils.nonblockingfd(self.input) |
Andrew Geissler | 220dafd | 2023-10-04 10:18:08 -0500 | [diff] [blame] | 382 | self.queue = bytearray() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 383 | self.cookercfg = None |
| 384 | self.databuilder = None |
| 385 | self.data = None |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 386 | self.extraconfigdata = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 387 | self.build_pids = {} |
| 388 | self.build_pipes = {} |
| 389 | |
| 390 | signal.signal(signal.SIGTERM, self.sigterm_exception) |
| 391 | # Let SIGHUP exit as SIGTERM |
| 392 | signal.signal(signal.SIGHUP, self.sigterm_exception) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 393 | if "beef" in sys.argv[1]: |
| 394 | bb.utils.set_process_name("Worker (Fakeroot)") |
| 395 | else: |
| 396 | bb.utils.set_process_name("Worker") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 397 | |
| 398 | def sigterm_exception(self, signum, stackframe): |
| 399 | if signum == signal.SIGTERM: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 400 | bb.warn("Worker received SIGTERM, shutting down...") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 401 | elif signum == signal.SIGHUP: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 402 | bb.warn("Worker received SIGHUP, shutting down...") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 403 | self.handle_finishnow(None) |
| 404 | signal.signal(signal.SIGTERM, signal.SIG_DFL) |
| 405 | os.kill(os.getpid(), signal.SIGTERM) |
| 406 | |
| 407 | def serve(self): |
| 408 | while True: |
| 409 | (ready, _, _) = select.select([self.input] + [i.input for i in self.build_pipes.values()], [] , [], 1) |
| 410 | if self.input in ready: |
| 411 | try: |
| 412 | r = self.input.read() |
| 413 | if len(r) == 0: |
| 414 | # EOF on pipe, server must have terminated |
| 415 | self.sigterm_exception(signal.SIGTERM, None) |
Andrew Geissler | 220dafd | 2023-10-04 10:18:08 -0500 | [diff] [blame] | 416 | self.queue.extend(r) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 417 | except (OSError, IOError): |
| 418 | pass |
| 419 | if len(self.queue): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 420 | self.handle_item(b"cookerconfig", self.handle_cookercfg) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 421 | self.handle_item(b"extraconfigdata", self.handle_extraconfigdata) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 422 | self.handle_item(b"workerdata", self.handle_workerdata) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 423 | self.handle_item(b"newtaskhashes", self.handle_newtaskhashes) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 424 | self.handle_item(b"runtask", self.handle_runtask) |
| 425 | self.handle_item(b"finishnow", self.handle_finishnow) |
| 426 | self.handle_item(b"ping", self.handle_ping) |
| 427 | self.handle_item(b"quit", self.handle_quit) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 428 | |
| 429 | for pipe in self.build_pipes: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 430 | if self.build_pipes[pipe].input in ready: |
| 431 | self.build_pipes[pipe].read() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 432 | if len(self.build_pids): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 433 | while self.process_waitpid(): |
| 434 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 435 | |
| 436 | |
| 437 | def handle_item(self, item, func): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 438 | if self.queue.startswith(b"<" + item + b">"): |
| 439 | index = self.queue.find(b"</" + item + b">") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 440 | while index != -1: |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 441 | try: |
| 442 | func(self.queue[(len(item) + 2):index]) |
| 443 | except pickle.UnpicklingError: |
| 444 | workerlog_write("Unable to unpickle data: %s\n" % ":".join("{:02x}".format(c) for c in self.queue)) |
| 445 | raise |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 446 | self.queue = self.queue[(index + len(item) + 3):] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 447 | index = self.queue.find(b"</" + item + b">") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 448 | |
| 449 | def handle_cookercfg(self, data): |
| 450 | self.cookercfg = pickle.loads(data) |
| 451 | self.databuilder = bb.cookerdata.CookerDataBuilder(self.cookercfg, worker=True) |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 452 | self.databuilder.parseBaseConfiguration(worker=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 453 | self.data = self.databuilder.data |
| 454 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 455 | def handle_extraconfigdata(self, data): |
| 456 | self.extraconfigdata = pickle.loads(data) |
| 457 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 458 | def handle_workerdata(self, data): |
| 459 | self.workerdata = pickle.loads(data) |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 460 | bb.build.verboseShellLogging = self.workerdata["build_verbose_shell"] |
| 461 | bb.build.verboseStdoutLogging = self.workerdata["build_verbose_stdout"] |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 462 | bb.msg.loggerDefaultLogLevel = self.workerdata["logdefaultlevel"] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 463 | bb.msg.loggerDefaultDomains = self.workerdata["logdefaultdomain"] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 464 | for mc in self.databuilder.mcdata: |
| 465 | self.databuilder.mcdata[mc].setVar("PRSERV_HOST", self.workerdata["prhost"]) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 466 | self.databuilder.mcdata[mc].setVar("BB_HASHSERVE", self.workerdata["hashservaddr"]) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 467 | self.databuilder.mcdata[mc].setVar("__bbclasstype", "recipe") |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 468 | |
| 469 | def handle_newtaskhashes(self, data): |
| 470 | self.workerdata["newhashes"] = pickle.loads(data) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 471 | |
| 472 | def handle_ping(self, _): |
| 473 | workerlog_write("Handling ping\n") |
| 474 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 475 | logger.warning("Pong from bitbake-worker!") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 476 | |
| 477 | def handle_quit(self, data): |
| 478 | workerlog_write("Handling quit\n") |
| 479 | |
| 480 | global normalexit |
| 481 | normalexit = True |
| 482 | sys.exit(0) |
| 483 | |
| 484 | def handle_runtask(self, data): |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 485 | runtask = pickle.loads(data) |
| 486 | |
| 487 | fn = runtask['fn'] |
| 488 | task = runtask['task'] |
| 489 | taskname = runtask['taskname'] |
| 490 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 491 | workerlog_write("Handling runtask %s %s %s\n" % (task, fn, taskname)) |
| 492 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 493 | pid, pipein, pipeout = fork_off_task(self.cookercfg, self.data, self.databuilder, self.workerdata, self.extraconfigdata, runtask) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 494 | self.build_pids[pid] = task |
| 495 | self.build_pipes[pid] = runQueueWorkerPipe(pipein, pipeout) |
| 496 | |
| 497 | def process_waitpid(self): |
| 498 | """ |
| 499 | Return none is there are no processes awaiting result collection, otherwise |
| 500 | collect the process exit codes and close the information pipe. |
| 501 | """ |
| 502 | try: |
| 503 | pid, status = os.waitpid(-1, os.WNOHANG) |
| 504 | if pid == 0 or os.WIFSTOPPED(status): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 505 | return False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 506 | except OSError: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 507 | return False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 508 | |
| 509 | workerlog_write("Exit code of %s for pid %s\n" % (status, pid)) |
| 510 | |
| 511 | if os.WIFEXITED(status): |
| 512 | status = os.WEXITSTATUS(status) |
| 513 | elif os.WIFSIGNALED(status): |
| 514 | # Per shell conventions for $?, when a process exits due to |
| 515 | # a signal, we return an exit code of 128 + SIGNUM |
| 516 | status = 128 + os.WTERMSIG(status) |
| 517 | |
| 518 | task = self.build_pids[pid] |
| 519 | del self.build_pids[pid] |
| 520 | |
| 521 | self.build_pipes[pid].close() |
| 522 | del self.build_pipes[pid] |
| 523 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 524 | worker_fire_prepickled(b"<exitcode>" + pickle.dumps((task, status)) + b"</exitcode>") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 525 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 526 | return True |
| 527 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 528 | def handle_finishnow(self, _): |
| 529 | if self.build_pids: |
| 530 | logger.info("Sending SIGTERM to remaining %s tasks", len(self.build_pids)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 531 | for k, v in iter(self.build_pids.items()): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 532 | try: |
| 533 | os.kill(-k, signal.SIGTERM) |
| 534 | os.waitpid(-1, 0) |
| 535 | except: |
| 536 | pass |
| 537 | for pipe in self.build_pipes: |
| 538 | self.build_pipes[pipe].read() |
| 539 | |
| 540 | try: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 541 | worker = BitbakeWorker(os.fdopen(sys.stdin.fileno(), 'rb')) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 542 | if not profiling: |
| 543 | worker.serve() |
| 544 | else: |
| 545 | profname = "profile-worker.log" |
| 546 | prof = profile.Profile() |
| 547 | try: |
| 548 | profile.Profile.runcall(prof, worker.serve) |
| 549 | finally: |
| 550 | prof.dump_stats(profname) |
| 551 | bb.utils.process_profilelog(profname) |
| 552 | except BaseException as e: |
| 553 | if not normalexit: |
| 554 | import traceback |
| 555 | sys.stderr.write(traceback.format_exc()) |
| 556 | sys.stderr.write(str(e)) |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 557 | finally: |
| 558 | worker_thread_exit = True |
| 559 | worker_thread.join() |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 560 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 561 | workerlog_write("exiting") |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 562 | if not normalexit: |
| 563 | sys.exit(1) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 564 | sys.exit(0) |