Skip to content

Commit 1380807

Browse files
committed
Auto merge of #293 - dhylands:gettid, r=kamalmarhubi
Add gettid I tested this under linux, and I noticed that this seems to also be built for OSX. It would be appreciated if someone could test this under OSX. I'm not familiar enough with rust to know if there is a way of integrating this without creating a sub-crate.
2 parents 3f0c3e1 + ca75212 commit 1380807

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ preadv_pwritev = []
2020
signalfd = []
2121

2222
[dependencies]
23-
libc = "0.2.7"
23+
libc = "0.2.8"
2424
bitflags = "0.4"
2525
cfg-if = "0.1.0"
2626

src/unistd.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ pub fn setpgid(pid: pid_t, pgid: pid_t) -> Result<()> {
5656
Errno::result(res).map(drop)
5757
}
5858

59+
#[cfg(any(target_os = "linux", target_os = "android"))]
60+
#[inline]
61+
pub fn gettid() -> pid_t {
62+
unsafe { libc::syscall(libc::SYS_gettid) as pid_t } // no error handling, according to man page: "These functions are always successful."
63+
}
64+
5965
#[inline]
6066
pub fn dup(oldfd: RawFd) -> Result<RawFd> {
6167
let res = unsafe { libc::dup(oldfd) };

test/test_unistd.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ fn test_getpid() {
5454
assert!(ppid > 0);
5555
}
5656

57+
#[cfg(any(target_os = "linux", target_os = "android"))]
58+
mod linux_android {
59+
use nix::unistd::gettid;
60+
61+
#[test]
62+
fn test_gettid() {
63+
let tid = gettid();
64+
assert!(tid > 0);
65+
}
66+
}
67+
5768
macro_rules! execve_test_factory(
5869
($test_name:ident, $syscall:ident, $unix_sh:expr, $android_sh:expr) => (
5970
#[test]

0 commit comments

Comments
 (0)