Skip to content

Commit

Permalink
Auto merge of #2999 - SteveLauC:major/minor-on-BSDs, r=JohnTitor
Browse files Browse the repository at this point in the history
add major/minor on BSDs/illumos

This PR adds `major/minor` on BSDs and `major/minor/makedev` on illumos.

Ref:
* [FreeBSD 11](https://github.com/freebsd/freebsd-src/blob/3e9337c6b211e778829ed3af783cd41447a8721b/sys/sys/types.h#L372-L374)
   ```c
   #define	major(x)	((int)(((u_int)(x) >> 8)&0xff))	/* major number */
   #define	minor(x)	((int)((x)&0xffff00ff))		/* minor number */
   ```
* [FreeBSD 12/13/14](https://github.com/freebsd/freebsd-src/blob/3d98e253febf816e6e2aea7d3b1c013c421895de/sys/sys/types.h#L332-L341)
  ```c
  static __inline int
  __major(dev_t _d)
  {
      return (((_d >> 32) & 0xffffff00) | ((_d >> 8) & 0xff));
  }

  static __inline int
  __minor(dev_t _d)
  {
      return (((_d >> 24) & 0xff00) | (_d & 0xffff00ff));
  }
  ```
* [DragonFly](https://github.com/DragonFlyBSD/DragonFlyBSD/blob/d7a10f947f5344fc95e874ca3b83e9e8d0986b25/sys/sys/types.h#L170-L171)
  ```c
  #define	major(x)	((int)(((u_int)(x) >> 8)&0xff)) /* major number */
  #define	minor(x)	((int)((x)&0xffff00ff))         /* minor number */
  ```
* [NetBSD](https://github.com/NetBSD/src/blob/a25a6fec1b0a676fc5b36fa01b2990e775775d90/sys/sys/types.h#L264-L266)
   ```c
  #define	major(x)	((devmajor_t)(((uint32_t)(x) & 0x000fff00) >>  8))
  #define	minor(x)	((devminor_t)((((uint32_t)(x) & 0xfff00000) >> 12) | \
				                      (((uint32_t)(x) & 0x000000ff) >>  0)))
   ```
* [OpenBSD](https://github.com/openbsd/src/blob/05cbc9aa8d8372e83274c75e35add6b8073c26f5/sys/sys/types.h#L211-L212)
   ```c
   #define	major(x)	(((unsigned)(x) >> 8) & 0xff)
   #define	minor(x)	((unsigned)((x) & 0xff) | (((x) & 0xffff0000) >> 8))
   ```
* illumos:

  1. [mkdev.c](https://github.com/illumos/illumos-gate/blob/8b26092d555bd1deaacf79ea64da374602aefb65/usr/src/lib/libc/port/gen/mkdev.c#L40-L146)
  2. [mkdev.h](https://github.com/illumos/illumos-gate/blob/8b26092d555bd1deaacf79ea64da374602aefb65/usr/src/uts/common/sys/mkdev.h#L97-L99)
  • Loading branch information
bors committed May 6, 2023
2 parents a11d63a + 0449416 commit cdf9aa7
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,7 @@ fn test_solarish(target: &str) {
"sys/ioctl.h",
"sys/lgrp_user.h",
"sys/loadavg.h",
"sys/mkdev.h",
"sys/mman.h",
"sys/mount.h",
"sys/priv.h",
Expand Down
8 changes: 8 additions & 0 deletions src/unix/bsd/freebsdlike/dragonfly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,14 @@ f! {
let (idx, offset) = ((cpu >> 6) & 3, cpu & 63);
0 != cpuset.ary[idx] & (1 << offset)
}

pub fn major(dev: ::dev_t) -> ::c_int {
((dev >> 8) & 0xff) as ::c_int
}

pub fn minor(dev: ::dev_t) -> ::c_int {
(dev & 0xffff00ff) as ::c_int
}
}

safe_f! {
Expand Down
10 changes: 10 additions & 0 deletions src/unix/bsd/freebsdlike/freebsd/freebsd11/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,16 @@ safe_f! {
}
}

f! {
pub fn major(dev: ::dev_t) -> ::c_int {
((dev >> 8) & 0xff) as ::c_int
}

pub fn minor(dev: ::dev_t) -> ::c_int {
(dev & 0xffff00ff) as ::c_int
}
}

extern "C" {
// Return type ::c_int was removed in FreeBSD 12
pub fn setgrent() -> ::c_int;
Expand Down
10 changes: 10 additions & 0 deletions src/unix/bsd/freebsdlike/freebsd/freebsd12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,16 @@ safe_f! {
}
}

f! {
pub fn major(dev: ::dev_t) -> ::c_int {
(((dev >> 32) & 0xffffff00) | ((dev >> 8) & 0xff)) as ::c_int
}

pub fn minor(dev: ::dev_t) -> ::c_int {
(((dev >> 24) & 0xff00) | (dev & 0xffff00ff)) as ::c_int
}
}

extern "C" {
pub fn setgrent();
pub fn mprotect(addr: *mut ::c_void, len: ::size_t, prot: ::c_int) -> ::c_int;
Expand Down
10 changes: 10 additions & 0 deletions src/unix/bsd/freebsdlike/freebsd/freebsd13/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,16 @@ safe_f! {
}
}

f! {
pub fn major(dev: ::dev_t) -> ::c_int {
(((dev >> 32) & 0xffffff00) | ((dev >> 8) & 0xff)) as ::c_int
}

pub fn minor(dev: ::dev_t) -> ::c_int {
(((dev >> 24) & 0xff00) | (dev & 0xffff00ff)) as ::c_int
}
}

extern "C" {
pub fn setgrent();
pub fn mprotect(addr: *mut ::c_void, len: ::size_t, prot: ::c_int) -> ::c_int;
Expand Down
10 changes: 10 additions & 0 deletions src/unix/bsd/freebsdlike/freebsd/freebsd14/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,16 @@ safe_f! {
}
}

f! {
pub fn major(dev: ::dev_t) -> ::c_int {
(((dev >> 32) & 0xffffff00) | ((dev >> 8) & 0xff)) as ::c_int
}

pub fn minor(dev: ::dev_t) -> ::c_int {
(((dev >> 24) & 0xff00) | (dev & 0xffff00ff)) as ::c_int
}
}

extern "C" {
pub fn setgrent();
pub fn mprotect(addr: *mut ::c_void, len: ::size_t, prot: ::c_int) -> ::c_int;
Expand Down
11 changes: 11 additions & 0 deletions src/unix/bsd/netbsdlike/netbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2472,6 +2472,17 @@ f! {
pub fn PROT_MPROTECT_EXTRACT(x: ::c_int) -> ::c_int {
(x >> 3) & 0x7
}

pub fn major(dev: ::dev_t) -> ::c_int {
(((dev as u32) & 0x000fff00) >> 8) as ::c_int
}

pub fn minor(dev: ::dev_t) -> ::c_int {
let mut res = 0;
res |= ((dev as u32) & 0xfff00000) >> 12;
res |= (dev as u32) & 0x000000ff;
res as ::c_int
}
}

safe_f! {
Expand Down
13 changes: 13 additions & 0 deletions src/unix/bsd/netbsdlike/openbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,19 @@ f! {
(_ALIGN(::mem::size_of::<::cmsghdr>()) + _ALIGN(length as usize))
as ::c_uint
}

pub fn major(dev: ::dev_t) -> ::c_uint{
((dev as ::c_uint) >> 8) & 0xff
}

pub fn minor(dev: ::dev_t) -> ::c_uint {
let dev = dev as ::c_uint;
let mut res = 0;
res |= (dev) & 0xff;
res |= ((dev) & 0xffff0000) >> 8;

res
}
}

safe_f! {
Expand Down
18 changes: 18 additions & 0 deletions src/unix/solarish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2584,6 +2584,8 @@ const _CMSG_HDR_ALIGNMENT: usize = 4;

const _CMSG_DATA_ALIGNMENT: usize = ::mem::size_of::<::c_int>();

const NEWDEV: ::c_int = 1;

const_fn! {
{const} fn _CMSG_HDR_ALIGN(p: usize) -> usize {
(p + _CMSG_HDR_ALIGNMENT - 1) & !(_CMSG_HDR_ALIGNMENT - 1)
Expand Down Expand Up @@ -3198,6 +3200,10 @@ extern "C" {
) -> ::c_int;

pub fn sync();

fn __major(version: ::c_int, devnum: ::dev_t) -> ::major_t;
fn __minor(version: ::c_int, devnum: ::dev_t) -> ::minor_t;
fn __makedev(version: ::c_int, majdev: ::major_t, mindev: ::minor_t) -> ::dev_t;
}

#[link(name = "sendfile")]
Expand Down Expand Up @@ -3254,6 +3260,18 @@ extern "C" {
pub fn lgrp_root(cookie: ::lgrp_cookie_t) -> ::lgrp_id_t;
}

pub unsafe fn major(device: ::dev_t) -> ::major_t {
__major(NEWDEV, device)
}

pub unsafe fn minor(device: ::dev_t) -> ::minor_t {
__minor(NEWDEV, device)
}

pub unsafe fn makedev(maj: ::major_t, min: ::minor_t) -> ::dev_t {
__makedev(NEWDEV, maj, min)
}

mod compat;
pub use self::compat::*;

Expand Down

0 comments on commit cdf9aa7

Please sign in to comment.