linux-sysprog · advanced · ~10 min

Check the mount-namespace flag

The clone-namespace flag set.

Challenge

Inspect an unshare/clone flags bitmask to see which namespaces it requests. This is pure bit-testing — no namespaces are created.

Task

Implement two predicates over a flags bitmask:

int unshares_mount(int flags);
int has_any_namespace(int flags);

Input

  • flags: a bitmask built from these namespace flag constants:
    • 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)

Output

  • unshares_mount returns 1 if the mount-namespace bit (CLONE_NEWNS) is set, else 0.
  • has_any_namespace returns 1 if any of the six listed namespace bits is set, else 0.

Example

unshares_mount(0x00020000)              ->   1
unshares_mount(0x00010000)              ->   0   (different bit)
unshares_mount(0x00020000 | 0x40000000) ->   1
has_any_namespace(0)                    ->   0
has_any_namespace(0x04000000)           ->   1   (UTS)
has_any_namespace(0x10000000)           ->   1   (USER)

Edge cases

  • flags == 0: both return 0.
  • A bit outside the listed set does not count for has_any_namespace.

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

An integer flags bitmask built from the CLONE_NEW* namespace constants.

Output format

unshares_mount: 1 if CLONE_NEWNS (0x00020000) is set. has_any_namespace: 1 if any of the six namespace bits is set.

Constraints

Test bits with &. Only the six listed namespace flags count for has_any_namespace.

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.