linux-sysprog · intermediate · ~15 min

Is the handler body safe?

Enforce the 'only set a flag' handler discipline.

Challenge

The safe signal-handler discipline is: set a flag and return, then do the real work in the main loop. Check a handler's op trace against that rule.

Task

Implement int handler_is_safe(const char *ops) that returns 1 only if every op in the trace is safe.

Input

  • ops: a string of handler ops, where 's' = set a volatile sig_atomic_t flag (safe) and 'p' = call printf (unsafe).

Output

1 if every op is 's'; 0 if any op is not 's'.

Example

handler_is_safe("sss")   ->   1
handler_is_safe("sps")   ->   0   (contains an unsafe printf)
handler_is_safe("")      ->   1   (no unsafe ops)

Edge cases

  • Empty trace: return 1.

Input format

ops: string of handler ops ('s'=set flag, 'p'=printf).

Output format

1 if every op is 's', else 0.

Constraints

Any op other than 's' is unsafe.

Starter code

int handler_is_safe(const char *ops) {
    /* TODO */
    return 1;
}

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