18 May 2007

umask 疑云

(很久没有好好 blogging 了……)

众所周知,Linux 下的文件有三种权限位: r(read), w(write), x(execute),每个文件又把用户分成三组: user, group, others。由此相成一个文件的权限:
$ touch file
$ ls -l file
-rw-r--r-- 1 jick users 0 2007-05-18 17:56 file

每一种权限又可以使用数字来表示:r - 4,w -2,x - 1。由此,上面文件的权限又可以表示为 644。r,w,x 全打开可表示为 777。用 touch 命令创建的文件会设置默认的权限位。

权限位使用 umask 查看和设置:
$ umask
0022

(之所以是四位而不是三位,是因为 u,g,o 之外还有 Super 位,这不在我们本次的讨论中 ;-)

这里有一个问题,全部设置后权限位是 0777,系统默认的 umask 值是 0022,为什么 touch 创建的文件权限位是 0666? 执行权限位跑哪里去了? 这就是今天要说的 umask 疑云。

umask 值会在两个地方用到: open() 和 mkdir(),也就是系统创建文件和目录的时候。我们来分析一下 open()。
       #include <sys/types.h>
       #include <sys/stat.h>
       #include <fcntl.h>

       int open(const char *pathname, int flags);
       int open(const char *pathname, int flags, mode_t mode);

open() 有两种调用方式,一种不设置 file 的 mode,而使用系统默认的 umask;一种可以设置 file 的 mode,从而产生不同的权限位。

当 open() 的 mode 设置为 0777 ,默认 umask 为 0022 时,所创建文件的权限位是 0755。从这里可以看出,umask 值是可以屏蔽  open() 的 mode。

但 touch 的时候,为什么系统会雪藏我们的执行权限位呢? 原来 ,在创建文件的时候,Linux 系统会拿掉执行权限位;而在创建目录的时候,Linux 系统则不会拿掉执行权限位,因为在查看目录的时候需要用到执行权限位(尝试拿掉目录的权限位,然后 ls 该目录,看到了什么?)。我猜测,Linux 继承 Unix 的这一传统,大概是出于安全性的考虑 ;-)

--
每一个不曾起舞的日子都是对生命的辜负。

3 comments:

  1. 你的blogger不错啊,欢迎加入到,http://www.ipowerblogger.cn
    这是专门用来推广blogger的,等待你的加入啊

    ReplyDelete
  2. 谢谢赞赏。

    关于ipowerblogger,类似的组织太多了,我参加不过来,于是放弃了。

    ReplyDelete
  3. Dimitar Berbatov buy flyff gold scored the only goal as Manchester United beat Final Fantasy XI gil Middlesbrough 1-0 at Old Trafford on Monday buy cheap ffxi gil to move to within seven points of Premier League ffxi gil leaders Liverpool -- with two vital buy Warhammer gold games in hand.

    The Bulgarian Warhammer gold international striker fired home a volley from close-range in the buy Warhammer gold 69th minute to give United the victory, leaving Middlesbrough deep in relegation trouble just one place above Warhammer gold the drop zone in 16th place.

    Prior to the goal, United wow po had missed a host of chances as Middlesbrough's wow or defense held firm, but the deadlock was finally broken when Michael Carrick's left-wing cross bounced back off Boro defender wow geld David Wheater into Berbatov's path.

    Berbatov, Wayne Rooney, wow geld Cristiano Ronaldo, Rafael, wow gold Nemanja Vidic and Ji-sung Park had earlier all squandered chances buy wow gold against a determined Middlesbrough side.

    Rooney should have given the wow gold hosts the lead when he sprang world of warcraft gold the offside trap in the 35th minute but inexplicably chose to pass rather wow gold than shoot.

    Then, two minutes wow soldi before half-time, defender Chris Riggott wow gold produced a superbly-timed block inside the penalty area to deny Berbatov buy wow gold just as he was about to shoot.

    After Berbatov's goal,wow gold Park produced a contender world of warcraft gold for miss of the season when he wow gold somehow ballooned over the wow oro crossbar from just three yards out from substitute wow gold Gary Neville's cross.

    But, despite not adding buy wow gold to their tally, the three points were United's wow gold and their next league game is now against second-placed Chelsea, who are just four points ahead wow power leveling of Sir Alex Ferguson's side

    ReplyDelete