OSDN Git Service

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