After opening up a socket in a C program, I ran fstat on it and printed out the major and minor device numbers. I got a major number of 0 and a minor number of 8. Looking at the Linux kernel documentation, I see that major device number 0 is reserved for "unnamed devices".

So, is this a guarantee of Linux? That is, will sockets always be on major device number 0? If so, is a minor number of 8 a guarantee as well? Are there macros in a header somewhere that define these values?

Here's my code:

#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/types.h>

int main() {
    int ret = 0, sock;
    struct stat fs;

    sock = socket(AF_INET, SOCK_STREAM, 0);
    if ( sock < 0 ) {
        return 1;

    if ( fstat(sock, &fs) != 0 ) {
        ret = 1;
    else {
        printf("Socket is on device %u:%u\n", major(fs.st_dev), minor(fs.st_dev));

    return ret;
  • 2
    I don’t have an answer for the other parts of the question, but minor 8 isn’t guaranteed: I have one system where I get minor 8, another where I get minor 9. Jun 7 at 16:22
  • 1
    @StephenKitt, Daniel, thanks both of you! Jun 7 at 16:35
  • 1
    wow, this is convoluted. I really can't find where static struct vfsmount *sock_mnt __read_mostly; from linux/net/socket.c anywhere specifically initializes anything that'd be used to populate st_dev. But I'm having a hard time believing this is uninitialized memory, considering the low variance of things we see. Jun 7 at 17:00
  • 3
    I wonder what happens if you do that with an Unix socket that's bound to a name in the filesystem. But if you just want to tell if the fd is a socket, S_ISSOCK(st_mode) might be a better idea.
    – ilkkachu
    Jun 7 at 19:12
  • 2
    @K-att- thanks for the investigation! If you don’t code-quote your URLs, they become links and are easier to follow: github.com/openvswitch/ovs/blob/master/lib/tc.h and elixir.bootlin.com/linux/latest/source/include/uapi/linux/…. Jun 23 at 16:37


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.