xref: /freebsd/lib/libc/gen/gethostname.3 (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1.\" Copyright (c) 1983, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     @(#)gethostname.3	8.1 (Berkeley) 6/4/93
29.\"
30.Dd July 13, 2020
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 LIBRARY
38.Lb libc
39.Sh SYNOPSIS
40.In unistd.h
41.Ft int
42.Fn gethostname "char *name" "size_t namelen"
43.Ft int
44.Fn sethostname "const char *name" "int namelen"
45.Sh DESCRIPTION
46The
47.Fn gethostname
48function
49returns the standard host name for the current processor, as
50previously set by
51.Fn sethostname .
52The
53.Fa namelen
54argument
55specifies the size of the
56.Fa name
57array.
58The returned name is null-terminated unless insufficient space is provided.
59.Pp
60The
61.Fn sethostname
62function
63sets the name of the host machine to be
64.Fa name ,
65which has length
66.Fa namelen .
67This call is restricted to the super-user and
68is normally used only when the system is bootstrapped.
69.Pp
70Applications should use
71.Fn sysconf _SC_HOST_NAME_MAX
72to find the maximum length of a host name (not including the terminating null).
73.Sh RETURN VALUES
74.Rv -std
75.Sh ERRORS
76The following errors may be returned by these calls:
77.Bl -tag -width Er
78.It Bq Er EFAULT
79The
80.Fa name
81or
82.Fa namelen
83argument gave an
84invalid address.
85.It Bq Er ENAMETOOLONG
86The current host name is longer than
87.Fa namelen .
88(For
89.Fn gethostname
90only.)
91.It Bq Er EPERM
92The caller tried to set the host name and was not the super-user.
93.El
94.Sh SEE ALSO
95.Xr sysconf 3 ,
96.Xr sysctl 3
97.Sh STANDARDS
98The
99.Fn gethostname
100function conforms to
101.St -p1003.1-2001 .
102Callers should be aware that
103.Brq Dv HOST_NAME_MAX
104may be variable or infinite, but is guaranteed to be no less than
105.Brq Dv _POSIX_HOST_NAME_MAX .
106On older systems, this limit was defined in the non-standard header
107.In sys/param.h
108as
109.Dv MAXHOSTNAMELEN ,
110and counted the terminating null.
111The
112.Fn sethostname
113function and the error returns for
114.Fn gethostname
115are not standardized.
116.Sh HISTORY
117The
118.Fn gethostname
119function appeared in
120.Bx 4.2 .
121The
122.Fa namelen
123argument to
124.Fn gethostname
125was changed to
126.Vt size_t
127in
128.Fx 5.2
129for alignment with
130.St -p1003.1-2001 .
131