gethostnameのヘルプ・マニュアル
日本語 英語
gethostname --help
man gethostname
GETHOSTNAME(2) Linux Programmer’s Manual GETHOSTNAME(2)
名前
gethostname, sethostname - ホスト名の取得・設定をする
書式
#include
int gethostname(char *name, size_t len);
int sethostname(const char *name, size_t len);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
gethostname(): _BSD_SOURCE || _XOPEN_SOURCE >= 500
sethostname(): _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
説明
こ れらのシステムコールは、現在のプロセッサのホスト名を取得・変更するた
めに使用される。
sethostname() は、ホスト名を、文字配列 name で指定された値に設定する 。
引き数 len には、 name のバイト数を指定する (そのため、 name では文字列
終端の NULL バイトは必要ない)。
gethostname() は、NULL 終端されたホスト名を、 len バイトの長さの文字 配
列 name に格納して返す。 NULL 終端されたホスト名が格納先のバッファより
も長い場合は、ホスト名は切り詰められ、エラーは返されない (下記の「注 意
」の節を参照)。 POSIX.1-2001 では、結果の切り詰めが発生した場合に、返さ
れたバッファに終端の NULL バイトが含まれているかどうかは規定されてい な
い。
返り値
成 功した場合 0 が返る。失敗した場合 -1 が返り、 errno がエラーの内容に
従って設定される。
エラー
EFAULT name が不正なアドレスである。
EINVAL len が負である。 sethostname() において len が許容された最大サイ
ズを越えている。
ENAMETOOLONG
(glibc gethostname() で) len が実際のホスト名の長さよりも小さい
(glibc バージョン 2.1 より前では、この状況で EINVAL が使用 さ れ
る)。
EPERM sethostname() において、呼び出した人が CAP_SYS_ADMIN ケーパビリ
ティ (capability) を持っていなかった。
準拠
SVr4, 4.4BSD (これらのインタフェースは 4.2BSD で初めて 登 場 し た) 。
POSIX.1-2001 で は gethostname() については規定しているが、 sethost-
name() は規定していない。
注意
SUSv2 では「ホスト名が 255 バイトに制限される」ことを保証し て い る 。
POSIX.1-2001 で は 「 ホ ス ト 名 ( 終端の NULL バイトは含まない) が
HOST_NAME_MAX バイトに制限される」ことを保証している。 Linux で は 、
HOST_NAME_MAX は 64 に定義されており、 Linux 1.0 以降ではこれが上限とな
ってきた (もっと古いカーネルでは 8 バイトの上限が適用されていた)。
glibc での注意
GNU C ライブラリは、 gethostname() システムコールを利用していない。その
代 わ り、 gethostname() をライブラリ関数として実装しており、この関数は
uname(2) を呼び出し、 uname(2) が返した nodename フィールド (の最大 len
バ イ ト) を name にコピーする。コピーを行った際に、この関数は nodename
の長さが len 以上かの確認を行い、 len 以上の場合には -1 を返し、 errno
に ENAMETOOLONG を設定する。この場合、返された name には終端の NULL バ
イトは含まれない。
バージョン 2.2 より前の glibc では、 nodename の長さが len 以上の場合の
扱 い が異なる; len 以上の場合には、 name には何もコピーせず、関数は -1
を返し、 errno に ENAMETOOLONG を設定する。
関連項目
getdomainname(2), setdomainname(2), uname(2)
Linux 2008-11-27 GETHOSTNAME(2)
GETHOSTNAME(2) Linux Programmer’s Manual GETHOSTNAME(2)
NAME
gethostname, sethostname - get/set hostname
SYNOPSIS
#include
int gethostname(char *name, size_t len);
int sethostname(const char *name, size_t len);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
gethostname(): _BSD_SOURCE || _XOPEN_SOURCE >= 500
sethostname(): _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
DESCRIPTION
These system calls are used to access or to change the hostname of the
current processor.
sethostname() sets the hostname to the value given in the character
array name. The len argument specifies the number of bytes in name.
(Thus, name does not require a terminating null byte.)
gethostname() returns the null-terminated hostname in the character
array name, which has a length of len bytes. If the null-terminated
hostname is too large to fit, then the name is truncated, and no error
is returned (but see NOTES below). POSIX.1-2001 says that if such
truncation occurs, then it is unspecified whether the returned buffer
includes a terminating null byte.
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is
set appropriately.
ERRORS
EFAULT name is an invalid address.
EINVAL len is negative or, for sethostname(), len is larger than the
maximum allowed size.
ENAMETOOLONG
(glibc gethostname()) len is smaller than the actual size.
(Before version 2.1, glibc uses EINVAL for this case.)
EPERM For sethostname(), the caller did not have the CAP_SYS_ADMIN
capability.
CONFORMING TO
SVr4, 4.4BSD (these interfaces first appeared in 4.2BSD).
POSIX.1-2001 specifies gethostname() but not sethostname().
NOTES
SUSv2 guarantees that "Host names are limited to 255 bytes".
POSIX.1-2001 guarantees that "Host names (not including the terminating
null byte) are limited to HOST_NAME_MAX bytes". On Linux,
HOST_NAME_MAX is defined with the value 64, which has been the limit
since Linux 1.0 (earlier kernels imposed a limit of 8 bytes).
Glibc Notes
The GNU C library does not employ the gethostname() system call;
instead, it implements gethostname() as a library function that calls
uname(2) and copies up to len bytes from the returned nodename field
into name. Having performed the copy, the function then checks if the
length of the nodename was greater than or equal to len, and if it is,
then the function returns -1 with errno set to ENAMETOOLONG; in this
case, no null-terminator is included in the returned name.
Versions of glibc before 2.2 handle the case where the length of the
nodename was greater than or equal to len differently: nothing is
copied into name and the function returns -1 with errno set to ENAME-
TOOLONG.
SEE ALSO
getdomainname(2), setdomainname(2), uname(2)
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/.
Linux 2008-11-27 GETHOSTNAME(2)