Skip to content

Commit af1fff8

Browse files
committed
squash me, add pid translation ioctls
1 parent 36b14c0 commit af1fff8

File tree

2 files changed

+83
-8
lines changed

2 files changed

+83
-8
lines changed

src/backend/linux_raw/c.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,5 +386,9 @@ pub(crate) use statx_flags::*;
386386

387387
#[cfg(feature = "thread")]
388388
pub(crate) use linux_raw_sys::ioctl::{
389-
NS_GET_NSTYPE, NS_GET_OWNER_UID, NS_GET_PARENT, NS_GET_USERNS,
389+
NS_GET_NSTYPE,
390+
NS_GET_OWNER_UID,
391+
NS_GET_PARENT,
392+
NS_GET_USERNS,
393+
// NS_GET_PID_FROM_PIDNS, NS_GET_PID_IN_PIDNS, NS_GET_TGID_FROM_PIDNS, NS_GET_TGID_IN_PIDNS,
390394
};

src/thread/ns.rs

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ use linux_raw_sys::general::{
44
CLONE_NEWTIME, CLONE_NEWUSER, CLONE_NEWUTS, CLONE_SYSVSEM,
55
};
66

7-
use crate::backend::c::{c_int, NS_GET_NSTYPE, NS_GET_OWNER_UID, NS_GET_PARENT, NS_GET_USERNS};
7+
use crate::backend::c::{
8+
c_int, NS_GET_NSTYPE, NS_GET_OWNER_UID, NS_GET_PARENT, NS_GET_PID_FROM_PIDNS,
9+
NS_GET_PID_IN_PIDNS, NS_GET_TGID_FROM_PIDNS, NS_GET_TGID_IN_PIDNS, NS_GET_USERNS,
10+
};
811
use crate::backend::thread::syscalls;
912
use crate::fd::BorrowedFd;
1013
use crate::fd::{AsFd, FromRawFd, OwnedFd};
11-
use crate::io;
14+
use crate::io::{self, Errno};
1215
use crate::ioctl;
1316

14-
use super::{RawUid, Uid};
17+
use super::{Pid, RawUid, Uid};
1518

1619
bitflags! {
1720
/// Namespace type.
@@ -213,7 +216,7 @@ pub fn unshare(flags: UnshareFlags) -> io::Result<()> {
213216
///
214217
/// # Safety
215218
///
216-
/// `fd` must refer to a `/proc/pid/ns/*` file.
219+
/// `fd` must refer to a `/proc/{pid}/ns/*` file.
217220
#[inline]
218221
#[doc(alias = "NS_GET_USERNS")]
219222
pub fn ioctl_ns_get_userns<FD: AsFd>(fd: FD) -> io::Result<OwnedFd> {
@@ -228,7 +231,7 @@ pub fn ioctl_ns_get_userns<FD: AsFd>(fd: FD) -> io::Result<OwnedFd> {
228231
///
229232
/// # Safety
230233
///
231-
/// `fd` must refer to a `/proc/pid/ns/*` file.
234+
/// `fd` must refer to a `/proc/{pid}/ns/*` file.
232235
#[inline]
233236
#[doc(alias = "NS_GET_PARENT")]
234237
pub fn ioctl_ns_get_parent<FD: AsFd>(fd: FD) -> io::Result<OwnedFd> {
@@ -243,7 +246,7 @@ pub fn ioctl_ns_get_parent<FD: AsFd>(fd: FD) -> io::Result<OwnedFd> {
243246
///
244247
/// # Safety
245248
///
246-
/// `fd` must refer to a `/proc/pid/ns/*` file.
249+
/// `fd` must refer to a `/proc/{pid}/ns/*` file.
247250
#[inline]
248251
#[doc(alias = "NS_GET_NSTYPE")]
249252
pub fn ioctl_ns_get_nstype<FD: AsFd>(fd: FD) -> io::Result<NamespaceType> {
@@ -258,7 +261,7 @@ pub fn ioctl_ns_get_nstype<FD: AsFd>(fd: FD) -> io::Result<NamespaceType> {
258261
///
259262
/// # Safety
260263
///
261-
/// `fd` must refer to a `/proc/pid/ns/*` file.
264+
/// `fd` must refer to a `/proc/{pid}/ns/*` file.
262265
#[inline]
263266
#[doc(alias = "NS_GET_OWNER_UID")]
264267
pub fn ioctl_ns_get_owner_uid<FD: AsFd>(fd: FD) -> io::Result<Uid> {
@@ -268,3 +271,71 @@ pub fn ioctl_ns_get_owner_uid<FD: AsFd>(fd: FD) -> io::Result<Uid> {
268271
ioctl::ioctl(fd, ctl).map(Uid::from_raw)
269272
}
270273
}
274+
275+
/// `ioctl(ns_fd, NS_GET_PID_FROM_PIDNS, pid)`
276+
///
277+
/// # Safety
278+
///
279+
/// `fd` must refer to a `/proc/{pid}/ns/pid` file.
280+
#[inline]
281+
#[doc(alias = "NS_GET_PID_FROM_PIDNS")]
282+
pub fn ioctl_ns_get_pid_from_pidns<FD: AsFd>(fd: FD, pid: Pid) -> io::Result<Pid> {
283+
#[allow(unsafe_code)]
284+
unsafe {
285+
let ctl = ioctl::ParameterizedReturnGetter::<{ NS_GET_PID_FROM_PIDNS }>::new(
286+
pid.as_raw_pid() as usize,
287+
);
288+
ioctl::ioctl(fd, ctl).and_then(|pid| Pid::from_raw(pid).ok_or(Errno::INVAL))
289+
}
290+
}
291+
292+
/// `ioctl(ns_fd, NS_GET_TGID_FROM_PIDNS, tgid)`
293+
///
294+
/// # Safety
295+
///
296+
/// `fd` must refer to a `/proc/{pid}/ns/pid` file.
297+
#[inline]
298+
#[doc(alias = "NS_GET_TGID_FROM_PIDNS")]
299+
pub fn ioctl_ns_get_tgid_from_pidns<FD: AsFd>(fd: FD, tgid: Pid) -> io::Result<Pid> {
300+
#[allow(unsafe_code)]
301+
unsafe {
302+
let ctl = ioctl::ParameterizedReturnGetter::<{ NS_GET_TGID_FROM_PIDNS }>::new(
303+
tgid.as_raw_pid() as usize,
304+
);
305+
ioctl::ioctl(fd, ctl).and_then(|tgid| Pid::from_raw(tgid).ok_or(Errno::INVAL))
306+
}
307+
}
308+
309+
/// `ioctl(ns_fd, NS_GET_PID_IN_PIDNS, pid)`
310+
///
311+
/// # Safety
312+
///
313+
/// `fd` must refer to a `/proc/{pid}/ns/pid` file.
314+
#[inline]
315+
#[doc(alias = "NS_GET_PID_IN_PIDNS")]
316+
pub fn ioctl_ns_get_pid_in_pidns<FD: AsFd>(fd: FD, pid: Pid) -> io::Result<Pid> {
317+
#[allow(unsafe_code)]
318+
unsafe {
319+
let ctl = ioctl::ParameterizedReturnGetter::<{ NS_GET_PID_IN_PIDNS }>::new(
320+
pid.as_raw_pid() as usize,
321+
);
322+
ioctl::ioctl(fd, ctl).and_then(|pid| Pid::from_raw(pid).ok_or(Errno::INVAL))
323+
}
324+
}
325+
326+
/// `ioctl(ns_fd, NS_GET_TGID_IN_PIDNS, tgid)`
327+
///
328+
/// # Safety
329+
///
330+
/// `fd` must refer to a `/proc/{pid}/ns/pid` file.
331+
#[inline]
332+
#[doc(alias = "NS_GET_TGID_IN_PIDNS")]
333+
pub fn ioctl_ns_get_tgid_in_pidns<FD: AsFd>(fd: FD, tgid: Pid) -> io::Result<Pid> {
334+
#[allow(unsafe_code)]
335+
unsafe {
336+
let ctl = ioctl::ParameterizedReturnGetter::<{ NS_GET_TGID_IN_PIDNS }>::new(
337+
tgid.as_raw_pid() as usize,
338+
);
339+
ioctl::ioctl(fd, ctl).and_then(|tgid| Pid::from_raw(tgid).ok_or(Errno::INVAL))
340+
}
341+
}

0 commit comments

Comments
 (0)