Question

Hi I'm running vfs_stat, fd is an opened file desciptor of /dev/tty0:

set_fs (KERNEL_DS);
if (vfs_fstat (fd, &stat))
{
    goto out3;
}

if (stat.mode & S_IFCHR)
    printk (KERN_INFO "opening %s (dev %d)\n", filename, stat.rdev);

And it prints:

[ 8657.480625] opening /dev/tty0 (dev 4194304)

So now I need to retrieve major number of the device, but I couldn't find major() or minor() definition in linux kernel.

I found this answer but it doesn't seem to be right:

#define major(dev) ((int)(((unsigned int) (dev) >> 8) & 0xff))
#define minor(dev) ((int)((dev) & 0xff))

Because if I do printk (KERN_INFO "opening %s (dev %d)\n", filename, major (stat.rdev)); The second field is always zero.

How should I get the major number then?

Était-ce utile?

La solution 2

I found linux/include/linux/kdev_t.h which has:

#define MINORBITS        20
#define MINORMASK        ((1U << MINORBITS) - 1)

#define MAJOR(dev)        ((unsigned int) ((dev) >> MINORBITS))
#define MINOR(dev)        ((unsigned int) ((dev) & MINORMASK))
#define MKDEV(ma,mi)      (((ma) << MINORBITS) | (mi))

Which seems "better" than the code you showed, since this has more bits allocated for the major number (20 rather than 8).

Autres conseils

On recent glibc and kernels, you should include sys/sysmacros.h and not only sys/types.h. Here is the comment that describe this decision:

/* BSD defines `major', `minor', and `makedev' in this header.
   However, these symbols are likely to collide with user code, so we are
   going to stop defining them here in an upcoming release.  Code that needs
   these macros should include <sys/sysmacros.h> directly.  Code that does
   not need these macros should #undef them after including this header.  */
# define __SYSMACROS_DEPRECATED_INCLUSION
# include <sys/sysmacros.h>
# undef __SYSMACROS_DEPRECATED_INCLUSION
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top