| 1 |
/* sethostname emulation for glibc compliance. |
| 2 |
|
| 3 |
Copyright (C) 2011-2026 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is free software: you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU Lesser General Public License as |
| 7 |
published by the Free Software Foundation; either version 2.1 of the |
| 8 |
License, or (at your option) any later version. |
| 9 |
|
| 10 |
This file is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public License |
| 16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
/* Ben Walton <[email protected]> */ |
| 19 |
|
| 20 |
#include <config.h> |
| 21 |
|
| 22 |
#if !(defined _WIN32 || defined __CYGWIN__) |
| 23 |
/* Unix API. */ |
| 24 |
|
| 25 |
/* Specification. */ |
| 26 |
# include <unistd.h> |
| 27 |
|
| 28 |
# include <errno.h> |
| 29 |
# include <stdio.h> |
| 30 |
# include <limits.h> |
| 31 |
|
| 32 |
/* Set up to LEN chars of NAME as system hostname. |
| 33 |
Return 0 if ok, set errno and return -1 on error. */ |
| 34 |
|
| 35 |
int |
| 36 |
sethostname (const char *name, size_t len) |
| 37 |
{ |
| 38 |
/* Ensure the string isn't too long. glibc does allow setting an |
| 39 |
empty hostname so no point in enforcing a lower bound. */ |
| 40 |
if (len > HOST_NAME_MAX) |
| 41 |
{ |
| 42 |
errno = EINVAL; |
| 43 |
return -1; |
| 44 |
} |
| 45 |
|
| 46 |
# ifdef __minix /* Minix */ |
| 47 |
{ |
| 48 |
/* glibc returns EFAULT, EINVAL, and EPERM on error. None of |
| 49 |
these are appropriate for us to set, even if they may match the |
| 50 |
situation, during failed open/write/close operations, so we |
| 51 |
leave errno alone and rely on what the system sets up. */ |
| 52 |
FILE *hostf = fopen ("/etc/hostname.file", "we"); |
| 53 |
int r = 0; |
| 54 |
if (hostf == NULL) |
| 55 |
r = -1; |
| 56 |
else |
| 57 |
{ |
| 58 |
fprintf (hostf, "%.*s\n", (int) len, name); |
| 59 |
if (ferror (hostf)) |
| 60 |
{ |
| 61 |
/* Close hostf, preserving the errno from the fprintf call. */ |
| 62 |
int saved_errno = errno; |
| 63 |
fclose (hostf); |
| 64 |
errno = saved_errno; |
| 65 |
r = -1; |
| 66 |
} |
| 67 |
else |
| 68 |
{ |
| 69 |
if (fclose (hostf)) |
| 70 |
/* fclose sets errno on failure. */ |
| 71 |
r = -1; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return r; |
| 76 |
} |
| 77 |
# else |
| 78 |
/* For platforms that we don't have a better option for, simply bail |
| 79 |
out. */ |
| 80 |
errno = ENOSYS; |
| 81 |
return -1; |
| 82 |
# endif |
| 83 |
} |
| 84 |
|
| 85 |
#else |
| 86 |
/* Native Windows API. Also used on Cygwin. */ |
| 87 |
|
| 88 |
/* Ensure that <windows.h> declares SetComputerNameEx. */ |
| 89 |
# if !defined _WIN32_WINNT || (_WIN32_WINNT < _WIN32_WINNT_WIN2K) |
| 90 |
# undef _WIN32_WINNT |
| 91 |
# define _WIN32_WINNT _WIN32_WINNT_WIN2K |
| 92 |
# endif |
| 93 |
|
| 94 |
# define WIN32_LEAN_AND_MEAN |
| 95 |
|
| 96 |
/* Specification. */ |
| 97 |
# include <unistd.h> |
| 98 |
|
| 99 |
# include <errno.h> |
| 100 |
# include <limits.h> |
| 101 |
# include <string.h> |
| 102 |
|
| 103 |
# include <windows.h> |
| 104 |
|
| 105 |
/* Don't assume that UNICODE is not defined. */ |
| 106 |
# undef GetComputerNameEx |
| 107 |
# define GetComputerNameEx GetComputerNameExA |
| 108 |
# undef SetComputerNameEx |
| 109 |
# define SetComputerNameEx SetComputerNameExA |
| 110 |
|
| 111 |
/* Set up to LEN chars of NAME as system hostname. |
| 112 |
Return 0 if ok, set errno and return -1 on error. */ |
| 113 |
|
| 114 |
int |
| 115 |
sethostname (const char *name, size_t len) |
| 116 |
{ |
| 117 |
/* Ensure the string isn't too long. glibc does allow setting an |
| 118 |
empty hostname so no point in enforcing a lower bound. */ |
| 119 |
if (len > HOST_NAME_MAX) |
| 120 |
{ |
| 121 |
errno = EINVAL; |
| 122 |
return -1; |
| 123 |
} |
| 124 |
|
| 125 |
/* Prepare a NUL-terminated copy of name. */ |
| 126 |
char name_asciz[HOST_NAME_MAX + 1]; |
| 127 |
memcpy (name_asciz, name, len); |
| 128 |
name_asciz[len] = '\0'; |
| 129 |
|
| 130 |
/* Save the old NetBIOS name. */ |
| 131 |
char old_name[HOST_NAME_MAX + 1]; |
| 132 |
DWORD old_name_len = sizeof (old_name) - 1; |
| 133 |
if (! GetComputerNameEx (ComputerNamePhysicalNetBIOS, |
| 134 |
old_name, &old_name_len)) |
| 135 |
old_name_len = 0; |
| 136 |
|
| 137 |
/* Set both the NetBIOS and the first part of the IP / DNS name. */ |
| 138 |
if (! SetComputerNameEx (ComputerNamePhysicalNetBIOS, name_asciz)) |
| 139 |
{ |
| 140 |
errno = (GetLastError () == ERROR_ACCESS_DENIED ? EPERM : EINVAL); |
| 141 |
return -1; |
| 142 |
} |
| 143 |
if (! SetComputerNameEx (ComputerNamePhysicalDnsHostname, name_asciz)) |
| 144 |
{ |
| 145 |
errno = (GetLastError () == ERROR_ACCESS_DENIED ? EPERM : EINVAL); |
| 146 |
/* Restore the old NetBIOS name. */ |
| 147 |
if (old_name_len > 0) |
| 148 |
{ |
| 149 |
old_name[old_name_len] = '\0'; |
| 150 |
SetComputerNameEx (ComputerNamePhysicalNetBIOS, old_name); |
| 151 |
} |
| 152 |
return -1; |
| 153 |
} |
| 154 |
|
| 155 |
/* Note that the new host name becomes effective only after a reboot! */ |
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
#endif |