bind() fails with ENOENT on Unix Domain Socket in Android NDK intermittently on some devices

I’m working on an Android NDK project where I need to bind a Unix domain socket to a path in the app’s internal storage. However, the bind() call occasionally fails with ENOENT (No such file or directory) on some devices. Most devices run the code without any issues, but on those where the error occurs, it happens consistently and never succeeds.

Here are the details:

Path used: /data/user/0/xxx.xxx.xxx.xxx/files/socket
Error message: bind() failed with errno: 2, [No such file or directory]
The code snippet for binding the socket is as follows:

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <cstdio>
#include <string>

bool create_and_listen(const std::string& name) {
    struct sockaddr_un addr;
    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    std::string path = "/data/user/0/xxx.xxx.xxx.xxx/files/"(this path is got from context.GetFilesDir() from Android Activity) + name;
    strncpy(addr.sun_path, path.c_str(), sizeof(addr.sun_path) - 1);


    int listen_socket = socket(AF_UNIX, SOCK_STREAM, 0);
    if (listen_socket == -1) {
        perror("Failed to create socket");
        return false;
    }

    unlink(addr.sun_path); // Remove old socket file if it exists

    if (bind(listen_socket, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
        perror("bind error");
        printf("Failed to bind socket, path: %s, errno: %dn", path.c_str(), errno);
        return false;
    }

    if (listen(listen_socket, SOMAXCONN) == -1) {
        perror("listen error");
        printf("Failed to listen on socket, path: %s, errno: %dn", path.c_str(), errno);
        return false;
    }

    printf("Successfully listening on Unix domain socket: %sn", path.c_str());
    return true;
}

int main() {
    create_and_listen("socket");
    return 0;
}

What I’ve checked so far:

  1. Path existence: The directory
    /data/user/0/com.levelinfinite.sgameGlobal/files/ exists, and I can
    create regular files in it without any issues.
  2. Path length: The full path length is well within the 108-byte limit
    for Unix domain socket paths.
  3. Permissions: Since this is the app’s internal storage, the app has
    full read/write permissions.

Additional Clues:

  1. Once a device exhibits this issue, it consistently fails and never
    succeeds in binding.
  2. The path is definitely valid, as I can
    successfully create other regular files in the same directory.

Questions:

Why would bind() return ENOENT when the path clearly exists and is writable?
Are there any specific considerations or restrictions on Unix domain sockets in Android’s internal storage that I might be missing?

Any insights or suggestions would be greatly appreciated!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật