(bind): Ensure NAME for the AF_LOCAL case is '\0'-terminated.
[glibc.git] / hurd / hurdlookup.c
blob05ed03833c646a1da68f4e2af371636c1bf35f7f
1 /* Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <hurd.h>
20 #include <hurd/lookup.h>
21 #include <string.h>
22 #include <limits.h>
23 #include <fcntl.h>
24 #include "stdio-common/_itoa.h"
25 #include <hurd/term.h>
28 /* Translate the error from dir_lookup into the error the user sees. */
29 static inline error_t
30 lookup_error (error_t error)
32 switch (error)
34 case EOPNOTSUPP:
35 case MIG_BAD_ID:
36 /* These indicate that the server does not understand dir_lookup
37 at all. If it were a directory, it would, by definition. */
38 return ENOTDIR;
39 default:
40 return error;
44 error_t
45 __hurd_file_name_lookup (error_t (*use_init_port)
46 (int which, error_t (*operate) (file_t)),
47 file_t (*get_dtable_port) (int fd),
48 const char *file_name, int flags, mode_t mode,
49 file_t *result)
51 error_t err;
52 enum retry_type doretry;
53 char retryname[1024]; /* XXX string_t LOSES! */
55 error_t lookup (mach_port_t startdir)
57 while (file_name[0] == '/')
58 file_name++;
60 return lookup_error (__dir_lookup (startdir, file_name, flags, mode,
61 &doretry, retryname, result));
64 err = (*use_init_port) (file_name[0] == '/'
65 ? INIT_PORT_CRDIR : INIT_PORT_CWDIR,
66 &lookup);
67 if (! err)
68 err = __hurd_file_name_lookup_retry (use_init_port, get_dtable_port,
69 doretry, retryname, flags, mode,
70 result);
72 return err;
74 weak_alias (__hurd_file_name_lookup, hurd_file_name_lookup)
76 error_t
77 __hurd_file_name_lookup_retry (error_t (*use_init_port)
78 (int which, error_t (*operate) (file_t)),
79 file_t (*get_dtable_port) (int fd),
80 enum retry_type doretry,
81 char retryname[1024],
82 int flags, mode_t mode,
83 file_t *result)
85 error_t err;
86 char *file_name;
87 int nloops;
89 error_t lookup (file_t startdir)
91 while (file_name[0] == '/')
92 file_name++;
94 return lookup_error (__dir_lookup (startdir, file_name, flags, mode,
95 &doretry, retryname, result));
97 error_t reauthenticate (file_t unauth)
99 error_t err;
100 mach_port_t ref = __mach_reply_port ();
101 error_t reauth (auth_t auth)
103 return __auth_user_authenticate (auth, unauth, ref,
104 MACH_MSG_TYPE_MAKE_SEND,
105 result);
107 err = __io_reauthenticate (unauth, ref, MACH_MSG_TYPE_MAKE_SEND);
108 if (! err)
109 err = (*use_init_port) (INIT_PORT_AUTH, &reauth);
110 __mach_port_destroy (__mach_task_self (), ref);
111 __mach_port_deallocate (__mach_task_self (), unauth);
112 return err;
115 nloops = 0;
116 err = 0;
119 file_t startdir = MACH_PORT_NULL;
120 int dirport = INIT_PORT_CWDIR;
122 switch (doretry)
124 case FS_RETRY_REAUTH:
125 if (err = reauthenticate (*result))
126 return err;
127 /* Fall through. */
129 case FS_RETRY_NORMAL:
130 #ifdef SYMLOOP_MAX
131 if (nloops++ >= SYMLOOP_MAX)
132 return ELOOP;
133 #endif
135 /* An empty RETRYNAME indicates we have the final port. */
136 if (retryname[0] == '\0')
138 /* We got a successful translation. Now apply any open-time
139 action flags we were passed. */
140 if (flags & O_EXLOCK)
141 ; /* XXX */
142 if (!err && (flags & O_SHLOCK))
143 ; /* XXX */
144 if (!err && (flags & O_TRUNC))
145 err = __file_set_size (*result, 0);
147 if (err)
148 __mach_port_deallocate (__mach_task_self (), *result);
149 return err;
152 startdir = *result;
153 file_name = retryname;
154 break;
156 case FS_RETRY_MAGICAL:
157 switch (retryname[0])
159 case '/':
160 dirport = INIT_PORT_CRDIR;
161 if (*result != MACH_PORT_NULL)
162 __mach_port_deallocate (__mach_task_self (), *result);
163 file_name = &retryname[1];
164 break;
166 case 'f':
167 if (retryname[1] == 'd' && retryname[2] == '/')
169 int fd;
170 char *end;
171 int save = errno;
172 errno = 0;
173 fd = (int) strtol (&retryname[3], &end, 10);
174 if (end == NULL || errno || /* Malformed number. */
175 /* Check for excess text after the number. A slash
176 is valid; it ends the component. Anything else
177 does not name a numeric file descriptor. */
178 (*end != '/' && *end != '\0'))
180 errno = save;
181 return ENOENT;
183 if (! get_dtable_port)
184 err = EGRATUITOUS;
185 else
187 *result = (*get_dtable_port) (fd);
188 if (*result == MACH_PORT_NULL)
190 /* If the name was a proper number, but the file
191 descriptor does not exist, we return EBADF instead
192 of ENOENT. */
193 err = errno;
194 errno = save;
197 errno = save;
198 if (err)
199 return err;
200 if (*end == '\0')
201 return 0;
202 else
204 /* Do a normal retry on the remaining components. */
205 startdir = *result;
206 file_name = end + 1; /* Skip the slash. */
207 break;
210 else
211 goto bad_magic;
212 break;
214 case 'm':
215 if (retryname[1] == 'a' && retryname[2] == 'c' &&
216 retryname[3] == 'h' && retryname[4] == 't' &&
217 retryname[5] == 'y' && retryname[6] == 'p' &&
218 retryname[7] == 'e')
220 error_t err;
221 struct host_basic_info hostinfo;
222 mach_msg_type_number_t hostinfocnt = HOST_BASIC_INFO_COUNT;
223 char *p;
224 /* XXX want client's host */
225 if (err = __host_info (__mach_host_self (), HOST_BASIC_INFO,
226 (natural_t *) &hostinfo,
227 &hostinfocnt))
228 return err;
229 if (hostinfocnt != HOST_BASIC_INFO_COUNT)
230 return EGRATUITOUS;
231 p = _itoa (hostinfo.cpu_subtype, &retryname[8], 10, 0);
232 *--p = '/';
233 p = _itoa (hostinfo.cpu_type, &retryname[8], 10, 0);
234 if (p < retryname)
235 abort (); /* XXX write this right if this ever happens */
236 if (p > retryname)
237 strcpy (retryname, p);
238 startdir = *result;
240 else
241 goto bad_magic;
242 break;
244 case 't':
245 if (retryname[1] == 't' && retryname[2] == 'y')
246 switch (retryname[3])
248 error_t opentty (file_t *result)
250 error_t err;
251 error_t ctty_open (file_t port)
253 if (port == MACH_PORT_NULL)
254 return ENXIO; /* No controlling terminal. */
255 return __termctty_open_terminal (port,
256 flags,
257 result);
259 err = (*use_init_port) (INIT_PORT_CTTYID, &ctty_open);
260 if (! err)
261 err = reauthenticate (*result);
262 return err;
265 case '\0':
266 return opentty (result);
267 case '/':
268 if (err = opentty (&startdir))
269 return err;
270 strcpy (retryname, &retryname[4]);
271 break;
272 default:
273 goto bad_magic;
275 else
276 goto bad_magic;
277 break;
279 default:
280 bad_magic:
281 return EGRATUITOUS;
283 break;
285 default:
286 return EGRATUITOUS;
289 if (startdir != MACH_PORT_NULL)
291 err = lookup (startdir);
292 __mach_port_deallocate (__mach_task_self (), startdir);
293 startdir = MACH_PORT_NULL;
295 else
296 err = (*use_init_port) (dirport, &lookup);
297 } while (! err);
299 return err;
301 weak_alias (__hurd_file_name_lookup_retry, hurd_file_name_lookup_retry)
303 error_t
304 __hurd_file_name_split (error_t (*use_init_port)
305 (int which, error_t (*operate) (file_t)),
306 file_t (*get_dtable_port) (int fd),
307 const char *file_name,
308 file_t *dir, char **name)
310 error_t addref (file_t crdir)
312 *dir = crdir;
313 return __mach_port_mod_refs (__mach_task_self (),
314 crdir, MACH_PORT_RIGHT_SEND, +1);
317 const char *lastslash = strrchr (file_name, '/');
319 if (lastslash != NULL)
321 if (lastslash == file_name)
323 /* "/foobar" => crdir + "foobar". */
324 *name = (char *) file_name + 1;
325 return (*use_init_port) (INIT_PORT_CRDIR, &addref);
327 else
329 /* "/dir1/dir2/.../file". */
330 char dirname[lastslash - file_name + 1];
331 memcpy (dirname, file_name, lastslash - file_name);
332 dirname[lastslash - file_name] = '\0';
333 *name = (char *) lastslash + 1;
334 return __hurd_file_name_lookup (use_init_port, get_dtable_port,
335 dirname, 0, 0, dir);
338 else
340 /* "foobar" => cwdir + "foobar". */
341 *name = (char *) file_name;
342 return (*use_init_port) (INIT_PORT_CWDIR, &addref);
345 weak_alias (__hurd_file_name_split, hurd_file_name_split)
348 file_t
349 __file_name_lookup (const char *file_name, int flags, mode_t mode)
351 error_t err;
352 file_t result;
354 err = __hurd_file_name_lookup (&_hurd_ports_use, &__getdport,
355 file_name, flags, mode,
356 &result);
358 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
360 weak_alias (__file_name_lookup, file_name_lookup)
363 file_t
364 __file_name_split (const char *file_name, char **name)
366 error_t err;
367 file_t result;
369 err = __hurd_file_name_split (&_hurd_ports_use, &__getdport,
370 file_name, &result, name);
372 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
374 weak_alias (__file_name_split, file_name_split)
377 file_t
378 __file_name_lookup_under (file_t startdir,
379 const char *file_name, int flags, mode_t mode)
381 error_t err;
382 file_t result;
384 error_t use_init_port (int which, error_t (*operate) (mach_port_t))
386 return (which == INIT_PORT_CWDIR ? (*operate) (startdir) :
387 _hurd_ports_use (which, operate));
390 err = __hurd_file_name_lookup (&use_init_port, &__getdport,
391 file_name, flags, mode,
392 &result);
394 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
396 weak_alias (__file_name_lookup_under, file_name_lookup_under)