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