1 | /* |
---|
2 | * chroot() - Change Root Directory |
---|
3 | * Author: fernando.ruiz@ctv.es |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989-2008. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * |
---|
8 | * Modifications to support reference counting in the file system are |
---|
9 | * Copyright (c) 2012 embedded brains GmbH. |
---|
10 | * |
---|
11 | * The license and distribution terms for this file may be |
---|
12 | * found in the file LICENSE in this distribution or at |
---|
13 | * http://www.rtems.com/license/LICENSE. |
---|
14 | */ |
---|
15 | |
---|
16 | #if HAVE_CONFIG_H |
---|
17 | #include "config.h" |
---|
18 | #endif |
---|
19 | |
---|
20 | #include <unistd.h> |
---|
21 | |
---|
22 | #include <rtems/libio_.h> |
---|
23 | |
---|
24 | int chroot( const char *path ) |
---|
25 | { |
---|
26 | int rv = 0; |
---|
27 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
28 | rtems_filesystem_eval_path_context_t ctx; |
---|
29 | int eval_flags = RTEMS_FS_PERMS_EXEC |
---|
30 | | RTEMS_FS_FOLLOW_LINK; |
---|
31 | rtems_filesystem_location_info_t loc; |
---|
32 | rtems_filesystem_global_location_t *new_current_loc; |
---|
33 | |
---|
34 | /* |
---|
35 | * We use the global environment for path evaluation. This makes it possible |
---|
36 | * to escape from a chroot environment referencing an unmounted file system. |
---|
37 | */ |
---|
38 | rtems_filesystem_eval_path_start_with_root_and_current( |
---|
39 | &ctx, |
---|
40 | path, |
---|
41 | eval_flags, |
---|
42 | &rtems_global_user_env.root_directory, |
---|
43 | &rtems_global_user_env.current_directory |
---|
44 | ); |
---|
45 | |
---|
46 | rtems_filesystem_eval_path_extract_currentloc( &ctx, &loc ); |
---|
47 | new_current_loc = rtems_filesystem_location_transform_to_global( &loc ); |
---|
48 | if ( !rtems_filesystem_global_location_is_null( new_current_loc ) ) { |
---|
49 | rtems_filesystem_global_location_t *new_root_loc = |
---|
50 | rtems_filesystem_global_location_obtain( &new_current_loc ); |
---|
51 | rtems_filesystem_node_types_t type = |
---|
52 | (*new_root_loc->location.mt_entry->ops->node_type_h)( |
---|
53 | &new_root_loc->location |
---|
54 | ); |
---|
55 | |
---|
56 | if ( type == RTEMS_FILESYSTEM_DIRECTORY ) { |
---|
57 | sc = rtems_libio_set_private_env(); |
---|
58 | if (sc == RTEMS_SUCCESSFUL) { |
---|
59 | rtems_filesystem_global_location_assign( |
---|
60 | &rtems_filesystem_root, |
---|
61 | new_root_loc |
---|
62 | ); |
---|
63 | rtems_filesystem_global_location_assign( |
---|
64 | &rtems_filesystem_current, |
---|
65 | new_current_loc |
---|
66 | ); |
---|
67 | } else { |
---|
68 | if (sc != RTEMS_UNSATISFIED) { |
---|
69 | errno = ENOMEM; |
---|
70 | } |
---|
71 | rv = -1; |
---|
72 | } |
---|
73 | } else { |
---|
74 | rtems_filesystem_location_error( &new_root_loc->location, ENOTDIR ); |
---|
75 | rv = -1; |
---|
76 | } |
---|
77 | |
---|
78 | if ( rv != 0 ) { |
---|
79 | rtems_filesystem_global_location_release( new_root_loc ); |
---|
80 | } |
---|
81 | } else { |
---|
82 | rv = -1; |
---|
83 | } |
---|
84 | |
---|
85 | rtems_filesystem_eval_path_cleanup( &ctx ); |
---|
86 | |
---|
87 | if ( rv != 0 ) { |
---|
88 | rtems_filesystem_global_location_release( new_current_loc ); |
---|
89 | } |
---|
90 | |
---|
91 | return rv; |
---|
92 | } |
---|