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