]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/mach/hurd/chroot.c
Update.
[thirdparty/glibc.git] / sysdeps / mach / hurd / chroot.c
CommitLineData
db11c38c 1/* Copyright (C) 1991,92,93,94,95,97,99,2001 Free Software Foundation, Inc.
ebbad4cc 2 This file is part of the GNU C Library.
28f540f4 3
ebbad4cc
UD
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.
28f540f4 8
ebbad4cc
UD
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.
28f540f4 13
ebbad4cc
UD
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 not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
28f540f4 18
db11c38c 19#include <string.h>
28f540f4 20#include <unistd.h>
db11c38c 21
28f540f4 22#include <hurd.h>
28f540f4
RM
23#include <hurd/port.h>
24
db11c38c
MK
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. */
28f540f4 28int
db11c38c 29chroot (const char *path)
28f540f4 30{
db11c38c
MK
31 const char *lookup;
32 size_t len;
33 file_t dir, root;
34 error_t err;
35
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 (path[len - 2] == '/' && path[len - 1] == '.')
40 lookup = path;
41 else
42 {
43 char *n = alloca (len + 2);
44 memcpy (n, path, len);
45 n[len] = '/';
46 n[len + 1] = '.';
47 n[len + 2] = '\0';
48 lookup = n;
49 }
50
51 dir = __file_name_lookup (lookup, 0, 0);
52 if (dir == MACH_PORT_NULL)
53 return -1;
54
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);
60
61 _hurd_port_set (&_hurd_ports[INIT_PORT_CRDIR], root);
62 return 0;
28f540f4 63}