linux-sysprog · advanced · ~10 min
The clone-namespace flag set.
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)Each unshare flag isolates a specific kernel resource. Knowing which flag controls mount visibility is the foundation of every container builder.
Integer flags.
0/1 for each.
Bit math.
int unshares_mount(int flags) { /* TODO */ (void)flags; return 0; }
int has_any_namespace(int flags) { /* TODO */ (void)flags; return 0; }
Confusing CLONE_NEWNS (mount) with CLONE_NEWNET (network).
Flags == 0; all flags set.
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.