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

x/sys/unix: no major, minor, and mkdev functions #8106

Closed
gopherbot opened this issue May 27, 2014 · 12 comments
Closed

x/sys/unix: no major, minor, and mkdev functions #8106

gopherbot opened this issue May 27, 2014 · 12 comments

Comments

@gopherbot
Copy link

by tim.thelion:

Hello, I am in need of the Major, Minor, and Mkdev functions from the linux kernel:
https://github.com/torvalds/linux/blob/master/include/linux/kdev_t.h#L9 for converting a
device number produced by "stat.Sys().(*syscall.Stat_t)" to it's major and
minor components, and vice versa.  As you can see, simply imitating the kernel sources
won't work: http://play.golang.org/p/zvRqa2eEjN


Thank you for your time,

Timothy Hobbs
@ianlancetaylor
Copy link
Contributor

Comment 1:

Labels changed: added repo-main, release-go1.4.

@gopherbot
Copy link
Author

Comment 2 by tim.thelion:

Hopefully the bit-fiddling is all right here:
https://github.com/dotcloud/docker/pull/4191/files#diff-6ce9e79ddb91a3f06352abe1f2c72ecbR468
func Major(devNumber int) int64{
    return int64((devNumber >> 8) & 0xfff)
}
func Minor(devNumber int) int64{
    return int64((devNumber & 0xff) | ((devNumber >> 12) & 0xfff00))
}
func Mkdev(majorNumber int64,minorNumber int64) int{
    return int((majorNumber << 8) | (minorNumber & 0xff) | ((minorNumber & 0xfff00) << 12))
}
Not so sure about the last one, as I wrote it myself and I don't really understand the
format of the numbers and I am not an expert at bit fiddling.

@rsc
Copy link
Contributor

rsc commented Sep 18, 2014

Comment 3:

Labels changed: added repo-sys, release-none, removed repo-main, release-go1.4.

Status changed to Accepted.

@mikioh mikioh changed the title go.sys/unix: no major, minor, and mkdev functions unix: no major, minor, and mkdev functions Jan 7, 2015
@rsc rsc added this to the Unplanned milestone Apr 10, 2015
@rsc rsc changed the title unix: no major, minor, and mkdev functions x/sys/unix: no major, minor, and mkdev functions Apr 14, 2015
@rsc rsc modified the milestones: Unreleased, Unplanned Apr 14, 2015
@rsc rsc removed the repo-sys label Apr 14, 2015
@dswarbrick
Copy link

dswarbrick commented Nov 21, 2016

The functions in comment 2 might work for Linux, but FreeBSD is different, as are most likely other flavours of Unix.

Whilst Linux (2.6+) specifies 12 bits for the major number and 20 bits for the minor number, FreeBSD states:

     The major() macro returns a device major number that has a value between
     0 and 255.  The minor() macro returns a device minor number whose value
     can span the complete range of an int.

(https://www.freebsd.org/cgi/man.cgi?query=major()&sektion=#end)

Also, if we're being pedantic, shouldn't the functions take (and return) uint64 instead, so as to comply with https://golang.org/pkg/syscall/#Stat_t ?

@gopherbot
Copy link
Author

CL https://golang.org/cl/50550 mentions this issue.

@tklauser
Copy link
Member

I sent CL https://golang.org/cl/50550 adding the requested functions on Linux.

For FreeBSD, the macros in https://github.com/freebsd/freebsd/blob/master/sys/sys/types.h#L372-L374 could possibly be re-implemented in x/sys/unix. As I don't have a FreeBSD system at hand for testing, I didn't add them though.

@dswarbrick
Copy link

After a bit of searching, I found the macros that Darwin uses. It looks to be more or less the same as BSD, which is not that big of a surprise.

#define	major(x)	((int32_t)(((u_int32_t)(x) >> 24) & 0xff))
#define	minor(x)	((int32_t)((x) & 0xffffff))
#define	makedev(x,y) ((dev_t)(((x) << 24) | (y)))

(https://github.com/apple/darwin-xnu/blob/master/bsd/sys/types.h)

@gopherbot
Copy link
Author

Change https://golang.org/cl/60610 mentions this issue: unix: add Major, Minor and Mkdev functions on Darwin

gopherbot pushed a commit to golang/sys that referenced this issue Sep 1, 2017
Add Major, Minor and Mkdev functions for converting devices numbers to
their major/minor components and vice versa.

The functions follow the behavior of the macros defined in Darwin's
sys/types.h header. However, the parameter and return types are changed
to match the respective Linux implementation of these functions.

Test the conversion macros with some well-known static device numbers
for devices which should be present on any Darwin system.

Updates golang/go#8106

Change-Id: I1862be64684cc1b5a53e15a883819571e368cb2b
Reviewed-on: https://go-review.googlesource.com/60610
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
@gopherbot
Copy link
Author

Change https://golang.org/cl/60970 mentions this issue: unix: add Major, Minor and Mkdev functions on NetBSD

gopherbot pushed a commit to golang/sys that referenced this issue Sep 1, 2017
Add Major, Minor and Mkdev functions for converting devices numbers to
their major/minor components and vice versa.

The functions follow the behavior of the macros defined in NetBSD's
sys/types.h header. However, the parameter and return types are changed
to match the existing implementations of these functions.

Test the conversion macros with some well-known device numbers.

Updates golang/go#8106

Change-Id: I536d6d2622f6fe9be3c1ed3beb266745fe4bfb6e
Reviewed-on: https://go-review.googlesource.com/60970
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
@gopherbot
Copy link
Author

Change https://golang.org/cl/61631 mentions this issue: unix: add Major, Minor and Mkdev functions on FreeBSD

@gopherbot
Copy link
Author

Change https://golang.org/cl/61630 mentions this issue: unix: add Major, Minor and Mkdev functions on Dragonfly

gopherbot pushed a commit to golang/sys that referenced this issue Sep 8, 2017
Add Major, Minor and Mkdev functions for converting devices numbers to
their major/minor components and vice versa.

The functions follow the behavior of the macros defined in DragonFlyBSD's
sys/types.h header. However, the parameter and return types are changed
to match the existing implementations of these functions.

Test the conversion macros with some well-known device numbers.

Updates golang/go#8106

Change-Id: I84c128dff3108821caaa75dcec620cf5bdb1f32d
Reviewed-on: https://go-review.googlesource.com/61630
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
gopherbot pushed a commit to golang/sys that referenced this issue Sep 9, 2017
Add Major, Minor and Mkdev functions for converting devices numbers to
their major/minor components and vice versa.

The functions follow the behavior of the macros defined in FreeBSD's
sys/types.h header. However, the parameter and return types are changed
to match the existing implementations of these functions.

Because FreeBSD dynamically allocates major/minor device numbers through
devfs at runtime, testing the conversion macros against well-known
device numbers is not possible.

Updates golang/go#8106

Change-Id: I9874e63f388cfc5c0d5cc47d4d0f6ccc489b28f2
Reviewed-on: https://go-review.googlesource.com/61631
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
micanzhang pushed a commit to micanzhang/sys that referenced this issue Sep 12, 2017
Add Major, Minor and Mkdev functions for converting devices numbers to
their major/minor components and vice versa.

The functions follow the behavior of glibc's corresponding macros. Also
add an explanatory comment about the device number format, so the magic
numbers make more sense.

Test the conversion macros with some well-known device numbers for
devices which should be present on any Linux system.

Fixes golang/go#8106

Change-Id: Id336317985d6ac85ee83bc54e5f23703257c9121
Reviewed-on: https://go-review.googlesource.com/50550
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
micanzhang pushed a commit to micanzhang/sys that referenced this issue Sep 12, 2017
Add Major, Minor and Mkdev functions for converting devices numbers to
their major/minor components and vice versa.

The functions follow the behavior of the macros defined in Darwin's
sys/types.h header. However, the parameter and return types are changed
to match the respective Linux implementation of these functions.

Test the conversion macros with some well-known static device numbers
for devices which should be present on any Darwin system.

Updates golang/go#8106

Change-Id: I1862be64684cc1b5a53e15a883819571e368cb2b
Reviewed-on: https://go-review.googlesource.com/60610
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
micanzhang pushed a commit to micanzhang/sys that referenced this issue Sep 12, 2017
Add Major, Minor and Mkdev functions for converting devices numbers to
their major/minor components and vice versa.

The functions follow the behavior of the macros defined in NetBSD's
sys/types.h header. However, the parameter and return types are changed
to match the existing implementations of these functions.

Test the conversion macros with some well-known device numbers.

Updates golang/go#8106

Change-Id: I536d6d2622f6fe9be3c1ed3beb266745fe4bfb6e
Reviewed-on: https://go-review.googlesource.com/60970
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
micanzhang pushed a commit to micanzhang/sys that referenced this issue Sep 12, 2017
Add Major, Minor and Mkdev functions for converting devices numbers to
their major/minor components and vice versa.

The functions follow the behavior of the macros defined in DragonFlyBSD's
sys/types.h header. However, the parameter and return types are changed
to match the existing implementations of these functions.

Test the conversion macros with some well-known device numbers.

Updates golang/go#8106

Change-Id: I84c128dff3108821caaa75dcec620cf5bdb1f32d
Reviewed-on: https://go-review.googlesource.com/61630
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
micanzhang pushed a commit to micanzhang/sys that referenced this issue Sep 12, 2017
Add Major, Minor and Mkdev functions for converting devices numbers to
their major/minor components and vice versa.

The functions follow the behavior of the macros defined in FreeBSD's
sys/types.h header. However, the parameter and return types are changed
to match the existing implementations of these functions.

Because FreeBSD dynamically allocates major/minor device numbers through
devfs at runtime, testing the conversion macros against well-known
device numbers is not possible.

Updates golang/go#8106

Change-Id: I9874e63f388cfc5c0d5cc47d4d0f6ccc489b28f2
Reviewed-on: https://go-review.googlesource.com/61631
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
@gopherbot
Copy link
Author

Change https://golang.org/cl/63070 mentions this issue: unix: add Major, Minor and Mkdev functions on OpenBSD

gopherbot pushed a commit to golang/sys that referenced this issue Sep 12, 2017
Add Major, Minor and Mkdev functions for converting devices numbers to
their major/minor components and vice versa.

The functions follow the behavior of the macros defined in OpenBSD's
sys/types.h header. However, the parameter and return types are changed
to match the existing implementations of these functions.

Test the conversion macros with some well-known device numbers.

Updates golang/go#8106

Change-Id: Ia50b7ccab18ff7c7e9dd77ddc7e4aa6cf5c79963
Reviewed-on: https://go-review.googlesource.com/63070
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
@golang golang locked and limited conversation to collaborators Sep 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants