【问题标题】:AF_UNIX socket in Linux above Windows WSL fails to bind to /mnt file: error 95, Operation not supportedWindows WSL 以上 Linux 中的 AF_UNIX 套接字无法绑定到 /mnt 文件:错误 95,不支持操作
【发布时间】:2022-07-23 08:59:50
【问题描述】:

我们需要将一个 Windows 客户端应用程序连接到一个 Linux 服务器。 Linux 端在 Windows 10 (10.0.19044) 中的 WSL2 之上运行。

我们想使用 UNIX 域套接字,并遵循 https://devblogs.microsoft.com/commandline/windowswsl-interop-with-af_unix/ 中的指导

服务器程序成功绑定到“本地”文件系统中的文件(例如 /tmp/mysock),但无法绑定到挂载驱动器中的“Windows 侧”文件(例如 /mnt/c /mysock),这是必需的,以便服务器可以接受来自 Windows 端 AF_UNIX 套接字的连接。

我得到的 errno 是 95:“不支持操作” 我试过用 sudo 运行,但结果相同。

知道发生了什么吗?

服务器代码是:

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

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <errno.h>

#undef NDEBUG

// filename comes as command-line argument
int main(int argc, char *argv[])
{
    struct sockaddr_un addr;
    int ret = -1;
    
    printf("Starting NVC_LINUX...\n");
    
    assert(argc == 2);
    char *filename = argv[1];
    
    int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
    assert(sfd != -1);

    // Delete any file that already exists at the address. Make sure the deletion
    // succeeds. If the error is just that the file/directory doesn't exist, it's fine.
    ret = remove(filename);
    assert(ret != -1 || errno == ENOENT);

    // Zero out the address, and set family and path.
    memset(&addr, 0, sizeof(struct sockaddr_un));
    addr.sun_family = AF_UNIX;
    assert(strlen(filename) <= sizeof(addr.sun_path) - 1);
    strncpy(addr.sun_path, filename, sizeof(addr.sun_path) - 1);

    ret = bind(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un));
    if (ret == -1) printf("errno : %d - %s\n", errno, strerror(errno));
    assert(ret != -1);

    ret = listen(sfd, 1);
    assert(ret != -1);

    printf("Waiting to accept a connection...\n");
    // NOTE: blocks until a connection request arrives.
    int cfd = accept(sfd, NULL, NULL);
    assert(cfd != -1);
    printf("Accepted socket fd = %d\n", cfd);

    char cmd;
    char res[32];
    while (1)
    {
        // get char from Win side
        ssize_t num_read = read(cfd, (void *) &cmd, sizeof(cmd));
        assert(num_read == sizeof(cmd));

        printf("  cmd=%c\n", cmd);
        
        // generate reply
        sprintf(res, "Hello from Linux, %c\n", cmd);

        // send data
        ssize_t num_written = write(cfd, (const void *) res, sizeof(res));
        assert(num_written == sizeof(res));
    }

    (void) close(cfd);
    (void) close(sfd);

    printf("done.\n");
    return 0;
}

【问题讨论】:

    标签: linux bind windows-subsystem-for-linux wsl-2 unix-socket


    【解决方案1】:

    巧合的是,我昨天在 WSL GitHub issues 搜索有关 X11 套接字的信息时,遇到了 this issue(与我的主题无关,但与您的主题相关),关于 AF_UNIX WSL2 中的支持,这引起了我的注意,仅仅是因为它最近作为 By Design 关闭。

    简而言之,AF_UNIX 目前在 WSL2 下不受支持。如果您的应用程序可能,请考虑将 WSL 发行版转换(或复制)为支持 AF_UNIX 的 WSL1。

    或者,您可以在 Windows 和 Linux 端之间使用网络连接。

    【讨论】:

    • 将发行版降级到 WSL1 (wsl --set-version Ubuntu-20.04 1) 使绑定成功,然后连接也成功。谢谢!
    猜你喜欢
    • 2020-03-21
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    相关资源
    最近更新 更多