Skip to content

Add support for dup/dup2 #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2021
Merged

Add support for dup/dup2 #28

merged 1 commit into from
Feb 25, 2021

Conversation

lorentey
Copy link
Member

This adds support for duplicating file descriptors via a FileDescriptor.duplicate method:

extension FileDescriptor {
  public func duplicate(
      as target: FileDescriptor? = nil,
      retryOnInterrupt: Bool = true
    ) throws -> FileDescriptor
}

The method dispatches to either dup or dup2 based on whether or not it has an explicit target.

Usually, a non-nil target will be one of the file descriptors conventionally reserved for the stdin/stdout/stderr role; these were previously assigned symbolic names in #21. To target other file descriptors, users will need to use the .init(rawValue:) initializer. (This will rarely be needed, but some like to pass additional communication channels down to child processes using some custom numbering convention beyond the universal 0/1/2.)

let controlFd = try fd.duplicate(as: FileDescriptor(rawValue: 3))

@lorentey lorentey requested a review from milseman February 24, 2021 21:42
Copy link
Contributor

@milseman milseman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@milseman milseman merged commit 37e23f2 into apple:main Feb 25, 2021
@milseman milseman mentioned this pull request Feb 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants