|
Packit |
6c4009 |
/* Get a host configuration item kept as the whole contents of a file.
|
|
Packit |
6c4009 |
Copyright (C) 1996-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <fcntl.h>
|
|
Packit |
6c4009 |
#include <hurd.h>
|
|
Packit |
6c4009 |
#include <hurd/lookup.h>
|
|
Packit |
6c4009 |
#include "hurdhost.h"
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ssize_t
|
|
Packit |
6c4009 |
_hurd_get_host_config (const char *item, char *buf, size_t buflen)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
error_t err;
|
|
Packit |
6c4009 |
char *data;
|
|
Packit |
6c4009 |
mach_msg_type_number_t nread, more;
|
|
Packit |
6c4009 |
file_t config;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
err = __hurd_file_name_lookup (&_hurd_ports_use, &__getdport, 0,
|
|
Packit |
6c4009 |
item, O_RDONLY, 0, &config);
|
|
Packit |
6c4009 |
switch (err)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
case 0: /* Success; read file contents below. */
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
case ENOENT: /* ? Others? All errors? */
|
|
Packit |
6c4009 |
/* The file does not exist, so no value has been set. Rather than
|
|
Packit |
6c4009 |
causing gethostname et al to fail with ENOENT, give an empty value
|
|
Packit |
6c4009 |
as other systems do before sethostname has been called. */
|
|
Packit |
6c4009 |
if (buflen != 0)
|
|
Packit |
6c4009 |
*buf = '\0';
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
default:
|
|
Packit |
6c4009 |
return __hurd_fail (err);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
data = buf;
|
|
Packit |
6c4009 |
nread = buflen;
|
|
Packit |
6c4009 |
err = __io_read (config, &data, &nread, -1, buflen);
|
|
Packit |
6c4009 |
if (! err)
|
|
Packit |
6c4009 |
/* Check if there is more in the file we didn't read. */
|
|
Packit |
6c4009 |
err = __io_readable (config, &more);
|
|
Packit |
6c4009 |
__mach_port_deallocate (__mach_task_self (), config);
|
|
Packit |
6c4009 |
if (err)
|
|
Packit |
6c4009 |
return __hurd_fail (err);
|
|
Packit |
6c4009 |
if (data != buf)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
memcpy (buf, data, nread);
|
|
Packit |
6c4009 |
__vm_deallocate (__mach_task_self (), (vm_address_t) data, nread);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* If the file is empty, give an empty value. */
|
|
Packit |
6c4009 |
if (nread == 0 && more == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (buflen != 0)
|
|
Packit |
6c4009 |
*buf = '\0';
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Remove newlines in case someone wrote the file by hand. */
|
|
Packit |
6c4009 |
while (nread > 0 && buf[nread - 1] == '\n')
|
|
Packit |
6c4009 |
buf[--nread] = '\0';
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Null-terminate the result if there is enough space. */
|
|
Packit |
6c4009 |
if (nread < buflen)
|
|
Packit |
6c4009 |
buf[nread] = '\0';
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
if (nread != 0 && buf[nread - 1] != '\0')
|
|
Packit |
6c4009 |
more = 1;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (more)
|
|
Packit |
6c4009 |
/* If we didn't read the whole file, tell the caller to use a bigger
|
|
Packit |
6c4009 |
buffer next time. */
|
|
Packit |
6c4009 |
return __hurd_fail (ENAMETOOLONG);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return nread;
|
|
Packit |
6c4009 |
}
|