linux-sysprog · intermediate · ~15 min

Async-signal-safe function?

Recall which calls are legal inside a signal handler.

Challenge

Only async-signal-safe functions may be called from inside a signal handler; functions that use locks or global state (like printf, malloc) can deadlock or corrupt. Classify a function name against a safe allowlist.

Task

Implement int is_async_signal_safe(const char *fn) that returns 1 if fn names an async-signal-safe function, else 0.

Input

  • fn: a function name string.

Output

1 for "write", "_exit", "signal", "kill", "read"; 0 for anything else (e.g. "printf", "malloc", "free").

Example

is_async_signal_safe("write")    ->   1
is_async_signal_safe("_exit")    ->   1
is_async_signal_safe("printf")   ->   0
is_async_signal_safe("malloc")   ->   0

Input format

fn: a function name string.

Output format

1 if fn is in the safe allowlist, else 0.

Constraints

Safe set: write, _exit, signal, kill, read.

Starter code

#include <string.h>

int is_async_signal_safe(const char *fn) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.