.\" Hey Emacs! This file is -*- nroff -*- source. .\" .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu) .\" .\" 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 .\" 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. .\" .\" 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 .\" .\" 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 2008-11-27 "Linux" "Linux Programmer's Manual" .SH 名前 gethostname, sethostname \- ホスト名の取得・設定をする .SH 書式 .B #include .sp .BI "int gethostname(char *" name ", size_t " len ); .br .BI "int sethostname(const char *" name ", size_t " len ); .sp .in -4n glibc 向けの機能検査マクロの要件 .RB ( feature_test_macros (7) 参照): .in .sp .ad l .BR gethostname (): _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 .br .BR sethostname (): _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE\ <\ 500) .ad b .SH 説明 これらのシステムコールは、現在のプロセッサのホスト名を取得・変更するために 使用される。 .BR sethostname () は、ホスト名を、文字配列 .I name で指定された値に設定する。 引き数 .I len には、 .I name のバイト数を指定する (そのため、 .I name では文字列終端の NULL バイトは必要ない)。 .BR gethostname () は、NULL 終端されたホスト名を、 .I len バイトの長さの文字配列 .I name に格納して返す。 NULL 終端されたホスト名が格納先のバッファよりも長い場合は、 ホスト名は切り詰められ、エラーは返されない (下記の「注意」の節を参照)。 POSIX.1-2001 では、結果の切り詰めが発生した場合に、 返されたバッファに終端の NULL バイトが含まれているかどうかは 規定されていない。 .SH 返り値 成功した場合 0 が返る。失敗した場合 \-1 が返り、 .I errno がエラーの内容に従って設定される。 .SH エラー .TP .B EFAULT .I name が不正なアドレスである。 .TP .B EINVAL .I len が負である。 .\" gethostbyname() ラッパー関数では、'len' は unsigned 型なので .\" このエラーが発生することはない。 .\" その中で呼ばれるシステムコールでは発生することがある。 .BR sethostname () において .I len が許容された最大サイズを越えている。 .TP .B ENAMETOOLONG .RB "(glibc " gethostname () で) .I len が実際のホスト名の長さよりも小さい (glibc バージョン 2.1 より前では、この状況で .B EINVAL が使用される)。 .TP .B EPERM .BR sethostname () において、呼び出した人が .B CAP_SYS_ADMIN ケーパビリティ (capability) を持っていなかった。 .SH 準拠 SVr4, 4.4BSD (これらのインタフェースは 4.2BSD で初めて登場した)。 POSIX.1-2001 では .BR gethostname () については規定しているが、 .BR sethostname () は規定していない。 .SH 注意 SUSv2 では「ホスト名が 255 バイトに制限される」ことを保証している。 POSIX.1-2001 では「ホスト名 (終端の NULL バイトは含まない) が .B HOST_NAME_MAX バイトに制限される」ことを保証している。 Linux では、 .B HOST_NAME_MAX は 64 に定義されており、 Linux 1.0 以降ではこれが上限となってきた (もっと古いカーネルでは 8 バイトの上限が適用されていた)。 .SS "glibc での注意" GNU C ライブラリは、 .BR gethostname () システムコールを利用していない。その代わり、 .BR gethostname () をライブラリ関数として実装しており、 この関数は .BR uname (2) を呼び出し、 .BR uname (2) が返した .I nodename フィールド (の最大 .I len バイト) を .I name にコピーする。 コピーを行った際に、この関数は .I nodename の長さが .I len 以上かの確認を行い、 .I len 以上の場合には \-1 を返し、 .I errno に .B ENAMETOOLONG を設定する。 この場合、返された .I name には終端の NULL バイトは含まれない。 バージョン 2.2 より前の glibc .\" 少なくとも glibc 2.0 と 2.1。それより古いバージョンは未確認 では、 .I nodename の長さが .I len 以上の場合の扱いが異なる; .I len 以上の場合には、 .I name には何もコピーせず、関数は \-1 を返し、 .I errno に .B ENAMETOOLONG を設定する。 .SH 関連項目 .BR getdomainname (2), .BR setdomainname (2), .BR uname (2)