---
name: "fileport_makefd"
section: "2"
platform: "macos"
package: "xnu"
version: "12377.121.6"
release: "macOS 26.5"
platforms: ["macos"]
url: "https://new.carta.tech/fileport_makeport"
---

# fileport\_makefd(2)

manipulate fileports

Part of [fileport\_makeport(2)](/fileport_makeport/2.md).

## NAME

**fileport\_makeport**, **fileport\_makefd** – manipulate fileports

## SYNOPSIS

**#include \<sys/fileport.h\>**

*int*\

**fileport\_makeport**(*int fildes*, *fileport\_t \*port*);

*int*\

**fileport\_makefd**(*fileport\_t port*);

## DESCRIPTION

A fileport is a flavor of Mach port that implicitly contains a reference to an existing open file description. The **fileport\_makeport**() interface creates a new fileport from an open file descriptor *fildes*, while **fileport\_makefd**() creates a new file descriptor from a fileport.

A valid fileport may be sent from one process to another via Mach IPC. Once the sending process has successfully created a fileport from a file descriptor with **fileport\_makeport**(), it may immediately close the descriptor with [close(2)](/close/2.md), or use it to share the open file description with the receiver.

The receiving process should use **fileport\_makefd**() to create a new file descriptor from the received fileport; that descriptor will reference the same underlying open file description as the sending process. The new descriptor is created with the close-on-exec file descriptor flag enabled.

Further file descriptors can be created from the fileport using **fileport\_makefd**() until either the fileport is deallocated or a descriptor limit is exceeded.

## NOTES

Fileports are a low-level primitive, and Mach IPC can be complex. Developers who need to move file descriptors between processes are advised to use XPC and the corresponding xpc\_fd\_create(3) and xpc\_fd\_dup(3) interfaces instead.

Certain special types of open file descriptions, e.g. a kqueue, cannot be sent between processes; **fileport\_makeport**() will return an error for those descriptors.

The effect of using these primitives as a file descriptor IPC mechanism, as outlined above, has the same sharing semantics as other file descriptor IPC mechanisms on the platform: it is as if the original file descriptor from the sending process has been duplicated, in the sense of [dup(2)](/dup/2.md), into the receiving process.

## RETURN VALUES

If successful, **fileport\_makefd**() returns a non-negative integer. If successful, **fileport\_makeport**() returns 0. Otherwise on failure both interfaces return -1 and the global variable *errno* is set to indicate the error.

## ERRORS

The **fileport\_makeport**() system call will fail if:

- \[EBADF\]

  *fildes* is not a valid file descriptor.

- \[EINVAL\]

  The file descriptor cannot be sent to another procss.

- \[EFAULT\]

  Fileport address is invalid.

- \[EAGAIN\]

  Temporary resource shortage.

The **fileport\_makefd**() system call will fail if:

- \[EINVAL\]

  The fileport is invalid.

- \[EMFILE\]

  The process has reached its open file descriptor limit.

- \[ENFILE\]

  Unable to extend the file table

- \[ENOMEM\]

  Insufficient memory to allocate a descriptor.

## SEE ALSO

xpc(3), xpc\_fd\_create(3), xpc\_fd\_dup(3), [dup(2)](/dup/2.md), [getrlimit(2)](/getrlimit/2.md), [sendmsg(2)](/sendmsg/2.md), [recvmsg(2)](/recvmsg/2.md), [kqueue(2)](/kqueue/2.md)
