Skip to content

Commit 6869494

Browse files
authored
Command: Pass the right flags to /dev/null (#449)
1 parent 6d548a0 commit 6869494

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

Sources/ContainerizationOS/Command.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ extension Command {
183183

184184
let set = try createFileset()
185185
defer {
186-
try? set.null.close()
186+
for nullHandle in set.nullHandles {
187+
try? nullHandle.close()
188+
}
187189
}
188190
var fds = [Int32](repeating: 0, count: set.handles.count)
189191
for (i, handle) in set.handles.enumerated() {
@@ -248,21 +250,22 @@ extension Command {
248250
}
249251

250252
/// Create a posix_spawn file actions set of fds to pass to the new process
251-
private func createFileset() throws -> (null: FileHandle, handles: [FileHandle]) {
252-
// grab dev null incase a handle passed by the user is nil
253-
let null = try openDevNull()
253+
private func createFileset() throws -> (nullHandles: [FileHandle], handles: [FileHandle]) {
254+
// grab dev null handles for different purposes
255+
let nullRead = try openDevNull(flags: O_RDONLY)
256+
let nullWrite = try openDevNull(flags: O_WRONLY)
254257
var files = [FileHandle]()
255-
files.append(stdin ?? null)
256-
files.append(stdout ?? null)
257-
files.append(stderr ?? null)
258+
files.append(stdin ?? nullRead)
259+
files.append(stdout ?? nullWrite)
260+
files.append(stderr ?? nullWrite)
258261
files.append(contentsOf: extraFiles)
259-
return (null: null, handles: files)
262+
return (nullHandles: [nullRead, nullWrite], handles: files)
260263
}
261264

262-
/// Returns a file handle to /dev/null.
263-
private func openDevNull() throws -> FileHandle {
264-
let fd = open("/dev/null", O_WRONLY, 0)
265-
guard fd > 0 else {
265+
/// Returns a file handle to /dev/null with the specified flags.
266+
private func openDevNull(flags: Int32) throws -> FileHandle {
267+
let fd = open("/dev/null", flags, 0)
268+
guard fd >= 0 else {
266269
throw POSIXError(.init(rawValue: errno)!)
267270
}
268271
return FileHandle(fileDescriptor: fd, closeOnDealloc: false)

0 commit comments

Comments
 (0)