Skip to content
BSD syscall#244

svc · unix #244

posix_spawn

Creates a new process running a specified program in a single atomic syscall.

Prototype

int posix_spawn(pid_t *pid, const char *path, const struct _posix_spawn_args_desc *adesc, char **argv, char **envp);

Returns: int

Arguments

NameTypeDirDescription
pidpid_t-
pathconst char-
adescconst struct _posix_spawn_args_desc-
argvchar-
envpchar-

Version history

XNU tagmacOS#
xnu-1456.1.26macOS 10.6 Snow Leopard244
xnu-1486.2.11244
xnu-1504.15.3244
xnu-1504.3.12244
xnu-1504.7.4244
xnu-1504.9.17244
xnu-1504.9.26244
xnu-1504.9.37244

Examples

Spawn with CLOEXEC default

posix_spawnattr_t attr;
posix_spawnattr_init(&attr);
posix_spawnattr_setflags(&attr, POSIX_SPAWN_CLOEXEC_DEFAULT);
pid_t pid;
char *argv[] = {"/bin/echo", "hi", NULL};
posix_spawn(&pid, argv[0], NULL, &attr, argv, environ);
posix_spawnattr_destroy(&attr);

Notes

Apple's preferred process-creation primitive. Internally it bypasses fork() entirely, building the child task and image with a dedicated kernel path, which is why it is safe in apps that link Cocoa. File actions and attribute objects allow fd dup/close, signal masks, and macOS-specific extensions (POSIX_SPAWN_CLOEXEC_DEFAULT, POSIX_SPAWN_START_SUSPENDED, persona attributes). libdispatch, launchd, and NSTask are all layered on top.

Detection

Endpoint Security raises ES_EVENT_TYPE_NOTIFY_EXEC for spawned processes just like execve. DTrace probe syscall::posix_spawn:entry exposes the path argument and attribute pointer.

Related APIs

forkvforkexecvewait4waitidexit