.\" Copyright 1995 James R. Van Zandt .\" .\" %%%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 .\" .\" Changed Tue Sep 19 01:49:29 1995, aeb: moved from man2 to man3 .\" added ref to /etc/utmp, added BUGS section, etc. .\" modified 2003 Walter Harms, aeb - added getlogin_r, note on stdin use .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1998 NAKANO Takeo all rights reserved. .\" Translated 1998-03-01, NAKANO Takeo .\" Updated & Modified 1999-08-21, NAKANO Takeo .\" Updated & Modified 2005-03-18, Akihiro MOTOKI .\" Updated 2013-03-25, Akihiro MOTOKI .\" Updated 2013-07-24, Akihiro MOTOKI .\" .TH GETLOGIN 3 " 2019\-03\-06" GNU "Linux Programmer's Manual" .SH 名前 getlogin, getlogin_r, cuserid \- ユーザー名を取得する .SH 書式 \fB#include \fP .PP \fBchar *getlogin(void);\fP .br \fBint getlogin_r(char *\fP\fIbuf\fP\fB, size_t \fP\fIbufsize\fP\fB);\fP .PP \fB#include \fP .PP \fBchar *cuserid(char *\fP\fIstring\fP\fB);\fP .PP .RS -4 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照): .RE .PP .\" Deprecated: _REENTRANT || \fBgetlogin_r\fP(): _POSIX_C_SOURCE\ >=\ 199506L .PP \fBcuserid\fP(): .nf glibc 2.24 以降: (_XOPEN_SOURCE && ! (_POSIX_C_SOURCE >= 200112L) || _GNU_SOURCE glibc 2.23 以前: _XOPEN_SOURCE .fi .SH 説明 \fBgetlogin\fP() は、現在のプロセスの制御端末にログインしているユーザー名の文字列への ポインターを返す。ユーザー名が決定できない場合はヌルポインターを返す。 文字列は静的領域に割り当てられており、この後でこの関数や \fBcuserid\fP() が呼び出された際に上書きされることがある。 .PP \fBgetlogin_r\fP() は、上記の同じユーザー名を、大きさ \fIbufsize\fP の配列 \fIbuf\fP に入れて返す。 .PP \fBcuserid\fP() は、現在のプロセスの実効ユーザーID に対応するユーザー名の 文字列へのポインターを返す。 \fIstring\fP がヌルポインター以外の場合、\fIstring\fP は少なくとも \fBL_cuserid\fP 文字を保持できる配列でなければならない。 \fIstring\fP が ヌルポインターの場合には、静的領域に置かれた文字列への ポインターが返される。この文字列は静的領域に割り当てられており、後で この関数や \fBgetlogin\fP() が呼び出された際に上書きされることがある。 .PP マクロ \fBL_cuserid\fP は integer の定数で、ユーザー名を保持するために 必要な配列の長さを示す。 \fBL_cuserid\fP は \fBstdio.h\fP で宣言されて いる。 .PP これらの関数を使うと、プログラムを実行しているユーザー (\fBcuserid\fP()) や このセッションにログインしているユーザー (\fBgetlogin\fP()) を明確に特定することができる (ただし set\-user\-ID プログラムでは、状況が違うこともある)。 .PP たいていの目的では、ユーザーの特定には環境変数 \fBLOGNAME\fP を調べ るほうが便利である。LOGNAME 変数はユーザーが自由に設定できるので より柔軟な対応が可能になる。 .SH 返り値 \fBgetlogin\fP() は成功した場合はユーザー名へのポインターを返す。 失敗した場合は NULL を返し、 \fIerrno\fP にエラーの原因を示す値を設定する。 \fBgetlogin_r\fP() は成功すると 0 を返し、失敗すると 0 以外を返す。 .SH エラー POSIX では以下のエラーが規定されている: .TP \fBEMFILE\fP The per\-process limit on the number of open file descriptors has been reached. .TP \fBENFILE\fP オープンされたファイルの総数がシステム全体の上限に達していた。 .TP \fBENXIO\fP 呼び出し元プロセスには制御端末がない。 .TP \fBERANGE\fP (getlogin_r) (終端のヌルバイト (\(aq\e0\(aq) も含めた) ユーザー名の長さが \fIbufsize\fP よりも長い。 .PP Linux/glibc には以下のエラーもある。 .TP \fBENOENT\fP utmp ファイルに対応するエントリーがなかった。 .TP \fBENOMEM\fP passwd 構造体を割り当てるのに十分なメモリーがない。 .TP \fBENOTTY\fP 標準入力が端末を参照していない (「バグ」の節を参照)。 .SH ファイル .TP \fI/etc/passwd\fP パスワードデータベースのファイル .TP \fI/var/run/utmp\fP (伝統的には \fI/etc/utmp\fP が使われている; libc の中には \fI/var/adm/utmp\fP を使うものもある) .SH 属性 この節で使用されている用語の説明については、 \fBattributes\fP(7) を参照。 .TS allbox; lb lb lb l l l. インターフェース 属性 値 T{ \fBgetlogin\fP() T} Thread safety T{ MT\-Unsafe race:getlogin race:utent .br sig:ALRM timer locale T} T{ \fBgetlogin_r\fP() T} Thread safety T{ MT\-Unsafe race:utent sig:ALRM timer .br locale T} T{ \fBcuserid\fP() T} Thread safety MT\-Unsafe race:cuserid/!string locale .TE .sp 1 In the above table, \fIutent\fP in \fIrace:utent\fP signifies that if any of the functions \fBsetutent\fP(3), \fBgetutent\fP(3), or \fBendutent\fP(3) are used in parallel in different threads of a program, then data races could occur. \fBgetlogin\fP() and \fBgetlogin_r\fP() call those functions, so we use race:utent to remind users. .SH 準拠 \fBgetlogin\fP() and \fBgetlogin_r\fP(): POSIX.1\-2001, POSIX.1\-2008. .PP System V にも \fBcuserid\fP() があるが、 これは実効ユーザー ID ではなく、実ユーザー ID を使用する。 \fBcuserid\fP() 関数は 1988 年版の POSIX には含まれていたが、 1990 年版では削除された。 SUSv2 に存在したが、POSIX.1\-2001 で削除された。 .PP OpenBSD には \fBgetlogin\fP() と \fBsetlogin\fP() があり、 セッションに対応したユーザー名がある。制御端末がない セッションの場合であっても、対応するユーザー名がある。 .SH バグ 残念ながら、 \fBgetlogin\fP() をだますのはそれほど難しいことではない。別のプログラムが utmp ファイルを 壊してしまうと、全く動作しないこともある。またログイン名の最初の 8 文字 しか返さないことも多い。また、プログラムの制御端末に現在ログインしているユーザーは、プログラムを実行したユーザーでない場合もある。 セキュリティの絡む用途には \fBgetlogin\fP() を用いるべきではない。 .PP glibc は POSIX 仕様には従っておらず、 \fI/dev/tty\fP ではなく \fI標準入力 (stdin)\fP を使う。これはバグである。 (SunOS 5.8 や HP\-UX 11.11 や FreeBSD 4.8 といった他の最近のシステムはいずれも、 \fI標準入力\fP がリダイレクトされた場合でもログイン名を返す。) .PP \fBcuserid\fP() が何を行っているのか、実際のところを知っている者は誰もいない; 移植性が求められるプログラムでは \fBcuserid\fP() は使うべきではない。 というかどんなプログラムでも使うべきではない: 代わりに \fIgetpwuid(geteuid())\fP を用いるべきである (これが意図していることならば、だが)。 \fBcuserid\fP() は「使わない」こと。 .SH 関連項目 \fBlogname\fP(1), \fBgeteuid\fP(2), \fBgetuid\fP(2), \fButmp\fP(5) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。