OSDN Git Service

1cac08927526b71f53b8496796a80c4f0a36a68f
[linuxjm/LDP_man-pages.git] / release / man2 / chown.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
4 .\" and Copyright (c) 1998 Andries Brouwer (aeb@cwi.nl)
5 .\" and Copyright (c) 2007, 2008 Michael Kerrisk <mtk.manpages@gmail.com>
6 .\"
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
10 .\"
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
15 .\"
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein.  The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
22 .\" professionally.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
26 .\"
27 .\" Modified by Michael Haardt <michael@moria.de>
28 .\" Modified 1993-07-21 by Rik Faith <faith@cs.unc.edu>
29 .\" Modified 1996-07-09 by Andries Brouwer <aeb@cwi.nl>
30 .\" Modified 1996-11-06 by Eric S. Raymond <esr@thyrsus.com>
31 .\" Modified 1997-05-18 by Michael Haardt <michael@cantor.informatik.rwth-aachen.de>
32 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
33 .\" 2007-07-08, mtk, added an example program; updated SYNOPSIS
34 .\" 2008-05-08, mtk, Describe rules governing ownership of new files
35 .\"     (bsdgroups versus sysvgroups, and the effect of the parent
36 .\"     directory's set-group-ID permission bit).
37 .\"
38 .\" Japanese Version Copyright (c) 1996 Yosiaki Yanagihara
39 .\"         all rights reserved.
40 .\" Translated 1996-06-24, Yosiaki Yanagihara <yosiaki@bsd2.kbnes.nec.co.jp>
41 .\" Modified 1998-05-11, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
42 .\" Modified 2004-02-29, Yuichi SATO <ysato444@yahoo.co.jp>
43 .\" Updated & Modified Wed Dec 29 06:48:16 JST 2004 by Yuichi SATO
44 .\" Updated 2007-09-04, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.64
45 .\" Updated 2008-08-09, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.05
46 .\"
47 .TH CHOWN 2 2010-09-26 "Linux" "Linux Programmer's Manual"
48 .SH 名前
49 chown, fchown, lchown \- ファイルの所有者を変更する
50 .SH 書式
51 .B #include <unistd.h>
52 .sp
53 .BI "int chown(const char *" path ", uid_t " owner ", gid_t " group );
54 .br
55 .BI "int fchown(int " fd ", uid_t " owner ", gid_t " group );
56 .br
57 .BI "int lchown(const char *" path ", uid_t " owner ", gid_t " group );
58 .sp
59 .in -4n
60 glibc 向けの機能検査マクロの要件
61 .RB ( feature_test_macros (7)
62 参照):
63 .in
64 .sp
65 .BR fchown (),
66 .BR lchown ():
67 .PD 0
68 .ad l
69 .RS 4
70 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
71 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
72 .br
73 || /* glibc 2.12 以降: */ _POSIX_C_SOURCE\ >=\ 200809L
74 .RE
75 .ad
76 .PD
77 .SH 説明
78 これらのシステムコールは、ファイルの所有者 (owner) とグループを変更する。
79 システムコール間の違いは、ファイルの指定の仕方だけである。
80 .IP * 2
81 .BR chown ()
82
83 .I path
84 で指定されたファイルの所有権を変更する。
85 .I path
86 がシンボリック・リンクの場合は、リンクの展開が行われる。
87 .IP *
88 .BR fchown ()
89 はオープンされたファイルディスクリプタ
90 .I fd
91 により参照されるファイルの所有権を変更する。
92 .IP *
93 .BR lchown ()
94
95 .BR chown ()
96 と同じだが、シンボリック・リンクを展開しない点が異なる。
97 .PP
98 特権を持つプロセス (Linux では
99 .B CAP_CHOWN
100 ケーパビリティ (capability) を持つプロセス) だけが
101 ファイルの所有者を変更できる。
102 ファイルの所有者は、その所有者が属しているグループのいずれかに
103 ファイルのグループを変更することができる。
104 特権 (Linux では
105 .BR CAP_CHOWN )
106 を持つプロセスは、任意のグループに変更できる。
107
108 .I owner
109 または
110 .I group
111 に \-1 が指定された場合、それらの ID は変更されない。
112
113 非特権ユーザーにより実行ファイルの所有者またはグループが
114 変更された場合は
115 .B S_ISUID
116
117 .B ISGID
118 モードビットはクリアされる。
119 POSIX はこの動作やルートが
120 .BR chown ()
121 を行なった場合については特に指定されていない。
122 Linux における動作はカーネルのバージョンに依存する。
123 .\" Linux 2.0 カーネルでは、スーパー・ユーザーでの動作は
124 .\" 他のユーザーの場合と同じであった。
125 .\" 2.2 では、2.2.12 までは、スーパー・ユーザーの場合には
126 .\" これらのビットはクリアされなくなった。
127 .\" 2.2.13 以降では、スーパー・ユーザーでの動作は
128 .\" 再び他のユーザーの場合と同じになった。
129 非グループ実行ファイル
130 .RB ( S_IXGRP
131 ビットが設定されていないファイル) の場合には
132 .B S_ISGID
133 ビットは強制ロック (mandatory locking) を意味している。
134 そしてそれは
135 .BR chown ()
136 ではクリアできない。
137 .SH 返り値
138 成功すると、0 を返す。
139 失敗すると、\-1 を返し、
140 .I errno
141 に適切な値を設定する。
142 .SH エラー
143 ファイルシステムによっては他のエラーが返される事がある。
144 .B chmod
145 で一般的なエラーを以下に挙げる。
146 .TP
147 .B EACCES
148 .I path
149 の構成要素に検索許可 (search permission) がない
150 .RB ( path_resolution (7)
151 も参照)。
152 .TP
153 .B EFAULT
154 .I path
155 が割り当てられたアドレス空間外を指している。
156 .TP
157 .B ELOOP
158 .I path
159 を解決する際に遭遇したシンボリック・リンクが多過ぎる。
160 .TP
161 .B ENAMETOOLONG
162 .I path
163 が長過ぎる。
164 .TP
165 .B ENOENT
166 ファイルが存在しない。
167 .TP
168 .B ENOMEM
169 カーネルに十分なメモリがない。
170 .TP
171 .B ENOTDIR
172 .I path
173 の構成要素がディレクトリでない。
174 .TP
175 .B EPERM
176 呼び出したプロセスに所有者またはグループ (もしくはその両方) を変更するために
177 要求される許可 (上記を参照) がない。
178 .TP
179 .B EROFS
180 指定したファイルが読み込み専用 (read-only) のファイル・システム上にある。
181 .PP
182 .BR fchown ()
183 で一般的なエラーを以下に挙げる:
184 .TP
185 .B EBADF
186 ディスクリプターが有効でない。
187 .TP
188 .B EIO
189 i ノード (inode) を変更する際に低レベル I/O エラーが発生した。
190 .TP
191 .B ENOENT
192 上記を参照。
193 .TP
194 .B EPERM
195 上記を参照。
196 .TP
197 .B EROFS
198 上記を参照。
199 .SH 準拠
200 4.4BSD, SVr4, POSIX.1-2001.
201
202 4.4BSD 版ではスーパー・ユーザーのみが使用できる
203 (つまり、普通のユーザーはファイルを手放すことはできない)。
204 .\" chown():
205 .\" SVr4 には EINVAL, EINTR, ENOLINK, EMULTIHOP を返すと記載されているが、
206 .\" ENOMEM はない。
207 .\" POSIX.1 には ENOMEM, ELOOP について記載はない。
208 .\" fchown():
209 .\" SVr4 には他に EINVAL, EIO, EINTR, ENOLINK エラー状態についての記載がある。
210 .SH 注意
211 .RB ( open (2)
212
213 .BR mkdir (2)
214 などにより) 新しいファイルが作成されるとき、
215 その所有者は呼び出したプロセスのファイルシステム・ユーザ ID と
216 同じに設定される。
217 そのファイルのグループはいくつかの要因により決定される。
218 その要因としては、
219 ファイルシステムの種類、そのファイルシステムのマウント時に
220 使用されたオプション、親ディレクトリで set-group-ID 許可ビットが
221 有効になっているどうか、がある。
222 ファイルシステムが
223 .BR mount (8)
224 オプションの
225 .I "\-o\ grpid"
226 .RI ( "\-o\ bsdgroups"
227 も同義語) と
228 .I "\-o\ nogrpid"
229 .RI ( "\-o sysvgroups"
230 も同義語) に対応している場合、ルールは以下の通りとなる。
231 .IP * 2
232 ファイルシステムが
233 .I "\-o\ grpid"
234 付きでマウントされている場合、新しいファイルのグループは
235 親ディレクトリのグループと同じになる。
236 .IP *
237 ファイルシステムが
238 .I "\-o\ nogrpid"
239 付きでマウントされており、親ディレクトリでは set-group-ID ビットが
240 無効になっている場合、新しいファイルのグループは
241 プロセスのファイルシステム GID と同じになる。
242 .IP *
243 ファイルシステムが
244 .I "\-o\ nogrpid"
245 付きでマウントされており、親ディレクトリでは set-group-ID ビットが
246 有効になっている場合、新しいファイルのグループは
247 親ディレクトリのグループと同じになる。
248 .PP
249 Linux 2.6.25 では、マウントオプション
250 .I "\-o\ grpid"
251
252 .I "\-o\ nogrpid"
253 に対応しているファイルシステムは
254 ext2, ext3, ext4, XFS である。
255 これらのマウントオプションに対応していないファイルシステムでは、
256 .I "\-o\ nogrpid"
257 に関するルールが適用される。
258 .PP
259 .BR chown ()
260 方式は UID マッピングを使用した
261 NFS ファイル・システムを侵害する。
262 さらにファイルの内容にアクセスする全てのシステム・コールを侵害する。
263 これは
264 .BR chown ()
265 が既にオープンされたファイルに対する
266 アクセスをただちに取り消すことによる。
267 クライアント側のキャッシュにより所有権が変更されて
268 ユーザーのアクセスが許した時点と、実際に他のクライアントでユーザーによって
269 ファイルにアクセスできる時点との間に時間差があるかもしれない。
270
271 Linux の 2.1.81 より前のバージョン (特に 2.1.46 以前) では、
272 .BR chown ()
273 はシンボリック・リンクを追跡しない。
274 Linux 2.1.81 以降では
275 .BR chown ()
276 はシンボリック・リンクを追跡し、新たなシステム・コール
277 .BR lchown ()
278 はシンボリック・リンクを追跡しない。
279 Linux 2.1.86 以降ではこの新しいコール (古い
280 .BR chown ()
281 と全く同じ動作を行なう) は同じシステムコール番号を持ち
282 .BR chown ()
283 は新しく導入された番号を持つ。
284 .SH 例
285 .PP
286 以下のプログラムは、
287 二つ目のコマンドライン引き数で指定された名前のファイルの所有者を、
288 一つ目のコマンドライン引き数で指定された値に変更する。
289 新しい所有者は、数字のユーザ ID かユーザ名のいずれかで指定できる
290 (ユーザ名で指定した場合には、
291 .BR getpwnam (3)
292 を使ってシステムのパスワードファイルの検索が行われ、
293 ユーザ ID への変換が行われる)。
294 .nf
295
296 #include <pwd.h>
297 #include <stdio.h>
298 #include <stdlib.h>
299 #include <unistd.h>
300
301 int
302 main(int argc, char *argv[])
303 {
304     uid_t uid;
305     struct passwd *pwd;
306     char *endptr;
307
308     if (argc != 3 || argv[1][0] == \(aq\\0\(aq) {
309         fprintf(stderr, "%s <owner> <file>\\n", argv[0]);
310         exit(EXIT_FAILURE);
311     }
312
313     uid = strtol(argv[1], &endptr, 10);  /* Allow a numeric string */
314
315     if (*endptr != \(aq\\0\(aq) {         /* Was not pure numeric string */
316         pwd = getpwnam(argv[1]);   /* Try getting UID for username */
317         if (pwd == NULL) {
318             perror("getpwnam");
319             exit(EXIT_FAILURE);
320         }
321
322         uid = pwd\->pw_uid;
323     }
324
325     if (chown(argv[2], uid, \-1) == \-1) {
326         perror("chown");
327         exit(EXIT_FAILURE);
328     }
329
330     exit(EXIT_SUCCESS);
331 }
332 .fi
333 .SH 関連項目
334 .BR chmod (2),
335 .BR fchownat (2),
336 .BR flock (2),
337 .BR path_resolution (7),
338 .BR symlink (7)