1.\"	$OpenBSD: gethostname.3,v 1.28 2018/01/12 04:36:44 deraadt Exp $
2.\"
3.\" Copyright (c) 1983, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.Dd $Mdocdate: January 12 2018 $
31.Dt GETHOSTNAME 3
32.Os
33.Sh NAME
34.Nm gethostname ,
35.Nm sethostname
36.Nd get/set name of current host
37.Sh SYNOPSIS
38.In unistd.h
39.Ft int
40.Fn gethostname "char *name" "size_t namelen"
41.Ft int
42.Fn sethostname "const char *name" "size_t namelen"
43.Sh DESCRIPTION
44The
45.Fn gethostname
46function returns the standard host name for the current
47machine, as previously set by
48.Fn sethostname .
49The parameter
50.Fa namelen
51specifies the size of the
52.Fa name
53array.
54If insufficient space is provided, the returned name is truncated.
55The returned name is always NUL terminated.
56If no space is provided, an error is returned.
57.Pp
58.Fn sethostname
59sets the name of the host machine to be
60.Fa name ,
61which has length
62.Fa namelen .
63This call is restricted to the superuser and
64is normally used only when the system is bootstrapped.
65.Sh RETURN VALUES
66If the call succeeds a value of 0 is returned.
67If the call fails, a value of \-1 is returned and an error code is
68placed in the global variable
69.Va errno .
70.Sh ERRORS
71The following errors may be returned by these calls:
72.Bl -tag -width Er
73.It Bq Er EFAULT
74The
75.Fa name
76parameter gave an invalid address.
77.It Bq Er ENOMEM
78The
79.Ar namelen
80parameter was zero.
81.It Bq Er EPERM
82The caller tried to set the hostname and was not the superuser.
83.El
84.Sh SEE ALSO
85.Xr hostname 1 ,
86.Xr getdomainname 3 ,
87.Xr gethostid 3 ,
88.Xr sysctl 2 ,
89.Xr sysctl 8 ,
90.Xr yp 8
91.Sh STANDARDS
92The
93.Fn gethostname
94function call conforms to
95.St -xpg4.2 .
96.Sh HISTORY
97The
98.Fn gethostname
99function call appeared in
100.Bx 4.2 .
101.Sh BUGS
102Host names are limited to
103.Dv MAXHOSTNAMELEN
104(from
105.In sys/param.h )
106characters, currently 256.
107This includes the terminating NUL character.
108Note that the corresponding POSIX definition
109.Dv HOST_NAME_MAX
110in
111.In limits.h
112does
113.Em not
114include the terminating NUL character.
115.Pp
116If the buffer passed to
117.Fn gethostname
118is smaller than
119.Dv MAXHOSTNAMELEN ,
120other operating systems may not guarantee termination with NUL.
121