Home:ALL Converter>Unix Socket bind() doesn't fail, but socket doesn't appear in filesystem?

Unix Socket bind() doesn't fail, but socket doesn't appear in filesystem?

Ask Time:2021-09-22T05:49:27         Author:C-Scholl20

Json Formatter

As the title describes, I'm trying to bind a Unix domain socket to some arbitrary file path, and the output would suggest that the call to bind() was successful (i.e. I'd get a printout to stdout if it failed).

[root@localhost rmm_service]# bin/Debug/rmm_mgrd 
Starting the RMM Manager Service!
Attempting to bind to /tmp/rmm_server.socket...
Found SATA device sda!
/dev/sda is not the RMM...

However, when I check the /tmp directory...

[root@localhost sf_loon-ccu]# cd /tmp
[root@localhost tmp]# ls -l
total 0
drwxr-xr-x. 2 cscholl cscholl  6 Sep 16 14:06 hsperfdata_cscholl
drwxr-xr-x. 2 root    root     6 Sep 16 13:41 hsperfdata_root
drwx------. 2 cscholl cscholl 25 Sep 21 16:07 ssh-Ly2mr17FPPym
drwx------. 2 cscholl cscholl 24 Sep 22 08:24 ssh-rHpliZpZ8njt
drwx------. 2 cscholl cscholl 24 Sep 21 14:28 ssh-suwMYD7i611B
drwx------. 3 root    root    17 Sep 22 08:24 systemd-private-39cfa8e7c2bd497b924b36b533255fc6-colord.service-jYLJ9b
drwx------. 3 root    root    17 Sep 22 08:23 systemd-private-39cfa8e7c2bd497b924b36b533255fc6-cups.service-32BDoF
drwx------. 3 root    root    17 Sep 22 08:23 systemd-private-39cfa8e7c2bd497b924b36b533255fc6-rtkit-daemon.service-jXTmC1
drwx------. 3 root    root    17 Sep 21 16:06 systemd-private-c6d04ab32240427299e3bca5557945b2-colord.service-pAki1e
drwx------. 3 root    root    17 Sep 21 16:06 systemd-private-c6d04ab32240427299e3bca5557945b2-cups.service-wob7sr
drwx------. 3 root    root    17 Sep 21 16:06 systemd-private-c6d04ab32240427299e3bca5557945b2-rtkit-daemon.service-XvQWTb
drwx------. 3 root    root    17 Sep 21 14:27 systemd-private-d8864db6ec24423c98ff23d006422dcd-colord.service-bfTewZ
drwx------. 3 root    root    17 Sep 21 14:27 systemd-private-d8864db6ec24423c98ff23d006422dcd-cups.service-dFZ3HK
drwx------. 3 root    root    17 Sep 21 14:27 systemd-private-d8864db6ec24423c98ff23d006422dcd-rtkit-daemon.service-jKbPbk
drwx------. 3 root    root    17 Sep 21 16:04 systemd-private-d8864db6ec24423c98ff23d006422dcd-systemd-hostnamed.service-hdYh0N
drwx------. 2 cscholl cscholl  6 Aug 31 12:23 tracker-extract-files.1000

I also noticed that if I do the bind() call with the third argument being sizeof(sock_addr.sun_family) + strlen(path) - 1, the socket appears, but the name gets truncated. Any clues as to what's happening here?

[root@localhost tmp]# ls -l
total 0
drwxr-xr-x. 2 cscholl cscholl  6 Sep 16 14:06 hsperfdata_cscholl
drwxr-xr-x. 2 root    root     6 Sep 16 13:41 hsperfdata_root
srwxr-xr-x. 1 root    root     0 Sep 22 08:35 rmm_server.socke    <------------ Where did the 't' go?
drwx------. 2 cscholl cscholl 25 Sep 21 16:07 ssh-Ly2mr17FPPym
drwx------. 2 cscholl cscholl 24 Sep 22 08:24 ssh-rHpliZpZ8njt
drwx------. 2 cscholl cscholl 24 Sep 21 14:28 ssh-suwMYD7i611B
drwx------. 3 root    root    17 Sep 22 08:24 systemd-private-39cfa8e7c2bd497b924b36b533255fc6-colord.service-jYLJ9b
drwx------. 3 root    root    17 Sep 22 08:23 systemd-private-39cfa8e7c2bd497b924b36b533255fc6-cups.service-32BDoF
drwx------. 3 root    root    17 Sep 22 08:23 systemd-private-39cfa8e7c2bd497b924b36b533255fc6-rtkit-daemon.service-jXTmC1
drwx------. 3 root    root    17 Sep 21 16:06 systemd-private-c6d04ab32240427299e3bca5557945b2-colord.service-pAki1e
drwx------. 3 root    root    17 Sep 21 16:06 systemd-private-c6d04ab32240427299e3bca5557945b2-cups.service-wob7sr
drwx------. 3 root    root    17 Sep 21 16:06 systemd-private-c6d04ab32240427299e3bca5557945b2-rtkit-daemon.service-XvQWTb
drwx------. 3 root    root    17 Sep 21 14:27 systemd-private-d8864db6ec24423c98ff23d006422dcd-colord.service-bfTewZ
drwx------. 3 root    root    17 Sep 21 14:27 systemd-private-d8864db6ec24423c98ff23d006422dcd-cups.service-dFZ3HK
drwx------. 3 root    root    17 Sep 21 14:27 systemd-private-d8864db6ec24423c98ff23d006422dcd-rtkit-daemon.service-jKbPbk
drwx------. 3 root    root    17 Sep 21 16:04 systemd-private-d8864db6ec24423c98ff23d006422dcd-systemd-hostnamed.service-hdYh0N
drwx------. 2 cscholl cscholl  6 Sep 22 08:34 tracker-extract-files.1000

EDIT: Here's the function I've written to create the socket. I'm not at liberty to post the code calling the function.

#define CONNECTION_BACKLOG 5
#define SERVER_SOCKET_PATH "/tmp/rmm_server.socket"
static bool init_rmm_socket(void)
{
    struct sockaddr_un sock_addr;
    memset(&sock_addr, 0, sizeof(sock_addr));

    /* Create a non-blocking socket */
    socket_fd = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK, 0);
    if (socket_fd == -1)
    {
        printf("Failed to create the Unix Domain socket!\n");
        return false;
    }

    sock_addr.sun_family = AF_UNIX;
    strncpy(sock_addr.sun_path, SERVER_SOCKET_PATH, sizeof(sock_addr.sun_path) - 1);
    
    printf("Attempting to bind to %s...\n", sock_addr.sun_path);
    
    /* Bind the socket to SERVER_SOCKET_PATH */
    if (bind(socket_fd, (const struct sockaddr *) &sock_addr, sizeof(sock_addr)) == -1) /* Results in no socket listed */
    /* if (bind(socket_fd, (const struct sockaddr *) &sock_addr, sizeof(sock_addr.sun_family) + strlen(SERVER_SOCKET_PATH) - 1) == -1) -> Truncated socket name */
    {
        printf("Failed to bind socket to %s!\n", SERVER_SOCKET_PATH);
        return false;
    }

    /* Listen for connection requests on the socket */
    if (listen(socket_fd, CONNECTION_BACKLOG) == -1)
    {
        printf("Failed to start listening on %s!\n", SERVER_SOCKET_PATH);
        return false;
    }
}

Author:C-Scholl20,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69275910/unix-socket-bind-doesnt-fail-but-socket-doesnt-appear-in-filesystem
yy