chmod

Section: System Calls (2)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

chmod, fchmod, fchmodat - ファイルのモードを変更する  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <sys/stat.h>

int chmod(const char *path, mode_t mode);
int fchmod(int fd, mode_t mode);

#include <fcntl.h>           /* AT_* 定数の定義 */
#include <sys/stat.h>

int fchmodat(int dirfd, const char *path, mode_t mode, int flags);

glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):



fchmod():
    Since glibc 2.24:
        _POSIX_C_SOURCE >= 199309L
    glibc 2.19 to glibc 2.23
        _POSIX_C_SOURCE
    glibc 2.16 to glibc 2.19:
        _BSD_SOURCE || _POSIX_C_SOURCE
    glibc 2.12 to glibc 2.16:
        _BSD_SOURCE || _XOPEN_SOURCE >= 500
            || _POSIX_C_SOURCE >= 200809L
    glibc 2.11 and earlier:
        _BSD_SOURCE || _XOPEN_SOURCE >= 500

fchmodat():

    glibc 2.10 以降:
        _POSIX_C_SOURCE >= 200809L
    glibc 2.10 以前:
        _ATFILE_SOURCE
 

説明

The chmod() and fchmod() system calls change a file's mode bits. (The file mode consists of the file permission bits plus the set-user-ID, set-group-ID, and sticky bits.) These system calls differ only in how the file is specified:
•
chmod() changes the mode of the file specified whose pathname is given in path, which is dereferenced if it is a symbolic link.
•
fchmod() は、オープンされたファイルディスクリプター fd により参照されるファイルのモードを変更する。

新しいファイルモードは mode で指定される。 mode は、以下に示す値の 0 個以上の OR (論理和) をとって作成される ビットマスクである。

S_ISUID (04000)
set-user-ID (execve(2) 時にプロセスの実効ユーザー ID にセットされる)
S_ISGID (02000)
set-group-ID (以下の場合に (execve(2) 実行時にプロセスの実効グループ ID にセットされる、 強制ロック (mandatory locking) を行う (fcntl(2) の説明を参照)、 新しいファイルのグループとして親ディレクトリと同じものを使う (chown(2) と (mkdir(2) の説明を参照)
S_ISVTX (01000)
スティッキービット (sticky bit) (削除制限フラグ、 unlink(2) の説明を参照)
S_IRUSR (00400)
所有者による読み取り
S_IWUSR (00200)
所有者による書き込み
S_IXUSR (00100)
所有者による実行 (execute) / 検索 (search) (「検索」はディレクトリに対して適用されるもので、 そのディレクトリ内のエントリーへアクセスできるかを意味する)
S_IRGRP (00040)
グループによる読み取り
S_IWGRP (00020)
グループによる書き込み
S_IXGRP (00010)
グループによる実行 / 検索
S_IROTH (00004)
他人 (others) による読み取り
S_IWOTH (00002)
他人による書き込み
S_IXOTH (00001)
他人による実行 / 検索

呼び出したプロセスの実効 (effective) UID がファイルの所有者と一致するか、 そのプロセスが特権を持たなければならない (Linux では CAP_FOWNER ケーパビリティ (capability) を持たなければならない)。

呼び出したプロセスに特権がなく (Linux では CAP_FSETID ケーパビリティがなく)、かつファイルのグループ ID が プロセスの実効グループ ID または補助的なグループ ID にマッチしない場合、 S_ISGID ビットはオフにされるが、これによってエラーが返されることはない。

安全のための処置として、 ファイルシステムによっては、ファイルの書き込みを行う時に set-user-ID とset-group-ID ビットと実行ビットが オフにされることがある。 (Linux では、書き込みプロセスが CAP_FSETID ケーパビリティを持っていない場合に、これが起こる。) ファイルシステムの中には、スーパーユーザーだけが 特別の意味を持つスティッキービットを設定できるものがある。 スティッキービットとディレクトリに対する set-user-ID (set-group-ID) ビットについては、 inode(7) を見よ。

NFS ファイルシステム上では、パーミッションを制限すると、 既にオープンされているファイルに対してすぐに影響が及ぶ。 これはアクセス制御がサーバー上で行われているが、 オープンされているファイルはクライアント側で管理されているためである。 クライアント側でファイル属性のキャッシュが有効になっている場合に、 パーミッションの制限を緩くすると、 他のクライアントに情報が伝わるのが遅れるかもしれない。  

fchmodat()

fchmodat() システムコールは chmod() と全く同様に動作するが、以下で説明する点が異なる。

If path is relative, then it is interpreted relative to the directory referred to by the file descriptor dirfd (rather than relative to the current working directory of the calling process, as is done by chmod() for a relative pathname).

If path is relative and dirfd is the special value AT_FDCWD, then path is interpreted relative to the current working directory of the calling process (like chmod()).

path で指定されたパス名が絶対パスの場合、 dirfd は無視される。

flags can either be 0, or include the following flags:

AT_EMPTY_PATH (since Linux 6.6)
If path is an empty string, operate on the file referred to by dirfd (which may have been obtained using the open(2) O_PATH flag). In this case, dirfd can refer to any type of file, not just a directory. If dirfd is AT_FDCWD, the call operates on the current working directory. This flag is Linux-specific; define _GNU_SOURCE to obtain its definition.
AT_SYMLINK_NOFOLLOW
If path is a symbolic link, do not dereference it: instead operate on the link itself.

fchmodat() の必要性についての説明については openat(2) を参照。  

返り値

成功すると、0 を返す。 エラーの場合、-1 を返し、 errno にエラーを示す値を設定する。  

エラー

ファイルシステムによっては、下記の一覧にない他のエラーが返されることもある。

chmod() のごく一般的なエラーを挙げる:

EACCES
パス名の構成要素に検索許可がない (path_resolution(7) も見よ)。
EBADF
(fchmod()) The file descriptor fd is not valid.
EBADF
(fchmodat()) path is relative but dirfd is neither AT_FDCWD nor a valid file descriptor.
EFAULT
path がアクセスできるアドレス空間外を指している。
EINVAL
(fchmodat()) Invalid flag specified in flags.
EIO
I/O エラーが発生した。
ELOOP
path を解決する際に遭遇したシンボリックリンクが多過ぎる。
ENAMETOOLONG
path が長過ぎる。
ENOENT
ファイルが存在しない。
ENOMEM
カーネルに十分なメモリーがない。
ENOTDIR
パス名の構成要素がディレクトリではない。
ENOTDIR
(fchmodat()) path is relative and dirfd is a file descriptor referring to a file other than a directory.
ENOTSUP
(fchmodat()) flags specified AT_SYMLINK_NOFOLLOW, which is not supported.
EPERM
実効 UID がファイルの所有者と一致せず、プロセスに特権がない (Linux では CAP_FOWNER ケーパビリティを持たない)。
EPERM
The file is marked immutable or append-only. (See FS_IOC_SETFLAGS(2const).)
EROFS
ファイルが読み込み専用 (read only) のファイルシステム上にある。
 

バージョン

 

C ライブラリとカーネルの違い

GNU C ライブラリの fchmodat() ラッパー関数は、このページで説明している POSIX で規定されたインターフェースを実装している。このインターフェースは、内部で呼ばれる Linux のシステムコールとは異なる。システムコールは flags 引数を持たない。  

glibc での注意

On older kernels where fchmodat() is unavailable, the glibc wrapper function falls back to the use of chmod(). When path is a relative pathname, glibc constructs a pathname based on the symbolic link in /proc/self/fd that corresponds to the dirfd argument.  

標準規格

POSIX.1-2024.  

歴史

chmod()
SVr4, POSIX.1-1988, 4.4BSD.
fchmod()
SVr4, 4.4BSD, SUSv1, POSIX.1-1996.
fchmodat()
POSIX.1-2008. Linux 2.6.16, glibc 2.4.
AT_SYMLINK_NOFOLLOW
POSIX.1-2008, glibc 2.32, Linux 6.5.
 

関連項目

chmod(1), chown(2), execve(2), open(2), stat(2), inode(7), path_resolution(7), symlink(7)


 

Index

名前
ライブラリ
書式
説明
fchmodat()
返り値
エラー
バージョン
C ライブラリとカーネルの違い
glibc での注意
標準規格
歴史
関連項目

This document was created by man2html, using the manual pages.