Question

I am using Unix domain sockets. Want to know about its location in the system.

If I am creating a socketpair using a system call

socketpair(AF_UNIX,SOCK_STREAM,0,fd) ;

I have read it is unnamed socket (a socket that is not been bound to pathname using bind). On the other hand, named socket or better a socket bound to file system path name using bind call get stored in some directory we specify. for example

 struct sockaddr_un {
               sa_family_t sun_family;               /* AF_UNIX */
               char        sun_path[UNIX_PATH_MAX];  /* pathname */
           };

here sun_path can be /tmp/sock file.

So, similarly , I want to know does unnamed socket have any location in the system or anywhere in the memory or kernel ?

Thanks in advance.

Était-ce utile?

La solution

I'm no kernel expert, so take this as an (educated?) guess.

#include <sys/un.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>

int main()
{
    struct sockaddr_un sun;
    socklen_t socklen;
    int fd[2];
    if(socketpair(AF_UNIX,SOCK_STREAM,0,fd) < 0) {
        perror("socketpair");
        return 111;
    }
    socklen = sizeof(sun);
    memset(&sun, 0, sizeof sun);
    sun.sun_path[0] = '!'; /* replace with any character */
    if(getsockname(fd[0], (struct sockaddr *)&sun, &socklen) < 0) {
        perror("getsockname");
        return 111;
    }
    printf("sunpath(%s)\n", sun.sun_path);
    return 0;
}

This program says the socket doesn't have a corresponding path, so my guess is that a unix socketpair is never associated with a filename -- it only stays alive as a data structure inside the kernel until all references are closed.

A better answer is welcome of course :)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top