Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Sources/ContainerizationOS/Terminal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,15 @@ extension Terminal {

extension Terminal {
/// Enable raw mode for the pty.
public func setraw() throws {
/// - Parameter preserveSignalGeneration: Keep `ISIG` enabled so control
/// characters such as Ctrl-C still signal the foreground process.
/// - Throws: An error if the terminal attributes cannot be read or applied.
public func setraw(preserveSignalGeneration: Bool = false) throws {
var attr = try Self.getattr(descriptor)
cfmakeraw(&attr)
if preserveSignalGeneration {
attr.c_lflag |= tcflag_t(ISIG)
}
attr.c_oflag = attr.c_oflag | tcflag_t(OPOST)
try fromSyscall(tcsetattr(descriptor, TCSANOW, &attr))
}
Expand Down
86 changes: 86 additions & 0 deletions Tests/ContainerizationOSTests/TerminalRawModeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//===----------------------------------------------------------------------===//
// Copyright © 2026 Apple Inc. and the Containerization project authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===----------------------------------------------------------------------===//

import Foundation
import Testing

@testable import ContainerizationOS

#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#endif

@Suite("Terminal raw mode tests")
struct TerminalRawModeTests {
private func termiosAttributes(of descriptor: Int32) throws -> termios {
var attrs = termios()
try #require(tcgetattr(descriptor, &attrs) == 0, "tcgetattr failed, errno: \(errno)")
return attrs
}

private func has(_ flag: tcflag_t, in flags: tcflag_t) -> Bool {
flags & flag != 0
}

@Test("pty slave is in canonical mode before raw-mode setup")
func ptySlaveIsCanonicalBeforeSetup() throws {
let (parent, child) = try Terminal.create(initialSize: Terminal.Size(width: 120, height: 40))
defer {
try? parent.close()
try? child.close()
}

// A freshly allocated pty slave is canonical by default (ICANON set).
let attrs = try termiosAttributes(of: child.handle.fileDescriptor)
#expect(has(tcflag_t(ICANON), in: attrs.c_lflag))
}

@Test("setraw clears ICANON and preserves OPOST")
func setrawClearsCanonicalAndPreservesOutputPostProcessing() throws {
let (parent, child) = try Terminal.create(initialSize: Terminal.Size(width: 120, height: 40))
defer {
try? parent.close()
try? child.close()
}

try child.setraw()

let attrs = try termiosAttributes(of: child.handle.fileDescriptor)
#expect(!has(tcflag_t(ICANON), in: attrs.c_lflag), "setraw must clear canonical mode")
#expect(!has(tcflag_t(ISIG), in: attrs.c_lflag), "setraw must clear ISIG by default")
#expect(has(tcflag_t(OPOST), in: attrs.c_oflag), "setraw must preserve OPOST for output post-processing")
#expect(has(tcflag_t(ONLCR), in: attrs.c_oflag), "setraw must preserve ONLCR for CRLF output translation")
}

@Test("setraw can preserve terminal signal generation")
func setrawCanPreserveSignalGeneration() throws {
let (parent, child) = try Terminal.create(initialSize: Terminal.Size(width: 120, height: 40))
defer {
try? parent.close()
try? child.close()
}

try child.setraw(preserveSignalGeneration: true)

let attrs = try termiosAttributes(of: child.handle.fileDescriptor)
#expect(!has(tcflag_t(ICANON), in: attrs.c_lflag), "raw input must remain non-canonical")
#expect(has(tcflag_t(ISIG), in: attrs.c_lflag), "Ctrl-C must continue to generate SIGINT")
}
}
10 changes: 10 additions & 0 deletions vminitd/Sources/vmexec/Console.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
//===----------------------------------------------------------------------===//

import ContainerizationOS
import FoundationEssentials
import LCShim

Expand Down Expand Up @@ -55,6 +56,15 @@ class Console {
}
defer { _ = _close(slaveFD) }

// Put the pty slave into raw mode so the tty line discipline does not
// buffer or truncate large output writes (the container's stdout goes
// through this slave). `Terminal.setraw()` applies cfmakeraw and keeps
// OPOST, so newline translation to CRLF on output is preserved. Keep
// ISIG enabled because cctl forwards terminal bytes, and guest Ctrl-C
// therefore depends on the slave line discipline generating SIGINT.
try Terminal(descriptor: slaveFD, setInitState: false)
.setraw(preserveSignalGeneration: true)

for fd: Int32 in 0...2 {
guard dup3(slaveFD, fd, 0) != -1 else {
throw App.Errno(stage: "dup3")
Expand Down