Subject: Re: fchroot(2) emulation ?
To: Darren Reed <darrenr@cyber.com.au>
From: Todd Vierling <tv@pobox.com>
List: port-sparc
Date: 12/12/1997 08:40:37
On Fri, 12 Dec 1997, Darren Reed wrote:

: Given there is no fchroot(2) in NetBSD, how does NetBSD emulate this
: system call ?
: 
: Is there a case for adding fchroot(2) to NetBSD ?

As a quick fix:

    fchroot(fd);
becomes
    fchdir(fd); chroot(".");

but you will lose the directory you are currently in (cwd becomes the new
root).  Alternatively, you can use the following function, which will save
and restore the current directory.

[-current users note:  I've seen fchroot on other OS'es; does the below
function look sane enough to go in libcompat?  It certainly doesn't *need*
to be a syscall as far as I can tell.]

=====

int fchroot(int fd) {
	int cfd;
	int rc = -1;

	if ((cfd = open(".", O_RDONLY)) >= 0) {
		if (fchdir(fd) == 0) {
			if (chroot(".") == 0)
				rc = 0;
			fchdir(cfd);
		}
		close(cfd);
	}
	return rc;
}

=====
== Todd Vierling (Personal tv@pobox.com; Business tv@lucent.com)
== Vierling's Axiom: The revolution won't be televised; it will be posted.