linux-sysprog · advanced · ~10 min

Check the mount-namespace flag

The clone-namespace flag set.

Challenge

unshare(int flags) takes a bitmask. The mount-namespace flag is CLONE_NEWNS = 0x00020000. Implement int unshares_mount(int flags) returning 1 if the flag is set in flags, 0 otherwise.

Also implement int has_any_namespace(int flags) returning 1 if ANY of these well-known namespace flags is set:

  • CLONE_NEWNS 0x00020000 (mount)
  • CLONE_NEWUTS 0x04000000 (hostname)
  • CLONE_NEWIPC 0x08000000 (IPC)
  • CLONE_NEWUSER 0x10000000 (uid mapping)
  • CLONE_NEWPID 0x20000000 (pid)
  • CLONE_NEWNET 0x40000000 (network)

Why this matters

Each unshare flag isolates a specific kernel resource. Knowing which flag controls mount visibility is the foundation of every container builder.

Input format

Integer flags.

Output format

0/1 for each.

Constraints

Bit math.

Starter code

int unshares_mount(int flags) { /* TODO */ (void)flags; return 0; }
int has_any_namespace(int flags) { /* TODO */ (void)flags; return 0; }

Common mistakes

Confusing CLONE_NEWNS (mount) with CLONE_NEWNET (network).

Edge cases to handle

Flags == 0; all flags set.

Complexity

O(1).

Background lessons

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