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