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 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 364 | lock.close() |
| 365 | lock = None |
| 366 | |
| 367 | while not lock: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 368 | i = 0 |
| 369 | lock = None |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 370 | if not os.path.exists(os.path.basename(lockfile)): |
| 371 | serverlog("Lockfile directory gone, exiting.") |
| 372 | return |
| 373 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 374 | while not lock and i < 30: |
| 375 | lock = bb.utils.lockfile(lockfile, shared=False, retry=False, block=False) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 376 | if not lock: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 377 | newlockcontents = get_lock_contents(lockfile) |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 378 | if not newlockcontents[0].startswith([os.getpid() + "\n", os.getpid() + " "]): |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 379 | # A new server was started, the lockfile contents changed, we can exit |
| 380 | serverlog("Lockfile now contains different contents, exiting: " + str(newlockcontents)) |
| 381 | return |
| 382 | time.sleep(0.1) |
| 383 | i += 1 |
| 384 | if lock: |
| 385 | # We hold the lock so we can remove the file (hide stale pid data) |
| 386 | # via unlockfile. |
| 387 | bb.utils.unlockfile(lock) |
| 388 | serverlog("Exiting as we could obtain the lock") |
| 389 | return |
| 390 | |
| 391 | if not lock: |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 392 | procs = get_lockfile_process_msg(lockfile) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 393 | 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] | 394 | if procs: |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 395 | msg.append(":\n%s" % procs) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 396 | serverlog("".join(msg)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 397 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 398 | def idle_thread(self): |
| 399 | def remove_idle_func(function): |
| 400 | with bb.utils.lock_timeout(self._idlefuncsLock): |
| 401 | del self._idlefuns[function] |
| 402 | self.idle_cond.notify_all() |
| 403 | |
| 404 | while not self.quit: |
| 405 | nextsleep = 0.1 |
| 406 | fds = [] |
| 407 | |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 408 | try: |
| 409 | self.cooker.process_inotify_updates() |
| 410 | except Exception as exc: |
| 411 | serverlog("Exception %s in inofify updates broke the idle_thread, exiting" % traceback.format_exc()) |
| 412 | self.quit = True |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 413 | |
| 414 | with bb.utils.lock_timeout(self._idlefuncsLock): |
| 415 | items = list(self._idlefuns.items()) |
| 416 | |
| 417 | for function, data in items: |
| 418 | try: |
| 419 | retval = function(self, data, False) |
| 420 | if isinstance(retval, idleFinish): |
| 421 | serverlog("Removing idle function %s at idleFinish" % str(function)) |
| 422 | remove_idle_func(function) |
| 423 | self.cooker.command.finishAsyncCommand(retval.msg) |
| 424 | nextsleep = None |
| 425 | elif retval is False: |
| 426 | serverlog("Removing idle function %s" % str(function)) |
| 427 | remove_idle_func(function) |
| 428 | nextsleep = None |
| 429 | elif retval is True: |
| 430 | nextsleep = None |
| 431 | elif isinstance(retval, float) and nextsleep: |
| 432 | if (retval < nextsleep): |
| 433 | nextsleep = retval |
| 434 | elif nextsleep is None: |
| 435 | continue |
| 436 | else: |
| 437 | fds = fds + retval |
| 438 | except SystemExit: |
| 439 | raise |
| 440 | except Exception as exc: |
| 441 | if not isinstance(exc, bb.BBHandledException): |
| 442 | logger.exception('Running idle function') |
| 443 | remove_idle_func(function) |
| 444 | serverlog("Exception %s broke the idle_thread, exiting" % traceback.format_exc()) |
| 445 | self.quit = True |
| 446 | |
| 447 | # Create new heartbeat event? |
| 448 | now = time.time() |
| 449 | if bb.event._heartbeat_enabled and now >= self.next_heartbeat: |
| 450 | # We might have missed heartbeats. Just trigger once in |
| 451 | # that case and continue after the usual delay. |
| 452 | self.next_heartbeat += self.heartbeat_seconds |
| 453 | if self.next_heartbeat <= now: |
| 454 | self.next_heartbeat = now + self.heartbeat_seconds |
| 455 | if hasattr(self.cooker, "data"): |
| 456 | heartbeat = bb.event.HeartbeatEvent(now) |
| 457 | try: |
| 458 | bb.event.fire(heartbeat, self.cooker.data) |
| 459 | except Exception as exc: |
| 460 | if not isinstance(exc, bb.BBHandledException): |
| 461 | logger.exception('Running heartbeat function') |
| 462 | serverlog("Exception %s broke in idle_thread, exiting" % traceback.format_exc()) |
| 463 | self.quit = True |
| 464 | if nextsleep and bb.event._heartbeat_enabled and now + nextsleep > self.next_heartbeat: |
| 465 | # Shorten timeout so that we we wake up in time for |
| 466 | # the heartbeat. |
| 467 | nextsleep = self.next_heartbeat - now |
| 468 | |
| 469 | if nextsleep is not None: |
| 470 | select.select(fds,[],[],nextsleep)[0] |
| 471 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 472 | def idle_commands(self, delay, fds=None): |
| 473 | nextsleep = delay |
| 474 | if not fds: |
| 475 | fds = [] |
| 476 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 477 | if not self.idle: |
| 478 | self.idle = threading.Thread(target=self.idle_thread) |
| 479 | self.idle.start() |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 480 | elif self.idle and not self.idle.is_alive(): |
| 481 | serverlog("Idle thread terminated, main thread exiting too") |
| 482 | bb.error("Idle thread terminated, main thread exiting too") |
| 483 | self.quit = True |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 484 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 485 | if nextsleep is not None: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 486 | if self.xmlrpc: |
| 487 | nextsleep = self.xmlrpc.get_timeout(nextsleep) |
| 488 | try: |
| 489 | return select.select(fds,[],[],nextsleep)[0] |
| 490 | except InterruptedError: |
| 491 | # Ignore EINTR |
| 492 | return [] |
| 493 | else: |
| 494 | return select.select(fds,[],[],0)[0] |
| 495 | |
| 496 | |
| 497 | class ServerCommunicator(): |
| 498 | def __init__(self, connection, recv): |
| 499 | self.connection = connection |
| 500 | self.recv = recv |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 501 | |
| 502 | def runCommand(self, command): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 503 | self.connection.send(command) |
| 504 | if not self.recv.poll(30): |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 505 | logger.info("No reply from server in 30s") |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 506 | if not self.recv.poll(30): |
| 507 | 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] | 508 | ret, exc = self.recv.get() |
| 509 | # Should probably turn all exceptions in exc back into exceptions? |
| 510 | # For now, at least handle BBHandledException |
| 511 | if exc and ("BBHandledException" in exc or "SystemExit" in exc): |
| 512 | raise bb.BBHandledException() |
| 513 | return ret, exc |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 514 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 515 | def updateFeatureSet(self, featureset): |
| 516 | _, error = self.runCommand(["setFeatures", featureset]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 517 | if error: |
| 518 | logger.error("Unable to set the cooker to the correct featureset: %s" % error) |
| 519 | raise BaseException(error) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 520 | |
| 521 | def getEventHandle(self): |
| 522 | handle, error = self.runCommand(["getUIHandlerNum"]) |
| 523 | if error: |
| 524 | logger.error("Unable to get UI Handler Number: %s" % error) |
| 525 | raise BaseException(error) |
| 526 | |
| 527 | return handle |
| 528 | |
| 529 | def terminateServer(self): |
| 530 | self.connection.send(['terminateServer']) |
| 531 | return |
| 532 | |
| 533 | class BitBakeProcessServerConnection(object): |
| 534 | def __init__(self, ui_channel, recv, eq, sock): |
| 535 | self.connection = ServerCommunicator(ui_channel, recv) |
| 536 | self.events = eq |
| 537 | # Save sock so it doesn't get gc'd for the life of our connection |
| 538 | self.socket_connection = sock |
| 539 | |
| 540 | def terminate(self): |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 541 | self.events.close() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 542 | self.socket_connection.close() |
| 543 | self.connection.connection.close() |
| 544 | self.connection.recv.close() |
| 545 | return |
| 546 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 547 | start_log_format = '--- Starting bitbake server pid %s at %s ---' |
| 548 | start_log_datetime_format = '%Y-%m-%d %H:%M:%S.%f' |
| 549 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 550 | class BitBakeServer(object): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 551 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 552 | def __init__(self, lock, sockname, featureset, server_timeout, xmlrpcinterface, profile): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 553 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 554 | self.server_timeout = server_timeout |
| 555 | self.xmlrpcinterface = xmlrpcinterface |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 556 | self.featureset = featureset |
| 557 | self.sockname = sockname |
| 558 | self.bitbake_lock = lock |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 559 | self.profile = profile |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 560 | self.readypipe, self.readypipein = os.pipe() |
| 561 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 562 | # Place the log in the builddirectory alongside the lock file |
| 563 | 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] | 564 | self.logfile = logfile |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 565 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 566 | startdatetime = datetime.datetime.now() |
| 567 | bb.daemonize.createDaemon(self._startServer, logfile) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 568 | self.bitbake_lock.close() |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 569 | os.close(self.readypipein) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 570 | |
| 571 | ready = ConnectionReader(self.readypipe) |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 572 | r = ready.poll(5) |
| 573 | if not r: |
| 574 | bb.note("Bitbake server didn't start within 5 seconds, waiting for 90") |
| 575 | r = ready.poll(90) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 576 | if r: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 577 | try: |
| 578 | r = ready.get() |
| 579 | except EOFError: |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 580 | # Trap the child exiting/closing the pipe and error out |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 581 | r = None |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 582 | if not r or r[0] != "r": |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 583 | ready.close() |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 584 | bb.error("Unable to start bitbake server (%s)" % str(r)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 585 | if os.path.exists(logfile): |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 586 | 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] | 587 | started = False |
| 588 | lines = [] |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 589 | lastlines = [] |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 590 | with open(logfile, "r") as f: |
| 591 | for line in f: |
| 592 | if started: |
| 593 | lines.append(line) |
| 594 | else: |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 595 | lastlines.append(line) |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 596 | res = logstart_re.search(line.rstrip()) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 597 | if res: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 598 | ldatetime = datetime.datetime.strptime(res.group(2), start_log_datetime_format) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 599 | if ldatetime >= startdatetime: |
| 600 | started = True |
| 601 | lines.append(line) |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 602 | if len(lastlines) > 60: |
| 603 | lastlines = lastlines[-60:] |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 604 | if lines: |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 605 | if len(lines) > 60: |
| 606 | 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] | 607 | else: |
| 608 | 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] | 609 | elif lastlines: |
| 610 | bb.error("Server didn't start, last 60 loglines (%s):\n%s" % (logfile, "".join(lastlines))) |
| 611 | else: |
| 612 | bb.error("%s doesn't exist" % logfile) |
| 613 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 614 | raise SystemExit(1) |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 615 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 616 | ready.close() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 617 | |
| 618 | def _startServer(self): |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 619 | os.close(self.readypipe) |
| 620 | os.set_inheritable(self.bitbake_lock.fileno(), True) |
| 621 | os.set_inheritable(self.readypipein, True) |
| 622 | serverscript = os.path.realpath(os.path.dirname(__file__) + "/../../../bin/bitbake-server") |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 623 | 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] | 624 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 625 | def execServer(lockfd, readypipeinfd, lockname, sockname, server_timeout, xmlrpcinterface, profile): |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 626 | |
| 627 | import bb.cookerdata |
| 628 | import bb.cooker |
| 629 | |
| 630 | serverlog(start_log_format % (os.getpid(), datetime.datetime.now().strftime(start_log_datetime_format))) |
| 631 | |
| 632 | try: |
| 633 | bitbake_lock = os.fdopen(lockfd, "w") |
| 634 | |
| 635 | # Create server control socket |
| 636 | if os.path.exists(sockname): |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 637 | serverlog("WARNING: removing existing socket file '%s'" % sockname) |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 638 | os.unlink(sockname) |
| 639 | |
| 640 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 641 | # AF_UNIX has path length issues so chdir here to workaround |
| 642 | cwd = os.getcwd() |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 643 | try: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 644 | os.chdir(os.path.dirname(sockname)) |
| 645 | sock.bind(os.path.basename(sockname)) |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 646 | finally: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 647 | os.chdir(cwd) |
| 648 | sock.listen(1) |
| 649 | |
| 650 | server = ProcessServer(bitbake_lock, lockname, sock, sockname, server_timeout, xmlrpcinterface) |
| 651 | writer = ConnectionWriter(readypipeinfd) |
| 652 | try: |
| 653 | featureset = [] |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 654 | cooker = bb.cooker.BBCooker(featureset, server) |
| 655 | cooker.configuration.profile = profile |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 656 | except bb.BBHandledException: |
| 657 | return None |
| 658 | writer.send("r") |
| 659 | writer.close() |
| 660 | server.cooker = cooker |
| 661 | serverlog("Started bitbake server pid %d" % os.getpid()) |
| 662 | |
| 663 | server.run() |
| 664 | finally: |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 665 | # Flush any messages/errors to the logfile before exit |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 666 | sys.stdout.flush() |
| 667 | sys.stderr.flush() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 668 | |
| 669 | def connectProcessServer(sockname, featureset): |
| 670 | # Connect to socket |
| 671 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 672 | # AF_UNIX has path length issues so chdir here to workaround |
| 673 | cwd = os.getcwd() |
| 674 | |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 675 | readfd = writefd = readfd1 = writefd1 = readfd2 = writefd2 = None |
| 676 | eq = command_chan_recv = command_chan = None |
| 677 | |
| 678 | sock.settimeout(10) |
| 679 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 680 | try: |
Brad Bishop | e2d5b61 | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 681 | try: |
| 682 | os.chdir(os.path.dirname(sockname)) |
Brad Bishop | f058f49 | 2019-01-28 23:50:33 -0500 | [diff] [blame] | 683 | finished = False |
| 684 | while not finished: |
| 685 | try: |
| 686 | sock.connect(os.path.basename(sockname)) |
| 687 | finished = True |
| 688 | except IOError as e: |
| 689 | if e.errno == errno.EWOULDBLOCK: |
| 690 | pass |
Richard Purdie | 3da1114 | 2019-02-05 21:34:37 +0000 | [diff] [blame] | 691 | raise |
Brad Bishop | e2d5b61 | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 692 | finally: |
| 693 | os.chdir(cwd) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 694 | |
| 695 | # Send an fd for the remote to write events to |
| 696 | readfd, writefd = os.pipe() |
| 697 | eq = BBUIEventQueue(readfd) |
| 698 | # Send an fd for the remote to recieve commands from |
| 699 | readfd1, writefd1 = os.pipe() |
| 700 | command_chan = ConnectionWriter(writefd1) |
| 701 | # Send an fd for the remote to write commands results to |
| 702 | readfd2, writefd2 = os.pipe() |
| 703 | command_chan_recv = ConnectionReader(readfd2) |
| 704 | |
| 705 | sendfds(sock, [writefd, readfd1, writefd2]) |
| 706 | |
| 707 | server_connection = BitBakeProcessServerConnection(command_chan, command_chan_recv, eq, sock) |
| 708 | |
| 709 | # Close the ends of the pipes we won't use |
| 710 | for i in [writefd, readfd1, writefd2]: |
| 711 | os.close(i) |
| 712 | |
| 713 | server_connection.connection.updateFeatureSet(featureset) |
| 714 | |
| 715 | except (Exception, SystemExit) as e: |
| 716 | if command_chan_recv: |
| 717 | command_chan_recv.close() |
| 718 | if command_chan: |
| 719 | command_chan.close() |
| 720 | for i in [writefd, readfd1, writefd2]: |
| 721 | try: |
Brad Bishop | e2d5b61 | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 722 | if i: |
| 723 | os.close(i) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 724 | except OSError: |
| 725 | pass |
| 726 | sock.close() |
| 727 | raise |
| 728 | |
| 729 | return server_connection |
| 730 | |
| 731 | def sendfds(sock, fds): |
| 732 | '''Send an array of fds over an AF_UNIX socket.''' |
| 733 | fds = array.array('i', fds) |
| 734 | msg = bytes([len(fds) % 256]) |
| 735 | sock.sendmsg([msg], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, fds)]) |
| 736 | |
| 737 | def recvfds(sock, size): |
| 738 | '''Receive an array of fds over an AF_UNIX socket.''' |
| 739 | a = array.array('i') |
| 740 | bytes_size = a.itemsize * size |
| 741 | msg, ancdata, flags, addr = sock.recvmsg(1, socket.CMSG_LEN(bytes_size)) |
| 742 | if not msg and not ancdata: |
| 743 | raise EOFError |
| 744 | try: |
| 745 | if len(ancdata) != 1: |
| 746 | raise RuntimeError('received %d items of ancdata' % |
| 747 | len(ancdata)) |
| 748 | cmsg_level, cmsg_type, cmsg_data = ancdata[0] |
| 749 | if (cmsg_level == socket.SOL_SOCKET and |
| 750 | cmsg_type == socket.SCM_RIGHTS): |
| 751 | if len(cmsg_data) % a.itemsize != 0: |
| 752 | raise ValueError |
| 753 | a.frombytes(cmsg_data) |
| 754 | assert len(a) % 256 == msg[0] |
| 755 | return list(a) |
| 756 | except (ValueError, IndexError): |
| 757 | pass |
| 758 | raise RuntimeError('Invalid data received') |
| 759 | |
| 760 | class BBUIEventQueue: |
| 761 | def __init__(self, readfd): |
| 762 | |
| 763 | self.eventQueue = [] |
| 764 | self.eventQueueLock = threading.Lock() |
| 765 | self.eventQueueNotify = threading.Event() |
| 766 | |
| 767 | self.reader = ConnectionReader(readfd) |
| 768 | |
| 769 | self.t = threading.Thread() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 770 | self.t.run = self.startCallbackHandler |
| 771 | self.t.start() |
| 772 | |
| 773 | def getEvent(self): |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 774 | with bb.utils.lock_timeout(self.eventQueueLock): |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 775 | if len(self.eventQueue) == 0: |
| 776 | return None |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 777 | |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 778 | item = self.eventQueue.pop(0) |
| 779 | if len(self.eventQueue) == 0: |
| 780 | self.eventQueueNotify.clear() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 781 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 782 | return item |
| 783 | |
| 784 | def waitEvent(self, delay): |
| 785 | self.eventQueueNotify.wait(delay) |
| 786 | return self.getEvent() |
| 787 | |
| 788 | def queue_event(self, event): |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 789 | with bb.utils.lock_timeout(self.eventQueueLock): |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 790 | self.eventQueue.append(event) |
| 791 | self.eventQueueNotify.set() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 792 | |
| 793 | def send_event(self, event): |
| 794 | self.queue_event(pickle.loads(event)) |
| 795 | |
| 796 | def startCallbackHandler(self): |
| 797 | bb.utils.set_process_name("UIEventQueue") |
| 798 | while True: |
| 799 | try: |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 800 | ready = self.reader.wait(0.25) |
| 801 | if ready: |
| 802 | event = self.reader.get() |
| 803 | self.queue_event(event) |
| 804 | except (EOFError, OSError, TypeError): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 805 | # Easiest way to exit is to close the file descriptor to cause an exit |
| 806 | break |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 807 | |
| 808 | def close(self): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 809 | self.reader.close() |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 810 | self.t.join() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 811 | |
| 812 | class ConnectionReader(object): |
| 813 | |
| 814 | def __init__(self, fd): |
| 815 | self.reader = multiprocessing.connection.Connection(fd, writable=False) |
| 816 | self.rlock = multiprocessing.Lock() |
| 817 | |
| 818 | def wait(self, timeout=None): |
| 819 | return multiprocessing.connection.wait([self.reader], timeout) |
| 820 | |
| 821 | def poll(self, timeout=None): |
| 822 | return self.reader.poll(timeout) |
| 823 | |
| 824 | def get(self): |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 825 | with bb.utils.lock_timeout(self.rlock): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 826 | res = self.reader.recv_bytes() |
| 827 | return multiprocessing.reduction.ForkingPickler.loads(res) |
| 828 | |
| 829 | def fileno(self): |
| 830 | return self.reader.fileno() |
| 831 | |
| 832 | def close(self): |
| 833 | return self.reader.close() |
| 834 | |
| 835 | |
| 836 | class ConnectionWriter(object): |
| 837 | |
| 838 | def __init__(self, fd): |
| 839 | self.writer = multiprocessing.connection.Connection(fd, readable=False) |
| 840 | self.wlock = multiprocessing.Lock() |
| 841 | # Why bb.event needs this I have no idea |
| 842 | self.event = self |
| 843 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 844 | def _send(self, obj): |
Patrick Williams | de0582f | 2022-04-08 10:23:27 -0500 | [diff] [blame] | 845 | gc.disable() |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 846 | with bb.utils.lock_timeout(self.wlock): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 847 | self.writer.send_bytes(obj) |
Patrick Williams | de0582f | 2022-04-08 10:23:27 -0500 | [diff] [blame] | 848 | gc.enable() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 849 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 850 | def send(self, obj): |
| 851 | obj = multiprocessing.reduction.ForkingPickler.dumps(obj) |
| 852 | # See notes/code in CookerParser |
| 853 | # We must not terminate holding this lock else processes will hang. |
| 854 | # For SIGTERM, raising afterwards avoids this. |
| 855 | # For SIGINT, we don't want to have written partial data to the pipe. |
| 856 | # pthread_sigmask block/unblock would be nice but doesn't work, https://bugs.python.org/issue47139 |
| 857 | process = multiprocessing.current_process() |
| 858 | if process and hasattr(process, "queue_signals"): |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 859 | with bb.utils.lock_timeout(process.signal_threadlock): |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 860 | process.queue_signals = True |
| 861 | self._send(obj) |
| 862 | process.queue_signals = False |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 863 | try: |
| 864 | for sig in process.signal_received.pop(): |
| 865 | process.handle_sig(sig, None) |
| 866 | except IndexError: |
| 867 | pass |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 868 | else: |
| 869 | self._send(obj) |
| 870 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 871 | def fileno(self): |
| 872 | return self.writer.fileno() |
| 873 | |
| 874 | def close(self): |
| 875 | return self.writer.close() |