Skip to content

Commit

Permalink
[#81] Move translation to C
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Jan 14, 2024
1 parent 36676b6 commit c965b3b
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 167 deletions.
8 changes: 4 additions & 4 deletions iceoryx2-bb/posix/src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,13 +499,13 @@ impl SignalHandler {

fn register_signal_from_state(&mut self, details: SignalDetail) -> posix::sigaction_t {
let adjusted_state = posix::sigaction_t {
sa_handler: details.state.sa_handler,
sa_flags: if self.do_repeat_eintr_call {
iox2_sa_handler: details.state.iox2_sa_handler,
iox2_sa_flags: if self.do_repeat_eintr_call {
posix::SA_RESTART
} else {
0
},
sa_mask: details.state.sa_mask,
iox2_sa_mask: details.state.iox2_sa_mask,
};
let mut previous_action = posix::sigaction_t::new();

Expand All @@ -524,7 +524,7 @@ impl SignalHandler {
callback: posix::sighandler_t,
) -> posix::sigaction_t {
let mut action = posix::sigaction_t::new();
action.sa_handler = callback;
action.iox2_sa_handler = callback;
self.register_signal_from_state(SignalDetail::new(signal, action))
}

Expand Down
17 changes: 10 additions & 7 deletions iceoryx2-pal/posix/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ fn main() {
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
println!("cargo:rustc-link-lib=pthread");

if std::env::var("DOCS_RS").is_ok() {
println!("cargo:rerun-if-changed=src/c/posix_docs_rs.h");
} else {
#[cfg(all(target_os = "linux", feature = "acl"))]
println!("cargo:rustc-link-lib=acl");
println!("cargo:rerun-if-changed=src/c/posix.h");
}
#[cfg(all(target_os = "linux", feature = "acl"))]
println!("cargo:rustc-link-lib=acl");
println!("cargo:rerun-if-changed=src/c/posix.h");

let bindings = if std::env::var("DOCS_RS").is_ok() {
bindgen::Builder::default()
Expand Down Expand Up @@ -61,10 +57,17 @@ fn main() {
.write_to_file(out_path.join("posix_generated.rs"))
.expect("Couldn't write bindings!");

println!("cargo:rerun-if-changed=src/c/sigaction.c");
cc::Build::new()
.file("src/c/sigaction.c")
.compile("libsigaction.a");

println!("cargo:rerun-if-changed=src/c/scandir.c");
cc::Build::new()
.file("src/c/scandir.c")
.compile("libscandir.a");

println!("cargo:rerun-if-changed=src/c/socket_macros.c");
cc::Build::new()
.file("src/c/socket_macros.c")
.compile("libsocket_macros.a");
Expand Down
18 changes: 18 additions & 0 deletions iceoryx2-pal/posix/src/c/posix.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

#ifdef __FreeBSD__
#include <mqueue.h>
#if defined(IOX2_ACL_SUPPORT) && !defined(IOX2_DOCS_RS_SUPPORT)
Expand Down Expand Up @@ -89,3 +101,9 @@ int acl_set_fd(int, acl_t) { return 0; }
char *acl_to_text(acl_t, ssize_t *) { return NULL; }
acl_t acl_from_text(const char *) { return 0; }
#endif

struct iox2_sigaction {
size_t iox2_sa_handler;
sigset_t iox2_sa_mask;
int iox2_sa_flags;
};
71 changes: 0 additions & 71 deletions iceoryx2-pal/posix/src/c/posix_docs_rs.h

This file was deleted.

12 changes: 12 additions & 0 deletions iceoryx2-pal/posix/src/c/scandir.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

#ifndef _WIN64
#include <dirent.h>

Expand Down
47 changes: 47 additions & 0 deletions iceoryx2-pal/posix/src/c/sigaction.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

#ifndef _WIN64
#include "posix.h"

int iox2_sigaction_func(int sig, const struct iox2_sigaction *restrict act,
struct iox2_sigaction *restrict oact) {
struct sigaction tr_act;
memset(&tr_act, 0, sizeof(struct sigaction));
struct sigaction *tr_act_ptr = NULL;

struct sigaction tr_oact;
memset(&tr_act, 0, sizeof(struct sigaction));
struct sigaction *tr_oact_ptr = NULL;

if (act != NULL) {
tr_act.sa_flags = act->iox2_sa_flags;
tr_act.sa_mask = act->iox2_sa_mask;
tr_act.sa_handler = (void (*)(int))act->iox2_sa_handler;
tr_act_ptr = &tr_act;
}

if (oact != NULL) {
tr_oact_ptr = &tr_oact;
}

int ret_val = sigaction(sig, tr_act_ptr, tr_oact_ptr);

if (ret_val == 0 && oact != NULL) {
oact->iox2_sa_flags = tr_act.sa_flags;
oact->iox2_sa_mask = tr_act.sa_mask;
oact->iox2_sa_handler = (size_t)tr_act.sa_handler;
}

return ret_val;
}
#endif
17 changes: 13 additions & 4 deletions iceoryx2-pal/posix/src/c/socket_macros.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

#ifdef _WIN64
#include <MSWSock.h>
#include <WinSock2.h>
#include <Windows.h>
#include <MSWSock.h>
#include <io.h>



#else
#include <sys/select.h>
#include <sys/socket.h>
Expand Down
1 change: 0 additions & 1 deletion iceoryx2-pal/posix/src/freebsd/dirent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pub unsafe fn dirfd(dirp: *mut DIR) -> int {
mod internal {
use super::*;

#[cfg_attr(target_os = "freebsd", link(name = "c"))]
extern "C" {
pub(super) fn scandir_ext(path: *const c_char, namelist: *mut *mut *mut dirent) -> int;
}
Expand Down
18 changes: 13 additions & 5 deletions iceoryx2-pal/posix/src/freebsd/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@
use crate::posix::types::*;

pub unsafe fn sigaction(sig: int, act: *const sigaction_t, oact: *mut sigaction_t) -> int {
crate::internal::sigaction(
sig,
act as *const crate::internal::sigaction,
oact as *mut crate::internal::sigaction,
)
internal::iox2_sigaction_func(sig, act, oact)
}

pub unsafe fn kill(pid: pid_t, sig: int) -> int {
crate::internal::kill(pid, sig)
}

mod internal {
use super::*;

extern "C" {
pub(super) fn iox2_sigaction_func(
sig: int,
act: *const sigaction_t,
oact: *mut sigaction_t,
) -> int;
}
}
1 change: 0 additions & 1 deletion iceoryx2-pal/posix/src/linux/dirent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ pub unsafe fn readdir(dirp: *mut DIR) -> *const dirent {
mod internal {
use super::*;

#[cfg_attr(target_os = "linux", link(name = "c"))]
extern "C" {
pub(super) fn scandir_ext(path: *const c_char, namelist: *mut *mut *mut dirent) -> int;
}
Expand Down
72 changes: 13 additions & 59 deletions iceoryx2-pal/posix/src/linux/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,70 +12,24 @@

#![allow(non_camel_case_types)]
#![allow(clippy::missing_safety_doc)]
use crate::posix::{types::*, Struct};
use crate::posix::types::*;

impl Struct for crate::internal::sigaction {}

impl From<&sigaction_t> for crate::internal::sigaction {
fn from(value: &sigaction_t) -> Self {
let mut this = crate::internal::sigaction::new();
this.sa_mask = value.sa_mask;
this.sa_flags = value.sa_flags;
this.__sigaction_handler.sa_handler =
Some(unsafe { core::mem::transmute(value.sa_handler) });

this
}
pub unsafe fn sigaction(sig: int, act: *const sigaction_t, oact: *mut sigaction_t) -> int {
internal::iox2_sigaction_func(sig, act, oact)
}

impl From<&crate::internal::sigaction> for sigaction_t {
fn from(value: &crate::internal::sigaction) -> Self {
let mut this = sigaction_t::new();
this.sa_mask = value.sa_mask;
this.sa_flags = value.sa_flags;
this.sa_handler = match unsafe { value.__sigaction_handler.sa_handler } {
Some(v) => unsafe { core::mem::transmute(v) },
None => 0,
};

this
}
pub unsafe fn kill(pid: pid_t, sig: int) -> int {
crate::internal::kill(pid, sig)
}

pub unsafe fn sigaction(sig: int, act: *const sigaction_t, oact: *mut sigaction_t) -> int {
let c_act: crate::internal::sigaction = if act.is_null() {
crate::internal::sigaction::new()
} else {
(&*act).into()
};

let mut c_oact: crate::internal::sigaction = if oact.is_null() {
crate::internal::sigaction::new()
} else {
(&*oact).into()
};
mod internal {
use super::*;

let ret_val = crate::internal::sigaction(
sig,
if act.is_null() {
core::ptr::null()
} else {
&c_act
},
if oact.is_null() {
core::ptr::null_mut()
} else {
&mut c_oact
},
);

if ret_val == 0 && !oact.is_null() {
*oact = (&c_oact).into();
extern "C" {
pub(super) fn iox2_sigaction_func(
sig: int,
act: *const sigaction_t,
oact: *mut sigaction_t,
) -> int;
}

ret_val
}

pub unsafe fn kill(pid: pid_t, sig: int) -> int {
crate::internal::kill(pid, sig)
}
17 changes: 2 additions & 15 deletions iceoryx2-pal/posix/src/linux/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,8 @@ impl Struct for rlimit {}
pub type sched_param = crate::internal::sched_param;
impl Struct for sched_param {}

#[repr(C)]
pub struct sigaction_t {
pub sa_handler: sighandler_t,
pub sa_mask: sigset_t,
pub sa_flags: int,
}
impl Struct for sigaction_t {
fn new() -> Self {
Self {
sa_handler: 0,
sa_mask: sigset_t::new(),
sa_flags: 0,
}
}
}
pub type sigaction_t = crate::internal::iox2_sigaction;
impl Struct for sigaction_t {}

#[repr(C)]
pub struct stat_t {
Expand Down
Loading

0 comments on commit c965b3b

Please sign in to comment.