Prerequisites for chdir at Init() time.

Jonathan Brandmeyer jbrandmeyer at planetiq.com
Wed Aug 21 19:34:04 UTC 2019


Are there any special pre-requsites to using the chdir system call
effectively in RTEMS?

I've got a function which unpacks a tarred up filesystem by chdir into
a chosen directory, unpacking the filesystem, and then chdir() back to
"/".  If I call this function from the RTEMS shell, everything behaves
normally, and the files appear relative to the path I cd'ed into.
However, if I call it directly from Init(), it acts strangely.  The
chdir() call reports success, and subsequent call to getcwd() returns
the directory that I chdir'ed into.  However, the tarball gets
unpacked relative to the filesystem root.

chdir("/mnt/somepath") appears to succeed.  A subsequent call to
getcwd() also returns the directory cd'ed into.

However, the subsequent unpacking of the tarball does so into the root.

I did some digging into the shell initialization code, and saw that
`rtems_shell_init_user_env()` is constructing a private user
environment for the shell thread.  In the process of doing so, it
makes a suspicious system call: chroot("/").

Sure enough, adding chroot("/") early enough in my POSIX_Init() allows
subsequent chdir operations to have the right side-effects on creat()
and open() calls.

What's up with that?  Is it intentional?  Or is the shell's chroot()
covering up some deficiency in the way that the
`rtems_global_user_env` is getting initialized when the IMFS rootfs is
set up?

Thanks,
-- 
Jonathan Brandmeyer


More information about the users mailing list