`/ tmp`是UNIX域套接字文件的正确位置吗?

时间:2017-03-30 17:36:33

标签: sockets unix

使用UNIX套接字进行IPC通信时,必须在文件系统中创建文件。互联网上的许多示例都建议将/tmp作为放置套接字的好地方。有些建议使用/tmp/specific_folder 700权限来保持/tmp清洁并且套接字安全来自其他用户访问。

然而,那可靠吗? OS可以随时删除那里的任何文件吗?让我们假设这些文件要在那里存留数天或数月,有些文件可能不会用于长时间的通信(即:天)。

此外,将这些文件存储在本地用户文件夹中(例如,在./.hidden_sockets/中)的缺点是,例如,系统崩溃时不清除这些文件。系统重启后甚至都没有。

你会把这些文件放在哪里?有标准/首选方式吗?

2 个答案:

答案 0 :(得分:2)

可能最好的地方是 $XDG_RUNTIME_DIR 下的子目录,其中非特权用户软件可以存储运行时数据,例如通信原语。这与/ run类似,但对于用户应用程序。它是用户私有命名空间,因此使用起来非常安全。它在注销时自动清理。粘滞位可以防止文件被老化"老化"。

清理

来源

答案 1 :(得分:0)

当在文件夹上设置粘滞位时,只有文件和root用户的所有者可以从该目录中删除该文件,并且默认情况下/ tmp目录受粘性位保护。 这就是为什么建议将套接字文件放在/ tmp文件夹中,以便只有所有者和root有权删除它,但每个人都可以访问它。

drwxrwxrwt. 25 root root 720 Mar 30 23:36 /tmp/
---------^--
this t indicate sticky bit ###this 1 in "chmod 1777 dir" used to set sticky bit

有关完整的故事,请查看以下一步一步的命令 - 考虑我们有3个用户root,a和b。

[root@localhost ~]# mkdir /test     
[root@localhost ~]# chmod 777 /test/ 
[root@localhost ~]# su - a 
[a@localhost ~]$ cd /test/ 
[a@localhost test]$ touch a.txt 
[a@localhost test]$ ls -ltr a.txt 
-rw-rw-r--. 1 a a 0 Mar 30 17:25 a.txt 
[a@localhost test]$ su - b 
[b@localhost ~]$ cd /test/ 
[b@localhost test]$ touch b.txt 
[b@localhost test]$ rm a.txt 
rm: remove write-protected regular empty file 'a.txt'? y 
[b@localhost test]$ su - a 
[a@localhost ~]$ cd /test/ 
[a@localhost test]$ ll 
-rw-rw-r--. 1 b b 0 Mar 30 17:25 b.txt 
[a@localhost test]$ rm b.txt 
rm: remove write-protected regular empty file 'b.txt'? y 
[a@localhost test]$ su - root 
[root@localhost ~]# chmod 1777 /test/ ####setting sticky bit
[root@localhost ~]# cd /test/ 
[root@localhost test]# su a 
[a@localhost test]$ pwd 
/test 
[a@localhost test]$ touch a.txt 
[a@localhost test]$ ll 
-rw-rw-r--. 1 a a 0 Mar 30 17:27 a.txt 
[root@localhost test]# su b 
[b@localhost test]$ touch b.txt 
[b@localhost test]$ ll 
-rw-rw-r--. 1 a a 0 Mar 30 17:27 a.txt 
-rw-rw-r--. 1 b b 0 Mar 30 17:27 b.txt 
[b@localhost test]$ rm a.txt  
rm: remove write-protected regular empty file 'a.txt'? y 
rm: cannot remove 'a.txt': Operation not permitted
  

(b用户无法删除该文件,因为我们设置了粘性   位目录测试中的位置)

相关问题