Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ categories = ["api-bindings", "external-ffi-bindings", "no-std", "os"]
edition = "2024"
include = ["src/**/*", "LICENSE-*", "README.md"]

[target.'cfg(target_vendor = "apple")'.dependencies]
libc = { version = "0.2", default-features = false }

[features]
default = []
# Uses newer APIs
Expand Down
11 changes: 4 additions & 7 deletions examples/dump_process_registers.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//! A script to read and dump to stdout the current register values of a
//! process.

extern crate libc;
extern crate mach2;

use std::io;
use std::mem;
use std::ptr;
Expand All @@ -29,16 +26,16 @@ use mach2::structs::x86_thread_state64_t as thread_state64_t;

use std::io::prelude::*;

fn read_int() -> Result<libc::c_int, ()> {
fn read_int() -> Result<core::ffi::c_int, ()> {
let stdin = io::stdin();
let mut line = String::new();

stdin.read_line(&mut line).ok().unwrap();
let mut value: libc::c_int = 0;
let mut value: core::ffi::c_int = 0;

for c in line.chars().take_while(|&c| c != '\n') {
if let Some(d) = c.to_digit(10) {
value = value * 10 + (d as libc::c_int);
value = value * 10 + (d as core::ffi::c_int);
} else {
return Err(());
}
Expand Down Expand Up @@ -143,7 +140,7 @@ fn main() {
mach_vm_deallocate(
mach_task_self(),
thread_list as _,
((thread_count as usize) * mem::size_of::<libc::c_int>()) as _,
((thread_count as usize) * mem::size_of::<core::ffi::c_int>()) as _,
);

println!("mach_host={:?}", mach2::mach_init::mach_host_self());
Expand Down
1 change: 0 additions & 1 deletion mach-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2021"

[dependencies]
mach2 = { path = ".." }
libc = "0.2"

[build-dependencies]
ctest = "=0.5.0-beta.1"
Expand Down
85 changes: 0 additions & 85 deletions mach-test/build.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
extern crate ctest;

use std::env;
use std::fs;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};

#[derive(Eq, Ord, PartialEq, PartialOrd, Copy, Clone, Debug)]
struct Xcode(pub u32, pub u32);

Expand Down Expand Up @@ -44,8 +36,6 @@ impl Xcode {
}

fn main() {
inject_libc_extern();

let xcode = Xcode::version();
// kept on purpose for debugging:
// println!("cargo:warning=\"Xcode version: {:?}\"", xcode);
Expand Down Expand Up @@ -312,78 +302,3 @@ fn main() {
// the module to generate.
ctest::generate_test(&mut cfg, "../src/lib.rs", "all.rs").unwrap();
}

fn inject_libc_extern() {
if env::var_os("CTEST_LIBC_WRAPPER_INSTALLED").is_some() {
return;
}

let rustc = env::var("RUSTC").unwrap_or_else(|_| String::from("rustc"));

let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR must be set"));
let profile_dir = out_dir
.ancestors()
.nth(3)
.expect("failed to locate Cargo profile directory");
let deps_dir = profile_dir.join("deps");

let libc_artifact = find_libc_artifact(&deps_dir)
.unwrap_or_else(|| panic!("failed to locate libc artifact in {}", deps_dir.display()));

let wrapper_path = create_rustc_wrapper(&out_dir);

unsafe {
env::set_var("CTEST_REAL_RUSTC", &rustc);
env::set_var("CTEST_LIBC_ARTIFACT", &libc_artifact);
env::set_var("RUSTC", &wrapper_path);
env::set_var("CTEST_LIBC_WRAPPER_INSTALLED", "1");
}
}

fn find_libc_artifact(dir: &Path) -> Option<PathBuf> {
let mut fallback = None;
let entries = fs::read_dir(dir).ok()?;
for entry in entries.flatten() {
let path = entry.path();
let file_name = path.file_name()?.to_string_lossy();
if !file_name.starts_with("liblibc-") {
continue;
}
match path.extension().and_then(|ext| ext.to_str()) {
Some("rlib") => return Some(path),
Some("rmeta") | Some("a") | Some("lib") if fallback.is_none() => {
fallback = Some(path);
}
_ => {}
}
}
fallback
}

fn create_rustc_wrapper(out_dir: &Path) -> PathBuf {
#[cfg(unix)]
{
let wrapper_path = out_dir.join("rustc-with-libc.sh");
let script = r#"#!/bin/sh
exec "$CTEST_REAL_RUSTC" --extern "libc=$CTEST_LIBC_ARTIFACT" "$@"
"#;
fs::write(&wrapper_path, script).expect("failed to write rustc wrapper script");
let mut perms = fs::metadata(&wrapper_path)
.and_then(|meta| Ok(meta.permissions()))
.expect("failed to read wrapper permissions");
perms.set_mode(0o755);
fs::set_permissions(&wrapper_path, perms)
.expect("failed to set executable bit on rustc wrapper script");
wrapper_path
}
#[cfg(not(unix))]
{
let wrapper_path = out_dir.join("rustc-with-libc.cmd");
let script = r#"@echo off
setlocal
"%CTEST_REAL_RUSTC%" --extern libc="%CTEST_LIBC_ARTIFACT%" %*
"#;
fs::write(&wrapper_path, script).expect("failed to write rustc wrapper script");
wrapper_path
}
}
2 changes: 2 additions & 0 deletions mach-test/test/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(bad_style)]

#[allow(unused_imports)]
use core::ffi::{c_char, c_int, c_uchar, c_uint, c_ulong, c_ulonglong, c_ushort};
use mach2::boolean::*;
use mach2::bootstrap::*;
use mach2::clock::*;
Expand Down
4 changes: 2 additions & 2 deletions src/boolean.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This module corresponds to `mach/i386/boolean.h`.

#[cfg(target_arch = "x86_64")]
pub type boolean_t = libc::c_uint;
pub type boolean_t = core::ffi::c_uint;

#[cfg(not(target_arch = "x86_64"))]
pub type boolean_t = libc::c_int;
pub type boolean_t = core::ffi::c_int;
53 changes: 27 additions & 26 deletions src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,41 @@
use crate::boolean::boolean_t;
use crate::kern_return::kern_return_t;
use crate::port::mach_port_t;
use core::ffi::{c_char, c_int, c_uint};

pub const BOOTSTRAP_MAX_NAME_LEN: libc::c_uint = 128;
pub const BOOTSTRAP_MAX_CMD_LEN: libc::c_uint = 512;
pub const BOOTSTRAP_MAX_NAME_LEN: c_uint = 128;
pub const BOOTSTRAP_MAX_CMD_LEN: c_uint = 512;

pub const BOOTSTRAP_MAX_LOOKUP_COUNT: libc::c_uint = 20;
pub const BOOTSTRAP_MAX_LOOKUP_COUNT: c_uint = 20;

pub const BOOTSTRAP_SUCCESS: libc::c_uint = 0;
pub const BOOTSTRAP_NOT_PRIVILEGED: libc::c_uint = 1100;
pub const BOOTSTRAP_NAME_IN_USE: libc::c_uint = 1101;
pub const BOOTSTRAP_UNKNOWN_SERVICE: libc::c_uint = 1102;
pub const BOOTSTRAP_SERVICE_ACTIVE: libc::c_uint = 1103;
pub const BOOTSTRAP_BAD_COUNT: libc::c_uint = 1104;
pub const BOOTSTRAP_NO_MEMORY: libc::c_uint = 1105;
pub const BOOTSTRAP_NO_CHILDREN: libc::c_uint = 1106;
pub const BOOTSTRAP_SUCCESS: c_uint = 0;
pub const BOOTSTRAP_NOT_PRIVILEGED: c_uint = 1100;
pub const BOOTSTRAP_NAME_IN_USE: c_uint = 1101;
pub const BOOTSTRAP_UNKNOWN_SERVICE: c_uint = 1102;
pub const BOOTSTRAP_SERVICE_ACTIVE: c_uint = 1103;
pub const BOOTSTRAP_BAD_COUNT: c_uint = 1104;
pub const BOOTSTRAP_NO_MEMORY: c_uint = 1105;
pub const BOOTSTRAP_NO_CHILDREN: c_uint = 1106;

pub const BOOTSTRAP_STATUS_INACTIVE: libc::c_uint = 0;
pub const BOOTSTRAP_STATUS_ACTIVE: libc::c_uint = 1;
pub const BOOTSTRAP_STATUS_ON_DEMAND: libc::c_uint = 2;
pub const BOOTSTRAP_STATUS_INACTIVE: c_uint = 0;
pub const BOOTSTRAP_STATUS_ACTIVE: c_uint = 1;
pub const BOOTSTRAP_STATUS_ON_DEMAND: c_uint = 2;

pub type name_t = [libc::c_char; 128];
pub type cmd_t = [libc::c_char; 512];
pub type name_t = [c_char; 128];
pub type cmd_t = [c_char; 512];
pub type name_array_t = *mut name_t;
pub type bootstrap_status_t = libc::c_int;
pub type bootstrap_status_t = c_int;
pub type bootstrap_status_array_t = *mut bootstrap_status_t;
pub type bootstrap_property_t = libc::c_uint;
pub type bootstrap_property_t = c_uint;
pub type bootstrap_property_array_t = *mut bootstrap_property_t;
pub type bool_array_t = *mut boolean_t;

unsafe extern "C" {
pub static bootstrap_port: mach_port_t;
pub fn bootstrap_create_server(
bp: mach_port_t,
server_cmd: *mut libc::c_char,
server_uid: libc::uid_t,
server_cmd: *mut c_char,
server_uid: c_uint,
on_demand: boolean_t,
server_port: *mut mach_port_t,
) -> kern_return_t;
Expand All @@ -49,28 +50,28 @@ unsafe extern "C" {
pub fn bootstrap_parent(bp: mach_port_t, parent_port: *mut mach_port_t) -> kern_return_t;
pub fn bootstrap_register(
bp: mach_port_t,
service_name: *mut libc::c_char,
service_name: *mut c_char,
sp: mach_port_t,
) -> kern_return_t;
pub fn bootstrap_create_service(
bp: mach_port_t,
service_name: *mut libc::c_char,
service_name: *mut c_char,
sp: *mut mach_port_t,
) -> kern_return_t;
pub fn bootstrap_check_in(
bp: mach_port_t,
service_name: *const libc::c_char,
service_name: *const c_char,
sp: *mut mach_port_t,
) -> kern_return_t;
pub fn bootstrap_look_up(
bp: mach_port_t,
service_name: *const libc::c_char,
service_name: *const c_char,
sp: *mut mach_port_t,
) -> kern_return_t;
pub fn bootstrap_status(
bp: mach_port_t,
service_name: *mut libc::c_char,
service_name: *mut c_char,
service_active: *mut bootstrap_status_t,
) -> kern_return_t;
pub fn bootstrap_strerror(r: kern_return_t) -> *const libc::c_char;
pub fn bootstrap_strerror(r: kern_return_t) -> *const c_char;
}
6 changes: 3 additions & 3 deletions src/clock.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! This module roughly corresponds to `mach/clock.h`.

pub const clock_MSG_COUNT: libc::c_uint = 3;

use crate::clock_types::{alarm_type_t, clock_attr_t, clock_flavor_t, mach_timespec_t};
use crate::kern_return::kern_return_t;
use crate::mach_types::{clock_reply_t, clock_serv_t};
use crate::message::mach_msg_type_number_t;
use core::ffi::c_uint;

pub const clock_MSG_COUNT: c_uint = 3;

unsafe extern "C" {
pub fn clock_get_time(
Expand Down
57 changes: 29 additions & 28 deletions src/clock_types.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,56 @@
//! This module roughly corresponds to `mach/clock_types.h`.
use core::ffi::{c_int, c_uint, c_ulonglong};

pub type alarm_type_t = libc::c_int;
pub type sleep_type_t = libc::c_int;
pub type clock_id_t = libc::c_int;
pub type clock_flavor_t = libc::c_int;
pub type clock_attr_t = *mut libc::c_int;
pub type clock_res_t = libc::c_int;
pub type alarm_type_t = c_int;
pub type sleep_type_t = c_int;
pub type clock_id_t = c_int;
pub type clock_flavor_t = c_int;
pub type clock_attr_t = *mut c_int;
pub type clock_res_t = c_int;

#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Hash, PartialOrd, PartialEq, Eq, Ord)]
pub struct mach_timespec {
pub tv_sec: libc::c_uint,
pub tv_sec: c_uint,
pub tv_nsec: clock_res_t,
}
pub type mach_timespec_t = mach_timespec;

pub const SYSTEM_CLOCK: libc::c_uint = 0;
pub const CALENDAR_CLOCK: libc::c_uint = 1;
pub const REALTIME_CLOCK: libc::c_uint = 0;
pub const SYSTEM_CLOCK: c_uint = 0;
pub const CALENDAR_CLOCK: c_uint = 1;
pub const REALTIME_CLOCK: c_uint = 0;

pub const CLOCK_GET_TIME_RES: libc::c_uint = 1;
pub const CLOCK_ALARM_CURRES: libc::c_uint = 3;
pub const CLOCK_ALARM_MINRES: libc::c_uint = 4;
pub const CLOCK_ALARM_MAXRES: libc::c_uint = 5;
pub const CLOCK_GET_TIME_RES: c_uint = 1;
pub const CLOCK_ALARM_CURRES: c_uint = 3;
pub const CLOCK_ALARM_MINRES: c_uint = 4;
pub const CLOCK_ALARM_MAXRES: c_uint = 5;

pub const NSEC_PER_USEC: libc::c_ulonglong = 1000;
pub const USEC_PER_SEC: libc::c_ulonglong = 1_000_000;
pub const NSEC_PER_SEC: libc::c_ulonglong = 1_000_000_000;
pub const NSEC_PER_MSEC: libc::c_ulonglong = 1_000_000;
pub const NSEC_PER_USEC: c_ulonglong = 1000;
pub const USEC_PER_SEC: c_ulonglong = 1_000_000;
pub const NSEC_PER_SEC: c_ulonglong = 1_000_000_000;
pub const NSEC_PER_MSEC: c_ulonglong = 1_000_000;

#[allow(non_snake_case)]
pub fn BAD_MACH_TIMESPEC(t: mach_timespec) -> bool {
t.tv_nsec < 0 || (t.tv_nsec as libc::c_ulonglong) >= NSEC_PER_SEC
t.tv_nsec < 0 || (t.tv_nsec as c_ulonglong) >= NSEC_PER_SEC
}

#[allow(non_snake_case)]
pub fn CMP_MACH_TIMESPEC(t1: &mach_timespec, t2: &mach_timespec) -> libc::c_ulonglong {
pub fn CMP_MACH_TIMESPEC(t1: &mach_timespec, t2: &mach_timespec) -> c_ulonglong {
if t1.tv_sec > t2.tv_sec {
return NSEC_PER_SEC;
}
if t1.tv_sec < t2.tv_sec {
return !NSEC_PER_SEC;
}
(t1.tv_nsec as libc::c_ulonglong) - (t2.tv_nsec as libc::c_ulonglong)
(t1.tv_nsec as c_ulonglong) - (t2.tv_nsec as c_ulonglong)
}

#[allow(non_snake_case)]
pub fn ADD_MACH_TIMESPEC(t1: &mut mach_timespec, t2: &mach_timespec) {
t1.tv_nsec += t2.tv_nsec;
if (t1.tv_nsec as libc::c_ulonglong) >= NSEC_PER_SEC {
t1.tv_nsec = (t1.tv_nsec as libc::c_ulonglong - NSEC_PER_SEC) as clock_res_t;
if (t1.tv_nsec as c_ulonglong) >= NSEC_PER_SEC {
t1.tv_nsec = (t1.tv_nsec as c_ulonglong - NSEC_PER_SEC) as clock_res_t;
t1.tv_sec += 1;
}
t1.tv_sec += t2.tv_sec;
Expand All @@ -59,17 +60,17 @@ pub fn ADD_MACH_TIMESPEC(t1: &mut mach_timespec, t2: &mach_timespec) {
pub fn SUB_MACH_TIMESPEC(t1: &mut mach_timespec, t2: &mach_timespec) {
t1.tv_nsec -= t2.tv_nsec;
if t1.tv_nsec < 0 {
t1.tv_nsec = (t1.tv_nsec as libc::c_ulonglong + NSEC_PER_SEC) as clock_res_t;
t1.tv_nsec = (t1.tv_nsec as c_ulonglong + NSEC_PER_SEC) as clock_res_t;
t1.tv_sec -= 1;
}
t1.tv_sec -= t2.tv_sec;
}

pub const ALRMTYPE: libc::c_uint = 0xff;
pub const TIME_ABSOLUTE: libc::c_uint = 0x00;
pub const TIME_RELATIVE: libc::c_uint = 0x01;
pub const ALRMTYPE: c_uint = 0xff;
pub const TIME_ABSOLUTE: c_uint = 0x00;
pub const TIME_RELATIVE: c_uint = 0x01;

#[allow(non_snake_case)]
pub fn BAD_ALRMTYPE(t: libc::c_uint) -> bool {
pub fn BAD_ALRMTYPE(t: c_uint) -> bool {
t & (!TIME_RELATIVE) != 0
}
Loading