getpwnamのヘルプ・マニュアル
日本語 英語
getpwnam --help
man getpwnam
GETPWNAM(3) Linux Programmer’s Manual GETPWNAM(3)
名前
getpwnam, getpwnam_r, getpwuid, getpwuid_r - パスワードファイルのエント
リの取得
書式
#include
#include
struct passwd *getpwnam(const char *name);
struct passwd *getpwuid(uid_t uid);
int getpwnam_r(const char *name, struct passwd *pwd,
char *buf, size_t buflen, struct passwd **result);
int getpwuid_r(uid_t uid, struct passwd *pwd,
char *buf, size_t buflen, struct passwd **result);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
getpwnam_r(), getpwuid_r(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE ||
_BSD_SOURCE || _SVID_SOURCE || _POSIX_SOURCE
説明
getpwnam() 関数は、ユーザ名 name にマッチするパスワード・データベースの
エントリを要素毎に分解し、各要素を格納した構造体へのポインタを返す ( パ
ス ワ ード・データベースの例: ローカルのパスワードファイル /etc/passwd,
NIS, LDAP)。
getpwuid() 関数は、ユーザ ID uid にマッチするパスワード・データベースの
エントリを要素毎に分解し、各要素を格納した構造体へのポインタを返す。
getpwnam_r() と getpwuid_r() 関数は (上記の関数と) 同じ情報を取得するが
、取得した passwd 構造体を pwd が指す領域に格納する。この passwd 構造体
に は文字列へのポインタが含まれ、これらの文字列はサイズ buflen のバッフ
ァ buf に格納される。成功した場合 *result には結果へのポインタが格納 さ
れ る。エントリが見つからなかった場合やエラーが発生した場合には *result
には NULL が入る。
passwd 構造体は、 で以下のように定義されている:
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; /* シェルプログラム */
};
buf に最大必要なサイズは、 sysconf(3) に引き数 _SC_GETPW_R_SIZE_MAX を
指定して実行することで分かる。
返り値
getpwnam() と getpwuid() 関数は、 passwd 構造体へのポインタを返す。一致
するエントリが見つからなかった場合や、エラーが発生した場合は NULL を 返
す 。 エ ラ ーが起こった場合、 errno が適切に設定される。呼び出しの後で
errno をチェックしたい場合は、呼び出しの前に (この値を) 0 に設定して お
くべきである。
返 り 値 は 静 的 な領域を指しており、その後の getpwent(3), getpwnam(),
getpwuid() の呼び出しで上書きされるかもしれない。 (返されたポイン タ を
free(3) に渡さないこと。)
成 功すると、 getpwnam_r() と getpwuid_r() は 0 を返し、 *result に pwd
を設定する。マッチするパスワード・エントリが見つからなかった場合には 、
0 を返し、 *result に NULL を設定する。エラーの場合、エラー番号を返し、
*result に NULL を設定する。
エラー
0 または ENOENT または ESRCH または EBADF または EPERM または ...
指定された name または uid が見つからなかった。
EINTR シグナルがキャッチされた。
EIO I/O エラー。
EMFILE 呼び出したプロセスにおいて、既に最大数 (OPEN_MAX) のファイルがオ
ープンされている。
ENFILE システム上で既に最大数のファイルがオープンされている。
ENOMEM passwd 構造体を割り当てるためのメモリが不十分。
ERANGE 与えられたバッファ空間が不十分である。
ファイル
/etc/passwd
ローカルのパスワード・データベースファイル
準拠
SVr4, 4.3BSD, POSIX.1-2001.
注意
上記の「返り値」以下の記述は POSIX.1-2001 に拠る。この標準は「(エントリ
が) 見つからないこと」をエラーとしていないので、そのような場合に errno
が どのような値になるかを定めていない。そのため、エラーを認識することは
不可能である。 POSIX に準拠して、エントリが見つからない場合は errno を
変 更 し ないようにすべきである、と主張する人もいるかもしれない。様々な
Unix 系のシステムで試してみると、そのような場合には 0, ENOENT, EBADF,
ESRCH, EWOULDBLOCK, EPERM といった様々な値が返される。他の値が返される
かもしれない。
フィールド pw_dir には、ユーザの作業ディレクトリ名の初期値が格納され る
。ログインプロセスは、このフィールドの値を使って、ログインシェルの HOME
環境変数を初期化する。アプリケーションが、ユーザのホーム・ディレクト リ
を 決定する場合には、 (getpwuid(getuid())->pw_dir の値ではなく) HOME の
値を検査するようにすべきである。なぜなら、このようにすることで、ユー ザ
が ログイン・セッション中で「ホーム・ディレクトリ」の意味を変更できるよ
うになるからである。別のユーザのホーム・ディレクトリ (の初期値) を知 る
には getpwnam("username")->pw_dir か同様の方法を使う必要がある。
例
以 下のプログラムは getpwnam_r() の使用例を示したもので、コマンドライン
引き数で渡されたユーザ名に対する完全なユーザ名とユーザ ID を探すもの で
ある。
#include
#include
#include
#include
#include
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\n", 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\n");
else {
errno = s;
perror("getpwnam_r");
}
exit(EXIT_FAILURE);
}
printf("Name: %s; UID: %ld0, pwd.pw_gecos, (long) pwd.pw_uid);
exit(EXIT_SUCCESS);
}
関連項目
endpwent(3), fgetpwent(3), getgrnam(3), getpw(3), getpwent(3), getsp-
nam(3), putpwent(3), setpwent(3), passwd(5)
GNU 2009-03-30 GETPWNAM(3)
GETPWNAM(3) Linux Programmer’s Manual GETPWNAM(3)
NAME
getpwnam, getpwnam_r, getpwuid, getpwuid_r - get password file entry
SYNOPSIS
#include
#include
struct passwd *getpwnam(const char *name);
struct passwd *getpwuid(uid_t uid);
int getpwnam_r(const char *name, struct passwd *pwd,
char *buf, size_t buflen, struct passwd **result);
int getpwuid_r(uid_t uid, struct passwd *pwd,
char *buf, size_t buflen, struct passwd **result);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
getpwnam_r(), getpwuid_r(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE ||
_BSD_SOURCE || _SVID_SOURCE || _POSIX_SOURCE
DESCRIPTION
The getpwnam() function returns a pointer to a structure containing the
broken-out fields of the record in the password database (e.g., the
local password file /etc/passwd, NIS, and LDAP) that matches the user-
name name.
The getpwuid() function returns a pointer to a structure containing the
broken-out fields of the record in the password database that matches
the user ID uid.
The getpwnam_r() and getpwuid_r() functions obtain the same informa-
tion, but store the retrieved passwd structure in the space pointed to
by pwd. This passwd structure contains pointers to strings, and these
strings are stored in the buffer buf of size buflen. A pointer to the
result (in case of success) or NULL (in case no entry was found or an
error occurred) is stored in *result.
The passwd structure is defined in as follows:
struct passwd {
char *pw_name; /* username */
char *pw_passwd; /* user password */
uid_t pw_uid; /* user ID */
gid_t pw_gid; /* group ID */
char *pw_gecos; /* real name */
char *pw_dir; /* home directory */
char *pw_shell; /* shell program */
};
The maximum needed size for buf can be found using sysconf(3) with the
argument _SC_GETPW_R_SIZE_MAX.
RETURN VALUE
The getpwnam() and getpwuid() functions return a pointer to a passwd
structure, or NULL if the matching entry is not found or an error
occurs. If an error occurs, errno is set appropriately. If one wants
to check errno after the call, it should be set to zero before the
call.
The return value may point to a static area, and may be overwritten by
subsequent calls to getpwent(3), getpwnam(), or getpwuid(). (Do not
pass the returned pointer to free(3).)
On success, getpwnam_r() and getpwuid_r() return zero, and set *result
to pwd. If no matching password record was found, these functions
return 0 and store NULL in *result. In case of error, an error number
is returned, and NULL is stored in *result.
ERRORS
0 or ENOENT or ESRCH or EBADF or EPERM or ...
The given name or uid was not found.
EINTR A signal was caught.
EIO I/O error.
EMFILE The maximum number (OPEN_MAX) of files was open already in the
calling process.
ENFILE The maximum number of files was open already in the system.
ENOMEM Insufficient memory to allocate passwd structure.
ERANGE Insufficient buffer space supplied.
FILES
/etc/passwd
local password database file
CONFORMING TO
SVr4, 4.3BSD, POSIX.1-2001.
NOTES
The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
It does not call "not found" an error, and hence does not specify what
value errno might have in this situation. But that makes it impossible
to recognize errors. One might argue that according to POSIX errno
should be left unchanged if an entry is not found. Experiments on var-
ious Unix-like systems show that lots of different values occur in this
situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM and probably
others.
The pw_dir field contains the name of the initial working directory of
the user. Login programs use the value of this field to initialize the
HOME environment variable for the login shell. An application that
wants to determine its user’s home directory should inspect the value
of HOME (rather than the value getpwuid(getuid())->pw_dir) since this
allows the user to modify their notion of "the home directory" during a
login session. To determine the (initial) home directory of another
user, it is necessary to use getpwnam("username")->pw_dir or similar.
EXAMPLE
The program below demonstrates the use of getpwnam_r() to find the full
username and user ID for the username supplied as a command-line argu-
ment.
#include
#include
#include
#include
#include
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\n", argv[0]);
exit(EXIT_FAILURE);
}
bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
if (bufsize == -1) /* Value was indeterminate */
bufsize = 16384; /* Should be more than enough */
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\n");
else {
errno = s;
perror("getpwnam_r");
}
exit(EXIT_FAILURE);
}
printf("Name: %s; UID: %ld0, pwd.pw_gecos, (long) pwd.pw_uid);
exit(EXIT_SUCCESS);
}
SEE ALSO
endpwent(3), fgetpwent(3), getgrnam(3), getpw(3), getpwent(3), getsp-
nam(3), putpwent(3), setpwent(3), passwd(5)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU 2009-03-30 GETPWNAM(3)