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