blob: ee2d6224a59ad29845ced09394b9f4f92c41fbb5 [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002
3import os
4import sys
5import warnings
6sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(sys.argv[0])), 'lib'))
7from bb import fetch2
8import logging
9import bb
10import select
11import errno
12import signal
Patrick Williamsc0f7c042017-02-23 20:41:17 -060013import pickle
Brad Bishop37a0e4d2017-12-04 01:01:44 -050014import traceback
15import queue
Patrick Williamsf1e5d692016-03-30 15:21:19 -050016from multiprocessing import Lock
Brad Bishop37a0e4d2017-12-04 01:01:44 -050017from threading import Thread
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018
Patrick Williamsc0f7c042017-02-23 20:41:17 -060019if sys.getfilesystemencoding() != "utf-8":
20 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.")
21
Patrick Williamsc124f4f2015-09-15 14:41:29 -050022# Users shouldn't be running this code directly
23if 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
27profiling = False
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050028if sys.argv[1].startswith("decafbadbad"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050029 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
38try:
39 if sys.stdout.name == '<stdout>':
Patrick Williamsc0f7c042017-02-23 20:41:17 -060040 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 Williamsc124f4f2015-09-15 14:41:29 -050045except:
46 pass
47
48logger = logging.getLogger("BitBake")
49
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050worker_pipe = sys.stdout.fileno()
51bb.utils.nonblockingfd(worker_pipe)
Patrick Williamsf1e5d692016-03-30 15:21:19 -050052# Need to guard against multiprocessing being used in child processes
53# and multiple processes trying to write to the parent at the same time
54worker_pipe_lock = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055
56handler = bb.event.LogHandler()
57logger.addHandler(handler)
58
59if 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 Bishop37a0e4d2017-12-04 01:01:44 -050069worker_queue = queue.Queue()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050070
71def worker_fire(event, d):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060072 data = b"<event>" + pickle.dumps(event) + b"</event>"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 worker_fire_prepickled(data)
74
75def worker_fire_prepickled(event):
76 global worker_queue
77
Brad Bishop37a0e4d2017-12-04 01:01:44 -050078 worker_queue.put(event)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079
Brad Bishop37a0e4d2017-12-04 01:01:44 -050080#
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#
85worker_thread_exit = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086
Brad Bishop37a0e4d2017-12-04 01:01:44 -050087def worker_flush(worker_queue):
88 worker_queue_int = b""
89 global worker_pipe, worker_thread_exit
Patrick Williamsc124f4f2015-09-15 14:41:29 -050090
Brad Bishop37a0e4d2017-12-04 01:01:44 -050091 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 Bishop6e60e8b2018-02-01 10:27:11 -050098 (_, ready, _) = select.select([], [worker_pipe], [], 1)
Brad Bishop37a0e4d2017-12-04 01:01:44 -050099 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
109worker_thread = Thread(target=worker_flush, args=(worker_queue,))
110worker_thread.start()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111
112def worker_child_fire(event, d):
113 global worker_pipe
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500114 global worker_pipe_lock
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500115
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600116 data = b"<event>" + pickle.dumps(event) + b"</event>"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500117 try:
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500118 worker_pipe_lock.acquire()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500119 worker_pipe.write(data)
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500120 worker_pipe_lock.release()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121 except IOError:
122 sigterm_handler(None, None)
123 raise
124
125bb.event.worker_fire = worker_fire
126
127lf = None
128#lf = open("/tmp/workercommandlog", "w+")
129def workerlog_write(msg):
130 if lf:
131 lf.write(msg)
132 lf.flush()
133
134def sigterm_handler(signum, frame):
135 signal.signal(signal.SIGTERM, signal.SIG_DFL)
136 os.killpg(0, signal.SIGTERM)
137 sys.exit()
138
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500139def fork_off_task(cfg, data, databuilder, workerdata, fn, task, taskname, appends, taskdepdata, extraconfigdata, quieterrors=False, dry_run_exec=False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500140 # 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 Bishop6e60e8b2018-02-01 10:27:11 -0500155 dry_run = cfg.dry_run or dry_run_exec
156
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500157 # We can't use the fakeroot environment in a dry run as it possibly hasn't been built
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500158 if 'fakeroot' in taskdep and taskname in taskdep['fakeroot'] and not dry_run:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500159 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 Williamsc0f7c042017-02-23 20:41:17 -0600186 logger.critical("fork failed: %d (%s)" % (e.errno, e.strerror))
187 sys.exit(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500188
189 if pid == 0:
190 def child():
191 global worker_pipe
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500192 global worker_pipe_lock
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500193 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 Williamsf1e5d692016-03-30 15:21:19 -0500205 worker_pipe_lock = Lock()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500206
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 Williamsc124f4f2015-09-15 14:41:29 -0500219 try:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600220 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 Bishop6e60e8b2018-02-01 10:27:11 -0500225 if cfg.limited_deps:
226 the_data.setVar("BB_LIMITEDDEPS", "1")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600227 the_data.setVar("BUILDNAME", workerdata["buildname"])
228 the_data.setVar("DATE", workerdata["date"])
229 the_data.setVar("TIME", workerdata["time"])
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500230 for varname, value in extraconfigdata.items():
231 the_data.setVar(varname, value)
232
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600233 bb.parse.siggen.set_taskdata(workerdata["sigdata"])
234 ret = 0
235
236 the_data = bb_cache.loadDataFull(fn, appends)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500237 the_data.setVar('BB_TASKHASH', workerdata["runq_hash"][task])
238
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500239 bb.utils.set_process_name("%s:%s" % (the_data.getVar("PN"), taskname.replace("do_", "")))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500240
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500241 # 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 Williamsc0f7c042017-02-23 20:41:17 -0600244
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500245 bb.utils.empty_environment()
246 for e, v in exports:
247 os.environ[e] = v
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600248
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500249 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 Bishop6e60e8b2018-02-01 10:27:11 -0500254 task_exports = the_data.getVarFlag(taskname, 'exports')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600255 if task_exports:
256 for e in task_exports.split():
257 the_data.setVarFlag(e, 'export', '1')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500258 v = the_data.getVar(e)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600259 if v is not None:
260 os.environ[e] = v
261
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500262 if quieterrors:
263 the_data.setVarFlag(taskname, "quieterrors", "1")
264
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500265 except Exception:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500266 if not quieterrors:
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500267 logger.critical(traceback.format_exc())
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500268 os._exit(1)
269 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500270 if dry_run:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500271 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 Williamsc0f7c042017-02-23 20:41:17 -0600287 for key, value in iter(envbackup.items()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500288 if value is None:
289 del os.environ[key]
290 else:
291 os.environ[key] = value
292
293 return pid, pipein, pipeout
294
295class 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 Williamsc0f7c042017-02-23 20:41:17 -0600304 self.queue = b""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500305
306 def read(self):
307 start = len(self.queue)
308 try:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600309 self.queue = self.queue + (self.input.read(102400) or b"")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500310 except (OSError, IOError) as e:
311 if e.errno != errno.EAGAIN:
312 raise
313
314 end = len(self.queue)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600315 index = self.queue.find(b"</event>")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500316 while index != -1:
317 worker_fire_prepickled(self.queue[:index+8])
318 self.queue = self.queue[index+8:]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600319 index = self.queue.find(b"</event>")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500320 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
329normalexit = False
330
331class BitbakeWorker(object):
332 def __init__(self, din):
333 self.input = din
334 bb.utils.nonblockingfd(self.input)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600335 self.queue = b""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500336 self.cookercfg = None
337 self.databuilder = None
338 self.data = None
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500339 self.extraconfigdata = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500340 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 Williamsd8c66bc2016-06-20 12:57:21 -0500346 if "beef" in sys.argv[1]:
347 bb.utils.set_process_name("Worker (Fakeroot)")
348 else:
349 bb.utils.set_process_name("Worker")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500350
351 def sigterm_exception(self, signum, stackframe):
352 if signum == signal.SIGTERM:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500353 bb.warn("Worker received SIGTERM, shutting down...")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500354 elif signum == signal.SIGHUP:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500355 bb.warn("Worker received SIGHUP, shutting down...")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500356 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 Williamsc0f7c042017-02-23 20:41:17 -0600373 self.handle_item(b"cookerconfig", self.handle_cookercfg)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500374 self.handle_item(b"extraconfigdata", self.handle_extraconfigdata)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600375 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 Williamsc124f4f2015-09-15 14:41:29 -0500380
381 for pipe in self.build_pipes:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500382 if self.build_pipes[pipe].input in ready:
383 self.build_pipes[pipe].read()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500384 if len(self.build_pids):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500385 while self.process_waitpid():
386 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500387
388
389 def handle_item(self, item, func):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600390 if self.queue.startswith(b"<" + item + b">"):
391 index = self.queue.find(b"</" + item + b">")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500392 while index != -1:
393 func(self.queue[(len(item) + 2):index])
394 self.queue = self.queue[(index + len(item) + 3):]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600395 index = self.queue.find(b"</" + item + b">")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500396
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 Bishop6e60e8b2018-02-01 10:27:11 -0500403 def handle_extraconfigdata(self, data):
404 self.extraconfigdata = pickle.loads(data)
405
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500406 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 Williamsc0f7c042017-02-23 20:41:17 -0600412 for mc in self.databuilder.mcdata:
413 self.databuilder.mcdata[mc].setVar("PRSERV_HOST", self.workerdata["prhost"])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500414
415 def handle_ping(self, _):
416 workerlog_write("Handling ping\n")
417
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600418 logger.warning("Pong from bitbake-worker!")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500419
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 Bishop6e60e8b2018-02-01 10:27:11 -0500428 fn, task, taskname, quieterrors, appends, taskdepdata, dry_run_exec = pickle.loads(data)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500429 workerlog_write("Handling runtask %s %s %s\n" % (task, fn, taskname))
430
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500431 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 Williamsc124f4f2015-09-15 14:41:29 -0500432
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 Bishop6e60e8b2018-02-01 10:27:11 -0500444 return False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500445 except OSError:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500446 return False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500447
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 Williamsc0f7c042017-02-23 20:41:17 -0600463 worker_fire_prepickled(b"<exitcode>" + pickle.dumps((task, status)) + b"</exitcode>")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500464
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500465 return True
466
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500467 def handle_finishnow(self, _):
468 if self.build_pids:
469 logger.info("Sending SIGTERM to remaining %s tasks", len(self.build_pids))
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600470 for k, v in iter(self.build_pids.items()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500471 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
479try:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600480 worker = BitbakeWorker(os.fdopen(sys.stdin.fileno(), 'rb'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500481 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)
491except BaseException as e:
492 if not normalexit:
493 import traceback
494 sys.stderr.write(traceback.format_exc())
495 sys.stderr.write(str(e))
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500496
497worker_thread_exit = True
498worker_thread.join()
499
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500500workerlog_write("exitting")
501sys.exit(0)
502