Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
| 2 | # BitBake Process based server. |
| 3 | # |
| 4 | # Copyright (C) 2010 Bob Foerster <robert@erafx.com> |
| 5 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 6 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | |
| 9 | """ |
| 10 | This module implements a multiprocessing.Process based server for bitbake. |
| 11 | """ |
| 12 | |
| 13 | import bb |
| 14 | import bb.event |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 15 | import logging |
| 16 | import multiprocessing |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 17 | import threading |
| 18 | import array |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | import os |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 | import sys |
| 21 | import time |
| 22 | import select |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 23 | import socket |
| 24 | import subprocess |
| 25 | import errno |
| 26 | import re |
| 27 | import datetime |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 28 | import pickle |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 29 | import traceback |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 30 | import bb.server.xmlrpcserver |
| 31 | from bb import daemonize |
| 32 | from multiprocessing import queues |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 33 | |
| 34 | logger = logging.getLogger('BitBake') |
| 35 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 36 | class ProcessTimeout(SystemExit): |
| 37 | pass |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 39 | def serverlog(msg): |
| 40 | print(str(os.getpid()) + " " + datetime.datetime.now().strftime('%H:%M:%S.%f') + " " + msg) |
| 41 | sys.stdout.flush() |
| 42 | |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 43 | class ProcessServer(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | profile_filename = "profile.log" |
| 45 | profile_processed_filename = "profile.log.processed" |
| 46 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 47 | def __init__(self, lock, lockname, sock, sockname, server_timeout, xmlrpcinterface): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 48 | self.command_channel = False |
| 49 | self.command_channel_reply = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 50 | self.quit = False |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 51 | self.heartbeat_seconds = 1 # default, BB_HEARTBEAT_EVENT will be checked once we have a datastore. |
| 52 | self.next_heartbeat = time.time() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 53 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 54 | self.event_handle = None |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 55 | self.hadanyui = False |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 56 | self.haveui = False |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 57 | self.maxuiwait = 30 |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 58 | self.xmlrpc = False |
| 59 | |
| 60 | self._idlefuns = {} |
| 61 | |
| 62 | self.bitbake_lock = lock |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 63 | self.bitbake_lock_name = lockname |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 64 | self.sock = sock |
| 65 | self.sockname = sockname |
| 66 | |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 67 | self.server_timeout = server_timeout |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 68 | self.timeout = self.server_timeout |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 69 | self.xmlrpcinterface = xmlrpcinterface |
| 70 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 71 | def register_idle_function(self, function, data): |
| 72 | """Register a function to be called while the server is idle""" |
| 73 | assert hasattr(function, '__call__') |
| 74 | self._idlefuns[function] = data |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 75 | |
| 76 | def run(self): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 77 | |
| 78 | if self.xmlrpcinterface[0]: |
| 79 | self.xmlrpc = bb.server.xmlrpcserver.BitBakeXMLRPCServer(self.xmlrpcinterface, self.cooker, self) |
| 80 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 81 | serverlog("Bitbake XMLRPC server address: %s, server port: %s" % (self.xmlrpc.host, self.xmlrpc.port)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 82 | |
| 83 | try: |
| 84 | self.bitbake_lock.seek(0) |
| 85 | self.bitbake_lock.truncate() |
| 86 | if self.xmlrpc: |
| 87 | self.bitbake_lock.write("%s %s:%s\n" % (os.getpid(), self.xmlrpc.host, self.xmlrpc.port)) |
| 88 | else: |
| 89 | self.bitbake_lock.write("%s\n" % (os.getpid())) |
| 90 | self.bitbake_lock.flush() |
| 91 | except Exception as e: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 92 | serverlog("Error writing to lock file: %s" % str(e)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 93 | pass |
| 94 | |
| 95 | if self.cooker.configuration.profile: |
| 96 | try: |
| 97 | import cProfile as profile |
| 98 | except: |
| 99 | import profile |
| 100 | prof = profile.Profile() |
| 101 | |
| 102 | ret = profile.Profile.runcall(prof, self.main) |
| 103 | |
| 104 | prof.dump_stats("profile.log") |
| 105 | bb.utils.process_profilelog("profile.log") |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 106 | serverlog("Raw profiling information saved to profile.log and processed statistics to profile.log.processed") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 107 | |
| 108 | else: |
| 109 | ret = self.main() |
| 110 | |
| 111 | return ret |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 112 | |
| 113 | def main(self): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 114 | self.cooker.pre_serve() |
| 115 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 116 | bb.utils.set_process_name("Cooker") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 117 | |
| 118 | ready = [] |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 119 | newconnections = [] |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 120 | |
| 121 | self.controllersock = False |
| 122 | fds = [self.sock] |
| 123 | if self.xmlrpc: |
| 124 | fds.append(self.xmlrpc) |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 125 | seendata = False |
| 126 | serverlog("Entering server connection loop") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 127 | |
| 128 | def disconnect_client(self, fds): |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 129 | serverlog("Disconnecting Client") |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 130 | if self.controllersock: |
| 131 | fds.remove(self.controllersock) |
| 132 | self.controllersock.close() |
| 133 | self.controllersock = False |
| 134 | if self.haveui: |
| 135 | fds.remove(self.command_channel) |
| 136 | bb.event.unregister_UIHhandler(self.event_handle, True) |
| 137 | self.command_channel_reply.writer.close() |
| 138 | self.event_writer.writer.close() |
| 139 | self.command_channel.close() |
| 140 | self.command_channel = False |
| 141 | del self.event_writer |
| 142 | self.lastui = time.time() |
| 143 | self.cooker.clientComplete() |
| 144 | self.haveui = False |
| 145 | ready = select.select(fds,[],[],0)[0] |
| 146 | if newconnections: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 147 | serverlog("Starting new client") |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 148 | conn = newconnections.pop(-1) |
| 149 | fds.append(conn) |
| 150 | self.controllersock = conn |
Andrew Geissler | 5f35090 | 2021-07-23 13:09:54 -0400 | [diff] [blame] | 151 | elif not self.timeout and not ready: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 152 | serverlog("No timeout, exiting.") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 153 | self.quit = True |
| 154 | |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 155 | self.lastui = time.time() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 156 | while not self.quit: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 157 | if self.sock in ready: |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 158 | while select.select([self.sock],[],[],0)[0]: |
| 159 | controllersock, address = self.sock.accept() |
| 160 | if self.controllersock: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 161 | serverlog("Queuing %s (%s)" % (str(ready), str(newconnections))) |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 162 | newconnections.append(controllersock) |
| 163 | else: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 164 | serverlog("Accepting %s (%s)" % (str(ready), str(newconnections))) |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 165 | self.controllersock = controllersock |
| 166 | fds.append(controllersock) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 167 | if self.controllersock in ready: |
| 168 | try: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 169 | serverlog("Processing Client") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 170 | ui_fds = recvfds(self.controllersock, 3) |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 171 | serverlog("Connecting Client") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 172 | |
| 173 | # Where to write events to |
| 174 | writer = ConnectionWriter(ui_fds[0]) |
| 175 | self.event_handle = bb.event.register_UIHhandler(writer, True) |
| 176 | self.event_writer = writer |
| 177 | |
| 178 | # Where to read commands from |
| 179 | reader = ConnectionReader(ui_fds[1]) |
| 180 | fds.append(reader) |
| 181 | self.command_channel = reader |
| 182 | |
| 183 | # Where to send command return values to |
| 184 | writer = ConnectionWriter(ui_fds[2]) |
| 185 | self.command_channel_reply = writer |
| 186 | |
| 187 | self.haveui = True |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 188 | self.hadanyui = True |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 189 | |
| 190 | except (EOFError, OSError): |
| 191 | disconnect_client(self, fds) |
| 192 | |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 193 | if not self.timeout == -1.0 and not self.haveui and self.timeout and \ |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 194 | (self.lastui + self.timeout) < time.time(): |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 195 | serverlog("Server timeout, exiting.") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 196 | self.quit = True |
| 197 | |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 198 | # If we don't see a UI connection within maxuiwait, its unlikely we're going to see |
| 199 | # one. We have had issue with processes hanging indefinitely so timing out UI-less |
| 200 | # servers is useful. |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 201 | if not self.hadanyui and not self.xmlrpc and not self.timeout and (self.lastui + self.maxuiwait) < time.time(): |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 202 | serverlog("No UI connection within max timeout, exiting to avoid infinite loop.") |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 203 | self.quit = True |
| 204 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 205 | if self.command_channel in ready: |
| 206 | try: |
| 207 | command = self.command_channel.get() |
| 208 | except EOFError: |
| 209 | # Client connection shutting down |
| 210 | ready = [] |
| 211 | disconnect_client(self, fds) |
| 212 | continue |
| 213 | if command[0] == "terminateServer": |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 214 | self.quit = True |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 215 | continue |
| 216 | try: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 217 | serverlog("Running command %s" % command) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 218 | self.command_channel_reply.send(self.cooker.command.runCommand(command)) |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 219 | serverlog("Command Completed") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 220 | except Exception as e: |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 221 | stack = traceback.format_exc() |
| 222 | serverlog('Exception in server main event loop running command %s (%s)' % (command, stack)) |
| 223 | logger.exception('Exception in server main event loop running command %s (%s)' % (command, stack)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 224 | |
| 225 | if self.xmlrpc in ready: |
| 226 | self.xmlrpc.handle_requests() |
| 227 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 228 | if not seendata and hasattr(self.cooker, "data"): |
| 229 | heartbeat_event = self.cooker.data.getVar('BB_HEARTBEAT_EVENT') |
| 230 | if heartbeat_event: |
| 231 | try: |
| 232 | self.heartbeat_seconds = float(heartbeat_event) |
| 233 | except: |
| 234 | bb.warn('Ignoring invalid BB_HEARTBEAT_EVENT=%s, must be a float specifying seconds.' % heartbeat_event) |
| 235 | |
| 236 | self.timeout = self.server_timeout or self.cooker.data.getVar('BB_SERVER_TIMEOUT') |
| 237 | try: |
| 238 | if self.timeout: |
| 239 | self.timeout = float(self.timeout) |
| 240 | except: |
| 241 | bb.warn('Ignoring invalid BB_SERVER_TIMEOUT=%s, must be a float specifying seconds.' % self.timeout) |
| 242 | seendata = True |
| 243 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 244 | ready = self.idle_commands(.1, fds) |
| 245 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 246 | if len(threading.enumerate()) != 1: |
| 247 | serverlog("More than one thread left?: " + str(threading.enumerate())) |
| 248 | |
| 249 | serverlog("Exiting") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 250 | # Remove the socket file so we don't get any more connections to avoid races |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 251 | try: |
| 252 | os.unlink(self.sockname) |
| 253 | except: |
| 254 | pass |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 255 | self.sock.close() |
| 256 | |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 257 | try: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 258 | self.cooker.shutdown(True) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 259 | self.cooker.notifier.stop() |
| 260 | self.cooker.confignotifier.stop() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 261 | except: |
| 262 | pass |
| 263 | |
| 264 | self.cooker.post_serve() |
| 265 | |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 266 | # Flush logs before we release the lock |
| 267 | sys.stdout.flush() |
| 268 | sys.stderr.flush() |
| 269 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 270 | # Finally release the lockfile but warn about other processes holding it open |
| 271 | lock = self.bitbake_lock |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 272 | lockfile = self.bitbake_lock_name |
| 273 | |
| 274 | def get_lock_contents(lockfile): |
| 275 | try: |
| 276 | with open(lockfile, "r") as f: |
| 277 | return f.readlines() |
| 278 | except FileNotFoundError: |
| 279 | return None |
| 280 | |
| 281 | lockcontents = get_lock_contents(lockfile) |
| 282 | serverlog("Original lockfile contents: " + str(lockcontents)) |
| 283 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 284 | lock.close() |
| 285 | lock = None |
| 286 | |
| 287 | while not lock: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 288 | i = 0 |
| 289 | lock = None |
| 290 | while not lock and i < 30: |
| 291 | lock = bb.utils.lockfile(lockfile, shared=False, retry=False, block=False) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 292 | if not lock: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 293 | newlockcontents = get_lock_contents(lockfile) |
| 294 | if newlockcontents != lockcontents: |
| 295 | # A new server was started, the lockfile contents changed, we can exit |
| 296 | serverlog("Lockfile now contains different contents, exiting: " + str(newlockcontents)) |
| 297 | return |
| 298 | time.sleep(0.1) |
| 299 | i += 1 |
| 300 | if lock: |
| 301 | # We hold the lock so we can remove the file (hide stale pid data) |
| 302 | # via unlockfile. |
| 303 | bb.utils.unlockfile(lock) |
| 304 | serverlog("Exiting as we could obtain the lock") |
| 305 | return |
| 306 | |
| 307 | if not lock: |
| 308 | # Some systems may not have lsof available |
| 309 | procs = None |
| 310 | try: |
| 311 | procs = subprocess.check_output(["lsof", '-w', lockfile], stderr=subprocess.STDOUT) |
| 312 | except subprocess.CalledProcessError: |
| 313 | # File was deleted? |
| 314 | continue |
| 315 | except OSError as e: |
| 316 | if e.errno != errno.ENOENT: |
| 317 | raise |
| 318 | if procs is None: |
| 319 | # Fall back to fuser if lsof is unavailable |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 320 | try: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 321 | procs = subprocess.check_output(["fuser", '-v', lockfile], stderr=subprocess.STDOUT) |
| 322 | except subprocess.CalledProcessError: |
| 323 | # File was deleted? |
| 324 | continue |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 325 | except OSError as e: |
| 326 | if e.errno != errno.ENOENT: |
| 327 | raise |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 328 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 329 | msg = ["Delaying shutdown due to active processes which appear to be holding bitbake.lock"] |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 330 | if procs: |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 331 | msg.append(":\n%s" % str(procs.decode("utf-8"))) |
| 332 | serverlog("".join(msg)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 333 | |
| 334 | def idle_commands(self, delay, fds=None): |
| 335 | nextsleep = delay |
| 336 | if not fds: |
| 337 | fds = [] |
| 338 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 339 | for function, data in list(self._idlefuns.items()): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 340 | try: |
| 341 | retval = function(self, data, False) |
| 342 | if retval is False: |
| 343 | del self._idlefuns[function] |
| 344 | nextsleep = None |
| 345 | elif retval is True: |
| 346 | nextsleep = None |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 347 | elif isinstance(retval, float) and nextsleep: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 348 | if (retval < nextsleep): |
| 349 | nextsleep = retval |
| 350 | elif nextsleep is None: |
| 351 | continue |
| 352 | else: |
| 353 | fds = fds + retval |
| 354 | except SystemExit: |
| 355 | raise |
| 356 | except Exception as exc: |
| 357 | if not isinstance(exc, bb.BBHandledException): |
| 358 | logger.exception('Running idle function') |
| 359 | del self._idlefuns[function] |
| 360 | self.quit = True |
| 361 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 362 | # Create new heartbeat event? |
| 363 | now = time.time() |
| 364 | if now >= self.next_heartbeat: |
| 365 | # We might have missed heartbeats. Just trigger once in |
| 366 | # that case and continue after the usual delay. |
| 367 | self.next_heartbeat += self.heartbeat_seconds |
| 368 | if self.next_heartbeat <= now: |
| 369 | self.next_heartbeat = now + self.heartbeat_seconds |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 370 | if hasattr(self.cooker, "data"): |
| 371 | heartbeat = bb.event.HeartbeatEvent(now) |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 372 | try: |
| 373 | bb.event.fire(heartbeat, self.cooker.data) |
| 374 | except Exception as exc: |
| 375 | if not isinstance(exc, bb.BBHandledException): |
| 376 | logger.exception('Running heartbeat function') |
| 377 | self.quit = True |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 378 | if nextsleep and now + nextsleep > self.next_heartbeat: |
| 379 | # Shorten timeout so that we we wake up in time for |
| 380 | # the heartbeat. |
| 381 | nextsleep = self.next_heartbeat - now |
| 382 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 383 | if nextsleep is not None: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 384 | if self.xmlrpc: |
| 385 | nextsleep = self.xmlrpc.get_timeout(nextsleep) |
| 386 | try: |
| 387 | return select.select(fds,[],[],nextsleep)[0] |
| 388 | except InterruptedError: |
| 389 | # Ignore EINTR |
| 390 | return [] |
| 391 | else: |
| 392 | return select.select(fds,[],[],0)[0] |
| 393 | |
| 394 | |
| 395 | class ServerCommunicator(): |
| 396 | def __init__(self, connection, recv): |
| 397 | self.connection = connection |
| 398 | self.recv = recv |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 399 | |
| 400 | def runCommand(self, command): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 401 | self.connection.send(command) |
| 402 | if not self.recv.poll(30): |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 403 | logger.info("No reply from server in 30s") |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 404 | if not self.recv.poll(30): |
| 405 | raise ProcessTimeout("Timeout while waiting for a reply from the bitbake server (60s)") |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 406 | ret, exc = self.recv.get() |
| 407 | # Should probably turn all exceptions in exc back into exceptions? |
| 408 | # For now, at least handle BBHandledException |
| 409 | if exc and ("BBHandledException" in exc or "SystemExit" in exc): |
| 410 | raise bb.BBHandledException() |
| 411 | return ret, exc |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 412 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 413 | def updateFeatureSet(self, featureset): |
| 414 | _, error = self.runCommand(["setFeatures", featureset]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 415 | if error: |
| 416 | logger.error("Unable to set the cooker to the correct featureset: %s" % error) |
| 417 | raise BaseException(error) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 418 | |
| 419 | def getEventHandle(self): |
| 420 | handle, error = self.runCommand(["getUIHandlerNum"]) |
| 421 | if error: |
| 422 | logger.error("Unable to get UI Handler Number: %s" % error) |
| 423 | raise BaseException(error) |
| 424 | |
| 425 | return handle |
| 426 | |
| 427 | def terminateServer(self): |
| 428 | self.connection.send(['terminateServer']) |
| 429 | return |
| 430 | |
| 431 | class BitBakeProcessServerConnection(object): |
| 432 | def __init__(self, ui_channel, recv, eq, sock): |
| 433 | self.connection = ServerCommunicator(ui_channel, recv) |
| 434 | self.events = eq |
| 435 | # Save sock so it doesn't get gc'd for the life of our connection |
| 436 | self.socket_connection = sock |
| 437 | |
| 438 | def terminate(self): |
| 439 | self.socket_connection.close() |
| 440 | self.connection.connection.close() |
| 441 | self.connection.recv.close() |
| 442 | return |
| 443 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 444 | start_log_format = '--- Starting bitbake server pid %s at %s ---' |
| 445 | start_log_datetime_format = '%Y-%m-%d %H:%M:%S.%f' |
| 446 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 447 | class BitBakeServer(object): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 448 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 449 | def __init__(self, lock, sockname, featureset, server_timeout, xmlrpcinterface): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 450 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 451 | self.server_timeout = server_timeout |
| 452 | self.xmlrpcinterface = xmlrpcinterface |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 453 | self.featureset = featureset |
| 454 | self.sockname = sockname |
| 455 | self.bitbake_lock = lock |
| 456 | self.readypipe, self.readypipein = os.pipe() |
| 457 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 458 | # Place the log in the builddirectory alongside the lock file |
| 459 | logfile = os.path.join(os.path.dirname(self.bitbake_lock.name), "bitbake-cookerdaemon.log") |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 460 | self.logfile = logfile |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 461 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 462 | startdatetime = datetime.datetime.now() |
| 463 | bb.daemonize.createDaemon(self._startServer, logfile) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 464 | self.bitbake_lock.close() |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 465 | os.close(self.readypipein) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 466 | |
| 467 | ready = ConnectionReader(self.readypipe) |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 468 | r = ready.poll(5) |
| 469 | if not r: |
| 470 | bb.note("Bitbake server didn't start within 5 seconds, waiting for 90") |
| 471 | r = ready.poll(90) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 472 | if r: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 473 | try: |
| 474 | r = ready.get() |
| 475 | except EOFError: |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 476 | # Trap the child exiting/closing the pipe and error out |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 477 | r = None |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 478 | if not r or r[0] != "r": |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 479 | ready.close() |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 480 | bb.error("Unable to start bitbake server (%s)" % str(r)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 481 | if os.path.exists(logfile): |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 482 | logstart_re = re.compile(start_log_format % ('([0-9]+)', '([0-9-]+ [0-9:.]+)')) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 483 | started = False |
| 484 | lines = [] |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 485 | lastlines = [] |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 486 | with open(logfile, "r") as f: |
| 487 | for line in f: |
| 488 | if started: |
| 489 | lines.append(line) |
| 490 | else: |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 491 | lastlines.append(line) |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 492 | res = logstart_re.search(line.rstrip()) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 493 | if res: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 494 | ldatetime = datetime.datetime.strptime(res.group(2), start_log_datetime_format) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 495 | if ldatetime >= startdatetime: |
| 496 | started = True |
| 497 | lines.append(line) |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 498 | if len(lastlines) > 60: |
| 499 | lastlines = lastlines[-60:] |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 500 | if lines: |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 501 | if len(lines) > 60: |
| 502 | bb.error("Last 60 lines of server log for this session (%s):\n%s" % (logfile, "".join(lines[-60:]))) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 503 | else: |
| 504 | bb.error("Server log for this session (%s):\n%s" % (logfile, "".join(lines))) |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 505 | elif lastlines: |
| 506 | bb.error("Server didn't start, last 60 loglines (%s):\n%s" % (logfile, "".join(lastlines))) |
| 507 | else: |
| 508 | bb.error("%s doesn't exist" % logfile) |
| 509 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 510 | raise SystemExit(1) |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 511 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 512 | ready.close() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 513 | |
| 514 | def _startServer(self): |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 515 | os.close(self.readypipe) |
| 516 | os.set_inheritable(self.bitbake_lock.fileno(), True) |
| 517 | os.set_inheritable(self.readypipein, True) |
| 518 | serverscript = os.path.realpath(os.path.dirname(__file__) + "/../../../bin/bitbake-server") |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 519 | os.execl(sys.executable, "bitbake-server", serverscript, "decafbad", str(self.bitbake_lock.fileno()), str(self.readypipein), self.logfile, self.bitbake_lock.name, self.sockname, str(self.server_timeout or 0), str(self.xmlrpcinterface[0]), str(self.xmlrpcinterface[1])) |
Brad Bishop | e2d5b61 | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 520 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 521 | def execServer(lockfd, readypipeinfd, lockname, sockname, server_timeout, xmlrpcinterface): |
| 522 | |
| 523 | import bb.cookerdata |
| 524 | import bb.cooker |
| 525 | |
| 526 | serverlog(start_log_format % (os.getpid(), datetime.datetime.now().strftime(start_log_datetime_format))) |
| 527 | |
| 528 | try: |
| 529 | bitbake_lock = os.fdopen(lockfd, "w") |
| 530 | |
| 531 | # Create server control socket |
| 532 | if os.path.exists(sockname): |
| 533 | os.unlink(sockname) |
| 534 | |
| 535 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 536 | # AF_UNIX has path length issues so chdir here to workaround |
| 537 | cwd = os.getcwd() |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 538 | try: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 539 | os.chdir(os.path.dirname(sockname)) |
| 540 | sock.bind(os.path.basename(sockname)) |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 541 | finally: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 542 | os.chdir(cwd) |
| 543 | sock.listen(1) |
| 544 | |
| 545 | server = ProcessServer(bitbake_lock, lockname, sock, sockname, server_timeout, xmlrpcinterface) |
| 546 | writer = ConnectionWriter(readypipeinfd) |
| 547 | try: |
| 548 | featureset = [] |
| 549 | cooker = bb.cooker.BBCooker(featureset, server.register_idle_function) |
| 550 | except bb.BBHandledException: |
| 551 | return None |
| 552 | writer.send("r") |
| 553 | writer.close() |
| 554 | server.cooker = cooker |
| 555 | serverlog("Started bitbake server pid %d" % os.getpid()) |
| 556 | |
| 557 | server.run() |
| 558 | finally: |
| 559 | # Flush any ,essages/errors to the logfile before exit |
| 560 | sys.stdout.flush() |
| 561 | sys.stderr.flush() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 562 | |
| 563 | def connectProcessServer(sockname, featureset): |
| 564 | # Connect to socket |
| 565 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 566 | # AF_UNIX has path length issues so chdir here to workaround |
| 567 | cwd = os.getcwd() |
| 568 | |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 569 | readfd = writefd = readfd1 = writefd1 = readfd2 = writefd2 = None |
| 570 | eq = command_chan_recv = command_chan = None |
| 571 | |
| 572 | sock.settimeout(10) |
| 573 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 574 | try: |
Brad Bishop | e2d5b61 | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 575 | try: |
| 576 | os.chdir(os.path.dirname(sockname)) |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 577 | finished = False |
| 578 | while not finished: |
| 579 | try: |
| 580 | sock.connect(os.path.basename(sockname)) |
| 581 | finished = True |
| 582 | except IOError as e: |
| 583 | if e.errno == errno.EWOULDBLOCK: |
| 584 | pass |
Richard Purdie | 3da1114 | 2019-02-05 21:34:37 +0000 | [diff] [blame] | 585 | raise |
Brad Bishop | e2d5b61 | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 586 | finally: |
| 587 | os.chdir(cwd) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 588 | |
| 589 | # Send an fd for the remote to write events to |
| 590 | readfd, writefd = os.pipe() |
| 591 | eq = BBUIEventQueue(readfd) |
| 592 | # Send an fd for the remote to recieve commands from |
| 593 | readfd1, writefd1 = os.pipe() |
| 594 | command_chan = ConnectionWriter(writefd1) |
| 595 | # Send an fd for the remote to write commands results to |
| 596 | readfd2, writefd2 = os.pipe() |
| 597 | command_chan_recv = ConnectionReader(readfd2) |
| 598 | |
| 599 | sendfds(sock, [writefd, readfd1, writefd2]) |
| 600 | |
| 601 | server_connection = BitBakeProcessServerConnection(command_chan, command_chan_recv, eq, sock) |
| 602 | |
| 603 | # Close the ends of the pipes we won't use |
| 604 | for i in [writefd, readfd1, writefd2]: |
| 605 | os.close(i) |
| 606 | |
| 607 | server_connection.connection.updateFeatureSet(featureset) |
| 608 | |
| 609 | except (Exception, SystemExit) as e: |
| 610 | if command_chan_recv: |
| 611 | command_chan_recv.close() |
| 612 | if command_chan: |
| 613 | command_chan.close() |
| 614 | for i in [writefd, readfd1, writefd2]: |
| 615 | try: |
Brad Bishop | e2d5b61 | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 616 | if i: |
| 617 | os.close(i) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 618 | except OSError: |
| 619 | pass |
| 620 | sock.close() |
| 621 | raise |
| 622 | |
| 623 | return server_connection |
| 624 | |
| 625 | def sendfds(sock, fds): |
| 626 | '''Send an array of fds over an AF_UNIX socket.''' |
| 627 | fds = array.array('i', fds) |
| 628 | msg = bytes([len(fds) % 256]) |
| 629 | sock.sendmsg([msg], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, fds)]) |
| 630 | |
| 631 | def recvfds(sock, size): |
| 632 | '''Receive an array of fds over an AF_UNIX socket.''' |
| 633 | a = array.array('i') |
| 634 | bytes_size = a.itemsize * size |
| 635 | msg, ancdata, flags, addr = sock.recvmsg(1, socket.CMSG_LEN(bytes_size)) |
| 636 | if not msg and not ancdata: |
| 637 | raise EOFError |
| 638 | try: |
| 639 | if len(ancdata) != 1: |
| 640 | raise RuntimeError('received %d items of ancdata' % |
| 641 | len(ancdata)) |
| 642 | cmsg_level, cmsg_type, cmsg_data = ancdata[0] |
| 643 | if (cmsg_level == socket.SOL_SOCKET and |
| 644 | cmsg_type == socket.SCM_RIGHTS): |
| 645 | if len(cmsg_data) % a.itemsize != 0: |
| 646 | raise ValueError |
| 647 | a.frombytes(cmsg_data) |
| 648 | assert len(a) % 256 == msg[0] |
| 649 | return list(a) |
| 650 | except (ValueError, IndexError): |
| 651 | pass |
| 652 | raise RuntimeError('Invalid data received') |
| 653 | |
| 654 | class BBUIEventQueue: |
| 655 | def __init__(self, readfd): |
| 656 | |
| 657 | self.eventQueue = [] |
| 658 | self.eventQueueLock = threading.Lock() |
| 659 | self.eventQueueNotify = threading.Event() |
| 660 | |
| 661 | self.reader = ConnectionReader(readfd) |
| 662 | |
| 663 | self.t = threading.Thread() |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 664 | self.t.daemon = True |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 665 | self.t.run = self.startCallbackHandler |
| 666 | self.t.start() |
| 667 | |
| 668 | def getEvent(self): |
| 669 | self.eventQueueLock.acquire() |
| 670 | |
| 671 | if len(self.eventQueue) == 0: |
| 672 | self.eventQueueLock.release() |
| 673 | return None |
| 674 | |
| 675 | item = self.eventQueue.pop(0) |
| 676 | |
| 677 | if len(self.eventQueue) == 0: |
| 678 | self.eventQueueNotify.clear() |
| 679 | |
| 680 | self.eventQueueLock.release() |
| 681 | return item |
| 682 | |
| 683 | def waitEvent(self, delay): |
| 684 | self.eventQueueNotify.wait(delay) |
| 685 | return self.getEvent() |
| 686 | |
| 687 | def queue_event(self, event): |
| 688 | self.eventQueueLock.acquire() |
| 689 | self.eventQueue.append(event) |
| 690 | self.eventQueueNotify.set() |
| 691 | self.eventQueueLock.release() |
| 692 | |
| 693 | def send_event(self, event): |
| 694 | self.queue_event(pickle.loads(event)) |
| 695 | |
| 696 | def startCallbackHandler(self): |
| 697 | bb.utils.set_process_name("UIEventQueue") |
| 698 | while True: |
| 699 | try: |
| 700 | self.reader.wait() |
| 701 | event = self.reader.get() |
| 702 | self.queue_event(event) |
| 703 | except EOFError: |
| 704 | # Easiest way to exit is to close the file descriptor to cause an exit |
| 705 | break |
| 706 | self.reader.close() |
| 707 | |
| 708 | class ConnectionReader(object): |
| 709 | |
| 710 | def __init__(self, fd): |
| 711 | self.reader = multiprocessing.connection.Connection(fd, writable=False) |
| 712 | self.rlock = multiprocessing.Lock() |
| 713 | |
| 714 | def wait(self, timeout=None): |
| 715 | return multiprocessing.connection.wait([self.reader], timeout) |
| 716 | |
| 717 | def poll(self, timeout=None): |
| 718 | return self.reader.poll(timeout) |
| 719 | |
| 720 | def get(self): |
| 721 | with self.rlock: |
| 722 | res = self.reader.recv_bytes() |
| 723 | return multiprocessing.reduction.ForkingPickler.loads(res) |
| 724 | |
| 725 | def fileno(self): |
| 726 | return self.reader.fileno() |
| 727 | |
| 728 | def close(self): |
| 729 | return self.reader.close() |
| 730 | |
| 731 | |
| 732 | class ConnectionWriter(object): |
| 733 | |
| 734 | def __init__(self, fd): |
| 735 | self.writer = multiprocessing.connection.Connection(fd, readable=False) |
| 736 | self.wlock = multiprocessing.Lock() |
| 737 | # Why bb.event needs this I have no idea |
| 738 | self.event = self |
| 739 | |
| 740 | def send(self, obj): |
| 741 | obj = multiprocessing.reduction.ForkingPickler.dumps(obj) |
| 742 | with self.wlock: |
| 743 | self.writer.send_bytes(obj) |
| 744 | |
| 745 | def fileno(self): |
| 746 | return self.writer.fileno() |
| 747 | |
| 748 | def close(self): |
| 749 | return self.writer.close() |