Skip to content

Commit

Permalink
Use std::os::raw types instead of libc types
Browse files Browse the repository at this point in the history
Should fix issue #38

Signed-off-by: David Henningsson <diwic@ubuntu.com>
  • Loading branch information
diwic committed Mar 28, 2016
1 parent 0ed5422 commit d2e1129
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(dead_code)]

use libc::{c_void, c_char, c_uint, c_int};
use std::os::raw::{c_void, c_char, c_uint, c_int};

pub type DBusConnection = c_void;
pub type DBusMessage = c_void;
Expand Down
27 changes: 14 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use std::collections::LinkedList;
use std::cell::{Cell, RefCell};
use std::mem;
use std::os::unix::io::RawFd;
use std::os::raw::{c_void, c_char, c_int, c_uint};

#[allow(missing_docs)]
mod ffi;
Expand Down Expand Up @@ -172,7 +173,7 @@ pub struct Error {

unsafe impl Send for Error {}

fn c_str_to_slice(c: & *const libc::c_char) -> Option<&str> {
fn c_str_to_slice(c: & *const c_char) -> Option<&str> {
if *c == ptr::null() { None }
else { std::str::from_utf8( unsafe { CStr::from_ptr(*c).to_bytes() }).ok() }
}
Expand Down Expand Up @@ -243,12 +244,12 @@ impl std::fmt::Display for Error {

impl WatchEvent {
/// After running poll, this transforms the revents into a parameter you can send into `Connection::watch_handle`
pub fn from_revents(revents: libc::c_short) -> libc::c_uint {
pub fn from_revents(revents: libc::c_short) -> c_uint {
0 +
if (revents & libc::POLLIN) != 0 { WatchEvent::Readable as libc::c_uint } else { 0 } +
if (revents & libc::POLLOUT) != 0 { WatchEvent::Writable as libc::c_uint } else { 0 } +
if (revents & libc::POLLERR) != 0 { WatchEvent::Error as libc::c_uint } else { 0 } +
if (revents & libc::POLLHUP) != 0 { WatchEvent::Hangup as libc::c_uint } else { 0 }
if (revents & libc::POLLIN) != 0 { WatchEvent::Readable as c_uint } else { 0 } +
if (revents & libc::POLLOUT) != 0 { WatchEvent::Writable as c_uint } else { 0 } +
if (revents & libc::POLLERR) != 0 { WatchEvent::Error as c_uint } else { 0 } +
if (revents & libc::POLLHUP) != 0 { WatchEvent::Hangup as c_uint } else { 0 }
}
}

Expand Down Expand Up @@ -284,7 +285,7 @@ impl<'a> Iterator for ConnectionItems<'a> {

match self.timeout_ms {
Some(t) => {
let r = unsafe { ffi::dbus_connection_read_write_dispatch(self.c.conn(), t as libc::c_int) };
let r = unsafe { ffi::dbus_connection_read_write_dispatch(self.c.conn(), t as c_int) };
if !self.c.i.pending_items.borrow().is_empty() { continue };
if r == 0 { return None; }
return Some(ConnectionItem::Nothing);
Expand Down Expand Up @@ -316,7 +317,7 @@ pub struct Connection {
}

extern "C" fn filter_message_cb(conn: *mut ffi::DBusConnection, msg: *mut ffi::DBusMessage,
user_data: *mut libc::c_void) -> ffi::DBusHandlerResult {
user_data: *mut c_void) -> ffi::DBusHandlerResult {

let m = message::message_from_ptr(msg, true);
let i: &IConnection = unsafe { mem::transmute(user_data) };
Expand All @@ -339,7 +340,7 @@ extern "C" fn filter_message_cb(conn: *mut ffi::DBusConnection, msg: *mut ffi::D
}

extern "C" fn object_path_message_cb(conn: *mut ffi::DBusConnection, msg: *mut ffi::DBusMessage,
user_data: *mut libc::c_void) -> ffi::DBusHandlerResult {
user_data: *mut c_void) -> ffi::DBusHandlerResult {

let m = message::message_from_ptr(msg, true);
let i: &IConnection = unsafe { mem::transmute(user_data) };
Expand Down Expand Up @@ -389,7 +390,7 @@ impl Connection {
let mut e = Error::empty();
let response = unsafe {
ffi::dbus_connection_send_with_reply_and_block(self.conn(), message::get_message_ptr(&msg),
timeout_ms as libc::c_int, e.get_mut())
timeout_ms as c_int, e.get_mut())
};
if response == ptr::null_mut() {
return Err(e);
Expand Down Expand Up @@ -433,7 +434,7 @@ impl Connection {
dbus_internal_pad4: None,
};
let r = unsafe {
let user_data: *mut libc::c_void = mem::transmute(&*self.i);
let user_data: *mut c_void = mem::transmute(&*self.i);
ffi::dbus_connection_try_register_object_path(self.conn(), p.as_ptr(), &vtable, user_data, e.get_mut())
};
if r == 0 { Err(e) } else { Ok(()) }
Expand All @@ -449,7 +450,7 @@ impl Connection {
/// List registered object paths.
pub fn list_registered_object_paths(&self, path: &str) -> Vec<String> {
let p = to_c_str(path);
let mut clist: *mut *mut libc::c_char = ptr::null_mut();
let mut clist: *mut *mut c_char = ptr::null_mut();
let r = unsafe { ffi::dbus_connection_list_registered(self.conn(), p.as_ptr(), &mut clist) };
if r == 0 { panic!("Out of memory"); }
let mut v = Vec::new();
Expand Down Expand Up @@ -511,7 +512,7 @@ impl Connection {
/// The returned iterator will return pending items only, never block for new events.
///
/// See the `Watch` struct for an example.
pub fn watch_handle(&self, fd: RawFd, flags: libc::c_uint) -> ConnectionItems {
pub fn watch_handle(&self, fd: RawFd, flags: c_uint) -> ConnectionItems {
self.i.watches.as_ref().unwrap().watch_handle(fd, flags);
ConnectionItems { c: self, timeout_ms: None }
}
Expand Down
25 changes: 13 additions & 12 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::{ffi, Error, MessageType, TypeSig, libc, to_c_str, c_str_to_slice, in
use super::{BusName, Path, Interface, Member, ErrorName};
use std::os::unix::io::{RawFd, AsRawFd};
use std::ffi::CStr;
use std::os::raw::{c_void, c_char, c_int};

use super::arg::{Append, IterAppend, Get, Iter};

Expand Down Expand Up @@ -104,7 +105,7 @@ pub enum MessageItem {
fn iter_get_basic(i: &mut ffi::DBusMessageIter) -> i64 {
let mut c: i64 = 0;
unsafe {
let p: *mut libc::c_void = mem::transmute(&mut c);
let p: *mut c_void = mem::transmute(&mut c);
ffi::dbus_message_iter_get_basic(i, p);
}
c
Expand All @@ -113,15 +114,15 @@ fn iter_get_basic(i: &mut ffi::DBusMessageIter) -> i64 {
fn iter_get_f64(i: &mut ffi::DBusMessageIter) -> f64 {
let mut c: f64 = 0.0;
unsafe {
let p: *mut libc::c_void = mem::transmute(&mut c);
let p: *mut c_void = mem::transmute(&mut c);
ffi::dbus_message_iter_get_basic(i, p);
}
c
}

fn iter_append_f64(i: &mut ffi::DBusMessageIter, v: f64) {
unsafe {
let p: *const libc::c_void = mem::transmute(&v);
let p: *const c_void = mem::transmute(&v);
ffi::dbus_message_iter_append_basic(i, ffi::DBUS_TYPE_DOUBLE, p);
}
}
Expand Down Expand Up @@ -281,8 +282,8 @@ impl MessageItem {
let a = MessageItem::from_iter(&mut subiter);
let t = if a.len() > 0 { a[0].type_sig() } else {
let c = unsafe { ffi::dbus_message_iter_get_signature(&mut subiter) };
let s = c_str_to_slice(&(c as *const libc::c_char)).unwrap().to_string();
unsafe { ffi::dbus_free(c as *mut libc::c_void) };
let s = c_str_to_slice(&(c as *const c_char)).unwrap().to_string();
unsafe { ffi::dbus_free(c as *mut c_void) };
Cow::Owned(s)
};
Some(MessageItem::Array(a, t))
Expand All @@ -293,17 +294,17 @@ impl MessageItem {
Some(MessageItem::Struct(MessageItem::from_iter(&mut subiter)))
},
ffi::DBUS_TYPE_STRING => {
let mut c: *const libc::c_char = ptr::null();
let mut c: *const c_char = ptr::null();
unsafe {
let p: *mut libc::c_void = mem::transmute(&mut c);
let p: *mut c_void = mem::transmute(&mut c);
ffi::dbus_message_iter_get_basic(i, p);
};
Some(MessageItem::Str(c_str_to_slice(&c).expect("D-Bus string error").to_string()))
},
ffi::DBUS_TYPE_OBJECT_PATH => {
let mut c: *const libc::c_char = ptr::null();
let mut c: *const c_char = ptr::null();
unsafe {
let p: *mut libc::c_void = mem::transmute(&mut c);
let p: *mut c_void = mem::transmute(&mut c);
ffi::dbus_message_iter_get_basic(i, p);
};
let o = Path::new(c_str_to_slice(&c).expect("D-Bus object path error")).ok().expect("D-Bus object path error");
Expand Down Expand Up @@ -335,8 +336,8 @@ impl MessageItem {
fn iter_append_basic(&self, i: &mut ffi::DBusMessageIter, v: i64) {
let t = self.array_type();
unsafe {
let p: *const libc::c_void = mem::transmute(&v);
ffi::dbus_message_iter_append_basic(i, t as libc::c_int, p);
let p: *const c_void = mem::transmute(&v);
ffi::dbus_message_iter_append_basic(i, t as c_int, p);
}
}

Expand Down Expand Up @@ -845,7 +846,7 @@ mod test {
if let Some(&MessageItem::UnixFd(ref z)) = m.get_items().get(0) {
println!("Got {:?}", m.get_items());
let mut q: libc::c_char = 100;
assert_eq!(1, unsafe { libc::read(z.as_raw_fd(), &mut q as *mut i8 as *mut libc::c_void, 1) });
assert_eq!(1, unsafe { libc::read(z.as_raw_fd(), &mut q as *mut _ as *mut libc::c_void, 1) });
assert_eq!(q, 'z' as libc::c_char);
break;
}
Expand Down
24 changes: 12 additions & 12 deletions src/msgarg.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#![allow(dead_code)]

use super::{ffi, libc, Message};
use super::{ffi, Message};
use super::message::get_message_ptr;
use std::{mem, ptr};
use std::marker::PhantomData;

use std::ffi::{CStr, CString};

use std::os::raw::{c_void, c_char};
use super::{Signature, Path, OwnedFd};

fn check(f: &str, i: u32) { if i == 0 { panic!("D-Bus error: '{}' failed", f) }}

fn ffi_iter() -> ffi::DBusMessageIter { unsafe { mem::zeroed() }}

fn arg_append_basic(i: *mut ffi::DBusMessageIter, arg_type: i32, v: i64) {
let p = &v as *const _ as *const libc::c_void;
let p = &v as *const _ as *const c_void;
unsafe {
check("dbus_message_iter_append_basic", ffi::dbus_message_iter_append_basic(i, arg_type, p));
};
Expand All @@ -24,13 +24,13 @@ fn arg_get_basic(i: *mut ffi::DBusMessageIter, arg_type: i32) -> Option<i64> {
let mut c = 0i64;
unsafe {
if ffi::dbus_message_iter_get_arg_type(i) != arg_type { return None };
ffi::dbus_message_iter_get_basic(i, &mut c as *mut _ as *mut libc::c_void);
ffi::dbus_message_iter_get_basic(i, &mut c as *mut _ as *mut c_void);
}
Some(c)
}

fn arg_append_f64(i: *mut ffi::DBusMessageIter, arg_type: i32, v: f64) {
let p = &v as *const _ as *const libc::c_void;
let p = &v as *const _ as *const c_void;
unsafe {
check("dbus_message_iter_append_basic", ffi::dbus_message_iter_append_basic(i, arg_type, p));
};
Expand All @@ -40,14 +40,14 @@ fn arg_get_f64(i: *mut ffi::DBusMessageIter, arg_type: i32) -> Option<f64> {
let mut c = 0f64;
unsafe {
if ffi::dbus_message_iter_get_arg_type(i) != arg_type { return None };
ffi::dbus_message_iter_get_basic(i, &mut c as *mut _ as *mut libc::c_void);
ffi::dbus_message_iter_get_basic(i, &mut c as *mut _ as *mut c_void);
}
Some(c)
}

fn arg_append_str(i: *mut ffi::DBusMessageIter, arg_type: i32, v: &CStr) {
let p = v.as_ptr();
let q = &p as *const _ as *const libc::c_void;
let q = &p as *const _ as *const c_void;
unsafe {
check("dbus_message_iter_append_basic", ffi::dbus_message_iter_append_basic(i, arg_type, q));
};
Expand All @@ -56,8 +56,8 @@ fn arg_append_str(i: *mut ffi::DBusMessageIter, arg_type: i32, v: &CStr) {
unsafe fn arg_get_str<'a>(i: *mut ffi::DBusMessageIter, arg_type: i32) -> Option<&'a CStr> {
if ffi::dbus_message_iter_get_arg_type(i) != arg_type { return None };
let mut p = ptr::null_mut();
ffi::dbus_message_iter_get_basic(i, &mut p as *mut _ as *mut libc::c_void);
Some(CStr::from_ptr(p as *const libc::c_char))
ffi::dbus_message_iter_get_basic(i, &mut p as *mut _ as *mut c_void);
Some(CStr::from_ptr(p as *const c_char))
}

/// Types that can represent a D-Bus message argument implement this trait.
Expand Down Expand Up @@ -184,7 +184,7 @@ impl<'a> Append for &'a str {
bb.push(0);
Cow::Owned(bb)
};
let z = unsafe { CStr::from_ptr(v.as_ptr() as *const libc::c_char) };
let z = unsafe { CStr::from_ptr(v.as_ptr() as *const c_char) };
arg_append_str(&mut i.0, Self::arg_type(), &z)
}
}
Expand Down Expand Up @@ -309,7 +309,7 @@ impl<'a, T: Arg + Append + Clone> Append for &'a [T] {

i.append_container(Self::arg_type(), Some(T::signature().as_cstr()), |s|
if can_fixed_array { unsafe { check("dbus_message_iter_append_fixed_array",
ffi::dbus_message_iter_append_fixed_array(&mut s.0, a.0, &zptr as *const _ as *const libc::c_void, zlen)) }}
ffi::dbus_message_iter_append_fixed_array(&mut s.0, a.0, &zptr as *const _ as *const c_void, zlen)) }}
else { for arg in z { arg.clone().append(s) }});
}
}
Expand All @@ -322,7 +322,7 @@ impl<'a, T: Get<'a> + FixedArray> Get<'a> for &'a [T] {

let mut v = ptr::null_mut();
let mut i = 0;
ffi::dbus_message_iter_get_fixed_array(&mut si.0, &mut v as *mut _ as *mut libc::c_void, &mut i);
ffi::dbus_message_iter_get_fixed_array(&mut si.0, &mut v as *mut _ as *mut c_void, &mut i);
Some(::std::slice::from_raw_parts(v, i as usize))
})
}
Expand Down
13 changes: 7 additions & 6 deletions src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{str, fmt, ops, default};
use std::ffi::{CStr, CString};
use std::borrow::Cow;
use Error;
use libc;
use std::os::raw::c_char;

macro_rules! cstring_wrapper {
($t: ident, $s: ident) => {
Expand All @@ -25,9 +25,9 @@ impl<'m> $t<'m> {
pub fn from_slice(s: &'m [u8]) -> Result<$t<'m>, String> {
if s.len() == 0 || s[s.len()-1] != 0 { return $t::new(s) };
let mut e = Error::empty();
let b = unsafe { ffi::$s(s.as_ptr() as *const libc::c_char, e.get_mut()) };
let b = unsafe { ffi::$s(s.as_ptr() as *const c_char, e.get_mut()) };
if b != 0 {
let c = unsafe { CStr::from_ptr(s.as_ptr() as *const libc::c_char) };
let c = unsafe { CStr::from_ptr(s.as_ptr() as *const c_char) };
Ok($t(Cow::Borrowed(c)))
}
else { Err(e.message().unwrap().into()) }
Expand All @@ -37,7 +37,7 @@ impl<'m> $t<'m> {
/// It's up to you to guarantee that s ends with a \0 and is valid.
pub unsafe fn from_slice_unchecked(s: &'m [u8]) -> $t<'m> {
debug_assert!(s[s.len()-1] == 0);
$t(Cow::Borrowed(CStr::from_ptr(s.as_ptr() as *const libc::c_char)))
$t(Cow::Borrowed(CStr::from_ptr(s.as_ptr() as *const c_char)))
}

/// View this struct as a CStr.
Expand Down Expand Up @@ -106,7 +106,7 @@ cstring_wrapper!(Path, dbus_validate_path);

// This is needed so one can make arrays of paths easily
impl<'a> default::Default for Path<'a> {
fn default() -> Path<'a> { Path(Cow::Borrowed(unsafe { CStr::from_ptr(b"/\0".as_ptr() as *const libc::c_char)})) }
fn default() -> Path<'a> { Path(Cow::Borrowed(unsafe { CStr::from_ptr(b"/\0".as_ptr() as *const c_char)})) }
}

/// A wrapper around a string that is guaranteed to be
Expand Down Expand Up @@ -139,9 +139,10 @@ cstring_wrapper!(ErrorName, dbus_validate_error_name);

#[test]
fn some_path() {
use std::os::raw::c_char;
let p1: Path = "/valid".into();
let p2 = Path::new("##invalid##");
assert_eq!(p1, Path(Cow::Borrowed(unsafe { CStr::from_ptr(b"/valid\0".as_ptr() as *const libc::c_char) })));
assert_eq!(p1, Path(Cow::Borrowed(unsafe { CStr::from_ptr(b"/valid\0".as_ptr() as *const c_char) })));
assert_eq!(p2, Err("Object path was not valid: '##invalid##'".into()));
}

Expand Down
13 changes: 7 additions & 6 deletions src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::Connection;
use std::mem;
use std::cell::RefCell;
use std::os::unix::io::{RawFd, AsRawFd};
use std::os::raw::{c_void, c_uint};

/// A file descriptor to watch for incoming events (for async I/O).
///
Expand Down Expand Up @@ -78,7 +79,7 @@ impl WatchList {
}


pub fn watch_handle(&self, fd: RawFd, flags: libc::c_uint) {
pub fn watch_handle(&self, fd: RawFd, flags: c_uint) {
// println!("watch_handle {} flags {}", fd, flags);
for &q in self.watches.borrow().iter() {
let w = self.get_watch(q);
Expand All @@ -99,8 +100,8 @@ impl WatchList {
let enabled = self.watches.borrow().contains(&watch) && unsafe { ffi::dbus_watch_get_enabled(watch) != 0 };
let flags = unsafe { ffi::dbus_watch_get_flags(watch) };
if enabled {
w.read = (flags & ffi::DBusWatchEvent::Readable as libc::c_uint) != 0;
w.write = (flags & ffi::DBusWatchEvent::Writable as libc::c_uint) != 0;
w.read = (flags & ffi::DBusWatchEvent::Readable as c_uint) != 0;
w.write = (flags & ffi::DBusWatchEvent::Writable as c_uint) != 0;
}
// println!("Get watch fd {:?} ptr {:?} enabled {:?} flags {:?}", w, watch, enabled, flags);
w
Expand Down Expand Up @@ -133,22 +134,22 @@ impl WatchList {
}
}

extern "C" fn add_watch_cb(watch: *mut ffi::DBusWatch, data: *mut libc::c_void) -> u32 {
extern "C" fn add_watch_cb(watch: *mut ffi::DBusWatch, data: *mut c_void) -> u32 {
let wlist: &WatchList = unsafe { mem::transmute(data) };
// println!("Add watch {:?}", watch);
wlist.watches.borrow_mut().push(watch);
wlist.update(watch);
1
}

extern "C" fn remove_watch_cb(watch: *mut ffi::DBusWatch, data: *mut libc::c_void) {
extern "C" fn remove_watch_cb(watch: *mut ffi::DBusWatch, data: *mut c_void) {
let wlist: &WatchList = unsafe { mem::transmute(data) };
// println!("Removed watch {:?}", watch);
wlist.watches.borrow_mut().retain(|w| *w != watch);
wlist.update(watch);
}

extern "C" fn toggled_watch_cb(watch: *mut ffi::DBusWatch, data: *mut libc::c_void) {
extern "C" fn toggled_watch_cb(watch: *mut ffi::DBusWatch, data: *mut c_void) {
let wlist: &WatchList = unsafe { mem::transmute(data) };
// println!("Toggled watch {:?}", watch);
wlist.update(watch);
Expand Down

0 comments on commit d2e1129

Please sign in to comment.