apple / apple/swift-system

Large parts of (e.g.) `sched.h` not accessible without `_GNU_SOURCE`

Open
#229 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
1.4k
Forks
154
PR merge metrics
No merged PRs in 30d

Description

I'm experimenting with using Swift for some low(ish)-level Linux system programming, and I'd like to invoke the [unshare(2)][unshare(2)] system call to create a new namespace.

`unshare` is normally exposed in C by `sched.h` (ultimately `bits/sched.h`). CSystem on Linux [includes `sched.h`][CSystemLinux.h]; however, as it doesn't define `_GNU_SOURCE`, most of `bits/sched.h` is not exposed, including the namespace (containerisation) functions `clone`, `unshare`, and `setns` (and their associated `CLONE_*` flags).

I understand the primary purpose of CSystem is to expose functionality that is then wrapped by System; and maybe one day System on Linux will include a wrapper for Linux namespaces: they're a compelling feature for the platform. Unfortunately, I'm not yet experienced enough with Swift to volunteer to write one.

In any case, would it be possible to `#define _GNU_SOURCE` in CSystem on Linux? What problems (if any) could this cause?

Other desirable system interfaces, often including safer alternatives to standard interfaces, are also gated behind it, e.g. `get_current_dir_name`, which was suggested for use in System in https://github.com/apple/swift-system/pull/71#issuecomment-968160476.

[unshare(2)]: https://manpages.debian.org/bookworm/manpages-dev/unshare.2.en.html
[CSystemLinux.h]: https://github.com/apple/swift-system/blob/main/Sources/CSystem/include/CSystemLinux.h

Contributor guide

Open the contributing guide

Research direction

Start with Sources/CSystem/include/CSystemLinux.h and the sched.h inclusion described in the issue. Check how defining _GNU_SOURCE affects the exposed namespace APIs, CLONE_* flags, and other gated interfaces, then verify that CSystem still builds cleanly on Linux without causing compatibility problems.

Written by the indexing model from the issue text.

Assessment

Tech stack
linux, swift
Domain
operating-systems
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.