あけましておめでとうございます。
気がついたのでメモ。
ただ、どういう役に立つかは知らん。
- 注1: Linux です。OS X や他の UNIX は挙動が異なる場合あり。っていうかそもそも OS X の
su
は-c
オプションを受け付けないし。 - 注2: bash や zsh など POSIX な shell を想定しています。
まず、とりあえず適当にファイルを開いておく。っていうかこの適当なFDに新しいファイル(や別の FD)をバインドすることができるのも今日知ったrz
$ temp=`mktemp` $ exec 3<>$temp
自分のシェルが開いているファイルを確認。
$ ls -l /proc/$$/fd total 0 lrwx------ 1 lfs lfs 64 Jan 1 19:42 0 -> /dev/pts/0 lrwx------ 1 lfs lfs 64 Jan 1 19:42 1 -> /dev/pts/0 lrwx------ 1 lfs lfs 64 Jan 1 19:42 2 -> /dev/pts/0 lrwx------ 1 lfs lfs 64 Jan 3 22:10 255 -> /dev/pts/0 lrwx------ 1 lfs lfs 64 Jan 1 19:42 3 -> /tmp/tmp.nKKIH7bnT0 ## 開いた
sudo で上と同じコマンドを起動してみる。
$ sudo sh -c 'ls -l "/proc/$$/fd"' [sudo] password for lfs total 0 lrwx------ 1 root root 64 Jan 3 22:26 0 -> /dev/pts/0 lrwx------ 1 root root 64 Jan 3 22:26 1 -> /dev/pts/0 lrwx------ 1 root root 64 Jan 3 22:26 2 -> /dev/pts/0 lr-x------ 1 root root 64 Jan 3 22:26 3 -> /proc/1821/fd ## 開いたファイルはない。
今度は su で起動してみる。
$ su -c "sh -c 'ls -l "'/proc/$$/fd'"'" Password: total 0 lrwx------ 1 root root 64 Jan 3 22:30 0 -> /dev/pts/0 lrwx------ 1 root root 64 Jan 3 22:30 1 -> /dev/pts/0 lrwx------ 1 root root 64 Jan 3 22:30 2 -> /dev/pts/0 lrwx------ 1 root root 64 Jan 3 22:30 3 -> /tmp/tmp.nKKIH7bnT0 ## 開いたまま lr-x------ 1 root root 64 Jan 3 22:30 4 -> /proc/1828/fd
結論: su は開いたファイルを引き継ぐ。sudo は開いたファイルを引き継がない。
閉じる。
$ exec 3<&- $ exec 3>&- $ rm $temp
って、書いたけど、ちゃんとマニュアルに載ってました。このおバカちん。
$ man sudo
-C num, --close-from=num
Close all file descriptors greater than or equal to
num
before executing a command. Values less than three are not permitted. By default, sudo will close all open file descriptors other than standard input, standard output and standard error when executing a command. The security policy may restrict the user's ability to use this option. The sudoers policy only permits use of the-C
option when the administrator has enabled theclosefrom_override
option.
まあ、確かに、この方が安全ではある。