|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// Copyright © 2025 Apple Inc. and the Containerization project authors. All rights reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +import ContainerizationError |
| 18 | +import ContainerizationOS |
| 19 | +import Foundation |
| 20 | +import Logging |
| 21 | +import Synchronization |
| 22 | + |
| 23 | +final class IOPair: Sendable { |
| 24 | + let readFrom: IOCloser |
| 25 | + let writeTo: IOCloser |
| 26 | + nonisolated(unsafe) let buffer: UnsafeMutableBufferPointer<UInt8> |
| 27 | + private let logger: Logger? |
| 28 | + |
| 29 | + private let done: Atomic<Bool> |
| 30 | + |
| 31 | + init(readFrom: IOCloser, writeTo: IOCloser, logger: Logger? = nil) { |
| 32 | + self.readFrom = readFrom |
| 33 | + self.writeTo = writeTo |
| 34 | + self.done = Atomic(false) |
| 35 | + self.buffer = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: Int(getpagesize())) |
| 36 | + self.logger = logger |
| 37 | + } |
| 38 | + |
| 39 | + func relay() throws { |
| 40 | + let readFromFd = self.readFrom.fileDescriptor |
| 41 | + let writeToFd = self.writeTo.fileDescriptor |
| 42 | + |
| 43 | + let readFrom = OSFile(fd: readFromFd) |
| 44 | + let writeTo = OSFile(fd: writeToFd) |
| 45 | + |
| 46 | + try ProcessSupervisor.default.poller.add(readFromFd, mask: EPOLLIN) { mask in |
| 47 | + if mask.isHangup && !mask.readyToRead { |
| 48 | + self.close() |
| 49 | + return |
| 50 | + } |
| 51 | + // Loop so that in the case that someone wrote > buf.count down the pipe |
| 52 | + // we properly will drain it fully. |
| 53 | + while true { |
| 54 | + let r = readFrom.read(self.buffer) |
| 55 | + if r.read > 0 { |
| 56 | + let view = UnsafeMutableBufferPointer( |
| 57 | + start: self.buffer.baseAddress, |
| 58 | + count: r.read |
| 59 | + ) |
| 60 | + |
| 61 | + let w = writeTo.write(view) |
| 62 | + if w.wrote != r.read { |
| 63 | + self.logger?.error("stopping relay: short write for stdio") |
| 64 | + self.close() |
| 65 | + return |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + switch r.action { |
| 70 | + case .error(let errno): |
| 71 | + self.logger?.error("failed with errno \(errno) while reading for fd \(readFromFd)") |
| 72 | + fallthrough |
| 73 | + case .eof: |
| 74 | + self.close() |
| 75 | + self.logger?.debug("closing relay for \(readFromFd)") |
| 76 | + return |
| 77 | + case .again: |
| 78 | + // We read all we could, exit. |
| 79 | + if mask.isHangup { |
| 80 | + self.close() |
| 81 | + } |
| 82 | + return |
| 83 | + default: |
| 84 | + break |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + func close() { |
| 91 | + guard |
| 92 | + self.done.compareExchange( |
| 93 | + expected: false, |
| 94 | + desired: true, |
| 95 | + successOrdering: .acquiringAndReleasing, |
| 96 | + failureOrdering: .acquiring |
| 97 | + ).exchanged |
| 98 | + else { |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + self.buffer.deallocate() |
| 103 | + |
| 104 | + let readFromFd = self.readFrom.fileDescriptor |
| 105 | + // Remove the fd from our global epoll instance first. |
| 106 | + do { |
| 107 | + try ProcessSupervisor.default.poller.delete(readFromFd) |
| 108 | + } catch { |
| 109 | + self.logger?.error("failed to delete fd from epoll \(readFromFd): \(error)") |
| 110 | + } |
| 111 | + |
| 112 | + do { |
| 113 | + try self.readFrom.close() |
| 114 | + } catch { |
| 115 | + self.logger?.error("failed to close reader fd for IOPair: \(error)") |
| 116 | + } |
| 117 | + |
| 118 | + do { |
| 119 | + try self.writeTo.close() |
| 120 | + } catch { |
| 121 | + self.logger?.error("failed to close writer fd for IOPair: \(error)") |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments