linux-sysprog · advanced · ~10 min
The clone-namespace flag set.
Inspect an unshare/clone flags bitmask to see which namespaces it requests. This is pure bit-testing — no namespaces are created.
Implement two predicates over a flags bitmask:
int unshares_mount(int flags);
int has_any_namespace(int flags);
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)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.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)
flags == 0: both return 0.has_any_namespace.Each unshare flag isolates a specific kernel resource. Knowing which flag controls mount visibility is the foundation of every container builder.
An integer flags bitmask built from the CLONE_NEW* namespace constants.
unshares_mount: 1 if CLONE_NEWNS (0x00020000) is set. has_any_namespace: 1 if any of the six namespace bits is set.
Test bits with &. Only the six listed namespace flags count for has_any_namespace.
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.