Skip to content

Commit 2e11d9e

Browse files
committed
Add more fcntl and seal constants for Android/Linux
We now create an additional binary `linux_fcntl` for testing this since there are header conflicts when including all necessary headers. This binary is run on all platforms even though it's empty on all non- Android/non-Linux platforms. Testing has been switched from a custom binary to using a runner-less test (or pair of tests). This means that for local development a simple `cd libc-test && cargo test` will run all the tests. CI has also been updated here to reflect that.
1 parent 13478d0 commit 2e11d9e

File tree

9 files changed

+100
-59
lines changed

9 files changed

+100
-59
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ With that in mind, the steps for adding a new API are:
9393
We have two automated tests running on [Travis](https://travis-ci.org/rust-lang/libc):
9494

9595
1. [`libc-test`](https://github.com/alexcrichton/ctest)
96-
- `cd libc-test && cargo run`
96+
- `cd libc-test && cargo test`
9797
- Use the `skip_*()` functions in `build.rs` if you really need a workaround.
9898
2. Style checker
9999
- `rustc ci/style.rs && ./style src`

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ build: false
2424

2525
test_script:
2626
- cargo test --target %TARGET%
27-
- cargo run --manifest-path libc-test/Cargo.toml --target %TARGET%
27+
- cargo test --manifest-path libc-test/Cargo.toml --target %TARGET%
2828

2929
cache:
3030
- target

ci/run.sh

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,14 @@ fi
7070

7171
case "$TARGET" in
7272
*-apple-ios)
73-
cargo rustc --manifest-path libc-test/Cargo.toml --target $TARGET -- \
74-
-C link-args=-mios-simulator-version-min=7.0
73+
cargo rustc --manifest-path libc-test/Cargo.toml --target $TARGET \
74+
--test main -- -C link-args=-mios-simulator-version-min=7.0
75+
cargo rustc --manifest-path libc-test/Cargo.toml --target $TARGET \
76+
--test linux-fcntl -- -C link-args=-mios-simulator-version-min=7.0
7577
;;
7678

7779
*)
78-
cargo build --manifest-path libc-test/Cargo.toml --target $TARGET
80+
cargo build --manifest-path libc-test/Cargo.toml --target $TARGET --tests
7981
;;
8082
esac
8183

@@ -95,68 +97,89 @@ case "$TARGET" in
9597
fi
9698
emulator @$arch -no-window $accel &
9799
adb wait-for-device
98-
adb push $CARGO_TARGET_DIR/$TARGET/debug/libc-test /data/local/tmp/libc-test
99-
adb shell /data/local/tmp/libc-test 2>&1 | tee /tmp/out
100+
adb push $CARGO_TARGET_DIR/$TARGET/debug/main-* /data/local/tmp/main
101+
adb shell /data/local/tmp/main 2>&1 | tee /tmp/out
102+
grep "^PASSED .* tests" /tmp/out
103+
adb push $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-* /data/local/tmp/linux_fcntl
104+
adb shell /data/local/tmp/linux_fcntl 2>&1 | tee /tmp/out
100105
grep "^PASSED .* tests" /tmp/out
101106
;;
102107

103108
i386-apple-ios)
104109
rustc -O ./ci/ios/deploy_and_run_on_ios_simulator.rs
105-
./deploy_and_run_on_ios_simulator $CARGO_TARGET_DIR/$TARGET/debug/libc-test
110+
./deploy_and_run_on_ios_simulator $CARGO_TARGET_DIR/$TARGET/debug/main-*
111+
./deploy_and_run_on_ios_simulator $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
106112
;;
107113

108114
x86_64-apple-ios)
109115
rustc -O ./ci/ios/deploy_and_run_on_ios_simulator.rs
110-
./deploy_and_run_on_ios_simulator $CARGO_TARGET_DIR/$TARGET/debug/libc-test
116+
./deploy_and_run_on_ios_simulator $CARGO_TARGET_DIR/$TARGET/debug/main-*
117+
./deploy_and_run_on_ios_simulator $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
111118
;;
112119

113120
arm-unknown-linux-gnueabihf)
114-
qemu-arm -L /usr/arm-linux-gnueabihf $CARGO_TARGET_DIR/$TARGET/debug/libc-test
121+
qemu-arm -L /usr/arm-linux-gnueabihf $CARGO_TARGET_DIR/$TARGET/debug/main-*
122+
qemu-arm -L /usr/arm-linux-gnueabihf $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
115123
;;
116124

117125
mips-unknown-linux-gnu)
118-
qemu-mips -L /usr/mips-linux-gnu $CARGO_TARGET_DIR/$TARGET/debug/libc-test
126+
qemu-mips -L /usr/mips-linux-gnu $CARGO_TARGET_DIR/$TARGET/debug/main-*
127+
qemu-mips -L /usr/mips-linux-gnu $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
119128
;;
120129

121130
mips64-unknown-linux-gnuabi64)
122-
qemu-mips64 -L /usr/mips64-linux-gnuabi64 $CARGO_TARGET_DIR/$TARGET/debug/libc-test
131+
qemu-mips64 -L /usr/mips64-linux-gnuabi64 $CARGO_TARGET_DIR/$TARGET/debug/main-*
132+
qemu-mips64 -L /usr/mips64-linux-gnuabi64 $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
123133
;;
124134

125135
mips-unknown-linux-musl)
126136
qemu-mips -L /toolchain/staging_dir/toolchain-mips_34kc_gcc-5.3.0_musl-1.1.15 \
127-
$CARGO_TARGET_DIR/$TARGET/debug/libc-test
137+
$CARGO_TARGET_DIR/$TARGET/debug/main-*
138+
qemu-mips -L /toolchain/staging_dir/toolchain-mips_34kc_gcc-5.3.0_musl-1.1.15 \
139+
$CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
128140
;;
129141

130142
mipsel-unknown-linux-musl)
131-
qemu-mipsel -L /toolchain $CARGO_TARGET_DIR/$TARGET/debug/libc-test
143+
qemu-mipsel -L /toolchain $CARGO_TARGET_DIR/$TARGET/debug/main-*
144+
qemu-mipsel -L /toolchain $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
132145
;;
133146

134147
powerpc-unknown-linux-gnu)
135-
qemu-ppc -L /usr/powerpc-linux-gnu $CARGO_TARGET_DIR/$TARGET/debug/libc-test
148+
qemu-ppc -L /usr/powerpc-linux-gnu $CARGO_TARGET_DIR/$TARGET/debug/main-*
149+
qemu-ppc -L /usr/powerpc-linux-gnu $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
136150
;;
137151

138152
powerpc64-unknown-linux-gnu)
139-
qemu-ppc64 -L /usr/powerpc64-linux-gnu $CARGO_TARGET_DIR/$TARGET/debug/libc-test
153+
qemu-ppc64 -L /usr/powerpc64-linux-gnu $CARGO_TARGET_DIR/$TARGET/debug/main-*
154+
qemu-ppc64 -L /usr/powerpc64-linux-gnu $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
140155
;;
141156

142157
aarch64-unknown-linux-gnu)
143-
qemu-aarch64 -L /usr/aarch64-linux-gnu/ $CARGO_TARGET_DIR/$TARGET/debug/libc-test
158+
qemu-aarch64 -L /usr/aarch64-linux-gnu/ $CARGO_TARGET_DIR/$TARGET/debug/main-*
159+
qemu-aarch64 -L /usr/aarch64-linux-gnu/ $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
144160
;;
145161

146162
s390x-unknown-linux-gnu)
147163
# TODO: in theory we should execute this, but qemu segfaults immediately :(
148-
# qemu-s390x -L /usr/s390x-linux-gnu/ $CARGO_TARGET_DIR/$TARGET/debug/libc-test
164+
# qemu-s390x -L /usr/s390x-linux-gnu/ $CARGO_TARGET_DIR/$TARGET/debug/main-*
165+
# qemu-s390x -L /usr/s390x-linux-gnu/ $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
149166
;;
150167

151168
*-rumprun-netbsd)
152-
rumprun-bake hw_virtio /tmp/libc-test.img $CARGO_TARGET_DIR/$TARGET/debug/libc-test
169+
rumprun-bake hw_virtio /tmp/libc-test.img $CARGO_TARGET_DIR/$TARGET/debug/main-*
170+
qemu-system-x86_64 -nographic -vga none -m 64 \
171+
-kernel /tmp/libc-test.img 2>&1 | tee /tmp/out &
172+
sleep 5
173+
grep "^PASSED .* tests" /tmp/out
174+
rumprun-bake hw_virtio /tmp/libc-test.img $CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
153175
qemu-system-x86_64 -nographic -vga none -m 64 \
154176
-kernel /tmp/libc-test.img 2>&1 | tee /tmp/out &
155177
sleep 5
156178
grep "^PASSED .* tests" /tmp/out
157179
;;
158180

159181
*)
160-
$CARGO_TARGET_DIR/$TARGET/debug/libc-test
182+
$CARGO_TARGET_DIR/$TARGET/debug/main-*
183+
$CARGO_TARGET_DIR/$TARGET/debug/linux_fcntl-*
161184
;;
162185
esac

libc-test/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,13 @@ libc = { path = ".." }
99

1010
[build-dependencies]
1111
ctest = "0.1"
12+
13+
[[test]]
14+
name = "main"
15+
path = "test/main.rs"
16+
harness = false
17+
18+
[[test]]
19+
name = "linux-fcntl"
20+
path = "test/linux_fcntl.rs"
21+
harness = false

libc-test/build.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,11 @@ fn main() {
468468
// it's been fixed in CI.
469469
"MADV_SOFT_OFFLINE" if mips && linux => true,
470470

471+
// These constants are tested in a separate test program generated below because there
472+
// are header conflicts if we try to include the headers that define them here.
473+
"F_CANCELLK" | "F_ADD_SEALS" | "F_GET_SEALS" => true,
474+
"F_SEAL_SEAL" | "F_SEAL_SHRINK" | "F_SEAL_GROW" | "F_SEAL_WRITE" => true,
475+
471476
_ => false,
472477
}
473478
});
@@ -641,5 +646,31 @@ fn main() {
641646
}
642647
});
643648

644-
cfg.generate("../src/lib.rs", "all.rs");
649+
cfg.generate("../src/lib.rs", "main.rs");
650+
651+
// On Linux or Android also generate another script for testing linux/fcntl declarations.
652+
// These cannot be tested normally because including both `linux/fcntl.h` and `fcntl.h`
653+
// fails on a lot of platforms.
654+
let mut cfg = ctest::TestGenerator::new();
655+
cfg.skip_type(|_| true)
656+
.skip_struct(|_| true)
657+
.skip_fn(|_| true);
658+
if android || linux {
659+
// musl defines these directly in `fcntl.h`
660+
if musl {
661+
cfg.header("fcntl.h");
662+
} else {
663+
cfg.header("linux/fcntl.h");
664+
}
665+
cfg.skip_const(move |name| {
666+
match name {
667+
"F_CANCELLK" | "F_ADD_SEALS" | "F_GET_SEALS" => false,
668+
"F_SEAL_SEAL" | "F_SEAL_SHRINK" | "F_SEAL_GROW" | "F_SEAL_WRITE" => false,
669+
_ => true,
670+
}
671+
});
672+
} else {
673+
cfg.skip_const(|_| true);
674+
}
675+
cfg.generate("../src/lib.rs", "linux_fcntl.rs");
645676
}

libc-test/test/linux_fcntl.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![allow(bad_style, improper_ctypes)]
2+
extern crate libc;
3+
4+
use libc::*;
5+
6+
include!(concat!(env!("OUT_DIR"), "/linux_fcntl.rs"));

libc-test/src/main.rs renamed to libc-test/test/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ extern crate libc;
33

44
use libc::*;
55

6-
include!(concat!(env!("OUT_DIR"), "/all.rs"));
6+
include!(concat!(env!("OUT_DIR"), "/main.rs"));

src/macros.rs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -69,40 +69,3 @@ macro_rules! f {
6969
macro_rules! __item {
7070
($i:item) => ($i)
7171
}
72-
73-
#[cfg(test)]
74-
mod tests {
75-
cfg_if! {
76-
if #[cfg(test)] {
77-
use std::option::Option as Option2;
78-
fn works1() -> Option2<u32> { Some(1) }
79-
} else {
80-
fn works1() -> Option<u32> { None }
81-
}
82-
}
83-
84-
cfg_if! {
85-
if #[cfg(foo)] {
86-
fn works2() -> bool { false }
87-
} else if #[cfg(test)] {
88-
fn works2() -> bool { true }
89-
} else {
90-
fn works2() -> bool { false }
91-
}
92-
}
93-
94-
cfg_if! {
95-
if #[cfg(foo)] {
96-
fn works3() -> bool { false }
97-
} else {
98-
fn works3() -> bool { true }
99-
}
100-
}
101-
102-
#[test]
103-
fn it_works() {
104-
assert!(works1().is_some());
105-
assert!(works2());
106-
assert!(works3());
107-
}
108-
}

src/unix/notbsd/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,17 @@ pub const F_SETFL: ::c_int = 4;
212212
pub const F_SETLEASE: ::c_int = 1024;
213213
pub const F_GETLEASE: ::c_int = 1025;
214214
pub const F_NOTIFY: ::c_int = 1026;
215+
pub const F_CANCELLK: ::c_int = 1029;
215216
pub const F_DUPFD_CLOEXEC: ::c_int = 1030;
216217
pub const F_SETPIPE_SZ: ::c_int = 1031;
217218
pub const F_GETPIPE_SZ: ::c_int = 1032;
219+
pub const F_ADD_SEALS: ::c_int = 1033;
220+
pub const F_GET_SEALS: ::c_int = 1034;
221+
222+
pub const F_SEAL_SEAL: ::c_int = 0x0001;
223+
pub const F_SEAL_SHRINK: ::c_int = 0x0002;
224+
pub const F_SEAL_GROW: ::c_int = 0x0004;
225+
pub const F_SEAL_WRITE: ::c_int = 0x0008;
218226

219227
// TODO(#235): Include file sealing fcntls once we have a way to verify them.
220228

0 commit comments

Comments
 (0)