Skip to content

fix(ulimit): compile against musl and OpenHarmony libcs - #449

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/ulimit-musl-ohos
Oct 9, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/ulimit-musl-ohos

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

背景

rubash 现在能不能跑在 HarmonyOS 上,卡点只剩 ulimit 一个文件。这一版把那个卡点拆掉了。

问题

src/builtins/ulimit.rs 里这两处的资源选择器写死了 libc::__rlimit_resource_t:

#[cfg(target_os = "linux")]
fn resource(option: char) -> libc::__rlimit_resource_t { ... }

#[cfg(target_os = "linux")]
fn rlimit_get(res: libc::__rlimit_resource_t) -> io::Result<(u64, u64)> { ... }

这个 typedef 只有 glibc 有:

  • glibc:pub type __rlimit_resource_t = c_uint;(libc/src/unix/linux_like/linux/gnu/mod.rs:6),getrlimit/setrlimit 收 c_uint
  • musl / OpenHarmony libc:根本没有这个类型,getrlimit/setrlimit 直接收 c_int

libc 自己在 linux_like/linux/arch/generic/mod.rs 里就是按 target_env 分家的——RLIMIT_CPU 在 gnu 下是 crate::__rlimit_resource_t(L282),在非 gnu 下是 c_int(L302)。

所以 target_os = "linux" 这层 cfg 拦不住 musl/ohos:它们确实是 target_os = "linux"(target_env = "gnu" 才是 glibc 的判据),于是走进函数体,然后找不到类型。

修法

加一个按 target_env 分家的类型别名,两处引用都走它:

#[cfg(all(target_os = "linux", target_env = "gnu"))]
type RlimitResource = libc::__rlimit_resource_t;

#[cfg(all(target_os = "linux", not(target_env = "gnu")))]
type RlimitResource = libc::c_int;

+11 / −2,只碰这一个文件。

验证

三平台 cargo check 全绿:

target 修前 修后
x86_64-unknown-linux-gnu(宿主) 绿 绿
x86_64-unknown-linux-musl 2 errors 绿
aarch64-unknown-linux-ohos 2 errors 绿

修前的两个报错都是 cannot find type __rlimit_resource_t in crate libc(resource / rlimit_get 各一)。

中间踩过一个坑记录一下:第一版我图省事直接全局 sed s/__rlimit_resource_t/c_int/,musl/ohos 是绿了,宿主 glibc 反过来炸了 4 处(expected u32, found i32)——因为 glibc 那边真是 c_uint。所以必须是类型别名,不能统一改成 c_int。

后续

  • 两个 rlimit_get 的函数体现在逐字节相同了,理论上可以合成一个;这次没动,想让这个 diff 保持好读,留着以后单独提。
  • cross-check job 里目前只有几个 linux target,建议把 aarch64-unknown-linux-ohos 和 x86_64-unknown-linux-musl 加进去(niubash 那边同步加),否则这类回归没有 CI 兜着。

顺带说明:__rlimit_resource_t 在 upstream master(29baac9a)里仍在这两行,直接 git apply 就能落。

`resource()` and `rlimit_get()` spelled the selector as
`libc::__rlimit_resource_t`, which only glibc defines. On musl and on the
OpenHarmony libc (`target_os = "linux"`, `target_env = "musl"`/`"ohos"`)
that typedef does not exist at all; both libcs take a plain `c_int`.

Alias the type once, gated on `target_env`, so one body compiles for all
three libcs:

  target_env = "gnu"   -> libc::__rlimit_resource_t  (c_uint)
  otherwise            -> libc::c_int

Verified with `cargo check` on x86_64-unknown-linux-gnu (host),
x86_64-unknown-linux-musl and aarch64-unknown-linux-ohos: all three are
clean. Before this change musl and ohos each failed with 2 errors here
(`cannot find type __rlimit_resource_t`), and the ohos target is the only
thing standing between rubash and HarmonyOS PC support.

Note the two `rlimit_get` bodies (linux / other-unix) are now
byte-identical and could be merged in a follow-up; left alone here to keep
this diff reviewable.
@caomengxuan666

Copy link
Copy Markdown
Member Author

配套 PR:unixwin/niubash#210 — 在 niubash 的 cross-check 旁边加一个 cross-target-check job,把 aarch64-unknown-linux-ohos 和 x86_64-unknown-linux-musl 都纳入 cargo check 矩阵。

那个 job 现在会红,就是在等这个 PR 落地。

反向实测:不打这个补丁,ohos 到底炸不炸

为了确认这个修不是「顺手改改」,我在本地把补丁撤掉重跑了一次 niubash 的 ohos 检查:

error[E0425]: cannot find type `__rlimit_resource_t` in crate `libc`
error[E0425]: cannot find type `__rlimit_resource_t` in crate `libc`
error: could not compile `rubash` (lib) due to 2 previous errors

正好 2 处,跟预期一致。

打上补丁之后的实测

不只是 rubash 自己过——整个 niubash workspace 在 ohos 上也编译通过:

Checking rubash v1.3.4 (/tmp/xcheck/rubash)
Compiling niubash v1.3.4 (/tmp/xcheck/niubash)
Checking niubash-runtime v1.3.4 (/tmp/xcheck/niubash/crates/niubash-runtime)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 32.59s

musl 那边同理(55.12s,全绿)。这是 niubash 本体第一次被证明能在 aarch64-unknown-linux-ohos 上编译通过——之前只验到 rubash 那一层。

一句话

这个补丁是 HarmonyOS PC 那条路上唯一的编译阻塞点。它落地,aarch64-unknown-linux-ohos 就通了。

@caomengxuan666 caomengxuan666 added the P1-high 高:核心语义或高频路径受损,影响日常使用/发布 label Oct 9, 2026
@caomengxuan666
caomengxuan666 merged commit 2b9c437 into master Oct 9, 2026
6 checks passed
caomengxuan666 added a commit to unixwin/niubash that referenced this pull request Oct 9, 2026
`cross-check` runs on ubuntu-latest and macos-latest, which are both
x86_64/glibc. Nothing covers musl, and nothing covers aarch64 at all, so
whole classes of libc and arch divergence are invisible to CI.

That is not hypothetical. rubash's `ulimit` typed its resource selector as
`libc::__rlimit_resource_t`, a typedef only glibc defines. musl and the
OpenHarmony libc take a plain `c_int`, so both targets failed with two
`cannot find type` errors each -- while `cross-check` stayed green,
because the host is glibc and the cfg guard was `target_os = "linux"`,
which ohos satisfies (`target_env = "ohos"`, not `"gnu"`).

Add a `cross-target-check` job that `cargo check --workspace` for an
explicit target:

  x86_64-unknown-linux-musl   - the musl libc, and the static/Alpine story
  aarch64-unknown-linux-ohos  - HarmonyOS PC

`aarch64-unknown-linux-ohos` is Tier 2 with host tools, so a check-only
build needs no OpenHarmony SDK or clang wrapper; `setup-rust-toolchain`'s
`target:` input is enough. Both targets are unix, so the sibling rubash
checkout and the `[patch]` override behave exactly as in the other jobs --
in particular no `cargo update -p rubash`, same reasoning as the comment
already carried by `build-and-test` and `cross-check`.

Depends on unixwin/rubash#449 (merged as 2b9c437), which is the fix that
makes ohos clean.
@caomengxuan666
caomengxuan666 deleted the fix/ulimit-musl-ohos branch October 10, 2026 01:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P1-high 高:核心语义或高频路径受损,影响日常使用/发布

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant