gethostname(2) System Calls Manual gethostname(2)

gethostname, sethostname - pobiera/ustawia nazwę stacji

Standardowa biblioteka C (libc-lc)

#include <unistd.h>
int gethostname(char *name, size_t size);
int sethostname(const char *name, size_t size);
Wymagane ustawienia makr biblioteki glibc (patrz feature_test_macros(7)):

gethostname():

    _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L
        || /* glibc 2.19 i wcześniejsze */ _BSD_SOURCE

sethostname():

    Od glibc 2.21:
        _DEFAULT_SOURCE
    W glibc 2.19 i 2.20:
        _DEFAULT_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
    Do glibc 2.19 włącznie:
        _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)

Wywołania systemowe służą do uzyskania dostępu lub zmiany systemowej nazwy komputera. Mówiąc precyzyjniej, działają na nazwie komputera, związanej z przestrzenią nazw UTS procesu wywołującego.

sethostname() sets the hostname to the value given in the character array name. The size 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 size of size bytes. If the null-terminated hostname is too large to fit, then the name is truncated, and no error is returned (but see VERSIONS below). POSIX.1 says that if such truncation occurs, then it is unspecified whether the returned buffer includes a terminating null byte.

Po pomyślnym zakończeniu zwracane jest zero. Po błędzie zwracane jest -1 i ustawiane errno, wskazując błąd.

name jest nieprawidłowym adresem.
size is negative or, for sethostname(), size is larger than the maximum allowed size.
(glibc gethostname()) size is smaller than the actual size. (Before glibc 2.1, glibc uses EINVAL for this case.)
W przypadku sethostname(), wywołujący nie posiadał przywileju (ang. capability) CAP_SYS_ADMIN w przestrzeni nazw użytkownika związanej z jego przestrzenią nazw UTS (zob. namespaces(7)).

SUSv2 gwarantuje, że „nazwy stacji są ograniczone do 255 bajtów”. POSIX 1003.1-2001 gwarantuje, że „nazwy stacji (bez kończącego NUL) są ograniczone do HOST_NAME_MAX bajtów”. W Linuksie HOST_NAME_MAX jest zdefiniowane z wartością 64, co stanowiło limit od Linuksa 1.0 (wcześniejsze jądra narzucały limit 8 bajtów).

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 size 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 size, and if it is, then the function returns -1 with errno set to ENAMETOOLONG; in this case, a terminating null byte is not included in the returned name.

POSIX.1-2008.
Brak.

SVr4, 4.4BSD (interfejsy te pierwotnie pojawiły się w 4.2BSD). POSIX.1-2001 i POSIX.1-2008 definiuje gethostname() ale nie sethostname().

Versions of glibc before glibc 2.2 handle the case where the length of the nodename was greater than or equal to size differently: nothing is copied into name and the function returns -1 with errno set to ENAMETOOLONG.

hostname(1), getdomainname(2), setdomainname(2), uname(2), uts_namespaces(7)

Autorami polskiego tłumaczenia niniejszej strony podręcznika są: Przemek Borys <pborys@dione.ids.pl>, Andrzej Krzysztofowicz <ankry@green.mf.pg.gda.pl> i Michał Kułach <michal.kulach@gmail.com>

Niniejsze tłumaczenie jest wolną dokumentacją. Bliższe informacje o warunkach licencji można uzyskać zapoznając się z GNU General Public License w wersji 3 lub nowszej. Nie przyjmuje się ŻADNEJ ODPOWIEDZIALNOŚCI.

Błędy w tłumaczeniu strony podręcznika prosimy zgłaszać na adres listy dyskusyjnej manpages-pl-list@lists.sourceforge.net.

17 listopada 2024 r. Linux man-pages 6.12