syscalls

epoll_ctl() — Linux system interface

Modify the epoll interest list using EPOLL_CTL_ADD, MOD or DEL. ADD and MOD copy the event mask and application data; DEL ignores event on current Linux. Duplicate ADD gives EEXIST; modifying or deleting a missing registration can give ENOENT. Some descriptors such as ordinary files do not support epoll. Explicitly remove registrations as needed, especially with duplicated descriptors; closing a single duplicate may not remove the underlying registration. Compile the complete C example in a fresh private working directory using gcc -std=c17 -Wall -Wextra -Wpedantic -Werror example.c -o example; run ./example. Linux-specific interface or flags; tested on Linux/glibc only. These are libc-facing signatures, not raw architecture-specific syscall ABIs.

Formal Syntax & Structure

linux syntaxlinux
#include <sys/epoll.h>int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);

Parameters & Attributes

NameTypeDescription
epfdintEpoll instance descriptor.
opintADD, MOD or DEL operation.
fdintTarget descriptor.
eventstruct epoll_event *Mask and data for ADD/MOD; NULL allowed for DEL.

Return Value

int

0 on success. On failure, returns -1 and sets errno.

Working Example

Example Snippetlinux
#define _GNU_SOURCE#define _POSIX_C_SOURCE 200809L#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/epoll.h>
/* A failed check ends this short-lived demonstration. */#define CHECK(x) do { if (!(x)) { \    fprintf(stderr, "check failed at line %d: %s\n", __LINE__, #x); \    return EXIT_FAILURE; \} } while (0)
int main(void){    int f[2]; CHECK(pipe(f) == 0);    int e = epoll_create1(EPOLL_CLOEXEC); CHECK(e >= 0);    struct epoll_event ev = { .events=EPOLLIN, .data.u64=7 };    CHECK(epoll_ctl(e, EPOLL_CTL_ADD, f[0], &ev) == 0);    CHECK(epoll_ctl(e, EPOLL_CTL_ADD, f[0], &ev) == -1 && errno == EEXIST);    ev.data.u64 = 9;    CHECK(epoll_ctl(e, EPOLL_CTL_MOD, f[0], &ev) == 0);    CHECK(write(f[1], "e", 1) == 1);    CHECK(epoll_wait(e, &ev, 1, 1000) == 1 && ev.data.u64 == 9);    CHECK(epoll_ctl(e, EPOLL_CTL_DEL, f[0], NULL) == 0);    CHECK(epoll_wait(e, &ev, 1, 0) == 0);    CHECK(close(f[0]) == 0 && close(f[1]) == 0 && close(e) == 0);    puts("ADD; EEXIST; MOD data=9; DEL");    return EXIT_SUCCESS;}
Linux Reference