如何创建Unix域套接字与特定的权限? [英] How to create Unix Domain Socket with a specific permissions?
问题描述
我有一个简单的code,如:
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_STREAM, 0);
bind(fd, (sockaddr*)(&address), sizeof(address));
listen(fd, 100);
我要的原子创建具有特定权限的Unix域套接字文件,说: 0777
。本手册不说的套接字文件权限对于的umask
或其他任何东西。甚至,如果的umask
确实会影响套接字文件,那么它是不是一个原子的方式 - 在多线程程序
I want to atomically create the Unix Domain Socket file with a specific permissions, say: 0777
. The manual doesn't say anything about socket file permissions with regard to umask
or whatever. Even, if the umask
does affect the socket file, then it's not an atomic way - in multi-threaded program.
我希望有一种方式来实现我的目标,而无需使用)同步的umask(
来电。
I hope, there is a way to achieve my goal without using synchronization of umask()
calls.
推荐答案
另一个解决方案是创建具有所需权限的目录,然后在不进行错误检查和缓冲区的任何方面创造插座里面(例如code溢出):
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, 077);
// 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);
这篇关于如何创建Unix域套接字与特定的权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!