How stable are major and minor mode of a chardev?

208 Views Asked by At

I'm working on a serial port library, and I am thinking on using the major/minor mode of the character device to check whether the given file is a platform serial port, a pty or a usb serial port, in complement of using other frameworks that exist under Linux or OSX.

Actually, for finding out whether a file is an USB serial port, or a platform serial port, there are ways using the available frameworks, like libudev on linux or IOKit on OSX. But I think that for checking a PTY file, the only way is using good old stat():

#if defined(OS_LINUX)
#define PTY_MAJOR_NODE 136
#elif defined(OS_MAC)
#define PTY_MAJOR_NODE 16
#elif defined(OS_SOLARIS)
#define PTY_MAJOR_NODE 24
#endif

bool is_pty(const char* file) {
    struct stat filestat;
    if (0 == stat(file, &filestat)
            && S_ISCHR(filestat.st_mode)
            && major(filestat.st_rdev) == PTY_MAJOR_NODE) {
        return true;
    }
    return false;
}

So far I found out on my debian linux that the PTS chardev all have a major mode of 136, and on my OSX, all PTS have a mode of 16. Crawling on ddg, I found out Solaris may use a major mode of 24.

I could find many resources online talking about what are PTY/PTS and their history. The manpage of openpty, pty or pts - though that one give major and minor of /dev/ptmx - do not talk about major/minor mode of PTS devices. And no resource listing the Major modes accross unices.

So here are my questions:

  • are the major mode of PTS (and by extension other serial chardev) stable for a given Unix flavor? (I'm looking for something stable at ±2 years)
  • where are those defined? I guess it's a kernel driver that handles that, isn't it?
  • is it a good idea to rely on major mode of the chardev to detect what kind of device this is?

N.B.: I have hesitated to post on unix.SE, but as I'm in the context of using that in code, I considered this is more a question to ask here, on SO.

Edit: I posted a related question on unix.SE asking for help finding other major modes and look how stable they are accross systems. My guess is that if nobody has an answer, at least we can query the systems and look at the sources.

1

There are 1 best solutions below

1
On

Major and minor device numbers are defined at compile or link/load time depending on the hardware involved. Adding an additional SBUS card to a Solaris machine might (must?) have an arbitrary major device number. I know mine did.
In short, I don't think major/minor device numbers are going to do what you want.
Consider Debian Linux Ports https://www.debian.org/ports
I can't imagine that the SPARC, s390, MIPS, PowerPC, and ARM architectures all use the same major/minor numbers; the bus architectures are too different.