OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man2 / gethostname.2
1 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" Modified 1995-07-22 by Michael Chastain <mec@duracef.shout.net>:
26 .\"   'gethostname' is real system call on Linux/Alpha.
27 .\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
28 .\" Modified 2000-06-04, 2001-12-15 by aeb
29 .\" Modified 2004-06-17 by mtk
30 .\" Modified 2008-11-27 by mtk
31 .\"
32 .TH GETHOSTNAME 2 2014-08-19 "Linux" "Linux Programmer's Manual"
33 .SH NAME
34 gethostname, sethostname \- get/set hostname
35 .SH SYNOPSIS
36 .B #include <unistd.h>
37 .sp
38 .BI "int gethostname(char *" name ", size_t " len );
39 .br
40 .BI "int sethostname(const char *" name ", size_t " len );
41 .sp
42 .in -4n
43 Feature Test Macro Requirements for glibc (see
44 .BR feature_test_macros (7)):
45 .in
46 .sp
47 .ad l
48 .PD 0
49 .BR gethostname ():
50 .RS 4
51 Since glibc 2.12:
52 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
53 .br
54 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200112L
55 .RE
56 .br
57 .BR sethostname ():
58 .RS 4
59 _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE\ <\ 500)
60 .RE
61 .PD
62 .ad
63 .SH DESCRIPTION
64 These system calls are used to access or to change the hostname of the
65 current processor.
66
67 .BR sethostname ()
68 sets the hostname to the value given in the character array
69 .IR name .
70 The
71 .I len
72 argument specifies the number of bytes in
73 .IR name .
74 (Thus,
75 .I name
76 does not require a terminating null byte.)
77
78 .BR gethostname ()
79 returns the null-terminated hostname in the character array
80 .IR name ,
81 which has a length of
82 .I len
83 bytes.
84 If the null-terminated hostname is too large to fit,
85 then the name is truncated, and no error is returned (but see NOTES below).
86 POSIX.1-2001 says that if such truncation occurs,
87 then it is unspecified whether the returned buffer
88 includes a terminating null byte.
89 .SH RETURN VALUE
90 On success, zero is returned.
91 On error, \-1 is returned, and
92 .I errno
93 is set appropriately.
94 .SH ERRORS
95 .TP
96 .B EFAULT
97 .I name
98 is an invalid address.
99 .TP
100 .B EINVAL
101 .I len
102 is negative
103 .\" Can't occur for gethostbyname() wrapper, since 'len' has an
104 .\" unsigned type; can occur for the underlying system call.
105 or, for
106 .BR sethostname (),
107 .I len
108 is larger than the maximum allowed size.
109 .TP
110 .B ENAMETOOLONG
111 .RB "(glibc " gethostname ())
112 .I len
113 is smaller than the actual size.
114 (Before version 2.1, glibc uses
115 .BR EINVAL
116 for this case.)
117 .TP
118 .B EPERM
119 For
120 .BR sethostname (),
121 the caller did not have the
122 .B CAP_SYS_ADMIN
123 capability.
124 .SH CONFORMING TO
125 SVr4, 4.4BSD  (these interfaces first appeared in 4.2BSD).
126 POSIX.1-2001 specifies
127 .BR gethostname ()
128 but not
129 .BR sethostname ().
130 .SH NOTES
131 SUSv2 guarantees that "Host names are limited to 255 bytes".
132 POSIX.1-2001 guarantees that "Host names (not including
133 the terminating null byte) are limited to
134 .B HOST_NAME_MAX
135 bytes".
136 On Linux,
137 .B HOST_NAME_MAX
138 is defined with the value 64, which has been the limit since Linux 1.0
139 (earlier kernels imposed a limit of 8 bytes).
140 .SS C library/kernel ABI differences
141 The GNU C library does not employ the
142 .BR gethostname ()
143 system call; instead, it implements
144 .BR gethostname ()
145 as a library function that calls
146 .BR uname (2)
147 and copies up to
148 .I len
149 bytes from the returned
150 .I nodename
151 field into
152 .IR name .
153 Having performed the copy, the function then checks if the length of the
154 .I nodename
155 was greater than or equal to
156 .IR len ,
157 and if it is, then the function returns \-1 with
158 .I errno
159 set to
160 .BR ENAMETOOLONG ;
161 in this case, a terminating null byte is not included in the returned
162 .IR name .
163
164 Versions of glibc before 2.2
165 .\" At least glibc 2.0 and 2.1, older versions not checked
166 handle the case where the length of the
167 .I nodename
168 was greater than or equal to
169 .I len
170 differently: nothing is copied into
171 .I name
172 and the function returns \-1 with
173 .I errno
174 set to
175 .BR ENAMETOOLONG .
176 .SH SEE ALSO
177 .BR getdomainname (2),
178 .BR setdomainname (2),
179 .BR uname (2)
180 .SH COLOPHON
181 This page is part of release 3.79 of the Linux
182 .I man-pages
183 project.
184 A description of the project,
185 information about reporting bugs,
186 and the latest version of this page,
187 can be found at
188 \%http://www.kernel.org/doc/man\-pages/.