How to create Unix Domain Socket with a specific permissions in C?

后端 未结 3 1901
一个人的身影
一个人的身影 2020-12-29 21:39

I have a simple code, like:

sockaddr_un address;
address.sun_family = AF_UNIX;
strcpy(address.sun_path, path);
unlink(path);

int fd = socket(AF_UNIX, SOCK_S         


        
3条回答
  •  北海茫月
    2020-12-29 22:36

    Another solution is to create a directory with the desired permissions, and then create the socket inside it (example code without any regard for error checking and buffer overflows):

    // Create a directory with the proper permissions
    mkdir(path, 0700);
    // Append the name of the socket
    strcat(path, "/socket_name");
    
    // Create the socket normally
    sockaddr_un address;
    address.sun_family = AF_UNIX;
    strcpy(address.sun_path, path);
    int fd = socket(AF_UNIX, SOCK_STREAM, 0);
    bind(fd, (sockaddr*)(&address), sizeof(address));
    listen(fd, 100);
    

提交回复
热议问题