Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add syslog macros LOG_MASK/LOG_UPTO #4238

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions libc-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,8 @@ harness = false
name = "primitive_types"
path = "test/primitive_types.rs"
harness = true

[[test]]
name = "syslog"
path = "test/syslog.rs"
harness = true
2 changes: 2 additions & 0 deletions libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ fn do_cc() {
{
cc::Build::new().file("src/makedev.c").compile("makedev");
}

cc::Build::new().file("src/syslog.c").compile("syslog");
}
if target.contains("android") || target.contains("linux") {
cc::Build::new().file("src/errqueue.c").compile("errqueue");
Expand Down
2 changes: 2 additions & 0 deletions libc-test/semver/unix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ LOG_LOCAL6
LOG_LOCAL7
LOG_LPR
LOG_MAIL
LOG_MASK
LOG_NDELAY
LOG_NEWS
LOG_NOTICE
Expand All @@ -218,6 +219,7 @@ LOG_ODELAY
LOG_PID
LOG_PRIMASK
LOG_SYSLOG
LOG_UPTO
LOG_USER
LOG_UUCP
LOG_WARNING
Expand Down
14 changes: 14 additions & 0 deletions libc-test/src/syslog.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <sys/syslog.h>

// Since the syslog(3) macros are macros instead of functions, they aren't
// available to FFI. libc must reimplement them, which is error-prone. This
// file provides FFI access to the actual macros so they can be tested against
// the Rust reimplementations.

int LOG_MASK_ffi(int priority) {
return LOG_MASK(priority);
}

int LOG_UPTO_ffi(int priority) {
return LOG_UPTO(priority);
}
55 changes: 55 additions & 0 deletions libc-test/test/syslog.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#[cfg(unix)]
mod t {
use libc::c_int;

use libc::LOG_ALERT;
use libc::LOG_CRIT;
use libc::LOG_DEBUG;
use libc::LOG_EMERG;
use libc::LOG_ERR;
use libc::LOG_INFO;
use libc::LOG_NOTICE;
use libc::LOG_WARNING;

use libc::LOG_MASK;
use libc::LOG_UPTO;

extern "C" {
pub fn LOG_MASK_ffi(priority: c_int) -> c_int;
pub fn LOG_UPTO_ffi(priority: c_int) -> c_int;
}

#[test]
fn test_log_mask() {
// Ensure our Rust impl returns the same value as the C macros

// *_ffi interfaces are unsafe
unsafe {
assert_eq!(LOG_MASK(LOG_EMERG), LOG_MASK_ffi(LOG_EMERG));
assert_eq!(LOG_MASK(LOG_ALERT), LOG_MASK_ffi(LOG_ALERT));
assert_eq!(LOG_MASK(LOG_CRIT), LOG_MASK_ffi(LOG_CRIT));
assert_eq!(LOG_MASK(LOG_ERR), LOG_MASK_ffi(LOG_ERR));
assert_eq!(LOG_MASK(LOG_WARNING), LOG_MASK_ffi(LOG_WARNING));
assert_eq!(LOG_MASK(LOG_NOTICE), LOG_MASK_ffi(LOG_NOTICE));
assert_eq!(LOG_MASK(LOG_INFO), LOG_MASK_ffi(LOG_INFO));
assert_eq!(LOG_MASK(LOG_DEBUG), LOG_MASK_ffi(LOG_DEBUG));
}
}

#[test]
fn test_log_upto() {
// Ensure our Rust impl returns the same value as the C macros

// *_ffi interfaces are unsafe
unsafe {
assert_eq!(LOG_UPTO(LOG_EMERG), LOG_UPTO_ffi(LOG_EMERG));
assert_eq!(LOG_UPTO(LOG_ALERT), LOG_UPTO_ffi(LOG_ALERT));
assert_eq!(LOG_UPTO(LOG_CRIT), LOG_UPTO_ffi(LOG_CRIT));
assert_eq!(LOG_UPTO(LOG_ERR), LOG_UPTO_ffi(LOG_ERR));
assert_eq!(LOG_UPTO(LOG_WARNING), LOG_UPTO_ffi(LOG_WARNING));
assert_eq!(LOG_UPTO(LOG_NOTICE), LOG_UPTO_ffi(LOG_NOTICE));
assert_eq!(LOG_UPTO(LOG_INFO), LOG_UPTO_ffi(LOG_INFO));
assert_eq!(LOG_UPTO(LOG_DEBUG), LOG_UPTO_ffi(LOG_DEBUG));
}
}
}
8 changes: 8 additions & 0 deletions src/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,14 @@ safe_f! {
pub {const} fn ntohs(netshort: u16) -> u16 {
u16::from_be(netshort)
}

pub {const} fn LOG_MASK(priority: c_int) -> c_int {
1 << priority
}

pub {const} fn LOG_UPTO(priority: c_int) -> c_int {
(1 << ((priority) + 1)) - 1
}
}

cfg_if! {
Expand Down
Loading