OSDN Git Service

Update release for LDP 3.67
[linuxjm/LDP_man-pages.git] / release / man3 / getpwnam.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\" and Copyright 2008, Linux Foundation, written by Michael Kerrisk
3 .\"     <mtk.manpages@gmail.com>
4 .\"
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
26 .\"
27 .\" References consulted:
28 .\"     Linux libc source code
29 .\"     Lewine's "POSIX Programmer's Guide" (O'Reilly & Associates, 1991)
30 .\"     386BSD man pages
31 .\"
32 .\" Modified 1993-07-24 by Rik Faith (faith@cs.unc.edu)
33 .\" Modified 1996-05-27 by Martin Schulze (joey@linux.de)
34 .\" Modified 2003-11-15 by aeb
35 .\" 2008-11-07, mtk, Added an example program for getpwnam_r().
36 .\"
37 .\"*******************************************************************
38 .\"
39 .\" This file was generated with po4a. Translate the source file.
40 .\"
41 .\"*******************************************************************
42 .\"
43 .\" Japanese Version Copyright (c) 1997 HIROFUMI Nishizuka
44 .\"     all rights reserved.
45 .\" Translated 1997-12-18, HIROFUMI Nishizuka <nishi@rpts.cl.nec.co.jp>
46 .\" Updated & Modified 2004-01-06, Yuichi SATO <ysato444@yahoo.co.jp>
47 .\" Updated & Modified 2005-09-06, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
48 .\" Updated & Modified 2005-10-08, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
49 .\" Updated 2008-12-24, Akihiro MOTOKI, LDP v3.14
50 .\" Updated 2012-05-01, Akihiro MOTOKI <amotoki@gmail.com>
51 .\" Updated 2013-08-16, Akihiro MOTOKI <amotoki@gmail.com>
52 .\"
53 .TH GETPWNAM 3 2013\-07\-22 GNU "Linux Programmer's Manual"
54 .SH 名前
55 getpwnam, getpwnam_r, getpwuid, getpwuid_r \- パスワードファイルのエントリの取得
56 .SH 書式
57 .nf
58 \fB#include <sys/types.h>\fP
59 \fB#include <pwd.h>\fP
60 .sp
61 \fBstruct passwd *getpwnam(const char *\fP\fIname\fP\fB);\fP
62 .sp
63 \fBstruct passwd *getpwuid(uid_t \fP\fIuid\fP\fB);\fP
64 .sp
65 \fBint getpwnam_r(const char *\fP\fIname\fP\fB, struct passwd *\fP\fIpwd\fP\fB,\fP
66 .br
67 \fB            char *\fP\fIbuf\fP\fB, size_t \fP\fIbuflen\fP\fB, struct passwd **\fP\fIresult\fP\fB);\fP
68 .sp
69 \fBint getpwuid_r(uid_t \fP\fIuid\fP\fB, struct passwd *\fP\fIpwd\fP\fB,\fP
70 .br
71 \fB            char *\fP\fIbuf\fP\fB, size_t \fP\fIbuflen\fP\fB, struct passwd **\fP\fIresult\fP\fB);\fP
72 .fi
73 .sp
74 .in -4n
75 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
76 .in
77 .sp
78 .ad l
79 \fBgetpwnam_r\fP(), \fBgetpwuid_r\fP():
80 .RS 4
81 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE
82 || _POSIX_SOURCE
83 .RE
84 .ad b
85 .SH 説明
86 \fBgetpwnam\fP()  関数は、ユーザ名 \fIname\fP にマッチするパスワード・データベースのエントリを
87 要素毎に分解し、各要素を格納した構造体へのポインタを返す (パスワード・データベースの例: ローカルのパスワードファイル \fI/etc/passwd\fP,
88 NIS, LDAP)。
89 .PP
90 \fBgetpwuid\fP()  関数は、ユーザ ID \fIuid\fP にマッチするパスワード・データベースのエントリを
91 要素毎に分解し、各要素を格納した構造体へのポインタを返す。
92 .PP
93 \fIpasswd\fP 構造体は、\fI<pwd.h>\fP で以下のように定義されている:
94 .sp
95 .in +4n
96 .nf
97 struct passwd {
98     char   *pw_name;       /* ユーザ名 */
99     char   *pw_passwd;     /* ユーザのパスワード */
100     uid_t   pw_uid;        /* ユーザ ID */
101     gid_t   pw_gid;        /* グループ ID */
102     char   *pw_gecos;      /* ユーザ情報 */
103     char   *pw_dir;        /* ホームディレクトリ */
104     char   *pw_shell;      /* シェルプログラム */
105 };
106 .fi
107 .in
108 .PP
109 これらのフィールドの詳しい情報については \fBpasswd\fP(5) を参照のこと。
110 .PP
111 \fBgetpwnam_r\fP() と \fBgetpwuid_r\fP() 関数は、それぞれ\fBgetpwnam\fP() と
112 \fBgetpwuid\fP() と同じ情報を取得するが、取得した \fIpasswd\fP 構造体を
113 \fIpwd\fP が指す領域に格納する。\fIpasswd\fP 構造体のメンバーが指す文字列は、
114 サイズ \fIbuflen\fP のバッファ \fIbuf\fP に格納される。成功した場合
115 \fI*result\fP には結果へのポインタが格納される。エントリが見つからなかった
116 場合やエラーが発生した場合には \fI*result\fP には NULL が入る。
117 .PP
118 呼び出し
119
120     sysconf(_SC_GETPW_R_SIZE_MAX)
121
122 は、 \fIerrno\fP を変更せずに \-1 を返すか、 \fIbuf\fP の初期サイズの推奨値を
123 返す。(このサイズが小さすぎる場合、呼び出しは \fBERANGE\fP で失敗し、この
124 場合には呼び出し側はバッファを大きくしてから再度呼び出すことができる。)
125 .SH 返り値
126 \fBgetpwnam\fP()  と \fBgetpwuid\fP()  関数は、 \fIpasswd\fP 構造体へのポインタを返す。
127 一致するエントリが見つからなかった場合や、エラーが発生した場合は NULL を返す。 エラーが起こった場合、 \fIerrno\fP が適切に設定される。
128 呼び出しの後で \fIerrno\fP をチェックしたい場合は、 呼び出しの前に (この値を) 0 に設定しておくべきである。
129 .LP
130 返り値は静的な領域を指しており、その後の \fBgetpwent\fP(3), \fBgetpwnam\fP(), \fBgetpwuid\fP()
131 の呼び出しで上書きされるかもしれない。 (返されたポインタを \fBfree\fP(3)  に渡さないこと。)
132 .LP
133 成功すると、 \fBgetpwnam_r\fP()  と \fBgetpwuid_r\fP()  は 0 を返し、 \fI*result\fP に \fIpwd\fP
134 を設定する。 マッチするパスワード・エントリが見つからなかった場合には、 0 を返し、 \fI*result\fP に NULL を設定する。
135 エラーの場合、エラー番号を返し、 \fI*result\fP に NULL を設定する。
136 .SH エラー
137 .TP 
138 \fB0\fP または \fBENOENT\fP または \fBESRCH\fP または \fBEBADF\fP または \fBEPERM\fP または ... 
139 指定された \fIname\fP または \fIuid\fP が見つからなかった。
140 .TP 
141 \fBEINTR\fP
142 シグナルが捕捉された。
143 .TP 
144 \fBEIO\fP
145 I/O エラー。
146 .TP 
147 \fBEMFILE\fP
148 呼び出し元プロセスがオープンしているファイル数が すでに上限 (\fBOPEN_MAX\fP)  であった。
149 .TP 
150 \fBENFILE\fP
151 システムでオープンされているファイル数がすでに上限であった。
152 .TP 
153 \fBENOMEM\fP
154 .\" not in POSIX
155 .\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45)
156 \fIpasswd\fP 構造体に割り当てるメモリが十分なかった。
157 .TP 
158 \fBERANGE\fP
159 与えられたバッファ空間が不十分である。
160 .SH ファイル
161 .TP 
162 \fI/etc/passwd\fP
163 ローカルのパスワード・データベースファイル
164 .SH 属性
165 .SS "マルチスレッディング (pthreads(7) 参照)"
166 関数 \fBgetpwnam\fP() と \fBgetpwuid\fP() はスレッドセーフではない。
167 .LP
168 関数 \fBgetpwnam_r\fP() と \fBgetpwuid_r\fP() はスレッドセーフである。
169 .SH 準拠
170 SVr4, 4.3BSD, POSIX.1\-2001.  \fIpw_gecos\fP フィールドは POSIX では規定されていないが、
171 ほとんどの実装に存在する。
172 .SH 注意
173 .\" more precisely:
174 .\" AIX 5.1 - gives ESRCH
175 .\" OSF1 4.0g - gives EWOULDBLOCK
176 .\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT
177 .\" glibc since version 2.7 - give 0
178 .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM
179 .\" SunOS 5.8 - gives EBADF
180 .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0
181 上記の「返り値」以下の記述は POSIX.1\-2001 に拠る。 この標準は「(エントリが) 見つからないこと」をエラーとしていないので、
182 そのような場合に \fIerrno\fP がどのような値になるかを定めていない。 そのため、エラーを認識することは不可能である。 POSIX
183 に準拠して、エントリが見つからない場合は \fIerrno\fP を変更しないようにすべきである、と主張する人もいるかもしれない。 様々な UNIX
184 系のシステムで試してみると、そのような場合には 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM
185 といった様々な値が返される。 他の値が返されるかもしれない。
186
187 フィールド \fIpw_dir\fP には、ユーザの作業ディレクトリ名の初期値が格納される。 ログインプロセスは、このフィールドの値を使って、
188 ログインシェルの \fBHOME\fP 環境変数を初期化する。 アプリケーションが、ユーザのホーム・ディレクトリを決定する場合には、
189 (\fIgetpwuid(getuid())\->pw_dir\fP の値ではなく)  \fBHOME\fP の値を検査するようにすべきである。
190 なぜなら、このようにすることで、ユーザがログイン・セッション中で 「ホーム・ディレクトリ」の意味を変更できるようになるからである。
191 別のユーザのホーム・ディレクトリ (の初期値) を知るには \fIgetpwnam("username")\->pw_dir\fP
192 か同様の方法を使う必要がある。
193 .SH 例
194 以下のプログラムは \fBgetpwnam_r\fP()  の使用例を示したもので、コマンドライン引き数で渡されたユーザ名に対する 完全なユーザ名とユーザ
195 ID を探すものである。
196
197 .nf
198 #include <pwd.h>
199 #include <stdio.h>
200 #include <stdlib.h>
201 #include <unistd.h>
202 #include <errno.h>
203
204 int
205 main(int argc, char *argv[])
206 {
207     struct passwd pwd;
208     struct passwd *result;
209     char *buf;
210     size_t bufsize;
211     int s;
212
213     if (argc != 2) {
214         fprintf(stderr, "Usage: %s username\en", argv[0]);
215         exit(EXIT_FAILURE);
216     }
217
218     bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
219     if (bufsize == \-1)          /* 値を決定できなかった */
220         bufsize = 16384;        /* 十分大きな値にすべき */
221
222     buf = malloc(bufsize);
223     if (buf == NULL) {
224         perror("malloc");
225         exit(EXIT_FAILURE);
226     }
227
228     s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
229     if (result == NULL) {
230         if (s == 0)
231             printf("Not found\en");
232         else {
233             errno = s;
234             perror("getpwnam_r");
235         }
236         exit(EXIT_FAILURE);
237     }
238
239     printf("Name: %s; UID: %ld\en", pwd.pw_gecos, (long) pwd.pw_uid);
240     exit(EXIT_SUCCESS);
241 }
242 .fi
243 .SH 関連項目
244 \fBendpwent\fP(3), \fBfgetpwent\fP(3), \fBgetgrnam\fP(3), \fBgetpw\fP(3),
245 \fBgetpwent\fP(3), \fBgetspnam\fP(3), \fBputpwent\fP(3), \fBsetpwent\fP(3), \fBpasswd\fP(5)
246 .SH この文書について
247 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.67 の一部
248 である。プロジェクトの説明とバグ報告に関する情報は
249 http://www.kernel.org/doc/man\-pages/ に書かれている。