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