.
[glibc/history.git] / hurd / hurdlookup.c
blobbfee61444e9773789edcb173362362e77da6b3da
1 /* Copyright (C) 1992, 1993, 1994, 1995, 1996 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 error_t (*lookup)
49 (file_t dir, char *name, int flags, mode_t mode,
50 retry_type *do_retry, string_t retry_name,
51 mach_port_t *result),
52 const char *file_name, int flags, mode_t mode,
53 file_t *result)
55 error_t err;
56 enum retry_type doretry;
57 char retryname[1024]; /* XXX string_t LOSES! */
59 error_t lookup_op (mach_port_t startdir)
61 while (file_name[0] == '/')
62 file_name++;
64 return lookup_error ((*lookup) (startdir, file_name, flags, mode,
65 &doretry, retryname, result));
68 if (! lookup)
69 lookup = __dir_lookup;
71 err = (*use_init_port) (file_name[0] == '/'
72 ? INIT_PORT_CRDIR : INIT_PORT_CWDIR,
73 &lookup_op);
74 if (! err)
75 err = __hurd_file_name_lookup_retry (use_init_port, get_dtable_port, lookup,
76 doretry, retryname, flags, mode,
77 result);
79 return err;
81 weak_alias (__hurd_file_name_lookup, hurd_file_name_lookup)
83 error_t
84 __hurd_file_name_lookup_retry (error_t (*use_init_port)
85 (int which, error_t (*operate) (file_t)),
86 file_t (*get_dtable_port) (int fd),
87 error_t (*lookup)
88 (file_t dir, char *name, int flags, mode_t mode,
89 retry_type *do_retry, string_t retry_name,
90 mach_port_t *result),
91 enum retry_type doretry,
92 char retryname[1024],
93 int flags, mode_t mode,
94 file_t *result)
96 error_t err;
97 char *file_name;
98 int nloops;
100 error_t lookup_op (file_t startdir)
102 while (file_name[0] == '/')
103 file_name++;
105 return lookup_error ((*lookup) (startdir, file_name, flags, mode,
106 &doretry, retryname, result));
108 error_t reauthenticate (file_t unauth)
110 error_t err;
111 mach_port_t ref = __mach_reply_port ();
112 error_t reauth (auth_t auth)
114 return __auth_user_authenticate (auth, ref,
115 MACH_MSG_TYPE_MAKE_SEND,
116 result);
118 err = __io_reauthenticate (unauth, ref, MACH_MSG_TYPE_MAKE_SEND);
119 if (! err)
120 err = (*use_init_port) (INIT_PORT_AUTH, &reauth);
121 __mach_port_destroy (__mach_task_self (), ref);
122 __mach_port_deallocate (__mach_task_self (), unauth);
123 return err;
126 if (! lookup)
127 lookup = __dir_lookup;
129 nloops = 0;
130 err = 0;
133 file_t startdir = MACH_PORT_NULL;
134 int dirport = INIT_PORT_CWDIR;
136 switch (doretry)
138 case FS_RETRY_REAUTH:
139 if (err = reauthenticate (*result))
140 return err;
141 /* Fall through. */
143 case FS_RETRY_NORMAL:
144 #ifdef SYMLOOP_MAX
145 if (nloops++ >= SYMLOOP_MAX)
146 return ELOOP;
147 #endif
149 /* An empty RETRYNAME indicates we have the final port. */
150 if (retryname[0] == '\0' &&
151 /* If reauth'd, we must do one more retry on "" to give the new
152 translator a chance to make a new port for us. */
153 doretry == FS_RETRY_NORMAL)
155 /* We got a successful translation. Now apply any open-time
156 action flags we were passed. */
157 if (flags & O_EXLOCK)
158 ; /* XXX */
159 if (!err && (flags & O_SHLOCK))
160 ; /* XXX */
161 if (!err && (flags & O_TRUNC))
162 err = __file_set_size (*result, 0);
164 if (err)
165 __mach_port_deallocate (__mach_task_self (), *result);
166 return err;
169 startdir = *result;
170 file_name = retryname;
171 break;
173 case FS_RETRY_MAGICAL:
174 switch (retryname[0])
176 case '/':
177 dirport = INIT_PORT_CRDIR;
178 if (*result != MACH_PORT_NULL)
179 __mach_port_deallocate (__mach_task_self (), *result);
180 file_name = &retryname[1];
181 break;
183 case 'f':
184 if (retryname[1] == 'd' && retryname[2] == '/')
186 int fd;
187 char *end;
188 int save = errno;
189 errno = 0;
190 fd = (int) strtol (&retryname[3], &end, 10);
191 if (end == NULL || errno || /* Malformed number. */
192 /* Check for excess text after the number. A slash
193 is valid; it ends the component. Anything else
194 does not name a numeric file descriptor. */
195 (*end != '/' && *end != '\0'))
197 errno = save;
198 return ENOENT;
200 if (! get_dtable_port)
201 err = EGRATUITOUS;
202 else
204 *result = (*get_dtable_port) (fd);
205 if (*result == MACH_PORT_NULL)
207 /* If the name was a proper number, but the file
208 descriptor does not exist, we return EBADF instead
209 of ENOENT. */
210 err = errno;
211 errno = save;
214 errno = save;
215 if (err)
216 return err;
217 if (*end == '\0')
218 return 0;
219 else
221 /* Do a normal retry on the remaining components. */
222 startdir = *result;
223 file_name = end + 1; /* Skip the slash. */
224 break;
227 else
228 goto bad_magic;
229 break;
231 case 'm':
232 if (retryname[1] == 'a' && retryname[2] == 'c' &&
233 retryname[3] == 'h' && retryname[4] == 't' &&
234 retryname[5] == 'y' && retryname[6] == 'p' &&
235 retryname[7] == 'e')
237 error_t err;
238 struct host_basic_info hostinfo;
239 mach_msg_type_number_t hostinfocnt = HOST_BASIC_INFO_COUNT;
240 char *p;
241 /* XXX want client's host */
242 if (err = __host_info (__mach_host_self (), HOST_BASIC_INFO,
243 (natural_t *) &hostinfo,
244 &hostinfocnt))
245 return err;
246 if (hostinfocnt != HOST_BASIC_INFO_COUNT)
247 return EGRATUITOUS;
248 p = _itoa (hostinfo.cpu_subtype, &retryname[8], 10, 0);
249 *--p = '/';
250 p = _itoa (hostinfo.cpu_type, &retryname[8], 10, 0);
251 if (p < retryname)
252 abort (); /* XXX write this right if this ever happens */
253 if (p > retryname)
254 strcpy (retryname, p);
255 startdir = *result;
257 else
258 goto bad_magic;
259 break;
261 case 't':
262 if (retryname[1] == 't' && retryname[2] == 'y')
263 switch (retryname[3])
265 error_t opentty (file_t *result)
267 error_t err;
268 error_t ctty_open (file_t port)
270 if (port == MACH_PORT_NULL)
271 return ENXIO; /* No controlling terminal. */
272 return __termctty_open_terminal (port,
273 flags,
274 result);
276 err = (*use_init_port) (INIT_PORT_CTTYID, &ctty_open);
277 if (! err)
278 err = reauthenticate (*result);
279 return err;
282 case '\0':
283 return opentty (result);
284 case '/':
285 if (err = opentty (&startdir))
286 return err;
287 strcpy (retryname, &retryname[4]);
288 break;
289 default:
290 goto bad_magic;
292 else
293 goto bad_magic;
294 break;
296 default:
297 bad_magic:
298 return EGRATUITOUS;
300 break;
302 default:
303 return EGRATUITOUS;
306 if (startdir != MACH_PORT_NULL)
308 err = lookup_op (startdir);
309 __mach_port_deallocate (__mach_task_self (), startdir);
310 startdir = MACH_PORT_NULL;
312 else
313 err = (*use_init_port) (dirport, &lookup_op);
314 } while (! err);
316 return err;
318 weak_alias (__hurd_file_name_lookup_retry, hurd_file_name_lookup_retry)
320 error_t
321 __hurd_file_name_split (error_t (*use_init_port)
322 (int which, error_t (*operate) (file_t)),
323 file_t (*get_dtable_port) (int fd),
324 error_t (*lookup)
325 (file_t dir, char *name, int flags, mode_t mode,
326 retry_type *do_retry, string_t retry_name,
327 mach_port_t *result),
328 const char *file_name,
329 file_t *dir, char **name)
331 error_t addref (file_t crdir)
333 *dir = crdir;
334 return __mach_port_mod_refs (__mach_task_self (),
335 crdir, MACH_PORT_RIGHT_SEND, +1);
338 const char *lastslash = strrchr (file_name, '/');
340 if (lastslash != NULL)
342 if (lastslash == file_name)
344 /* "/foobar" => crdir + "foobar". */
345 *name = (char *) file_name + 1;
346 return (*use_init_port) (INIT_PORT_CRDIR, &addref);
348 else
350 /* "/dir1/dir2/.../file". */
351 char dirname[lastslash - file_name + 1];
352 memcpy (dirname, file_name, lastslash - file_name);
353 dirname[lastslash - file_name] = '\0';
354 *name = (char *) lastslash + 1;
355 return
356 __hurd_file_name_lookup (use_init_port, get_dtable_port, lookup,
357 dirname, 0, 0, dir);
360 else
362 /* "foobar" => cwdir + "foobar". */
363 *name = (char *) file_name;
364 return (*use_init_port) (INIT_PORT_CWDIR, &addref);
367 weak_alias (__hurd_file_name_split, hurd_file_name_split)
370 file_t
371 __file_name_lookup (const char *file_name, int flags, mode_t mode)
373 error_t err;
374 file_t result;
376 err = __hurd_file_name_lookup (&_hurd_ports_use, &__getdport, 0,
377 file_name, flags, mode & ~_hurd_umask,
378 &result);
380 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
382 weak_alias (__file_name_lookup, file_name_lookup)
385 file_t
386 __file_name_split (const char *file_name, char **name)
388 error_t err;
389 file_t result;
391 err = __hurd_file_name_split (&_hurd_ports_use, &__getdport, 0,
392 file_name, &result, name);
394 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
396 weak_alias (__file_name_split, file_name_split)
399 file_t
400 __file_name_lookup_under (file_t startdir,
401 const char *file_name, int flags, mode_t mode)
403 error_t err;
404 file_t result;
406 error_t use_init_port (int which, error_t (*operate) (mach_port_t))
408 return (which == INIT_PORT_CWDIR ? (*operate) (startdir) :
409 _hurd_ports_use (which, operate));
412 err = __hurd_file_name_lookup (&use_init_port, &__getdport, 0,
413 file_name, flags, mode & ~_hurd_umask,
414 &result);
416 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
418 weak_alias (__file_name_lookup_under, file_name_lookup_under)