.\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu) .\" .\" %%%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 .\" .\" Modified 1995-07-22 by Michael Chastain : .\" 'gethostname' is real system call on Linux/Alpha. .\" Modified 1997-01-31 by Eric S. Raymond .\" Modified 2000-06-04, 2001-12-15 by aeb .\" Modified 2004-06-17 by mtk .\" Modified 2008-11-27 by mtk .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1997 SUTO, Mitsuaki .\" all rights reserved. .\" Translated 1997-06-27, SUTO, Mitsuaki .\" Updated 2000-09-30, Yuichi SATO .\" Updated & Modified 2002-01-14, Yuichi SATO .\" Updated & Modified 2004-12-30, Yuichi SATO .\" Updated & Modified 2006-01-31, Akihiro MOTOKI .\" Updated 2008-12-26, Akihiro MOTOKI, LDP v3.15 .\" .TH GETHOSTNAME 2 " 2019\-10\-10" Linux "Linux Programmer's Manual" .SH 名前 gethostname, sethostname \- ホスト名の取得・設定をする .SH 書式 \fB#include \fP .PP \fBint gethostname(char *\fP\fIname\fP\fB, size_t \fP\fIlen\fP\fB);\fP .br \fBint sethostname(const char *\fP\fIname\fP\fB, size_t \fP\fIlen\fP\fB);\fP .PP .RS -4 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照): .RE .PP .ad l .PD 0 \fBgethostname\fP(): .RS 4 glibc 2.12 以降: _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 .br || /* glibc 2.12 以降: */ _POSIX_C_SOURCE\ >=\ 200112L .RE .br \fBsethostname\fP(): .nf .\" commit 266865c0e7b79d4196e2cc393693463f03c90bd8 glibc 2.21 以降: _DEFAULT_SOURCE glibc 2.19 と 2.20: _DEFAULT_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE\ <\ 500) glibc 2.19 以前: _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE\ <\ 500) .fi .PD .ad .SH 説明 These system calls are used to access or to change the system hostname. More precisely, they operate on the hostname associated with the calling process's UTS namespace. .PP \fBsethostname\fP() は、ホスト名を、文字配列 \fIname\fP で指定された値に設定する。 引数 \fIlen\fP には、 \fIname\fP のバイト数を指定する (そのため、 \fIname\fP では文字列終端のヌルバイトは必要ない)。 .PP \fBgethostname\fP() は、NULL 終端されたホスト名を、 \fIlen\fP バイトの長さの文字配列 \fIname\fP に格納して返す。ヌル終端されたホスト名が格納先のバッファーよりも長い場合は、 ホスト名は切り詰められ、エラーは返されない (下記の「注意」の節を参照)。 POSIX.1 では、結果の切り詰めが発生した場合に、 返されたバッファーに終端のヌルバイトが含まれているかどうかは 規定されていない。 .SH 返り値 成功した場合は 0 が返される。エラーの場合は \-1 が返され、 \fIerrno\fP が適切に設定される。 .SH エラー .TP \fBEFAULT\fP \fIname\fP が不正なアドレスである。 .TP \fBEINVAL\fP .\" Can't occur for gethostbyname() wrapper, since 'len' has an .\" unsigned type; can occur for the underlying system call. \fIlen\fP が負である。 \fBsethostname\fP() において \fIlen\fP が許容された最大サイズを越えている。 .TP \fBENAMETOOLONG\fP (glibc \fBgethostname\fP() で) \fIlen\fP が実際のホスト名の長さよりも小さい (glibc バージョン 2.1 より前では、この状況で \fBEINVAL\fP が使用される)。 .TP \fBEPERM\fP For \fBsethostname\fP(), the caller did not have the \fBCAP_SYS_ADMIN\fP capability in the user namespace associated with its UTS namespace (see \fBnamespaces\fP(7)). .SH 準拠 SVr4, 4.4BSD (これらのインターフェースは 4.2BSD で初めて登場した)。 POSIX.1\-2001 と POSIX.1\-2008 では \fBgethostname\fP() については規定しているが、 \fBsethostname\fP() は規定していない。 .SH 注意 SUSv2 では「ホスト名が 255 バイトに制限される」ことを保証している。 POSIX.1 では「ホスト名 (終端のヌルバイトは含まない) が \fBHOST_NAME_MAX\fP バイトに制限される」ことを保証している。 Linux では、 \fBHOST_NAME_MAX\fP は 64 に定義されており、 Linux 1.0 以降ではこれが上限となってきた (もっと古いカーネルでは 8 バイトの上限が適用されていた)。 .SS "C ライブラリとカーネルの違い" GNU C ライブラリは、 \fBgethostname\fP() システムコールを利用していない。その代わり、 \fBgethostname\fP() をライブラリ関数として実装しており、 この関数は \fBuname\fP(2) を呼び出し、 \fBuname\fP(2) が返した \fInodename\fP フィールド (の最大 \fIlen\fP バイト) を \fIname\fP にコピーする。 コピーを行った際に、この関数は \fInodename\fP の長さが \fIlen\fP 以上かの確認を行い、 \fIlen\fP 以上の場合には \-1 を返し、 \fIerrno\fP に \fBENAMETOOLONG\fP を設定する。 この場合、返された \fIname\fP には終端のヌルバイトは含まれない。 .PP .\" At least glibc 2.0 and 2.1, older versions not checked バージョン 2.2 より前の glibc では、 \fInodename\fP の長さが \fIlen\fP 以上の場合の扱いが異なる; \fIlen\fP 以上の場合には、 \fIname\fP には何もコピーせず、関数は \-1 を返し、 \fIerrno\fP に \fBENAMETOOLONG\fP を設定する。 .SH 関連項目 \fBhostname\fP(1), \fBgetdomainname\fP(2), \fBsetdomainname\fP(2), \fBuname\fP(2), \fButs_namespaces\fP(7) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。