Skip to content
BSD syscall#92

svc · unix #92

fcntl

Multiplexed file descriptor control call covering descriptor duplication, flag manipulation, locking and macOS-specific operations.

Prototype

int fcntl(int fd, int cmd, long arg);

Returns: int

Arguments

NameTypeDirDescription
fdint-
cmdint-
arglong-

Version history

XNU tagmacOS#
xnu-1456.1.26macOS 10.6 Snow Leopard92
xnu-1486.2.1192
xnu-1504.15.392
xnu-1504.3.1292
xnu-1504.7.492
xnu-1504.9.1792
xnu-1504.9.2692
xnu-1504.9.3792

Examples

C — set close-on-exec

int fl = fcntl(fd, F_GETFD);
fcntl(fd, F_SETFD, fl | FD_CLOEXEC);

C — durable flush

if (fcntl(fd, F_FULLFSYNC) == -1)
    fsync(fd); // fall back

Notes

macOS extends POSIX fcntl with a large set of F_* commands: F_PREALLOCATE, F_PUNCHHOLE, F_FULLFSYNC, F_NOCACHE, F_GETPATH, F_BARRIERFSYNC, F_SETBACKINGSTORE and many more. F_FULLFSYNC is required when callers need data on stable media; plain fsync only flushes to the drive cache. Locking commands F_GETLK/F_SETLK/F_SETLKW implement advisory POSIX locks per fileglob.

Detection

syscall::fcntl:entry in DTrace exposes the cmd argument. Endpoint Security does not break down fcntl variants, but holes punched via F_PUNCHHOLE leave VFS write events.

Related APIs

openopenatflockfsyncguarded_open_npdup