File: getpwnam.3

package info (click to toggle)
manpages-ja 0.5.0.0.20180315%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 28,516 kB
  • sloc: perl: 161; makefile: 101
file content (249 lines) | stat: -rw-r--r-- 11,519 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
.\" and Copyright 2008, Linux Foundation, written by Michael Kerrisk
.\"     <mtk.manpages@gmail.com>
.\"
.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\" %%%LICENSE_END
.\"
.\" References consulted:
.\"     Linux libc source code
.\"     Lewine's "POSIX Programmer's Guide" (O'Reilly & Associates, 1991)
.\"     386BSD man pages
.\"
.\" Modified 1993-07-24 by Rik Faith (faith@cs.unc.edu)
.\" Modified 1996-05-27 by Martin Schulze (joey@linux.de)
.\" Modified 2003-11-15 by aeb
.\" 2008-11-07, mtk, Added an example program for getpwnam_r().
.\"
.\"*******************************************************************
.\"
.\" This file was generated with po4a. Translate the source file.
.\"
.\"*******************************************************************
.\"
.\" Japanese Version Copyright (c) 1997 HIROFUMI Nishizuka
.\"	all rights reserved.
.\" Translated 1997-12-18, HIROFUMI Nishizuka <nishi@rpts.cl.nec.co.jp>
.\" Updated & Modified 2004-01-06, Yuichi SATO <ysato444@yahoo.co.jp>
.\" Updated & Modified 2005-09-06, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
.\" Updated & Modified 2005-10-08, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
.\" Updated 2008-12-24, Akihiro MOTOKI, LDP v3.14
.\" Updated 2012-05-01, Akihiro MOTOKI <amotoki@gmail.com>
.\" Updated 2013-08-16, Akihiro MOTOKI <amotoki@gmail.com>
.\"
.TH GETPWNAM 3 2013\-07\-22 GNU "Linux Programmer's Manual"
.SH 名前
getpwnam, getpwnam_r, getpwuid, getpwuid_r \- パスワードファイルのエントリーの取得
.SH 書式
.nf
\fB#include <sys/types.h>\fP
\fB#include <pwd.h>\fP
.sp
\fBstruct passwd *getpwnam(const char *\fP\fIname\fP\fB);\fP
.sp
\fBstruct passwd *getpwuid(uid_t \fP\fIuid\fP\fB);\fP
.sp
\fBint getpwnam_r(const char *\fP\fIname\fP\fB, struct passwd *\fP\fIpwd\fP\fB,\fP
.br
\fB               char *\fP\fIbuf\fP\fB, size_t \fP\fIbuflen\fP\fB, struct passwd **\fP\fIresult\fP\fB);\fP
.sp
\fBint getpwuid_r(uid_t \fP\fIuid\fP\fB, struct passwd *\fP\fIpwd\fP\fB,\fP
.br
\fB               char *\fP\fIbuf\fP\fB, size_t \fP\fIbuflen\fP\fB, struct passwd **\fP\fIresult\fP\fB);\fP
.fi
.sp
.in -4n
glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
.in
.sp
.ad l
\fBgetpwnam_r\fP(), \fBgetpwuid_r\fP():
.RS 4
_POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE
|| _POSIX_SOURCE
.RE
.ad b
.SH 説明
\fBgetpwnam\fP()  関数は、ユーザー名 \fIname\fP にマッチするパスワードデータベースのエントリーを
要素毎に分解し、各要素を格納した構造体へのポインターを返す (パスワードデータベースの例: ローカルのパスワードファイル \fI/etc/passwd\fP,
NIS, LDAP)。
.PP
\fBgetpwuid\fP()  関数は、ユーザー ID \fIuid\fP にマッチするパスワードデータベースのエントリーを
要素毎に分解し、各要素を格納した構造体へのポインターを返す。
.PP
\fIpasswd\fP 構造体は、\fI<pwd.h>\fP で以下のように定義されている:
.sp
.in +4n
.nf
struct passwd {
    char   *pw_name;       /* ユーザー名 */
    char   *pw_passwd;     /* ユーザーのパスワード */
    uid_t   pw_uid;        /* ユーザー ID */
    gid_t   pw_gid;        /* グループ ID */
    char   *pw_gecos;      /* ユーザー情報 */
    char   *pw_dir;        /* ホームディレクトリ */
    char   *pw_shell;      /* シェルプログラム */
};
.fi
.in
.PP
これらのフィールドの詳しい情報については \fBpasswd\fP(5) を参照のこと。
.PP
\fBgetpwnam_r\fP() と \fBgetpwuid_r\fP() 関数は、それぞれ\fBgetpwnam\fP() と
\fBgetpwuid\fP() と同じ情報を取得するが、取得した \fIpasswd\fP 構造体を
\fIpwd\fP が指す領域に格納する。\fIpasswd\fP 構造体のメンバーが指す文字列は、
サイズ \fIbuflen\fP のバッファー \fIbuf\fP に格納される。成功した場合
\fI*result\fP には結果へのポインターが格納される。エントリーが見つからなかった
場合やエラーが発生した場合には \fI*result\fP には NULL が入る。
.PP
呼び出し

    sysconf(_SC_GETPW_R_SIZE_MAX)

は、 \fIerrno\fP を変更せずに \-1 を返すか、 \fIbuf\fP の初期サイズの推奨値を
返す。(このサイズが小さすぎる場合、呼び出しは \fBERANGE\fP で失敗し、この
場合には呼び出し側はバッファーを大きくしてから再度呼び出すことができる。)
.SH 返り値
\fBgetpwnam\fP()  と \fBgetpwuid\fP()  関数は、 \fIpasswd\fP 構造体へのポインターを返す。
一致するエントリーが見つからなかった場合や、エラーが発生した場合は NULL を返す。 エラーが起こった場合、 \fIerrno\fP が適切に設定される。
呼び出しの後で \fIerrno\fP をチェックしたい場合は、 呼び出しの前に (この値を) 0 に設定しておくべきである。
.LP
返り値は静的な領域を指しており、その後の \fBgetpwent\fP(3), \fBgetpwnam\fP(), \fBgetpwuid\fP()
の呼び出しで上書きされるかもしれない。 (返されたポインターを \fBfree\fP(3)  に渡さないこと。)
.LP
成功すると、 \fBgetpwnam_r\fP()  と \fBgetpwuid_r\fP()  は 0 を返し、 \fI*result\fP に \fIpwd\fP
を設定する。 マッチするパスワードエントリーが見つからなかった場合には、 0 を返し、 \fI*result\fP に NULL を設定する。
エラーの場合、エラー番号を返し、 \fI*result\fP に NULL を設定する。
.SH エラー
.TP 
\fB0\fP または \fBENOENT\fP または \fBESRCH\fP または \fBEBADF\fP または \fBEPERM\fP または ... 
指定された \fIname\fP または \fIuid\fP が見つからなかった。
.TP 
\fBEINTR\fP
シグナルが捕捉された。
.TP 
\fBEIO\fP
I/O エラー。
.TP 
\fBEMFILE\fP
呼び出し元プロセスがオープンしているファイル数が すでに上限 (\fBOPEN_MAX\fP)  であった。
.TP 
\fBENFILE\fP
システムでオープンされているファイル数がすでに上限であった。
.TP 
\fBENOMEM\fP
.\" not in POSIX
.\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45)
\fIpasswd\fP 構造体に割り当てるメモリーが十分なかった。
.TP 
\fBERANGE\fP
与えられたバッファー空間が不十分である。
.SH ファイル
.TP 
\fI/etc/passwd\fP
ローカルのパスワードデータベースファイル
.SH 属性
.SS "マルチスレッディング (pthreads(7) 参照)"
関数 \fBgetpwnam\fP() と \fBgetpwuid\fP() はスレッドセーフではない。
.LP
関数 \fBgetpwnam_r\fP() と \fBgetpwuid_r\fP() はスレッドセーフである。
.SH 準拠
SVr4, 4.3BSD, POSIX.1\-2001.  \fIpw_gecos\fP フィールドは POSIX では規定されていないが、
ほとんどの実装に存在する。
.SH 注意
.\" more precisely:
.\" AIX 5.1 - gives ESRCH
.\" OSF1 4.0g - gives EWOULDBLOCK
.\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT
.\" glibc since version 2.7 - give 0
.\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM
.\" SunOS 5.8 - gives EBADF
.\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0
上記の「返り値」以下の記述は POSIX.1\-2001 に拠る。 この標準は「(エントリーが) 見つからないこと」をエラーとしていないので、
そのような場合に \fIerrno\fP がどのような値になるかを定めていない。 そのため、エラーを認識することは不可能である。 POSIX
に準拠して、エントリーが見つからない場合は \fIerrno\fP を変更しないようにすべきである、と主張する人もいるかもしれない。 様々な UNIX
系のシステムで試してみると、そのような場合には 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM
といった様々な値が返される。 他の値が返されるかもしれない。

フィールド \fIpw_dir\fP には、ユーザーの作業ディレクトリ名の初期値が格納される。 ログインプロセスは、このフィールドの値を使って、
ログインシェルの \fBHOME\fP 環境変数を初期化する。 アプリケーションが、ユーザーのホームディレクトリを決定する場合には、
(\fIgetpwuid(getuid())\->pw_dir\fP の値ではなく)  \fBHOME\fP の値を検査するようにすべきである。
なぜなら、このようにすることで、ユーザーがログインセッション中で 「ホームディレクトリ」の意味を変更できるようになるからである。
別のユーザーのホームディレクトリ (の初期値) を知るには \fIgetpwnam("username")\->pw_dir\fP
か同様の方法を使う必要がある。
.SH 例
以下のプログラムは \fBgetpwnam_r\fP()  の使用例を示したもので、コマンドライン引き数で渡されたユーザー名に対する
完全なユーザー名とユーザー ID を探すものである。

.nf
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

int
main(int argc, char *argv[])
{
    struct passwd pwd;
    struct passwd *result;
    char *buf;
    size_t bufsize;
    int s;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s username\en", argv[0]);
        exit(EXIT_FAILURE);
    }

    bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
    if (bufsize == \-1)          /* 値を決定できなかった */
        bufsize = 16384;        /* 十分大きな値にすべき */

    buf = malloc(bufsize);
    if (buf == NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }

    s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
    if (result == NULL) {
        if (s == 0)
            printf("Not found\en");
        else {
            errno = s;
            perror("getpwnam_r");
        }
        exit(EXIT_FAILURE);
    }

    printf("Name: %s; UID: %ld\en", pwd.pw_gecos, (long) pwd.pw_uid);
    exit(EXIT_SUCCESS);
}
.fi
.SH 関連項目
\fBendpwent\fP(3), \fBfgetpwent\fP(3), \fBgetgrnam\fP(3), \fBgetpw\fP(3),
\fBgetpwent\fP(3), \fBgetspnam\fP(3), \fBputpwent\fP(3), \fBsetpwent\fP(3), \fBpasswd\fP(5)
.SH この文書について
この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部
である。プロジェクトの説明とバグ報告に関する情報は
http://www.kernel.org/doc/man\-pages/ に書かれている。