---
name: "posix_spawn_file_actions_addclose"
section: "3"
platform: "macos"
package: "xnu"
version: "12377.121.6"
release: "macOS 26.5"
platforms: ["freebsd", "macos"]
url: "https://new.carta.tech/posix_spawn_file_actions_addclose"
---

# posix\_spawn\_file\_actions\_addclose(3)

add open or close actions to a posix\_spawn\_file\_actions\_t

## NAME

**posix\_spawn\_file\_actions\_addclose** **posix\_spawn\_file\_actions\_addopen** – add open or close actions to a *posix\_spawn\_file\_actions\_t*

## SYNOPSIS

**#include \<spawn.h\>**

*int*\

**posix\_spawn\_file\_actions\_addclose**(*posix\_spawn\_file\_actions\_t \*file\_actions*, *int filedes*);

*int*\

**posix\_spawn\_file\_actions\_addopen**(*posix\_spawn\_file\_actions\_t \*restrict file\_actions*, *int filedes*, *const char \*restrict path*, *int oflag*, *mode\_t mode*);

*int*\

**posix\_spawn\_file\_actions\_adddup2**(*posix\_spawn\_file\_actions\_t \*file\_actions*, *int filedes*, *int newfiledes*);

*int*\

**posix\_spawn\_file\_actions\_addinherit\_np**(*posix\_spawn\_file\_actions\_t \*file\_actions*, *int filedes*);

*int*\

**posix\_spawn\_file\_actions\_addchdir**(*posix\_spawn\_file\_actions\_t \*file\_actions*, *const char \*restrict path*);

*int*\

**posix\_spawn\_file\_actions\_addfchdir**(*posix\_spawn\_file\_actions\_t \*file\_actions*, *int filedes*);

## DESCRIPTION

The **posix\_spawn\_file\_actions\_addclose**() function adds a close operation to the list of operations associated with the object referenced by *file\_actions*, for subsequent use in a call to [posix\_spawn(2)](/posix_spawn/2.md) or  posix\_spawnp(2). The descriptor referred to by *filedes* is closed as if **close**() had been called on it prior to the new child process starting execution.

The **posix\_spawn\_file\_actions\_addopen**() function adds an open operation to the list of operations associated with the object referenced by *file\_actions*, for subsequent use in a call to [posix\_spawn(2)](/posix_spawn/2.md) or  posix\_spawnp(2). The descriptor referred to by *filedes* is opened using the *path*, *oflag*, and *mode* arguments as if **open**() had been called on it prior to the new child process starting execution.  The string *path* is copied by the **posix\_spawn\_file\_actions\_addopen**() function during this process, so storage need not be persistent in the caller.

The **posix\_spawn\_file\_actions\_adddup2**() function adds a dup2 operation to the list of operations associated with the object referenced by *file\_actions*, for subsequent use in a call to [posix\_spawn(2)](/posix_spawn/2.md) or  posix\_spawnp(2). The descriptor referred to by *newfiledes* is created as if **dup2**() had been called on *filedes* prior to the new child process starting execution.

The **posix\_spawn\_file\_actions\_addinherit\_np**() function adds an abstract inheritance operation to the list of operations associated with the object referenced by *file\_actions*, for subsequent use in a call to [posix\_spawn(2)](/posix_spawn/2.md) or posix\_spawnp(2). The pre-existing descriptor referred to by *filedes* is marked for inheritance into the new process image, and the *FD\_CLOEXEC* flag is cleared from the file descriptor in the new process image.

Normally, for [posix\_spawn(2)](/posix_spawn/2.md) and posix\_spawnp(2), all file descriptors are inherited from the parent process into the spawned process, except for those explicitly marked as close-on-exec.  However if the flag *POSIX\_SPAWN\_CLOEXEC\_DEFAULT* is set, then during the spawn operation, all pre-existing file descriptors in the parent process are treated as if they had been marked close-on-exec i.e. none of them are automatically inherited.  See [posix\_spawnattr\_setflags(3)](/posix_spawnattr_setflags/3.md). Only file descriptors explicitly manipulated via  *file\_actions* are made available in the spawned process. In that case, **posix\_spawn\_file\_actions\_addinherit\_np**() can be used to make specific pre-existing file descriptors from the parent process be available in the spawned process.

The **posix\_spawn\_file\_actions\_addchdir**() function adds an chdir operation to the list of operations associated with the object referenced by *file\_actions*, for subsequent use in a call to [posix\_spawn(2)](/posix_spawn/2.md) or  posix\_spawnp(2). The current working directory will be set as if **chdir**() had been called with *path* prior to the new child process starting execution.

The **posix\_spawn\_file\_actions\_addfchdir**() function adds a fchdir operation to the list of operations associated with the object referenced by *file\_actions*, for subsequent use in a call to [posix\_spawn(2)](/posix_spawn/2.md) or  posix\_spawnp(2). The current working directory will be set as if **fchdir**() had been called with *filedes* prior to the new child process starting execution. When *POSIX\_SPAWN\_CLOEXEC\_DEFAULT* is set, the file descriptor *filedes* will not be automatically inherited unless an explicit **posix\_spawn\_file\_actions\_addinherit\_np**() action for *filedes* has been added.

## RETURN VALUES

On success, these functions return 0; on failure they return an error number from ⟨errno.h⟩.

## ERRORS

These functions may fail if:

- \[EBADF\]

  The value specified by *filedes* is negative or would cause the process to exceed the maximum number of open files it is allowed.

- \[EINVAL\]

  The value of *file\_actions* is invalid.

- \[ENAMETOOLONG\]

  The length of the value specified by *path* exceeds PATH\_MAX.

- \[ENOMEM\]

  Insufficient memory was available to add the new action to *file\_actions*.

## SEE ALSO

[posix\_spawn(2)](/posix_spawn/2.md), posix\_spawnp(2), [posix\_spawn\_file\_actions\_init(3)](/posix_spawn_file_actions_init/3.md), [posix\_spawn\_file\_actions\_destroy(3)](/posix_spawn_file_actions_destroy/3.md), [posix\_spawnattr\_setflags(3)](/posix_spawnattr_setflags/3.md).

## STANDARDS

Version 3 of the Single UNIX Specification (“SUSv3”)

## HISTORY

The **posix\_spawn\_file\_actions\_init**() and **posix\_spawn\_file\_actions\_destroy**() function calls appeared in Version 3 of the Single UNIX Specification (“SUSv3”).
