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