swift-system icon indicating copy to clipboard operation
swift-system copied to clipboard

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

Open felixphew opened this issue 8 months ago • 1 comments

I'm experimenting with using Swift for some low(ish)-level Linux system programming, and I'd like to invoke the 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; 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.

felixphew avatar May 06 '25 02:05 felixphew

This might have to be a change made to how Glibc is imported. This issue has been sporadically discussed for quite some time, e.g. https://forums.swift.org/t/defining-gnu-source-for-module-map-included-headers/679. The forums have other threads about this as well.

glessard avatar Oct 09 '25 20:10 glessard