2.9
[glibc/nacl-glibc.git] / sysdeps / mach / hurd / chroot.c
blobfde01647137e7ddbe44bdbc6f1a48c60a693e8fd
1 /* Copyright (C) 1991,92,93,94,95,97,99,2001 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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <string.h>
20 #include <unistd.h>
22 #include <hurd.h>
23 #include <hurd/port.h>
25 /* Make PATH be the root directory (the starting point for absolute
26 paths). Note that while on traditional UNIX systems this call is
27 restricted to the super-user, it isn't on the Hurd. */
28 int
29 chroot (const char *path)
31 const char *lookup;
32 size_t len;
33 file_t dir, root;
34 error_t err;
36 /* Append trailing "/." to directory name to force ENOTDIR if it's not a
37 directory and EACCES if we don't have search permission. */
38 len = strlen (path);
39 if (len >= 2 && path[len - 2] == '/' && path[len - 1] == '.')
40 lookup = path;
41 else
43 char *n = alloca (len + 3);
44 memcpy (n, path, len);
45 n[len] = '/';
46 n[len + 1] = '.';
47 n[len + 2] = '\0';
48 lookup = n;
51 dir = __file_name_lookup (lookup, 0, 0);
52 if (dir == MACH_PORT_NULL)
53 return -1;
55 /* Prevent going through DIR's .. */
56 err = __file_reparent (dir, MACH_PORT_NULL, &root);
57 __mach_port_deallocate (__mach_task_self (), dir);
58 if (err)
59 return __hurd_fail (err);
61 _hurd_port_set (&_hurd_ports[INIT_PORT_CRDIR], root);
62 return 0;