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