进一步了解Sticky Bit

本文深入探讨了UNIX文件系统中sticky位的历史背景及其在现代操作系统中的应用。sticky位最初是为了提高程序加载效率而设计,但现在主要用于限制特定目录下文件的删除权限。文章详细解释了在HP-UX和Solaris等系统中sticky位的不同作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      我们在的前一篇文章《深入了解 UNIX 文件权限》中提到过:当一个目录设置了 sticky bit ,该目录下的文件仅可以被文件所有者或目录所有者删除。这种机制是由 POSIX 规定的并被普遍推广。但是,上述功能并不是 sticky bit 最原始的目的。 POSIX ls 命令中给 sticky bit 是这样定义的:“限制删除功能开关”。在 C 语言头文件中代表 sticky bit 的常数是 S_ISVTX svtx 代表“ save text” 而这也正是提示了 sticky bit 的本来目的。
        POSIX 最初想法是:如果 sticky bit 设置了,进程中的“ text segment” 将会留在 swap 空间中;这样会使得它只需一次硬盘读就可以将其读入内存中。起初, UNIX 使用大小为 512 字节且趋于分散的块;因此需要一定的时间去收集那些分散的“ text segment” 并加载到内存中去。最早期的 UNIX 中没有页面调度,只有页面交换技术;因此要运行一个程序需要全部加载到内存中去;现在我们根据程序的需要加载数据页到内存中。这是不是说 sticky bit 的初始用途已经消失的呢?答案是否定的,根据不同的操作系统有不同的情况。
HP-UX 中, sticky bit 依然存在并可以在 HP-UX chmod(2) man page [1] 中查证。什么在用 sticky bit 呢?,通过 HP-UX FAQ [2] 中可以看到在 [The HP-UX Kernel Tuning and Performance Guide] 中有如下描述:

"When applications are located remotely, set the "sticky bit" on the applications binaries, using the chmod +t command. This tells the system to page the text to the local disk. Otherwise, it is "retrieved" across the network. Of course, this would only apply when there is actual paging occurring. More recently, there is a kernel parameter, page_text_to_local, which when set to 1, will tell the kernel to page all NFS executable text pages to local swap space."

Solaris 中虽然没有关于 sticky bit 初始用途的说明,但根据 Solaris chmod(2) man page [3] 可以看到如下描述 :

"If a regular file is not executable and has S_ISVTX set, the file is assumed to be a swap file. In this case, the system's page cache will not be used to hold the file's data. If the S_ISVTX bit is set on any other file, the results are unspecified."

     如果你发现一个文件设置了sticky bit,您就需要查看对应操作系统的chmod命令的man page看看有什么样的影响。另外请注意,如果有影响,OS会限制对root设置sticky bit。例如:在HP-UX,一个恶意的用户可以设置很多sticky bit让系统用完swap空间导致系统崩溃。

以下是不同OSchmod帮助手册中对sticky bit描述:

表一:不同OSsticky bit的描述

HP-UX实际上还有一些符号链接文件设置了sticky bit这将在以后的文章中继续讨论。



[1] 参考 HP-UX chmod 命令帮助手册 : http://docs.hp.com/en/B2355-90693/chmod.2.html
[2] 参考 HP-UX FAQ http://www.faqs.org/faqs/hp/hpux-faq/
[3] 参考 Solaris chmod 命令帮助手册 : http://docs.sun.com/app/docs/doc/816-5167/chmod-2?a=view
[4] 其它参考资料: www.wikipedia.org
A. http://nixdoc.net/man-pages/Linux/man1/chmod.1.html
B. http://nixdoc.net/man-pages/FreeBSD/man2/chmod.2.html
C. http://nixdoc.net/man-pages/IRIX/man2/standard/chmod.2.html
D. http://www.manpagez.com/man/2/chmod/
E. http://uw714doc.sco.com/en/man/html.2/chmod.2.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值