1 | /* Get a host configuration item kept as the whole contents of a file. |
2 | Copyright (C) 1996-2024 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library 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 GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <fcntl.h> |
20 | #include <hurd.h> |
21 | #include <hurd/lookup.h> |
22 | #include "hurdhost.h" |
23 | #include <string.h> |
24 | |
25 | ssize_t |
26 | _hurd_get_host_config (const char *item, char *buf, size_t buflen) |
27 | { |
28 | error_t err; |
29 | char *data; |
30 | mach_msg_type_number_t nread; |
31 | vm_size_t more; |
32 | file_t config; |
33 | |
34 | err = __hurd_file_name_lookup (&_hurd_ports_use, &__getdport, 0, |
35 | item, O_RDONLY, 0, &config); |
36 | switch (err) |
37 | { |
38 | case 0: /* Success; read file contents below. */ |
39 | break; |
40 | |
41 | case ENOENT: /* ? Others? All errors? */ |
42 | /* The file does not exist, so no value has been set. Rather than |
43 | causing gethostname et al to fail with ENOENT, give an empty value |
44 | as other systems do before sethostname has been called. */ |
45 | if (buflen != 0) |
46 | *buf = '\0'; |
47 | return 0; |
48 | |
49 | default: |
50 | return __hurd_fail (err); |
51 | } |
52 | |
53 | data = buf; |
54 | nread = buflen; |
55 | err = __io_read (config, &data, &nread, -1, buflen); |
56 | if (! err) |
57 | /* Check if there is more in the file we didn't read. */ |
58 | err = __io_readable (config, &more); |
59 | __mach_port_deallocate (__mach_task_self (), config); |
60 | if (err) |
61 | return __hurd_fail (err); |
62 | if (data != buf) |
63 | { |
64 | memcpy (buf, data, nread); |
65 | __vm_deallocate (__mach_task_self (), (vm_address_t) data, nread); |
66 | } |
67 | |
68 | /* If the file is empty, give an empty value. */ |
69 | if (nread == 0 && more == 0) |
70 | { |
71 | if (buflen != 0) |
72 | *buf = '\0'; |
73 | return 0; |
74 | } |
75 | |
76 | /* Remove newlines in case someone wrote the file by hand. */ |
77 | while (nread > 0 && buf[nread - 1] == '\n') |
78 | buf[--nread] = '\0'; |
79 | |
80 | /* Null-terminate the result if there is enough space. */ |
81 | if (nread < buflen) |
82 | buf[nread] = '\0'; |
83 | else |
84 | if (nread != 0 && buf[nread - 1] != '\0') |
85 | more = 1; |
86 | |
87 | if (more) |
88 | /* If we didn't read the whole file, tell the caller to use a bigger |
89 | buffer next time. */ |
90 | return __hurd_fail (ENAMETOOLONG); |
91 | |
92 | return nread; |
93 | } |
94 | |