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