Mengfei Murphy
Mengfei Murphy

Reputation: 1089

In Posix how is type dev_t getting used?

What I am after is the meaning of such type and what interface can use it.

It is explained in Posix spec that dev_t is used for device IDs. However, what device id means for any object described by a path, which can be a file, a directy, a fifo or a physical device?

For example, calling stat() shall give you a struct including a member of such type; and you can stat any kinds of object in your file system. The device id should have different meanings for different file types then.

Upvotes: 8

Views: 15522

Answers (4)

Steve Lau
Steve Lau

Reputation: 1003

NOT an answer to the question, just update some info about st_dev

dev_t in current glibc (2.35) is 64-bit, with 32-bit major and minor numbers. glibc's default encoding is MMMM Mmmm mmmM MMmm, where M is a hex digit of the major number and m is a hex digit of the minor number. This is backward compatible with legacy systems where dev_t is 16 bits wide, encoded as MMmm. It is also backward compatible with the Linux kernel, which for some architectures uses 32-bit dev_t, encoded as mmmM MMmm.

One can use major(3) and minor(3) to decompose a dev_t:

$ cat main.c
#include <assert.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <stdio.h>

int main(void)
{
        struct stat buf;
        assert(0 == stat(".", &buf));

        printf("Major device ID: %d\n", major(buf.st_dev));
        printf("Minor device ID: %d\n", minor(buf.st_dev));
}

$ gccs main.c && ./a.out
Major device ID: 0
Minor device ID: 39

Upvotes: 1

user2948075
user2948075

Reputation:

Within the kernel, the dev_t type who is defined in is used to hold device numbers (major/minor). dev_t is a 32-bit quantity with 12 bits set aside for the major number and 20 for the minor number.

Upvotes: 6

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215287

The only use of dev_t in the vast majority of programs (ones which are portable and not connected to a single OS) is to determine that two file names or file descriptors refer to the same underlying file. This is true if and only if the st_ino and st_dev entries for the two files' stat structures match one another.

Basically, st_dev tells which "device" (e.g. mounted partition, network share, etc.) the file resides on, and st_ino is a unique identifier of the file within the context of a single device.

Upvotes: 11

Fred Foo
Fred Foo

Reputation: 363627

Actually, there are two dev_t-typed fields in struct stat:

  • st_dev is the "[d]evice ID of device containing file", so if two files have the same st_dev, they're on the same filesystem.
  • st_rdev is the device ID of the device denoted by a character or block special file, i.e. the files commonly encountered in /dev. It has no meaning for other types of files.

Upvotes: 7

Related Questions