source: rtems/cpukit/libcsupport/src/chroot.c @ a7d1992c

Last change on this file since a7d1992c was da154e14, checked in by Sebastian Huber <sebastian.huber@…>, on 05/14/12 at 14:55:41

Filesystem: Move operations to mount table entry

The scope of the file system operations is the file system instance.
The scope of the file system node handlers is the file location. The
benefit of moving the operations to the mount table entry is a size
reduction of the file location (rtems_filesystem_location_info_t). The
code size is slightly increased due to additional load instructions.

Restructure rtems_filesystem_mount_table_entry_t to improve cache
efficiency.

  • Property mode set to 100644
File size: 2.5 KB
Line 
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
24int 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}
Note: See TracBrowser for help on using the repository browser.