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