Linux权限管理

当user ,group 都不相同时,看other如果有执行权限,并且user有设置用户id位,才可以execl


umask()函数可以指定进程创建的文件的默认权限屏蔽位(即哪些权限被屏蔽,特殊的umask(0)就是允许所有权限),在使用creat()函数时,也会指定文件mode,那么新创建文件最终的mode,是umask指定mode与creat指定mode与的结果。例子如下

我们首先用umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)设置文件模式的默认屏蔽字,即组读,组写,其他读,其他写不可用,那么该文件的默认权限就是111001001,如果creat中又指定了文件的mode为110110110,那么文件最终的mode为(111 001 001)&(110 110 110),即(110 000 000),也充分的说明了被umask屏蔽的权限位,一定是不可用的!!!


用umask命令显示当前的文件模式创建屏蔽字,显示0022,这是八进制的表示方法,即新建的文件都屏蔽组用户写和其他用户写权限。


我们在shell中创建新文件时,有时会发现文件的mode与umask指定的mode不一致,为什么会出现这种情况呢,下面我引用一篇文章,讲的比较好


A umask is a bitmask by means of which a user can affect the default permissions of files they create on unix systems. You should find out what file permissions are if you are unsure. Its important to fully appreciate that a umask is not a default permission set it is a default permission mask so bits in the mask are the bits which you want not to be in the permissions of the file. A umask of 777 will thus mean that processes create files with permissions of zero whereas one of 0 will create files with full permissions thussly:


% umask 777                                                
% touch foo                                                
% umask 0                                                  
% touch bar                                                
% ls -l foo bar                                            
-rw-rw-rw-    1 sean   sean             0 Jul  2 11:15 bar
----------    1 sean   sean             0 Jul  2 11:15 foo
Now you might well wonder why (if my umask is zero) the execute bits aren't also being set on "bar"? Lets see what "touch" is doing under the covers...


% strace touch furb 2>&1 | grep furb 
execve("/bin/touch", ["touch", "furb"], [/* 137 vars */]) = 0
open("furb", O_WRONLY|O_NONBLOCK|O_CREAT|O_NOCTTY|O_LARGEFILE, 0666) = 3
utime("furb", NULL)                     = 0
As you can see, the "touch" process only tries to create the file with permissions of 0666. The permissions requested at open(2) are then masked by the umask and the bits requested in the open(2) call that are not in the mask end up in the permissions of the file created. Processes (such as compilers) which know they are creating executable files will generally use open(2) with the execute bits set as well. Because umask is a mask it can't raise the permissions higher than the process asks for in the open(2) call. The same is true of any other process which opens files.


linux系统中,每一个进程都有一个当前工作目录,此目录是搜索所有相对路径名的起点(不以反斜杠开始的路径名是相对路径名)。当用户登录到UNIX系统时,其当前工作目录通常是口令文件(/etc/passwd)中该用户登录项的第六个字段——用户的起始目录(home directory,一般是用户的家目录)。当前工作目录是进程的一个属性,起始目录则是登录名的一个属性。在进程中用chdir改变工作目录,不影响shell的当前工作目录,因为chdir工作目录是针对进程的一个属性!!!

当一个应用程序需要在文件系统中返回到其工作的起点时,getcwd函数是有用的。在更换工作目录之前,我们可以调用getcwd函数先将其保存起来。在完成了处理后,就可将从getcwd获得的路径名作为调用参数传送给chdir,这样就返回到了文件系统中的起点。fchdir函数向我们提供了一种完成此任务的便捷方法。在更换到文件系统中的不同位置前,无需调用getcwd函数,而是使用open打开当前工作目录,然后保存文件描述符。当希望回到原工作目录时,只需要简单的将该文件描述符传递给fchdir

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值