Ticket #339: chroot_nodeclone.patch

File chroot_nodeclone.patch, 3.0 KB (added by strauman, on Dec 3, 2006 at 1:31:12 PM)

chroot_nodeclone.patch

Line 
1Index: chroot.c
2===================================================================
3RCS file: /afs/slac/g/spear/cvsrep/rtems/src/c/src/lib/libc/chroot.c,v
4retrieving revision 1.1.1.2
5diff -c -r1.1.1.2 chroot.c
6*** chroot.c    7 Mar 2002 01:53:47 -0000       1.1.1.2
7--- chroot.c    8 Nov 2002 01:50:00 -0000
8***************
9*** 38,51 ****
10      rtems_set_errno_and_return_minus_one( ENOTSUP );
11    };
12 
13-   loc = rtems_filesystem_root;     /* save the value */
14-
15    result = chdir(pathname);
16    if (result) {
17-     rtems_filesystem_root = loc; /* restore the value */
18      rtems_set_errno_and_return_minus_one( errno );
19    };
20!   rtems_filesystem_root = rtems_filesystem_current;
21 
22    return 0;
23  }
24--- 38,54 ----
25      rtems_set_errno_and_return_minus_one( ENOTSUP );
26    };
27 
28    result = chdir(pathname);
29    if (result) {
30      rtems_set_errno_and_return_minus_one( errno );
31    };
32!   /* clone the new root location */
33!   if (rtems_filesystem_evaluate_path(".", 0, &loc, 0)) {
34!       /* our cwd has changed, though - but there is no easy way of return :-( */
35!     rtems_set_errno_and_return_minus_one( errno );
36!   }
37!   rtems_filesystem_freenode(&rtems_filesystem_root);
38!   rtems_filesystem_root = loc;
39 
40    return 0;
41  }
42Index: fchdir.c
43===================================================================
44RCS file: /afs/slac/g/spear/cvsrep/rtems/src/c/src/lib/libc/fchdir.c,v
45retrieving revision 1.1.1.2
46diff -c -r1.1.1.2 fchdir.c
47*** fchdir.c    7 Mar 2002 01:53:48 -0000       1.1.1.2
48--- fchdir.c    8 Nov 2002 02:15:57 -0000
49***************
50*** 29,34 ****
51--- 29,35 ----
52  )
53  {
54    rtems_libio_t *iop;
55+   rtems_filesystem_location_info_t loc, saved;
56   
57    rtems_libio_check_fd( fd );
58    iop = rtems_libio_iop( fd );
59***************
60*** 57,63 ****
61      rtems_set_errno_and_return_minus_one( ENOTDIR );
62    }
63   
64-   rtems_filesystem_freenode( &rtems_filesystem_current );
65 
66    /*
67     * FIXME : I feel there should be another call to
68--- 58,63 ----
69***************
70*** 65,74 ****
71     *         this node which we are making here. I can
72     *         see the freenode interface but do not see
73     *         allocnode node interface. It maybe node_type.
74     */
75   
76    rtems_filesystem_current = iop->pathinfo;
77   
78    return 0;
79  }
80-
81--- 65,90 ----
82     *         this node which we are making here. I can
83     *         see the freenode interface but do not see
84     *         allocnode node interface. It maybe node_type.
85+    *
86+    * FIXED:  T.Straumann: it is evaluate_path()
87+    *         but note the race condition. Threads who
88+    *         share their rtems_filesystem_current better
89+    *         be synchronized!
90     */
91   
92+   saved                    = rtems_filesystem_current;
93    rtems_filesystem_current = iop->pathinfo;
94   
95+   /* clone the current node */
96+   if (rtems_filesystem_evaluate_path(".", 0, &loc, 0)) {
97+     /* cloning failed; restore original and bail out */
98+     rtems_filesystem_current = saved;
99+       return -1;
100+   }
101+   /* release the old one */
102+   rtems_filesystem_freenode( &saved );
103+
104+   rtems_filesystem_current = loc;
105+   
106    return 0;
107  }