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